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