]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
(getpath): Fix buffer overflow. Remote hole in commands bomb, drop,
[empserver] / src / lib / subs / paths.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  path.c: Routines associated with paths, directions, etc.
29  * 
30  *  Known contributors to this file:
31  *   
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "path.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "file.h"
41 #include "prototypes.h"
42
43 int
44 chkdir(s_char dir_char, int min_dir, int max_dir)
45 {
46     register int i;
47
48     for (i = min_dir; i <= max_dir; i++)
49         if (dir_char == dirch[i])
50             return i;
51     return -1;
52 }
53
54 void
55 direrr(s_char *stop_msg, s_char *view_msg, s_char *map_msg)
56 {
57     pr("Legal directions are:\n");
58     pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
59     pr("%c   %c\n", dirch[DIR_L], dirch[DIR_R]);
60     pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
61     if (stop_msg != 0)
62         pr(stop_msg, dirch[DIR_STOP]);
63     if (view_msg != 0)
64         pr(view_msg, dirch[DIR_VIEW]);
65     if (map_msg != 0)
66         pr(map_msg, dirch[DIR_MAP]);
67 }
68
69 /*
70  * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
71  * DIR must be a valid direction.
72  */
73 int
74 diridx(char dir)
75 {
76     unsigned i = dir - 'a';
77
78     if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
79                     || dirindex[i] < 0))
80         return DIR_STOP;
81     return dirindex[i];
82 }
83
84 /*
85  * return pointer to path; prompt user until a stop char
86  * or a bomb char have been entered.  A "bomb" char in
87  * this context is actually "execute" for the partial
88  * move commands, and isn't valid for those commands
89  * which do not accept partial moves.
90  */
91 char *
92 getpath(char *buf, char *arg, coord x, coord y, int onlyown,
93         int showdes, int destinations)
94 {
95     char buf2[1024];
96     char *p = buf;
97     char *bp;
98     char prompt[128];
99     coord dx, dy;
100     struct sctstr sect, dsect;
101     coord nx, ny;
102     int dir;
103     double mv_cost;
104
105     if (arg) {
106         strncpy(buf, arg, MAX_PATH_LEN - 1);
107         buf[MAX_PATH_LEN - 1] = 0;
108     } else {
109         *p = 0;
110     }
111
112     getsect(x, y, &sect);
113     nx = x;
114     ny = y;
115
116   more:
117     while (*p) {
118         if (sarg_xy(p, &dx, &dy)) {
119             bp = NULL;
120             if (destinations == P_NONE) {
121                 pr("Destination sectors not allowed here!\n");
122             }
123             if (getsect(dx, dy, &dsect)) {
124                 if (destinations == P_WALKING) {
125                     bp = BestLandPath(buf2, &sect, &dsect,
126                                       &mv_cost, MOB_ROAD);
127                 } else if (destinations == P_FLYING) {
128                     bp = BestAirPath(buf2, nx, ny, dx, dy);
129                 }
130             } else {
131                 pr("Invalid destination sector!\n");
132             }
133             if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
134                 strcpy(p, bp);
135                 pr("Using best path  '%s'\n", p);
136                 pr("Using total path '%s'\n", buf);
137                 return buf;
138             } else {
139                 pr("Can't get to %s from here!\n",
140                    xyas(dx, dy, player->cnum));
141             }
142             break;
143         }
144         dir = chkdir(*p, DIR_STOP, DIR_LAST);
145         if (dir < 0) {
146             pr("\"%c\" is not legal...", *p);
147             direrr("'%c' to stop\n", NULL, NULL);
148             break;
149         }
150         nx = x + diroff[dir][0];
151         ny = y + diroff[dir][1];
152         getsect(nx, ny, &sect);
153         if (onlyown && sect.sct_own != player->cnum) {
154             pr("You don't own %s; you can't go there!\n",
155                xyas(nx, ny, player->cnum));
156             break;
157         }
158         if (dir == DIR_STOP || dir == DIR_MAP) {
159             p[1] = 0;
160             return buf;
161         }
162         ++p;
163         x = nx;
164         y = ny;
165     }
166     fly_map(x, y);
167     if (showdes) {
168         getsect(x, y, &sect);
169         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
170                 xyas(x, y, player->cnum));
171     } else {
172         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
173                 xyas(x, y, player->cnum));
174     }
175     bp = getstring(prompt, buf2);
176     if (bp && p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
177         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
178         pr("Aborting...\n");
179         bp = NULL;
180     }
181     if (!bp)
182         return NULL;
183     strcpy(p, bp);
184     if (*bp)
185         goto more;
186     return buf;
187 }
188
189 /*
190  * fly move cost
191  */
192 /* ARGSUSED */
193 double
194 fcost(struct sctstr *sp, natid own)
195 {
196     return 1.0;
197 }
198
199 /*
200  * nav move cost
201  */
202 /* ARGSUSED */
203 double
204 ncost(struct sctstr *sp, natid own)
205 {
206     return 1.0;
207 }
208
209 /*
210  * return end x,y of path, and the base
211  * movement cost it takes to get there.
212  */
213 double
214 pathtoxy(s_char *path, coord *xp, coord *yp,
215          double (*cost) (struct sctstr *, natid))
216 {
217     struct sctstr s;
218     s_char *pp;
219     coord x;
220     coord y;
221     int val;
222     double m;
223
224     x = *xp;
225     y = *yp;
226     m = 0.0;
227     for (pp = path; *pp; pp++) {
228         if ((val = diridx(*pp)) == DIR_STOP)
229             break;
230         x += diroff[val][0];
231         y += diroff[val][1];
232         if (!getsect(x, y, &s))
233             return -1.0;
234         m += cost(&s, s.sct_own);
235     }
236     *xp = xnorm(x);
237     *yp = ynorm(y);
238     return m;
239 }
240
241 void
242 pathrange(coord cx, coord cy, s_char *pp, int border, struct range *range)
243 {
244     int dir;
245
246     range->lx = cx;
247     range->hx = cx;
248     range->ly = cy;
249     range->hy = cy;
250     range->width = 0;
251     range->height = 0;
252     for (; *pp; pp++) {
253         dir = diridx(*pp);
254         if (dir == DIR_STOP)
255             break;
256         cx += diroff[dir][0];
257         cy += diroff[dir][1];
258         if (cx < range->lx)
259             range->lx = cx;
260         if (cx > range->hx)
261             range->hx = cx;
262         if (cy < range->ly)
263             range->ly = cy;
264         if (cy > range->hy)
265             range->hy = cy;
266     }
267     range->lx = xnorm(range->lx - border * 2);
268     range->ly = ynorm(range->ly - border);
269     range->hx = xnorm(range->hx + border * 2 + 1);
270     range->hy = ynorm(range->hy + border + 1);
271     xysize_range(range);
272 }