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