]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
navigate: Fix buffer overrun for impossibly long paths taken
[empserver] / src / lib / commands / navi.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  navi.c: Navigate ships and such
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995 (rewritten)
31  *     Ron Koenderink, 2006-2007
32  *     Markus Armbruster, 2006-2014
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "map.h"
39 #include "optlist.h"
40 #include "path.h"
41 #include "unit.h"
42
43 static void pr_leader_change(struct empobj *leader);
44 static struct empobj *get_leader(struct emp_qelem *list);
45 static void switch_leader(struct emp_qelem *list, int uid);
46
47 int
48 navi(void)
49 {
50     struct nstr_item ni_ship;
51     struct emp_qelem ship_list;
52     double minmob, maxmob;
53     int together;
54
55     if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
56         return RET_SYN;
57     shp_sel(&ni_ship, &ship_list);
58     shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
59     if (QEMPTY(&ship_list)) {
60         pr("No ships\n");
61         return RET_FAIL;
62     }
63
64     return do_unit_move(&ship_list, &together, &minmob, &maxmob);
65 }
66
67 int
68 do_unit_move(struct emp_qelem *ulist, int *together,
69              double *minmob, double *maxmob)
70 {
71     char *cp = NULL;
72     int leader_uid;
73     struct empobj *leader;
74     int dir;
75     int stopping = 0;
76     int skip = 0;
77     char buf[1024];
78     char prompt[128];
79     char bmap_flag;
80     int ac;
81     int type;
82
83     leader = get_leader(ulist);
84     leader_uid = leader->uid;
85     type = leader->ef_type;
86     pr("%s is %s\n",
87         type == EF_SHIP ? "Flagship" : "Leader",
88         unit_nameof(leader));
89
90     if (player->argp[2]) {
91         strcpy(buf, player->argp[2]);
92         cp = unit_path(*together, leader, buf, sizeof(buf));
93     }
94
95     while (!QEMPTY(ulist)) {
96         char dp[80];
97
98         if (cp == NULL || *cp == '\0' || stopping) {
99             stopping = 0;
100             if (type == EF_SHIP)
101                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
102             else
103                 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
104             if (QEMPTY(ulist)) {
105                 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
106                 return RET_OK;
107             }
108             leader = get_leader(ulist);
109             if (leader->uid != leader_uid) {
110                 leader_uid = leader->uid;
111                 pr_leader_change(leader);
112                 stopping = 1;
113                 continue;
114             }
115             if (!skip)
116                 nav_map(leader->x, leader->y,
117                         type == EF_SHIP
118                         ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
119             else
120                 skip = 0;
121             sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
122                     *minmob, xyas(leader->x, leader->y, player->cnum));
123             cp = getstring(prompt, buf);
124             /* Just in case any of our units were shelled while we were
125              * at the prompt, we call shp_nav() or lnd_mar() again.
126              */
127             if (type == EF_SHIP)
128                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
129             else
130                 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
131             if (QEMPTY(ulist)) {
132                 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
133                 return RET_OK;
134             }
135             leader = get_leader(ulist);
136             if (leader->uid != leader_uid) {
137                 leader_uid = leader->uid;
138                 pr_leader_change(leader);
139                 stopping = 1;
140                 continue;
141             }
142             if (cp)
143                 cp = unit_path(*together, leader, buf, sizeof(buf));
144         }
145         if (type == EF_SHIP) {
146             rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
147                         leader->tech, mchr[leader->type].m_vrnge);
148         }
149         if (cp == NULL || *cp == '\0')
150             cp = &dirch[DIR_STOP];
151         dir = chkdir(*cp, DIR_STOP, DIR_LAST);
152         if (dir >= 0) {
153             if (type == EF_SHIP)
154                 stopping |= shp_nav_one_sector(ulist, dir,
155                                                player->cnum, *together);
156             else
157                 stopping |=
158                     lnd_mar_one_sector(ulist, dir, player->cnum,
159                                        *together);
160             cp++;
161             continue;
162         }
163         ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
164         if (ac <= 0) {
165             player->argp[0] = "";
166             cp = NULL;
167         } else if (ac == 1) {
168             sprintf(dp, "%d", leader->uid);
169             player->argp[1] = dp;
170             cp++;
171         } else
172             cp = NULL;
173         bmap_flag = 0;
174         switch (*player->argp[0]) {
175         case 'B':
176             bmap_flag = 'b';
177             /*
178              * fall through
179              */
180         case 'M':
181             display_region_map(bmap_flag, type, leader->x, leader->y,
182                                player->argp[1], player->argp[2]);
183             skip = 1;
184             continue;
185         case 'f':
186             if (ac <= 1)
187                 switch_leader(ulist, -1);
188             else
189                 switch_leader(ulist, atoi(player->argp[1]));
190             leader = get_leader(ulist);
191             if (leader->uid != leader_uid) {
192                 leader_uid = leader->uid;
193                 pr_leader_change(leader);
194             }
195             continue;
196         case 'i':
197             unit_list(ulist);
198             continue;
199         case 'm':
200             if (type == EF_SHIP)
201                 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
202             else {
203                 lnd_sweep(ulist, 1, 1, player->cnum);
204                 stopping |= lnd_check_mines(ulist);
205             }
206             continue;
207         case 'r':
208             radar(leader->ef_type);
209             skip = 1;
210             player->btused++;
211             continue;
212         case 'l':
213             do_look(type);
214             player->btused++;
215             continue;
216         case 's':
217             if (leader->ef_type != EF_SHIP)
218                 break;
219             sona();
220             player->btused++;
221             skip = 1;
222             continue;
223         case 'd':
224             if (ac < 3) {
225                 player->argp[2] = ac < 2 ? "1" : player->argp[1];
226                 sprintf(dp, "%d", leader->uid);
227                 player->argp[1] = dp;
228             }
229             if (type == EF_SHIP)
230                 mine();
231             else
232                 landmine();
233             stopping = 1;
234             skip = 1;
235             player->btused++;
236             continue;
237         case 'v':
238             unit_view(ulist);
239             continue;
240         }
241         direrr("`%c' to stop", ", `%c' to view", NULL);
242         pr(", `i' to list %s, `f' to change %s,\n",
243             type == EF_SHIP ? "ships" : "units",
244             type == EF_SHIP ? "flagship" : "leader");
245         pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
246             type == EF_SHIP ? "`s' to sonar, " : "");
247         pr("`d' to drop mines, and `m' to minesweep\n");
248         stopping = 1;
249     }
250     return RET_OK;
251 }
252
253 int
254 nav_map(int x, int y, int show_designations)
255 {
256     char *ptr;
257     struct nstr_sect ns;
258     struct sctstr sect;
259     int i;
260     /* Note this is not re-entrant anyway, so we keep the buffers
261        around */
262     static char *wmapbuf = NULL;
263     static char **wmap = NULL;
264     int changed = 0;
265
266     if (!wmapbuf)
267         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
268     if (!wmap) {
269         wmap = malloc(WORLD_Y * sizeof(*wmap));
270         if (wmap && wmapbuf) {
271             for (i = 0; i < WORLD_Y; i++)
272                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
273         } else if (wmap) {
274             free(wmap);
275             wmap = NULL;
276         }
277     }
278     if (!wmapbuf || !wmap) {
279         pr("Memory error, tell the deity.\n");
280         logerror("malloc failed in navi\n");
281         return RET_FAIL;
282     }
283     snxtsct_dist(&ns, x, y, 1);
284     blankfill(wmapbuf, &ns.range, 1);
285     while (nxtsct(&ns, &sect)) {
286         ptr = &wmap[ns.dy][ns.dx];
287         *ptr = dchr[sect.sct_type].d_mnem;
288         if (!show_designations &&
289             sect.sct_own != player->cnum &&
290             sect.sct_type != SCT_WATER &&
291             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
292             *ptr = '?';
293         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
294         /*
295          * We do it this way so that 'x' and 'X'
296          * bdesignations will show up. This can
297          * be used to mark mined sectors. So, the
298          * player will see the current des, UNLESS
299          * they've marked the sector 'x' or 'X',
300          * in which case they'll see that.
301          * --ts
302          */
303         *ptr = player->bmap[sect.sct_uid];
304     }
305     if (changed)
306         writemap(player->cnum);
307     for (i = 0; i < ns.range.height; i++)
308         pr("%s\n", wmap[i]);
309     return RET_OK;
310 }
311
312 static void
313 pr_leader_change(struct empobj *leader)
314 {
315     pr("Changing %s to %s\n",
316         leader->ef_type == EF_SHIP ? "flagship" : "leader",
317         unit_nameof(leader));
318 }
319
320 static struct empobj *
321 get_leader(struct emp_qelem *list)
322 {
323     return &((struct ulist *)(list->q_back))->unit.gen;
324 }
325
326 static void
327 switch_leader(struct emp_qelem *list, int uid)
328 {
329     struct emp_qelem *qp, *save;
330     struct ulist *ulp;
331
332     if (QEMPTY(list))
333         return;
334
335     save = qp = list->q_back;
336     do {
337         emp_remque(qp);
338         emp_insque(qp, list);
339         qp = list->q_back;
340         ulp = (struct ulist *)qp;
341         if (ulp->unit.gen.uid == uid || uid == -1)
342             break;
343     } while (list->q_back != save);
344 }