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