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