]> git.pond.sub.org Git - empserver/blob - src/lib/common/path.c
cbd5f0a6f04b9d7d86d0c8acf4f681ede07a241d
[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 bp_mobtype;
55     struct as_data *adp;
56 };
57
58 static int bp_path(struct as_path *pp, char *buf);
59 static int bp_neighbors(struct as_coord c, struct as_coord *cp, void *);
60 static double bp_lbcost(struct as_coord from, struct as_coord to, void *);
61 static double bp_realcost(struct as_coord from, struct as_coord to, void *);
62 static double bp_seccost(struct as_coord from, struct as_coord to, void *);
63 static int bp_coord_hash(struct as_coord c);
64
65 /* We use this for caching neighbors.  It never changes except
66  * at reboot time (maybe) so we never need to free it */
67 static struct sctstr **neighsects;
68
69 static struct bestp *
70 bp_init(void)
71 {
72     struct bestp *bp;
73
74     bp = malloc(sizeof(*bp));
75     memset(bp, 0, sizeof(*bp));
76     bp->adp = as_init(BP_NEIGHBORS, BP_ASHASHSIZE, bp_coord_hash,
77                       bp_neighbors, bp_lbcost, bp_realcost,
78                       bp_seccost, bp);
79
80     if (bp->adp == NULL)
81         return NULL;
82
83     if (neighsects == NULL)
84         neighsects = calloc(WORLD_SZ() * 6, sizeof(struct sctstr *));
85
86     return bp;
87 }
88
89 /*
90  * Find the best path from sector to to sector, and put the Empire movement
91  * string in path.  Return 0 on success, -1 on error.
92  * FIXME unsafe by design: assumes path[] has space; buffer overrun
93  * when path gets long!
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     return 0;
125 }
126
127 /*
128  * Translate an A* path into an empire movement string.  Return 0 on
129  * success, -1 on failure.
130  */
131 static int
132 bp_path(struct as_path *pp, char *buf)
133 {
134     struct as_path *np;
135     char *cp = buf;
136     int dx, dy;
137     int n;
138
139     np = pp->next;
140     while (np) {
141         dx = np->c.x - pp->c.x;
142         /* deal with wraparound from non-neg coords */
143         if (dx < -2)
144             dx += WORLD_X;
145         else if (dx > 2)
146             dx -= WORLD_X;
147         dy = np->c.y - pp->c.y;
148         if (dy < -1)
149             dy += WORLD_Y;
150         else if (dy > 1)
151             dy -= WORLD_Y;
152         for (n = 1; n <= 6; n++)
153             if (dx == diroff[n][0] && dy == diroff[n][1])
154                 break;
155         if (n > 6)
156             return -1;
157
158         *cp++ = dirch[n];
159         pp = np;
160         np = np->next;
161     }
162     *cp = '\0';
163     return 0;
164 }
165
166 /*
167  * Find coords neighboring this sector; return number of such
168  * coords, and coordinartes themselves in an array pointed
169  * to by *cpp.
170  * XXX need to check ownership, sector types, etc.
171  */
172 static int
173 bp_neighbors(struct as_coord c, struct as_coord *cp, void *pp)
174 {
175     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
176     struct bestp *bp = pp;
177     coord x, y;
178     coord nx, ny;
179     int n = 0, i;
180     struct sctstr *sp, *from, **ssp;
181     /* Six pointers, just in case our cache isn't there */
182     struct sctstr *tsp[] = { NULL, NULL, NULL, NULL, NULL, NULL };
183     int sx, sy, offset;
184
185     x = c.x;
186     y = c.y;
187     sx = XNORM(x);
188     sy = YNORM(y);
189     offset = XYOFFSET(sx, sy);
190     from = &sectp[offset];
191
192     if (neighsects == NULL)
193         ssp = (struct sctstr **)&tsp[0];
194     else
195         ssp = (struct sctstr **)&neighsects[offset * 6];
196     for (i = 1; i <= 6; i++, ssp++) {
197         if (*ssp == NULL) {
198             /* We haven't cached this neighbor yet */
199             nx = x + diroff[i][0];
200             ny = y + diroff[i][1];
201             sx = XNORM(nx);
202             sy = YNORM(ny);
203             offset = XYOFFSET(sx, sy);
204             sp = &sectp[offset];
205             *ssp = sp;
206         } else {
207             sp = *ssp;
208             sx = XNORM(sp->sct_x);
209             sy = YNORM(sp->sct_y);
210         }
211         /* No need to calculate cost each time, just make sure we can
212            move through it.  We calculate it later. */
213         if (dchr[sp->sct_type].d_mob0 < 0)
214             continue;
215         if (bp->bp_mobtype == MOB_RAIL && !SCT_HAS_RAIL(sp))
216             continue;
217         if (sp->sct_own != from->sct_own)
218             continue;
219         cp[n].x = sx;
220         cp[n].y = sy;
221         n++;
222     }
223     return n;
224 }
225
226 /*
227  * Compute a lower-bound on the cost from "from" to "to".
228  */
229 /*ARGSUSED*/
230 static double
231 bp_lbcost(struct as_coord from, struct as_coord to, void *pp)
232 {
233     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
234     struct bestp *bp = pp;
235     int x, y, sx, sy, offset;
236
237     x = to.x;
238     y = to.y;
239     sx = XNORM(x);
240     sy = YNORM(y);
241     offset = XYOFFSET(sx, sy);
242     return sector_mcost(&sectp[offset], bp->bp_mobtype);
243 }
244
245 /*
246  * Compute the real cost to move from "from" to "to".
247  */
248 static double
249 bp_realcost(struct as_coord from, struct as_coord to, void *pp)
250 {
251     return bp_lbcost(from, to, pp);
252 }
253
254 /*
255  * Tie breaker secondary metric (only used when lower bound costs
256  * are equal).
257  */
258 /*ARGSUSED*/
259 static double
260 bp_seccost(struct as_coord from, struct as_coord to, void *pp)
261 {
262     return mapdist((coord)from.x, (coord)from.y,
263                    (coord)to.x, (coord)to.y);
264 }
265
266 /*
267  * Hash a coordinate into an integer.
268  */
269 static int
270 bp_coord_hash(struct as_coord c)
271 {
272     return ((abs(c.x) + 1) << 3) ^ abs(c.y);
273 }
274
275 void
276 bp_enable_cachepath(void)
277 {
278     as_enable_cachepath();
279 }
280
281 void
282 bp_disable_cachepath(void)
283 {
284     as_disable_cachepath();
285 }
286
287 void
288 bp_clear_cachepath(void)
289 {
290     as_clear_cachepath();
291 }
292
293 double
294 pathcost(struct sctstr *start, char *path, int mob_type)
295 {
296     struct sctstr *sectp = (void *)empfile[EF_SECTOR].cache;
297     unsigned i;
298     int o;
299     int cx, cy;
300     double cost = 0.0;
301     struct sctstr *sp;
302     int sx, sy, offset;
303
304     cx = start->sct_x;
305     cy = start->sct_y;
306
307     while (*path) {
308         if (*path == 'h') {
309             path++;
310             continue;
311         }
312         i = *path - 'a';
313         if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)))
314             break;
315         o = dirindex[i];
316         if (CANT_HAPPEN(o < 0))
317             break;
318         cx += diroff[o][0];
319         cy += diroff[o][1];
320         sx = XNORM(cx);
321         sy = YNORM(cy);
322         offset = XYOFFSET(sx, sy);
323         sp = &sectp[offset];
324         cost += sector_mcost(sp, mob_type);
325         path++;
326     }
327
328     return cost;
329 }
330
331 char *
332 BestDistPath(char *path,
333              struct sctstr *from,
334              struct sctstr *to, double *cost)
335 {
336     return BestLandPath(path, from, to, cost, MOB_MOVE);
337 }
338
339 char *
340 BestLandPath(char *path,
341              struct sctstr *from,
342              struct sctstr *to, double *cost, int mob_type)
343 {
344     int length;
345
346     *path = 0;
347     *cost = 0.0;
348     if (best_path(from, to, path, mob_type) < 0)
349         return NULL;
350     *cost = pathcost(from, path, mob_type);
351     length = strlen(path);
352     path[length] = 'h';
353     path[length + 1] = '\0';
354     return path;
355 }
356
357 char *
358 BestShipPath(char *path, int fx, int fy, int tx, int ty, int owner)
359 {
360     char *map;
361
362     map = ef_ptr(EF_BMAP, owner);
363     if (!map)
364         return NULL;
365     return bestownedpath(path, map, fx, fy, tx, ty, owner);
366 }
367
368 char *
369 BestAirPath(char *path, int fx, int fy, int tx, int ty)
370 {
371     return bestownedpath(path, NULL, fx, fy, tx, ty, -1);
372 }