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