From: Erik V. <ev...@us...> - 2010-03-05 20:17:49
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv20806/rails/game Modified Files: MapManager.java PublicCompany.java Log Message: Fixed 1835 token cost algorithm Index: MapManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/MapManager.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** MapManager.java 20 Feb 2010 12:42:54 -0000 1.19 --- MapManager.java 5 Mar 2010 20:17:30 -0000 1.20 *************** *** 24,28 **** protected Map<String, MapHex> mHexes = new HashMap<String, MapHex>(); protected int maxX, maxY; ! // upgrade costs on the map for noMapMode protected SortedSet<Integer> possibleTileCosts; --- 24,28 ---- protected Map<String, MapHex> mHexes = new HashMap<String, MapHex>(); protected int maxX, maxY; ! // upgrade costs on the map for noMapMode protected SortedSet<Integer> possibleTileCosts; *************** *** 96,100 **** } log.debug("Possible tileCosts on map are "+possibleTileCosts); ! hexes = new MapHex[1 + maxX][1 + maxY]; --- 96,100 ---- } log.debug("Possible tileCosts on map are "+possibleTileCosts); ! hexes = new MapHex[1 + maxX][1 + maxY]; *************** *** 235,260 **** */ public int getHexDistance (MapHex hex1, MapHex hex2) { ! Map<MapHex, Integer> passed = new HashMap<MapHex, Integer>(); ! 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) { 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()) { if (!hex2.getCurrentTile().hasStations()) continue; --- 235,286 ---- */ public int getHexDistance (MapHex hex1, MapHex hex2) { ! ! if (distances == null) distances = new HashMap<MapHex, Map<MapHex, Integer>> (); ! if (distances.get(hex1) == null) { ! distances.put(hex1, new HashMap<MapHex, Integer>()); ! calculateHexDistances(hex1, hex1, 0); ! } ! return distances.get(hex1).get(hex2); } ! ! private void calculateHexDistances (MapHex hex1, MapHex hex2, int depth) { ! ! if (distances.get(hex1).get(hex2) == null) { ! distances.get(hex1).put(hex2, depth); ! } else { ! if (distances.get(hex1).get(hex2) <= depth) return; ! distances.get(hex1).put(hex2, depth); ! } ! ! for (MapHex hex3 : hex2.getNeighbors()) { ! if (hex3 == null) continue; ! if (distances.get(hex1).get(hex3) == null) { ! calculateHexDistances (hex1, hex3, depth+1); ! } else if (distances.get(hex1).get(hex3) > depth+1) { ! calculateHexDistances (hex1, hex3, depth+1); ! } ! } ! } ! ! /** Cache to hold all unique distance values of tokenable cities from a given hex */ ! private Map<MapHex, int[]> uniqueCityDistances; ! /** Cache to hold all minimal hex distances from given hexes */ ! private Map<MapHex, Map<MapHex, Integer>> distances; ! ! /** * 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) { if (!hex.getCurrentTile().hasStations()) return new int[0]; ! if (uniqueCityDistances == null) uniqueCityDistances = new HashMap<MapHex, int[]> (); ! if (uniqueCityDistances.containsKey(hex)) return uniqueCityDistances.get(hex); ! int distance; ! Set<Integer> distancesSet = new TreeSet<Integer> (); for (MapHex hex2 : mHexes.values()) { if (!hex2.getCurrentTile().hasStations()) continue; *************** *** 267,314 **** distances[i++] = distance2; } ! distanceMap.put(hex, distances); return distances; } - /** Helper method to calculate the distance between two hexes. - * Called recursively. */ - private int calculateHexDistance (MapHex hex1, MapHex hex2, int depth, - Map<MapHex, Integer> passed) { - - /* Map to sort the neighbours (roughly) into decreasing distance */ - SortedMap<Integer, MapHex> neighbours = new TreeMap<Integer, MapHex>(); - - for (MapHex hex3 : hex1.getNeighbors()) { - - if (hex3 == null) continue; - - // Are we finished? - if (hex3 == hex2) { - return 1; - } - - if (passed.containsKey(hex3) && passed.get(hex3) < depth - 1) { - // Backtrack - return -1; - } - - // Sort neighbours on decreasing (rough) distance - int distance = Math.abs(hex2.getX() - hex3.getX()) - + Math.abs(hex2.getY() - hex3.getY()); - neighbours.put(distance, hex3); - } - passed.put (hex1, depth); - for (MapHex neighbour : neighbours.values()) { - if (passed.containsKey(neighbour)) continue; - int result = calculateHexDistance (neighbour, hex2, depth+1, passed); - if (result < 0) { - return 0; // Continue backtracking - } else if (result > 0) { - return result + 1; - } - // Continue loop if result == 0 - } - // Should never get here - return 0; - } } --- 293,299 ---- distances[i++] = distance2; } ! uniqueCityDistances.put(hex, distances); return distances; } } Index: PublicCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PublicCompany.java,v retrieving revision 1.90 retrieving revision 1.91 diff -C2 -d -r1.90 -r1.91 *** PublicCompany.java 4 Mar 2010 22:08:09 -0000 1.90 --- PublicCompany.java 5 Mar 2010 20:17:32 -0000 1.91 *************** *** 1692,1696 **** return baseTokenLayCost[0]; } else { ! return mapManager.getHexDistance(hex, homeHex) * baseTokenLayCost[0]; } } else { --- 1692,1696 ---- return baseTokenLayCost[0]; } else { ! return mapManager.getHexDistance(homeHex, hex) * baseTokenLayCost[0]; } } else { |