[customwars] r746 committed - Create the action command in the actions. Improve produce unit code. C...

0 views
Skip to first unread message

codesite...@google.com

unread,
Mar 20, 2011, 9:50:24 AM3/20/11
to customwar...@googlegroups.com
Revision: 746
Author: ace.stef
Date: Sun Mar 20 06:49:26 2011
Log: Create the action command in the actions. Improve produce unit
code. Change numeric id to text in the action commands. Use produce instead
of build unit to prevent confusion. units produce units, cities build units.
http://code.google.com/p/customwars/source/detail?r=746

Added:
/trunk/v2/src/com/customwars/client/action/ActionCommandEncoder.java
/trunk/v2/test/com/customwars/client/action
/trunk/v2/test/com/customwars/client/action/TestActionCommand.java
Modified:
/trunk/v2/resources/res/data/lang/Languages.properties
/trunk/v2/resources/res/plugin/dor/data/units.xml
/trunk/v2/src/com/customwars/client/action/AbstractCWAction.java
/trunk/v2/src/com/customwars/client/action/ActionBag.java
/trunk/v2/src/com/customwars/client/action/ActionFactory.java
/trunk/v2/src/com/customwars/client/action/ActionManager.java
/trunk/v2/src/com/customwars/client/action/ActionParser.java
/trunk/v2/src/com/customwars/client/action/CWAction.java
/trunk/v2/src/com/customwars/client/action/city/LaunchRocketAction.java
/trunk/v2/src/com/customwars/client/action/unit/AddUnitToTileAction.java
/trunk/v2/src/com/customwars/client/action/unit/AttackCityAction.java
/trunk/v2/src/com/customwars/client/action/unit/AttackUnitAction.java
/trunk/v2/src/com/customwars/client/action/unit/ConstructCityAction.java
/trunk/v2/src/com/customwars/client/action/unit/DropAction.java
/trunk/v2/src/com/customwars/client/action/unit/FireFlareAction.java
/trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
/trunk/v2/src/com/customwars/client/action/unit/ProduceUnitAction.java
/trunk/v2/src/com/customwars/client/action/unit/TransformTerrainAction.java
/trunk/v2/src/com/customwars/client/controller/HumanCityController.java
/trunk/v2/src/com/customwars/client/controller/UnitController.java
/trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java
/trunk/v2/src/com/customwars/client/model/CWGameController.java
/trunk/v2/src/com/customwars/client/model/GameController.java
/trunk/v2/src/com/customwars/client/model/game/GameReplay.java
/trunk/v2/src/com/customwars/client/model/gameobject/Unit.java
/trunk/v2/src/com/customwars/client/model/gameobject/UnitStats.java
/trunk/v2/src/com/customwars/client/network/MessageSender.java
/trunk/v2/src/com/customwars/client/network/MessageSenderAdapter.java

=======================================
--- /dev/null
+++ /trunk/v2/src/com/customwars/client/action/ActionCommandEncoder.java
Sun Mar 20 06:49:26 2011
@@ -0,0 +1,70 @@
+package com.customwars.client.action;
+
+import com.customwars.client.model.game.Player;
+import com.customwars.client.model.gameobject.City;
+import com.customwars.client.model.gameobject.Terrain;
+import com.customwars.client.model.gameobject.Unit;
+import com.customwars.client.model.map.Location;
+
+/**
+ * Uses the builder pattern to encode game objects into text.
+ * Methods calls can be chained.
+ * Each parameter is separated by a single space.
+ * Example: 'move 0 0 1 0'
+ */
+public class ActionCommandEncoder {
+ private final StringBuilder actionCommand;
+
+ public ActionCommandEncoder() {
+ actionCommand = new StringBuilder(12);
+ }
+
+ public ActionCommandEncoder(String str) {
+ this();
+ actionCommand.append(str);
+ }
+
+ public ActionCommandEncoder add(Location t) {
+ add(t.getCol() + "");
+ add(t.getRow() + "");
+ return this;
+ }
+
+ public ActionCommandEncoder add(Unit unit) {
+ return add(unit.getStats().getName());
+ }
+
+ public ActionCommandEncoder add(City city) {
+ return add(city.getName());
+ }
+
+ public ActionCommandEncoder add(Terrain terrain) {
+ return add(terrain.getName());
+ }
+
+ public ActionCommandEncoder add(Player player) {
+ return add(player.getId() + "");
+ }
+
+ public ActionCommandEncoder add(int integer) {
+ return add(integer + "");
+ }
+
+ /**
+ * Add a parameter to the action command.
+ * A space character is put before the given parameter if the action
command is not empty.
+ *
+ * @param parameter The parameter to add to the action command.
+ */
+ public ActionCommandEncoder add(String parameter) {
+ if (actionCommand.length() != 0) {
+ actionCommand.append(" ");
+ }
+ actionCommand.append(parameter);
+ return this;
+ }
+
+ public String build() {
+ return actionCommand.toString();
+ }
+}
=======================================
--- /dev/null
+++ /trunk/v2/test/com/customwars/client/action/TestActionCommand.java Sun
Mar 20 06:49:26 2011
@@ -0,0 +1,197 @@
+package com.customwars.client.action;
+
+import com.customwars.client.action.unit.MoveAnimatedAction;
+import com.customwars.client.model.TestData;
+import com.customwars.client.model.drop.DropLocationsQueue;
+import com.customwars.client.model.game.Game;
+import com.customwars.client.model.game.Player;
+import com.customwars.client.model.gameobject.City;
+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.map.Location;
+import com.customwars.client.model.map.Location2D;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import slick.HardCodedGame;
+
+import java.awt.Color;
+
+public class TestActionCommand {
+ private Game game;
+
+ @Before
+ public void beforeEachTest() {
+ TestData.storeTestData();
+ game = HardCodedGame.getGame();
+ game.startGame();
+ }
+
+ @Test
+ public void testActionCommandEncoder() {
+ ActionCommandEncoder actionCommandEncoder = new
ActionCommandEncoder("my action");
+ final Location from = new Location2D(0, 0);
+ final Location to = new Location2D(5, 0);
+ final Player neutralPlayer = Player.createNeutralPlayer(Color.blue);
+
+ actionCommandEncoder.add(from).add(to);
+ Assert.assertEquals("my action 0 0 5 0", actionCommandEncoder.build());
+
+ actionCommandEncoder.add(neutralPlayer);
+ Assert.assertEquals("my action 0 0 5 0 -1",
actionCommandEncoder.build());
+
+ actionCommandEncoder.add(403);
+ Assert.assertEquals("my action 0 0 5 0 -1 403",
actionCommandEncoder.build());
+ }
+
+ @Test
+ public void moveAnimatedActionSingleActionTest() {
+ CWAction action = new MoveAnimatedAction(new Location2D(0, 0), new
Location2D(5, 0));
+ Assert.assertEquals("0 0 5 0", action.getActionCommand());
+ }
+
+ @Test
+ public void testDropActionCommand() {
+ Unit apc = game.getMap().getUnitOn(6, 2);
+ Location apcLocation = apc.getLocation();
+ DropLocationsQueue dropQueue = new DropLocationsQueue();
+ dropQueue.addDropLocation(new Location2D(6, 1), (Unit)
apc.getLastLocatable());
+ CWAction loadAction = ActionFactory.buildDropAction(apc, apcLocation,
apcLocation, dropQueue);
+
+ // From - To - unit in transport index - drop location
+ String expectedActionCommand = "drop 6 2 6 2 0 6 1";
+ Assert.assertEquals(expectedActionCommand,
loadAction.getActionCommand());
+ }
+
+ @Test
+ public void testMoveActionCommand() {
+ Unit apc = game.getMap().getUnitOn(6, 2);
+ CWAction moveAction = ActionFactory.buildMoveAction(apc, new
Location2D(6, 0));
+
+ String expectedActionCommand = "move 6 2 6 0";
+ Assert.assertEquals(expectedActionCommand,
moveAction.getActionCommand());
+ }
+
+ @Test
+ public void testCaptureActionCommand() {
+ Unit inf = game.getMap().getUnitOn(5, 9);
+ City city = game.getMap().getCityOn(7, 8);
+ CWAction captureAction = ActionFactory.buildCaptureAction(inf, city);
+
+ String expectedActionCommand = "capture 5 9 7 8";
+ Assert.assertEquals(expectedActionCommand,
captureAction.getActionCommand());
+ }
+
+ @Test
+ public void testLoadActionCommand() {
+ Unit mech = game.getMap().getUnitOn(0, 8);
+ Unit apc = game.getMap().getUnitOn(1, 7);
+ CWAction loadAction = ActionFactory.buildLoadAction(mech, apc);
+
+ String expectedActionCommand = "load 0 8 1 7";
+ Assert.assertEquals(expectedActionCommand,
loadAction.getActionCommand());
+ }
+
+ @Test
+ public void testSupplyActionCommand() {
+ Unit apc = game.getMap().getUnitOn(1, 7);
+ CWAction supplyAction = ActionFactory.buildSupplyAction(apc, new
Location2D(1, 7));
+
+ Assert.assertEquals("supply 1 7 1 7", supplyAction.getActionCommand());
+ }
+
+ @Test
+ public void testJoinActionCommand() {
+ Unit inf1 = game.getMap().getUnitOn(0, 5);
+ Unit inf2 = game.getMap().getUnitOn(2, 6);
+ CWAction joinAction = ActionFactory.buildJoinAction(inf1, inf2);
+
+ Assert.assertEquals("join 0 5 2 6", joinAction.getActionCommand());
+ }
+
+ @Test
+ public void testUnitvsUnitAttackActionCommand() {
+ Unit rockets = game.getMap().getUnitOn(2, 7);
+ Unit inf = game.getMap().getUnitOn(5, 7);
+ CWAction attackAction =
ActionFactory.buildUnitVsUnitAttackAction(rockets, inf, new Location2D(2,
7));
+
+ Assert.assertEquals("attack_unit 2 7 2 7 5 7",
attackAction.getActionCommand());
+ }
+
+ @Test
+ public void testUnitvsCityAttackActionCommand() {
+ Unit rockets = game.getMap().getUnitOn(2, 7);
+ City city = game.getMap().getCityOn(0, 0);
+ CWAction attackAction =
ActionFactory.buildUnitVsCityAttackAction(rockets, city, new Location2D(2,
7));
+
+ Assert.assertEquals("attack_city 2 7 2 7 0 0",
attackAction.getActionCommand());
+ }
+
+ @Test
+ public void testAddUnitActionCommand() {
+ Unit unit = UnitFactory.getUnit(0);
+
+ CWAction attackAction = ActionFactory.buildAddUnitToTileAction(unit,
game.getPlayerByID(0), new Location2D(2, 13));
+ // location - unit name - player ID
+ Assert.assertEquals("build_unit 2 13 infantry 0",
attackAction.getActionCommand());
+ }
+
+ @Test
+ public void testLaunchRocketActionCommand() {
+ Unit inf = game.getMap().getUnitOn(8, 9);
+ City silo = game.getMap().getCityOn(8, 8);
+
+ CWAction launchRocketAction =
ActionFactory.buildLaunchRocketAction(inf, silo, new Location2D(0, 0));
+ // inf location - silo location - rocket destination
+ Assert.assertEquals("launch_rocket 8 9 8 8 0 0",
launchRocketAction.getActionCommand());
+ }
+
+ @Test
+ public void testTransformTerrainActionCommand() {
+ Unit apc = game.getMap().getUnitOn(6, 2);
+ Terrain plain = TerrainFactory.getTerrain("plain");
+
+ CWAction transformTerrainAction =
ActionFactory.buildTransformTerrainAction(apc, new Location2D(6, 1), plain);
+ Assert.assertEquals("transform_terrain 6 2 6 1 plain",
transformTerrainAction.getActionCommand());
+ }
+
+ @Test
+ public void testFireFlareActionCommand() {
+ Unit flare = game.getMap().getUnitOn(6, 2);
+
+ CWAction fireFlareActionCommand =
ActionFactory.buildFireFlareAction(flare, new Location2D(6, 1), new
Location2D(0, 0));
+ Assert.assertEquals("flare 6 2 6 1 0 0",
fireFlareActionCommand.getActionCommand());
+ }
+
+ @Test
+ public void testConstructCityActionCommand() {
+ Unit flare = game.getMap().getUnitOn(6, 2);
+
+ CWAction constructCityAction =
ActionFactory.buildConstructCityAction(flare, "villa", new Location2D(0,
0));
+ Assert.assertEquals("build_city 6 2 0 0 villa",
constructCityAction.getActionCommand());
+ }
+
+ @Test
+ public void testLoadCOActionCommand() {
+ Unit flare = game.getMap().getUnitOn(6, 2);
+
+ CWAction loadCOAction = ActionFactory.buildLoadCOAction(flare, new
Location2D(0, 0));
+ Assert.assertEquals("load_co 6 2 0 0",
loadCOAction.getActionCommand());
+ }
+
+ @Test
+ public void testProduceUnitActionCommand() {
+ Unit apc = game.getMap().getUnitOn(6, 2);
+
+ CWAction produceUnitAction =
ActionFactory.buildProduceUnitAction(apc, "infantry");
+ Assert.assertEquals("produce 6 2 infantry",
produceUnitAction.getActionCommand());
+ }
+
+ @Test
+ public void testEndGame() {
+ CWAction endTurnAction = ActionFactory.buildEndTurnAction();
+ Assert.assertEquals("end_turn", endTurnAction.getActionCommand());
+ }
+}
=======================================
--- /trunk/v2/resources/res/data/lang/Languages.properties Sun Feb 27
04:51:33 2011
+++ /trunk/v2/resources/res/data/lang/Languages.properties Sun Mar 20
06:49:26 2011
@@ -72,7 +72,7 @@
jet = Interceptor

# Unit menu
-launch = Launch
+launch = Take off
load = Load
join = Join
wait = Wait
@@ -80,6 +80,7 @@
supply = Supply
capture = Capture
build = Build
+produce = Produce
drop = Drop
flare = Flare
dive = Dive
=======================================
--- /trunk/v2/resources/res/plugin/dor/data/units.xml Mon Mar 14 07:18:14
2011
+++ /trunk/v2/resources/res/plugin/dor/data/units.xml Sun Mar 20 06:49:26
2011
@@ -2,7 +2,7 @@

<!ELEMENT unit (price, movement, vision,
canHide?, canDive?, canCapture?, canJoin?, canFlare?,
canLaunchUnit?,
- transports?, transformTerrains?, buildCities?, buildUnits?,
supplyUnits?, supplyUnitsInTransport?,
+ transports?, transformTerrains?, buildCities?, produces?,
supplyUnits?, supplyUnitsInTransport?,
maxExperience?, maxHp, supplyRange?, maxSupplies,
suppliesPerTurn?, suppliesPerTurnWhenHidden?,
maxConstructionMaterial?, healRate?,
armyBranch,movementType, primaryWeaponName?, secondaryWeaponName?,
description)>
@@ -29,7 +29,7 @@

<!ELEMENT transformTerrains (entry*)>
<!ELEMENT buildCities (entry*)>
- <!ELEMENT buildUnits (unitID*)>
+ <!ELEMENT produces (unitID*)>
<!ELEMENT supplyUnitsInTransport (unitID*)>
<!ELEMENT supplyUnits (unitID*)>

@@ -551,9 +551,9 @@
<unitID>fighter</unitID>
</transports>

- <buildUnits>
+ <produces>
<unitID>sea_plane</unitID>
- </buildUnits>
+ </produces>

<supplyUnitsInTransport>
<unitID>jet</unitID>
@@ -568,6 +568,7 @@
<maxHp>100</maxHp>
<maxSupplies>99</maxSupplies>
<suppliesPerTurn>1</suppliesPerTurn>
+ <maxConstructionMaterial>4</maxConstructionMaterial>
<healRate>2</healRate>

<armyBranch>NAVAL</armyBranch>
=======================================
--- /trunk/v2/src/com/customwars/client/action/AbstractCWAction.java Wed
Feb 17 14:18:31 2010
+++ /trunk/v2/src/com/customwars/client/action/AbstractCWAction.java Sun
Mar 20 06:49:26 2011
@@ -2,8 +2,6 @@

/**
* Default implementation of a CWAction
- *
- * @author stefan
*/
public abstract class AbstractCWAction implements CWAction {
private String name;
@@ -39,11 +37,11 @@
}

@Override
- public String getActionText() {
+ public String getActionCommand() {
return null;
}

public String toString() {
- return name + " canUndo=" + canUndo + " Completed=" + actionCompleted;
+ return String.format("%s canUndo=%s Completed=%s actionTxt=%s", name,
canUndo, actionCompleted, getActionCommand());
}
}
=======================================
--- /trunk/v2/src/com/customwars/client/action/ActionBag.java Wed Feb 17
14:18:31 2010
+++ /trunk/v2/src/com/customwars/client/action/ActionBag.java Sun Mar 20
06:49:26 2011
@@ -1,6 +1,5 @@
package com.customwars.client.action;

-import com.customwars.client.model.map.Location;
import com.customwars.client.ui.state.InGameContext;

import java.util.ArrayList;
@@ -10,10 +9,8 @@
* A List of CWActions
* If the list contains delayed actions then this class
* Will wait with processing the next actions until the delayed action has
been completed.
- *
+ * <p/>
* undo doesn't wait for the action to be completed, undo code should not
be delayed.
- *
- * @author stefan
*/
public class ActionBag implements CWAction {
private static final int CHECK_ACTION_COMPLETE_DELAY = 250;
@@ -23,11 +20,12 @@
private final List<CWAction> actions;
private final String actionName;
private InGameContext context;
- private String actionText;
+ private String actionCommand;

public ActionBag(String actionName) {
this.actionName = actionName;
actions = new ArrayList<CWAction>();
+ actionCommand = buildActionCommand();
}

public void invoke(InGameContext context) {
@@ -81,6 +79,24 @@

public void add(CWAction action) {
actions.add(action);
+ actionCommand = buildActionCommand();
+ }
+
+ private String buildActionCommand() {
+ StringBuilder actionCMDBuilder = new
StringBuilder(actionName.toLowerCase());
+ actionCMDBuilder.append(" ");
+
+ for (CWAction action : actions) {
+ String actionCMD = action.getActionCommand();
+ if (actionCMD != null) {
+ actionCMDBuilder.append(actionCMD);
+ actionCMDBuilder.append(" ");
+ }
+ }
+
+ // Remove last " " space.
+ actionCMDBuilder.deleteCharAt(actionCMDBuilder.length() - 1);
+ return actionCMDBuilder.toString();
}

/**
@@ -93,40 +109,6 @@
}
return true;
}
-
- public void setActionText(String actionText) {
- this.actionText = actionText;
- }
-
- /**
- * Set the action text where the action happens on 1 tile
- */
- public void setActionText(Location tile, Integer... params) {
- setActionText(tile, null, params);
- }
-
- /**
- * Set the action text where the action starts at <from> and ends on <to>
- */
- public void setActionText(Location from, Location to, Integer... params)
{
- StringBuilder actionTextBuilder = new
StringBuilder(actionName.toLowerCase());
- actionTextBuilder
- .append(' ').append(from.getCol())
- .append(' ').append(from.getRow());
-
- if (to != null) {
- actionTextBuilder
- .append(' ').append(to.getCol())
- .append(' ').append(to.getRow());
- }
-
- for (Integer param : params) {
- actionTextBuilder
- .append(' ')
- .append(param);
- }
- this.actionText = actionTextBuilder.toString();
- }

public String getName() {
return actionName;
@@ -136,8 +118,8 @@
return !started;
}

- public String getActionText() {
- return actionText;
+ public String getActionCommand() {
+ return actionCommand;
}

@Override
@@ -146,6 +128,9 @@
for (CWAction action : actions) {
strBuilder.append(action.getName()).append(" - ");
}
+
+ // Remove last " - "
+ strBuilder.delete(strBuilder.length() - 3, strBuilder.length());
return strBuilder.toString();
}
}
=======================================
--- /trunk/v2/src/com/customwars/client/action/ActionFactory.java Wed Feb
23 09:46:07 2011
+++ /trunk/v2/src/com/customwars/client/action/ActionFactory.java Sun Mar
20 06:49:26 2011
@@ -24,20 +24,14 @@
import com.customwars.client.action.unit.WaitAction;
import com.customwars.client.model.drop.DropLocation;
import com.customwars.client.model.drop.DropLocationsQueue;
+import com.customwars.client.model.game.Player;
import com.customwars.client.model.gameobject.City;
import com.customwars.client.model.gameobject.Terrain;
import com.customwars.client.model.gameobject.Unit;
import com.customwars.client.model.map.Location;
-import com.customwars.client.model.map.Tile;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;

/**
* Create in game actions
- *
- * @author stefan
*/
public class ActionFactory {
public static CWAction buildDropAction(Unit transport, Location from,
Location moveTo, DropLocationsQueue dropQueue) {
@@ -47,24 +41,13 @@
dropActions.add(new WaitAction(transport));

// Drop each unit in the drop queue to the user chosen drop location
- List<Integer> dropAction = new ArrayList<Integer>();
for (DropLocation dropLocation : dropQueue.getDropLocations()) {
dropActions.add(new DropAction(transport, dropLocation));
dropActions.add(new WaitAction(dropLocation.getUnit()));
- buildDropActionText(dropAction, transport, dropLocation);
}
dropActions.add(new ClearInGameStateAction());
- dropActions.setActionText(from, moveTo, dropAction.toArray(new
Integer[]{dropAction.size()}));
return dropActions;
}
-
- private static void buildDropActionText(Collection<Integer>
actionTextParam, Unit transport, DropLocation dropLocation) {
- Unit unitToDrop = dropLocation.getUnit();
- int unitInTransportIndex = transport.indexOf(unitToDrop);
- actionTextParam.add(unitInTransportIndex);
- actionTextParam.add(dropLocation.getLocation().getCol());
- actionTextParam.add(dropLocation.getLocation().getRow());
- }

public static CWAction buildMoveAction(Unit unit, Location moveTo) {
ActionBag moveActions = new ActionBag("Move");
@@ -72,7 +55,6 @@
moveActions.add(new MoveAnimatedAction(unit.getLocation(), moveTo));
moveActions.add(new WaitAction(unit));
moveActions.add(new ClearInGameStateAction());
- moveActions.setActionText(unit, moveTo);
return moveActions;
}

@@ -83,7 +65,6 @@
captureActions.add(new CaptureAction(unit, city));
captureActions.add(new WaitAction(unit));
captureActions.add(new ClearInGameStateAction());
- captureActions.setActionText(unit, city.getLocation());
return captureActions;
}

@@ -94,7 +75,6 @@
loadActions.add(new LoadAction(unit, transport));
loadActions.add(new WaitAction(unit));
loadActions.add(new ClearInGameStateAction());
- loadActions.setActionText(unit, transport);
return loadActions;
}

@@ -105,7 +85,6 @@
supplyActions.add(new SupplyAction(supplier));
supplyActions.add(new WaitAction(supplier));
supplyActions.add(new ClearInGameStateAction());
- supplyActions.setActionText(supplier, moveTo);
return supplyActions;
}

@@ -116,7 +95,6 @@
joinActions.add(new JoinAction(unit, target));
joinActions.add(new WaitAction(target));
joinActions.add(new ClearInGameStateAction());
- joinActions.setActionText(unit, target);
return joinActions;
}

@@ -127,7 +105,6 @@
attackActions.add(new AttackUnitAction(attacker, defender));
attackActions.add(new WaitAction(attacker));
attackActions.add(new ClearInGameStateAction());
- attackActions.setActionText(attacker, moveTo, defender.getCol(),
defender.getRow());
return attackActions;
}

@@ -138,37 +115,33 @@
attackActions.add(new AttackCityAction(attacker, city));
attackActions.add(new WaitAction(attacker));
attackActions.add(new ClearInGameStateAction());
- attackActions.setActionText(attacker, moveTo,
city.getLocation().getCol(), city.getLocation().getRow());
return attackActions;
}

- public static CWAction buildAddUnitToTileAction(Unit unit, Tile
selected, boolean canUndo) {
+ public static CWAction buildAddUnitToTileAction(Unit unit, Player
newUnitOwner, Location location) {
ActionBag buildUnitActions = new ActionBag("Build_Unit");
buildUnitActions.add(new InitAction());
- buildUnitActions.add(new AddUnitToTileAction(unit, selected, canUndo));
+ buildUnitActions.add(new AddUnitToTileAction(unit, newUnitOwner,
location));
buildUnitActions.add(new WaitAction(unit));
buildUnitActions.add(new ClearInGameStateAction());
- buildUnitActions.setActionText(selected, unit.getStats().getID(),
unit.getOwner().getId());
return buildUnitActions;
}

public static CWAction buildEndTurnAction() {
- ActionBag endTurnAction = new ActionBag("End Turn");
- endTurnAction.add(new ClearInGameStateAction());
- endTurnAction.add(new EndTurnAction());
- endTurnAction.setActionText("end_turn");
- return endTurnAction;
+ ActionBag endTurnActions = new ActionBag("End_Turn");
+ endTurnActions.add(new ClearInGameStateAction());
+ endTurnActions.add(new EndTurnAction());
+ return endTurnActions;
}

public static CWAction buildLaunchRocketAction(Unit unit, City city,
Location rocketDestination) {
- ActionBag launchRocketAction = new ActionBag("Launch_Rocket");
- launchRocketAction.add(new InitAction());
- launchRocketAction.add(new MoveAnimatedAction(unit.getLocation(),
city.getLocation()));
- launchRocketAction.add(new WaitAction(unit));
- launchRocketAction.add(new LaunchRocketAction(city, unit,
rocketDestination));
- launchRocketAction.add(new ClearInGameStateAction());
- launchRocketAction.setActionText(unit, city.getLocation(),
rocketDestination.getCol(), rocketDestination.getRow());
- return launchRocketAction;
+ ActionBag launchRocketActions = new ActionBag("Launch_Rocket");
+ launchRocketActions.add(new InitAction());
+ launchRocketActions.add(new MoveAnimatedAction(unit.getLocation(),
city.getLocation()));
+ launchRocketActions.add(new WaitAction(unit));
+ launchRocketActions.add(new LaunchRocketAction(city, unit,
rocketDestination));
+ launchRocketActions.add(new ClearInGameStateAction());
+ return launchRocketActions;
}

public static CWAction buildTransformTerrainAction(Unit unit, Location
to, Terrain transformTo) {
@@ -177,64 +150,56 @@
transformTerrainActions.add(new MoveAnimatedAction(unit.getLocation(),
to));
transformTerrainActions.add(new WaitAction(unit));
transformTerrainActions.add(new TransformTerrainAction(unit, to,
transformTo));
- transformTerrainActions.add(new ClearInGameStateAction());
- transformTerrainActions.setActionText(unit, to, transformTo.getID());
return transformTerrainActions;
}

public static CWAction buildFireFlareAction(Unit unit, Location to,
Location flareCenter) {
- ActionBag transformTerrainAction = new ActionBag("Flare");
- transformTerrainAction.add(new InitAction());
- transformTerrainAction.add(new MoveAnimatedAction(unit.getLocation(),
to));
- transformTerrainAction.add(new WaitAction(unit));
- transformTerrainAction.add(new FireFlareAction(unit, flareCenter));
- transformTerrainAction.add(new ClearInGameStateAction());
- transformTerrainAction.setActionText(unit.getLocation(), to,
flareCenter.getCol(), flareCenter.getRow());
- return transformTerrainAction;
+ ActionBag flareActions = new ActionBag("Flare");
+ flareActions.add(new InitAction());
+ flareActions.add(new MoveAnimatedAction(unit.getLocation(), to));
+ flareActions.add(new WaitAction(unit));
+ flareActions.add(new FireFlareAction(unit, flareCenter));
+ flareActions.add(new ClearInGameStateAction());
+ return flareActions;
}

- public static CWAction buildConstructCityAction(Unit unit, int cityID,
Location to) {
- ActionBag buildCityAction = new ActionBag("Build_City");
- buildCityAction.add(new InitAction());
- buildCityAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- buildCityAction.add(new WaitAction(unit));
- buildCityAction.add(new ConstructCityAction(unit, cityID, to));
- buildCityAction.add(new ClearInGameStateAction());
- buildCityAction.setActionText(unit, to, cityID);
- return buildCityAction;
+ public static CWAction buildConstructCityAction(Unit unit, String
cityID, Location to) {
+ ActionBag constructCityActions = new ActionBag("Build_City");
+ constructCityActions.add(new InitAction());
+ constructCityActions.add(new MoveAnimatedAction(unit.getLocation(),
to));
+ constructCityActions.add(new WaitAction(unit));
+ constructCityActions.add(new ConstructCityAction(unit, cityID, to));
+ constructCityActions.add(new ClearInGameStateAction());
+ return constructCityActions;
}

public static CWAction buildDiveAction(Unit unit, Location to) {
- ActionBag diveAction = new ActionBag("Dive");
- diveAction.add(new InitAction());
- diveAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- diveAction.add(new WaitAction(unit));
- diveAction.add(new DiveAction(unit));
- diveAction.add(new ClearInGameStateAction());
- diveAction.setActionText(unit, to);
- return diveAction;
+ ActionBag diveActions = new ActionBag("Dive");
+ diveActions.add(new InitAction());
+ diveActions.add(new MoveAnimatedAction(unit.getLocation(), to));
+ diveActions.add(new WaitAction(unit));
+ diveActions.add(new DiveAction(unit));
+ diveActions.add(new ClearInGameStateAction());
+ return diveActions;
}

public static CWAction buildSurfaceAction(Unit unit, Location to) {
- ActionBag surfaceAction = new ActionBag("Surface");
- surfaceAction.add(new InitAction());
- surfaceAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- surfaceAction.add(new WaitAction(unit));
- surfaceAction.add(new SurfaceAction(unit));
- surfaceAction.add(new ClearInGameStateAction());
- surfaceAction.setActionText(unit, to);
- return surfaceAction;
+ ActionBag surfaceActions = new ActionBag("Surface");
+ surfaceActions.add(new InitAction());
+ surfaceActions.add(new MoveAnimatedAction(unit.getLocation(), to));
+ surfaceActions.add(new WaitAction(unit));
+ surfaceActions.add(new SurfaceAction(unit));
+ surfaceActions.add(new ClearInGameStateAction());
+ return surfaceActions;
}

- public static CWAction buildProduceUnitAction(Unit producer, Unit
unitToBuild, Location to) {
- ActionBag produceUnitAction = new ActionBag("Produce");
- produceUnitAction.add(new InitAction());
- produceUnitAction.add(new MoveAnimatedAction(producer.getLocation(),
to));
- produceUnitAction.add(new ProduceUnitAction(producer, unitToBuild));
- produceUnitAction.add(new WaitAction(producer));
- produceUnitAction.add(new ClearInGameStateAction());
- produceUnitAction.setActionText(producer.getLocation(), to,
unitToBuild.getStats().getID());
- return produceUnitAction;
+ public static CWAction buildProduceUnitAction(Unit producer, String
unitNameToProduce) {
+ ActionBag produceUnitActions = new ActionBag("Produce");
+ produceUnitActions.add(new InitAction());
+ produceUnitActions.add(new ProduceUnitAction(producer,
unitNameToProduce));
+ produceUnitActions.add(new WaitAction(producer));
+ produceUnitActions.add(new ClearInGameStateAction());
+ return produceUnitActions;
}

public static CWAction buildEndGameAction() {
@@ -242,34 +207,31 @@
}

public static CWAction buildLoadCOAction(Unit unit, Location to) {
- ActionBag loadCOAction = new ActionBag("loadCO");
- loadCOAction.add(new InitAction());
- loadCOAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- loadCOAction.add(new LoadCOAction(unit));
- loadCOAction.add(new ClearInGameStateAction());
- loadCOAction.setActionText(unit, to);
- return loadCOAction;
+ ActionBag loadCOActions = new ActionBag("load_CO");
+ loadCOActions.add(new InitAction());
+ loadCOActions.add(new MoveAnimatedAction(unit.getLocation(), to));
+ loadCOActions.add(new LoadCOAction(unit));
+ loadCOActions.add(new ClearInGameStateAction());
+ return loadCOActions;
}

public static CWAction buildCOPowerAction(Unit unit, Location to) {
- ActionBag coPowerAction = new ActionBag("co_Power");
- coPowerAction.add(new InitAction());
- coPowerAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- coPowerAction.add(new COPowerAction());
- coPowerAction.add(new WaitAction(unit));
- coPowerAction.add(new ClearInGameStateAction());
- coPowerAction.setActionText(unit, to);
- return coPowerAction;
+ ActionBag coPowerActions = new ActionBag("co_Power");
+ coPowerActions.add(new InitAction());
+ coPowerActions.add(new MoveAnimatedAction(unit.getLocation(), to));
+ coPowerActions.add(new COPowerAction());
+ coPowerActions.add(new WaitAction(unit));
+ coPowerActions.add(new ClearInGameStateAction());
+ return coPowerActions;
}

public static CWAction buildCOSuperPowerAction(Unit unit, Location to) {
- ActionBag coSuperPowerAction = new ActionBag("co_Super_Power");
- coSuperPowerAction.add(new InitAction());
- coSuperPowerAction.add(new MoveAnimatedAction(unit.getLocation(), to));
- coSuperPowerAction.add(new COSuperPowerAction());
- coSuperPowerAction.add(new WaitAction(unit));
- coSuperPowerAction.add(new ClearInGameStateAction());
- coSuperPowerAction.setActionText(unit, to);
- return coSuperPowerAction;
+ ActionBag coSuperPowerActions = new ActionBag("co_Super_Power");
+ coSuperPowerActions.add(new InitAction());
+ coSuperPowerActions.add(new MoveAnimatedAction(unit.getLocation(),
to));
+ coSuperPowerActions.add(new COSuperPowerAction());
+ coSuperPowerActions.add(new WaitAction(unit));
+ coSuperPowerActions.add(new ClearInGameStateAction());
+ return coSuperPowerActions;
}
}
=======================================
--- /trunk/v2/src/com/customwars/client/action/ActionManager.java Wed Feb
17 14:18:31 2010
+++ /trunk/v2/src/com/customwars/client/action/ActionManager.java Sun Mar
20 06:49:26 2011
@@ -10,14 +10,14 @@
/**
* Allow Actions to be executed
* Keep a history of actions that can be undone -> undoManager
- * Keep a history of actions that have an action text -> actionHistory
+ * Keep a history of actions that have an action command -> actionHistory
*/
public class ActionManager {
private static final Logger logger =
Logger.getLogger(ActionManager.class);
private final UndoManager undoManager;
private final InGameContext inGameContext;
private final List<CWAction> actionHistory;
- private CWAction action;
+ private CWAction currentAction;

public ActionManager(InGameContext inGameContext) {
this.inGameContext = inGameContext;
@@ -27,7 +27,8 @@

/**
* Executes an action
- * If the action can be undone then it is added to the undo history, and
canUndo will return true
+ * If the action can be undone then it is added to the undo history, and
{@link #canUndo()} will return true
+ * If the action had an action command then it is added to the action
history.
*/
public void doAction(CWAction action) {
if (action == null) {
@@ -35,33 +36,33 @@
return;
}

- if (this.action == null) {
+ if (currentAction == null) {
invokeAction(action);

if (action.canUndo()) {
undoManager.addUndoAction(action, inGameContext);
}
} else {
- logger.debug("Skipping action -> " + action.getName() + " other
action " + this.action.getName() + " is still executing.");
+ logger.debug("Skipping action -> " + action.getName() + " other
action " + currentAction.getName() + " is still executing.");
}
}

private void invokeAction(CWAction action) {
logger.debug("Launching action -> " + action.getName());
- this.action = action;
+ currentAction = action;
action.invoke(inGameContext);

- if (action.getActionText() != null) {
+ if (action.getActionCommand() != null) {
actionHistory.add(action);
}
}

public void update(int elapsedTime) {
- if (action != null) {
- action.update(elapsedTime);
-
- if (action.isCompleted()) {
- action = null;
+ if (currentAction != null) {
+ currentAction.update(elapsedTime);
+
+ if (currentAction.isCompleted()) {
+ currentAction = null;
}
}
}
@@ -80,7 +81,7 @@
}

public boolean isActionCompleted() {
- return action == null;
+ return currentAction == null;
}

public List<CWAction> getExecutedActions() {
=======================================
--- /trunk/v2/src/com/customwars/client/action/ActionParser.java Fri Feb 18
15:15:52 2011
+++ /trunk/v2/src/com/customwars/client/action/ActionParser.java Sun Mar 20
06:49:26 2011
@@ -18,8 +18,6 @@

/**
* Parse a string into a CWAction
- *
- * @author stefan
*/
public class ActionParser {
private static final Logger logger =
Logger.getLogger(ActionParser.class);
@@ -79,6 +77,12 @@
return parseProduce(scanner);
} else if (actionName.equals("end_turn")) {
return ActionFactory.buildEndTurnAction();
+ } else if (actionName.equals("load_co")) {
+ return parseLoadCO(scanner);
+ } else if (actionName.equals("co_power")) {
+ return parseCOP(scanner);
+ } else if (actionName.equals("co_super_power")) {
+ return parseSCOP(scanner);
} else {
logger.warn("Unknown action " + actionName);
throw new IllegalArgumentException("unknown action " + actionName);
@@ -186,13 +190,12 @@
private CWAction parseBuildUnit(Scanner scanner) {
int col = scanner.nextInt();
int row = scanner.nextInt();
- int unitID = scanner.nextInt();
+ String unitID = scanner.next();
int playerID = scanner.nextInt();
Tile selectTile = map.getTile(col, row);
Unit unit = UnitFactory.getUnit(unitID);
Player player = game.getPlayerByID(playerID);
- unit.setOwner(player);
- return ActionFactory.buildAddUnitToTileAction(unit, selectTile, false);
+ return ActionFactory.buildAddUnitToTileAction(unit, player,
selectTile);
}

private CWAction parseLaunchRocketAction(Scanner scanner) {
@@ -213,7 +216,7 @@
int fromRow = scanner.nextInt();
int toCol = scanner.nextInt();
int toRow = scanner.nextInt();
- int terrainID = scanner.nextInt();
+ String terrainID = scanner.next();
Location from = map.getTile(fromCol, fromRow);
Location to = map.getTile(toCol, toRow);
Unit unit = map.getUnitOn(from);
@@ -239,7 +242,7 @@
int fromRow = scanner.nextInt();
int toCol = scanner.nextInt();
int toRow = scanner.nextInt();
- int cityID = scanner.nextInt();
+ String cityID = scanner.next();
Location from = map.getTile(fromCol, fromRow);
Location to = map.getTile(toCol, toRow);
Unit unit = map.getUnitOn(from);
@@ -269,12 +272,38 @@
private CWAction parseProduce(Scanner scanner) {
int fromCol = scanner.nextInt();
int fromRow = scanner.nextInt();
+ String unitID = scanner.next();
+ Unit producer = map.getUnitOn(fromCol, fromRow);
+ return ActionFactory.buildProduceUnitAction(producer, unitID);
+ }
+
+ private CWAction parseLoadCO(Scanner scanner) {
+ int fromCol = scanner.nextInt();
+ int fromRow = scanner.nextInt();
int toCol = scanner.nextInt();
int toRow = scanner.nextInt();
- int unitID = scanner.nextInt();
- Unit producer = map.getUnitOn(fromCol, fromRow);
+ Unit unit = map.getUnitOn(fromCol, fromRow);
Location to = map.getTile(toCol, toRow);
- Unit unitToBuild = UnitFactory.getUnit(unitID);
- return ActionFactory.buildProduceUnitAction(producer, unitToBuild, to);
+ return ActionFactory.buildLoadCOAction(unit, to);
+ }
+
+ private CWAction parseCOP(Scanner scanner) {
+ int fromCol = scanner.nextInt();
+ int fromRow = scanner.nextInt();
+ int toCol = scanner.nextInt();
+ int toRow = scanner.nextInt();
+ Unit unit = map.getUnitOn(fromCol, fromRow);
+ Location to = map.getTile(toCol, toRow);
+ return ActionFactory.buildCOPowerAction(unit, to);
+ }
+
+ private CWAction parseSCOP(Scanner scanner) {
+ int fromCol = scanner.nextInt();
+ int fromRow = scanner.nextInt();
+ int toCol = scanner.nextInt();
+ int toRow = scanner.nextInt();
+ Unit unit = map.getUnitOn(fromCol, fromRow);
+ Location to = map.getTile(toCol, toRow);
+ return ActionFactory.buildCOSuperPowerAction(unit, to);
}
}
=======================================
--- /trunk/v2/src/com/customwars/client/action/CWAction.java Wed Feb 17
14:18:31 2010
+++ /trunk/v2/src/com/customwars/client/action/CWAction.java Sun Mar 20
06:49:26 2011
@@ -5,10 +5,8 @@
/**
* Interface for each Action in the game
* an Action can be done and undone.
- *
- * When the action has been performed isActionCompleted will return true
- *
- * @author stefan
+ * <p/>
+ * When the action has been performed {@link #isCompleted()} will return
true
*/
public interface CWAction {
void invoke(InGameContext context);
@@ -24,14 +22,14 @@
boolean isCompleted();

/**
- * The action text that can be used to recreated this action. Each
parameter is separated with a space.
+ * The action command that can be used to recreate this action. Each
parameter is separated with a space.
* Following pattern is used:
* capture 0 0
* build_unit 0 0
* move 0 0 1 1
* <action_name> <param1> <param2>
*
- * @return The action text, null if this action can not be recreated
+ * @return The action command, null if this action has no action command.
*/
- String getActionText();
-}
+ String getActionCommand();
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/city/LaunchRocketAction.java
Sat Aug 7 10:05:42 2010
+++ /trunk/v2/src/com/customwars/client/action/city/LaunchRocketAction.java
Sun Mar 20 06:49:26 2011
@@ -2,6 +2,7 @@

import com.customwars.client.App;
import com.customwars.client.SFX;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.City;
@@ -72,4 +73,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(rocketDestination).build();
+ }
+}
=======================================
---
/trunk/v2/src/com/customwars/client/action/unit/AddUnitToTileAction.java
Thu Apr 1 07:20:09 2010
+++
/trunk/v2/src/com/customwars/client/action/unit/AddUnitToTileAction.java
Sun Mar 20 06:49:26 2011
@@ -1,11 +1,12 @@
package com.customwars.client.action.unit;

import com.customwars.client.App;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.game.Player;
import com.customwars.client.model.gameobject.Unit;
-import com.customwars.client.model.map.Tile;
+import com.customwars.client.model.map.Location;
import com.customwars.client.network.MessageSender;
import com.customwars.client.network.NetworkException;
import com.customwars.client.ui.GUI;
@@ -21,14 +22,14 @@
private GameController gameController;
private MessageSender messageSender;
private final Unit unit; // Unit to be put on tile
- private final Tile tile; // Tile to add unit to
- private final Player unitOwner;
-
- public AddUnitToTileAction(Unit unit, Tile tile, boolean canUndo) {
- super("Add unit to tile", canUndo);
+ private final Location location; // Tile to add unit to
+ private final Player newUnitOwner;
+
+ public AddUnitToTileAction(Unit unit, Player newUnitOwner, Location
location) {
+ super("Add unit to tile", false);
this.unit = unit;
- this.tile = tile;
- this.unitOwner = unit.getOwner();
+ this.location = location;
+ this.newUnitOwner = newUnitOwner;
}

protected void init(InGameContext inGameContext) {
@@ -37,13 +38,13 @@
}

protected void invokeAction() {
- gameController.buildUnit(unit, tile, unitOwner);
+ gameController.buildUnit(unit, location, newUnitOwner);
if (App.isMultiplayer()) sendBuildUnit();
}

private void sendBuildUnit() {
try {
- messageSender.buildUnit(unit, tile, unitOwner);
+ messageSender.buildUnit(unit, location, newUnitOwner);
} catch (NetworkException ex) {
logger.warn("Could not send build unit", ex);
if (GUI.askToResend(ex) == GUI.YES_OPTION) {
@@ -51,4 +52,12 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder()
+ .add(location)
+ .add(unit)
+ .add(newUnitOwner).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/AttackCityAction.java
Thu Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/AttackCityAction.java
Sun Mar 20 06:49:26 2011
@@ -2,6 +2,7 @@

import com.customwars.client.App;
import com.customwars.client.SFX;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.City;
@@ -67,4 +68,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(city.getLocation()).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/AttackUnitAction.java
Thu Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/AttackUnitAction.java
Sun Mar 20 06:49:26 2011
@@ -2,6 +2,7 @@

import com.customwars.client.App;
import com.customwars.client.SFX;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.Unit;
@@ -83,4 +84,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(defender.getLocation()).build();
+ }
+}
=======================================
---
/trunk/v2/src/com/customwars/client/action/unit/ConstructCityAction.java
Sun Jul 25 04:25:09 2010
+++
/trunk/v2/src/com/customwars/client/action/unit/ConstructCityAction.java
Sun Mar 20 06:49:26 2011
@@ -1,6 +1,7 @@
package com.customwars.client.action.unit;

import com.customwars.client.App;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.City;
@@ -20,9 +21,9 @@
private MessageSender messageSender;
private final Unit unit;
private final Location to;
- private final int cityID;
-
- public ConstructCityAction(Unit unit, int cityID, Location moveTo) {
+ private final String cityID;
+
+ public ConstructCityAction(Unit unit, String cityID, Location moveTo) {
super("Construct City", false);
this.unit = unit;
this.to = moveTo;
@@ -75,4 +76,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(cityID).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/DropAction.java Thu
Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/DropAction.java Sun Mar
20 06:49:26 2011
@@ -2,6 +2,7 @@

import com.customwars.client.App;
import com.customwars.client.SFX;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.model.drop.DropLocation;
import com.customwars.client.model.gameobject.Unit;
import com.customwars.client.model.map.Location;
@@ -13,17 +14,17 @@

/**
* Drop the unit within the transport to the drop location
- *
- * @author stefan
*/
public class DropAction extends MoveAnimatedAction {
private static final Logger logger = Logger.getLogger(DropAction.class);
- private MessageSender messageSender;
private final Unit transport;
+ private final DropLocation dropLocation;
+ private MessageSender messageSender;

public DropAction(Unit transport, DropLocation dropLocation) {
super(dropLocation.getUnit(), transport.getLocation(),
dropLocation.getLocation());
this.transport = transport;
+ this.dropLocation = dropLocation;
}

protected void init(InGameContext inGameContext) {
@@ -73,4 +74,11 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder()
+ .add(transport.indexOf(dropLocation.getUnit()))
+ .add(dropLocation.getLocation()).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/FireFlareAction.java
Thu Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/FireFlareAction.java
Sun Mar 20 06:49:26 2011
@@ -1,6 +1,7 @@
package com.customwars.client.action.unit;

import com.customwars.client.App;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.Unit;
@@ -14,8 +15,6 @@
/**
* Fires a flare
* all tiles within flare range, surrounding the center tile are revealed.
- *
- * @author stefan
*/
public class FireFlareAction extends DirectAction {
private static final Logger logger =
Logger.getLogger(FireFlareAction.class);
@@ -63,4 +62,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(flareCenter).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
Fri Feb 18 15:19:41 2011
+++ /trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
Sun Mar 20 06:49:26 2011
@@ -2,6 +2,7 @@

import com.customwars.client.App;
import com.customwars.client.SFX;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DelayedAction;
import com.customwars.client.controller.CursorController;
import com.customwars.client.model.GameController;
@@ -21,15 +22,13 @@
/**
* Moves the unit animated from the 'from' location to the 'to' location
* by moving the unit on each tile between those locations.
- *
- * @author stefan
*/
public class MoveAnimatedAction extends DelayedAction {
private static final Logger logger =
Logger.getLogger(MoveAnimatedAction.class);
private CursorController cursorControl;
private GameController gameController;
private MessageSender messageSender;
- InGameContext context;
+ InGameContext inGameContext;
MoveTraverse moveTraverse;
Game game;
Map map;
@@ -62,7 +61,7 @@
unit.setUnitState(UnitState.IDLE);
}

- this.context = inGameContext;
+ this.inGameContext = inGameContext;
game = inGameContext.getObj(Game.class);
map = game.getMap();
moveTraverse = inGameContext.getObj(MoveTraverse.class);
@@ -89,9 +88,9 @@
void pathMoveComplete() {
if (moveTraverse.foundTrapper()) {
SFX.playSound("trapped");
- context.setTrapped(true);
+ inGameContext.setTrapped(true);
} else {
- context.setTrapped(false);
+ inGameContext.setTrapped(false);
}

cursorControl.setCursorLocked(false);
@@ -114,4 +113,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(from).add(to).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/ProduceUnitAction.java
Thu Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/ProduceUnitAction.java
Sun Mar 20 06:49:26 2011
@@ -1,6 +1,7 @@
package com.customwars.client.action.unit;

import com.customwars.client.App;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.Unit;
@@ -14,14 +15,14 @@
private static final Logger logger =
Logger.getLogger(ProduceUnitAction.class);
private InGameContext inGameContext;
private final Unit producer;
- private final Unit unitToBuild;
+ private final String unitToProduce;
private GameController gameController;
private MessageSender messageSender;

- public ProduceUnitAction(Unit producer, Unit unitToBuild) {
+ public ProduceUnitAction(Unit producer, String unitToProduce) {
super("produce unit", false);
this.producer = producer;
- this.unitToBuild = unitToBuild;
+ this.unitToProduce = unitToProduce;
}

@Override
@@ -39,14 +40,14 @@
}

private void produce() {
- logger.debug(producer.getStats().getName() + " producing a " +
unitToBuild.getStats().getName());
- gameController.buildUnit(unitToBuild, producer, producer.getOwner());
+ logger.debug(producer.getStats().getName() + " producing a " +
unitToProduce);
+ gameController.produceUnit(producer, unitToProduce);
if (App.isMultiplayer()) sendProduce();
}

private void sendProduce() {
try {
- messageSender.buildUnit(unitToBuild, producer, producer.getOwner());
+ messageSender.produceUnit(producer, unitToProduce,
producer.getOwner());
} catch (NetworkException ex) {
logger.warn("Could not send produce unit", ex);
if (GUI.askToResend(ex) == GUI.YES_OPTION) {
@@ -54,4 +55,11 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder()
+ .add(producer.getLocation())
+ .add(unitToProduce).build();
+ }
+}
=======================================
---
/trunk/v2/src/com/customwars/client/action/unit/TransformTerrainAction.java
Thu Apr 1 07:20:09 2010
+++
/trunk/v2/src/com/customwars/client/action/unit/TransformTerrainAction.java
Sun Mar 20 06:49:26 2011
@@ -1,6 +1,7 @@
package com.customwars.client.action.unit;

import com.customwars.client.App;
+import com.customwars.client.action.ActionCommandEncoder;
import com.customwars.client.action.DirectAction;
import com.customwars.client.model.GameController;
import com.customwars.client.model.gameobject.Terrain;
@@ -60,4 +61,9 @@
}
}
}
-}
+
+ @Override
+ public String getActionCommand() {
+ return new ActionCommandEncoder().add(transformToTerrain).build();
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/controller/HumanCityController.java
Mon Feb 21 13:11:03 2011
+++ /trunk/v2/src/com/customwars/client/controller/HumanCityController.java
Sun Mar 20 06:49:26 2011
@@ -27,8 +27,6 @@
/**
* Allows a human to control a city
* by using a gui
- *
- * @author stefan
*/
public class HumanCityController extends CityController {
private static final org.newdawn.slick.Color DARKER_TEXT_COLOR = new
org.newdawn.slick.Color(255, 255, 255, 100);
@@ -74,8 +72,7 @@
CWAction addUnitToTileAction;

if (canAffordUnit) {
- unit.setOwner(city.getOwner());
- addUnitToTileAction =
ActionFactory.buildAddUnitToTileAction(unit, selected, false);
+ addUnitToTileAction =
ActionFactory.buildAddUnitToTileAction(unit, city.getOwner(), selected);
} else {
addUnitToTileAction = null;
}
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitController.java Mon
Feb 28 12:51:11 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitController.java Sun
Mar 20 06:49:26 2011
@@ -7,6 +7,7 @@
import com.customwars.client.model.gameobject.Locatable;
import com.customwars.client.model.gameobject.Terrain;
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.Location;
import com.customwars.client.model.map.Map;
@@ -211,8 +212,45 @@
return unit.canConstructCityOn(selected.getTerrain());
}

- boolean canBuildUnit() {
- return unit.canBuildUnit();
+ /**
+ * #1 The unit with the lowest produce cost can be bought by the player
+ * #2 Only transporting units can produce a unit
+ * #3 There is min 1 free place in the transport
+ * #4 The unit did not move
+ * #5 The unit has min 1 construction material
+ */
+ boolean canStartProduceUnit(Tile from) {
+ UnitStats stats = unit.getStats();
+
+ int lowestUnitPrice = Integer.MAX_VALUE;
+ for (String unitName : stats.getUnitsThatCanBeProduced()) {
+ int price = UnitFactory.getUnit(unitName).getPrice();
+ if (price < lowestUnitPrice) {
+ lowestUnitPrice = price;
+ }
+ }
+
+ boolean canProduce = stats.canProduceUnits();
+ boolean canAfford = unit.getOwner().isWithinBudget(lowestUnitPrice);
+ boolean canTransport = stats.canTransport() && !unit.isTransportFull();
+ boolean moved = from != unit.getLocation();
+ boolean hasMaterials = unit.hasConstructionMaterials();
+
+ return canProduce && canAfford && canTransport && !moved &&
hasMaterials;
+ }
+
+ /**
+ * #1 Same conditions as canStartProduce
+ * #2 The unitID can be bought
+ * #3 The unitID can be produced
+ */
+ public boolean canProduceUnit(Tile from, String unitID) {
+ boolean canStartProduce = canStartProduceUnit(from);
+ boolean canProduce = unit.getStats().canProduceUnit(unitID);
+ int price = UnitFactory.getUnit(unitID).getPrice();
+ boolean canBuy = unit.getOwner().isWithinBudget(price);
+
+ return canStartProduce && canProduce && canBuy;
}

boolean canDive() {
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Mon
Mar 14 07:18:14 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Sun
Mar 20 06:49:26 2011
@@ -14,7 +14,6 @@
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.map.Location;
import com.customwars.client.model.map.Map;
import com.customwars.client.model.map.Tile;
@@ -37,7 +36,7 @@
private boolean canDropUnit, canCapture, canSupply, canStartAttack,
canWait, canJoin, canLoad;
private boolean canLaunchRocketFromCity, canTransformTerrain;
private boolean canFireFlare;
- private boolean canBuildCity, canBuildUnit;
+ private boolean canBuildCity, canProduceUnit;
private boolean canDive, canSurface;
private final InGameContext inGameContext;
private final Unit unit;
@@ -115,14 +114,8 @@
}

private String getDropMenuItemText(Unit unitInTransport) {
- String menuText;
String unitName = App.translate(unitInTransport.getStats().getName());
- if (unitInTransport.isLand()) {
- menuText = App.translate("drop") + " - " + unitName;
- } else {
- menuText = App.translate("launch") + " - " + unitName;
- }
- return menuText;
+ return App.translate("drop") + " - " + unitName;
}

private void buildUnitContextMenu(Tile from, Tile selected) {
@@ -161,7 +154,7 @@
canTransformTerrain = controller.canTransformTerrain(selected);
canFireFlare = controller.canFireFlare(from);
canBuildCity = controller.canBuildCity(selected);
- canBuildUnit = controller.canBuildUnit();
+ canProduceUnit = controller.canStartProduceUnit(from);
canDive = controller.canDive();
canSurface = controller.canSurface();
canLoadCO = controller.canLoadCO(from);
@@ -189,12 +182,13 @@
map.teleport(to, from, unit);
}

- if (canBuildUnit) {
- for (String unitID : unit.getStats().getUnitsThatCanBeBuild()) {
- Unit unitThatCanBeBuild = UnitFactory.getUnit(unitID);
- CWAction buildUnitAction =
ActionFactory.buildProduceUnitAction(unit, unitThatCanBeBuild, to);
- String menuText = App.translate("build") + " - " +
App.translate(unitThatCanBeBuild.getStats().getName());
- addToMenu(buildUnitAction, menuText);
+ if (canProduceUnit) {
+ for (String unitID : unit.getStats().getUnitsThatCanBeProduced()) {
+ if (controller.canProduceUnit(from, unitID)) {
+ CWAction buildUnitAction =
ActionFactory.buildProduceUnitAction(unit, unitID);
+ String menuText = App.translate("produce") + " - " +
App.translate(unitID);
+ addToMenu(buildUnitAction, menuText);
+ }
}
}

@@ -243,7 +237,7 @@

if (canBuildCity) {
City city = getCityThatCanBeBuildOn(to);
- CWAction buildCityAction =
ActionFactory.buildConstructCityAction(unit, city.getID(), to);
+ CWAction buildCityAction =
ActionFactory.buildConstructCityAction(unit, city.getName(), to);
addToMenu(buildCityAction, App.translate("build") + ' ' +
App.translate(city.getName()));
}

=======================================
--- /trunk/v2/src/com/customwars/client/model/CWGameController.java Fri Mar
11 13:07:50 2011
+++ /trunk/v2/src/com/customwars/client/model/CWGameController.java Sun Mar
20 06:49:26 2011
@@ -11,6 +11,7 @@
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.UnitFight;
import com.customwars.client.model.gameobject.UnitState;
import com.customwars.client.model.gameobject.UnitVsCityFight;
@@ -184,11 +185,11 @@
}

@Override
- public boolean constructCity(Unit unit, int cityID, Location location) {
+ public boolean constructCity(Unit unit, String cityID, Location
location) {
City city = getCityUnderConstructionAt(location, cityID);
addCityUnderConstruction(location, city);

- // Note: When the city is constructed it is added to the player cities
collection.
+ // When the city is constructed it is added to the player cities
collection.
unit.construct(city);

if (unit.isConstructionComplete()) {
@@ -206,7 +207,7 @@
}
}

- public City getCityUnderConstructionAt(Location location, int cityID) {
+ public City getCityUnderConstructionAt(Location location, String cityID)
{
City city;
if (citiesUnderConstruction.containsKey(location)) {
city = citiesUnderConstruction.get(location);
@@ -266,7 +267,7 @@
// 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.canConstructCity()) {
+ if (unitThatMoved.hasConstructionMaterials()) {
Iterator<Location> iterator =
citiesUnderConstruction.keySet().iterator();

while (iterator.hasNext()) {
@@ -277,6 +278,12 @@
}
}
}
+
+ public void produceUnit(Unit producer, String unitToProduce) {
+ Unit unit = UnitFactory.getUnit(unitToProduce);
+ buildUnit(unit, producer, producer.getOwner());
+ producer.deCreaseConstructionMaterials();
+ }

@Override
public void buildUnit(Unit unit, Location location, Player player) {
=======================================
--- /trunk/v2/src/com/customwars/client/model/GameController.java Wed Feb
23 09:46:07 2011
+++ /trunk/v2/src/com/customwars/client/model/GameController.java Sun Mar
20 06:49:26 2011
@@ -95,7 +95,7 @@
* Constructing might take several turns before completion.
* Constructing a city costs construction materials.
*/
- boolean constructCity(Unit unit, int cityID, Location location);
+ boolean constructCity(Unit unit, String cityID, Location location);

void dive(Unit unit);

@@ -106,6 +106,15 @@
*/
void makeUnitWait(Unit unit);

+ /**
+ * Create a unit and load it into the producer unit.
+ * The unit cost is subtracted from the player.
+ *
+ * @param producer The unit that wants to create a new unit.
+ * @param unitToProduce The name of the unit to produce.
+ */
+ void produceUnit(Unit producer, String unitToProduce);
+
/**
* The player creates a new unit. The unit is placed on the location in
the map.
* Note that the location can be a Tile or a unit!
=======================================
--- /trunk/v2/src/com/customwars/client/model/game/GameReplay.java Fri Mar
26 08:19:00 2010
+++ /trunk/v2/src/com/customwars/client/model/game/GameReplay.java Sun Mar
20 06:49:26 2011
@@ -11,13 +11,12 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;

/**
* A replay of a game containing The initial game and a queue of text
actions.
* These text actions are converted to a CWAction when invoking the
execNextReplayAction method.
- *
+ * <p/>
* You can only move 1 step forward. The replay can only be run once.
* When at the last action hasMoreActions returns false.
*/
@@ -35,9 +34,9 @@
this.currentReplayIndex = 0;
}

- public void addActions(Collection<CWAction> actions) {
+ public void addActions(Iterable<CWAction> actions) {
for (CWAction action : actions) {
- replayQueue.add(action.getActionText());
+ replayQueue.add(action.getActionCommand());
}
}

=======================================
--- /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Mon Mar
14 07:18:14 2011
+++ /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Sun Mar
20 06:49:26 2011
@@ -85,6 +85,7 @@
supplies = otherUnit.supplies;
experience = otherUnit.experience;
unitState = otherUnit.unitState;
+ constructionMaterials = otherUnit.constructionMaterials;

primaryWeapon = otherUnit.primaryWeapon != null ? new
Weapon(otherUnit.primaryWeapon) : null;
secondaryWeapon = otherUnit.secondaryWeapon != null ? new
Weapon(otherUnit.secondaryWeapon) : null;
@@ -340,7 +341,7 @@
return stats.canTransport(unitName) && !isTransportFull();
}

- private boolean isTransportFull() {
+ public boolean isTransportFull() {
return transport.size() >= stats.getMaxTransportCount();
}

@@ -492,12 +493,12 @@
constructingCity = null;
}

- public boolean canConstructCity() {
+ public boolean hasConstructionMaterials() {
return constructionMaterials > 0;
}

public boolean canConstructCityOn(Terrain terrain) {
- return canConstructCity() && stats.canBuildCityOn(terrain);
+ return hasConstructionMaterials() && stats.canBuildCityOn(terrain);
}

//
---------------------------------------------------------------------------
@@ -916,24 +917,6 @@
public boolean isHidden() {
return hidden;
}
-
- /**
- * @return Can this unit build the specific unit
- */
- public boolean canBuildUnit(Unit unit) {
- return canBuildUnit() && stats.canBuildUnit(unit);
- }
-
- /**
- * Can this unit build units:
- * #1 Only transporting units can build a unit
- * #2 There is 1 free place in this transport
- *
- * @return Can this unit build units
- */
- public boolean canBuildUnit() {
- return stats.canTransport() && !isTransportFull();
- }

public boolean canHide() {
return stats.canHide;
=======================================
--- /trunk/v2/src/com/customwars/client/model/gameobject/UnitStats.java Mon
Mar 14 07:18:14 2011
+++ /trunk/v2/src/com/customwars/client/model/gameobject/UnitStats.java Sun
Mar 20 06:49:26 2011
@@ -42,7 +42,7 @@

private Map<String, String> transformTerrains; // Terrain Ids this unit
can transform to for a given TerrainId
private Map<String, String> buildCities; // City Ids this unit
can build on given terrains
- private List<String> buildUnits; // Units that can be build
+ private List<String> produces; // Units that can be
produced
private List<String> supplyUnitsInTransport; // Units that can be
supplied and healed when within this transport(empty means no unit can be
supplied)
private List<String> supplyUnits; // Units that can be
supplied and healed around this unit(empty means no unit can be supplied)
final ArmyBranch armyBranch; // Naval, Ground, Air
@@ -102,7 +102,7 @@
transportStats.init(this);
transformTerrains = Args.createEmptyMapIfNull(transformTerrains);
buildCities = Args.createEmptyMapIfNull(buildCities);
- buildUnits = Args.createEmptyListIfNull(buildUnits);
+ produces = Args.createEmptyListIfNull(produces);
supplyUnitsInTransport =
Args.createEmptyListIfNull(supplyUnitsInTransport);
supplyUnits = Args.createEmptyListIfNull(supplyUnits);
if (primaryWeaponName == null) primaryWeaponName = "";
@@ -133,7 +133,7 @@
public void validate() {
transportStats.validate(this);

- for (String unitName : buildUnits) {
+ for (String unitName : produces) {
Args.validate(!UnitFactory.hasUnitForName(unitName), "Illegal unit
name " + unitName + " in build unit stats");
}

@@ -273,14 +273,6 @@
public String getCityToBuildOnTerrain(Terrain terrain) {
return buildCities.get(terrain.getName());
}
-
- public boolean canBuildUnit(Unit unit) {
- return canBuildUnit(unit.getStats().name);
- }
-
- public boolean canBuildUnit(String unitID) {
- return buildUnits.contains(unitID);
- }

public boolean canSupplyUnitInTransport(Unit unit) {
return canSupplyUnitInTransport(unit.getStats().name);
@@ -298,8 +290,16 @@
return supplyUnits.contains(unitID);
}

- public Iterable<String> getUnitsThatCanBeBuild() {
- return Collections.unmodifiableList(buildUnits);
+ public boolean canProduceUnit(String unitID) {
+ return produces.contains(unitID);
+ }
+
+ public Iterable<String> getUnitsThatCanBeProduced() {
+ return Collections.unmodifiableList(produces);
+ }
+
+ public boolean canProduceUnits() {
+ return !produces.isEmpty();
}

public String getSecondaryWeaponName() {
@@ -344,7 +344,7 @@
sb.append(", canLaunchUnit=").append(canLaunchUnit);
sb.append(", transformTerrains=").append(transformTerrains);
sb.append(", buildCities=").append(buildCities);
- sb.append(", buildUnits=").append(buildUnits);
+ sb.append(", buildUnits=").append(produces);
sb.append(", supplyUnitsInTransport=").append(supplyUnitsInTransport);
sb.append(", supplyUnits=").append(supplyUnits);
sb.append(", armyBranch=").append(armyBranch);
=======================================
--- /trunk/v2/src/com/customwars/client/network/MessageSender.java Wed Feb
23 09:46:07 2011
+++ /trunk/v2/src/com/customwars/client/network/MessageSender.java Sun Mar
20 06:49:26 2011
@@ -66,7 +66,7 @@

void flare(Location flareCenter, int flareRange) throws NetworkException;

- void constructCity(Unit unit, int cityID, Location constructOn) throws
NetworkException;
+ void constructCity(Unit unit, String cityID, Location constructOn)
throws NetworkException;

void dive(Unit unit) throws NetworkException;

@@ -83,4 +83,6 @@
void coPower() throws NetworkException;

void coSuperPower() throws NetworkException;
-}
+
+ void produceUnit(Unit producer, String unitToProduce, Player owner)
throws NetworkException;
+}
=======================================
--- /trunk/v2/src/com/customwars/client/network/MessageSenderAdapter.java
Wed Feb 23 09:46:07 2011
+++ /trunk/v2/src/com/customwars/client/network/MessageSenderAdapter.java
Sun Mar 20 06:49:26 2011
@@ -119,7 +119,7 @@
}

@Override
- public void constructCity(Unit unit, int cityID, Location constructOn)
throws NetworkException {
+ public void constructCity(Unit unit, String cityID, Location
constructOn) throws NetworkException {
}

@Override
@@ -153,4 +153,8 @@
@Override
public void coSuperPower() throws NetworkException {
}
-}
+
+ @Override
+ public void produceUnit(Unit producer, String unitToProduce, Player
owner) throws NetworkException {
+ }
+}

Reply all
Reply to author
Forward
0 new messages