summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/src/ch/asynk/rustanddust/engine/PathBuilder.java19
-rw-r--r--core/src/ch/asynk/rustanddust/engine/util/Collection.java12
-rw-r--r--core/src/ch/asynk/rustanddust/engine/util/IterableArray.java117
-rw-r--r--core/src/ch/asynk/rustanddust/game/Command.java7
-rw-r--r--core/src/ch/asynk/rustanddust/game/Engagement.java4
-rw-r--r--core/src/ch/asynk/rustanddust/game/battles/BattleCommon.java8
-rw-r--r--core/test/ch/asynk/rustanddust/LineOfSightTest.java31
7 files changed, 144 insertions, 54 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);
}
diff --git a/core/test/ch/asynk/rustanddust/LineOfSightTest.java b/core/test/ch/asynk/rustanddust/LineOfSightTest.java
index fce9605..bb60ccc 100644
--- a/core/test/ch/asynk/rustanddust/LineOfSightTest.java
+++ b/core/test/ch/asynk/rustanddust/LineOfSightTest.java
@@ -1,10 +1,9 @@
package ch.asynk.rustanddust;
-import java.util.List;
-
import org.junit.Test;
import org.junit.Before;
+import ch.asynk.rustanddust.engine.util.Collection;
import ch.asynk.rustanddust.engine.SearchBoard.Node;
import static org.junit.Assert.assertTrue;
@@ -28,21 +27,21 @@ public class LineOfSightTest
((Helpers.FakeTile) fakeBoard.getTile(i, j)).setBlockLineOfSight(block);
}
- private void checkNode(List<Node> l, int i, int col, int row)
+ private void checkNode(Collection<Node> l, int i, int col, int row)
{
Node n = l.get(i);
assertTrue(n.col == col);
assertTrue(n.row == row);
}
- private List<Node> lineOfSight(int x0, int y0, int x1, int y1)
+ private Collection<Node> lineOfSight(int x0, int y0, int x1, int y1)
{
return sb.buildLineOfSight(x0, y0, x1, y1, true);
}
// from bottom left
@Test public void test_1() {
- List<Node> s = lineOfSight(0, 0, 2, 1);
+ Collection<Node> s = lineOfSight(0, 0, 2, 1);
assertTrue(s.size() == 4);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -51,7 +50,7 @@ public class LineOfSightTest
}
@Test public void test_2() {
- List<Node> s = lineOfSight(0, 0, 5, 1);
+ Collection<Node> s = lineOfSight(0, 0, 5, 1);
assertTrue(s.size() == 6);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -62,7 +61,7 @@ public class LineOfSightTest
}
@Test public void test_3() {
- List<Node> s = lineOfSight(0, 0, 8, 1);
+ Collection<Node> s = lineOfSight(0, 0, 8, 1);
assertTrue(s.size() == 10);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -77,7 +76,7 @@ public class LineOfSightTest
}
@Test public void test_4() {
- List<Node> s = lineOfSight(0, 0, 1, 2);
+ Collection<Node> s = lineOfSight(0, 0, 1, 2);
assertTrue(s.size() == 3);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 1);
@@ -85,7 +84,7 @@ public class LineOfSightTest
}
@Test public void test_5() {
- List<Node> s = lineOfSight(0, 0, 4, 2);
+ Collection<Node> s = lineOfSight(0, 0, 4, 2);
assertTrue(s.size() == 7);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -97,7 +96,7 @@ public class LineOfSightTest
}
@Test public void test_6() {
- List<Node> s = lineOfSight(0, 0, 7, 2);
+ Collection<Node> s = lineOfSight(0, 0, 7, 2);
assertTrue(s.size() == 8);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -110,7 +109,7 @@ public class LineOfSightTest
}
@Test public void test_7() {
- List<Node> s = lineOfSight(0, 0, 10, 2);
+ Collection<Node> s = lineOfSight(0, 0, 10, 2);
assertTrue(s.size() == 11);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -126,7 +125,7 @@ public class LineOfSightTest
}
@Test public void test_8() {
- List<Node> s = lineOfSight(0, 0, 6, 3);
+ Collection<Node> s = lineOfSight(0, 0, 6, 3);
assertTrue(s.size() == 10);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -141,7 +140,7 @@ public class LineOfSightTest
}
@Test public void test_9() {
- List<Node> s = lineOfSight(0, 0, 2, 4);
+ Collection<Node> s = lineOfSight(0, 0, 2, 4);
assertTrue(s.size() == 5);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 1);
@@ -151,7 +150,7 @@ public class LineOfSightTest
}
@Test public void test_10() {
- List<Node> s = lineOfSight(0, 0, 5, 4);
+ Collection<Node> s = lineOfSight(0, 0, 5, 4);
assertTrue(s.size() == 6);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 1);
@@ -162,7 +161,7 @@ public class LineOfSightTest
}
@Test public void test_11() {
- List<Node> s = lineOfSight(0, 0, 8, 4);
+ Collection<Node> s = lineOfSight(0, 0, 8, 4);
assertTrue(s.size() == 13);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);
@@ -180,7 +179,7 @@ public class LineOfSightTest
}
@Test public void test_12() {
- List<Node> s = lineOfSight(0, 0, 11, 4);
+ Collection<Node> s = lineOfSight(0, 0, 11, 4);
assertTrue(s.size() == 12);
checkNode(s, 0, 0, 0);
checkNode(s, 1, 1, 0);