]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
(bestownedpath): Fix for impassable end sector. Broken in rev. 1.23.
[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     if (!owned_and_navigable(bigmap, ex, ey, own))
118         return NULL;
119
120     for (i = 0; i < WORLD_X; i++)
121         for (j = 0; j < WORLD_Y; j++)
122             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
123
124     routelen = 0;               /* path length is now 0 */
125     mapindex[x][y] = 0;         /* mark starting spot   */
126     markedsectors = 1;          /* source sector marked */
127     minx = x - 2;               /* set X scan bounds    */
128     maxx = x + 2;
129     miny = y - 1;               /* set Y scan bounds    */
130     maxy = y + 1;
131
132     do {
133         if (++routelen == MAXROUTE - 1)
134             return NULL;
135         markedsectors = 0;
136         for (scanx = minx; scanx <= maxx; scanx++) {
137             x = XNORM(scanx);
138             for (scany = miny; scany <= maxy; scany++) {
139                 y = YNORM(scany);
140                 if (!valid(x, y))
141                     continue;
142                 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
143                     for (i = DIR_FIRST; i < DIR_LAST; i++) {
144                         tx = x + diroff[i][0];
145                         ty = y + diroff[i][1];
146                         tx = XNORM(tx);
147                         ty = YNORM(ty);
148                         if (mapindex[tx][ty] == 0xFFFF) {
149                             if (owned_and_navigable(bigmap, tx, ty, own)) {
150                                 if (CANT_HAPPEN(i < DIR_FIRST || i > DIR_LAST))
151                                     i = DIR_STOP;
152                                 mapindex[tx][ty] =
153                                     ((i - DIR_FIRST + 1) << 13) + routelen;
154                                 markedsectors++;
155                             }
156                         }
157                         if (tx == ex && ty == ey) {
158                             bpath[routelen] = 'h';
159                             bpath[routelen + 1] = 0;
160                             while (routelen--) {
161                                 i = (mapindex[tx][ty] >> 13)
162                                     - 1 + DIR_FIRST;
163                                 bpath[routelen] = dirch[i];
164                                 tx = tx - diroff[i][0];
165                                 ty = ty - diroff[i][1];
166                                 tx = XNORM(tx);
167                                 ty = YNORM(ty);
168                             }
169                             return bpath;
170                         }
171                     }
172                 }
173             }
174         }
175         miny--;
176         maxy++;
177         minx -= 2;
178         maxx += 2;
179     } while (markedsectors);
180
181     return NULL;                /* no route possible    */
182 }
183
184 /*
185  * Return non-zero if sector X, Y is passable.
186  * If BIGMAP is null, all sectors are passable (useful for flying).
187  * Else it is taken to be a bmap.
188  * Sectors owned by or allied to OWN are checked according to the
189  * usual rules, and the result is correct.
190  * Other sectors are assumed to be passable when BIGMAP shows '.' or
191  * nothing.
192  */
193 static int
194 owned_and_navigable(char *bigmap, int x, int y, int own)
195 {
196     char mapspot;
197     struct sctstr sect;
198
199     if (!bigmap)
200         return 1;
201
202     /* Owned or allied sector?  Check the real sector.  */
203     getsect(x, y, &sect);
204     if (sect.sct_own == own
205         || (sect.sct_own && getrel(getnatp(sect.sct_own), own) == ALLIED)) {
206         /* FIXME duplicates shp_check_nav() logic */
207         switch (dchr[sect.sct_type].d_nav) {
208         case NAVOK:
209             return 1;
210         case NAV_CANAL:
211             /* FIXME return 1 when all ships have M_CANAL */
212             return 0;
213         case NAV_02:
214             return sect.sct_effic >= 2;
215         case NAV_60:
216             return sect.sct_effic >= 60;
217         default:
218             return 0;
219         }
220     }
221
222     /* Can only check bigmap */
223     mapspot = bigmap[sctoff(x, y)];
224     return mapspot == '.' || mapspot == ' ' || mapspot == 0;
225 }