]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
march: Require all land units to start in the same sector
[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     int moved = 0;
78     char buf[1024];
79     char prompt[128];
80     char bmap_flag;
81     int ac;
82     int type;
83
84     leader = get_leader(ulist);
85     leader_uid = leader->uid;
86     type = leader->ef_type;
87     pr("%s is %s\n",
88         type == EF_SHIP ? "Flagship" : "Leader",
89         unit_nameof(leader));
90
91     if (player->argp[2]) {
92         strcpy(buf, player->argp[2]);
93         cp = unit_path(*together, leader, buf, sizeof(buf));
94     }
95
96     while (!QEMPTY(ulist)) {
97         char dp[80];
98
99         if (cp == NULL || *cp == '\0' || stopping) {
100             stopping = 0;
101             if (type == EF_SHIP)
102                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
103             else
104                 lnd_mar(ulist, minmob, maxmob, player->cnum);
105             if (QEMPTY(ulist)) {
106                 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
107                 return RET_OK;
108             }
109             leader = get_leader(ulist);
110             if (leader->uid != leader_uid) {
111                 leader_uid = leader->uid;
112                 pr_leader_change(leader);
113                 stopping = 1;
114                 continue;
115             }
116             if (!skip)
117                 nav_map(leader->x, leader->y,
118                         type == EF_SHIP
119                         ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
120             else
121                 skip = 0;
122             sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
123                     *minmob, xyas(leader->x, leader->y, player->cnum));
124             cp = getstring(prompt, buf);
125             /* Just in case any of our units were shelled while we were
126              * at the prompt, we call shp_nav() or lnd_mar() again.
127              */
128             if (type == EF_SHIP)
129                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
130             else
131                 lnd_mar(ulist, minmob, maxmob, player->cnum);
132             if (QEMPTY(ulist)) {
133                 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
134                 return RET_OK;
135             }
136             leader = get_leader(ulist);
137             if (leader->uid != leader_uid) {
138                 leader_uid = leader->uid;
139                 pr_leader_change(leader);
140                 stopping = 1;
141                 continue;
142             }
143             if (cp)
144                 cp = unit_path(*together, leader, buf, sizeof(buf));
145         }
146         if (type == EF_SHIP) {
147             rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
148                         leader->tech, mchr[leader->type].m_vrnge);
149         }
150         if (cp == NULL || *cp == '\0')
151             cp = &dirch[DIR_STOP];
152         dir = chkdir(*cp, DIR_STOP, DIR_LAST);
153         if (dir >= 0) {
154             if (type == EF_SHIP)
155                 stopping |= shp_nav_one_sector(ulist, dir,
156                                                player->cnum, *together);
157             else {
158                 if (!moved && !lnd_abandon_askyn(ulist))
159                     return RET_FAIL;
160                 stopping |= lnd_mar_one_sector(ulist, dir, player->cnum);
161             }
162             moved = 1;
163             cp++;
164             continue;
165         }
166         ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
167         if (ac <= 0) {
168             player->argp[0] = "";
169             cp = NULL;
170         } else if (ac == 1) {
171             sprintf(dp, "%d", leader->uid);
172             player->argp[1] = dp;
173             cp++;
174         } else
175             cp = NULL;
176         bmap_flag = 0;
177         switch (*player->argp[0]) {
178         case 'B':
179             bmap_flag = 'b';
180             /*
181              * fall through
182              */
183         case 'M':
184             display_region_map(bmap_flag, type, leader->x, leader->y,
185                                player->argp[1], player->argp[2]);
186             skip = 1;
187             continue;
188         case 'f':
189             if (ac <= 1)
190                 switch_leader(ulist, -1);
191             else
192                 switch_leader(ulist, atoi(player->argp[1]));
193             leader = get_leader(ulist);
194             if (leader->uid != leader_uid) {
195                 leader_uid = leader->uid;
196                 pr_leader_change(leader);
197             }
198             continue;
199         case 'i':
200             unit_list(ulist);
201             continue;
202         case 'm':
203             if (type == EF_SHIP)
204                 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
205             else {
206                 lnd_sweep(ulist, 1, 1, player->cnum);
207                 stopping |= lnd_check_mines(ulist);
208             }
209             continue;
210         case 'r':
211             radar(leader->ef_type);
212             skip = 1;
213             player->btused++;
214             continue;
215         case 'l':
216             do_look(type);
217             player->btused++;
218             continue;
219         case 's':
220             if (leader->ef_type != EF_SHIP)
221                 break;
222             sona();
223             player->btused++;
224             skip = 1;
225             continue;
226         case 'd':
227             if (ac < 3) {
228                 player->argp[2] = ac < 2 ? "1" : player->argp[1];
229                 sprintf(dp, "%d", leader->uid);
230                 player->argp[1] = dp;
231             }
232             if (type == EF_SHIP)
233                 mine();
234             else
235                 landmine();
236             stopping = 1;
237             skip = 1;
238             player->btused++;
239             continue;
240         case 'v':
241             unit_view(ulist);
242             continue;
243         }
244         direrr("`%c' to stop", ", `%c' to view", NULL);
245         pr(", `i' to list %s, `f' to change %s,\n",
246             type == EF_SHIP ? "ships" : "units",
247             type == EF_SHIP ? "flagship" : "leader");
248         pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
249             type == EF_SHIP ? "`s' to sonar, " : "");
250         pr("`d' to drop mines, and `m' to minesweep\n");
251         stopping = 1;
252     }
253     return RET_OK;
254 }
255
256 int
257 nav_map(int x, int y, int show_designations)
258 {
259     char *ptr;
260     struct nstr_sect ns;
261     struct sctstr sect;
262     int i;
263     /* Note this is not re-entrant anyway, so we keep the buffers
264        around */
265     static char *wmapbuf = NULL;
266     static char **wmap = NULL;
267     int changed = 0;
268
269     if (!wmapbuf)
270         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
271     if (!wmap) {
272         wmap = malloc(WORLD_Y * sizeof(*wmap));
273         if (wmap && wmapbuf) {
274             for (i = 0; i < WORLD_Y; i++)
275                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
276         } else if (wmap) {
277             free(wmap);
278             wmap = NULL;
279         }
280     }
281     if (!wmapbuf || !wmap) {
282         pr("Memory error, tell the deity.\n");
283         logerror("malloc failed in navi\n");
284         return RET_FAIL;
285     }
286     snxtsct_dist(&ns, x, y, 1);
287     blankfill(wmapbuf, &ns.range, 1);
288     while (nxtsct(&ns, &sect)) {
289         ptr = &wmap[ns.dy][ns.dx];
290         *ptr = dchr[sect.sct_type].d_mnem;
291         if (!show_designations &&
292             sect.sct_own != player->cnum &&
293             sect.sct_type != SCT_WATER &&
294             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
295             *ptr = '?';
296         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
297         /*
298          * We do it this way so that 'x' and 'X'
299          * bdesignations will show up. This can
300          * be used to mark mined sectors. So, the
301          * player will see the current des, UNLESS
302          * they've marked the sector 'x' or 'X',
303          * in which case they'll see that.
304          * --ts
305          */
306         *ptr = player->bmap[sect.sct_uid];
307     }
308     if (changed)
309         writemap(player->cnum);
310     for (i = 0; i < ns.range.height; i++)
311         pr("%s\n", wmap[i]);
312     return RET_OK;
313 }
314
315 static void
316 pr_leader_change(struct empobj *leader)
317 {
318     pr("Changing %s to %s\n",
319         leader->ef_type == EF_SHIP ? "flagship" : "leader",
320         unit_nameof(leader));
321 }
322
323 static struct empobj *
324 get_leader(struct emp_qelem *list)
325 {
326     return &((struct ulist *)(list->q_back))->unit.gen;
327 }
328
329 static void
330 switch_leader(struct emp_qelem *list, int uid)
331 {
332     struct emp_qelem *qp, *save;
333     struct ulist *ulp;
334
335     if (QEMPTY(list))
336         return;
337
338     save = qp = list->q_back;
339     do {
340         emp_remque(qp);
341         emp_insque(qp, list);
342         qp = list->q_back;
343         ulp = (struct ulist *)qp;
344         if (ulp->unit.gen.uid == uid || uid == -1)
345             break;
346     } while (list->q_back != save);
347 }