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
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.
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.
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
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.
28 * bestpath.c: Find the best path between sectors
30 * Known contributors to this file:
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2006
36 * IMPORTANT: These routines are very selectively used in the server.
38 * "bestownedpath" is only used to determine paths for ships and planes.
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
56 static int owned_and_navigable(char *, int, int, int);
59 #define valid(x,y) (((x^y)&1)==0)
61 static char *dirchar = "juygbn";
62 int dx[6] = { 2, 1, -1, -2, -1, 1 };
63 int dy[6] = { 0, -1, -1, 0, 1, 1 };
66 * Ok, note that here we malloc some buffers. BUT, we never
67 * free them. Why, you may ask? Because we want to allocate
68 * them based on world size which is now (or soon to be) dynamic,
69 * but we don't want to allocate each and every time, since that
70 * would be slow. And, since world size only changes at init
71 * time, we can do this safely.
73 static unsigned int *mapbuf;
74 static unsigned int **mapindex;
77 * Find passable path from X, Y to EX, EY for nation OWN.
78 * BPATH is a buffer capable of holding at least MAXROUTE characters.
79 * If BIGMAP is null, all sectors are passable (useful for flying).
80 * Else it is taken to be a bmap.
81 * Sectors owned by or allied to OWN are checked according to the
82 * usual rules, and the result is correct.
83 * Other sectors are assumed to be passable when BIGMAP shows '.' or
85 * Return path or a null pointer.
88 bestownedpath(char *bpath, char *bigmap,
89 int x, int y, int ex, int ey, int own)
91 int i, j, tx, ty, markedsectors;
92 int minx, maxx, miny, maxy, scanx, scany;
93 unsigned int routelen;
96 mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(unsigned int));
100 mapindex = malloc(WORLD_X * sizeof(unsigned int *));
102 /* Setup the map pointers */
103 for (i = 0; i < WORLD_X; i++)
104 mapindex[i] = &mapbuf[WORLD_Y * i];
115 if (x == ex && y == ey)
118 if (!valid(x, y) || !valid(ex, ey))
121 for (i = 0; i < WORLD_X; i++)
122 for (j = 0; j < WORLD_Y; j++)
123 mapindex[i][j] = 0xFFFF; /* clear the workspace */
125 routelen = 0; /* path length is now 0 */
126 mapindex[x][y] = 0; /* mark starting spot */
127 markedsectors = 1; /* source sector marked */
128 minx = x - 2; /* set X scan bounds */
130 miny = y - 1; /* set Y scan bounds */
134 if (++routelen == MAXROUTE - 1)
137 for (scanx = minx; scanx <= maxx; scanx++) {
139 for (scany = miny; scany <= maxy; scany++) {
143 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
144 for (i = 0; i < 6; i++) {
149 if (mapindex[tx][ty] == 0xFFFF) {
150 if (owned_and_navigable(bigmap, tx, ty, own)) {
152 ((i + 1) << 13) + routelen;
156 if (tx == ex && ty == ey) {
157 bpath[routelen] = 'h';
158 bpath[routelen + 1] = 0;
160 i = (mapindex[tx][ty] >> 13) - 1;
161 bpath[routelen] = dirchar[i];
177 } while (markedsectors);
179 return NULL; /* no route possible */
183 * Return non-zero if sector X, Y is passable.
184 * If BIGMAP is null, all sectors are passable (useful for flying).
185 * Else it is taken to be a bmap.
186 * Sectors owned by or allied to OWN are checked according to the
187 * usual rules, and the result is correct.
188 * Other sectors are assumed to be passable when BIGMAP shows '.' or
192 owned_and_navigable(char *bigmap, int x, int y, int own)
200 /* Owned or allied sector? Check the real sector. */
201 getsect(x, y, §);
202 if (sect.sct_own == own
203 || (sect.sct_own && getrel(getnatp(sect.sct_own), own) == ALLIED)) {
204 /* FIXME duplicates shp_check_nav() logic */
205 switch (dchr[sect.sct_type].d_nav) {
209 /* FIXME return 1 when all ships have M_CANAL */
212 return sect.sct_effic >= 2;
214 return sect.sct_effic >= 60;
220 /* Can only check bigmap */
221 mapspot = bigmap[sctoff(x, y)];
222 return mapspot == '.' || mapspot == ' ' || mapspot == 0;