From: Erik V. <ev...@us...> - 2009-02-04 20:36:52
|
Update of /cvsroot/rails/18xx/rails/game/specific/_1856 In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv4948/rails/game/specific/_1856 Modified Files: OperatingRound_1856.java Added Files: CGRFormationRound.java GameManager_1856.java Log Message: CGR Formation round phase 1: repaying loans. --- NEW FILE: CGRFormationRound.java --- package rails.game.specific._1856; import java.util.*; import rails.game.*; import rails.game.action.RepayLoans; import rails.game.move.CashMove; import rails.util.LocalText; public class CGRFormationRound extends OperatingRound { /* This isn't really a stock round, but it must subclass one of the * three base types, otherwise the not-subclassable GameUIManager * cannot handle it. StockRound has been chosen because the UI * should show the StatusWindow. */ private Player startingPlayer; private Map<Player, List<PublicCompanyI>> companiesToRepayLoans = null; private PublicCompanyI currentCompany = null; private int maxLoansToRepayByPresident = 0; private List<PublicCompanyI> mergingCompanies = new ArrayList<PublicCompanyI>(); public CGRFormationRound (GameManagerI gameManager) { super (gameManager); } @Override /** This class needs the game status window to show up * rather than the operating round window. */ public Class<? extends RoundI> getRoundTypeForUI () { return StockRound.class; } public void start (Player startingPlayer) { this.startingPlayer = startingPlayer; Player president; companiesToRepayLoans = null; ReportBuffer.add(LocalText.getText("StartCGRFormationRound", startingPlayer.getName())); // Collect companies having loans for (PublicCompanyI company : getOperatingCompanies()) { if (company.getCurrentNumberOfLoans() > 0) { if (companiesToRepayLoans == null) { companiesToRepayLoans = new HashMap<Player, List<PublicCompanyI>>(); } president = company.getPresident(); if (!companiesToRepayLoans.containsKey(president)) { companiesToRepayLoans.put (president, new ArrayList<PublicCompanyI>()); } companiesToRepayLoans.get(president).add(company); } } setCurrentPlayer (startingPlayer); setNextCompanyNeedingPresidentIntervention(); } private boolean setNextCompanyNeedingPresidentIntervention () { while (true) { while (!companiesToRepayLoans.containsKey(getCurrentPlayer())) { gameManager.setNextPlayer(); if (getCurrentPlayer().equals(startingPlayer)) { return false; } } // Player to act already has been selected Player player = getCurrentPlayer(); if (companiesToRepayLoans.get(player).isEmpty()) { companiesToRepayLoans.remove(player); continue; } currentCompany = companiesToRepayLoans.get(player).get(0); companiesToRepayLoans.get(player).remove(currentCompany); int numberOfLoans = currentCompany.getCurrentNumberOfLoans(); if (numberOfLoans == 0) continue; int compCash = currentCompany.getCash(); int presCash = player.getCash(); int valuePerLoan = currentCompany.getValuePerLoan(); String message; int payment; message = LocalText.getText("CompanyHasLoans", currentCompany.getName(), player.getName(), numberOfLoans, Bank.format(valuePerLoan), Bank.format(numberOfLoans * valuePerLoan)); ReportBuffer.add(message); DisplayBuffer.add(message, false); // Let company repay all loans for which it has the cash int numberToRepay = Math.min(numberOfLoans, compCash / valuePerLoan); if (numberToRepay > 0) { payment = numberToRepay * valuePerLoan; new CashMove (currentCompany, null, payment); currentCompany.addLoans(-numberToRepay); message = LocalText.getText("CompanyRepaysLoans", currentCompany.getName(), Bank.format(payment), Bank.format(numberOfLoans * valuePerLoan), numberToRepay, Bank.format(valuePerLoan)); ReportBuffer.add (message); DisplayBuffer.add(message, false); } // If that was all, we're done with this company numberOfLoans = currentCompany.getCurrentNumberOfLoans(); if (numberOfLoans == 0) continue; // Check the president's cash // He should be involved if at least one extra loan could be repaid compCash = currentCompany.getCash(); if ((compCash + presCash) / valuePerLoan > 0) { int maxNumber = Math.min((compCash + presCash)/valuePerLoan, numberOfLoans); if (maxNumber == numberOfLoans) { DisplayBuffer.add(LocalText.getText("YouCanRepayAllLoans", player.getName(), maxNumber, currentCompany.getName()), false); } else { DisplayBuffer.add(LocalText.getText("YouCannotRepayAllLoans", player.getName(), maxNumber, numberOfLoans, currentCompany.getName()), false); } maxLoansToRepayByPresident = maxNumber; break; } else { // President cannot help, this company will merge into CGR anyway mergingCompanies.add(currentCompany); message = LocalText.getText("WillMergeInto", currentCompany.getName(), "CGR"); DisplayBuffer.add(message, false); ReportBuffer.add(message); continue; } } return true; } @Override public boolean setPossibleActions() { RepayLoans action = new RepayLoans (currentCompany, 0, maxLoansToRepayByPresident, currentCompany.getValuePerLoan()); possibleActions.add(action); operatingCompany = currentCompany; return true; } @Override protected boolean repayLoans (RepayLoans action) { boolean result = super.repayLoans(action); if (action.getCompany().getCurrentNumberOfLoans() > 0) { mergingCompanies.add(currentCompany); String message = LocalText.getText("WillMergeInto", currentCompany.getName(), "CGR"); DisplayBuffer.add(message, true); ReportBuffer.add(message); } if (!setNextCompanyNeedingPresidentIntervention()) { gameManager.nextRound(this); } return result; } @Override public String toString() { return "1856 CGRFormationRound"; } } Index: OperatingRound_1856.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/specific/_1856/OperatingRound_1856.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** OperatingRound_1856.java 24 Jan 2009 15:09:14 -0000 1.7 --- OperatingRound_1856.java 4 Feb 2009 20:36:39 -0000 1.8 *************** *** 12,19 **** public class OperatingRound_1856 extends OperatingRound { ! ! BooleanState finalLoanRepaymentPending = new BooleanState ("LoanRepaymentPending", false); ! Player playerToStartLoanRepayment = null; public static final int STEP_REPAY_LOANS = 6; --- 12,19 ---- public class OperatingRound_1856 extends OperatingRound { ! ! private BooleanState finalLoanRepaymentPending = new BooleanState ("LoanRepaymentPending", false); ! private Player playerToStartLoanRepayment = null; public static final int STEP_REPAY_LOANS = 6; *************** *** 296,299 **** --- 296,300 ---- if (getStep() == STEP_REPAY_LOANS) { + // Has company any outstanding loans to repay? if (operatingCompany.getMaxNumberOfLoans() != 0 *************** *** 329,347 **** } } ! public boolean buyTrain(BuyTrain action) { ! PhaseI prePhase = currentPhase; ! boolean result = super.buyTrain(action); ! PhaseI postPhase = currentPhase; ! if (postPhase != prePhase && postPhase.getName().equals("5")) { finalLoanRepaymentPending.set(true); ! playerToStartLoanRepayment = gameManager.getPlayerByIndex(action.getPlayerIndex()); } ! return result; } --- 330,349 ---- } } ! ! @Override public boolean buyTrain(BuyTrain action) { ! PhaseI prePhase = currentPhase; ! boolean result = super.buyTrain(action); ! PhaseI postPhase = currentPhase; ! if (postPhase != prePhase && postPhase.getName().equals("5")) { finalLoanRepaymentPending.set(true); ! playerToStartLoanRepayment = gameManager.getPlayerByIndex(action.getPlayerIndex()); } ! return result; } *************** *** 419,429 **** return true; } - } else { - // We are not in this step - return true; } } ! protected void finishTurn() { --- 421,447 ---- return true; } } + return true; } ! ! @Override ! public void resume() { ! ! if (savedAction == null) { ! // End of CGRFormationRound ! finalLoanRepaymentPending.set(false); ! if (setNextOperatingCompany(false)) { ! setStep(STEP_INITIAL); ! } else { ! finishOR(); ! } ! } else { ! super.resume(); ! } ! } ! ! ! @Override protected void finishTurn() { *************** *** 435,442 **** priv.checkClosingIfExercised(true); } ! if (finalLoanRepaymentPending.booleanValue()) { ! // Must start final loan repayment and CGR formation ! // TODO } --- 453,461 ---- priv.checkClosingIfExercised(true); } ! if (finalLoanRepaymentPending.booleanValue()) { ! ! ((GameManager_1856)gameManager).startCGRFormationRound(this, playerToStartLoanRepayment); ! return; } *************** *** 447,451 **** } } - - } --- 466,468 ---- --- NEW FILE: GameManager_1856.java --- /* $Header: /cvsroot/rails/18xx/rails/game/specific/_1856/GameManager_1856.java,v 1.1 2009/02/04 20:36:39 evos Exp $ */ package rails.game.specific._1856; import rails.game.*; public class GameManager_1856 extends GameManager { private Player playerToStartCGRFRound = null; public void startCGRFormationRound(OperatingRound_1856 or, Player playerToStartCGRFRound) { this.playerToStartCGRFRound = playerToStartCGRFRound; interruptedRound = or; if (this.playerToStartCGRFRound != null) { createRound (CGRFormationRound.class).start (this.playerToStartCGRFRound); this.playerToStartCGRFRound = null; } } @Override public void nextRound(RoundI round) { if (round instanceof CGRFormationRound) { setRound(interruptedRound); ((OperatingRound_1856)interruptedRound).resume(); } else { super.nextRound(round); } } } |