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