]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
(mapindex, mapbuf): Change element type to unsigned short, because 16
[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 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  *  bestpath.c: Find the best path between sectors
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998-2000
32  *     Markus Armbruster, 2006
33  */
34
35 /* 
36  * IMPORTANT: These routines are very selectively used in the server.
37  *
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 "path.h"
54 #include "common.h"
55 #include "optlist.h"
56
57 static int owned_and_navigable(char *, int, int, int);
58
59 #define MAXROUTE        100
60 #define valid(x,y)      ((((x) ^ (y)) & 1) == 0)
61
62 /*
63  * Ok, note that here we malloc some buffers.  BUT, we never
64  * free them.  Why, you may ask?  Because we want to allocate
65  * them based on world size which is now (or soon to be) dynamic,
66  * but we don't want to allocate each and every time, since that
67  * would be slow.  And, since world size only changes at init
68  * time, we can do this safely.
69  */
70 static unsigned short *mapbuf;
71 static unsigned short **mapindex;
72
73 /*
74  * Find passable path from X, Y to EX, EY for nation OWN.
75  * BPATH is a buffer capable of holding at least MAXROUTE characters.
76  * If BIGMAP is null, all sectors are passable (useful for flying).
77  * Else it is taken to be a bmap.
78  * Sectors owned by or allied to OWN are then passable according to
79  * the usual rules.
80  * Other sectors are assumed to be passable when BIGMAP shows '.' or
81  * nothing.
82  * Return path or a null pointer.
83  */
84 char *
85 bestownedpath(char *bpath, char *bigmap,
86               int x, int y, int ex, int ey, int own)
87 {
88     int i, j, tx, ty, markedsectors;
89     int minx, maxx, miny, maxy, scanx, scany;
90     unsigned routelen;
91
92     if (!mapbuf)
93         mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(*mapbuf));
94     if (!mapbuf)
95         return NULL;
96     if (!mapindex) {
97         mapindex = malloc(WORLD_X * sizeof(*mapindex));
98         if (mapindex) {
99             /* Setup the map pointers */
100             for (i = 0; i < WORLD_X; i++)
101                 mapindex[i] = &mapbuf[WORLD_Y * i];
102         }
103     }
104     if (!mapindex)
105         return NULL;
106
107     x = XNORM(x);
108     y = YNORM(y);
109     ex = XNORM(ex);
110     ey = YNORM(ey);
111
112     if (x == ex && y == ey)
113         return "h";
114
115     if (!valid(x, y) || !valid(ex, ey))
116         return NULL;
117
118     for (i = 0; i < WORLD_X; i++)
119         for (j = 0; j < WORLD_Y; j++)
120             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
121
122     routelen = 0;               /* path length is now 0 */
123     mapindex[x][y] = 0;         /* mark starting spot   */
124     markedsectors = 1;          /* source sector marked */
125     minx = x - 2;               /* set X scan bounds    */
126     maxx = x + 2;
127     miny = y - 1;               /* set Y scan bounds    */
128     maxy = y + 1;
129
130     do {
131         if (++routelen == MAXROUTE - 1)
132             return NULL;
133         markedsectors = 0;
134         for (scanx = minx; scanx <= maxx; scanx++) {
135             x = XNORM(scanx);
136             for (scany = miny; scany <= maxy; scany++) {
137                 y = YNORM(scany);
138                 if (!valid(x, y))
139                     continue;
140                 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
141                     for (i = DIR_FIRST; i < DIR_LAST; i++) {
142                         tx = x + diroff[i][0];
143                         ty = y + diroff[i][1];
144                         tx = XNORM(tx);
145                         ty = YNORM(ty);
146                         if (mapindex[tx][ty] == 0xFFFF) {
147                             if (owned_and_navigable(bigmap, tx, ty, own)) {
148                                 mapindex[tx][ty] =
149                                     ((i - DIR_FIRST + 1) << 13) + routelen;
150                                 markedsectors++;
151                             }
152                         }
153                         if (tx == ex && ty == ey) {
154                             bpath[routelen] = 'h';
155                             bpath[routelen + 1] = 0;
156                             while (routelen--) {
157                                 i = (mapindex[tx][ty] >> 13)
158                                     - 1 + DIR_FIRST;
159                                 bpath[routelen] = dirch[i];
160                                 tx = tx - diroff[i][0];
161                                 ty = ty - diroff[i][1];
162                                 tx = XNORM(tx);
163                                 ty = YNORM(ty);
164                             }
165                             return bpath;
166                         }
167                     }
168                 }
169             }
170         }
171         miny--;
172         maxy++;
173         minx -= 2;
174         maxx += 2;
175     } while (markedsectors);
176
177     return NULL;                /* no route possible    */
178 }
179
180 /*
181  * Return non-zero if sector X, Y is passable.
182  * If BIGMAP is null, all sectors are passable (useful for flying).
183  * Else it is taken to be a bmap.
184  * Sectors owned by or allied to OWN are checked according to the
185  * usual rules, and the result is correct.
186  * Other sectors are assumed to be passable when BIGMAP shows '.' or
187  * nothing.
188  */
189 static int
190 owned_and_navigable(char *bigmap, int x, int y, int own)
191 {
192     char mapspot;
193     struct sctstr sect;
194
195     if (!bigmap)
196         return 1;
197
198     /* Owned or allied sector?  Check the real sector.  */
199     getsect(x, y, &sect);
200     if (sect.sct_own == own
201         || (sect.sct_own && getrel(getnatp(sect.sct_own), own) == ALLIED)) {
202         /* FIXME duplicates shp_check_nav() logic */
203         switch (dchr[sect.sct_type].d_nav) {
204         case NAVOK:
205             return 1;
206         case NAV_CANAL:
207             /* FIXME return 1 when all ships have M_CANAL */
208             return 0;
209         case NAV_02:
210             return sect.sct_effic >= 2;
211         case NAV_60:
212             return sect.sct_effic >= 60;
213         default:
214             return 0;
215         }
216     }
217
218     /* Can only check bigmap */
219     mapspot = bigmap[sctoff(x, y)];
220     return mapspot == '.' || mapspot == ' ' || mapspot == 0;
221 }