summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2015-11-11 09:48:08 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2015-11-11 09:48:08 +0100
commitc55913b6b11e9caa2f9ba464cae20d453345fd71 (patch)
tree2e73d2c8a9920783a00168aaca060278a0f89d04
parent3592f563c05088921ed5526ede646978f56c0d71 (diff)
downloadRustAndDust-c55913b6b11e9caa2f9ba464cae20d453345fd71.zip
RustAndDust-c55913b6b11e9caa2f9ba464cae20d453345fd71.tar.gz
PathBuilder: add Path.fitness and use it in findAllPaths() and choosePath()
-rw-r--r--core/src/ch/asynk/rustanddust/engine/Path.java1
-rw-r--r--core/src/ch/asynk/rustanddust/engine/PathBuilder.java26
2 files changed, 20 insertions, 7 deletions
diff --git a/core/src/ch/asynk/rustanddust/engine/Path.java b/core/src/ch/asynk/rustanddust/engine/Path.java
index 2ddf3e5..5df92ac 100644
--- a/core/src/ch/asynk/rustanddust/engine/Path.java
+++ b/core/src/ch/asynk/rustanddust/engine/Path.java
@@ -27,6 +27,7 @@ public class Path implements Disposable, Pool.Poolable
}
public int cost;
+ public int fitness;
public boolean roadMarch;
public ArrayList<Tile> tiles;
diff --git a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
index 7023727..f8df34a 100644
--- a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
+++ b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
@@ -111,7 +111,7 @@ public class PathBuilder implements Disposable
path.cost = to.costFrom(pawn, o);
paths.add(path);
} else {
- findAllPaths(from, pawn.getMovementPoints(), true);
+ findAllPaths(from, pawn.getMovementPoints(), 0, true);
}
// printToErr("paths", paths);
@@ -122,8 +122,15 @@ public class PathBuilder implements Disposable
public int choosePath()
{
if (paths.size() > 1) {
+ int f = Integer.MAX_VALUE;
+ Path good = null;
+ for (Path path : paths) {
+ if (path.fitness < f) {
+ good = path;
+ f = path.fitness;
+ }
+ }
ctrlTiles.clear();
- Path good = paths.get(1);
for (Tile tile : good.tiles) {
toggleCtrlTile(tile);
if (paths.size() == 1)
@@ -134,7 +141,7 @@ public class PathBuilder implements Disposable
return 1;
}
- private void findAllPaths(Tile from, int mvtLeft, boolean roadMarch)
+ private void findAllPaths(Tile from, int mvtLeft, int fitness, boolean roadMarch)
{
Tile moves[] = new Tile[6];
board.setAdjacentTiles(from, moves);
@@ -144,8 +151,12 @@ public class PathBuilder implements Disposable
if ((next == null) || next.isOffMap()) continue;
Orientation o = board.getSide(i);
- int m = (mvtLeft - next.costFrom(pawn, o));
- boolean r = roadMarch & next.road(o);
+ int n = next.costFrom(pawn, o);
+ boolean r = next.road(o);
+ if (!r) fitness += n;
+
+ int m = (mvtLeft - n);
+ r &= roadMarch;
int l = (m + (r ? pawn.getRoadMarchBonus() : 0));
@@ -157,11 +168,12 @@ public class PathBuilder implements Disposable
tiles.add(t);
}
path.roadMarch = r;
+ path.fitness = fitness;
path.cost = (pawn.getMovementPoints() - m);
paths.add(path);
} else {
stack.add(next);
- findAllPaths(next, m, r);
+ findAllPaths(next, m, fitness, r);
stack.remove(stack.size() - 1);
}
}
@@ -272,7 +284,7 @@ public class PathBuilder implements Disposable
{
System.err.println(what + pawn + " ("+paths.size()+") " + from + " -> " + to);
for (Path path : paths) {
- System.err.println(String.format(" - path (l:%d c:%d r:%b)", path.tiles.size(), path.cost, path.roadMarch));
+ System.err.println(String.format(" - path (l:%d c:%d r:%b f:%d)", path.tiles.size(), path.cost, path.roadMarch, path.fitness));
for(Tile tile : path.tiles)
System.err.println(" " + tile.toString());
}