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