]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
Use relations_with() in owned_and_navigable()
[empserver] / src / lib / common / bestpath.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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 "file.h"
49 #include "misc.h"
50 #include "nat.h"
51 #include "optlist.h"
52 #include "path.h"
53 #include "prototypes.h"
54 #include "sect.h"
55 #include "xy.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 a path if found, else a null pointer.
83  * Wart: the path isn't terminated with 'h', except when if X,Y equals
84  * EX,EY.
85  */
86 char *
87 bestownedpath(char *bpath, char *bigmap,
88               int x, int y, int ex, int ey, int own)
89 {
90     int i, j, tx, ty, markedsectors;
91     int minx, maxx, miny, maxy, scanx, scany;
92     unsigned routelen;
93
94     if (!mapbuf)
95         mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(*mapbuf));
96     if (!mapbuf)
97         return NULL;
98     if (!mapindex) {
99         mapindex = malloc(WORLD_X * sizeof(*mapindex));
100         if (mapindex) {
101             /* Setup the map pointers */
102             for (i = 0; i < WORLD_X; i++)
103                 mapindex[i] = &mapbuf[WORLD_Y * i];
104         }
105     }
106     if (!mapindex)
107         return NULL;
108
109     x = XNORM(x);
110     y = YNORM(y);
111     ex = XNORM(ex);
112     ey = YNORM(ey);
113
114     if (x == ex && y == ey)
115         return "h";
116
117     if (!valid(x, y) || !valid(ex, ey))
118         return NULL;
119     if (!owned_and_navigable(bigmap, ex, ey, own))
120         return NULL;
121
122     for (i = 0; i < WORLD_X; i++)
123         for (j = 0; j < WORLD_Y; j++)
124             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
125
126     routelen = 0;               /* path length is now 0 */
127     mapindex[x][y] = 0;         /* mark starting spot   */
128     minx = x - 2;               /* set X scan bounds    */
129     maxx = x + 2;
130     miny = y - 1;               /* set Y scan bounds    */
131     maxy = y + 1;
132
133     do {
134         if (++routelen == MAXROUTE)
135             return NULL;
136         markedsectors = 0;
137         for (scanx = minx; scanx <= maxx; scanx++) {
138             x = XNORM(scanx);
139             for (scany = miny; scany <= maxy; scany++) {
140                 y = YNORM(scany);
141                 if (!valid(x, y))
142                     continue;
143                 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
144                     for (i = DIR_FIRST; i <= DIR_LAST; i++) {
145                         tx = x + diroff[i][0];
146                         ty = y + diroff[i][1];
147                         tx = XNORM(tx);
148                         ty = YNORM(ty);
149                         if (mapindex[tx][ty] == 0xFFFF) {
150                             if (owned_and_navigable(bigmap, tx, ty, own)) {
151                                 if (CANT_HAPPEN(i < DIR_FIRST || i > DIR_LAST))
152                                     i = DIR_STOP;
153                                 mapindex[tx][ty] =
154                                     ((i - DIR_FIRST + 1) << 13) + routelen;
155                                 markedsectors++;
156                             }
157                         }
158                         if (tx == ex && ty == ey) {
159                             bpath[routelen] = 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 && relations_with(sect.sct_own, own) == ALLIED) {
205         /* FIXME duplicates shp_check_nav() logic */
206         switch (dchr[sect.sct_type].d_nav) {
207         case NAVOK:
208             return 1;
209         case NAV_CANAL:
210             /* FIXME return 1 when all ships have M_CANAL */
211             return 0;
212         case NAV_02:
213             return sect.sct_effic >= 2;
214         case NAV_60:
215             return sect.sct_effic >= 60;
216         default:
217             return 0;
218         }
219     }
220
221     /* Can only check bigmap */
222     mapspot = bigmap[sect.sct_uid];
223     return mapspot == '.' || mapspot == ' ' || mapspot == 0;
224 }