Modified:
/trunk/v2/src/com/customwars/client/model/CWGameController.java
/trunk/v2/src/com/customwars/client/model/map/Map.java
=======================================
--- /trunk/v2/src/com/customwars/client/model/CWGameController.java Sun Mar
20 13:51:30 2011
+++ /trunk/v2/src/com/customwars/client/model/CWGameController.java Mon Mar
21 08:15:29 2011
@@ -21,8 +21,6 @@
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
/**
* CW implementation of a GameController
@@ -31,13 +29,11 @@
private final Game game;
private final Map map;
private final ControllerManager controllerManager;
- private final java.util.Map<Location, City> citiesUnderConstruction;
public CWGameController(Game game, ControllerManager controllerManager) {
this.game = game;
this.map = game.getMap();
this.controllerManager = controllerManager;
- this.citiesUnderConstruction = new HashMap<Location, City>();
}
@Override
@@ -186,8 +182,13 @@
@Override
public boolean constructCity(Unit unit, String cityID, Location
location) {
- City city = getCityUnderConstructionAt(location, cityID);
- addCityUnderConstruction(location, city);
+ City city;
+ if (map.isConstructingCityAt(location)) {
+ city = map.getCityUnderConstructionAt(location);
+ } else {
+ city = CityFactory.getCity(cityID);
+ map.addCityUnderConstruction(location, city);
+ }
// When the city is constructed it is added to the player cities
collection.
unit.construct(city);
@@ -200,22 +201,6 @@
return false;
}
}
-
- private void addCityUnderConstruction(Location location, City city) {
- if (!citiesUnderConstruction.containsKey(location)) {
- citiesUnderConstruction.put(location, city);
- }
- }
-
- public City getCityUnderConstructionAt(Location location, String cityID)
{
- City city;
- if (citiesUnderConstruction.containsKey(location)) {
- city = citiesUnderConstruction.get(location);
- } else {
- city = CityFactory.getCity(cityID);
- }
- return city;
- }
private void addCityToTile(Location to, City city, Player cityOwner) {
Tile t = map.getTile(to);
@@ -231,7 +216,7 @@
public void stopConstructingCity(Unit unit, Location location) {
unit.stopConstructing();
- citiesUnderConstruction.remove(location);
+ map.stopConstructingCity(location);
}
@Override
@@ -260,29 +245,13 @@
Player activePlayer = game.getActivePlayer();
map.showLosFor(activePlayer);
map.resetAllHiddenUnits(activePlayer);
- validateConstructingCities(unit);
+ map.validateConstructingCities();
if (unit.isCoOnBoard()) {
updateCOZone(unit);
}
}
}
-
- private void validateConstructingCities(Unit unitThatMoved) {
- // Remove the city under construction location(s) with no apc on.
- // This happens when the apc construct a city in 1 turn.
- // But then moves to another location leaving the city in the
citiesUnderConstruction collection..
- if (unitThatMoved.hasConstructionMaterials()) {
- Iterator<Location> iterator =
citiesUnderConstruction.keySet().iterator();
-
- while (iterator.hasNext()) {
- Location location = iterator.next();
- if (map.getUnitOn(location) == null) {
- iterator.remove();
- }
- }
- }
- }
public void produceUnit(Unit producer, String unitToProduce) {
Unit unit = UnitFactory.getUnit(unitToProduce);
=======================================
--- /trunk/v2/src/com/customwars/client/model/map/Map.java Sun Mar 20
13:51:30 2011
+++ /trunk/v2/src/com/customwars/client/model/map/Map.java Mon Mar 21
08:15:29 2011
@@ -25,7 +25,9 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -50,6 +52,7 @@
private transient PathFinder pathFinder; // To builds paths
within the map
private Player neutralPlayer; // Idle neutral player owner of the
neutral cities
private GameRules defaultRules; // The default game rules as chosen by
the map creator
+ private final HashMap<Location, City> citiesUnderConstruction;
/**
* Convenient constructor to create an anonymous map. The map name and
author are set to anonymous.
@@ -77,6 +80,7 @@
this.pathFinder = new PathFinder(this);
this.neutralPlayer =
Player.createNeutralPlayer(App.getColor("plugin.neutral_color"));
this.defaultRules = new GameRules();
+ this.citiesUnderConstruction = new HashMap<Location, City>();
fillMap(cols, rows, startTerrain);
}
@@ -565,6 +569,7 @@
// If not hidden or directly next to the tile we can see everything
return !terrain.isHidden() || adjacent;
}
+
/**
* Normalise this map
@@ -638,6 +643,44 @@
public void setDefaultRules(GameRules rules) {
this.defaultRules = rules;
}
+
+ public void addCityUnderConstruction(Location location, City city) {
+ if (!citiesUnderConstruction.containsKey(location)) {
+ citiesUnderConstruction.put(location, city);
+ }
+ }
+
+ /**
+ * Remove the cities under construction with no unit on them.
+ * This happens when the apc construct a city in 1 turn.
+ * But then moves to another location leaving the city in the
citiesUnderConstruction collection.
+ */
+ public void validateConstructingCities() {
+ Iterator<Location> iterator =
citiesUnderConstruction.keySet().iterator();
+
+ while (iterator.hasNext()) {
+ Location location = iterator.next();
+ if (getUnitOn(location) == null) {
+ iterator.remove();
+ }
+ }
+ }
+
+ public City getCityUnderConstructionAt(Location location) {
+ City city = null;
+ if (isConstructingCityAt(location)) {
+ city = citiesUnderConstruction.get(location);
+ }
+ return city;
+ }
+
+ public boolean isConstructingCityAt(Location location) {
+ return citiesUnderConstruction.containsKey(location);
+ }
+
+ public void stopConstructingCity(Location location) {
+ citiesUnderConstruction.remove(location);
+ }
/**
* @see #getUnitOn(Location)