2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * navi.c: Navigate ships and such
30 * Known contributors to this file:
31 * Ken Stevens, 1995 (rewritten)
32 * Ron Koenderink, 2006-2007
44 static void pr_leader_change(struct empobj *leader);
45 static struct empobj *get_leader(struct emp_qelem *list);
46 static void switch_leader(struct emp_qelem *list, int uid);
51 struct nstr_item ni_ship;
52 struct emp_qelem ship_list;
53 double minmob, maxmob;
56 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
58 shp_sel(&ni_ship, &ship_list);
59 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
60 if (QEMPTY(&ship_list)) {
65 return do_unit_move(&ship_list, &together, &minmob, &maxmob);
69 do_unit_move(struct emp_qelem *ulist, int *together,
70 double *minmob, double *maxmob)
74 struct empobj *leader;
81 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
87 leader = get_leader(ulist);
88 leader_uid = leader->uid;
89 type = leader->ef_type;
91 type == EF_SHIP ? "Flagship" : "Leader",
94 if (player->argp[2]) {
95 strcpy(buf, player->argp[2]);
96 if (!(cp = unit_path(*together, leader, buf)))
101 while (!QEMPTY(ulist)) {
104 if (cp == NULL || *cp == '\0' || stopping) {
107 shp_nav(ulist, minmob, maxmob, together, player->cnum);
109 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
112 type == EF_SHIP ? "ships" : "lands");
113 if (type == EF_SHIP && strlen(pathtaken) > 1) {
114 pathtaken[strlen(pathtaken) - 1] = '\0';
115 pr("Path taken: %s\n", pathtaken);
119 leader = get_leader(ulist);
120 if (leader->uid != leader_uid) {
121 leader_uid = leader->uid;
122 pr_leader_change(leader);
127 nav_map(leader->x, leader->y,
129 !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
132 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
133 *minmob, xyas(leader->x, leader->y, player->cnum));
134 cp = getstring(prompt, buf);
135 /* Just in case any of our units were shelled while we were
136 * at the prompt, we call shp_nav() or lnd_mar() again.
139 shp_nav(ulist, minmob, maxmob, together, player->cnum);
141 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
144 type == EF_SHIP ? "ships" : "lands");
145 if (type == EF_SHIP && strlen(pathtaken) > 1) {
146 pathtaken[strlen(pathtaken) - 1] = '\0';
147 pr("Path taken: %s\n", pathtaken);
151 leader = get_leader(ulist);
152 if (leader->uid != leader_uid) {
153 leader_uid = leader->uid;
154 pr_leader_change(leader);
158 if (!(cp = unit_path(*together, leader, buf)))
161 if (type == EF_SHIP) {
162 radmapnopr(leader->x, leader->y, (int)leader->effic,
163 (int)techfact(leader->tech,
164 mchr[(int)leader->type].m_vrnge),
165 (mchr[(int)leader->type].m_flags & M_SONAR)
166 ? techfact(leader->tech, 1.0) : 0.0);
168 if (cp == NULL || *cp == '\0')
169 cp = &dirch[DIR_STOP];
170 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
172 if (type == EF_SHIP) {
173 stopping |= shp_nav_one_sector(ulist, dir,
174 player->cnum, *together);
181 lnd_mar_one_sector(ulist, dir, player->cnum,
186 ac = parse(cp, scanspace, player->argp, NULL, NULL, NULL);
188 player->argp[0] = "";
190 } else if (ac == 1) {
191 sprintf(dp, "%d", leader->uid);
192 player->argp[1] = dp;
197 switch (*player->argp[0]) {
204 do_map(bmap_flag, leader->ef_type, player->argp[1],
210 switch_leader(ulist, -1);
212 switch_leader(ulist, atoi(player->argp[1]));
213 leader = get_leader(ulist);
214 if (leader->uid != leader_uid) {
215 leader_uid = leader->uid;
216 pr_leader_change(leader);
224 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
226 lnd_sweep(ulist, 1, 1, player->cnum);
227 stopping |= lnd_check_mines(ulist);
231 radar(leader->ef_type);
240 if (leader->ef_type != EF_SHIP)
248 player->argp[2] = ac < 2 ? "1" : player->argp[1];
249 sprintf(dp, "%d", leader->uid);
250 player->argp[1] = dp;
264 direrr("`%c' to stop", ", `%c' to view", NULL);
265 pr(", `i' to list %s, `f' to change %s,\n",
266 type == EF_SHIP ? "ships" : "units",
267 type == EF_SHIP ? "flagship" : "leader");
268 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
269 type == EF_SHIP ? "`s' to sonar, " : "");
270 pr("`d' to drop mines, and `m' to minesweep\n");
273 if (type == EF_SHIP && strlen(pathtaken) > 1) {
274 pathtaken[strlen(pathtaken) - 1] = '\0';
275 pr("Path taken: %s\n", pathtaken);
281 nav_map(int x, int y, int show_designations)
287 /* Note this is not re-entrant anyway, so we keep the buffers
289 static char *wmapbuf = NULL;
290 static char **wmap = NULL;
294 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
296 wmap = malloc(WORLD_Y * sizeof(*wmap));
297 if (wmap && wmapbuf) {
298 for (i = 0; i < WORLD_Y; i++)
299 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
305 if (!wmapbuf || !wmap) {
306 pr("Memory error, tell the deity.\n");
307 logerror("malloc failed in navi\n");
310 snxtsct_dist(&ns, x, y, 1);
311 blankfill(wmapbuf, &ns.range, 1);
312 while (nxtsct(&ns, §)) {
313 ptr = &wmap[ns.dy][ns.dx];
314 *ptr = dchr[sect.sct_type].d_mnem;
315 if (!show_designations &&
316 sect.sct_own != player->cnum &&
317 sect.sct_type != SCT_WATER &&
318 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
320 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
322 * We do it this way so that 'x' and 'X'
323 * bdesignations will show up. This can
324 * be used to mark mined sectors. So, the
325 * player will see the current des, UNLESS
326 * they've marked the sector 'x' or 'X',
327 * in which case they'll see that.
330 *ptr = player->bmap[sect.sct_uid];
333 writemap(player->cnum);
334 for (i = 0; i < ns.range.height; i++)
340 pr_leader_change(struct empobj *leader)
342 pr("Changing %s to %s\n",
343 leader->ef_type == EF_SHIP ? "flagship" : "leader",
347 static struct empobj *
348 get_leader(struct emp_qelem *list)
350 return &((struct ulist *)(list->q_back))->unit.gen;
354 switch_leader(struct emp_qelem *list, int uid)
356 struct emp_qelem *qp, *save;
362 save = qp = list->q_back;
365 emp_insque(qp, list);
367 ulp = (struct ulist *)qp;
368 if (ulp->unit.gen.uid == uid || uid == -1)
370 } while (list->q_back != save);