]> git.pond.sub.org Git - empserver/blob - src/lib/common/bestpath.c
Update known contributors comment.
[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 "common.h"
54 #include "optlist.h"
55
56 static int owned_and_navigable(char *, int, int, int);
57
58 #define MAXROUTE        100
59 #define valid(x,y)      (((x^y)&1)==0)
60
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 };
64
65 /*
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.
72  */
73 static unsigned int *mapbuf;
74 static unsigned int **mapindex;
75
76 /*
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
84  * nothing.
85  * Return path or a null pointer.
86  */
87 char *
88 bestownedpath(char *bpath, char *bigmap,
89               int x, int y, int ex, int ey, int own)
90 {
91     int i, j, tx, ty, markedsectors;
92     int minx, maxx, miny, maxy, scanx, scany;
93     unsigned int routelen;
94
95     if (!mapbuf)
96         mapbuf = malloc(WORLD_X * WORLD_Y * sizeof(unsigned int));
97     if (!mapbuf)
98         return NULL;
99     if (!mapindex) {
100         mapindex = malloc(WORLD_X * sizeof(unsigned int *));
101         if (mapindex) {
102             /* Setup the map pointers */
103             for (i = 0; i < WORLD_X; i++)
104                 mapindex[i] = &mapbuf[WORLD_Y * i];
105         }
106     }
107     if (!mapindex)
108         return NULL;
109
110     x = XNORM(x);
111     y = YNORM(y);
112     ex = XNORM(ex);
113     ey = YNORM(ey);
114
115     if (x == ex && y == ey)
116         return "h";
117
118     if (!valid(x, y) || !valid(ex, ey))
119         return NULL;
120
121     for (i = 0; i < WORLD_X; i++)
122         for (j = 0; j < WORLD_Y; j++)
123             mapindex[i][j] = 0xFFFF;    /* clear the workspace  */
124
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    */
129     maxx = x + 2;
130     miny = y - 1;               /* set Y scan bounds    */
131     maxy = y + 1;
132
133     do {
134         if (++routelen == MAXROUTE - 1)
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 = 0; i < 6; i++) {
145                         tx = x + dx[i];
146                         ty = y + dy[i];
147                         tx = XNORM(tx);
148                         ty = YNORM(ty);
149                         if (mapindex[tx][ty] == 0xFFFF) {
150                             if (owned_and_navigable(bigmap, tx, ty, own)) {
151                                 mapindex[tx][ty] =
152                                     ((i + 1) << 13) + routelen;
153                                 markedsectors++;
154                             }
155                         }
156                         if (tx == ex && ty == ey) {
157                             bpath[routelen] = 'h';
158                             bpath[routelen + 1] = 0;
159                             while (routelen--) {
160                                 i = (mapindex[tx][ty] >> 13) - 1;
161                                 bpath[routelen] = dirchar[i];
162                                 tx = tx - dx[i];
163                                 ty = ty - dy[i];
164                                 tx = XNORM(tx);
165                                 ty = YNORM(ty);
166                             }
167                             return bpath;
168                         }
169                     }
170                 }
171             }
172         }
173         miny--;
174         maxy++;
175         minx -= 2;
176         maxx += 2;
177     } while (markedsectors);
178
179     return NULL;                /* no route possible    */
180 }
181
182 /*
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
189  * nothing.
190  */
191 static int
192 owned_and_navigable(char *bigmap, int x, int y, int own)
193 {
194     char mapspot;
195     struct sctstr sect;
196
197     if (!bigmap)
198         return 1;
199
200     /* Owned or allied sector?  Check the real sector.  */
201     getsect(x, y, &sect);
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) {
206         case NAVOK:
207             return 1;
208         case NAV_CANAL:
209             /* FIXME return 1 when all ships have M_CANAL */
210             return 0;
211         case NAV_02:
212             return sect.sct_effic >= 2;
213         case NAV_60:
214             return sect.sct_effic >= 60;
215         default:
216             return 0;
217         }
218     }
219
220     /* Can only check bigmap */
221     mapspot = bigmap[sctoff(x, y)];
222     return mapspot == '.' || mapspot == ' ' || mapspot == 0;
223 }