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