]> git.pond.sub.org Git - empserver/blob - src/lib/common/path.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / common / path.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 <stdlib.h>
42 #include "../as/as.h"
43 #include "misc.h"
44 #include "path.h"
45 #include "xy.h"
46 #include "sect.h"
47 #include "file.h"
48 #include "common.h"
49 #include "gen.h"
50 #include "optlist.h"
51
52 #define BP_ASHASHSIZE   128     /* A* queue hash table size */
53 #define BP_NEIGHBORS    6       /* max number of neighbors */
54
55 struct bestp {
56     int sctcache_hits;
57     int sctcache_misses;
58     int bp_mobtype;
59     struct as_data *adp;
60 };
61
62 static int bp_path(struct as_path *pp, s_char *buf);
63 static int bp_neighbors(struct as_coord c, struct as_coord *cp,
64                         s_char *pp);
65 static double bp_lbcost(struct as_coord from, struct as_coord to,
66                         s_char *pp);
67 static double bp_realcost(struct as_coord from, struct as_coord to,
68                           s_char *pp);
69 static double bp_seccost(struct as_coord from, struct as_coord to,
70                          s_char *pp);
71 static int bp_coord_hash(struct as_coord c);
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     bp = malloc(sizeof(*bp));
83     memset(bp, 0, sizeof(*bp));
84     bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
85                       bp_neighbors, bp_lbcost, bp_realcost,
86                       bp_seccost, (s_char *)bp);
87
88     if (bp->adp == NULL)
89         return NULL;
90
91     if (neighsects == (struct sctstr **)0)
92         neighsects = calloc(((WORLD_X * WORLD_Y) / 2) * 6,
93                             sizeof(struct sctstr *));
94
95     return (s_char *)bp;
96 }
97
98 /*
99  * Find the best path from sector to to sector, and put the Empire movement
100  * string in path.  Return 0 on success, -1 on error.
101  */
102 static int
103 best_path(struct sctstr *from, struct sctstr *to, s_char *path,
104           int mob_type)
105 {
106     static struct bestp *mybestpath;
107     struct as_data *adp;
108     struct as_path *ap;
109
110     if (mybestpath == 0)
111         mybestpath = (struct bestp *)bp_init();
112     adp = mybestpath->adp;
113     ap = as_find_cachepath(from->sct_x, from->sct_y, to->sct_x, to->sct_y);
114     if (ap == NULL) {
115         adp->from.x = from->sct_x;
116         adp->from.y = from->sct_y;
117         adp->to.x = to->sct_x;
118         adp->to.y = to->sct_y;
119         mybestpath->bp_mobtype = mob_type;
120
121         if (as_search(adp) < 0)
122             return -1;
123         ap = adp->path;
124     }
125
126     if (bp_path(ap, path) < 0)
127         return -1;
128
129 #ifdef AS_STATS
130     as_stats(adp, stderr);
131 #endif /* AS_STATS */
132 #ifdef BP_STATS
133     fprintf(stderr, "best path %s\n", path);
134     fprintf(stderr, "cache hits/misses: %d/%d\n",
135             bp->sctcache_hits, bp->sctcache_misses);
136 #endif /* BP_STATS */
137     return 0;
138 }
139
140 /*
141  * Translate an A* path into an empire movement string.  Return 0 on
142  * success, -1 on failure.
143  */
144 static int
145 bp_path(struct as_path *pp, s_char *buf)
146 {
147     struct as_path *np;
148     s_char *cp = buf;
149     int dx, dy;
150     int n;
151
152     np = pp->next;
153     while (np) {
154         dx = np->c.x - pp->c.x;
155         /* deal with wraparound from non-neg coords */
156         if (dx < -2)
157             dx += WORLD_X;
158         else if (dx > 2)
159             dx -= WORLD_X;
160         dy = np->c.y - pp->c.y;
161         if (dy < -1)
162             dy += WORLD_Y;
163         else if (dy > 1)
164             dy -= WORLD_Y;
165         for (n = 1; n <= 6; n++)
166             if (dx == diroff[n][0] && dy == diroff[n][1])
167                 break;
168         if (n > 6)
169             return -1;
170
171         *cp++ = dirch[n];
172         pp = np;
173         np = np->next;
174     }
175     *cp = '\0';
176     return 0;
177 }
178
179 /*
180  * Find coords neighboring this sector; return number of such
181  * coords, and coordinartes themselves in an array pointed
182  * to by *cpp.
183  * XXX need to check ownership, sector types, etc.
184  */
185 static int
186 bp_neighbors(struct as_coord c, struct as_coord *cp, s_char *pp)
187 {
188     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
189     coord x, y;
190     coord nx, ny;
191     int n = 0, q;
192     struct sctstr *sp, *from, **ssp;
193     /* Six pointers, just in case our cache isn't there */
194     struct sctstr *tsp[] = { 0, 0, 0, 0, 0, 0 };
195     int sx, sy, offset;
196
197     x = c.x;
198     y = c.y;
199     sx = XNORM(x);
200     sy = YNORM(y);
201     offset = (sy * WORLD_X + sx) / 2;
202     from = &sectp[offset];
203
204     if (neighsects == (struct sctstr **)0)
205         ssp = (struct sctstr **)&tsp[0];
206     else
207         ssp = (struct sctstr **)&neighsects[offset * 6];
208     for (q = 1; q <= 6; q++, ssp++) {
209         if (*ssp == (struct sctstr *)0) {
210             /* We haven't cached this neighbor yet */
211             nx = x + diroff[q][0];
212             ny = y + diroff[q][1];
213             sx = XNORM(nx);
214             sy = YNORM(ny);
215             offset = (sy * WORLD_X + sx) / 2;
216             sp = &sectp[offset];
217             *ssp = sp;
218         } else {
219             sp = *ssp;
220             sx = XNORM(sp->sct_x);
221             sy = YNORM(sp->sct_y);
222         }
223         /* No need to calculate cost each time, just make sure we can
224            move through it.  We calculate it later. */
225         if (dchr[sp->sct_type].d_mcst == 0)
226             continue;
227         if (sp->sct_own != from->sct_own)
228             continue;
229         cp[n].x = sx;
230         cp[n].y = sy;
231         n++;
232     }
233     return n;
234 }
235
236 /*
237  * Compute a lower-bound on the cost from "from" to "to".
238  */
239 /*ARGSUSED*/
240 static double
241 bp_lbcost(struct as_coord from, struct as_coord to, s_char *pp)
242 {
243     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
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 = &sectp[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     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
311     unsigned i;
312     int o;
313     int cx, cy;
314     double cost = 0.0;
315     struct sctstr *sp;
316     int sx, sy, offset;
317
318     cx = start->sct_x;
319     cy = start->sct_y;
320
321     while (*path) {
322         if (*path == 'h') {
323             path++;
324             continue;
325         }
326         i = *path - 'a';
327         if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
328             break;
329         o = dirindex[i];
330         if (CANT_HAPPEN(o < 0))
331             break;
332         cx += diroff[o][0];
333         cy += diroff[o][1];
334         sx = XNORM(cx);
335         sy = YNORM(cy);
336         offset = (sy * WORLD_X + sx) / 2;
337         sp = &sectp[offset];
338         cost += sector_mcost(sp, mob_type);
339         path++;
340     }
341
342     return cost;
343 }
344
345 s_char *
346 BestDistPath(s_char *path,
347              struct sctstr *from,
348              struct sctstr *to, double *cost, int mob_type)
349 {
350     return BestLandPath(path, from, to, cost, mob_type);
351 }
352
353 s_char *
354 BestLandPath(s_char *path,
355              struct sctstr *from,
356              struct sctstr *to, double *cost, int mob_type)
357 {
358     int length;
359
360     *path = 0;
361     *cost = 0.0;
362     if (best_path(from, to, path, mob_type) < 0)
363         return (s_char *)0;
364     *cost = pathcost(from, path, mob_type);
365     length = strlen(path);
366     path[length] = 'h';
367     path[length + 1] = '\0';
368     return path;
369 }
370
371 s_char *
372 BestShipPath(s_char *path, int fx, int fy, int tx, int ty, int owner)
373 {
374     s_char *map;
375
376     /* need to make sector database available to bestpath */
377     map = ef_ptr(EF_BMAP, owner);
378
379     return bestownedpath(path, map, fx, fy, tx, ty, ".=h", owner);
380 }
381
382 s_char *
383 BestAirPath(s_char *path, int fx, int fy, int tx, int ty)
384 {
385     return bestownedpath(path, 0, fx, fy, tx, ty, "", -1);
386     /*    return (bestpath(path, fx, fy, tx, ty, "")); */
387 }