Rename variables in pathfinding.c to be more clear

This commit is contained in:
2023-11-18 18:07:22 +01:00
parent 2d50a43a73
commit 2fc0d7196a
3 changed files with 28 additions and 29 deletions

View File

@@ -275,7 +275,7 @@ void render(float dt, void *userData) {
},
.target=(TilePosition) {tileX, tileY},
.map=&game->map,
.heap=heap,
.openSet=heap,
.outPath=&path,
.pool=game->pools.pathData
});

View File

@@ -24,20 +24,25 @@ bool findPath(const PathfindingDesc *desc) {
i8 x : 3;
i8 y : 3;
} Visited;
Visited visited[map->width * map->height] = {};
bzMemSet(visited, 0, sizeof(visited));
Visited closedSet[map->width * map->height] = {};
PathNode *heap = desc->heap;
if (!heap) heap = bzHeapCreate(PathNode, map->width * map->height);
else bzHeapClear(heap);
PathNode *openSet = desc->openSet;
if (!openSet) openSet = bzHeapCreate(PathNode, map->width * map->height);
else bzHeapClear(openSet);
i32 toTargetCost = dst(desc->start, desc->target);
bzHeapPush(heap, (PathNode) {toTargetCost, 0, toTargetCost, desc->start});
bzHeapPush(openSet, (PathNode) {
.weight=toTargetCost,
.gCost=0,
.hCost=toTargetCost,
.visited=false,
.pos=desc->start
});
bool foundPath = false;
while (!bzHeapIsEmpty(heap)) {
PathNode node = bzHeapPop(heap);
while (!bzHeapIsEmpty(openSet)) {
PathNode node = bzHeapPop(openSet);
if (node.pos.x == desc->target.x &&
node.pos.y == desc->target.y) {
// Found path
@@ -45,8 +50,7 @@ bool findPath(const PathfindingDesc *desc) {
break;
}
visited[node.pos.y * map->width + node.pos.x].visited = true;
// Node edges
for (int y = node.pos.y - 1; y <= node.pos.y + 1; y++) {
for (int x = node.pos.x - 1; x <= node.pos.x + 1; x++) {
if (x == node.pos.x && y == node.pos.y)
@@ -54,22 +58,23 @@ bool findPath(const PathfindingDesc *desc) {
if (y < 0 || y >= map->height ||
x < 0 || x >= map->width)
continue;
// not walkable
if (bzTileMapHasCollision(map, x, y))
continue;
Visited *curVisited = &visited[y * map->width + x];
if (curVisited->visited)
Visited *curClosed = &closedSet[y * map->width + x];
if (curClosed->visited)
continue;
curVisited->visited = true;
curClosed->visited = true;
TilePosition curPos = {x, y};
i32 gCost = node.gCost + dst(node.pos, curPos);
//if (gCost >= node.gCost) continue;
toTargetCost = dst(curPos, desc->target);
curVisited->x = (i8) (curPos.x - node.pos.x);
curVisited->y = (i8) (curPos.y - node.pos.y);
curClosed->x = (i8) (curPos.x - node.pos.x);
curClosed->y = (i8) (curPos.y - node.pos.y);
bzHeapPush(heap, (PathNode) {
bzHeapPush(openSet, (PathNode) {
.weight = gCost + toTargetCost,
.gCost = gCost,
.hCost = toTargetCost,
@@ -108,7 +113,7 @@ bool findPath(const PathfindingDesc *desc) {
pathData->waypoints[numWaypoints++] = waypoint;
pathData->numWaypoints = numWaypoints;
Visited visit = visited[pos.y * map->width + pos.x];
Visited visit = closedSet[pos.y * map->width + pos.x];
BZ_ASSERT(visit.x != 0 || visit.y != 0);
pos.x -= visit.x;
pos.y -= visit.y;
@@ -134,10 +139,8 @@ bool findPath(const PathfindingDesc *desc) {
}
}
if (!desc->heap) {
bzHeapDestroy(heap);
heap = NULL;
}
if (!desc->openSet)
bzHeapDestroy(openSet);
return foundPath ? pathLen : -1;
}

View File

@@ -5,15 +5,11 @@
#include "components.h"
typedef struct PathMove {
i8 x;
i8 y;
} PathMove;
typedef struct PathNode {
i32 weight; // g + h
i32 weight; // fCost = g + h
i32 gCost; // from start cost
i32 hCost; // to target cost
bool visited;
TilePosition pos;
} PathNode;
@@ -22,7 +18,7 @@ typedef struct PathfindingDesc {
TilePosition target;
BzObjectPool *pool;
BzTileMap *map;
PathNode *heap;
PathNode *openSet; // heap
Path *outPath;
} PathfindingDesc;