From: Erik V. <ev...@us...> - 2010-02-20 12:43:22
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv16681/rails/game Modified Files: MapManager.java PublicCompanyI.java OperatingRound.java PublicCompany.java Log Message: Implemented 1835 token lay cost calculation Index: OperatingRound.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/OperatingRound.java,v retrieving revision 1.102 retrieving revision 1.103 diff -C2 -d -r1.102 -r1.103 *** OperatingRound.java 17 Feb 2010 22:15:46 -0000 1.102 --- OperatingRound.java 20 Feb 2010 12:34:46 -0000 1.103 *************** *** 537,541 **** } ! cost = operatingCompany.getBaseTokenLayCost(); if (stl != null && stl.isFree()) cost = 0; --- 537,541 ---- } ! cost = operatingCompany.getBaseTokenLayCost(hex); if (stl != null && stl.isFree()) cost = 0; *************** *** 2245,2252 **** // LayBaseToken Actions if (operatingCompany.getNumberOfFreeBaseTokens() != 0) { ! possibleActions.add(new OperatingCost( ! operatingCompany, OperatingCost.OCType.LAY_BASE_TOKEN, ! operatingCompany.getBaseTokenLayCost() ! )); } --- 2245,2255 ---- // LayBaseToken Actions if (operatingCompany.getNumberOfFreeBaseTokens() != 0) { ! int[] costs = operatingCompany.getBaseTokenLayCosts(); ! for (int cost : costs) { ! possibleActions.add(new OperatingCost( ! operatingCompany, OperatingCost.OCType.LAY_BASE_TOKEN, ! cost ! )); ! } } *************** *** 2256,2260 **** )); if (operatingCompany.getNumberOfFreeBaseTokens() != 0 ! && operatingCompany.getBaseTokenLayCost() != 0) { possibleActions.add(new OperatingCost( operatingCompany, OperatingCost.OCType.LAY_BASE_TOKEN, 0 --- 2259,2263 ---- )); if (operatingCompany.getNumberOfFreeBaseTokens() != 0 ! && operatingCompany.getBaseTokenLayCost(null) != 0) { possibleActions.add(new OperatingCost( operatingCompany, OperatingCost.OCType.LAY_BASE_TOKEN, 0 Index: MapManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/MapManager.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** MapManager.java 14 Feb 2010 20:47:34 -0000 1.17 --- MapManager.java 20 Feb 2010 12:34:43 -0000 1.18 *************** *** 238,241 **** --- 238,275 ---- return calculateHexDistance (hex1, hex2, 1, passed); } + + /** Cache to hold distances between tokenable cities */ + private Map<MapHex, int[]> distanceMap; + + /** + * Calculate the distances between a given tokenable city hex + * and all other tokenable city hexes. + * <p> The array is cached, so it need be calculated only once. + * @param hex Start hex + * @return Sorted int array containing all occurring distances only once. + */ + public int[] getCityDistances (MapHex hex) { + log.debug("+++ Checking distances from "+hex.getName()); + if (!hex.getCurrentTile().hasStations()) return new int[0]; + if (distanceMap == null) distanceMap = new HashMap<MapHex, int[]> (); + if (distanceMap.containsKey(hex)) return distanceMap.get(hex); + + int distance; + Set<Integer> distancesSet = new TreeSet<Integer> (); + for (MapHex hex2 : mHexes.values()) { + log.debug("--- Checking other hex "+hex2.getName()); + if (!hex2.getCurrentTile().hasStations()) continue; + distance = getHexDistance (hex, hex2); + distancesSet.add(distance); + log.debug("=== Distance is "+distance); + } + int[] distances = new int[distancesSet.size()]; + int i=0; + for (int distance2 : distancesSet) { + distances[i++] = distance2; + } + distanceMap.put(hex, distances); + return distances; + } /** Helper method to calculate the distance between two hexes. Index: PublicCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompany.java,v retrieving revision 1.86 retrieving revision 1.87 diff -C2 -d -r1.86 -r1.87 *** PublicCompany.java 16 Feb 2010 20:15:57 -0000 1.86 --- PublicCompany.java 20 Feb 2010 12:34:50 -0000 1.87 *************** *** 31,34 **** --- 31,38 ---- protected static final int WHEN_FLOATED = 1; protected static final int START_OF_FIRST_OR = 2; + + // Base token lay cost calculation methods + public static final String BASE_COST_SEQUENCE = "sequence"; + public static final String BASE_COST_DISTANCE = "distance"; protected static final String[] tokenLayTimeNames = *************** *** 517,521 **** layCostTag.getAttributeAsString("method", baseTokenLayCostMethod); ! // Must validate the cost method! baseTokenLayCost = --- 521,533 ---- layCostTag.getAttributeAsString("method", baseTokenLayCostMethod); ! if (baseTokenLayCostMethod.equalsIgnoreCase(BASE_COST_SEQUENCE)) { ! baseTokenLayCostMethod = BASE_COST_SEQUENCE; ! } else if (baseTokenLayCostMethod.equalsIgnoreCase(BASE_COST_DISTANCE)) { ! baseTokenLayCostMethod = BASE_COST_DISTANCE; ! } else { ! throw new ConfigurationException( ! "Invalid base token lay cost calculation method: " ! + baseTokenLayCostMethod); ! } baseTokenLayCost = *************** *** 1651,1673 **** /** ! * Calculate the cost of laying a token. Currently hardcoded for the ! * "sequence" method. The other token layong costing methods will be ! * implemented later. ! * ! * @param index The sequence number of the token that the company is laying. * @return The cost of laying that token. */ ! public int getBaseTokenLayCost() { if (baseTokenLayCost == null) return 0; ! int index = getNumberOfLaidBaseTokens(); ! ! if (index >= baseTokenLayCost.length) { ! index = baseTokenLayCost.length - 1; ! } else if (index < 0) { ! index = 0; } ! return baseTokenLayCost[index]; } --- 1663,1715 ---- /** ! * Calculate the cost of laying a token, given the hex where ! * the token is laid. This only makes a diofference for de "distance" method. ! * @param hex The hex where the token is to be laid. * @return The cost of laying that token. */ ! public int getBaseTokenLayCost(MapHex hex) { if (baseTokenLayCost == null) return 0; ! if (baseTokenLayCostMethod.equals(BASE_COST_SEQUENCE)) { ! int index = getNumberOfLaidBaseTokens(); ! ! if (index >= baseTokenLayCost.length) { ! index = baseTokenLayCost.length - 1; ! } else if (index < 0) { ! index = 0; ! } ! return baseTokenLayCost[index]; ! } else if (baseTokenLayCostMethod.equals(BASE_COST_DISTANCE)) { ! if (hex == null) { ! return baseTokenLayCost[0]; ! } else { ! return mapManager.getHexDistance(hex, homeHex) * baseTokenLayCost[0]; ! } ! } else { ! return 0; } ! } ! ! /** Return all possible token lay costs to be incurred for the ! * company's next token lay. In the "distance" method, this will be an array. ! * @return ! */ ! public int[] getBaseTokenLayCosts () { ! ! if (baseTokenLayCostMethod.equals(BASE_COST_SEQUENCE)) { ! return new int[] {getBaseTokenLayCost(null)}; ! } else if (baseTokenLayCostMethod.equals(BASE_COST_DISTANCE)) { ! int[] distances = mapManager.getCityDistances(homeHex); ! int[] costs = new int[distances.length]; ! int i = 0; ! for (int distance : distances) { ! costs[i++] = distance * baseTokenLayCost[0]; ! } ! return costs; ! } else { ! return new int[] {0}; ! } ! } Index: PublicCompanyI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompanyI.java,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** PublicCompanyI.java 16 Feb 2010 20:15:42 -0000 1.47 --- PublicCompanyI.java 20 Feb 2010 12:34:46 -0000 1.48 *************** *** 154,158 **** public int getBaseTokensBuyCost(); ! public int getBaseTokenLayCost(); public boolean canHoldOwnShares(); --- 154,159 ---- public int getBaseTokensBuyCost(); ! public int getBaseTokenLayCost(MapHex hex); ! public int[] getBaseTokenLayCosts(); public boolean canHoldOwnShares(); |