From: Erik V. <ev...@us...> - 2009-09-02 21:48:01
|
Update of /cvsroot/rails/18xx/rails/game/specific/_1856 In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv24424/rails/game/specific/_1856 Modified Files: PublicCompany_State.java CGRFormationRound.java GameManager_1856.java OperatingRound_1856.java Log Message: Various fixes to CGR formation and operation Index: PublicCompany_State.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/specific/_1856/PublicCompany_State.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** PublicCompany_State.java 1 Sep 2009 21:14:38 -0000 1.4 --- PublicCompany_State.java 2 Sep 2009 21:47:46 -0000 1.5 *************** *** 5,15 **** import rails.game.*; import rails.game.move.MoveableHolderI; import rails.game.move.RemoveFromList; public class PublicCompany_State extends PublicCompany { /** Used for CGR */ ! private boolean hadPermanentTrain = false; public PublicCompany_State() { --- 5,17 ---- import rails.game.*; + import rails.game.move.CashMove; import rails.game.move.MoveableHolderI; import rails.game.move.RemoveFromList; + import rails.game.state.BooleanState; public class PublicCompany_State extends PublicCompany { /** Used for CGR */ ! private BooleanState hadPermanentTrain; public PublicCompany_State() { *************** *** 17,26 **** mustTradeTrainsAtFixedPrice = true; } public boolean hadPermanentTrain() { ! return hadPermanentTrain; } public boolean canRunTrains() { ! if (!hadPermanentTrain) { return true; } --- 19,44 ---- mustTradeTrainsAtFixedPrice = true; } + + /** Initialisation, to be called directly after instantiation (cloning) */ + @Override + public void init(String name, CompanyTypeI type) { + super.init(name, type); + hadPermanentTrain = new BooleanState (name+"_HadPermanentTrain", false); + + // Share price is initially fixed + canSharePriceVary.set(false); + } + public boolean hadPermanentTrain() { ! return hadPermanentTrain.booleanValue(); ! } ! ! public void setHadPermanentTrain(boolean hadPermanentTrain) { ! this.hadPermanentTrain.set(hadPermanentTrain); ! canSharePriceVary.set(true); } public boolean canRunTrains() { ! if (!hadPermanentTrain()) { return true; } *************** *** 29,33 **** public boolean runsWithBorrowedTrain () { ! return !hadPermanentTrain && getNumberOfTrains() == 0; } --- 47,63 ---- public boolean runsWithBorrowedTrain () { ! return !hadPermanentTrain() && getNumberOfTrains() == 0; ! } ! ! /** ! * CGR share price does not move until a permanent train is bought. ! * ! * @param The revenue amount. ! */ ! public void withhold(int amount) { ! if (amount > 0) new CashMove(null, this, amount); ! if (hasStockPrice && !runsWithBorrowedTrain()) { ! Game.getStockMarket().withhold(this); ! } } *************** *** 35,39 **** public void buyTrain(TrainI train, int price) { super.buyTrain (train, price); ! if (train.getType().isPermanent()) hadPermanentTrain = true; } --- 65,69 ---- public void buyTrain(TrainI train, int price) { super.buyTrain (train, price); ! if (train.getType().isPermanent()) setHadPermanentTrain(true); } *************** *** 74,78 **** @Override public boolean mustOwnATrain() { ! if (!hadPermanentTrain) { return false; } else { --- 104,108 ---- @Override public boolean mustOwnATrain() { ! if (!hadPermanentTrain()) { return false; } else { Index: CGRFormationRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/specific/_1856/CGRFormationRound.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** CGRFormationRound.java 28 Aug 2009 20:27:38 -0000 1.10 --- CGRFormationRound.java 2 Sep 2009 21:47:47 -0000 1.11 *************** *** 20,24 **** private int maxLoansToRepayByPresident = 0; private List<PublicCompanyI> mergingCompanies = new ArrayList<PublicCompanyI>(); ! private PublicCompanyI cgr = gameManager.getCompanyManager().getCompanyByName(CGRNAME); private String cgrName = CGRNAME; private List<TrainI> trainsToDiscardFrom = null; --- 20,24 ---- private int maxLoansToRepayByPresident = 0; private List<PublicCompanyI> mergingCompanies = new ArrayList<PublicCompanyI>(); ! private PublicCompany_State cgr = (PublicCompany_State)gameManager.getCompanyManager().getCompanyByName(CGRNAME); private String cgrName = CGRNAME; private List<TrainI> trainsToDiscardFrom = null; *************** *** 422,426 **** DisplayBuffer.add(message); ! // Collect the old token spots, and move cash and trains List<BaseToken> homeTokens = new ArrayList<BaseToken>(); nonHomeTokens = new ArrayList<BaseToken>(); --- 422,473 ---- DisplayBuffer.add(message); ! // Determine the CGR starting price, ! // and close the absorbed companies. ! int lowestPrice = 999; ! int totalPrice = 0; ! int price; ! int numberMerged = mergingCompanies.size(); ! for (PublicCompanyI comp : mergingCompanies) { ! price = comp.getMarketPrice(); ! totalPrice += price; ! if (price < lowestPrice) lowestPrice = price; ! //comp.setClosed(); ! } ! if (numberMerged >= 3) { ! totalPrice -= lowestPrice; ! numberMerged--; ! } ! int cgrPrice = Math.max(100, (((totalPrice/numberMerged)/5))*5); ! ! // Find the correct start space and start the CGR ! if (cgrPrice == 100) { ! cgr.start(100); ! } else { ! StockMarketI sm = StockMarket.getInstance(); ! int prevColPrice = 100; ! int colPrice; ! StockSpaceI startSpace; ! for (int col=6; col <= sm.getNumberOfColumns(); col++) { ! colPrice = sm.getStockSpace(1, col).getPrice(); ! if (cgrPrice > colPrice) continue; ! if (cgrPrice - prevColPrice < colPrice - cgrPrice) { ! startSpace = sm.getStockSpace(1, col-1); ! } else { ! startSpace = sm.getStockSpace(1, col); ! } ! cgr.start(startSpace); ! message = LocalText.getText("START_MERGED_COMPANY", ! "CGR", ! Bank.format(startSpace.getPrice()), ! startSpace.getName()); ! DisplayBuffer.add(message); ! ReportBuffer.add(message); ! break; ! } ! } ! cgr.setFloated(); ! ReportBuffer.add (LocalText.getText("Floats", "CGR")); ! ! // Collect the old token spots, and move cash and trains List<BaseToken> homeTokens = new ArrayList<BaseToken>(); nonHomeTokens = new ArrayList<BaseToken>(); *************** *** 449,452 **** --- 496,500 ---- for (TrainI train : trains) { train.moveTo(cgr.getPortfolio()); + if (train.getType().isPermanent()) cgr.setHadPermanentTrain(true); } } *************** *** 513,562 **** } ! // Determine the CGR starting price, ! // and close the absorbed companies. ! int lowestPrice = 999; ! int totalPrice = 0; ! int price; ! int numberMerged = mergingCompanies.size(); for (PublicCompanyI comp : mergingCompanies) { - price = comp.getMarketPrice(); - totalPrice += price; - if (price < lowestPrice) lowestPrice = price; comp.setClosed(); } - if (numberMerged >= 3) { - totalPrice -= lowestPrice; - numberMerged--; - } - int cgrPrice = Math.max(100, (((totalPrice/numberMerged)/5))*5); - - // Find the correct start space and start the CGR - if (cgrPrice == 100) { - cgr.start(100); - } else { - StockMarketI sm = StockMarket.getInstance(); - int prevColPrice = 100; - int colPrice; - StockSpaceI startSpace; - for (int col=6; col <= sm.getNumberOfColumns(); col++) { - colPrice = sm.getStockSpace(1, col).getPrice(); - if (cgrPrice > colPrice) continue; - if (cgrPrice - prevColPrice < colPrice - cgrPrice) { - startSpace = sm.getStockSpace(1, col-1); - } else { - startSpace = sm.getStockSpace(1, col); - } - cgr.start(startSpace); - message = LocalText.getText("START_MERGED_COMPANY", - "CGR", - Bank.format(startSpace.getPrice()), - startSpace.getName()); - DisplayBuffer.add(message); - ReportBuffer.add(message); - break; - } - } - cgr.setFloated(); - ReportBuffer.add (LocalText.getText("Floats", "CGR")); // Check the trains, autodiscard any excess non-permanent trains --- 561,568 ---- } ! // Close the merged companies for (PublicCompanyI comp : mergingCompanies) { comp.setClosed(); } // Check the trains, autodiscard any excess non-permanent trains *************** *** 742,746 **** } ! @Override public String toString() { return "1856 CGRFormationRound"; --- 748,756 ---- } ! public List<PublicCompanyI> getMergingCompanies() { ! return mergingCompanies; ! } ! ! @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.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** OperatingRound_1856.java 1 Sep 2009 21:14:38 -0000 1.16 --- OperatingRound_1856.java 2 Sep 2009 21:47:47 -0000 1.17 *************** *** 105,134 **** int requiredCash = 0; - // Check if any loan interest can be paid - if (operatingCompany.canLoan()) { - int loanValue = operatingCompany.getLoanValueModel().intValue(); - if (loanValue > 0) { - int interest = loanValue * operatingCompany.getLoanInterestPct() / 100; - int compCash = (operatingCompany.getCash() / 10) * 10; - requiredCash = Math.max(interest - compCash, 0); - } - } - // There is only revenue if there are any trains if (operatingCompany.canRunTrains()) { - int[] allowedRevenueActions = - operatingCompany.isSplitAlways() - ? new int[] { SetDividend.SPLIT } - : operatingCompany.isSplitAllowed() - ? new int[] { SetDividend.PAYOUT, - SetDividend.SPLIT, - SetDividend.WITHHOLD } - : new int[] { SetDividend.PAYOUT, - SetDividend.WITHHOLD }; ! possibleActions.add(new SetDividend( ! operatingCompany.getLastRevenue(), true, ! allowedRevenueActions, ! requiredCash)); // UI directions: // Any nonzero required cash should be reported to the user. --- 105,146 ---- int requiredCash = 0; // There is only revenue if there are any trains if (operatingCompany.canRunTrains()) { ! if (operatingCompany instanceof PublicCompany_State ! && ((PublicCompany_State)operatingCompany).runsWithBorrowedTrain()) { ! DisplayBuffer.add(LocalText.getText("RunsWithBorrowedTrain", ! "CGR", "D")); ! possibleActions.add(new SetDividend( ! operatingCompany.getLastRevenue(), true, ! new int[] {SetDividend.WITHHOLD })); ! } else { ! ! int[] allowedRevenueActions = ! operatingCompany.isSplitAlways() ! ? new int[] { SetDividend.SPLIT } ! : operatingCompany.isSplitAllowed() ! ? new int[] { SetDividend.PAYOUT, ! SetDividend.SPLIT, ! SetDividend.WITHHOLD } ! : new int[] { SetDividend.PAYOUT, ! SetDividend.WITHHOLD }; ! ! // Check if any loan interest can be paid ! if (operatingCompany.canLoan()) { ! int loanValue = operatingCompany.getLoanValueModel().intValue(); ! if (loanValue > 0) { ! int interest = loanValue * operatingCompany.getLoanInterestPct() / 100; ! int compCash = (operatingCompany.getCash() / 10) * 10; ! requiredCash = Math.max(interest - compCash, 0); ! } ! } ! ! possibleActions.add(new SetDividend( ! operatingCompany.getLastRevenue(), true, ! allowedRevenueActions, ! requiredCash)); ! } ! // UI directions: // Any nonzero required cash should be reported to the user. *************** *** 137,145 **** // In that case, the follow-up is done from this class. - if (operatingCompany instanceof PublicCompany_State - && !((PublicCompany_State)operatingCompany).runsWithBorrowedTrain()) { - DisplayBuffer.add(LocalText.getText("RunsWithBorrowedTrain", - "CGR", "D")); - } } } --- 149,152 ---- *************** *** 438,482 **** } ! @Override ! public void resume() { ! if (savedAction == null) { ! // End of CGRFormationRound ! finalLoanRepaymentPending.set(false); ! resetOperatingCompanies(); ! if (operatingCompany != null) { ! setStep(STEP_INITIAL); ! } else { ! finishOR(); ! } ! wasInterrupted.set(true); } else { ! super.resume(); } } ! private void resetOperatingCompanies() { ! ! int lastOperatingCompanyIndex = operatingCompanyIndex; ! // Find the first company that has not yet operated ! // and is not closed. ! while (setNextOperatingCompany(false) ! && getOperatingCompany().isClosed()); List<PublicCompanyI> companies = new ArrayList<PublicCompanyI>(Arrays.asList(operatingCompanyArray)); - PublicCompanyI company; PublicCompanyI cgr = companyManager.getCompanyByName("CGR"); - int index = 0; boolean cgrCanOperate = cgr.hasStarted(); ! for (Iterator<PublicCompanyI> it = companies.iterator(); ! it.hasNext(); ) { ! company = it.next(); ! if (company.isClosed()) { ! if (index <= lastOperatingCompanyIndex) cgrCanOperate = false; ! //it.remove(); ! } } if (operatingCompany != null) { operatingCompanyIndex = companies.indexOf(operatingCompany); --- 445,484 ---- } ! public void resume (List<PublicCompanyI> mergingCompanies) { ! // End of CGRFormationRound ! finalLoanRepaymentPending.set(false); ! resetOperatingCompanies(mergingCompanies); ! if (operatingCompany != null) { ! setStep(STEP_INITIAL); } else { ! finishOR(); } + wasInterrupted.set(true); } ! private void resetOperatingCompanies(List<PublicCompanyI> mergingCompanies) { List<PublicCompanyI> companies = new ArrayList<PublicCompanyI>(Arrays.asList(operatingCompanyArray)); PublicCompanyI cgr = companyManager.getCompanyByName("CGR"); boolean cgrCanOperate = cgr.hasStarted(); ! //for (Iterator<PublicCompanyI> it = companies.iterator(); ! // it.hasNext(); ) { ! // company = it.next(); ! // if (company.isClosed()) { ! // if (index <= lastOperatingCompanyIndex) cgrCanOperate = false; ! // //it.remove(); ! // } ! //} ! for (PublicCompanyI company : mergingCompanies) {; ! if (companiesOperatedThisRound.contains(company)) cgrCanOperate = false; } + // Find the first company that has not yet operated + // and is not closed. + while (setNextOperatingCompany(false) + && getOperatingCompany().isClosed()); + if (operatingCompany != null) { operatingCompanyIndex = companies.indexOf(operatingCompany); Index: GameManager_1856.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/specific/_1856/GameManager_1856.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GameManager_1856.java 4 Feb 2009 20:36:39 -0000 1.1 --- GameManager_1856.java 2 Sep 2009 21:47:47 -0000 1.2 *************** *** 24,28 **** if (round instanceof CGRFormationRound) { setRound(interruptedRound); ! ((OperatingRound_1856)interruptedRound).resume(); } else { super.nextRound(round); --- 24,28 ---- if (round instanceof CGRFormationRound) { setRound(interruptedRound); ! ((OperatingRound_1856)interruptedRound).resume(((CGRFormationRound)round).getMergingCompanies()); } else { super.nextRound(round); |