Skip to content

Commit

Permalink
HAHAHA! IT WORKS! Rotation pathfinding, that is.
Browse files Browse the repository at this point in the history
This was incredibly difficult to debug and wound up simply needing two
adjacency grids supplied to CustomDijkstraMap, one for getting to a cell
and one for coming from a cell. Getting to that point was very painful,
but thank you @derrickcreamer for your patient advice on this arduous
journey.
  • Loading branch information
tommyettinger committed Sep 30, 2016
1 parent bff0131 commit 5db2f00
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 91 deletions.
Expand Up @@ -59,9 +59,9 @@ public class CustomDijkstraMap implements Serializable {

/**
* The neighbors map, as produced by adjacency; can be modified by passing neighbors as the first argument to
* {@link Adjacency#portal(int[][], int, int, boolean)} if you want to create portals between non-adjacent cells.
* {@link Adjacency#portal(int[][][], int, int, boolean)} if you want to create portals between non-adjacent cells.
*/
public int[][] neighbors;
public int[][][] neighbors;
/**
* Height of the map. Exciting stuff. Don't change this, instead call initialize().
*/
Expand Down Expand Up @@ -518,8 +518,8 @@ public double[] scan(int[] impassable) {
adjacency.putAllVariants(closed, impassable[i], WALL);
}
}
int[][] neighbors = this.neighbors;
int near, cen, neighborCount = neighbors.length, mid;
int[][] fromNeighbors = neighbors[0];
int near, cen, neighborCount = fromNeighbors.length, mid;

for (IntDoubleOrderedMap.MapEntry entry : goals.mapEntrySet()) {
closed.remove(entry.getIntKey());
Expand Down Expand Up @@ -549,14 +549,14 @@ else if (gradientMap[l] < currentLowest) {
for (IntDoubleOrderedMap.MapEntry cell : open.mapEntrySet()) {
cen = cell.getIntKey();
for (int d = 0; d < neighborCount; d++) {
near = neighbors[d][cen];
near = fromNeighbors[d][cen];
if (!adjacency.validate(near))
// Outside the map
continue;
if(adjacency.isBlocked(cen, d, neighbors, gradientMap, WALL))
continue;
if(adjacency.twoStepRule) {
near = neighbors[d][mid = near];
near = fromNeighbors[d][mid = near];
// Outside the map
if (!adjacency.validate(near))
continue;
Expand Down Expand Up @@ -621,7 +621,8 @@ public double[] partialScan(int limit, int... impassable) {
adjacency.putAllVariants(closed, impassable[i], WALL);
}
}
int near, cen, neighborCount = neighbors.length, mid;
int[][] fromNeighbors = neighbors[0];
int near, invNear, cen, neighborCount = neighbors.length, mid;

for (IntDoubleOrderedMap.MapEntry entry : goals.mapEntrySet()) {
closed.remove(entry.getIntKey());
Expand Down Expand Up @@ -652,14 +653,14 @@ else if (gradientMap[l] < currentLowest) {
for (IntDoubleOrderedMap.MapEntry cell : open.mapEntrySet()) {
cen = cell.getIntKey();
for (int d = 0; d < neighborCount; d++) {
near = neighbors[d][cen];
near = fromNeighbors[d][cen];
if (!adjacency.validate(near))
// Outside the map
continue;
if(adjacency.isBlocked(cen, d, neighbors, gradientMap, WALL))
continue;
if(adjacency.twoStepRule) {
near = neighbors[d][mid = near];
near = fromNeighbors[d][mid = near];
if (!adjacency.validate(near))
// Outside the map
continue;
Expand Down Expand Up @@ -977,6 +978,7 @@ public ArrayList<Coord> findNearestMultiple(Coord start, int limit, Set<Coord> t
public double[] scan(int size, int[] impassable) {
if (!initialized) return null;

int[][] fromNeighbors = neighbors[0];
int near, cen, neighborCount = neighbors.length, mid, tmp, tmp2, xStore, yStore, rStore, nStore;
double valStore;
Adjacency adjacency = this.adjacency;
Expand Down Expand Up @@ -1040,14 +1042,14 @@ else if (gradientMap[l] < currentLowest) {
for (IntDoubleOrderedMap.MapEntry cell : open.mapEntrySet()) {
cen = cell.getIntKey();
for (int d = 0; d < neighborCount; d++) {
near = neighbors[d][cen];
near = fromNeighbors[d][cen];
if (!adjacency.validate(near))
// Outside the map
continue;
//if(adjacency.isBlocked(cen, d, neighbors, gradientMap, WALL))
// continue;
if(adjacency.twoStepRule) {
near = neighbors[d][mid = near];
near = fromNeighbors[d][mid = near];
if (!adjacency.validate(near))
// Outside the map
continue;
Expand Down Expand Up @@ -1253,6 +1255,7 @@ public IntVLA findPath(int length, IntVLA impassable,
if (goals.isEmpty())
return new IntVLA(path);
Adjacency adjacency = this.adjacency;
int[][] toNeighbors = neighbors[1];
scan(impassable2.toArray());
int currentPos = start, pt;
double paidLength = 0.0;
Expand All @@ -1266,18 +1269,18 @@ public IntVLA findPath(int length, IntVLA impassable,
int choice = rng.nextIntHasty(adjacency.maxAdjacent);

for (int d = 0; d < adjacency.maxAdjacent; d++) {
pt = neighbors[reuse[d]][currentPos];
pt = toNeighbors[reuse[d]][currentPos];
if (gradientMap[pt] < best && !path.contains(pt)) {
best = gradientMap[pt];
choice = adjacency.invertAdjacent[reuse[d]];
choice = reuse[d];// adjacency.invertAdjacent[reuse[d]];
}
}


if (best >= gradientMap[currentPos] || physicalMap[neighbors[choice][currentPos]] > FLOOR) {
if (best >= gradientMap[currentPos] || physicalMap[toNeighbors[choice][currentPos]] > FLOOR) {
break;
}
currentPos = neighbors[choice][pt = currentPos];
currentPos = toNeighbors[choice][pt = currentPos];
path.add(currentPos);
paidLength += adjacency.costRules.get(costMap[currentPos] | ((adjacency.extractR(pt) == adjacency.extractR(currentPos) ? 0 : 0x10000)));
frustration++;
Expand Down Expand Up @@ -1532,6 +1535,7 @@ public IntVLA findFleePath(int length, double preferLongerPaths, IntVLA impassab
cachedFleeMap = scan(scanArray);
}
Adjacency adjacency = this.adjacency;
int[][] toNeighbors = neighbors[1];
int currentPos = start, pt;
double paidLength = 0.0;
while (true) {
Expand All @@ -1544,19 +1548,19 @@ public IntVLA findFleePath(int length, double preferLongerPaths, IntVLA impassab
int choice = rng.nextIntHasty(adjacency.maxAdjacent);

for (int d = 0; d < adjacency.maxAdjacent; d++) {
pt = neighbors[reuse[d]][currentPos];
pt = toNeighbors[reuse[d]][currentPos];
if (gradientMap[pt] < best && !path.contains(pt)) {
best = gradientMap[pt];
choice = reuse[d];
}
}


if (best >= gradientMap[currentPos] || physicalMap[neighbors[choice][currentPos]] > FLOOR) {
if (best >= gradientMap[currentPos] || physicalMap[toNeighbors[choice][currentPos]] > FLOOR) {
path.clear();
break;
}
currentPos = neighbors[choice][pt = currentPos];
currentPos = toNeighbors[choice][pt = currentPos];
path.add(currentPos);
paidLength += adjacency.costRules.get(costMap[currentPos] | ((adjacency.extractR(pt) == adjacency.extractR(currentPos) ? 0 : 0x10000)));
frustration++;
Expand Down Expand Up @@ -1617,6 +1621,7 @@ public IntVLA findPathLarge (int size, int length, IntVLA impassable,
if (goals.isEmpty())
return new IntVLA(path);
Adjacency adjacency = this.adjacency;
int[][] toNeighbors = neighbors[1];
scan(size, impassable2.toArray());
int currentPos = start, pt;
double paidLength = 0.0;
Expand All @@ -1630,19 +1635,19 @@ public IntVLA findPathLarge (int size, int length, IntVLA impassable,
int choice = rng.nextIntHasty(adjacency.maxAdjacent);

for (int d = 0; d < adjacency.maxAdjacent; d++) {
pt = neighbors[reuse[d]][currentPos];
pt = toNeighbors[reuse[d]][currentPos];
if (gradientMap[pt] < best && !path.contains(pt)) {
best = gradientMap[pt];
choice = reuse[d];
}
}


if (best >= gradientMap[currentPos] || physicalMap[neighbors[choice][currentPos]] > FLOOR) {
if (best >= gradientMap[currentPos] || physicalMap[toNeighbors[choice][currentPos]] > FLOOR) {
path.clear();
break;
}
currentPos = neighbors[choice][pt = currentPos];
currentPos = toNeighbors[choice][pt = currentPos];
path.add(currentPos);
paidLength += adjacency.costRules.get(costMap[currentPos] | ((adjacency.extractR(pt) == adjacency.extractR(currentPos) ? 0 : 0x10000)));
frustration++;
Expand Down Expand Up @@ -2076,6 +2081,7 @@ public IntVLA findFleePathLarge(int size, int length, double preferLongerPaths,
cachedFleeMap = scan(size, scanArray);
}
Adjacency adjacency = this.adjacency;
int[][] toNeighbors = neighbors[1];
int currentPos = start, pt;
double paidLength = 0.0;
while (true) {
Expand All @@ -2088,19 +2094,19 @@ public IntVLA findFleePathLarge(int size, int length, double preferLongerPaths,
int choice = rng.nextIntHasty(adjacency.maxAdjacent);

for (int d = 0; d < adjacency.maxAdjacent; d++) {
pt = neighbors[reuse[d]][currentPos];
pt = toNeighbors[reuse[d]][currentPos];
if (gradientMap[pt] < best && !path.contains(pt)) {
best = gradientMap[pt];
choice = reuse[d];
}
}


if (best >= gradientMap[currentPos] || physicalMap[neighbors[choice][currentPos]] > FLOOR) {
if (best >= gradientMap[currentPos] || physicalMap[toNeighbors[choice][currentPos]] > FLOOR) {
path.clear();
break;
}
currentPos = neighbors[choice][pt = currentPos];
currentPos = toNeighbors[choice][pt = currentPos];
path.add(currentPos);
paidLength += adjacency.costRules.get(costMap[currentPos] | ((adjacency.extractR(pt) == adjacency.extractR(currentPos) ? 0 : 0x10000)));
frustration++;
Expand Down

0 comments on commit 5db2f00

Please sign in to comment.