Remove memory allocation in pathfinding
This commit is contained in:
@@ -123,6 +123,7 @@ static void heapUpdate(Heap *heap, i32 idx);
|
||||
|
||||
bool pathfindAStar(const PathfindingDesc *desc) {
|
||||
BZ_ASSERT(desc->map);
|
||||
BZ_ASSERT(desc->alloc);
|
||||
BzTileMap *map = desc->map;
|
||||
|
||||
TilePosition start = {0}, target = {0};
|
||||
@@ -133,18 +134,17 @@ bool pathfindAStar(const PathfindingDesc *desc) {
|
||||
|
||||
i32 numTiles = map->width * map->height;
|
||||
|
||||
PathNodeRecord *closedSet = desc->closedSet;
|
||||
if (!closedSet) closedSet = bzAlloc(sizeof(*closedSet) * numTiles);
|
||||
PathNodeRecord *closedSet = bzStackAlloc(desc->alloc, sizeof(*closedSet) * numTiles);
|
||||
bzMemSet(closedSet, 0, sizeof(*closedSet) * numTiles);
|
||||
|
||||
PathNode *openSetArr = bzStackAlloc(desc->alloc, sizeof(*openSetArr) * numTiles);
|
||||
Heap openSet = {
|
||||
.arr=desc->openSet,
|
||||
.arr=openSetArr,
|
||||
.size=0,
|
||||
.capacity=numTiles,
|
||||
.mapWidth=map->width,
|
||||
.records=closedSet
|
||||
};
|
||||
if (!openSet.arr) openSet.arr = bzAlloc(sizeof(*openSet.arr) * numTiles);
|
||||
|
||||
i32 toTargetCost = dst(start, target);
|
||||
heapPush(&openSet, (PathNode) {
|
||||
@@ -258,10 +258,8 @@ bool pathfindAStar(const PathfindingDesc *desc) {
|
||||
*desc->outPath = (Path) {pathData, 0};
|
||||
}
|
||||
|
||||
if (!desc->closedSet)
|
||||
bzFree(closedSet);
|
||||
if (!desc->openSet)
|
||||
bzFree(openSet.arr);
|
||||
bzStackAllocFree(desc->alloc, openSet.arr);
|
||||
bzStackAllocFree(desc->alloc, closedSet);
|
||||
|
||||
return foundPath ? pathLen : -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user