Added:
/trunk/v2/src/com/customwars/client/tools/ListUtil.java
Deleted:
/trunk/v2/src/com/customwars/client/io/converter/CW1MapConverter.java
Modified:
/trunk/v2/resources/res/plugin/dor/data/baseTerrains.xml
/trunk/v2/resources/res/plugin/dor/data/terrains.xml
/trunk/v2/src/com/customwars/client/model/game/Player.java
/trunk/v2/src/com/customwars/client/tools/Args.java
=======================================
--- /dev/null
+++ /trunk/v2/src/com/customwars/client/tools/ListUtil.java Mon Mar 14
07:06:28 2011
@@ -0,0 +1,23 @@
+package com.customwars.client.tools;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ListUtil {
+ /**
+ * This is a static utility class. It cannot be constructed.
+ */
+ private ListUtil() {
+ }
+
+ /**
+ * Join the given lists into 1 list.
+ */
+ public static <T> List<T> join(List<T>... lists) {
+ List<T> result = new ArrayList<T>();
+ for (List<T> list : lists) {
+ result.addAll(list);
+ }
+ return result;
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/io/converter/CW1MapConverter.java
Fri Feb 18 15:15:52 2011
+++ /dev/null
@@ -1,470 +0,0 @@
-package com.customwars.client.io.converter;
-
-import com.customwars.client.io.loading.CWResourceLoader;
-import com.customwars.client.io.loading.ModelLoader;
-import com.customwars.client.io.loading.map.BinaryCW2MapParser;
-import com.customwars.client.model.ArmyBranch;
-import com.customwars.client.model.game.Player;
-import com.customwars.client.model.gameobject.City;
-import com.customwars.client.model.gameobject.CityFactory;
-import com.customwars.client.model.gameobject.Terrain;
-import com.customwars.client.model.gameobject.TerrainFactory;
-import com.customwars.client.model.gameobject.Unit;
-import com.customwars.client.model.gameobject.UnitFactory;
-import com.customwars.client.model.gameobject.UnitStats;
-import com.customwars.client.model.map.Map;
-import com.customwars.client.model.map.Range;
-import com.customwars.client.model.map.Tile;
-import com.customwars.client.model.map.connector.TerrainConnector;
-import com.customwars.client.tools.FileUtil;
-import com.customwars.client.tools.MapUtil;
-import org.apache.log4j.BasicConfigurator;
-
-import java.awt.Color;
-import java.io.DataInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-
-/**
- * Read all cw1 maps from CW1_INPUT_DIR
- * Convert to CW2 map object
- * Save to CW2_OUTPUT_DIR as CW2 map file.
- */
-public class CW1MapConverter {
- private static final String CW1_INPUT_DIR
= "/home/stefan/projects/cw1/versus";
- private static final String CW2_OUTPUT_DIR
= "/home/stefan/projects/cw2/maps/versus/";
-
- // CW1 has max 10 players
- private List<Player> players = new ArrayList<Player>(10);
-
- public CW1MapConverter() {
- BasicConfigurator.configure();
- loadCW2Resources();
- convertMaps();
- }
-
- private void loadCW2Resources() {
- CWResourceLoader modelLoader = new
ModelLoader("resources/res/plugin/default/data/");
-
- try {
- modelLoader.load();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private void convertMaps() {
- for (File file : new File(CW1_INPUT_DIR).listFiles()) {
- if (file.isFile()) {
- Map map = readCW1MAPFile(file);
-
- // Terrains need to be connected, since cw1 maps only store base
terrains
- // not specific terrains
- connectTerrains(map);
- writeCW2Map(file, map);
- } else {
- System.out.println("Skipping dir " + file);
- }
- }
- }
-
- //reads a .map file (determines which .map type a file is and calls
correct reading function)
-
- public Map readCW1MAPFile(File file) {
- int fileType = 0;
- DataInputStream inputStream = null;
-
- try {
- inputStream = new DataInputStream(new FileInputStream(file));
- fileType = inputStream.readInt();
- } catch (IOException e) {
- System.err.println(e);
- }
-
- Map map = null;
- try {
- if (fileType <= -1) {
- map = readNewMapFile(inputStream);
- } else {
- //map = readOldMAPFile(inputStream);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return map;
- }
-
- public Map readNewMapFile(DataInputStream in) throws IOException {
- String name = readUntil((char) 0, in);
- String author = readUntil((char) 0, in);
- String desc = readUntil((char) 0, in);
- int width = in.readInt();
- int height = in.readInt();
- int numArmies = in.readByte();
-
- for (int playerIndex = 0; playerIndex < numArmies; playerIndex++) {
- Color color = getColor(in.readByte());
- players.add(new Player(playerIndex, color));
- }
-
- // Add the neutral player
- players.add(Player.createNeutralPlayer(Color.GRAY));
-
- Terrain plain = new Terrain(0, "dummy", "dum", "my", 0, 0, false, 0,
Arrays.asList(5));
- Map map = new Map(width, height, 32, plain);
- map.setMapName(name);
- map.setAuthor(author);
- map.setDescription(desc);
-
- for (int col = 0; col < width; col++) {
- for (int row = 0; row < height; row++) {
- byte terrainID = in.readByte();
- byte colorID = in.readByte();
- Color color = getColor(colorID);
- Player armyPtr = getArmy(color);
- Tile t = map.getTile(col, row);
- Terrain ter = getTerrain(terrainID);
-
- if (ter instanceof City) {
- City city = (City) ter;
- MapUtil.addCityToMap(map, t, city, armyPtr);
-
- if (city.isHQ()) {
- armyPtr.setHq(city);
- }
- } else {
- t.setTerrain(ter);
- }
- }
- }
-
- while (true) {
- int unitID = in.readByte();
- if (unitID == -10) break;
- Color color = getColor(in.readByte());
- Player armyPtr = getArmy(color);
- int col = in.readInt();
- int row = in.readInt();
- Unit unit = getUnit(unitID);
- armyPtr.addUnit(unit);
- Tile t = map.getTile(col, row);
- t.add(unit);
- }
-
- players.clear();
- return map;
- }
-
- private String readUntil(char c, DataInputStream in) throws IOException {
- String word = "";
- char tempbyte;
- while (true) {
- tempbyte = (char) in.readByte();
- if (tempbyte == c) break;
- word += tempbyte;
- }
- return word;
- }
-
- private Color getColor(byte b) {
- Color c = Color.RED;
- switch (b) {
- case -1:
- c = Color.GRAY;
- break;
- case 0:
- c = Color.RED;
- break;
- case 1:
- c = Color.BLUE;
- break;
- case 2:
- c = Color.GREEN;
- break;
- case 3:
- c = Color.YELLOW;
- break;
- case 4:
- c = Color.BLACK;
- break;
- case 5:
- c = Color.ORANGE;
- break;
- case 6:
- // Out of colors
- c = Color.ORANGE;
- break;
- case 7:
- c = Color.cyan;
- break;
- case 8:
- c = Color.magenta;
- break;
- case 9:
- // I'm out of colors, using pink
- c = Color.pink;
- break;
- default:
- System.out.println("Defaulting to RED for id " + b);
- return c;
- }
- return c;
- }
-
- //returns a given player by color
-
- public Player getArmy(Color color) {
- for (Player player : players) {
- if (player.getColor().equals(color))
- return player;
- }
- throw new IllegalStateException("No player found for " + color);
- }
-
- private Terrain getTerrain(int type) {
- Terrain selectedTerrain = null;
-
- switch (type) {
- case 0:
- selectedTerrain = TerrainFactory.getTerrain(0);
- break;
- case 1:
- selectedTerrain = TerrainFactory.getTerrain(1);
- break;
- case 2:
- selectedTerrain = TerrainFactory.getTerrain(17);
- break;
- case 3:
- selectedTerrain = TerrainFactory.getTerrain(2);
- break;
- case 4:
- selectedTerrain = TerrainFactory.getTerrain(13);
- break;
- case 5:
- selectedTerrain = TerrainFactory.getTerrain(19);
- break;
- case 6:
- selectedTerrain = TerrainFactory.getTerrain(30);
- break;
- case 7:
- selectedTerrain = TerrainFactory.getTerrain(75);
- break;
- case 8:
- selectedTerrain = TerrainFactory.getTerrain(64);
- break;
- case 9:
- selectedTerrain = CityFactory.getCity(4);
- break;
- case 10:
- selectedTerrain = CityFactory.getCity(0);
- break;
- case 11:
- selectedTerrain = CityFactory.getCity(1);
- break;
- case 12:
- selectedTerrain = CityFactory.getCity(2);
- break;
- case 13:
- selectedTerrain = CityFactory.getCity(3);
- break;
- case 14:
- selectedTerrain = createCity(6);
- break;
- case 15:
- selectedTerrain = TerrainFactory.getTerrain(86);
- break;
- case 16:
- selectedTerrain = CityFactory.getCity(5);
- break;
- case 17:
- selectedTerrain = TerrainFactory.getTerrain(86);
- break;
- case 18:
- selectedTerrain = TerrainFactory.getTerrain(82);
- break;
- case 19:
- selectedTerrain = TerrainFactory.getTerrain(100);
- break;
- case 20:
- selectedTerrain = TerrainFactory.getTerrain(15);
- break;
- case 21:
- selectedTerrain = createTerrain(81);
- break;
- case 22:
- selectedTerrain = TerrainFactory.getTerrain(86);
- break;
- case 23:
- selectedTerrain = TerrainFactory.getTerrain(86);
- break;
- case 24:
- selectedTerrain = TerrainFactory.getTerrain(82);
- break;
- case 25:
- selectedTerrain = TerrainFactory.getTerrain(86);
- break;
- }
- return selectedTerrain;
- }
-
- public Unit getUnit(int type) {
- Unit unit = null;
- switch (type) {
- case 0:
- unit = UnitFactory.getUnit(0);
- break;
- case 1:
- unit = UnitFactory.getUnit(1);
- break;
- case 2:
- unit = UnitFactory.getUnit(2);
- break;
- case 3:
- unit = UnitFactory.getUnit(3);
- break;
- case 4:
- unit = UnitFactory.getUnit(4);
- break;
- case 5:
- unit = UnitFactory.getUnit(5);
- break;
- case 6:
- unit = UnitFactory.getUnit(6);
- break;
- case 7:
- unit = UnitFactory.getUnit(7);
- break;
- case 8:
- unit = UnitFactory.getUnit(8);
- break;
- case 9:
- unit = UnitFactory.getUnit(9);
- break;
- case 10:
- unit = UnitFactory.getUnit(10);
- break;
- case 11:
- unit = UnitFactory.getUnit(11);
- break;
- case 12:
- unit = UnitFactory.getUnit(12);
- break;
- case 13:
- unit = UnitFactory.getUnit(13);
- break;
- case 14:
- unit = UnitFactory.getUnit(14);
- break;
- case 15:
- unit = UnitFactory.getUnit(15);
- break;
- case 16:
- unit = UnitFactory.getUnit(16);
- break;
- case 17:
- unit = UnitFactory.getUnit(17);
- break;
- case 18:
- unit = UnitFactory.getUnit(18);
- break;
- case 19:
- unit = createUnit(36);
- break;
- case 20:
- unit = createUnit(27);
- break;
- case 21:
- unit = createUnit(28);
- break;
- case 22:
- unit = UnitFactory.getUnit(19);
- break;
- case 23:
- unit = createUnit(20);
- break;
- case 24:
- unit = createUnit(29);
- break;
- case 25:
- unit = createUnit(30);
- break;
- case 26:
- unit = createUnit(31);
- break;
- case 27:
- unit = createUnit(32);
- break;
- case 28:
- unit = createUnit(33);
- break;
- case 29:
- unit = UnitFactory.getUnit(20);
- break;
- case 30:
- unit = UnitFactory.getUnit(21);
- break;
- case 31:
- unit = createUnit(34);
- break;
- }
- return unit;
- }
-
- /**
- * Create a unit with the given id
- * All the other values are defaults, they should be ignored
- */
- private Unit createUnit(int id) {
- UnitStats unitStats = new UnitStats(id, 0, "dummy unit", "", 0, 0, 0,
0, 0, 0, 0, 0,
- false, false, false, false, false, false, false, null,
ArmyBranch.LAND, 0, Range.ZERO_RANGE, "", "", 0, 0, 0);
-
- return new Unit(unitStats);
- }
-
- private Terrain createTerrain(int id) {
- return new Terrain(id, "", "dummy terrain", "", 0, 0, false, 0,
Arrays.asList(1));
- }
-
- private City createCity(int id) {
- return new City(id);
- }
-
- private void writeCW2Map(File cw1File, Map map) {
- String mapName = map.getMapName();
-
- // Some cw1 maps don't have a name!
- // Default to the file name without the .map suffix
- if (mapName.trim().length() == 0) {
- mapName = FileUtil.getFileNameWithoutExtension(cw1File);
- }
-
- System.out.println("Writing " + mapName);
-
- File out = new File(CW2_OUTPUT_DIR, mapName + ".map");
- try {
- BinaryCW2MapParser mapParser = new BinaryCW2MapParser();
- mapParser.writeMap(map, new FileOutputStream(out));
- } catch (IOException e) {
- System.out.println("Problem writing map " + mapName + " " +
map.getDescription());
- e.printStackTrace();
- }
- }
-
- private void connectTerrains(Map map) {
- TerrainConnector terrainConnector = new TerrainConnector(map);
- for (Tile t : map.getAllTiles()) {
- Terrain terr = t.getTerrain();
- terrainConnector.turnSurroundingTerrains(t, terr);
- }
- }
-
- public static void main(String[] args) {
- new CW1MapConverter();
- }
-}
=======================================
--- /trunk/v2/resources/res/plugin/dor/data/baseTerrains.xml Tue Aug 3
13:14:57 2010
+++ /trunk/v2/resources/res/plugin/dor/data/baseTerrains.xml Mon Mar 14
07:06:28 2011
@@ -75,13 +75,13 @@
<int>1</int>
<int>127</int>
</moveCosts>
- <description>In Fog of War, these add 3 to the vision of inf and mech
units.</description>
+ <description>+3 vision for inf and mech units in Fog of
War.</description>
</terrain>
<terrain name="river" id="19" type="river">
<defenseBonus>0</defenseBonus>
<height>-1</height>
<moveCosts>
- <int>1</int>
+ <int>2</int>
<int>1</int>
<int>127</int>
<int>127</int>
=======================================
--- /trunk/v2/resources/res/plugin/dor/data/terrains.xml Tue Aug 3
13:14:57 2010
+++ /trunk/v2/resources/res/plugin/dor/data/terrains.xml Mon Mar 14
07:06:28 2011
@@ -224,7 +224,9 @@
<terrain name="ocean" id="74" type="ocean">
<connect>North, NorthEast, East, South, SouthWest, West</connect>
</terrain>
- <terrain name="reef" id="75" type="ocean"/>
+ <terrain name="reef" id="75" type="reef">
+ <connect type="ocean">North, NorthEast, East, SouthEast, South,
SouthWest, West, NorthWest</connect>
+ </terrain>
<terrain name="shoal" id="76" type="shoal">
<connect type="ocean">North, NorthEast, East, SouthWest, West,
NorthWest</connect>
</terrain>
=======================================
--- /trunk/v2/src/com/customwars/client/model/game/Player.java Thu Feb 17
14:20:21 2011
+++ /trunk/v2/src/com/customwars/client/model/game/Player.java Mon Mar 14
07:06:28 2011
@@ -14,6 +14,7 @@
import org.apache.log4j.Logger;
import java.awt.Color;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
@@ -22,12 +23,13 @@
/**
* Represent a player that can participate in a game, can be either a
human, AI or neutral player.
- * Can be allied with another Player by being in the same team
- * Each non neutral player has an unique ID, neutral players always have
the same NEUTRAL_PLAYER_ID
+ * Can be allied with another Player by being in the same team.
* <p/>
- * A Player is equal to another Player by having the same ID and color
- *
- * @author stefan
+ * Each non neutral player has an unique ID.
+ * Neutral players are special. They always have an id of -1 and are in
the team -1.
+ * They never have a HQ and the unit and city collection is always empty.
+ * <p/>
+ * A Player is equal to another Player by having the same ID and color.
*/
public class Player extends GameObject {
private static final Logger logger = Logger.getLogger(Player.class);
@@ -55,6 +57,30 @@
public Player(int id, Color color) {
this(id, color, "Unnamed", 0, 0, false, new BasicCO(DUMMY_CO_NAME));
}
+
+ /**
+ * Create a human or ai player with a dummy CO
+ */
+ public Player(int id, Color color, String name, int startBudget, int
team, boolean ai) {
+ this(id, color, name, startBudget, team, ai, new
BasicCO(DUMMY_CO_NAME));
+ }
+
+ /**
+ * Create a human or ai player with the given CO
+ */
+ public Player(int id, Color color, String name, int startBudget, int
team, boolean ai, CO co) {
+ this.id = id;
+ this.color = color;
+ this.name = name;
+ this.budget = startBudget;
+ this.team = team;
+ this.ai = ai;
+ this.co = co;
+ this.army = new LinkedList<Unit>();
+ this.cities = new LinkedList<City>();
+ this.unitFacingDirection = Unit.DEFAULT_ORIENTATION;
+ this.coZone = Collections.emptyList();
+ }
/**
* Create a neutral player with a dummy CO
@@ -81,32 +107,14 @@
this.army = new LinkedList<Unit>();
this.cities = new LinkedList<City>();
this.unitFacingDirection = otherPlayer.unitFacingDirection;
- this.coZone = otherPlayer.coZone;
-
- String otherPlayerCOName = otherPlayer.co.getName();
- if (otherPlayerCOName.equals(DUMMY_CO_NAME)) {
- this.co = new BasicCO(DUMMY_CO_NAME);
- } else {
- this.co = COFactory.getCO(otherPlayerCOName);
- }
+ this.coZone = new ArrayList<Location>(otherPlayer.coZone);
+ copyCO(otherPlayer.co);
}
- public Player(int id, Color color, String name, int startBudget, int
team, boolean ai) {
- this(id, color, name, startBudget, team, ai, new
BasicCO(DUMMY_CO_NAME));
- }
-
- public Player(int id, Color color, String name, int startBudget, int
team, boolean ai, CO co) {
- this.id = id;
- this.color = color;
- this.name = name;
- this.budget = startBudget;
- this.team = team;
- this.ai = ai;
- this.co = co;
- this.army = new LinkedList<Unit>();
- this.cities = new LinkedList<City>();
- this.unitFacingDirection = Unit.DEFAULT_ORIENTATION;
- this.coZone = Collections.emptyList();
+ private void copyCO(CO otherCO) {
+ String otherCOName = otherCO.getName();
+ boolean hasDummyCO = otherCOName.equals(DUMMY_CO_NAME);
+ this.co = hasDummyCO ? new BasicCO(DUMMY_CO_NAME) :
COFactory.getCO(otherCOName);
}
public void startTurn() {
@@ -130,7 +138,7 @@
}
/**
- * Destroy all units of this owner
+ * Destroy all units of this player.
* Change the owner of the cities owned by this player to the conqueror
player.
*
* @param conqueror the player that has conquered this player
@@ -142,19 +150,17 @@
logger.info(name + " is destroyed. All cities now belong to " +
conqueror.name);
}
- /**
- * Remove all units from army, until there are none left
- * unit.destroy() removes the unit from this player's army.
- */
private void destroyAllUnits() {
while (!army.isEmpty()) {
Unit unit = army.get(army.size() - 1);
+ // destroy will remove the unit from this army.
unit.destroy(true);
}
}
/**
- * Change the owner of each city owned by this player to newOwner
+ * Change the owner of each city owned by this player to newOwner.
+ * Post: getCityCount() = 0
*
* @param newOwner The new owner of the cities owned by this player
*/
@@ -257,14 +263,14 @@
}
/**
- * @see CO#resetPowerGauge();
+ * @see CO#resetPowerGauge
*/
public void resetPowerGauge() {
co.resetPowerGauge();
}
/**
- * @see CO#chargePowerGauge(double);
+ * @see CO#chargePowerGauge(double)
*/
public void chargePowerGauge(double chargeRate) {
co.chargePowerGauge(chargeRate);
@@ -304,10 +310,16 @@
return budget;
}
+ /**
+ * @return The number of units owned by this player.
+ */
public int getArmyCount() {
return army.size();
}
+ /**
+ * @return The number of cities owned by this player.
+ */
public int getCityCount() {
return cities.size();
}
@@ -332,8 +344,15 @@
return hq;
}
- public boolean isWithinBudget(int price) {
- return budget - price >= 0;
+ /**
+ * Check if the player can spend the given amount of money.
+ * without causing a negative balance.
+ *
+ * @param money The money to spend.
+ * @return If this player can spend the given money.
+ */
+ public boolean isWithinBudget(int money) {
+ return budget - money >= 0;
}
public CO getCO() {
@@ -343,7 +362,7 @@
/**
* Check if the given location is within the co Zone:
* <ul>
- * <li>A CO should be loaded into a unit</li>
+ * <li>A CO should be loaded into a unit owned by this player</li>
* <li>The location should be within the zone of this coUnit</li>
* </ul>
*/
@@ -399,8 +418,8 @@
@Override
public String toString() {
- return String.format("[name=%s id=%s state=%s color=%s budget=%s
team=%s]",
- name, id, getState(), ColorUtil.toString(color), budget, team);
+ return String.format("[name=%s id=%s state=%s color=%s budget=%s
team=%s co=%s]",
+ name, id, getState(), ColorUtil.toString(color), budget, team,
co.getName());
}
public String printStats() {
=======================================
--- /trunk/v2/src/com/customwars/client/tools/Args.java Wed Nov 11 11:27:50
2009
+++ /trunk/v2/src/com/customwars/client/tools/Args.java Mon Mar 14 07:06:28
2011
@@ -1,14 +1,14 @@
package com.customwars.client.tools;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Map;
/**
* Based on http://www.javapractices.com
* validate methods throw an <tt>IllegalArgumentException<tt> when the
input was not valid
* get methods return a value within bounds
- *
- * @author stefan
*/
public final class Args {
@@ -117,7 +117,17 @@
return result;
}
+ /**
+ * Create an empty list if the given list parameter is null, otherwise
return the list parameter.
+ */
public static <T> List<T> createEmptyListIfNull(List<T> lst) {
return lst == null ? new ArrayList<T>() : lst;
}
-}
+
+ /**
+ * Create an empty Map if the given map parameter is null, otherwise
return the map parameter.
+ */
+ public static <T> Map<T, T> createEmptyMapIfNull(Map<T, T> map) {
+ return map == null ? Collections.<T, T>emptyMap() : map;
+ }
+}