]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
Import of Empire 4.2.12
[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 
138                              & M_SONAR)
139                             ? techfact(shp->shp_tech, 1.0) : 0.0));
140         if (cp == 0 || *cp == '\0')
141             cp = &dirch[DIR_STOP];
142         if (*cp == 'M' ||
143             *cp == 'B' ||
144             *cp == 'f' ||
145             *cp == 'i' ||
146             *cp == 'm') {
147             ++cp;
148             if (cp[-1] == 'M') {
149                 unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
150                 draw_map(0, origin, MAP_SHIP, &ns, player->cnum);
151                 skip = 1;
152             } else if (cp[-1] == 'B') {
153                 unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
154                 draw_map(EF_BMAP, origin, MAP_SHIP, &ns, player->cnum);
155                 skip = 1;
156             } else if (cp[-1] == 'f') {
157                 struct emp_qelem *qp;
158                 qp = ship_list.q_back;
159                 emp_remque(ship_list.q_back);
160                 emp_insque(qp, &ship_list);
161                 set_flagship(&ship_list, &shp);
162             } else if (cp[-1] == 'i') {
163                 shp_list(&ship_list);
164             } else {
165                 stopping |= shp_sweep(&ship_list, 1, player->cnum);
166             }
167             continue;
168         } else if (*cp == 'r' ||
169                    *cp == 'l' ||
170                    *cp == 's') {
171             bp=++cp;
172             while((*bp != ' ') && (*bp)) bp++;
173             while((*bp == ' ') && (*bp)) bp++;
174             if ((bp != (s_char *)0) && (*bp))
175                 player->argp[1] = bp;
176             else {
177                 sprintf(dp,"%d",shp->shp_uid);
178                 player->argp[1] = dp;
179             }
180             if (cp[-1] == 'r') {
181                 rada();
182                 skip = 1;
183             } else if (cp[-1] == 'l')
184                 look();
185             else {
186                 player->argp[2] = 0;
187                 sona();
188                 skip = 1;
189             }
190             *cp = 0;
191             player->btused++;
192             continue;
193         } else {
194             dir = chkdir(*cp++, DIR_STOP, DIR_VIEW);
195             if (dir == -1) {
196                 if (NULL != (cp = shp_path(together, shp, buf)))
197                     continue;
198                 direrr("`%c' to stop", ", `%c' to view, ", 0);
199                 pr("`i' to list ships, `f' to change flagship,\n");
200                 pr("`r' to radar, `s' to sonar, `l' to look, `M' to map, `B' to bmap,\n");
201                 pr("and `m' to minesweep\n");
202                 stopping = 1;
203                 continue;
204             } else if (dir == DIR_VIEW) {
205                 shp_view(&ship_list);
206                 continue;
207             }
208         }
209         stopping |= shp_nav_one_sector(&ship_list, dir, player->cnum, together);
210         if (stopping != 2) {
211           *pt++ = dirch[dir];
212           *pt = '\0';
213         }
214     }
215     if (strlen(pathtaken) > 0) {
216         pathtaken[strlen(pathtaken)-1] = '\0';
217         if (strlen(pathtaken) > 0)
218             pr("Path taken: %s\n", pathtaken);
219     }
220     return RET_OK;
221 }
222
223 int
224 nav_map(int x, int y, int show_designations)
225 {
226     register s_char *ptr;
227     struct      nstr_sect ns;
228     struct      natstr *np;
229     struct      sctstr sect;
230     struct      range range;
231     int i;
232     /* Note this is not re-entrant anyway, so we keep the buffers
233        around */
234     static u_char      *bitmap = (u_char *)0;
235     static s_char      *wmapbuf = (s_char *)0;
236     static s_char      **wmap = (s_char **)0;
237     s_char      what[64];
238     int changed = 0;
239     
240     np = getnatp(player->cnum);
241     sprintf(what, "%d:%d,%d:%d",xrel(np,x-2),xrel(np,x+2),yrel(np,y-1),
242             yrel(np,y+1));
243     if (!snxtsct(&ns, what))
244         return RET_FAIL;
245     if (!wmapbuf)
246         wmapbuf = (s_char *)malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
247     if (!wmap) {
248         wmap = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
249         if (wmap && wmapbuf) {
250             for (i = 0; i < WORLD_Y; i++)
251                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
252         } else if (wmap) {
253             free((s_char *)wmap);
254             wmap = (s_char **)0;
255         }
256     }
257     if (!bitmap)
258         bitmap = (u_char *)malloc((WORLD_X * WORLD_Y) / 8);
259     if (!wmapbuf || !wmap || !bitmap) {
260         pr("Memory error, tell the deity.\n");
261         logerror("malloc failed in navi\n");
262         return RET_FAIL;
263     }
264     bzero((s_char *)bitmap, (WORLD_X * WORLD_Y) / 8);
265     /* zap any conditionals */
266     ns.ncond = 0;
267     xyrelrange(np, &ns.range, &range);
268     blankfill((s_char *)wmapbuf, &ns.range, 1);
269     while (nxtsct(&ns, &sect)){
270         ptr = &wmap[ns.dy][ns.dx];
271         *ptr = dchr[sect.sct_type].d_mnem;
272         if (!show_designations &&
273             sect.sct_own != player->cnum &&
274             sect.sct_type != SCT_WATER &&
275             sect.sct_type != SCT_BSPAN &&
276             sect.sct_type != SCT_HARBR)
277             *ptr = '?';
278         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
279         /*
280          * We do it this way so that 'x' and 'X'
281          * bdesignations will show up. This can
282          * be used to mark mined sectors. So, the
283          * player will see the current des, UNLESS
284          * they've marked the sector 'x' or 'X',
285          * in which case they'll see that.
286          * --ts
287          */
288         *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
289     }
290     if (changed)
291         writemap(player->cnum);
292     for (i=0; i < ns.range.height; i++)
293         pr("%s\n", wmap[i]);
294     return RET_OK;
295 }
296
297 static int
298 set_flagship(struct emp_qelem *list, struct shpstr **flagshipp)
299 {
300     struct mlist *mlp = (struct mlist *)(list->q_back);
301     
302     if (!*flagshipp)
303         pr("Flagship is ");
304     else if ((*flagshipp)->shp_uid != mlp->ship.shp_uid)
305         pr("Changing flagship to ");
306     else
307         return 0;
308     *flagshipp = &mlp->ship;
309     pr("%s\n", prship(&mlp->ship));
310     return 1;
311 }