Remove memory allocation in pathfinding
This commit is contained in:
@@ -21,7 +21,7 @@ void bzStackAllocDestroy(BzStackAlloc *alloc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *bzStackAlloc(BzStackAlloc *alloc, size_t numBytes) {
|
void *bzStackAlloc(BzStackAlloc *alloc, size_t numBytes) {
|
||||||
BZ_ASSERT(alloc->size + numBytes < alloc->size);
|
BZ_ASSERT(alloc->allocated + numBytes < alloc->size);
|
||||||
void *allocated = (u8 *) alloc->memory + alloc->allocated;
|
void *allocated = (u8 *) alloc->memory + alloc->allocated;
|
||||||
alloc->allocated += numBytes;
|
alloc->allocated += numBytes;
|
||||||
return allocated;
|
return allocated;
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ typedef struct Game {
|
|||||||
i64 gold;
|
i64 gold;
|
||||||
i64 pop;
|
i64 pop;
|
||||||
} resources;
|
} resources;
|
||||||
|
BzStackAlloc stackAlloc;
|
||||||
struct {
|
struct {
|
||||||
BzObjectPool *pathData;
|
BzObjectPool *pathData;
|
||||||
} pools;
|
} pools;
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ bool init(void *userData) {
|
|||||||
input->unitPositions = bzArrayCreate(Position, 16);
|
input->unitPositions = bzArrayCreate(Position, 16);
|
||||||
|
|
||||||
|
|
||||||
|
game->stackAlloc = bzStackAllocCreate(10 * 1000 * 1000); // 10 MB
|
||||||
// init pools
|
// init pools
|
||||||
game->pools.pathData = bzObjectPoolCreate(&(BzObjectPoolDesc) {
|
game->pools.pathData = bzObjectPoolCreate(&(BzObjectPoolDesc) {
|
||||||
.objectSize=sizeof(PathData),
|
.objectSize=sizeof(PathData),
|
||||||
@@ -189,6 +190,7 @@ void deinit(void *userData) {
|
|||||||
ECS = NULL;
|
ECS = NULL;
|
||||||
|
|
||||||
bzArrayDestroy(inputCopy.unitPositions);
|
bzArrayDestroy(inputCopy.unitPositions);
|
||||||
|
bzStackAllocDestroy(&gameCopy.stackAlloc);
|
||||||
bzObjectPoolDestroy(gameCopy.pools.pathData);
|
bzObjectPoolDestroy(gameCopy.pools.pathData);
|
||||||
bzSpatialGridDestroy(gameCopy.entityGrid);
|
bzSpatialGridDestroy(gameCopy.entityGrid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ static void heapUpdate(Heap *heap, i32 idx);
|
|||||||
|
|
||||||
bool pathfindAStar(const PathfindingDesc *desc) {
|
bool pathfindAStar(const PathfindingDesc *desc) {
|
||||||
BZ_ASSERT(desc->map);
|
BZ_ASSERT(desc->map);
|
||||||
|
BZ_ASSERT(desc->alloc);
|
||||||
BzTileMap *map = desc->map;
|
BzTileMap *map = desc->map;
|
||||||
|
|
||||||
TilePosition start = {0}, target = {0};
|
TilePosition start = {0}, target = {0};
|
||||||
@@ -133,18 +134,17 @@ bool pathfindAStar(const PathfindingDesc *desc) {
|
|||||||
|
|
||||||
i32 numTiles = map->width * map->height;
|
i32 numTiles = map->width * map->height;
|
||||||
|
|
||||||
PathNodeRecord *closedSet = desc->closedSet;
|
PathNodeRecord *closedSet = bzStackAlloc(desc->alloc, sizeof(*closedSet) * numTiles);
|
||||||
if (!closedSet) closedSet = bzAlloc(sizeof(*closedSet) * numTiles);
|
|
||||||
bzMemSet(closedSet, 0, sizeof(*closedSet) * numTiles);
|
bzMemSet(closedSet, 0, sizeof(*closedSet) * numTiles);
|
||||||
|
|
||||||
|
PathNode *openSetArr = bzStackAlloc(desc->alloc, sizeof(*openSetArr) * numTiles);
|
||||||
Heap openSet = {
|
Heap openSet = {
|
||||||
.arr=desc->openSet,
|
.arr=openSetArr,
|
||||||
.size=0,
|
.size=0,
|
||||||
.capacity=numTiles,
|
.capacity=numTiles,
|
||||||
.mapWidth=map->width,
|
.mapWidth=map->width,
|
||||||
.records=closedSet
|
.records=closedSet
|
||||||
};
|
};
|
||||||
if (!openSet.arr) openSet.arr = bzAlloc(sizeof(*openSet.arr) * numTiles);
|
|
||||||
|
|
||||||
i32 toTargetCost = dst(start, target);
|
i32 toTargetCost = dst(start, target);
|
||||||
heapPush(&openSet, (PathNode) {
|
heapPush(&openSet, (PathNode) {
|
||||||
@@ -258,10 +258,8 @@ bool pathfindAStar(const PathfindingDesc *desc) {
|
|||||||
*desc->outPath = (Path) {pathData, 0};
|
*desc->outPath = (Path) {pathData, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!desc->closedSet)
|
bzStackAllocFree(desc->alloc, openSet.arr);
|
||||||
bzFree(closedSet);
|
bzStackAllocFree(desc->alloc, closedSet);
|
||||||
if (!desc->openSet)
|
|
||||||
bzFree(openSet.arr);
|
|
||||||
|
|
||||||
return foundPath ? pathLen : -1;
|
return foundPath ? pathLen : -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,12 @@ typedef struct PathNodeRecord {
|
|||||||
typedef struct PathfindingDesc {
|
typedef struct PathfindingDesc {
|
||||||
Position start;
|
Position start;
|
||||||
Position target;
|
Position target;
|
||||||
BzObjectPool *pool;
|
|
||||||
BzTileMap *map;
|
BzTileMap *map;
|
||||||
PathNode *openSet; // heap (size: width * height)
|
Path *outPath;
|
||||||
PathNodeRecord *closedSet; // size: width * height
|
|
||||||
Path *outPath;
|
BzObjectPool *pool;
|
||||||
|
BzStackAlloc *alloc;
|
||||||
} PathfindingDesc;
|
} PathfindingDesc;
|
||||||
|
|
||||||
bool pathfindAStar(const PathfindingDesc *desc);
|
bool pathfindAStar(const PathfindingDesc *desc);
|
||||||
|
|||||||
@@ -138,7 +138,8 @@ void updatePlayerInput() {
|
|||||||
.target=target,
|
.target=target,
|
||||||
.map=map,
|
.map=map,
|
||||||
.outPath=&path,
|
.outPath=&path,
|
||||||
.pool=game->pools.pathData
|
.pool=game->pools.pathData,
|
||||||
|
.alloc=&game->stackAlloc
|
||||||
});
|
});
|
||||||
if (!path.paths) continue;
|
if (!path.paths) continue;
|
||||||
ecs_set_ptr(ECS, entity, Path, &path);
|
ecs_set_ptr(ECS, entity, Path, &path);
|
||||||
@@ -166,7 +167,8 @@ void updatePlayerInput() {
|
|||||||
.target=worldPos,
|
.target=worldPos,
|
||||||
.map=map,
|
.map=map,
|
||||||
.outPath=&path,
|
.outPath=&path,
|
||||||
.pool=game->pools.pathData
|
.pool=game->pools.pathData,
|
||||||
|
.alloc=&game->stackAlloc
|
||||||
});
|
});
|
||||||
if (!path.paths) continue;
|
if (!path.paths) continue;
|
||||||
ecs_set_ptr(ECS, entity, Path, &path);
|
ecs_set_ptr(ECS, entity, Path, &path);
|
||||||
|
|||||||
Reference in New Issue
Block a user