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