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