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