]> git.pond.sub.org Git - empserver/blob - src/lib/common/path.c
(dirindex): Narrow member type to signed char to save cache.
[empserver] / src / lib / common / path.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 of the License, or
9  *  (at your option) any later version.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  path.c: Empire/A* Interface code.
29  *          Define AS_STATS for A* statistics.
30  * 
31  *  Known contributors to this file:
32  *     Phil Lapsley, 1991
33  *     Dave Pare, 1991
34  *     Thomas Ruschak, 1993
35  *     Steve McClure, 1997
36  */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "../as/as.h"
41 #include "misc.h"
42 #include "path.h"
43 #include "xy.h"
44 #include "sect.h"
45 #include "file.h"
46 #include "common.h"
47 #include "gen.h"
48 #include "optlist.h"
49
50 #define BP_ASHASHSIZE   128     /* A* queue hash table size */
51 #define BP_NEIGHBORS    6       /* max number of neighbors */
52
53 struct bestp {
54     int sctcache_hits;
55     int sctcache_misses;
56     int bp_mobtype;
57     struct as_data *adp;
58 };
59
60 static int bp_path(struct as_path *pp, s_char *buf);
61 static int bp_neighbors(struct as_coord c, struct as_coord *cp,
62                         s_char *pp);
63 static double bp_lbcost(struct as_coord from, struct as_coord to,
64                         s_char *pp);
65 static double bp_realcost(struct as_coord from, struct as_coord to,
66                           s_char *pp);
67 static double bp_seccost(struct as_coord from, struct as_coord to,
68                          s_char *pp);
69 static int bp_coord_hash(struct as_coord c);
70
71 struct empfile *ep;
72
73 /* We use this for caching neighbors.  It never changes except
74  * at reboot time (maybe) so we never need to free it */
75 struct sctstr **neighsects = (struct sctstr **)0;
76
77 static s_char *
78 bp_init(void)
79 {
80     struct bestp *bp;
81
82     ep = &empfile[EF_SECTOR];
83
84     bp = malloc(sizeof(*bp));
85     memset(bp, 0, sizeof(*bp));
86     bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
87                       bp_neighbors, bp_lbcost, bp_realcost,
88                       bp_seccost, (s_char *)bp);
89
90     if (bp->adp == NULL)
91         return NULL;
92
93     if (neighsects == (struct sctstr **)0)
94         neighsects = calloc(((WORLD_X * WORLD_Y) / 2) * 6,
95                             sizeof(struct sctstr *));
96
97     return (s_char *)bp;
98 }
99
100 /*
101  * Find the best path from sector to to sector, and put the Empire movement
102  * string in path.  Return 0 on success, -1 on error.
103  */
104 static int
105 best_path(struct sctstr *from, struct sctstr *to, s_char *path,
106           int mob_type)
107 {
108     static struct bestp *mybestpath;
109     struct as_data *adp;
110     struct as_path *ap;
111
112     if (mybestpath == 0)
113         mybestpath = (struct bestp *)bp_init();
114     adp = mybestpath->adp;
115     ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
116     if (ap == NULL) {
117         adp->from.x = from->sct_x;
118         adp->from.y = from->sct_y;
119         adp->to.x = to->sct_x;
120         adp->to.y = to->sct_y;
121         mybestpath->bp_mobtype = mob_type;
122
123         if (as_search(adp) < 0)
124             return -1;
125         ap = adp->path;
126     }
127
128     if (bp_path(ap, path) < 0)
129         return -1;
130
131 #ifdef AS_STATS
132     as_stats(adp, stderr);
133 #endif /* AS_STATS */
134 #ifdef BP_STATS
135     fprintf(stderr, "best path %s\n", path);
136     fprintf(stderr, "cache hits/misses: %d/%d\n",
137             bp->sctcache_hits, bp->sctcache_misses);
138 #endif /* BP_STATS */
139     return 0;
140 }
141
142 /*
143  * Translate an A* path into an empire movement string.  Return 0 on
144  * success, -1 on failure.
145  */
146 static int
147 bp_path(struct as_path *pp, s_char *buf)
148 {
149     struct as_path *np;
150     s_char *cp = buf;
151     int dx, dy;
152     int n;
153
154     np = pp->next;
155     while (np) {
156         dx = np->c.x - pp->c.x;
157         /* deal with wraparound from non-neg coords */
158         if (dx < -2)
159             dx += WORLD_X;
160         else if (dx > 2)
161             dx -= WORLD_X;
162         dy = np->c.y - pp->c.y;
163         if (dy < -1)
164             dy += WORLD_Y;
165         else if (dy > 1)
166             dy -= WORLD_Y;
167         for (n = 1; n <= 6; n++)
168             if (dx == diroff[n][0] && dy == diroff[n][1])
169                 break;
170         if (n > 6)
171             return -1;
172
173         *cp++ = dirch[n];
174         pp = np;
175         np = np->next;
176     }
177     *cp = '\0';
178     return 0;
179 }
180
181 /*
182  * Find coords neighboring this sector; return number of such
183  * coords, and coordinartes themselves in an array pointed
184  * to by *cpp.
185  * XXX need to check ownership, sector types, etc.
186  */
187 static int
188 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
189 {
190     coord x, y;
191     coord nx, ny;
192     int n = 0, q;
193     struct sctstr *sp, *from, **ssp;
194     /* Six pointers, just in case our cache isn't there */
195     struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
196     int sx, sy, offset;
197
198     x = c.x;
199     y = c.y;
200     sx = XNORM(x);
201     sy = YNORM(y);
202     offset = (sy * WORLD_X + sx) / 2;
203     from = (struct sctstr *)(ep->cache + ep->size * offset);
204
205     if (neighsects == (struct sctstr **)0)
206         ssp = (struct sctstr **)&tsp[0];
207     else
208         ssp = (struct sctstr **)&neighsects[offset * 6];
209     for (q = 1; q <= 6; q++, ssp++) {
210         if (*ssp == (struct sctstr *)0) {
211             /* We haven't cached this neighbor yet */
212             nx = x + diroff[q][0];
213             ny = y + diroff[q][1];
214             sx = XNORM(nx);
215             sy = YNORM(ny);
216             offset = (sy * WORLD_X + sx) / 2;
217             sp = (struct sctstr *)(ep->cache + ep->size * offset);
218             *ssp = sp;
219         } else {
220             sp = *ssp;
221             sx = XNORM(sp->sct_x);
222             sy = YNORM(sp->sct_y);
223         }
224         /* No need to calculate cost each time, just make sure we can
225            move through it.  We calculate it later. */
226         if (dchr[sp->sct_type].d_mcst == 0)
227             continue;
228         if (sp->sct_own != from->sct_own)
229             continue;
230         cp[n].x = sx;
231         cp[n].y = sy;
232         n++;
233     }
234     return n;
235 }
236
237 /*
238  * Compute a lower-bound on the cost from "from" to "to".
239  */
240 /*ARGSUSED*/
241 static double
242 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
243 {
244     struct bestp *bp = (struct bestp *)pp;
245     struct sctstr *ts;
246     float cost;
247     int x, y, sx, sy, offset;
248
249     x = to.x;
250     y = to.y;
251     sx = XNORM(x);
252     sy = YNORM(y);
253     offset = (sy * WORLD_X + sx) / 2;
254     ts = (struct sctstr *)(ep->cache + ep->size * offset);
255     cost = sector_mcost(ts, bp->bp_mobtype);
256     return cost;
257 }
258
259 /*
260  * Compute the real cost to move from "from" to "to".
261  */
262 static double
263 bp_realcost(struct as_coord from, struct as_coord to, s_char *pp)
264 {
265     return bp_lbcost(from, to, pp);
266 }
267
268 /*
269  * Tie breaker secondary metric (only used when lower bound costs
270  * are equal).
271  */
272 /*ARGSUSED*/
273 static double
274 bp_seccost(struct as_coord from, struct as_coord to, s_char *pp)
275 {
276     return ((double)mapdist((coord)from.x, (coord)from.y,
277                             (coord)to.x, (coord)to.y));
278 }
279
280 /*
281  * Hash a coordinate into an integer.
282  */
283 static int
284 bp_coord_hash(struct as_coord c)
285 {
286     return ((abs(c.x) + 1) << 3) ^ abs(c.y);
287 }
288
289 void
290 bp_enable_cachepath(void)
291 {
292     as_enable_cachepath();
293 }
294
295 void
296 bp_disable_cachepath(void)
297 {
298     as_disable_cachepath();
299 }
300
301 void
302 bp_clear_cachepath(void)
303 {
304     as_clear_cachepath();
305 }
306
307 double
308 pathcost(struct sctstr *start, s_char *path, int mob_type)
309 {
310     unsigned i;
311     int o;
312     int cx, cy;
313     double cost = 0.0;
314     struct sctstr *sp;
315     int sx, sy, offset;
316
317     cx = start->sct_x;
318     cy = start->sct_y;
319
320     while (*path) {
321         if (*path == 'h') {
322             path++;
323             continue;
324         }
325         i = *path - 'a';
326         if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
327             break;
328         o = dirindex[i];
329         if (CANT_HAPPEN(o) < 0)
330             break;
331         cx += diroff[o][0];
332         cy += diroff[o][1];
333         sx = XNORM(cx);
334         sy = YNORM(cy);
335         offset = (sy * WORLD_X + sx) / 2;
336         sp = (struct sctstr *)(ep->cache + ep->size * offset);
337         cost += sector_mcost(sp, mob_type);
338         path++;
339     }
340
341     return cost;
342 }
343
344 s_char *
345 BestDistPath(s_char *path,
346              struct sctstr *from,
347              struct sctstr *to, double *cost, int mob_type)
348 {
349     return BestLandPath(path, from, to, cost, mob_type);
350 }
351
352 s_char *
353 BestLandPath(s_char *path,
354              struct sctstr *from,
355              struct sctstr *to, double *cost, int mob_type)
356 {
357     int length;
358
359     *path = 0;
360     *cost = 0.0;
361     if (best_path(from, to, path, mob_type) < 0)
362         return (s_char *)0;
363     *cost = pathcost(from, path, mob_type);
364     length = strlen(path);
365     path[length] = 'h';
366     path[length + 1] = '\0';
367     return path;
368 }
369
370 s_char *
371 BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
372 {
373     s_char *map;
374
375     /* need to make sector database available to bestpath */
376     map = ef_ptr(EF_BMAP, owner);
377
378     return bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner);
379 }
380
381 s_char *
382 BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
383 {
384     return bestownedpath(path, 0, fx, fy, tx, ty, "", -1);
385     /*    return (bestpath(path, fx, fy, tx, ty, "")); */
386 }