]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
Combined struct llist and struct mlist into superset struct ulist.
[empserver] / src / lib / commands / navi.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
33
34 #include <config.h>
35
36 #include <ctype.h>
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 int set_flagship(struct emp_qelem *list, struct shpstr **flagshipp);
45 static void switch_flagship(struct emp_qelem *list, int ship_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     char *cp = NULL;
55     struct shpstr *shp = NULL;  /* flagship */
56     int dir;
57     int stopping = 0;
58     int skip = 0;
59     char buf[1024];
60     char prompt[128];
61     char scanspace[1024];
62     char pathtaken[1024];       /* Doubtful we'll have a path longer than this */
63     char *pt = pathtaken;
64     char bmap_flag;
65     int ac;
66
67     if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
68         return RET_SYN;
69     shp_sel(&ni_ship, &ship_list);
70     shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
71     if (QEMPTY(&ship_list)) {
72         pr("No ships\n");
73         return RET_FAIL;
74     }
75     set_flagship(&ship_list, &shp);
76     if (player->argp[2]) {
77         strcpy(buf, player->argp[2]);
78         if (!(cp = shp_path(together, shp, buf)))
79             cp = player->argp[2];
80     }
81
82     *pt = '\0';
83     while (!QEMPTY(&ship_list)) {
84         char dp[80];
85
86         if (cp == NULL || *cp == '\0' || stopping) {
87             stopping = 0;
88             shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
89             if (QEMPTY(&ship_list)) {
90                 pr("No ships left\n");
91                 if (strlen(pathtaken) > 0) {
92                     pathtaken[strlen(pathtaken) - 1] = '\0';
93                     if (strlen(pathtaken) > 0)
94                         pr("Path taken: %s\n", pathtaken);
95                 }
96                 return RET_OK;
97             }
98             if (set_flagship(&ship_list, &shp)) {
99                 stopping = 1;
100                 continue;
101             }
102             if (!skip)
103                 nav_map(shp->shp_x, shp->shp_y,
104                         !(mchr[(int)shp->shp_type].m_flags & M_SUB));
105             else
106                 skip = 0;
107             sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
108                     minmob, xyas(shp->shp_x, shp->shp_y, player->cnum));
109             cp = getstring(prompt, buf);
110             /* Just in case any of our ships were shelled while we were
111              * at the prompt, we call shp_nav() again.
112              */
113             shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
114             if (QEMPTY(&ship_list)) {
115                 pr("No ships left\n");
116                 if (strlen(pathtaken) > 0) {
117                     pathtaken[strlen(pathtaken) - 1] = '\0';
118                     if (strlen(pathtaken) > 0)
119                         pr("Path taken: %s\n", pathtaken);
120                 }
121                 return RET_OK;
122             }
123             if (set_flagship(&ship_list, &shp)) {
124                 stopping = 1;
125                 continue;
126             }
127             if (cp && !(cp = shp_path(together, shp, buf)))
128                 cp = buf;
129         }
130         radmapnopr(shp->shp_x, shp->shp_y, (int)shp->shp_effic,
131                    (int)techfact(shp->shp_tech,
132                                  mchr[(int)shp->shp_type].m_vrnge),
133                    (mchr[(int)shp->shp_type].m_flags & M_SONAR)
134                    ? techfact(shp->shp_tech, 1.0) : 0.0);
135         if (cp == NULL || *cp == '\0')
136             cp = &dirch[DIR_STOP];
137         dir = chkdir(*cp, DIR_STOP, DIR_VIEW);
138         if (dir >= 0) {
139             if (dir == DIR_VIEW)
140                 shp_view(&ship_list);
141             else {
142                 stopping |= shp_nav_one_sector(&ship_list, dir, player->cnum, together);
143                 if (stopping != 2) {
144                     *pt++ = dirch[dir];
145                     *pt = '\0';
146                 }
147             }
148             cp++;
149             continue;
150         }
151         ac = parse(cp, player->argp, NULL, scanspace, NULL);
152         if (ac <= 1) {
153             sprintf(dp, "%d", shp->shp_uid);
154             player->argp[1] = dp;
155             cp++;
156         } else
157             cp = NULL;
158         bmap_flag = 0;
159         switch (*player->argp[0]) {
160         case 'B':
161             bmap_flag = 'b';
162             /*
163              * fall through
164              */
165         case 'M':
166             do_map(bmap_flag, EF_SHIP, player->argp[1], player->argp[2]);
167             skip = 1;
168             break;
169         case 'f':
170             if (ac <= 1) 
171                 switch_flagship(&ship_list, -1);
172             else
173                 switch_flagship(&ship_list, atoi(player->argp[1]));
174             set_flagship(&ship_list, &shp);
175             break;
176         case 'i':
177             shp_list(&ship_list);
178             break;
179         case 'm':
180             stopping |= shp_sweep(&ship_list, 1, 0, player->cnum);
181             break;
182         case 'r':
183             radar(EF_SHIP);
184             skip = 1;
185             player->btused++;
186             break;
187         case 'l':
188             look();
189             player->btused++;
190             break;
191         case 's':
192             sona();
193             player->btused++;
194             skip = 1;
195             break;
196         case 'd':
197             if (ac == 2) {
198                 player->argp[2] = player->argp[1];
199                 sprintf(dp, "%d", shp->shp_uid);
200                 player->argp[1] = dp;
201             }
202             mine();
203             skip = 1;
204             player->btused++;
205             break;
206         default:
207             direrr("`%c' to stop", ", `%c' to view, ", 0);
208             pr("`i' to list ships, `f' to change flagship,\n");
209             pr("`r' to radar, `s' to sonar, `l' to look, `M' to map, `B' to bmap,\n");
210             pr("`d' to drop mines, and `m' to minesweep\n");
211             stopping = 1;
212         }
213     }
214     if (strlen(pathtaken) > 0) {
215         pathtaken[strlen(pathtaken) - 1] = '\0';
216         if (strlen(pathtaken) > 0)
217             pr("Path taken: %s\n", pathtaken);
218     }
219     return RET_OK;
220 }
221
222 int
223 nav_map(int x, int y, int show_designations)
224 {
225     char *ptr;
226     struct nstr_sect ns;
227     struct natstr *np;
228     struct sctstr sect;
229     struct range range;
230     int i;
231     /* Note this is not re-entrant anyway, so we keep the buffers
232        around */
233     static unsigned char *bitmap = NULL;
234     static char *wmapbuf = NULL;
235     static char **wmap = NULL;
236     int changed = 0;
237
238     if (!wmapbuf)
239         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
240     if (!wmap) {
241         wmap = malloc(WORLD_Y * sizeof(*wmap));
242         if (wmap && wmapbuf) {
243             for (i = 0; i < WORLD_Y; i++)
244                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
245         } else if (wmap) {
246             free(wmap);
247             wmap = NULL;
248         }
249     }
250     if (!bitmap)
251         bitmap = malloc((WORLD_X * WORLD_Y) / 8);
252     if (!wmapbuf || !wmap || !bitmap) {
253         pr("Memory error, tell the deity.\n");
254         logerror("malloc failed in navi\n");
255         return RET_FAIL;
256     }
257     memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
258     snxtsct_dist(&ns, x, y, 1);
259     np = getnatp(player->cnum);
260     xyrelrange(np, &ns.range, &range);
261     blankfill(wmapbuf, &ns.range, 1);
262     while (nxtsct(&ns, &sect)) {
263         ptr = &wmap[ns.dy][ns.dx];
264         *ptr = dchr[sect.sct_type].d_mnem;
265         if (!show_designations &&
266             sect.sct_own != player->cnum &&
267             sect.sct_type != SCT_WATER &&
268             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
269             *ptr = '?';
270         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
271         /*
272          * We do it this way so that 'x' and 'X'
273          * bdesignations will show up. This can
274          * be used to mark mined sectors. So, the
275          * player will see the current des, UNLESS
276          * they've marked the sector 'x' or 'X',
277          * in which case they'll see that.
278          * --ts
279          */
280         *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
281     }
282     if (changed)
283         writemap(player->cnum);
284     for (i = 0; i < ns.range.height; i++)
285         pr("%s\n", wmap[i]);
286     return RET_OK;
287 }
288
289 static int
290 set_flagship(struct emp_qelem *list, struct shpstr **flagshipp)
291 {
292     struct ulist *mlp = (struct ulist *)(list->q_back);
293
294     if (!*flagshipp)
295         pr("Flagship is ");
296     else if ((*flagshipp)->shp_uid != mlp->unit.ship.shp_uid)
297         pr("Changing flagship to ");
298     else
299         return 0;
300     *flagshipp = &mlp->unit.ship;
301     pr("%s\n", prship(&mlp->unit.ship));
302     return 1;
303 }
304
305 static void
306 switch_flagship(struct emp_qelem *list, int ship_uid)
307 {
308     struct emp_qelem *qp, *save;
309     struct ulist *mlp;
310
311     if (QEMPTY(list))
312         return;
313
314     save = qp = list->q_back;
315     do {
316         emp_remque(qp);
317         emp_insque(qp, list);
318         qp = list->q_back;
319         mlp = (struct ulist *)qp;
320         if (mlp->unit.ship.shp_uid == ship_uid || ship_uid == -1)
321             break;
322     } while (list->q_back != save);
323 }
324