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