2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * pathfind.c: Find cheapest paths
29 * Known contributors to this file:
30 * Markus Armbruster, 2011
44 #ifdef PATH_FIND_DEBUG
45 #define DPRINTF(fmt, ...) ((void)printf(fmt , ## __VA_ARGS__))
47 #define DPRINTF(fmt, ...) ((void)0)
50 static char *bufrotate(char *buf, size_t bufsz, size_t i);
53 * Dijkstra's algorithm. Refer to your graph algorithm textbook for
54 * how it works. Implementation is specialized to hex maps.
58 * Array of sector data, indexed by sector uid
60 * We need to store a few values per sector visited by the path
61 * search. An array is the stupidest data structure that could
64 * Extra benefit: it works really well for distribution in a
65 * continental game, where we visit most sectors. That's our most
66 * demanding use of path search, and its performance has noticable
67 * impact on the update.
69 * Island game distribution is much less demanding. The array may not
70 * be the best choice here, but it's plainly good enough. Same for
71 * path searches outside the update.
76 * visit < pf_visit : unvisited, remaining members invalid
77 * visit == pf_visit : open, dir & cost tentative, heapi used
78 * visit == pf_visit + 1 : closed, dir & cost final, heapi unused
81 signed char dir; /* cheapest direction to source */
82 int heapi; /* index in heap, valid if open */
83 double cost; /* cost from source */
86 static unsigned short pf_visit;
87 static struct pf_map *pf_map;
90 * Binary heap, cost priority queue of all open sectors
92 * Again, we use the stupidest data structure that could possibly
93 * work: an array. And we make it so large it can hold *all* sectors.
94 * In practice, we need much less space, but a tighter upper bound is
95 * not obvious to me right now.
99 int uid; /* sector uid and */
100 coord x, y; /* coordinates, uid == XYOFFSET(x, y) */
101 double cost; /* cost from source */
104 static int pf_nheap; /* #entries in pf_nheap[] */
105 static struct pf_heap *pf_heap;
110 static coord pf_sx, pf_sy;
112 static natid pf_actor;
113 static double (*pf_sct_cost)(natid, int);
115 #ifndef NDEBUG /* silence "not used" warning */
116 /* Is sector with uid UID open? */
120 return pf_map[uid].visit == pf_visit;
124 /* Is sector with uid UID closed? */
126 pf_is_closed(int uid)
129 * optimization: check > pf_visit instead of == pf_visit + 1
130 * works because pf_map[uid].visit <= pf_visit + 1
132 return pf_map[uid].visit > pf_visit;
135 /* Is sector with uid UID unvisited? */
137 pf_is_unvisited(int uid)
139 return pf_map[uid].visit < pf_visit;
142 #ifdef PATH_FIND_DEBUG
148 for (i = 0; i < pf_nheap; i++) {
149 uid = pf_heap[i].uid;
150 assert(0 <= uid && uid < WORLD_SZ());
151 assert(pf_map[uid].heapi == i);
152 assert(pf_map[uid].visit == pf_visit);
153 assert(pf_map[uid].cost <= pf_heap[i].cost);
155 assert(c >= pf_nheap || pf_heap[i].cost <= pf_heap[c].cost);
157 assert(c >= pf_nheap || pf_heap[i].cost <= pf_heap[c].cost);
160 for (uid = 0; uid < WORLD_SZ(); uid++) {
161 assert(pf_map[uid].visit <= pf_visit + 1);
162 if (pf_map[uid].visit == pf_visit) {
163 i = pf_map[uid].heapi;
164 assert(0 <= i && i < pf_nheap && pf_heap[i].uid == uid);
169 #define pf_check() ((void)0)
172 /* Swap pf_heap's I-th and J-th elements. */
174 pf_heap_swap(int i, int j)
178 assert(0 <= i && i < pf_nheap);
179 assert(0 <= j && j < pf_nheap);
181 pf_heap[i] = pf_heap[j];
183 pf_map[pf_heap[i].uid].heapi = i;
184 pf_map[pf_heap[j].uid].heapi = j;
187 /* Restore heap property after N-th element's cost increased. */
193 assert(0 <= n && n < pf_nheap);
194 for (r = n; (c = 2 * r + 1) < pf_nheap; r = c) {
195 if (c + 1 < pf_nheap && pf_heap[c].cost > pf_heap[c + 1].cost)
197 if (pf_heap[r].cost < pf_heap[c].cost)
203 /* Restore heap property after N-th element's cost decreased. */
209 assert(0 <= n && n < pf_nheap);
210 for (c = n; (p = (c - 1) / 2), c > 0; c = p) {
211 if (pf_heap[p].cost < pf_heap[c].cost)
218 * Open the unvisited sector X,Y.
219 * UID is sector uid, it equals XYOFFSET(X,Y).
220 * Cheapest path from source comes from direction DIR and has cost COST.
223 pf_open(int uid, coord x, coord y, int dir, double cost)
228 DPRINTF("pf: open %d,%d %g %c %d\n", x, y, cost, dirch[dir], i);
229 assert(pf_is_unvisited(uid));
230 pf_map[uid].visit = pf_visit;
231 pf_map[uid].dir = dir;
232 pf_map[uid].heapi = i;
233 pf_map[uid].cost = cost;
234 pf_heap[i].uid = uid;
237 pf_heap[i].cost = cost;
244 * Close the sector at the top of the heap.
249 int uid = pf_heap[0].uid;
251 DPRINTF("pf: close %d,%d %d\n", pf_heap[0].x, pf_heap[0].y, pf_nheap);
252 assert(pf_is_open(uid));
254 pf_heap[0] = pf_heap[pf_nheap];
255 pf_map[pf_heap[0].uid].heapi = 0;
258 pf_map[uid].visit = pf_visit + 1;
263 x_in_dir(coord x, int dir)
267 assert(0 <= x && x < WORLD_X);
268 assert(0 <= dir && dir <= DIR_LAST);
269 xx = x + diroff[dir][0];
278 y_in_dir(coord y, int dir)
282 assert(0 <= y && y < WORLD_Y);
283 assert(0 <= dir && dir <= DIR_LAST);
284 yy = y + diroff[dir][1];
295 assert(DIR_FIRST <= dir && dir <= DIR_LAST);
296 return dir >= DIR_FIRST + 3 ? dir - 3 : dir + 3;
300 * Set the current source and cost function.
301 * SX,SY is the source.
302 * The cost to enter the sector with uid u is COST(ACTOR, u).
303 * Negative value means the sector can't be entered.
306 pf_set_source(coord sx, coord sy, natid actor, double (*cost)(natid, int))
308 DPRINTF("pf: source %d,%d\n", sx, sy);
311 pf_suid = XYOFFSET(sx, sy);
316 pf_map = calloc(WORLD_SZ(), sizeof(*pf_map));
317 pf_heap = malloc(WORLD_SZ() * sizeof(*pf_heap));
319 } else if ((unsigned short)(pf_visit + 3) < pf_visit) {
320 DPRINTF("pf: visit wrap-around\n");
321 memset(pf_map, 0, WORLD_SZ() * sizeof(*pf_map));
328 pf_open(pf_suid, pf_sx, pf_sy, DIR_STOP, 0.0);
332 * Find cheapest path from current source to DX,DY, return its cost.
335 path_find_to(coord dx, coord dy)
342 DPRINTF("pf: dest %d,%d\n", dx, dy);
343 duid = XYOFFSET(dx, dy);
344 if (pf_is_closed(duid)) {
345 DPRINTF("pf: done old %g\n", pf_map[duid].cost);
346 return pf_map[duid].cost;
349 while (pf_nheap > 0 && (uid = pf_heap[0].uid) != duid) {
352 cost = pf_heap[0].cost;
355 for (i = 0; i < 6; i++) { /* for all neighbors */
356 nx = x_in_dir(x, DIR_FIRST + i);
357 ny = y_in_dir(y, DIR_FIRST + i);
358 nuid = XYOFFSET(nx, ny);
360 * Cost to enter NX,NY doesn't depend on direction of
361 * entry. This X,Y is at least as expensive as any
362 * previous one. Therefore, cost to go to NX,NY via X,Y
363 * is at least as high as any previously found route.
364 * Skip neighbors that have a route already.
366 if (!pf_is_unvisited(nuid))
368 c1 = pf_sct_cost(pf_actor, nuid);
371 pf_open(nuid, nx, ny, DIR_FIRST + i, cost + c1);
375 DPRINTF("pf: done new %g\n", !pf_nheap ? -1.0 : pf_map[duid].cost);
378 return pf_map[duid].cost;
382 * Write route from SX,SY to DX,DY to BUF[BUFSIZ], return its length.
383 * If the route is longer than BUFSIZ-1 characters, it's truncated.
384 * You must compute path cost first, with path_find_to().
385 * SX,SY must be on a shortest path from the current source to DX,DY.
388 path_find_route(char *buf, size_t bufsz,
389 coord sx, coord sy, coord dx, coord dy)
395 suid = XYOFFSET(sx, sy);
396 assert(bufsz > 0 && !pf_is_unvisited(suid));
405 DPRINTF("pf: %d,%d %.*s%.*s\n",
407 (int)(bufsz - i), buf + i,
408 len >= bufsz ? (int)i : 0, buf);
409 uid = XYOFFSET(x, y);
410 assert(!pf_is_unvisited(uid));
412 if (d == DIR_STOP || uid == suid)
418 x = x_in_dir(x, rev_dir(d));
419 y = y_in_dir(y, rev_dir(d));
422 assert(x == sx && y == sy);
424 bufrotate(buf, bufsz, i);
426 assert(i + len < bufsz);
427 memmove(buf, buf + i, len + 1);
433 * Rotate BUF[BUFSZ] to put BUF[I] into BUF[0], and zero-terminate.
436 bufrotate(char *buf, size_t bufsz, size_t i)
442 n = MIN(i, sizeof(tmp));
444 memcpy(buf, buf + n, bufsz - n);
445 memcpy(buf + bufsz - n, tmp, n);
454 * Empire interface glue
458 cost_land(natid actor, int uid, int mobtype)
461 * Non-negative cost must not depend on ACTOR, see BestLandPath().
463 struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
465 if (sp[uid].sct_own != actor)
467 return sector_mcost(&sp[uid], mobtype);
471 cost_move(natid actor, int uid)
473 return cost_land(actor, uid, MOB_MOVE);
477 cost_march(natid actor, int uid)
479 return cost_land(actor, uid, MOB_MARCH);
483 cost_rail(natid actor, int uid)
485 return cost_land(actor, uid, MOB_RAIL);
488 static double (*cost_tab[])(natid, int) = {
489 cost_move, cost_march, cost_rail
493 * Start finding paths from SX,SY.
494 * Use mobility costs for ACTOR and MOBTYPE.
497 path_find_from(coord sx, coord sy, natid actor, int mobtype)
499 pf_set_source(sx, sy, actor, cost_tab[mobtype]);
503 * Find cheapest path from SX,SY to DX,DY, return its mobility cost.
504 * Use mobility costs for ACTOR and MOBTYPE.
507 path_find(coord sx, coord sy, coord dx, coord dy, natid actor, int mobtype)
509 pf_set_source(sx, sy, actor, cost_tab[mobtype]);
510 return path_find_to(dx, dy);