]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
0213be25900c06e3e5c7a5bf85a3b877d898bb39
[empserver] / src / lib / commands / navi.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  navi.c: Navigate ships and such
29  *
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995 (rewritten)
32  *     Ron Koenderink, 2006-2007
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 "empobj.h"
42 #include "unit.h"
43
44 static void pr_leader_change(struct empobj *leader);
45 static struct empobj *get_leader(struct emp_qelem *list);
46 static void switch_leader(struct emp_qelem *list, int uid);
47
48 int
49 navi(void)
50 {
51     struct nstr_item ni_ship;
52     struct emp_qelem ship_list;
53     double minmob, maxmob;
54     int together;
55
56     if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
57         return RET_SYN;
58     shp_sel(&ni_ship, &ship_list);
59     shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
60     if (QEMPTY(&ship_list)) {
61         pr("No ships\n");
62         return RET_FAIL;
63     }
64
65     return do_unit_move(&ship_list, &together, &minmob, &maxmob);
66 }
67
68 int
69 do_unit_move(struct emp_qelem *ulist, int *together,
70              double *minmob, double *maxmob)
71 {
72     char *cp = NULL;
73     int leader_uid;
74     struct empobj *leader;
75     int dir;
76     int stopping = 0;
77     int skip = 0;
78     char buf[1024];
79     char prompt[128];
80     char scanspace[1024];
81     char pathtaken[1024];  /* Doubtful we'll have a path longer than this */
82     char *pt = pathtaken;
83     char bmap_flag;
84     int ac;
85     int type;
86
87     leader = get_leader(ulist);
88     leader_uid = leader->uid;
89     type = leader->ef_type;
90     pr("%s is %s\n",
91         type == EF_SHIP ? "Flagship" : "Leader",
92         obj_nameof(leader));
93
94     if (player->argp[2]) {
95         strcpy(buf, player->argp[2]);
96         if (!(cp = unit_path(*together, leader, buf)))
97             cp = player->argp[2];
98     }
99
100     *pt = '\0';
101     while (!QEMPTY(ulist)) {
102         char dp[80];
103
104         if (cp == NULL || *cp == '\0' || stopping) {
105             stopping = 0;
106             if (type == EF_SHIP)
107                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
108             else
109                 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
110             if (QEMPTY(ulist)) {
111                 pr("No %s left\n",
112                     type == EF_SHIP ? "ships" : "lands");
113                 if (type == EF_SHIP && strlen(pathtaken) > 1) {
114                     pathtaken[strlen(pathtaken) - 1] = '\0';
115                     pr("Path taken: %s\n", pathtaken);
116                 }
117                 return RET_OK;
118             }
119             leader = get_leader(ulist);
120             if (leader->uid != leader_uid) {
121                 leader_uid = leader->uid;
122                 pr_leader_change(leader);
123                 stopping = 1;
124                 continue;
125             }
126             if (!skip)
127                 nav_map(leader->x, leader->y,
128                         type == EF_SHIP ?
129                             !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
130             else
131                 skip = 0;
132             sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
133                     *minmob, xyas(leader->x, leader->y, player->cnum));
134             cp = getstring(prompt, buf);
135             /* Just in case any of our units were shelled while we were
136              * at the prompt, we call shp_nav() or lnd_mar() again.
137              */
138             if (type == EF_SHIP)
139                 shp_nav(ulist, minmob, maxmob, together, player->cnum);
140             else
141                 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
142             if (QEMPTY(ulist)) {
143                 pr("No %s left\n",
144                     type == EF_SHIP ? "ships" : "lands");
145                 if (type == EF_SHIP && strlen(pathtaken) > 1) {
146                     pathtaken[strlen(pathtaken) - 1] = '\0';
147                     pr("Path taken: %s\n", pathtaken);
148                 }
149                 return RET_OK;
150             }
151             leader = get_leader(ulist);
152             if (leader->uid != leader_uid) {
153                 leader_uid = leader->uid;
154                 pr_leader_change(leader);
155                 stopping = 1;
156                 continue;
157             }
158             if (!(cp = unit_path(*together, leader, buf)))
159                 cp = buf;
160         }
161         if (type == EF_SHIP) {
162             radmapnopr(leader->x, leader->y, (int)leader->effic,
163                        (int)techfact(leader->tech,
164                                      mchr[(int)leader->type].m_vrnge),
165                        (mchr[(int)leader->type].m_flags & M_SONAR)
166                        ? techfact(leader->tech, 1.0) : 0.0);
167         }
168         if (cp == NULL || *cp == '\0')
169             cp = &dirch[DIR_STOP];
170         dir = chkdir(*cp, DIR_STOP, DIR_LAST);
171         if (dir >= 0) {
172             if (type == EF_SHIP) {
173                 stopping |= shp_nav_one_sector(ulist, dir,
174                     player->cnum, *together);
175                 if (stopping != 2) {
176                     *pt++ = dirch[dir];
177                     *pt = '\0';
178                 }
179             } else
180                 stopping |=
181                     lnd_mar_one_sector(ulist, dir, player->cnum,
182                         *together);
183             cp++;
184             continue;
185         }
186         ac = parse(cp, scanspace, player->argp, NULL, NULL, NULL);
187         if (ac <= 0) {
188             player->argp[0] = "";
189             cp = NULL;
190         } else if (ac == 1) {
191             sprintf(dp, "%d", leader->uid);
192             player->argp[1] = dp;
193             cp++;
194         } else
195             cp = NULL;
196         bmap_flag = 0;
197         switch (*player->argp[0]) {
198         case 'B':
199             bmap_flag = 'b';
200             /*
201              * fall through
202              */
203         case 'M':
204             do_map(bmap_flag, leader->ef_type, player->argp[1],
205                    player->argp[2]);
206             skip = 1;
207             continue;
208         case 'f':
209             if (ac <= 1)
210                 switch_leader(ulist, -1);
211             else
212                 switch_leader(ulist, atoi(player->argp[1]));
213             leader = get_leader(ulist);
214             if (leader->uid != leader_uid) {
215                 leader_uid = leader->uid;
216                 pr_leader_change(leader);
217             }
218             continue;
219         case 'i':
220             unit_list(ulist);
221             continue;
222         case 'm':
223             if (type == EF_SHIP)
224                 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
225             else {
226                 lnd_sweep(ulist, 1, 1, player->cnum);
227                 stopping |= lnd_check_mines(ulist);
228             }
229             continue;
230         case 'r':
231             radar(leader->ef_type);
232             skip = 1;
233             player->btused++;
234             continue;
235         case 'l':
236             do_look(type);
237             player->btused++;
238             continue;
239         case 's':
240             if (leader->ef_type != EF_SHIP)
241                 break;
242             sona();
243             player->btused++;
244             skip = 1;
245             continue;
246         case 'd':
247             if (ac < 3) {
248                 player->argp[2] = ac < 2 ? "1" : player->argp[1];
249                 sprintf(dp, "%d", leader->uid);
250                 player->argp[1] = dp;
251             }
252             if (type == EF_SHIP)
253                 mine();
254             else
255                 landmine();
256             skip = 1;
257             player->btused++;
258             continue;
259         case 'v':
260             unit_view(ulist);
261             continue;
262         }
263         direrr("`%c' to stop", ", `%c' to view", NULL);
264         pr(", `i' to list %s, `f' to change %s,\n",
265             type == EF_SHIP ? "ships" : "units",
266             type == EF_SHIP ? "flagship" : "leader");
267         pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
268             type == EF_SHIP ? "`s' to sonar, " : "");
269         pr("`d' to drop mines, and `m' to minesweep\n");
270         stopping = 1;
271     }
272     if (type == EF_SHIP && strlen(pathtaken) > 1) {
273         pathtaken[strlen(pathtaken) - 1] = '\0';
274         pr("Path taken: %s\n", pathtaken);
275     }
276     return RET_OK;
277 }
278
279 int
280 nav_map(int x, int y, int show_designations)
281 {
282     char *ptr;
283     struct nstr_sect ns;
284     struct sctstr sect;
285     int i;
286     /* Note this is not re-entrant anyway, so we keep the buffers
287        around */
288     static char *wmapbuf = NULL;
289     static char **wmap = NULL;
290     int changed = 0;
291
292     if (!wmapbuf)
293         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
294     if (!wmap) {
295         wmap = malloc(WORLD_Y * sizeof(*wmap));
296         if (wmap && wmapbuf) {
297             for (i = 0; i < WORLD_Y; i++)
298                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
299         } else if (wmap) {
300             free(wmap);
301             wmap = NULL;
302         }
303     }
304     if (!wmapbuf || !wmap) {
305         pr("Memory error, tell the deity.\n");
306         logerror("malloc failed in navi\n");
307         return RET_FAIL;
308     }
309     snxtsct_dist(&ns, x, y, 1);
310     blankfill(wmapbuf, &ns.range, 1);
311     while (nxtsct(&ns, &sect)) {
312         ptr = &wmap[ns.dy][ns.dx];
313         *ptr = dchr[sect.sct_type].d_mnem;
314         if (!show_designations &&
315             sect.sct_own != player->cnum &&
316             sect.sct_type != SCT_WATER &&
317             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
318             *ptr = '?';
319         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
320         /*
321          * We do it this way so that 'x' and 'X'
322          * bdesignations will show up. This can
323          * be used to mark mined sectors. So, the
324          * player will see the current des, UNLESS
325          * they've marked the sector 'x' or 'X',
326          * in which case they'll see that.
327          * --ts
328          */
329         *ptr = player->bmap[sect.sct_uid];
330     }
331     if (changed)
332         writemap(player->cnum);
333     for (i = 0; i < ns.range.height; i++)
334         pr("%s\n", wmap[i]);
335     return RET_OK;
336 }
337
338 static void
339 pr_leader_change(struct empobj *leader)
340 {
341     pr("Changing %s to %s\n",
342         leader->ef_type == EF_SHIP ? "flagship" : "leader",
343         obj_nameof(leader));
344 }
345
346 static struct empobj *
347 get_leader(struct emp_qelem *list)
348 {
349     return &((struct ulist *)(list->q_back))->unit.gen;
350 }
351
352 static void
353 switch_leader(struct emp_qelem *list, int uid)
354 {
355     struct emp_qelem *qp, *save;
356     struct ulist *ulp;
357
358     if (QEMPTY(list))
359         return;
360
361     save = qp = list->q_back;
362     do {
363         emp_remque(qp);
364         emp_insque(qp, list);
365         qp = list->q_back;
366         ulp = (struct ulist *)qp;
367         if (ulp->unit.gen.uid == uid || uid == -1)
368             break;
369     } while (list->q_back != save);
370 }