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