From: Erik V. <ev...@us...> - 2010-04-11 15:49:55
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv16112/rails/game Modified Files: City.java OperatingRound.java MapHex.java PrivateCompany.java Log Message: Added OperatingRound methods isBlockedForTileLays() and isBlockedForTokenLays(). The former replaces the existing isBLocked() (for privates still in operation). The latter is new and covers various cases where unlaid home bases prevent other companies to lay base tokens on a hex. Index: OperatingRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/OperatingRound.java,v retrieving revision 1.121 retrieving revision 1.122 diff -C2 -d -r1.121 -r1.122 *** OperatingRound.java 28 Mar 2010 20:14:20 -0000 1.121 --- OperatingRound.java 11 Apr 2010 15:49:47 -0000 1.122 *************** *** 516,519 **** --- 516,524 ---- break; } + + if (!isTokenLayAllowed (operatingCompany, hex, station)) { + errMsg = LocalText.getText("BaseTokenSlotIsReserved"); + break; + } if (!hex.hasTokenSlotsLeft(station)) { *************** *** 534,538 **** break; } ! if (action != null) { List<MapHex> locations = action.getLocations(); --- 539,543 ---- break; } ! if (action != null) { List<MapHex> locations = action.getLocations(); *************** *** 2505,2508 **** --- 2510,2549 ---- } + /** Reports if a tile lay is allowed by a certain company on a certain hex + * <p> + * This method can be used both in retricting possible actions + * and in validating submitted actions. + * <p> Currently, only a few standard checks are included. + * This method can be extended to perform other generic checks, + * such as if a route exists, + * and possibly in subclasses for game-specific checks. + * @param company The company laying a tile. + * @param hex The hex on which a tile is laid. + * @param orientation The orientation in which the tile is laid (-1 is any). + */ + protected boolean isTileLayAllowed (PublicCompanyI company, + MapHex hex, int orientation) { + return !hex.isBlockedForTileLays(); + } + + /** Reports if a token lay is allowed by a certain company on a certain hex and city + * <p> + * This method can be used both in retricting possible actions + * and in validating submitted actions. + * <p> Currently, only a few standard checks are included. + * This method can be extended to perform other generic checks, + * such as if a route exists, + * and possibly in subclasses for game-specific checks. + * + * @param company The company laying a tile. + * @param hex The hex on which a tile is laid. + * @param station The number of the station/city on which the token + * is to be laid (0 if any or immaterial). + */ + protected boolean isTokenLayAllowed (PublicCompanyI company, + MapHex hex, int station) { + return !hex.isBlockedForTokenLays(company, station); + } + /** * Can the company buy a train now? Index: City.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/City.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** City.java 31 Jan 2010 22:22:28 -0000 1.10 --- City.java 11 Apr 2010 15:49:47 -0000 1.11 *************** *** 130,133 **** --- 130,137 ---- return tokens.size() < slots; } + + public int getTokenSlotsLeft () { + return slots - tokens.size(); + } public boolean removeToken(TokenI token) { Index: MapHex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/MapHex.java,v retrieving revision 1.41 retrieving revision 1.42 diff -C2 -d -r1.41 -r1.42 *** MapHex.java 9 Apr 2010 17:49:31 -0000 1.41 --- MapHex.java 11 Apr 2010 15:49:47 -0000 1.42 *************** *** 90,94 **** * null as default implies false - see isBlocked() */ ! private BooleanState isBlockedState = null; protected Map<PublicCompanyI, City> homes; --- 90,105 ---- * null as default implies false - see isBlocked() */ ! private BooleanState isBlockedForTileLays = null; ! ! /** ! * Is the hex initially blocked for token lays (e.g. when a home base ! * must first be laid)? <p> ! * NOTE:<br>null means: blocked unless there is more free space than unlaid home bases,<br> ! * false means: blocked unless there is any free space.<br> ! * This makes a difference for 1835 Berlin, which is home to PR, but ! * the absence of a PR token does not block the third slot ! * when the green tile is laid. ! */ ! private BooleanState isBlockedForTokenLays = null; protected Map<PublicCompanyI, City> homes; *************** *** 178,181 **** --- 189,196 ---- infoText += " " + cityName; } + + if (tag.getAttributeAsString("unlaidHomeBlocksTokens") != null) { + setBlockedForTokenLays(tag.getAttributeAsBoolean("unlaidHomeBlocksTokens", false)); + } } *************** *** 729,732 **** --- 744,756 ---- token.moveTo(city); update(); + + if (isHomeFor(company) + && isBlockedForTokenLays != null + && isBlockedForTokenLays.booleanValue()) { + // Assume that there is only one home base on such a tile, + // so we don't need to check for other ones + isBlockedForTokenLays.set(false); + } + return true; } *************** *** 882,886 **** } ! public boolean isHome(PublicCompanyI company) { boolean result = homes != null && homes.get(company) != null; return result; --- 906,910 ---- } ! public boolean isHomeFor(PublicCompanyI company) { boolean result = homes != null && homes.get(company) != null; return result; *************** *** 902,912 **** /** ! * @return Returns the isBlocked. */ ! public boolean isBlocked() { ! if (isBlockedState == null) return false; else ! return isBlockedState.booleanValue(); } --- 926,936 ---- /** ! * @return Returns false if no tiles may yet be laid on this hex. */ ! public boolean isBlockedForTileLays() { ! if (isBlockedForTileLays == null) return false; else ! return isBlockedForTileLays.booleanValue(); } *************** *** 914,926 **** * @param isBlocked The isBlocked to set (state variable) */ ! public void setBlocked(boolean isBlocked) { ! if (isBlockedState == null) ! isBlockedState = new BooleanState("isBlocked", isBlocked); else ! isBlockedState.set(isBlocked); } public boolean isUpgradeableNow() { ! if (isBlocked()) { log.debug("Hex " + name + " is blocked"); return false; --- 938,950 ---- * @param isBlocked The isBlocked to set (state variable) */ ! public void setBlockedForTileLays(boolean isBlocked) { ! if (isBlockedForTileLays == null) ! isBlockedForTileLays = new BooleanState(name+"_IsBlockedForTileLays", isBlocked); else ! isBlockedForTileLays.set(isBlocked); } public boolean isUpgradeableNow() { ! if (isBlockedForTileLays()) { log.debug("Hex " + name + " is blocked"); return false; *************** *** 944,947 **** --- 968,1010 ---- } + /** + * @return Returns false if no base tokens may yet be laid on this hex and station. + * NOTE: this method currently only checks for prohibitions caused + * by the presence of unlaid hoem base tokens. + * It does NOT (yet) check for free space. + */ + public boolean isBlockedForTokenLays(PublicCompanyI company, int cityNumber) { + if (isHomeFor(company)) + // Company can always lay a home base + return false; + else if (isBlockedForTokenLays != null) { + // if true: token lay blocked because a required home base is not yet laid + // if false: token lay allowed if there is any free space + // Free space is not checked here (yet) + return isBlockedForTokenLays.booleanValue(); + } else if (homes != null && !homes.isEmpty()) { + // Check if this token lay does not block an unlaid home base + for (City city : homes.values()) { + if (cityNumber == city.getNumber() + // Assume that a city is never home to more than one company + && city.getTokens().isEmpty() + && city.getTokenSlotsLeft() < 2) { + return true; + } + } + } + return false; + } + + /** + * @param isBlocked The isBlocked to set (state variable) + */ + public void setBlockedForTokenLays(boolean isBlocked) { + if (isBlockedForTokenLays == null) + isBlockedForTokenLays = new BooleanState(name+"_IsBlockedForTokenLays", isBlocked); + else + isBlockedForTokenLays.set(isBlocked); + } + public boolean hasOffBoardValues() { return offBoardValues != null && offBoardValues.length > 0; Index: PrivateCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PrivateCompany.java,v retrieving revision 1.39 retrieving revision 1.40 diff -C2 -d -r1.39 -r1.40 *** PrivateCompany.java 21 Mar 2010 17:43:50 -0000 1.39 --- PrivateCompany.java 11 Apr 2010 15:49:47 -0000 1.40 *************** *** 134,138 **** MapHex hex = mapManager.getHex(hexName); blockedHexes.add(hex); ! hex.setBlocked(true); } } --- 134,138 ---- MapHex hex = mapManager.getHex(hexName); blockedHexes.add(hex); ! hex.setBlockedForTileLays(true); } } *************** *** 288,292 **** if (blockedHexes != null) { for (MapHex hex : blockedHexes) { ! hex.setBlocked(false); } } --- 288,292 ---- if (blockedHexes != null) { for (MapHex hex : blockedHexes) { ! hex.setBlockedForTileLays(false); } } |