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
35 * IMPORTANT: These routines are very selectively used in the server.
37 * "bestownedpath" is only used to determine paths for ships and planes.
39 * Callers should not be calling these directly anymore. They should use
40 * the "BestShipPath", "BestAirPath", "BestLandPath" and "BestDistPath"
41 * functions. Note that those last two use the A* algorithms to find
55 static int owned_and_navigable(char *, int, int, int);
58 #define valid(x,y) (((x^y)&1)==0)
60 static char *dirchar = "juygbn";
61 int dx[6] = { 2, 1, -1, -2, -1, 1 };
62 int dy[6] = { 0, -1, -1, 0, 1, 1 };
65 * Ok, note that here we malloc some buffers. BUT, we never
66 * free them. Why, you may ask? Because we want to allocate
67 * them based on world size which is now (or soon to be) dynamic,
68 * but we don't want to allocate each and every time, since that
69 * would be slow. And, since world size only changes at init
70 * time, we can do this safely.
72 static unsigned int *mapbuf;
73 static unsigned int **mapindex;
76 * Find passable path from X, Y to EX, EY for nation OWN.
77 * BPATH is a buffer capable of holding at least MAXROUTE characters.
78 * If BIGMAP is null, all sectors are passable (useful for flying).
79 * Else it is taken to be a bmap.
80 * Sectors owned by or allied to OWN are checked according to the
81 * usual rules, and the result is correct.
82 * Other sectors are assumed to be passable when BIGMAP shows '.' or
84 * Return path or a null pointer.
87 bestownedpath(char *bpath, char *bigmap,
88 int x, int y, int ex, int ey, int own)
90 int i, j, tx, ty, markedsectors;
91 int minx, maxx, miny, maxy, scanx, scany;
92 unsigned int routelen;
95 mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(unsigned int));
99 mapindex = malloc(WORLD_X * sizeof(unsigned int *));
101 /* Setup the map pointers */
102 for (i = 0; i < WORLD_X; i++)
103 mapindex[i] = &mapbuf[WORLD_Y * i];
114 if (x == ex && y == ey)
117 if (!valid(x, y) || !valid(ex, ey))
120 for (i = 0; i < WORLD_X; i++)
121 for (j = 0; j < WORLD_Y; j++)
122 mapindex[i][j] = 0xFFFF; /* clear the workspace */
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 */
129 miny = y - 1; /* set Y scan bounds */
133 if (++routelen == MAXROUTE - 1)
136 for (scanx = minx; scanx <= maxx; scanx++) {
138 for (scany = miny; scany <= maxy; scany++) {
142 if (((mapindex[x][y] & 0x1FFF) == routelen - 1)) {
143 for (i = 0; i < 6; i++) {
148 if (mapindex[tx][ty] == 0xFFFF) {
149 if (owned_and_navigable(bigmap, tx, ty, own)) {
151 ((i + 1) << 13) + routelen;
155 if (tx == ex && ty == ey) {
156 bpath[routelen] = 'h';
157 bpath[routelen + 1] = 0;
159 i = (mapindex[tx][ty] >> 13) - 1;
160 bpath[routelen] = dirchar[i];
176 } while (markedsectors);
178 return NULL; /* no route possible */
182 * Return non-zero if sector X, Y is passable.
183 * If BIGMAP is null, all sectors are passable (useful for flying).
184 * Else it is taken to be a bmap.
185 * Sectors owned by or allied to OWN are checked according to the
186 * usual rules, and the result is correct.
187 * Other sectors are assumed to be passable when BIGMAP shows '.' or
191 owned_and_navigable(char *bigmap, int x, int y, int own)
199 /* Owned or allied sector? Check the real sector. */
200 getsect(x, y, §);
201 if (sect.sct_own == own
202 || (sect.sct_own && getrel(getnatp(sect.sct_own), own) == ALLIED)) {
203 /* FIXME duplicates shp_check_nav() logic */
204 switch (dchr[sect.sct_type].d_nav) {
208 /* FIXME return 1 when all ships have M_CANAL */
211 return sect.sct_effic >= 2;
213 return sect.sct_effic >= 60;
219 /* Can only check bigmap */
220 mapspot = bigmap[sctoff(x, y)];
221 return mapspot == '.' || mapspot == ' ' || mapspot == 0;