]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / common / bestpath.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "gamesdef.h"
47 #include "misc.h"
48 #include "xy.h"
49 #include "var.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 int tmp;
113
114 /*
115  * Ok, note that here we malloc some buffers.  BUT, we never
116  * free them.  Why, you may ask?  Because we want to allocate
117  * them based on world size which is now (or soon to be) dynamic,
118  * but we don't want to allocate each and every time, since that
119  * would be slow.  And, since world size only changes at init
120  * time, we can do this safely.
121  * We also share these buffers between "bestpath" and "bestownedpath"
122  * since you can never be in both functions at the same time.  If that
123  * did happen, we'd already be so broken that it won't matter.
124  */
125 static unsigned int *mapbuf = (unsigned int *)0;
126 static unsigned int **mapindex = (unsigned int **)0;
127
128 s_char *
129 bestownedpath(s_char *bpath,
130               s_char *bigmap,
131               int x, int y, int ex, int ey, s_char *terrain, int own)
132 {
133     int i, j, tx, ty, markedsectors, restr2;
134     int minx, maxx, miny, maxy, scanx, scany;
135     unsigned int routelen;
136
137     if (!mapbuf)
138         mapbuf = (unsigned int *)malloc((WORLD_X * WORLD_Y) *
139                                         sizeof(unsigned int));
140     if (!mapbuf)
141         return ((s_char *)0);
142     if (!mapindex) {
143         mapindex =
144             (unsigned int **)malloc(WORLD_X * sizeof(unsigned int *));
145         if (mapindex) {
146             /* Setup the map pointers */
147             for (i = 0; i < WORLD_X; i++)
148                 mapindex[i] = &mapbuf[WORLD_Y * i];
149         }
150     }
151     if (!mapindex)
152         return ((s_char *)0);
153
154     bpath[0] = 0;
155     if (0 != (restr2 = (*terrain == 'R')))
156         terrain++;
157
158     x = XNORM(x);
159     y = YNORM(y);
160     ex = XNORM(ex);
161     ey = YNORM(ey);
162
163     if (x == ex && y == ey) {
164         bpath[0] = 'h';
165         bpath[1] = 0;
166         return ((s_char *)bpath);
167     }
168
169     if (!valid(x, y) || !valid(ex, ey))
170         return ((s_char *)0);
171
172     if (restr2 && (!owned_and_navigable(bigmap, x, y, terrain, own) ||
173                    !owned_and_navigable(bigmap, x, y, terrain, own)))
174         return ((s_char *)0);
175
176     for (i = 0; i < WORLD_X; i++)
177         for (j = 0; j < WORLD_Y; j++)
178             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
179
180     routelen = 0;               /* path length is now 0 */
181     mapindex[x][y] = 0;         /* mark starting spot   */
182     markedsectors = 1;          /* source sector marked */
183     minx = x - 2;               /* set X scan bounds    */
184     maxx = x + 2;
185     miny = y - 1;               /* set Y scan bounds    */
186     maxy = y + 1;
187
188     do {
189         if (++routelen == MAXROUTE) {
190             bpath[0] = '?';
191             bpath[1] = 0;
192             return ((s_char *)bpath);
193         }
194         markedsectors = 0;
195         for (scanx = minx; scanx <= maxx; scanx++) {
196             x = XNORM(scanx);
197             for (scany = miny; scany <= maxy; scany++) {
198                 y = YNORM(scany);
199                 if (valid(x, y)) {
200                     if ((((mapindex[x][y]) & 0x1FFF) == (routelen - 1))) {
201                         for (i = 0; i < 6; i++) {
202                             tx = x + dx[i];
203                             ty = y + dy[i];
204                             tx = XNORM(tx);
205                             ty = YNORM(ty);
206                             if (mapindex[tx][ty] == 0xFFFF) {
207                                 if (owned_and_navigable
208                                     (bigmap, tx, ty, terrain, own)
209                                     || (tx == ex && ty == ey && !restr2)) {
210                                     mapindex[tx][ty] =
211                                         ((i + 1) << 13) + routelen;
212                                     markedsectors++;
213                                 }
214                             }
215                             if (tx == ex && ty == ey) {
216                                 bpath[routelen] = 0;
217                                 while (routelen--) {
218                                     i = ((mapindex[tx][ty]) >> 13) - 1;
219                                     bpath[routelen] = dirchar[i];
220                                     tx = tx - dx[i];
221                                     ty = ty - dy[i];
222                                     tx = XNORM(tx);
223                                     ty = YNORM(ty);
224                                 }
225                                 return ((s_char *)bpath);
226                             }
227                         }
228                     }
229                 }
230             }
231         }
232         miny--;
233         maxy++;
234         minx -= 2;
235         maxx += 2;
236     } while (markedsectors);
237
238     bpath[0] = 0;
239     return ((s_char *)0);       /* no route possible    */
240 }
241
242 /* return TRUE if sector is passable */
243 static int
244 owned_and_navigable(s_char *map, int x, int y, s_char *terrain, int own)
245 {
246     s_char c;
247     s_char *t;
248     s_char mapspot;             /* What this spot on the bmap is */
249     int negate;
250     struct sctstr *sect;
251     int rel;
252
253     /* No terrain to check?  Everything is navigable! (this
254        probably means we are flying) */
255     if (!(*terrain))
256         return (1);
257
258     /* Are we checking this map? */
259     if (map) {
260         /* Do we know what this sector is?  If not, we assume it's ok,
261            since otherwise we'll never venture anywhere */
262         mapspot = map[sctoff(x, y)];
263         if (mapspot == ' ' || mapspot == 0)
264             return (1);
265
266         /* Now, is it marked with a 'x' or 'X'? If so, avoid it! */
267         if (mapspot == 'x' || mapspot == 'X')
268             return (0);
269     } else {
270         /* We don't know what it is since we have no map, so return ok! */
271         return (1);
272     }
273
274     /* Now, check this bmap entry to see if it is one of the
275        terrain types. */
276     t = terrain;
277     if (*t == '~') {
278         negate = 1;
279         t++;
280     } else
281         negate = 0;
282
283     while (*t) {
284         if (*t == mapspot)
285             break;
286         t++;
287     }
288     if (negate && *t) {
289         /* We found it, so we say it's bad since we are negating */
290         return (0);
291     } else if (!negate && !*t) {
292         /* We didn't find it, so we say it's bad since we aren't negating */
293         return (0);
294     }
295
296     /* According to our bmap, this sector is ok so far. */
297
298     /* Ok, we made it this far.  Now get the sector */
299     sect = getsectp(x, y);
300     c = dchr[sect->sct_type].d_mnem;
301     /* Ok, now, check the owner if needed */
302     if (own >= 0) {
303         if (sect->sct_own != own) {
304             rel = getrel(getnatp(sect->sct_own), own);
305             /* We can't sail through deity sectors, but we can sail
306                through any ocean */
307             if (rel < FRIENDLY && sect->sct_type != SCT_WATER)
308                 return (0);
309         }
310     }
311     /* Ok, now, check these two sector types */
312     /* check for bad harbors. */
313     if (c == 'h' && sect->sct_effic < 2)
314         return (0);
315     /* check for bad bridges  */
316     if (c == '=' && sect->sct_effic < 60)
317         return (0);
318     /* Woo-hoo, it's ok! */
319     return (1);
320 }