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