2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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
45 static void pr_leader_change(struct empobj *leader);
46 static struct empobj *get_leader(struct emp_qelem *list);
47 static void switch_leader(struct emp_qelem *list, int uid);
52 struct nstr_item ni_ship;
53 struct emp_qelem ship_list;
54 double minmob, maxmob;
57 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
59 shp_sel(&ni_ship, &ship_list);
60 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
61 if (QEMPTY(&ship_list)) {
66 return do_unit_move(&ship_list, &together, &minmob, &maxmob);
70 do_unit_move(struct emp_qelem *ulist, int *together,
71 double *minmob, double *maxmob)
75 struct empobj *leader;
82 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
88 leader = get_leader(ulist);
89 leader_uid = leader->uid;
90 type = leader->ef_type;
92 type == EF_SHIP ? "Flagship" : "Leader",
95 if (player->argp[2]) {
96 strcpy(buf, player->argp[2]);
97 if (type == EF_SHIP) {
98 if (!(cp = shp_path(*together, (struct shpstr *)leader, buf)))
101 if (!(cp = lnd_path(*together, (struct lndstr *)leader, buf)))
102 cp = player->argp[2];
107 while (!QEMPTY(ulist)) {
110 if (cp == NULL || *cp == '\0' || stopping) {
113 shp_nav(ulist, minmob, maxmob, together, player->cnum);
115 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
118 type == EF_SHIP ? "ships" : "lands");
119 if (type == EF_SHIP && strlen(pathtaken) > 1) {
120 pathtaken[strlen(pathtaken) - 1] = '\0';
121 pr("Path taken: %s\n", pathtaken);
125 leader = get_leader(ulist);
126 if (leader->uid != leader_uid) {
127 leader_uid = leader->uid;
128 pr_leader_change(leader);
133 nav_map(leader->x, leader->y,
135 !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
138 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
139 *minmob, xyas(leader->x, leader->y, player->cnum));
140 cp = getstring(prompt, buf);
141 /* Just in case any of our units were shelled while we were
142 * at the prompt, we call shp_nav() or lnd_mar() again.
145 shp_nav(ulist, minmob, maxmob, together, player->cnum);
147 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
150 type == EF_SHIP ? "ships" : "lands");
151 if (type == EF_SHIP && strlen(pathtaken) > 1) {
152 pathtaken[strlen(pathtaken) - 1] = '\0';
153 pr("Path taken: %s\n", pathtaken);
157 leader = get_leader(ulist);
158 if (leader->uid != leader_uid) {
159 leader_uid = leader->uid;
160 pr_leader_change(leader);
164 if (type == EF_SHIP) {
165 if (!(cp = shp_path(*together, (struct shpstr *)leader,
169 if (!(cp = lnd_path(*together, (struct lndstr *)leader,
174 if (type == EF_SHIP) {
175 radmapnopr(leader->x, leader->y, (int)leader->effic,
176 (int)techfact(leader->tech,
177 mchr[(int)leader->type].m_vrnge),
178 (mchr[(int)leader->type].m_flags & M_SONAR)
179 ? techfact(leader->tech, 1.0) : 0.0);
181 if (cp == NULL || *cp == '\0')
182 cp = &dirch[DIR_STOP];
183 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
185 if (type == EF_SHIP) {
186 stopping |= shp_nav_one_sector(ulist, dir,
187 player->cnum, *together);
194 lnd_mar_one_sector(ulist, dir, player->cnum,
199 ac = parse(cp, player->argp, NULL, scanspace, NULL);
201 sprintf(dp, "%d", leader->uid);
202 player->argp[1] = dp;
207 switch (*player->argp[0]) {
214 do_map(bmap_flag, leader->ef_type, player->argp[1],
220 switch_leader(ulist, -1);
222 switch_leader(ulist, atoi(player->argp[1]));
223 leader = get_leader(ulist);
224 if (leader->uid != leader_uid) {
225 leader_uid = leader->uid;
226 pr_leader_change(leader);
234 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
236 lnd_sweep(ulist, 1, 1, player->cnum);
237 stopping |= lnd_check_mines(ulist);
241 radar(leader->ef_type);
250 if (leader->ef_type != EF_SHIP)
258 player->argp[2] = player->argp[1];
259 sprintf(dp, "%d", leader->uid);
260 player->argp[1] = dp;
270 if (leader->ef_type != EF_SHIP)
275 direrr("`%c' to stop",
276 type == EF_SHIP ? ", `%c' to view" : NULL, NULL);
277 pr(", `i' to list %s, `f' to change %s,\n",
278 type == EF_SHIP ? "ships" : "units",
279 type == EF_SHIP ? "flagship" : "leader");
280 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
281 type == EF_SHIP ? "`s' to sonar, " : "");
282 pr("`d' to drop mines, and `m' to minesweep\n");
285 if (type == EF_SHIP && strlen(pathtaken) > 1) {
286 pathtaken[strlen(pathtaken) - 1] = '\0';
287 pr("Path taken: %s\n", pathtaken);
293 nav_map(int x, int y, int show_designations)
301 /* Note this is not re-entrant anyway, so we keep the buffers
303 static unsigned char *bitmap = NULL;
304 static char *wmapbuf = NULL;
305 static char **wmap = NULL;
309 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
311 wmap = malloc(WORLD_Y * sizeof(*wmap));
312 if (wmap && wmapbuf) {
313 for (i = 0; i < WORLD_Y; i++)
314 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
321 bitmap = malloc((WORLD_X * WORLD_Y) / 8);
322 if (!wmapbuf || !wmap || !bitmap) {
323 pr("Memory error, tell the deity.\n");
324 logerror("malloc failed in navi\n");
327 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
328 snxtsct_dist(&ns, x, y, 1);
329 np = getnatp(player->cnum);
330 xyrelrange(np, &ns.range, &range);
331 blankfill(wmapbuf, &ns.range, 1);
332 while (nxtsct(&ns, §)) {
333 ptr = &wmap[ns.dy][ns.dx];
334 *ptr = dchr[sect.sct_type].d_mnem;
335 if (!show_designations &&
336 sect.sct_own != player->cnum &&
337 sect.sct_type != SCT_WATER &&
338 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
340 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
342 * We do it this way so that 'x' and 'X'
343 * bdesignations will show up. This can
344 * be used to mark mined sectors. So, the
345 * player will see the current des, UNLESS
346 * they've marked the sector 'x' or 'X',
347 * in which case they'll see that.
350 *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
353 writemap(player->cnum);
354 for (i = 0; i < ns.range.height; i++)
360 pr_leader_change(struct empobj *leader)
362 pr("Changing %s to %s\n",
363 leader->ef_type == EF_SHIP ? "flagship" : "leader",
367 static struct empobj *
368 get_leader(struct emp_qelem *list)
370 return &((struct ulist *)(list->q_back))->unit.gen;
374 switch_leader(struct emp_qelem *list, int uid)
376 struct emp_qelem *qp, *save;
382 save = qp = list->q_back;
385 emp_insque(qp, list);
387 ulp = (struct ulist *)qp;
388 if (ulp->unit.gen.uid == uid || uid == -1)
390 } while (list->q_back != save);