diff options
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r-- | core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java | 171 |
1 files changed, 170 insertions, 1 deletions
diff --git a/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java index d1c31a4..5e1db49 100644 --- a/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java +++ b/core/src/ch/asynk/rustanddust/game/map/Map5Marshal.java @@ -8,14 +8,17 @@ import com.badlogic.gdx.utils.JsonValue; import com.badlogic.gdx.utils.JsonReader; import com.badlogic.gdx.utils.JsonWriter.OutputType; -import ch.asynk.rustanddust.engine.Move; import ch.asynk.rustanddust.engine.Tile; +import ch.asynk.rustanddust.engine.Path; +import ch.asynk.rustanddust.engine.Move; +import ch.asynk.rustanddust.engine.Orientation; import ch.asynk.rustanddust.engine.SelectedTile; import ch.asynk.rustanddust.RustAndDust; import ch.asynk.rustanddust.game.Hex; import ch.asynk.rustanddust.game.Battle; import ch.asynk.rustanddust.game.Player; +import ch.asynk.rustanddust.game.Army; import ch.asynk.rustanddust.game.Unit; import ch.asynk.rustanddust.game.UnitList; import ch.asynk.rustanddust.game.Order; @@ -25,6 +28,7 @@ import ch.asynk.rustanddust.game.Engagement; public abstract class Map5Marshal extends Map4Orders { private static StringWriter writer = new StringWriter(2048); + private static UnitList units = new UnitList(30); public Map5Marshal(final RustAndDust game, Texture map, SelectedTile hex) { @@ -218,4 +222,169 @@ public abstract class Map5Marshal extends Map4Orders } json.writeObjectEnd(); } + + // LOAD + public void load(String payload, Player[] players) + { + units.clear(); + JsonValue root = new JsonReader().parse(payload); + players[0] = loadPlayer(root.get("players").get(0)); + players[1] = loadPlayer(root.get("players").get(1)); + loadMap(root.get("map")); + loadOrders(root.get("orders")); + units.clear(); + } + + private Player loadPlayer(JsonValue v) + { + Player p = new Player(v.getInt("id"), Army.valueOf(v.getString("a"))); + JsonValue a = v.get("v"); + p.setTurn(a.getInt(0), a.getInt(1), a.getInt(2)); + p.actionCount = a.getInt(3); + p.objectivesWon = a.getInt(4); + p.engagementWon = a.getInt(5); + p.engagementLost = a.getInt(6); + + a = v.get("Us"); + for (int i = 0; i < a.size; i++) + p.units.add(loadUnit(a.get(i), true)); + a = v.get("Cs"); + for (int i = 0; i < a.size; i++) + p.casualties.add(loadUnit(a.get(i), false)); + a = v.get("Rs"); + for (int i = 0; i < a.size; i++) + p.reinforcement.add(loadUnit(a.get(i), false)); + a = v.get("Ws"); + for (int i = 0; i < a.size; i++) + p.withdrawed.add(loadUnit(a.get(i), false)); + + return p; + } + + private Unit loadUnit(JsonValue v, boolean pos) + { + int unitId = v.getInt("id"); + Unit.UnitCode code = Unit.UnitCode.valueOf(v.getString("code")); + JsonValue a = v.get("v"); + Unit u = game.factory.getUnit(code, a.getBoolean(0), a.getBoolean(1)); + u.hasMoved = a.getBoolean(2); + u.hasFired = a.getBoolean(3); + u.id = unitId; + if (pos) { + a = v.get("p"); + Hex h = getHex(a.getInt(0), a.getInt(1)); + u.setRotation(a.getInt(2)); + showOnBoard(u, h, Orientation.fromRotation(a.getInt(2))); + } + units.add(u); + return u; + } + + private void loadMap(JsonValue v) + { + JsonValue a = v.get("o"); + for (int i = 0; i < a.size; i++) { + JsonValue o = a.get(i); + Hex h = getHex(o.get("p").getInt(0), o.get("p").getInt(1)); + Army army = Army.valueOf(o.getString("army")); + h.claim(army); + showObjective(h, army); + } + } + + private void loadOrders(JsonValue v) + { + for (int i = 0; i < v.size; i++) { + JsonValue o = v.get(i); + Order order = null; + switch(Order.OrderType.valueOf(o.getString("type"))) { + case MOVE: + order = loadMoveOrder(o); + break; + case ENGAGE: + order = loadEngageOrder(o); + break; + case PROMOTE: + order = loadPromoteOrder(o); + break; + } + orders.add(order); + } + } + + private Order loadMoveOrder(JsonValue v) + { + Unit unit = findById(v.getInt("id")); + Hex from = loadHex(v, "from"); + Hex to = loadHex(v, "to"); + Orientation orientation = Orientation.fromRotation(v.get("to").getInt(2)); + if (unit == null) return null; + + Path path = null; + JsonValue p = v.get("path"); + if (p != null) { + path = Path.get(p.size); + for (int i = 0; i < p.size; i++) { + JsonValue h = p.get(i); + path.tiles.add(getHex(h.getInt(0), h.getInt(1))); + } + } + + Move move = Move.get(unit, from, to, orientation, path); + Order o = Order.get(); + o.setMove(unit, move); + o.unitHex = from; + return o; + } + + private Order loadEngageOrder(JsonValue v) + { + Order o = Order.get(); + JsonValue a = v.get("units"); + o.setEngage(findById(a.getInt(0)), findById(a.getInt(1))); + + a = v.get("dice"); + o.engagement.d1 = a.getInt(0); + o.engagement.d2 = a.getInt(1); + o.engagement.d3 = a.getInt(2); + o.engagement.d4 = a.getInt(3); + + a = v.get("res"); + o.engagement.success = a.getBoolean(0); + o.engagement.attackSum = a.getInt(1); + o.engagement.defenseSum = a.getInt(2); + + a = v.get("assists"); + for (int i = 0; i < a.size; i++) + o.engagement.assists.add(findById(a.getInt(i))); + + return o; + } + + private Order loadPromoteOrder(JsonValue v) + { + Unit unit = findById(v.getInt("id")); + if (unit == null) return null; + + Order o = Order.get(); + o.setPromote(unit); + return o; + } + + private Hex loadHex(JsonValue v, String key) + { + JsonValue p = v.get(key); + if (p == null) return null; + return getHex(p.getInt(0), p.getInt(1)); + } + + private static Unit findById(int id) + { + for (Unit u : units) { + if (u.id == id) + return u; + } + RustAndDust.error(String.format("loadPromoteOrder: unable to find unit %d", id)); + return null; + } } |