Rename variables in pathfinding.c to be more clear
This commit is contained in:
@@ -275,7 +275,7 @@ void render(float dt, void *userData) {
|
|||||||
},
|
},
|
||||||
.target=(TilePosition) {tileX, tileY},
|
.target=(TilePosition) {tileX, tileY},
|
||||||
.map=&game->map,
|
.map=&game->map,
|
||||||
.heap=heap,
|
.openSet=heap,
|
||||||
.outPath=&path,
|
.outPath=&path,
|
||||||
.pool=game->pools.pathData
|
.pool=game->pools.pathData
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,20 +24,25 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
i8 x : 3;
|
i8 x : 3;
|
||||||
i8 y : 3;
|
i8 y : 3;
|
||||||
} Visited;
|
} Visited;
|
||||||
Visited visited[map->width * map->height] = {};
|
Visited closedSet[map->width * map->height] = {};
|
||||||
bzMemSet(visited, 0, sizeof(visited));
|
|
||||||
|
|
||||||
PathNode *heap = desc->heap;
|
PathNode *openSet = desc->openSet;
|
||||||
if (!heap) heap = bzHeapCreate(PathNode, map->width * map->height);
|
if (!openSet) openSet = bzHeapCreate(PathNode, map->width * map->height);
|
||||||
else bzHeapClear(heap);
|
else bzHeapClear(openSet);
|
||||||
|
|
||||||
i32 toTargetCost = dst(desc->start, desc->target);
|
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;
|
bool foundPath = false;
|
||||||
|
|
||||||
while (!bzHeapIsEmpty(heap)) {
|
while (!bzHeapIsEmpty(openSet)) {
|
||||||
PathNode node = bzHeapPop(heap);
|
PathNode node = bzHeapPop(openSet);
|
||||||
if (node.pos.x == desc->target.x &&
|
if (node.pos.x == desc->target.x &&
|
||||||
node.pos.y == desc->target.y) {
|
node.pos.y == desc->target.y) {
|
||||||
// Found path
|
// Found path
|
||||||
@@ -45,8 +50,7 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
break;
|
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 y = node.pos.y - 1; y <= node.pos.y + 1; y++) {
|
||||||
for (int x = node.pos.x - 1; x <= node.pos.x + 1; x++) {
|
for (int x = node.pos.x - 1; x <= node.pos.x + 1; x++) {
|
||||||
if (x == node.pos.x && y == node.pos.y)
|
if (x == node.pos.x && y == node.pos.y)
|
||||||
@@ -54,22 +58,23 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
if (y < 0 || y >= map->height ||
|
if (y < 0 || y >= map->height ||
|
||||||
x < 0 || x >= map->width)
|
x < 0 || x >= map->width)
|
||||||
continue;
|
continue;
|
||||||
|
// not walkable
|
||||||
if (bzTileMapHasCollision(map, x, y))
|
if (bzTileMapHasCollision(map, x, y))
|
||||||
continue;
|
continue;
|
||||||
Visited *curVisited = &visited[y * map->width + x];
|
Visited *curClosed = &closedSet[y * map->width + x];
|
||||||
if (curVisited->visited)
|
if (curClosed->visited)
|
||||||
continue;
|
continue;
|
||||||
curVisited->visited = true;
|
curClosed->visited = true;
|
||||||
|
|
||||||
TilePosition curPos = {x, y};
|
TilePosition curPos = {x, y};
|
||||||
i32 gCost = node.gCost + dst(node.pos, curPos);
|
i32 gCost = node.gCost + dst(node.pos, curPos);
|
||||||
//if (gCost >= node.gCost) continue;
|
//if (gCost >= node.gCost) continue;
|
||||||
toTargetCost = dst(curPos, desc->target);
|
toTargetCost = dst(curPos, desc->target);
|
||||||
|
|
||||||
curVisited->x = (i8) (curPos.x - node.pos.x);
|
curClosed->x = (i8) (curPos.x - node.pos.x);
|
||||||
curVisited->y = (i8) (curPos.y - node.pos.y);
|
curClosed->y = (i8) (curPos.y - node.pos.y);
|
||||||
|
|
||||||
bzHeapPush(heap, (PathNode) {
|
bzHeapPush(openSet, (PathNode) {
|
||||||
.weight = gCost + toTargetCost,
|
.weight = gCost + toTargetCost,
|
||||||
.gCost = gCost,
|
.gCost = gCost,
|
||||||
.hCost = toTargetCost,
|
.hCost = toTargetCost,
|
||||||
@@ -108,7 +113,7 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
pathData->waypoints[numWaypoints++] = waypoint;
|
pathData->waypoints[numWaypoints++] = waypoint;
|
||||||
pathData->numWaypoints = numWaypoints;
|
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);
|
BZ_ASSERT(visit.x != 0 || visit.y != 0);
|
||||||
pos.x -= visit.x;
|
pos.x -= visit.x;
|
||||||
pos.y -= visit.y;
|
pos.y -= visit.y;
|
||||||
@@ -134,10 +139,8 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!desc->heap) {
|
if (!desc->openSet)
|
||||||
bzHeapDestroy(heap);
|
bzHeapDestroy(openSet);
|
||||||
heap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return foundPath ? pathLen : -1;
|
return foundPath ? pathLen : -1;
|
||||||
}
|
}
|
||||||
@@ -5,15 +5,11 @@
|
|||||||
|
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
|
||||||
typedef struct PathMove {
|
|
||||||
i8 x;
|
|
||||||
i8 y;
|
|
||||||
} PathMove;
|
|
||||||
|
|
||||||
typedef struct PathNode {
|
typedef struct PathNode {
|
||||||
i32 weight; // g + h
|
i32 weight; // fCost = g + h
|
||||||
i32 gCost; // from start cost
|
i32 gCost; // from start cost
|
||||||
i32 hCost; // to target cost
|
i32 hCost; // to target cost
|
||||||
|
bool visited;
|
||||||
TilePosition pos;
|
TilePosition pos;
|
||||||
} PathNode;
|
} PathNode;
|
||||||
|
|
||||||
@@ -22,7 +18,7 @@ typedef struct PathfindingDesc {
|
|||||||
TilePosition target;
|
TilePosition target;
|
||||||
BzObjectPool *pool;
|
BzObjectPool *pool;
|
||||||
BzTileMap *map;
|
BzTileMap *map;
|
||||||
PathNode *heap;
|
PathNode *openSet; // heap
|
||||||
Path *outPath;
|
Path *outPath;
|
||||||
} PathfindingDesc;
|
} PathfindingDesc;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user