From 9b760a3cfee8964620f8faed051159fd1ee4ba42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 4 Jan 2016 23:46:33 +0100 Subject: PathBuilder: replace setExit(Orientation) with chooseExit(Orientation) --- .../ch/asynk/rustanddust/engine/PathBuilder.java | 49 +++++++++++++++++----- .../ch/asynk/rustanddust/game/map/Map2Moves.java | 2 +- .../rustanddust/game/states/StateWithdraw.java | 6 +-- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java index f96c06f..3bd057b 100644 --- a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java +++ b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java @@ -151,6 +151,44 @@ public class PathBuilder implements Disposable return 1; } + public int chooseExit(Orientation o) + { + List ps = getPaths(); + + Path good = ps.get(0); + int mvt = pawn.getMovementPoints(); + int cost = to.exitCost(); + int rBonus = (to.road(o) ? pawn.getRoadMarchBonus() : 0); + + if (ps.size() > 1) { + good = null; + for (Path p : getPaths()) { + if (pathCanExit(p, mvt, cost, rBonus)) { + if (good != null) { + if ( (p.fitness > good.fitness) || ((p.fitness == good.fitness) && (p.cost < good.cost))) + good = p; + } else { + good = p; + } + } + } + + keepOnly(good); + } + + if (!pathCanExit(good, mvt, cost, rBonus)) + throw new RuntimeException("chosen path can't exit"); + + orientation = o; + if (from != to) { + good.cost += 1; + good.tiles.add(to); + } + to = board.getAdjTileAt(to, o); + + return 1; + } + private void keepOnly(Path path) { getPaths().remove(path); @@ -293,17 +331,6 @@ public class PathBuilder implements Disposable return filteredPaths.get(i); } - public void setExit(Orientation o) - { - orientation = o; - Path path = getPath(0); - if (from != to) { - path.cost += 1; - path.tiles.add(to); - } - to = board.getAdjTileAt(to, o); - } - private void printToErr(String what, List paths) { System.err.println(what + " " + paths.size() + " - " + from + " -> " + to); diff --git a/core/src/ch/asynk/rustanddust/game/map/Map2Moves.java b/core/src/ch/asynk/rustanddust/game/map/Map2Moves.java index 411e2c9..738de2b 100644 --- a/core/src/ch/asynk/rustanddust/game/map/Map2Moves.java +++ b/core/src/ch/asynk/rustanddust/game/map/Map2Moves.java @@ -72,10 +72,10 @@ public abstract class Map2Moves extends Map1Units public void pathsSetOrientation(Orientation o) { paths.orientation = o; } public boolean pathsIsSet() { return paths.isSet(); } public boolean pathsCanExit(Orientation o) { return paths.canExit(o); } - public void pathsSetExit(Orientation o) { paths.setExit(o); } public boolean pathsContains(Hex hex) { return paths.contains(hex); } public int pathsChooseBest() { return paths.chooseBest(); } public int pathsChooseShortest() { return paths.chooseShortest(); } + public int pathsChooseExit(Orientation o) { return paths.chooseExit(o); } public int pathsToggleHex(Hex hex) { boolean enable = !hex.isOverlayEnabled(Hex.MOVE); diff --git a/core/src/ch/asynk/rustanddust/game/states/StateWithdraw.java b/core/src/ch/asynk/rustanddust/game/states/StateWithdraw.java index a759e51..12bb3c4 100644 --- a/core/src/ch/asynk/rustanddust/game/states/StateWithdraw.java +++ b/core/src/ch/asynk/rustanddust/game/states/StateWithdraw.java @@ -43,14 +43,12 @@ public class StateWithdraw extends StateCommon if (map.pathsTo() == null) map.pathsBuild(hex); - if (map.pathsSize() > 1) - RustAndDust.debug("ERROR: Withdraw pathsSize() == " + map.pathsSize()); - Hex exitHex = (Hex) map.pathsTo(); if (!exitZone.contains(exitHex)) throw new RuntimeException(String.format("%s not in exitZone", exitHex)); - map.pathsSetExit(exitZone.orientation); + if (map.pathsChooseExit(exitZone.orientation) > 1) + RustAndDust.debug("ERROR: Withdraw pathsSize() == " + map.pathsSize()); unit.hideActiveable(); if (to != null) -- cgit v1.1-2-g2b99