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