Update of /cvsroot/rails/18xx/rails/game In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv6543/rails/game Modified Files: PublicCompanyI.java BonusToken.java OperatingRound.java PhaseManager.java PublicCompany.java GameManager.java Log Message: Basic loans taking and specific rules for 1856. Index: PublicCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompany.java,v retrieving revision 1.45 retrieving revision 1.46 diff -C2 -d -r1.45 -r1.46 *** PublicCompany.java 3 Dec 2008 20:15:15 -0000 1.45 --- PublicCompany.java 11 Jan 2009 17:24:46 -0000 1.46 *************** *** 219,222 **** --- 219,230 ---- protected String initialTrain = null; + /* Loans */ + protected int maxNumberOfLoans = 0; + protected int valuePerLoan = 0; + protected IntegerState currentNumberOfLoans = null; + protected int loanInterestPct = 0; + protected int maxLoansPerRound = 0; + protected MoneyModel currentLoanValue = null; + /** * The constructor. The way this class is instantiated does not allow *************** *** 523,526 **** --- 531,544 ---- mustHaveOperatedToTradeShares); } + + Tag loansTag = tag.getChild("Loans"); + if (loansTag != null) { + maxNumberOfLoans = loansTag.getAttributeAsInteger("number", -1); + // Note: -1 means undefined, to be handled in the code + // (for instance: 1856). + valuePerLoan = loansTag.getAttributeAsInteger("value", 0); + loanInterestPct = loansTag.getAttributeAsInteger("interest", 0); + maxLoansPerRound = loansTag.getAttributeAsInteger("perRound", -1); + } } *************** *** 579,582 **** --- 597,606 ---- } + if (maxNumberOfLoans != 0) { + currentNumberOfLoans = new IntegerState (name+"_Loans", 0); + currentLoanValue = new MoneyModel (name+"_LoanValue", 0); + currentLoanValue.setOption(MoneyModel.SUPPRESS_ZERO); + } + } *************** *** 1241,1252 **** } ! public int percentageOwnedByPlayers() { ! int share = 0; for (PublicCertificateI cert : certificates) { if (cert.getPortfolio().getOwner() instanceof Player) { ! share += cert.getShare(); } } ! return share; } --- 1265,1276 ---- } ! public int sharesOwnedByPlayers() { ! int shares = 0; for (PublicCertificateI cert : certificates) { if (cert.getPortfolio().getOwner() instanceof Player) { ! shares += cert.getShares(); } } ! return shares; } *************** *** 1592,1595 **** --- 1616,1652 ---- } + public int getCurrentNumberOfLoans() { + return currentNumberOfLoans.intValue(); + } + + public int getCurrentLoanValue () { + return getCurrentNumberOfLoans() * getValuePerLoan(); + } + + public void addLoans(int number) { + currentNumberOfLoans.add(number); + currentLoanValue.add(number * getValuePerLoan()); + } + + public int getLoanInterestPct() { + return loanInterestPct; + } + + public int getMaxNumberOfLoans() { + return maxNumberOfLoans; + } + + public int getMaxLoansPerRound() { + return maxLoansPerRound; + } + + public int getValuePerLoan() { + return valuePerLoan; + } + + public MoneyModel getLoanValueModel () { + return currentLoanValue; + } + @Override public Object clone() { Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** GameManager.java 3 Jan 2009 22:51:35 -0000 1.38 --- GameManager.java 11 Jan 2009 17:24:46 -0000 1.39 *************** *** 84,87 **** --- 84,89 ---- protected boolean canAnyCompanyHoldShares = false; protected boolean bonusTokensExist = false; + protected boolean hasAnyCompanyLoans = false; + protected int stockRoundSequenceRule = StockRound.SELL_BUY_SELL; *************** *** 323,326 **** --- 325,329 ---- canAnyCompanyBuyPrivates = canAnyCompanyBuyPrivates || company.canBuyPrivates(); canAnyCompanyHoldShares = canAnyCompanyHoldShares || company.canHoldOwnShares(); + hasAnyCompanyLoans = hasAnyCompanyLoans || company.getMaxNumberOfLoans() != 0; } *************** *** 482,486 **** return new Integer(numOfORs).toString(); } ! /* (non-Javadoc) * @see rails.game.GameManagerI#getSRNumber() --- 485,489 ---- return new Integer(numOfORs).toString(); } ! /* (non-Javadoc) * @see rails.game.GameManagerI#getSRNumber() *************** *** 940,943 **** --- 943,948 ---- case DO_BONUS_TOKENS_EXIST: return bonusTokensExist; + case HAS_ANY_COMPANY_LOANS: + return hasAnyCompanyLoans; default: return null; Index: PublicCompanyI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompanyI.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** PublicCompanyI.java 3 Dec 2008 20:15:15 -0000 1.26 --- PublicCompanyI.java 11 Jan 2009 17:24:46 -0000 1.27 *************** *** 15,23 **** public static final int CAPITALISE_INCREMENTAL = 1; ! public static final int CAPITALISE_WHEN_BOUGHT = 2; public void init2() throws ConfigurationException; ! public void setIndex (int index); --- 15,23 ---- public static final int CAPITALISE_INCREMENTAL = 1; ! public static final int CAPITALISE_WHEN_BOUGHT = 2; public void init2() throws ConfigurationException; ! public void setIndex (int index); *************** *** 25,29 **** /** * Return the company token background colour. ! * * @return Color object */ --- 25,29 ---- /** * Return the company token background colour. ! * * @return Color object */ *************** *** 32,36 **** /** * Return the company token background colour. ! * * @return Hexadecimal string RRGGBB. */ --- 32,36 ---- /** * Return the company token background colour. ! * * @return Hexadecimal string RRGGBB. */ *************** *** 39,43 **** /** * Return the company token foreground colour. ! * * @return Color object. */ --- 39,43 ---- /** * Return the company token foreground colour. ! * * @return Color object. */ *************** *** 47,51 **** /** * Return the company token foreground colour. ! * * @return Hexadecimal string RRGGBB. */ --- 47,51 ---- /** * Return the company token foreground colour. ! * * @return Hexadecimal string RRGGBB. */ *************** *** 89,93 **** /** * Has the company already floated? ! * * @return true if the company has floated. */ --- 89,93 ---- /** * Has the company already floated? ! * * @return true if the company has floated. */ *************** *** 97,101 **** /** * Has the company already operated? ! * * @return true if the company has operated. */ --- 97,101 ---- /** * Has the company already operated? ! * * @return true if the company has operated. */ *************** *** 106,110 **** /** * Start the company and set its initial (par) price. ! * * @param spaceI */ --- 106,110 ---- /** * Start the company and set its initial (par) price. ! * * @param spaceI */ *************** *** 114,118 **** /** * Get the company par (initial) price. ! * * @return StockSpace object, which defines the company start position on * the stock chart. --- 114,118 ---- /** * Get the company par (initial) price. ! * * @return StockSpace object, which defines the company start position on * the stock chart. *************** *** 122,126 **** /** * Set a new company price. ! * * @param price The StockSpace object that defines the new location on the * stock market. --- 122,126 ---- /** * Set a new company price. ! * * @param price The StockSpace object that defines the new location on the * stock market. *************** *** 130,134 **** /** * Get the current company share price. ! * * @return The StockSpace object that defines the current location on the * stock market. --- 130,134 ---- /** * Get the current company share price. ! * * @return The StockSpace object that defines the current location on the * stock market. *************** *** 143,147 **** public int getIPOPrice (); ! public int getMarketPrice (); --- 143,147 ---- public int getIPOPrice (); ! public int getMarketPrice (); *************** *** 158,162 **** /** * Get a list of this company's certificates. ! * * @return ArrayList containing the certificates (item 0 is the President's * share). --- 158,162 ---- /** * Get a list of this company's certificates. ! * * @return ArrayList containing the certificates (item 0 is the President's * share). *************** *** 166,170 **** /** * Assign a predefined array of certificates to this company. ! * * @param list ArrayList containing the certificates. */ --- 166,170 ---- /** * Assign a predefined array of certificates to this company. ! * * @param list ArrayList containing the certificates. */ *************** *** 173,177 **** /** * Add a certificate to the end of this company's list of certificates. ! * * @param certificate The certificate to add. */ --- 173,177 ---- /** * Add a certificate to the end of this company's list of certificates. ! * * @param certificate The certificate to add. */ *************** *** 181,185 **** * Get the current company treasury. <p> <i>Note: other cash-related methods * are declared in interface CashHolder </i> ! * * @return The current cash amount. */ --- 181,185 ---- * Get the current company treasury. <p> <i>Note: other cash-related methods * are declared in interface CashHolder </i> ! * * @return The current cash amount. */ *************** *** 192,196 **** /** * Get the last revenue earned by this company. ! * * @return The last revenue amount. */ --- 192,196 ---- /** * Get the last revenue earned by this company. ! * * @return The last revenue amount. */ *************** *** 206,210 **** public Player getPresident(); ! public PublicCertificateI getPresidentsShare(); --- 206,210 ---- public Player getPresident(); ! public PublicCertificateI getPresidentsShare(); *************** *** 223,227 **** /** * Get the unit of share. ! * * @return The percentage of ownership that is called "one share". */ --- 223,227 ---- /** * Get the unit of share. ! * * @return The percentage of ownership that is called "one share". */ *************** *** 240,244 **** /** * Is company present on the Stock Market? ! * * @return True if the company has a stock price. */ --- 240,244 ---- /** * Is company present on the Stock Market? ! * * @return True if the company has a stock price. */ *************** *** 322,329 **** public boolean hasReachedDestination(); public void setReachedDestination (boolean value); ! public int getNumberOfTileLays(String tileColour); public boolean mustOwnATrain(); } --- 322,339 ---- public boolean hasReachedDestination(); public void setReachedDestination (boolean value); ! public int getNumberOfTileLays(String tileColour); public boolean mustOwnATrain(); + public int getCurrentNumberOfLoans(); + public int getCurrentLoanValue (); + public void addLoans(int number); + public int getLoanInterestPct(); + public int getMaxNumberOfLoans(); + public int getMaxLoansPerRound(); + public int getValuePerLoan(); + public MoneyModel getLoanValueModel (); + + public int sharesOwnedByPlayers(); } Index: OperatingRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/OperatingRound.java,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** OperatingRound.java 3 Jan 2009 18:24:53 -0000 1.49 --- OperatingRound.java 11 Jan 2009 17:24:46 -0000 1.50 *************** *** 6,9 **** --- 6,10 ---- import rails.game.action.*; import rails.game.move.CashMove; + import rails.game.move.MapChange; import rails.game.move.MoveSet; import rails.game.special.*; *************** *** 56,59 **** --- 57,62 ---- protected List<TrainTypeI> trainsBoughtThisTurn = new ArrayList<TrainTypeI>(4); + + protected Map<PublicCompanyI, Integer> loansThisRound = null; protected PhaseI currentPhase; *************** *** 207,210 **** --- 210,217 ---- result = reachDestinations ((ReachDestinations) selectedAction); + + } else if (selectedAction instanceof TakeLoans) { + + result = takeLoans((TakeLoans) selectedAction); } else if (selectedAction instanceof NullAction) { *************** *** 1490,1494 **** --- 1497,1602 ---- } + + protected boolean takeLoans (TakeLoans action) { + + String errMsg = validateTakeLoans (action); + + if (errMsg != null) { + DisplayBuffer.add(LocalText.getText("CannotTakeLoans", + action.getCompanyName(), + action.getNumberTaken(), + action.getPrice(), + errMsg)); + + return false; + } + + MoveSet.start(true); + + executeTakeLoans (action); + + return true; + + } + + protected String validateTakeLoans (TakeLoans action) { + + String errMsg = null; + PublicCompanyI company = action.getCompany(); + String companyName = company.getName(); + int number = action.getNumberTaken(); + + // Dummy loop to enable a quick jump out. + while (true) { + + // Checks + // Is company operating? + if (company != operatingCompany) { + errMsg = + LocalText.getText("WrongCompany", + companyName, + action.getCompanyName()); + break; + } + // Does company allow any loans? + if (company.getMaxNumberOfLoans() == 0) { + errMsg = LocalText.getText("LoansNotAllowed", + companyName); + break; + } + // Does the company exceed the maximum number of loans? + if (company.getMaxNumberOfLoans() > 0 + && company.getCurrentNumberOfLoans() + number > + company.getMaxNumberOfLoans()) { + errMsg = + LocalText.getText("MoreLoansNotAllowed", + companyName, + company.getMaxNumberOfLoans()); + break; + } + break; + } + return errMsg; + } + + protected void executeTakeLoans (TakeLoans action) { + + int number = action.getNumberTaken(); + int amount = calculateLoanAmount (number); + operatingCompany.addLoans(number); + new CashMove (null, operatingCompany, amount); + if (number == 1) { + ReportBuffer.add(LocalText.getText("CompanyTakesLoan", + operatingCompany.getName(), + Bank.format(operatingCompany.getValuePerLoan()), + Bank.format(amount) + )); + } else { + ReportBuffer.add(LocalText.getText("CompanyTakesLoans", + operatingCompany.getName(), + number, + Bank.format(operatingCompany.getValuePerLoan()), + Bank.format(amount) + )); + } + + if (operatingCompany.getMaxLoansPerRound() > 0) { + int oldLoansThisRound = 0; + if (loansThisRound == null) { + loansThisRound = new HashMap<PublicCompanyI, Integer>(); + } else if (loansThisRound.containsKey(operatingCompany)){ + oldLoansThisRound = loansThisRound.get(operatingCompany); + } + new MapChange<PublicCompanyI, Integer> (loansThisRound, + operatingCompany, + new Integer (oldLoansThisRound + number)); + } + } + + protected int calculateLoanAmount (int numberOfLoans) { + return numberOfLoans * operatingCompany.getValuePerLoan(); + } + /*----- METHODS TO BE CALLED TO SET UP THE NEXT TURN -----*/ *************** *** 1846,1849 **** --- 1954,1985 ---- } + + public void repayLoans (int number) { + operatingCompany.addLoans(-number); + int amount = number * operatingCompany.getValuePerLoan(); + new CashMove (operatingCompany, null, amount); + DisplayBuffer.add(LocalText.getText("CompanyRepaysLoan", + new String[] { + operatingCompany.getName(), + String.valueOf(number), + Bank.format(operatingCompany.getValuePerLoan()), + Bank.format(amount) + })); + } + + public void payLoanInterest () { + int amount = operatingCompany.getCurrentLoanValue() + * operatingCompany.getLoanInterestPct() / 100; + new CashMove (operatingCompany, null, amount); + DisplayBuffer.add(LocalText.getText("CompanyPaysLoanInterest", + new String[] { + operatingCompany.getName(), + Bank.format(amount), + String.valueOf(operatingCompany.getLoanInterestPct()), + String.valueOf(operatingCompany.getCurrentNumberOfLoans()), + Bank.format(operatingCompany.getValuePerLoan()), + })); + } + /* TODO This is just a start of a possible approach to a Help system */ Index: PhaseManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PhaseManager.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** PhaseManager.java 30 Jun 2008 20:35:29 -0000 1.12 --- PhaseManager.java 11 Jan 2009 17:24:46 -0000 1.13 *************** *** 76,85 **** } ! public PhaseI getPhaseNyName(String name) { return phaseMap.get(name); } public boolean hasReachedPhase(String phaseName) { ! return getCurrentPhase().getIndex() >= getPhaseNyName(phaseName).getIndex(); } --- 76,85 ---- } ! public PhaseI getPhaseByName(String name) { return phaseMap.get(name); } public boolean hasReachedPhase(String phaseName) { ! return getCurrentPhase().getIndex() >= getPhaseByName(phaseName).getIndex(); } Index: BonusToken.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/BonusToken.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** BonusToken.java 4 Jun 2008 19:00:31 -0000 1.7 --- BonusToken.java 11 Jan 2009 17:24:46 -0000 1.8 *************** *** 71,75 **** if (spec[0].equalsIgnoreCase("Phase")) { removingObject = ! PhaseManager.getInstance().getPhaseNyName(spec[1]); } } --- 71,75 ---- if (spec[0].equalsIgnoreCase("Phase")) { removingObject = ! PhaseManager.getInstance().getPhaseByName(spec[1]); } } |