sanity check

This commit is contained in:
Leijurv 2018-08-02 14:05:47 -04:00
parent 13d9ba2f87
commit 47c7413215
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 33 additions and 2 deletions

View File

@ -62,6 +62,14 @@ public abstract class Action implements AbstractGameEventListener, Helper, Actio
&& currentState.getStatus() != ActionStatus.WAITING); && currentState.getStatus() != ActionStatus.WAITING);
} }
public BlockPos getSrc() {
return null; // TODO
}
public BlockPos getDest() {
return null; // TODO
}
/** /**
* Calculate latest action state. * Calculate latest action state.
* Gets called once a tick. * Gets called once a tick.

View File

@ -32,9 +32,13 @@ class Path implements IPath {
this.path = new ArrayList<>(); this.path = new ArrayList<>();
this.actions = new ArrayList<>(); this.actions = new ArrayList<>();
assemblePath(start, end); assemblePath(start, end);
sanityCheck();
} }
private final void assemblePath(PathNode start, PathNode end) { private final void assemblePath(PathNode start, PathNode end) {
if (!path.isEmpty() || !actions.isEmpty()) {
throw new IllegalStateException();
}
PathNode current = end; PathNode current = end;
LinkedList<BlockPos> tempPath = new LinkedList<>();//repeatedly inserting to the beginning of an arraylist is O(n^2) LinkedList<BlockPos> tempPath = new LinkedList<>();//repeatedly inserting to the beginning of an arraylist is O(n^2)
LinkedList<Action> tempActions = new LinkedList<>();//instead, do it into a linked list, then convert at the end LinkedList<Action> tempActions = new LinkedList<>();//instead, do it into a linked list, then convert at the end
@ -51,8 +55,27 @@ class Path implements IPath {
actions.addAll(tempActions); actions.addAll(tempActions);
} }
protected void sanityCheck() { public void sanityCheck() {
if (!start.equals(path.get(0))) {
throw new IllegalStateException();
}
if (!end.equals(path.get(path.size() - 1))) {
throw new IllegalStateException();
}
if (path.size() != actions.size() + 1) {
throw new IllegalStateException();
}
for (int i = 0; i < path.size(); i++) {
BlockPos src = path.get(i);
BlockPos dest = path.get(i + 1);
Action action = actions.get(i);
if (!src.equals(action.getSrc())) {
throw new IllegalStateException();
}
if (!dest.equals(action.getDest())) {
throw new IllegalStateException();
}
}
} }
@Override @Override