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