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