]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
navigate: Require all ships 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 = 1;
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, player->cnum);
59     if (QEMPTY(&ship_list)) {
60         pr("No ships\n");
61         return RET_FAIL;
62     }
63     return do_unit_move(&ship_list, &together, &minmob, &maxmob);
64 }
65
66 int
67 do_unit_move(struct emp_qelem *ulist, int *together,
68              double *minmob, double *maxmob)
69 {
70     char *cp = NULL;
71     int leader_uid;
72     struct empobj *leader;
73     int dir;
74     int stopping = 0;
75     int skip = 0;
76     int moved = 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, player->cnum);
102             else
103                 lnd_mar(ulist, minmob, maxmob, 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, player->cnum);
129             else
130                 lnd_mar(ulist, minmob, maxmob, 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, player->cnum);
155             else {
156                 if (!moved && !lnd_abandon_askyn(ulist))
157                     return RET_FAIL;
158                 stopping |= lnd_mar_one_sector(ulist, dir, player->cnum);
159             }
160             moved = 1;
161             cp++;
162             continue;
163         }
164         ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
165         if (ac <= 0) {
166             player->argp[0] = "";
167             cp = NULL;
168         } else if (ac == 1) {
169             sprintf(dp, "%d", leader->uid);
170             player->argp[1] = dp;
171             cp++;
172         } else
173             cp = NULL;
174         bmap_flag = 0;
175         switch (*player->argp[0]) {
176         case 'B':
177             bmap_flag = 'b';
178             /*
179              * fall through
180              */
181         case 'M':
182             display_region_map(bmap_flag, type, leader->x, leader->y,
183                                player->argp[1], player->argp[2]);
184             skip = 1;
185             continue;
186         case 'f':
187             if (ac <= 1)
188                 switch_leader(ulist, -1);
189             else
190                 switch_leader(ulist, atoi(player->argp[1]));
191             leader = get_leader(ulist);
192             if (leader->uid != leader_uid) {
193                 leader_uid = leader->uid;
194                 pr_leader_change(leader);
195             }
196             continue;
197         case 'i':
198             unit_list(ulist);
199             continue;
200         case 'm':
201             if (type == EF_SHIP)
202                 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
203             else {
204                 lnd_sweep(ulist, 1, 1, player->cnum);
205                 stopping |= lnd_check_mines(ulist);
206             }
207             continue;
208         case 'r':
209             radar(leader->ef_type);
210             skip = 1;
211             player->btused++;
212             continue;
213         case 'l':
214             do_look(type);
215             player->btused++;
216             continue;
217         case 's':
218             if (leader->ef_type != EF_SHIP)
219                 break;
220             sona();
221             player->btused++;
222             skip = 1;
223             continue;
224         case 'd':
225             if (ac < 3) {
226                 player->argp[2] = ac < 2 ? "1" : player->argp[1];
227                 sprintf(dp, "%d", leader->uid);
228                 player->argp[1] = dp;
229             }
230             if (type == EF_SHIP)
231                 mine();
232             else
233                 landmine();
234             stopping = 1;
235             skip = 1;
236             player->btused++;
237             continue;
238         case 'v':
239             unit_view(ulist);
240             continue;
241         }
242         direrr("`%c' to stop", ", `%c' to view", NULL);
243         pr(", `i' to list %s, `f' to change %s,\n",
244             type == EF_SHIP ? "ships" : "units",
245             type == EF_SHIP ? "flagship" : "leader");
246         pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
247             type == EF_SHIP ? "`s' to sonar, " : "");
248         pr("`d' to drop mines, and `m' to minesweep\n");
249         stopping = 1;
250     }
251     return RET_OK;
252 }
253
254 int
255 nav_map(int x, int y, int show_designations)
256 {
257     char *ptr;
258     struct nstr_sect ns;
259     struct sctstr sect;
260     int i;
261     /* Note this is not re-entrant anyway, so we keep the buffers
262        around */
263     static char *wmapbuf = NULL;
264     static char **wmap = NULL;
265     int changed = 0;
266
267     if (!wmapbuf)
268         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
269     if (!wmap) {
270         wmap = malloc(WORLD_Y * sizeof(*wmap));
271         if (wmap && wmapbuf) {
272             for (i = 0; i < WORLD_Y; i++)
273                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
274         } else if (wmap) {
275             free(wmap);
276             wmap = NULL;
277         }
278     }
279     if (!wmapbuf || !wmap) {
280         pr("Memory error, tell the deity.\n");
281         logerror("malloc failed in navi\n");
282         return RET_FAIL;
283     }
284     snxtsct_dist(&ns, x, y, 1);
285     blankfill(wmapbuf, &ns.range, 1);
286     while (nxtsct(&ns, &sect)) {
287         ptr = &wmap[ns.dy][ns.dx];
288         *ptr = dchr[sect.sct_type].d_mnem;
289         if (!show_designations &&
290             sect.sct_own != player->cnum &&
291             sect.sct_type != SCT_WATER &&
292             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
293             *ptr = '?';
294         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
295         /*
296          * We do it this way so that 'x' and 'X'
297          * bdesignations will show up. This can
298          * be used to mark mined sectors. So, the
299          * player will see the current des, UNLESS
300          * they've marked the sector 'x' or 'X',
301          * in which case they'll see that.
302          * --ts
303          */
304         *ptr = player->bmap[sect.sct_uid];
305     }
306     if (changed)
307         writemap(player->cnum);
308     for (i = 0; i < ns.range.height; i++)
309         pr("%s\n", wmap[i]);
310     return RET_OK;
311 }
312
313 static void
314 pr_leader_change(struct empobj *leader)
315 {
316     pr("Changing %s to %s\n",
317         leader->ef_type == EF_SHIP ? "flagship" : "leader",
318         unit_nameof(leader));
319 }
320
321 static struct empobj *
322 get_leader(struct emp_qelem *list)
323 {
324     return &((struct ulist *)(list->q_back))->unit.gen;
325 }
326
327 static void
328 switch_leader(struct emp_qelem *list, int uid)
329 {
330     struct emp_qelem *qp, *save;
331     struct ulist *ulp;
332
333     if (QEMPTY(list))
334         return;
335
336     save = qp = list->q_back;
337     do {
338         emp_remque(qp);
339         emp_insque(qp, list);
340         qp = list->q_back;
341         ulp = (struct ulist *)qp;
342         if (ulp->unit.gen.uid == uid || uid == -1)
343             break;
344     } while (list->q_back != save);
345 }