Fixed OutOfMemory error for pathfinding

This commit is contained in:
Anuken
2018-01-25 21:50:00 -05:00
parent 9001fb0dcc
commit 5f131723c4
6 changed files with 30 additions and 37 deletions
@@ -2,12 +2,14 @@ package io.anuke.mindustry.ai;
import com.badlogic.gdx.ai.pfa.*;
import com.badlogic.gdx.utils.BinaryHeap;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.TimeUtils;
/**An IndexedAStarPathfinder that uses an OptimizedGraph, and therefore has less allocations.*/
public class OptimizedPathFinder<N> implements PathFinder<N> {
OptimizedGraph<N> graph;
NodeRecord<N>[] nodeRecords;
//NodeRecord<N>[] nodeRecords; //TODO remove.
IntMap<NodeRecord<N>> records = new IntMap<>();
BinaryHeap<NodeRecord<N>> openList;
NodeRecord<N> current;
@@ -23,22 +25,13 @@ public class OptimizedPathFinder<N> implements PathFinder<N> {
@SuppressWarnings("unchecked")
public OptimizedPathFinder(OptimizedGraph<N> graph) {
this.graph = graph;
this.nodeRecords = (NodeRecord<N>[]) new NodeRecord[graph.getNodeCount()];
//this.nodeRecords = (NodeRecord<N>[]) new NodeRecord[graph.getNodeCount()];
this.openList = new BinaryHeap<>();
}
@Override
public boolean searchConnectionPath(N startNode, N endNode, Heuristic<N> heuristic, GraphPath<Connection<N>> outPath) {
// Perform AStar
boolean found = search(startNode, endNode, heuristic);
if (found) {
// Create a path made of connections
generateConnectionPath(startNode, outPath);
}
return found;
return false;
}
@Override
@@ -196,27 +189,13 @@ public class OptimizedPathFinder<N> implements PathFinder<N> {
}
protected void generateConnectionPath(N startNode, GraphPath<Connection<N>> outPath) {
//do ABSOLUTELY NOTHING
/*
// Work back along the path, accumulating connections
// outPath.clear();
while (current.node != startNode) {
outPath.add(current.connection);
current = nodeRecords[graph.getIndex(current.connection.getFromNode())];
}
// Reverse the path
outPath.reverse();*/
}
protected void generateNodePath(N startNode, GraphPath<N> outPath) {
// Work back along the path, accumulating nodes
// outPath.clear();
while (current.from != null) {
outPath.add(current.node);
current = nodeRecords[graph.getIndex(current.from)];
current = records.get(graph.getIndex(current.from));
}
outPath.add(startNode);
@@ -230,6 +209,16 @@ public class OptimizedPathFinder<N> implements PathFinder<N> {
}
protected NodeRecord<N> getNodeRecord(N node) {
if(!records.containsKey(graph.getIndex(node))){
NodeRecord<N> record = new NodeRecord<>();
record.node = node;
record.searchId = searchId;
records.put(graph.getIndex(node), record);
return record;
}else{
return records.get(graph.getIndex(node));
}
/*
int index = graph.getIndex(node);
NodeRecord<N> nr = nodeRecords[index];
if (nr != null) {
@@ -242,7 +231,7 @@ public class OptimizedPathFinder<N> implements PathFinder<N> {
nr = nodeRecords[index] = new NodeRecord<>();
nr.node = node;
nr.searchId = searchId;
return nr;
return nr;*/
}
/**
@@ -127,6 +127,7 @@ public class Pathfind{
if(point.finder.search(point.request, maxTime)){
smoother.smoothPath(point.path);
point.pathTiles = point.path.nodes.toArray(Tile.class);
point.finder = null;
}
}catch (ArrayIndexOutOfBoundsException e){
//no path