diff options
Diffstat (limited to 'core/src/ch')
6 files changed, 129 insertions, 38 deletions
diff --git a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java index ecc7634..a663d6a 100644 --- a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java +++ b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java @@ -1,9 +1,8 @@ package ch.asynk.rustanddust.engine; -import java.util.List; - import com.badlogic.gdx.utils.Disposable; +import ch.asynk.rustanddust.engine.util.Collection; import ch.asynk.rustanddust.engine.util.IterableArray; import ch.asynk.rustanddust.engine.util.IterableStack; import ch.asynk.rustanddust.engine.util.IterableSet; @@ -18,9 +17,9 @@ public class PathBuilder implements Disposable public int distance; public Orientation orientation; private IterableStack<Tile> stack; - private List<Tile> ctrlTiles; - private List<Path> paths; - private List<Path> filteredPaths; + private Collection<Tile> ctrlTiles; + private Collection<Path> paths; + private Collection<Path> filteredPaths; private IterableSet<Tile> tiles; public PathBuilder(Board board, int tSize, int stSize, int ftSize, int psSize) @@ -121,7 +120,7 @@ public class PathBuilder implements Disposable public int chooseBest() { - List<Path> ps = getPaths(); + Collection<Path> ps = getPaths(); if (ps.size() > 1) { Path good = null; @@ -135,7 +134,7 @@ public class PathBuilder implements Disposable public int chooseShortest() { - List<Path> ps = getPaths(); + Collection<Path> ps = getPaths(); if (ps.size() > 1) { Path good = ps.get(0); @@ -151,7 +150,7 @@ public class PathBuilder implements Disposable public int chooseExit(Orientation o) { - List<Path> ps = getPaths(); + Collection<Path> ps = getPaths(); Path good = ps.get(0); int mvt = pawn.getMovementPoints(); @@ -318,7 +317,7 @@ public class PathBuilder implements Disposable return false; } - private List<Path> getPaths() + private Collection<Path> getPaths() { if (ctrlTiles.size() == 0) return paths; @@ -332,7 +331,7 @@ public class PathBuilder implements Disposable return filteredPaths.get(i); } - private void printToErr(String what, List<Path> paths) + private void printToErr(String what, Collection<Path> paths) { System.err.println(what + " " + paths.size() + " - " + from + " -> " + to); for (Path path : paths) { diff --git a/core/src/ch/asynk/rustanddust/engine/util/Collection.java b/core/src/ch/asynk/rustanddust/engine/util/Collection.java index c768d45..a7b5a02 100644 --- a/core/src/ch/asynk/rustanddust/engine/util/Collection.java +++ b/core/src/ch/asynk/rustanddust/engine/util/Collection.java @@ -4,15 +4,21 @@ import java.util.Iterator; public interface Collection<E> extends Iterator, Iterable<E> { - public void clear(); - public int size(); public boolean isEmpty(); + public void clear(); + + public void ensureCapacity(int c); + + public boolean contains(E e); + public E get(int idx); public boolean add(E e); - // public boolean contains(E e); + public E remove(int idx); + + public boolean remove(E e); } diff --git a/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java b/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java index baa58d9..1b965fc 100644 --- a/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java +++ b/core/src/ch/asynk/rustanddust/engine/util/IterableArray.java @@ -1,51 +1,138 @@ package ch.asynk.rustanddust.engine.util; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; -public class IterableArray<E> extends ArrayList<E> implements Collection<E> +public class IterableArray<E> implements Collection<E> { - private int i; + private int idx; private int s; + private int c; + transient E[] data; - public IterableArray() + @SuppressWarnings("unchecked") + public IterableArray(int capacity) + { + this.s = 0; + this.c = capacity; + this.data = (E[]) new Object[c]; + } + + @Override + public int size() + { + return s; + } + + @Override + public boolean isEmpty() + { + return (s == 0); + } + + @Override + public void clear() + { + for (int i = 0; i < s; i++) + data[i] = null; + s = 0; + } + + @Override + public void ensureCapacity(int min) + { + if (c > min) return; + c += (c >> 1); + if (c < min) + c = min; + data = Arrays.copyOf(data, c); + } + + @Override + public boolean contains(E e) + { + if (e == null) { + for (int i = 0; i < s; i++) { + if (data[i] == null) + return true; + } + } else { + for (int i = 0; i < s; i++) { + if (e.equals(data[i])) + return true; + } + } + return false; + } + + @Override + public E get(int i) + { + return data[i]; + } + + @Override + public boolean add(E e) { - super(); + ensureCapacity(s + 1); + data[s] = e; + s += 1; + return true; } - public IterableArray(int n) + @Override + public E remove(int i) + { + E e = data[i]; + int m = (s - i - 1); + if (m > 0) + System.arraycopy(data, i+1, data, i, m); + data[--s] = null; + + return e; + } + + @Override + public boolean remove(E e) { - super(n); + for (int i = 0; i < s; i++) { + if (e.equals(data[i])) { + int m = (s - i - 1); + if (m > 0) + System.arraycopy(data, i+1, data, i, m); + data[--s] = null; + return true; + } + } + return false; } @Override @SuppressWarnings("unchecked") public Iterator<E> iterator() { - this.i = 0; - this.s = size(); + this.idx = 0; return (Iterator<E>) this; } @Override public boolean hasNext() { - return (i < s); + return (idx < s); } @Override public E next() { - E e = get(i); - i += 1; + E e = get(idx); + idx += 1; return e; } @Override public void remove() { - i -=1; - s -= 1; - remove(i); + idx -= 1; + remove(idx); } } diff --git a/core/src/ch/asynk/rustanddust/game/Command.java b/core/src/ch/asynk/rustanddust/game/Command.java index 330e44a..dbd0c21 100644 --- a/core/src/ch/asynk/rustanddust/game/Command.java +++ b/core/src/ch/asynk/rustanddust/game/Command.java @@ -1,7 +1,5 @@ package ch.asynk.rustanddust.game; -import java.util.List; - import com.badlogic.gdx.utils.Pool; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.JsonValue; @@ -10,6 +8,7 @@ import ch.asynk.rustanddust.engine.Order; import ch.asynk.rustanddust.engine.Move; import ch.asynk.rustanddust.engine.Pawn; import ch.asynk.rustanddust.engine.Tile; +import ch.asynk.rustanddust.engine.util.Collection; public class Command extends Order { @@ -191,7 +190,7 @@ public class Command extends Order json.writeObjectEnd(); } - private void writeUnits(Json json, String key, List<Unit> units) + private void writeUnits(Json json, String key, Collection<Unit> units) { json.writeArrayStart(key); for (Unit u : units) @@ -209,7 +208,7 @@ public class Command extends Order json.writeObjectEnd(); } - private void writeTiles(Json json, String key, List<Tile> tiles) + private void writeTiles(Json json, String key, Collection<Tile> tiles) { json.writeArrayStart(key); for (Tile t : tiles) diff --git a/core/src/ch/asynk/rustanddust/game/Engagement.java b/core/src/ch/asynk/rustanddust/game/Engagement.java index 937739e..2d3bd24 100644 --- a/core/src/ch/asynk/rustanddust/game/Engagement.java +++ b/core/src/ch/asynk/rustanddust/game/Engagement.java @@ -1,11 +1,11 @@ package ch.asynk.rustanddust.game; -import java.util.List; import java.util.Random; import com.badlogic.gdx.utils.Pool; import com.badlogic.gdx.utils.Disposable; +import ch.asynk.rustanddust.engine.util.Collection; import ch.asynk.rustanddust.engine.util.IterableArray; public class Engagement implements Disposable, Pool.Poolable @@ -36,7 +36,7 @@ public class Engagement implements Disposable, Pool.Poolable public Unit attacker; public Unit defender; - public List<Unit> assists; + public Collection<Unit> assists; public boolean success; public int d1; public int d2; diff --git a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java index 5023d5a..e09ce8d 100644 --- a/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java +++ b/core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java @@ -28,8 +28,8 @@ public abstract class BattleCommon implements Battle protected Player currentPlayer; protected Player usPlayer; protected Player gePlayer; - protected IterableArray<Zone> entryZone = new IterableArray<Zone>(); - protected IterableArray<Zone> exitZone = new IterableArray<Zone>(); + protected IterableArray<Zone> entryZones = new IterableArray<Zone>(10); + protected IterableArray<Zone> exitZones = new IterableArray<Zone>(10); protected HashMap<Unit, Zone> unitEntry = new HashMap<Unit, Zone>(); protected HashMap<Unit, Zone> unitExit = new HashMap<Unit, Zone>(); @@ -172,12 +172,12 @@ public abstract class BattleCommon implements Battle protected void addEntryZone(Zone entry) { - entryZone.add(entry); + entryZones.add(entry); } protected void addExitZone(Zone exit) { - exitZone.add(exit); + exitZones.add(exit); exit.enable(Hex.EXIT, true); } |