]> git.pond.sub.org Git - empserver/blob - src/lib/commands/navi.c
(march, navi): Switch to struct empobj * for leader and rename to leader.
[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             break;
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             break;
186         case 'i':
187             shp_list(&ship_list);
188             break;
189         case 'm':
190             stopping |= shp_sweep(&ship_list, 1, 1, player->cnum);
191             break;
192         case 'r':
193             radar(EF_SHIP);
194             skip = 1;
195             player->btused++;
196             break;
197         case 'l':
198             look();
199             player->btused++;
200             break;
201         case 's':
202             sona();
203             player->btused++;
204             skip = 1;
205             break;
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             break;
216         default:
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     }
224     if (strlen(pathtaken) > 0) {
225         pathtaken[strlen(pathtaken) - 1] = '\0';
226         if (strlen(pathtaken) > 0)
227             pr("Path taken: %s\n", pathtaken);
228     }
229     return RET_OK;
230 }
231
232 int
233 nav_map(int x, int y, int show_designations)
234 {
235     char *ptr;
236     struct nstr_sect ns;
237     struct natstr *np;
238     struct sctstr sect;
239     struct range range;
240     int i;
241     /* Note this is not re-entrant anyway, so we keep the buffers
242        around */
243     static unsigned char *bitmap = NULL;
244     static char *wmapbuf = NULL;
245     static char **wmap = NULL;
246     int changed = 0;
247
248     if (!wmapbuf)
249         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
250     if (!wmap) {
251         wmap = malloc(WORLD_Y * sizeof(*wmap));
252         if (wmap && wmapbuf) {
253             for (i = 0; i < WORLD_Y; i++)
254                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
255         } else if (wmap) {
256             free(wmap);
257             wmap = NULL;
258         }
259     }
260     if (!bitmap)
261         bitmap = malloc((WORLD_X * WORLD_Y) / 8);
262     if (!wmapbuf || !wmap || !bitmap) {
263         pr("Memory error, tell the deity.\n");
264         logerror("malloc failed in navi\n");
265         return RET_FAIL;
266     }
267     memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
268     snxtsct_dist(&ns, x, y, 1);
269     np = getnatp(player->cnum);
270     xyrelrange(np, &ns.range, &range);
271     blankfill(wmapbuf, &ns.range, 1);
272     while (nxtsct(&ns, &sect)) {
273         ptr = &wmap[ns.dy][ns.dx];
274         *ptr = dchr[sect.sct_type].d_mnem;
275         if (!show_designations &&
276             sect.sct_own != player->cnum &&
277             sect.sct_type != SCT_WATER &&
278             sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
279             *ptr = '?';
280         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
281         /*
282          * We do it this way so that 'x' and 'X'
283          * bdesignations will show up. This can
284          * be used to mark mined sectors. So, the
285          * player will see the current des, UNLESS
286          * they've marked the sector 'x' or 'X',
287          * in which case they'll see that.
288          * --ts
289          */
290         *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
291     }
292     if (changed)
293         writemap(player->cnum);
294     for (i = 0; i < ns.range.height; i++)
295         pr("%s\n", wmap[i]);
296     return RET_OK;
297 }