|
From: <ev...@us...> - 2010-10-13 19:07:45
|
Revision: 1450
http://rails.svn.sourceforge.net/rails/?rev=1450&view=rev
Author: evos
Date: 2010-10-13 19:07:38 +0000 (Wed, 13 Oct 2010)
Log Message:
-----------
1. Reset Autopass if the player gets over a share/certificate limit during a Stock Round.
2. Skip Treasury trading step if nothing can be done.
Modified Paths:
--------------
trunk/18xx/rails/game/Game.java
trunk/18xx/rails/game/GameManager.java
trunk/18xx/rails/game/GameManagerI.java
trunk/18xx/rails/game/OperatingRound.java
trunk/18xx/rails/game/StockRound.java
Modified: trunk/18xx/rails/game/Game.java
===================================================================
--- trunk/18xx/rails/game/Game.java 2010-10-12 18:50:32 UTC (rev 1449)
+++ trunk/18xx/rails/game/Game.java 2010-10-13 19:07:38 UTC (rev 1450)
@@ -261,6 +261,8 @@
log.debug("Starting to execute loaded actions");
+ gameManager.setReloading(true);
+
Object actionObject = null;
while (true) { // Single-pass loop.
try {
@@ -299,7 +301,7 @@
}
break;
}
-
+
// load user comments (is the last
if (actionObject instanceof SortedMap) {
ReportBuffer.setCommentItems((SortedMap<Integer, String>) actionObject);
@@ -316,10 +318,11 @@
// but also the java.io.StreamCorruptedException: invalid type code
}
}
-
+
ois.close();
- game.getGameManager().finishLoading();
+ gameManager.setReloading(false);
+ gameManager.finishLoading();
return game;
} catch (Exception e) {
Modified: trunk/18xx/rails/game/GameManager.java
===================================================================
--- trunk/18xx/rails/game/GameManager.java 2010-10-12 18:50:32 UTC (rev 1449)
+++ trunk/18xx/rails/game/GameManager.java 2010-10-13 19:07:38 UTC (rev 1450)
@@ -89,6 +89,9 @@
protected boolean dynamicOperatingOrder = true;
+ /** Will only be set during game reload */
+ protected boolean reloading = false;
+
protected EnumMap<GameDef.Parm, Object> gameParameters
= new EnumMap<GameDef.Parm, Object>(GameDef.Parm.class);
@@ -198,6 +201,14 @@
/** indicates that the recoverySave already issued a warning, avoids displaying several warnings */
protected boolean recoverySaveWarning = true;
+ /** Flag to skip a subsequent Done action (if present) during reloading.
+ * <br>This is a fix to maintain backwards compatibility when redundant
+ * actions are skipped in new code versions (such as the bypassing of
+ * a treasury trading step if it cannot be executed).
+ * <br>This flag will be reset after processing <i>any</i> action (not just Done).
+ */
+ protected boolean skipNextDone = false;
+
protected static Logger log =
Logger.getLogger(GameManager.class.getPackage().getName());
@@ -938,7 +949,8 @@
public boolean processOnReload(PossibleAction action) throws Exception {
DisplayBuffer.clear();
- // TEMPORARY FIX TO ALLOW OLD 1856 SAVED FILES TO BE PROCESSED
+
+ // XXX TEMPORARY FIX TO ALLOW OLD 1856 SAVED FILES TO BE PROCESSED
if (gameName.equals("1856")
&& possibleActions.contains(RepayLoans.class)
&& (!possibleActions.contains(action.getClass())
@@ -954,7 +966,20 @@
try {
log.debug("Action ("+action.getPlayerName()+"): " + action);
- if (!processCorrectionActions(action) && !getCurrentRound().process(action)) {
+
+ // XXX FOR BACKWARDS COMPATIBILITY
+ boolean doProcess = true;
+ log.debug("SkipNextDone="+skipNextDone);
+ if (skipNextDone) {
+ if (action instanceof NullAction
+ && ((NullAction)action).getMode() == NullAction.DONE) {
+ log.info("Skipping processing a Done action during reload");
+ doProcess = false;
+ }
+ }
+ skipNextDone = false;
+
+ if (doProcess && !processCorrectionActions(action) && !getCurrentRound().process(action)) {
String msg = "Player "+action.getPlayerName()+"\'s action \""
+action.toString()+"\"\n in "+getCurrentRound().getRoundName()
+" is considered invalid by the game engine";
@@ -1684,5 +1709,16 @@
return new ArrayList<PublicCompanyI>(operatingCompanies.values());
}
+ public boolean isReloading() {
+ return reloading;
+ }
+
+ public void setReloading(boolean reloading) {
+ this.reloading = reloading;
+ }
+
+ public void setSkipDone () {
+ skipNextDone = true;
+ }
}
Modified: trunk/18xx/rails/game/GameManagerI.java
===================================================================
--- trunk/18xx/rails/game/GameManagerI.java 2010-10-12 18:50:32 UTC (rev 1449)
+++ trunk/18xx/rails/game/GameManagerI.java 2010-10-13 19:07:38 UTC (rev 1450)
@@ -205,5 +205,7 @@
public CorrectionManagerI getCorrectionManager(CorrectionType ct);
public List<PublicCompanyI> getCompaniesInRunningOrder ();
-
+ public boolean isReloading();
+ public void setReloading(boolean reloading);
+ public void setSkipDone ();
}
\ No newline at end of file
Modified: trunk/18xx/rails/game/OperatingRound.java
===================================================================
--- trunk/18xx/rails/game/OperatingRound.java 2010-10-12 18:50:32 UTC (rev 1449)
+++ trunk/18xx/rails/game/OperatingRound.java 2010-10-13 19:07:38 UTC (rev 1450)
@@ -247,8 +247,8 @@
NullAction nullAction = (NullAction) action;
switch (nullAction.getMode()) {
+ case NullAction.DONE:
case NullAction.PASS:
- case NullAction.DONE:
result = done();
break;
case NullAction.SKIP:
@@ -1201,6 +1201,9 @@
/** Take the next step after a given one (see nextStep()) */
protected void nextStep(GameDef.OrStep step) {
+
+ PublicCompanyI company = operatingCompany.get();
+
// Cycle through the steps until we reach one where a user action is
// expected.
int stepIndex;
@@ -1212,13 +1215,13 @@
log.debug("Step " + step);
if (step == GameDef.OrStep.LAY_TOKEN
- && operatingCompany.get().getNumberOfFreeBaseTokens() == 0) {
+ && company.getNumberOfFreeBaseTokens() == 0) {
continue;
}
if (step == GameDef.OrStep.CALC_REVENUE) {
- if (!operatingCompany.get().canRunTrains()) {
+ if (!company.canRunTrains()) {
// No trains, then the revenue is zero.
executeSetRevenueAndDividend (
new SetDividend (0, false, new int[] {SetDividend.NO_TRAIN}));
@@ -1235,11 +1238,40 @@
if (step == GameDef.OrStep.TRADE_SHARES) {
// Is company allowed to trade trasury shares?
- if (!operatingCompany.get().mayTradeShares()
- || !operatingCompany.get().hasOperated()) {
+ if (!company.mayTradeShares()
+ || !company.hasOperated()) {
continue;
}
+ /* Check if any trading is possible.
+ * If not, skip this step.
+ * (but register a Done action for BACKWARDS COMPATIBILITY only)
+ */
+ // Preload some expensive results
+ int ownShare = company.getPortfolio().getShare(company);
+ int poolShare = pool.getShare(company); // Expensive, do it once
+ // Can it buy?
+ boolean canBuy =
+ ownShare < getGameParameterAsInt (GameDef.Parm.TREASURY_SHARE_LIMIT)
+ && company.getCash() >= company.getCurrentSpace().getPrice()
+ && poolShare > 0;
+ // Can it sell?
+ boolean canSell =
+ company.getPortfolio().getShare(company) > 0
+ && poolShare < getGameParameterAsInt (GameDef.Parm.POOL_SHARE_LIMIT);
+ // Above we ignore the possible existence of double shares (as in 1835).
+
+ if (!canBuy && !canSell) {
+ // XXX For BACKWARDS COMPATIBILITY only,
+ // register a Done skip action during reloading.
+ if (gameManager.isReloading()) {
+ gameManager.setSkipDone();
+ log.debug("If the next saved action is 'Done', skip it");
+ }
+ log.info("Skipping Treasury share trading step");
+ continue;
+ }
+
gameManager.startTreasuryShareTradingRound();
}
Modified: trunk/18xx/rails/game/StockRound.java
===================================================================
--- trunk/18xx/rails/game/StockRound.java 2010-10-12 18:50:32 UTC (rev 1449)
+++ trunk/18xx/rails/game/StockRound.java 2010-10-13 19:07:38 UTC (rev 1450)
@@ -1365,8 +1365,13 @@
setNextPlayer();
sellPrices.clear();
if (hasAutopassed(currentPlayer)) {
- // Process a pass for a player that has set Autopass
- done (currentPlayer.getName(), true);
+ if (isPlayerOverLimits(currentPlayer)) {
+ // Being over a share/certificate limit undoes an Autopass setting
+ setAutopass (currentPlayer, false);
+ } else {
+ // Process a pass for a player that has set Autopass
+ done (currentPlayer.getName(), true);
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|