]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
87b0b2582384c9da723696ae5643bd64d0ff3144
[empserver] / src / lib / common / bestpath.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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  bestpath.c: Find the best path between sectors
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998-2000
32  */
33
34 /* 
35  * IMPORTANT: These routines are very selectively used in the server.
36  *
37  * "bestpath" is now obsolete (and removed)
38  * "bestownedpath" is only used to determine paths for ships and planes.
39  * 
40  * Callers should not be calling these directly anymore. They should use
41  * the "BestShipPath", "BestAirPath", "BestLandPath" and "BestDistPath"
42  * functions.  Note that those last two use the A* algorithms to find
43  * information.
44  */
45
46 #include <config.h>
47
48 #include "misc.h"
49 #include "xy.h"
50 #include "sect.h"
51 #include "file.h"
52 #include "nat.h"
53 #include "common.h"
54 #include "optlist.h"
55
56 static int owned_and_navigable(s_char *, int, int, s_char *, int);
57
58 #define MAXROUTE        100     /* return '?' if path longer than this */
59 #define valid(x,y)      (((x^y)&1)==0)
60
61 /* ________________________________________________________________
62 **
63 **  bestpath(x1,y1,x2,y2,(s_char *)terrain);
64 **
65 **  Calculate routing string to get from sector [x1,y1] to sector [x2,y2]
66 **  via a specified type of terrain.
67 **
68 **  Specify:
69 **
70 **      x1,y1        starting coordinates
71 **
72 **      x2,y2        destination coordinates
73 **
74 **      terrain      ptr to string showing the types of sectors that
75 **                   we're allowed to pass through:
76 **
77 **                   A null string enables routing through any kind of
78 **                   sector (useful for airplanes).
79 **
80 **                   A string that begins with an 'R' ensures that
81 **                   the source and destination sectors also match
82 **                   the specified type of terrain.
83 **
84 **                   A string that begins with a '~' (after the 'R',
85 **                   if necessary) specifies that we can pass through
86 **                   any kind of sector EXCEPT those in the remainder
87 **                   of the string.
88 **
89 **                   Examples:
90 **
91 **                      "R~.^"  all sectors along route must be
92 **                              non-ocean, non-mountain
93 **
94 **                      "+"     all sectors between start and end
95 **                              must be highway
96 **
97 **                      "h. "   all sectors along route must be
98 **                              harbor, water, or unmapped
99 **
100 **  'bestpath' returns a pointer to a route string containing either:
101 **
102 **     yugjbn - string of normal routing characters if route possible
103 **     ?      - if route is longer than MAXROUTE characters
104 **     \0     - (null string) if no route possible
105 **     h      - if start and end points are the same sector
106 ** ________________________________________________________________
107 */
108
109 s_char *dirchar = "juygbn";
110 int dx[6] = { 2, 1, -1, -2, -1, 1 };
111 int dy[6] = { 0, -1, -1, 0, 1, 1 };
112
113 /*
114  * Ok, note that here we malloc some buffers.  BUT, we never
115  * free them.  Why, you may ask?  Because we want to allocate
116  * them based on world size which is now (or soon to be) dynamic,
117  * but we don't want to allocate each and every time, since that
118  * would be slow.  And, since world size only changes at init
119  * time, we can do this safely.
120  * We also share these buffers between "bestpath" and "bestownedpath"
121  * since you can never be in both functions at the same time.  If that
122  * did happen, we'd already be so broken that it won't matter.
123  */
124 static unsigned int *mapbuf = (unsigned int *)0;
125 static unsigned int **mapindex = (unsigned int **)0;
126
127 s_char *
128 bestownedpath(s_char *bpath,
129               s_char *bigmap,
130               int x, int y, int ex, int ey, s_char *terrain, int own)
131 {
132     int i, j, tx, ty, markedsectors, restr2;
133     int minx, maxx, miny, maxy, scanx, scany;
134     unsigned int routelen;
135
136     if (!mapbuf)
137         mapbuf = malloc((WORLD_X * WORLD_Y) *
138                                         sizeof(unsigned int));
139     if (!mapbuf)
140         return NULL;
141     if (!mapindex) {
142         mapindex = malloc(WORLD_X * sizeof(unsigned int *));
143         if (mapindex) {
144             /* Setup the map pointers */
145             for (i = 0; i < WORLD_X; i++)
146                 mapindex[i] = &mapbuf[WORLD_Y * i];
147         }
148     }
149     if (!mapindex)
150         return NULL;
151
152     bpath[0] = 0;
153     if (0 != (restr2 = (*terrain == 'R')))
154         terrain++;
155
156     x = XNORM(x);
157     y = YNORM(y);
158     ex = XNORM(ex);
159     ey = YNORM(ey);
160
161     if (x == ex && y == ey) {
162         bpath[0] = 'h';
163         bpath[1] = 0;
164         return bpath;
165     }
166
167     if (!valid(x, y) || !valid(ex, ey))
168         return NULL;
169
170     if (restr2 && (!owned_and_navigable(bigmap, x, y, terrain, own) ||
171                    !owned_and_navigable(bigmap, x, y, terrain, own)))
172         return NULL;
173
174     for (i = 0; i < WORLD_X; i++)
175         for (j = 0; j < WORLD_Y; j++)
176             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
177
178     routelen = 0;               /* path length is now 0 */
179     mapindex[x][y] = 0;         /* mark starting spot   */
180     markedsectors = 1;          /* source sector marked */
181     minx = x - 2;               /* set X scan bounds    */
182     maxx = x + 2;
183     miny = y - 1;               /* set Y scan bounds    */
184     maxy = y + 1;
185
186     do {
187         if (++routelen == MAXROUTE) {
188             bpath[0] = '?';
189             bpath[1] = 0;
190             return bpath;
191         }
192         markedsectors = 0;
193         for (scanx = minx; scanx <= maxx; scanx++) {
194             x = XNORM(scanx);
195             for (scany = miny; scany <= maxy; scany++) {
196                 y = YNORM(scany);
197                 if (valid(x, y)) {
198                     if ((((mapindex[x][y]) & 0x1FFF) == (routelen - 1))) {
199                         for (i = 0; i < 6; i++) {
200                             tx = x + dx[i];
201                             ty = y + dy[i];
202                             tx = XNORM(tx);
203                             ty = YNORM(ty);
204                             if (mapindex[tx][ty] == 0xFFFF) {
205                                 if (owned_and_navigable(bigmap, tx, ty,
206                                                         terrain, own)
207                                     || (tx == ex && ty == ey && !restr2)) {
208                                     mapindex[tx][ty] =
209                                         ((i + 1) << 13) + routelen;
210                                     markedsectors++;
211                                 }
212                             }
213                             if (tx == ex && ty == ey) {
214                                 bpath[routelen] = 0;
215                                 while (routelen--) {
216                                     i = ((mapindex[tx][ty]) >> 13) - 1;
217                                     bpath[routelen] = dirchar[i];
218                                     tx = tx - dx[i];
219                                     ty = ty - dy[i];
220                                     tx = XNORM(tx);
221                                     ty = YNORM(ty);
222                                 }
223                                 return bpath;
224                             }
225                         }
226                     }
227                 }
228             }
229         }
230         miny--;
231         maxy++;
232         minx -= 2;
233         maxx += 2;
234     } while (markedsectors);
235
236     bpath[0] = 0;
237     return NULL;                /* no route possible    */
238 }
239
240 /* return TRUE if sector is passable */
241 static int
242 owned_and_navigable(s_char *map, int x, int y, s_char *terrain, int own)
243 {
244     s_char c;
245     s_char *t;
246     s_char mapspot;             /* What this spot on the bmap is */
247     int negate;
248     struct sctstr *sect;
249     int rel;
250
251     /* No terrain to check?  Everything is navigable! (this
252        probably means we are flying) */
253     if (!(*terrain))
254         return 1;
255
256     /* Are we checking this map? */
257     if (map) {
258         /* Do we know what this sector is?  If not, we assume it's ok,
259            since otherwise we'll never venture anywhere */
260         mapspot = map[sctoff(x, y)];
261         if (mapspot == ' ' || mapspot == 0)
262             return 1;
263
264         /* Now, is it marked with a 'x' or 'X'? If so, avoid it! */
265         if (mapspot == 'x' || mapspot == 'X')
266             return 0;
267     } else {
268         /* We don't know what it is since we have no map, so return ok! */
269         return 1;
270     }
271
272     /* Now, check this bmap entry to see if it is one of the
273        terrain types. */
274     t = terrain;
275     if (*t == '~') {
276         negate = 1;
277         t++;
278     } else
279         negate = 0;
280
281     while (*t) {
282         if (*t == mapspot)
283             break;
284         t++;
285     }
286     if (negate && *t) {
287         /* We found it, so we say it's bad since we are negating */
288         return 0;
289     } else if (!negate && !*t) {
290         /* We didn't find it, so we say it's bad since we aren't negating */
291         return 0;
292     }
293
294     /* According to our bmap, this sector is ok so far. */
295
296     /* Ok, we made it this far.  Now get the sector */
297     sect = getsectp(x, y);
298     c = dchr[sect->sct_type].d_mnem;
299     /* Ok, now, check the owner if needed */
300     if (own >= 0) {
301         if (sect->sct_own != own) {
302             rel = getrel(getnatp(sect->sct_own), own);
303             /* We can't sail through deity sectors, but we can sail
304                through any ocean */
305             if (rel < FRIENDLY && sect->sct_type != SCT_WATER)
306                 return 0;
307         }
308     }
309     /* Ok, now, check these two sector types */
310     /* check for bad harbors. */
311     if (c == 'h' && sect->sct_effic < 2)
312         return 0;
313     /* check for bad bridges  */
314     if (c == '=' && sect->sct_effic < 60)
315         return 0;
316     /* Woo-hoo, it's ok! */
317     return 1;
318 }