defer movement propagation and assembly to postprocessing

This commit is contained in:
Leijurv 2018-10-03 08:04:30 -07:00
parent 36bdaa99ec
commit 28af41b789
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -86,11 +86,11 @@ class Path implements IPath {
throw new IllegalStateException();
}
PathNode current = end;
LinkedList<BetterBlockPos> tempPath = new LinkedList<>(); // Repeatedly inserting to the beginning of an arraylist is O(n^2)
LinkedList<Movement> tempMovements = new LinkedList<>(); // Instead, do it into a linked list, then convert at the end
LinkedList<BetterBlockPos> tempPath = new LinkedList<>();
// Repeatedly inserting to the beginning of an arraylist is O(n^2)
// Instead, do it into a linked list, then convert at the end
while (!current.equals(start)) {
tempPath.addFirst(new BetterBlockPos(current.x, current.y, current.z));
tempMovements.addFirst(runBackwards(current.previous, current));
current = current.previous;
}
tempPath.addFirst(this.start);
@ -98,20 +98,6 @@ class Path implements IPath {
// inserting into a LinkedList<E> keeps track of length, then when we addall (which calls .toArray) it's able
// to performantly do that conversion since it knows the length.
path.addAll(tempPath);
movements.addAll(tempMovements);
}
private static Movement runBackwards(PathNode src0, PathNode dest0) { // TODO this is horrifying
BetterBlockPos src = new BetterBlockPos(src0.x, src0.y, src0.z);
BetterBlockPos dest = new BetterBlockPos(dest0.x, dest0.y, dest0.z);
for (Moves moves : Moves.values()) {
Movement move = moves.apply0(src);
if (move.getDest().equals(dest)) {
return move;
}
}
// leave this as IllegalStateException; it's caught in AbstractNodeCostSearch
throw new IllegalStateException("Movement became impossible during calculation " + src + " " + dest + " " + dest.subtract(src));
}
/**
@ -140,12 +126,33 @@ class Path implements IPath {
}
}
private void assembleMovements() {
if (path.isEmpty() || !movements.isEmpty()) {
throw new IllegalStateException();
}
for (int i = 0; i < path.size() - 1; i++) {
movements.add(runBackwards(path.get(i), path.get(i + 1)));
}
}
private static Movement runBackwards(BetterBlockPos src, BetterBlockPos dest) { // TODO this is horrifying
for (Moves moves : Moves.values()) {
Movement move = moves.apply0(src);
if (move.getDest().equals(dest)) {
return move;
}
}
// leave this as IllegalStateException; it's caught in AbstractNodeCostSearch
throw new IllegalStateException("Movement became impossible during calculation " + src + " " + dest + " " + dest.subtract(src));
}
@Override
public void postprocess() {
if (verified) {
throw new IllegalStateException();
}
verified = true;
assembleMovements();
// more post processing here
movements.forEach(Movement::checkLoadedChunk);
sanityCheck();