From 0c56e7f556277cf9518d0fb191539125e2e6adbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Fri, 31 Oct 2014 14:07:39 +0100 Subject: use new PossiblePaths class --- core/src/ch/asynk/tankontank/engine/Board.java | 102 +--------------- core/src/ch/asynk/tankontank/engine/Pawn.java | 11 +- .../ch/asynk/tankontank/engine/SearchBoard.java | 131 --------------------- core/src/ch/asynk/tankontank/game/Map.java | 15 +-- .../ch/asynk/tankontank/game/states/StateMove.java | 13 +- 5 files changed, 24 insertions(+), 248 deletions(-) diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java index e446365..60a3611 100644 --- a/core/src/ch/asynk/tankontank/engine/Board.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -1,11 +1,10 @@ package ch.asynk.tankontank.engine; -import java.util.Set; import java.util.List; -import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.Collection; +import java.util.ArrayList; +import java.util.LinkedHashSet; import com.badlogic.gdx.Gdx; @@ -28,8 +27,6 @@ import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; public abstract class Board implements Disposable { private final Tile neighbours[] = new Tile[6]; - protected List> paths; - private final ArrayList finalPath = new ArrayList(10); public interface TileBuilder { @@ -140,9 +137,6 @@ public abstract class Board implements Disposable for (int i = 0, n = animations.size(); i < n; i++) animations.get(i).dispose(); animations.clear(); - for (Vector3 v : finalPath) - vector3Pool.free(v); - finalPath.clear(); } public float getWidth() @@ -322,91 +316,6 @@ public abstract class Board implements Disposable return assists.size(); } - private int nodesToSet(List> nodes, TileCollection tiles) - { - tiles.clear(); - - for (ArrayList path : nodes) { - for (int i = 1, n = (path.size() - 1); i < n; i++) { - SearchBoard.Node node = path.get(i); - Tile tile = getTile(node.col, node.row); - if (!tiles.contains(tile)) - tiles.add(tile); - } - } - - return nodes.size(); - } - - protected int collectPossiblePaths(Pawn pawn, Tile to, TileCollection tiles) - { - Tile from = pawn.getTile(); - paths = searchBoard.possiblePaths(pawn, from.getCol(), from.getRow(), to.getCol(), to.getRow()); - return nodesToSet(paths, tiles); - } - - protected int possiblePathsFilterToggle(Tile tile, TileCollection tiles) - { - paths = searchBoard.possiblePathsFilterToggle(tile.getCol(), tile.getRow()); - return nodesToSet(paths, tiles); - } - - protected int getPathCost(Pawn pawn, int i) - { - return searchBoard.pathCost(pawn, paths.get(i)); - } - - protected int getCoordinatePath(Pawn pawn, int idx, ArrayList path, Orientation finalOrientation) - { - for (Vector3 v : path) - vector3Pool.free(v); - path.clear(); - - Vector2 tmpCoords = new Vector2(); - - Vector3 p = pawn.getPosition(); - Vector3 v = vector3Pool.obtain(); - v.set(p.x, p.y, 0f); - Orientation prevOrientation = pawn.getOrientation(); - - ArrayList nodes = paths.get(idx); - SearchBoard.Node prevNode = nodes.get(0); - // Gdx.app.debug("Board", "getCoordinatePath()"); - // Gdx.app.debug("Board", " " + prevNode); - - for (int i = 1, n = nodes.size(); i < n; i++) { - SearchBoard.Node node = nodes.get(i); - // Gdx.app.debug("Board", " " + node); - Orientation o = Orientation.fromMove(prevNode.col, prevNode.row, node.col, node.row); - if ((o != Orientation.KEEP) && (o != prevOrientation)) { - v.z = o.r(); - path.add(v); - v = vector3Pool.obtain(); - } - pawn.getPosAt(getTile(node.col, node.row), tmpCoords); - v.set(tmpCoords.x, tmpCoords.y, o.r()); - path.add(v); - prevOrientation = o; - v = vector3Pool.obtain(); - v.set(tmpCoords.x, tmpCoords.y, 0f); - - prevNode = node; - } - - if (finalOrientation != prevOrientation) { - v.z = finalOrientation.r(); - path.add(v); - } else { - vector3Pool.free(v); - } - - // Gdx.app.debug("Board", " =>"); - // for (Vector3 vector :path) - // Gdx.app.debug("Board", " " + vector); - - return path.size(); - } - public void enableOverlayOn(Tile tile, int i, boolean enable) { if(tile.enableOverlay(i, enable)) @@ -450,12 +359,11 @@ public abstract class Board implements Disposable return pawn; } - protected void movePawn(final Pawn pawn, int cost, Orientation o, RunnableAnimation whenDone) + protected void movePawn(final Pawn pawn, PossiblePaths possiblePaths, RunnableAnimation whenDone) { - getCoordinatePath(pawn, 0, finalPath, o); removePawn(pawn); - AnimationSequence seq = pawn.getMoveAnimation(finalPath, 2); + AnimationSequence seq = pawn.getMoveAnimation(possiblePaths.iterator(), possiblePaths.pathSteps(0) + 2); seq.addAnimation(RunnableAnimation.get(pawn, new Runnable() { @Override public void run() { @@ -466,7 +374,7 @@ public abstract class Board implements Disposable })); seq.addAnimation(whenDone); addAnimation(seq); - pawn.move(cost); + pawn.move(possiblePaths.pathCost(0)); } protected void rotatePawn(final Pawn pawn, Orientation o, RunnableAnimation whenDone) diff --git a/core/src/ch/asynk/tankontank/engine/Pawn.java b/core/src/ch/asynk/tankontank/engine/Pawn.java index db54f36..38510e2 100644 --- a/core/src/ch/asynk/tankontank/engine/Pawn.java +++ b/core/src/ch/asynk/tankontank/engine/Pawn.java @@ -1,7 +1,6 @@ package ch.asynk.tankontank.engine; -import java.util.ArrayList; -import java.util.ArrayDeque; +import java.util.Iterator; import com.badlogic.gdx.utils.Disposable; @@ -246,12 +245,12 @@ public abstract class Pawn implements Moveable, Disposable return seq; } - public AnimationSequence getMoveAnimation(ArrayList path, int size) + public AnimationSequence getMoveAnimation(Iterator vectors, int size) { prevPosition.set(position); - AnimationSequence seq = AnimationSequence.get(path.size() + size); - for (Vector3 v : path) - seq.addAnimation(MoveToAnimation.get(this, v, MOVE_TIME)); + AnimationSequence seq = AnimationSequence.get(size); + while (vectors.hasNext()) + seq.addAnimation(MoveToAnimation.get(this, vectors.next(), MOVE_TIME)); return seq; } diff --git a/core/src/ch/asynk/tankontank/engine/SearchBoard.java b/core/src/ch/asynk/tankontank/engine/SearchBoard.java index 55e9113..632dfdd 100644 --- a/core/src/ch/asynk/tankontank/engine/SearchBoard.java +++ b/core/src/ch/asynk/tankontank/engine/SearchBoard.java @@ -41,14 +41,8 @@ public class SearchBoard private ArrayDeque stack; private LinkedList queue; private ArrayDeque roadMarch; - - private List targets; private List los; - private List path; - private List> possiblePaths; - private List possiblePathsFilters; - public SearchBoard(Board board, int cols, int rows) { this.cols = cols; @@ -66,13 +60,7 @@ public class SearchBoard this.queue = new LinkedList(); this.stack = new ArrayDeque(20); this.roadMarch = new ArrayDeque(5); - - this.targets = new ArrayList(10); this.los = new ArrayList(10); - - this.path = new ArrayList(20); - this.possiblePaths = new LinkedList>(); - this.possiblePathsFilters = new ArrayList(5); } // FIXME be carefull with this if I expand the size of the board to be bigger than the playable size @@ -547,123 +535,4 @@ public class SearchBoard return los; } - - public void clearPossiblePaths() - { - path.clear(); - for (List v : possiblePaths) - v.clear(); - possiblePaths.clear(); - possiblePathsFilters.clear(); - } - - public List> possiblePathsFilterToggle(int col, int row) - { - Node n = getNode(col, row); - if (possiblePathsFilters.contains(n)) - possiblePathsFilters.remove(n); - else - possiblePathsFilters.add(n); - return possiblePaths(); - } - - public List> possiblePaths() - { - int s = possiblePathsFilters.size(); - - List> paths = new LinkedList>(); - for (ArrayList path : possiblePaths) { - int ok = 0; - for (Node filter : possiblePathsFilters) { - if (path.contains(filter)) - ok += 1; - } - if (ok == s) { - if (path.size() == (s + 2)) { - paths.clear(); - paths.add(path); - return paths; - } else - paths.add(path); - } - } - - return paths; - } - - public List> possiblePaths(Pawn pawn, int col0, int row0, int col1, int row1) - { - clearPossiblePaths(); - - Node from = getNode(col0, row0); - Node to = getNode(col1, row1); - - if (distance(from, to) == 1) { - ArrayList temp = new ArrayList(2); - temp.add(from); - temp.add(to); - possiblePaths.add(temp); - } else { - path.add(from); - findAllPaths(pawn, from, to, pawn.getMovementPoints(), true, pawn.getRoadMarchBonus()); - } - - return possiblePaths; - } - - private void findAllPaths(Pawn pawn, Node from, Node to, int mvtLeft, boolean roadMarch, int roadMarchBonus) - { - Node moves[] = new Node[6]; - adjacentMoves(from, moves); - - for(int i = 0; i < 6; i++) { - Node next = moves[i]; - if (next == null) continue; - - Tile t = getTile(next); - boolean road = t.road(board.getSide(i)); - int cost = t.costFrom(pawn, board.getSide(i), road); - int r = (mvtLeft - cost); - if (roadMarch & road) r += roadMarchBonus; - - if ((distance(next, to) <= r)) { - if (next == to) { - ArrayList temp = new ArrayList(path.size() + 1); - for (Node n: path) - temp.add(n); - temp.add(next); - possiblePaths.add(temp); - } else { - path.add(next); - findAllPaths(pawn, next, to, (mvtLeft - cost), (roadMarch & road), roadMarchBonus); - path.remove(path.size() - 1); - } - } - } - } - - public int pathCost(Pawn pawn, List path) - { - int cost = 0; - boolean roadMarch = true; - Node prev = null; - - for (Node next : path) { - if (prev != null) { - Orientation o = Orientation.fromMove(next.col, next.row, prev.col, prev.row); - Tile t = getTile(next); - boolean road = t.road(o); - cost += t.costFrom(pawn, o, road); - roadMarch &= road; - } - prev = next; - } - - if (roadMarch) - cost -= pawn.getRoadMarchBonus(); - if (cost < 1) - cost = 1; - - return cost; - } } diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index e6554e2..296ddbe 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -14,6 +14,7 @@ import ch.asynk.tankontank.engine.PawnSet; import ch.asynk.tankontank.engine.TileSet; import ch.asynk.tankontank.engine.Board; import ch.asynk.tankontank.engine.Orientation; +import ch.asynk.tankontank.engine.PossiblePaths; import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; import ch.asynk.tankontank.engine.gfx.animations.SpriteAnimation; import ch.asynk.tankontank.engine.gfx.animations.SoundAnimation; @@ -25,7 +26,7 @@ public abstract class Map extends Board private final Ctrl ctrl; public final Board.TileCollection possibleMoves; - public final Board.TileCollection possiblePaths; + public final PossiblePaths possiblePaths; public final Board.PawnCollection moveablePawns; public final Board.PawnCollection possibleTargets; @@ -53,7 +54,7 @@ public abstract class Map extends Board setup(); possibleMoves = new TileSet(this, 40); - possiblePaths = new TileSet(this, 10); + possiblePaths = new PossiblePaths(this, 10, 20, 5, 10); moveablePawns = new PawnSet(this, 6); possibleTargets = new PawnSet(this, 10); @@ -104,12 +105,12 @@ public abstract class Map extends Board public int collectPossiblePaths(Pawn pawn, Hex to) { - return collectPossiblePaths(pawn, to, possiblePaths); + return possiblePaths.init(pawn, to); } - public int possiblePathsPointToggle(Hex hex) + public int togglePossiblePathHex(Hex hex) { - return possiblePathsFilterToggle(hex, possiblePaths); + return possiblePaths.toggleCtrlTile(hex); } public int collectPossibleTargets(Pawn pawn, Iterator foes) @@ -178,8 +179,8 @@ public abstract class Map extends Board public int movePawn(Pawn pawn, Orientation o) { System.err.println(" movePawn : " + pawn.getTile() + " " + o); - int cost = getPathCost(pawn, 0); - movePawn(pawn, cost, o, notifyDoneAnimation(pawn)); + possiblePaths.setLastOrientation(o); + movePawn(pawn, possiblePaths, notifyDoneAnimation(pawn)); return startMove(pawn); } diff --git a/core/src/ch/asynk/tankontank/game/states/StateMove.java b/core/src/ch/asynk/tankontank/game/states/StateMove.java index bce5baf..89d7535 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateMove.java +++ b/core/src/ch/asynk/tankontank/game/states/StateMove.java @@ -9,7 +9,6 @@ public class StateMove extends StateCommon @Override public void enter(boolean fromSelect) { - map.possiblePaths.clear(); boolean moreThanOne = ((map.moveablePawns.size() + map.activatedPawns.size()) > 1); ctrl.hud.show(false, true, true, false, moreThanOne, ctrl.cfg.canCancel); ctrl.hud.moveBtn.setOn(); @@ -70,7 +69,7 @@ public class StateMove extends StateCommon } else if ((s == 0) && map.possibleMoves.contains(upHex)) { s = collectPaths(); } else if (map.possiblePaths.contains(upHex)) { - s = togglePoint(s); + s = togglePoint(downHex, s); } if (s == 1) { @@ -127,16 +126,16 @@ public class StateMove extends StateCommon return s; } - private int togglePoint(int s) + private int togglePoint(Hex hex, int s) { - if (downHex == activeUnit.getHex()) { + if (hex == activeUnit.getHex()) { // - } else if (downHex == to) { + } else if (hex == to) { // } else { map.hidePossiblePaths(); - map.togglePathOverlay(downHex); - s = map.possiblePathsPointToggle(downHex); + map.togglePathOverlay(hex); + s = map.togglePossiblePathHex(hex); map.showPossiblePaths(); } -- cgit v1.1-2-g2b99