| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "path_cache.h" |
| 29 |
|
| 30 |
#include <math.h> |
| 31 |
#include <time.h> |
| 32 |
|
| 33 |
#include "ai_utils.h" |
| 34 |
#include "log.h" |
| 35 |
|
| 36 |
#define PATH_CACHE_MAX 128 |
| 37 |
|
| 38 |
PathCache gPathCache; |
| 39 |
|
| 40 |
|
| 41 |
static CachedPath CachedPathCopy(CachedPath *c) |
| 42 |
{ |
| 43 |
CachedPath copy; |
| 44 |
memcpy(©, c, sizeof *c); |
| 45 |
(*copy.refs)++; |
| 46 |
return copy; |
| 47 |
} |
| 48 |
void CachedPathDestroy(CachedPath *c) |
| 49 |
{ |
| 50 |
if (c->Path == NULL && c->refs == NULL) |
| 51 |
{ |
| 52 |
return; |
| 53 |
} |
| 54 |
(*c->refs)--; |
| 55 |
CASSERT(*c->refs >= 0, "out of sync ref count"); |
| 56 |
if (*c->refs == 0) |
| 57 |
{ |
| 58 |
ASPathDestroy(c->Path); |
| 59 |
CFREE(c->refs); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static bool CachedPathMatches( |
| 64 |
const CachedPath *c, const struct vec2i from, const struct vec2i to) |
| 65 |
{ |
| 66 |
return svec2i_is_equal(c->from, from) && svec2i_is_equal(c->to, to); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
void PathCacheInit(PathCache *pc, Map *m) |
| 71 |
{ |
| 72 |
CArrayInit(&pc->paths, sizeof(CachedPath)); |
| 73 |
pc->head = 0; |
| 74 |
pc->map = m; |
| 75 |
} |
| 76 |
void PathCacheTerminate(PathCache *pc) |
| 77 |
{ |
| 78 |
PathCacheClear(pc); |
| 79 |
CArrayTerminate(&pc->paths); |
| 80 |
} |
| 81 |
|
| 82 |
void PathCacheClear(PathCache *pc) |
| 83 |
{ |
| 84 |
CA_FOREACH(CachedPath, c, pc->paths) |
| 85 |
CachedPathDestroy(c); |
| 86 |
CA_FOREACH_END() |
| 87 |
CArrayClear(&pc->paths); |
| 88 |
pc->head = 0; |
| 89 |
} |
| 90 |
|
| 91 |
typedef struct |
| 92 |
{ |
| 93 |
Map *Map; |
| 94 |
TileSelectFunc IsTileOk; |
| 95 |
} AStarContext; |
| 96 |
static void AddTileNeighbors( |
| 97 |
ASNeighborList neighbors, void *node, void *context); |
| 98 |
static float AStarHeuristic(void *fromNode, void *toNode, void *context); |
| 99 |
static ASPathNodeSource cPathNodeSource = |
| 100 |
{ |
| 101 |
sizeof(struct vec2i), AddTileNeighbors, AStarHeuristic, NULL, NULL |
| 102 |
}; |
| 103 |
CachedPath PathCacheCreate( |
| 104 |
PathCache *pc, struct vec2i from, struct vec2i to, |
| 105 |
const bool ignoreObjects, const bool cache) |
| 106 |
{ |
| 107 |
|
| 108 |
CA_FOREACH(CachedPath, c, pc->paths) |
| 109 |
if (CachedPathMatches(c, from, to)) |
| 110 |
{ |
| 111 |
LOG(LM_PATH, LL_TRACE, "cached path (%d, %d) to (%d, %d)...", |
| 112 |
from.x, from.y, to.x, to.y); |
| 113 |
return CachedPathCopy(c); |
| 114 |
} |
| 115 |
CA_FOREACH_END() |
| 116 |
|
| 117 |
LOG(LM_PATH, LL_TRACE, "find path (%d, %d) to (%d, %d)...", |
| 118 |
from.x, from.y, to.x, to.y); |
| 119 |
const clock_t start = clock(); |
| 120 |
|
| 121 |
|
| 122 |
CachedPath cp; |
| 123 |
AStarContext ac; |
| 124 |
ac.Map = pc->map; |
| 125 |
ac.IsTileOk = ignoreObjects ? IsTileWalkable : IsTileWalkableAroundObjects; |
| 126 |
cp.Path = ASPathCreate(&cPathNodeSource, &ac, &from, &to); |
| 127 |
CMALLOC(cp.refs, sizeof *cp.refs); |
| 128 |
(*cp.refs) = 1; |
| 129 |
cp.from = from; |
| 130 |
cp.to = to; |
| 131 |
|
| 132 |
if (cache) |
| 133 |
{ |
| 134 |
(*cp.refs)++; |
| 135 |
|
| 136 |
if ((int)pc->paths.size < PATH_CACHE_MAX) |
| 137 |
{ |
| 138 |
CArrayPushBack(&pc->paths, &cp); |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
|
| 143 |
CachedPath *oldest = CArrayGet(&pc->paths, pc->head); |
| 144 |
CachedPathDestroy(oldest); |
| 145 |
memcpy(oldest, &cp, sizeof cp); |
| 146 |
|
| 147 |
pc->head++; |
| 148 |
if (pc->head == pc->paths.size) |
| 149 |
{ |
| 150 |
pc->head = 0; |
| 151 |
} |
| 152 |
} |
| 153 |
LOG(LM_PATH, LL_TRACE, "Cached %d paths", (int)pc->paths.size); |
| 154 |
} |
| 155 |
const clock_t diff = clock() - start; |
| 156 |
const int ms = (int)(diff * 1000 / CLOCKS_PER_SEC); |
| 157 |
LOG(LM_PATH, LL_DEBUG, "Pathfind time %dms", ms); |
| 158 |
return cp; |
| 159 |
} |
| 160 |
|
| 161 |
static void AddTileNeighbors( |
| 162 |
ASNeighborList neighbors, void *node, void *context) |
| 163 |
{ |
| 164 |
struct vec2i *v = node; |
| 165 |
int y; |
| 166 |
AStarContext *c = context; |
| 167 |
for (y = v->y - 1; y <= v->y + 1; y++) |
| 168 |
{ |
| 169 |
int x; |
| 170 |
if (y < 0 || y >= c->Map->Size.y) |
| 171 |
{ |
| 172 |
continue; |
| 173 |
} |
| 174 |
for (x = v->x - 1; x <= v->x + 1; x++) |
| 175 |
{ |
| 176 |
float cost; |
| 177 |
struct vec2i neighbor; |
| 178 |
neighbor.x = x; |
| 179 |
neighbor.y = y; |
| 180 |
if (x < 0 || x >= c->Map->Size.x) |
| 181 |
{ |
| 182 |
continue; |
| 183 |
} |
| 184 |
if (x == v->x && y == v->y) |
| 185 |
{ |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if (!c->IsTileOk(c->Map, svec2i(x, y)) || |
| 191 |
!c->IsTileOk(c->Map, svec2i(v->x, y)) || |
| 192 |
!c->IsTileOk(c->Map, svec2i(x, v->y))) |
| 193 |
{ |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if (x != v->x && y != v->y) |
| 201 |
{ |
| 202 |
cost = TILE_WIDTH * 1.1f; |
| 203 |
} |
| 204 |
else if (x != v->x) |
| 205 |
{ |
| 206 |
cost = TILE_WIDTH; |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
cost = TILE_HEIGHT; |
| 211 |
} |
| 212 |
ASNeighborListAdd(neighbors, &neighbor, cost); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
static float AStarHeuristic(void *fromNode, void *toNode, void *context) |
| 217 |
{ |
| 218 |
const struct vec2i *v1 = fromNode; |
| 219 |
const struct vec2i *v2 = toNode; |
| 220 |
UNUSED(context); |
| 221 |
return CHEBYSHEV_DISTANCE( |
| 222 |
(float)v1->x, (float)v1->y, (float)v2->x, (float)v2->y); |
| 223 |
} |