Debian Codegraph @

src/cdogs/path_cache.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014, 2016 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
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(&copy, 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 // Search through existing cache for path
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 // Cached path not found; find the path now
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 // Cache the path, optionally
132 if (cache)
133 {
134 (*cp.refs)++;
135 // Add to the cache if we are under the max size
136 if ((int)pc->paths.size < PATH_CACHE_MAX)
137 {
138 CArrayPushBack(&pc->paths, &cp);
139 }
140 else
141 {
142 // Replace the oldest cached path with this one
143 CachedPath *oldest = CArrayGet(&pc->paths, pc->head);
144 CachedPathDestroy(oldest);
145 memcpy(oldest, &cp, sizeof cp);
146 // Move the head
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 // if we're moving diagonally,
189 // need to check the axis-aligned neighbours are also clear
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 // Calculate cost of direction
197 // Note that there are different horizontal and vertical costs,
198 // due to the tiles being non-square
199 // Slightly prefer axes instead of diagonals
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 }