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