2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * navi.c: Navigate ships and such
29 * Known contributors to this file:
30 * Ken Stevens, 1995 (rewritten)
31 * Ron Koenderink, 2006-2007
32 * Markus Armbruster, 2006-2014
43 static void pr_leader_change(struct empobj *leader);
44 static struct empobj *get_leader(struct emp_qelem *list);
45 static void switch_leader(struct emp_qelem *list, int uid);
50 struct nstr_item ni_ship;
51 struct emp_qelem ship_list;
52 double minmob, maxmob;
54 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
56 shp_sel(&ni_ship, &ship_list);
57 shp_nav(&ship_list, &minmob, &maxmob, player->cnum);
58 if (QEMPTY(&ship_list)) {
62 return do_unit_move(&ship_list, &minmob, &maxmob);
66 do_unit_move(struct emp_qelem *ulist, double *minmob, double *maxmob)
70 struct empobj *leader;
81 leader = get_leader(ulist);
82 leader_uid = leader->uid;
83 type = leader->ef_type;
85 type == EF_SHIP ? "Flagship" : "Leader",
88 if (player->argp[2]) {
89 strcpy(buf, player->argp[2]);
90 cp = unit_path(leader, buf, sizeof(buf));
93 while (!QEMPTY(ulist)) {
96 if (cp == NULL || *cp == '\0' || stopping) {
99 shp_nav(ulist, minmob, maxmob, player->cnum);
101 lnd_mar(ulist, minmob, maxmob, player->cnum);
103 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
106 leader = get_leader(ulist);
107 if (leader->uid != leader_uid) {
108 leader_uid = leader->uid;
109 pr_leader_change(leader);
114 nav_map(leader->x, leader->y,
116 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
119 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
120 *minmob, xyas(leader->x, leader->y, player->cnum));
121 cp = getstring(prompt, buf);
122 /* Just in case any of our units were shelled while we were
123 * at the prompt, we call shp_nav() or lnd_mar() again.
126 shp_nav(ulist, minmob, maxmob, player->cnum);
128 lnd_mar(ulist, minmob, maxmob, player->cnum);
130 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
133 leader = get_leader(ulist);
134 if (leader->uid != leader_uid) {
135 leader_uid = leader->uid;
136 pr_leader_change(leader);
141 cp = unit_path(leader, buf, sizeof(buf));
143 if (type == EF_SHIP) {
144 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
145 leader->tech, mchr[leader->type].m_vrnge);
147 if (cp == NULL || *cp == '\0')
148 cp = &dirch[DIR_STOP];
149 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
152 stopping |= shp_nav_one_sector(ulist, dir, player->cnum);
154 if (!moved && !lnd_abandon_askyn(ulist))
156 stopping |= lnd_mar_one_sector(ulist, dir, player->cnum);
162 ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
164 player->argp[0] = "";
166 } else if (ac == 1) {
167 sprintf(dp, "%d", leader->uid);
168 player->argp[1] = dp;
173 switch (*player->argp[0]) {
180 display_region_map(bmap_flag, type, leader->x, leader->y,
181 player->argp[1], player->argp[2]);
186 switch_leader(ulist, -1);
188 switch_leader(ulist, atoi(player->argp[1]));
189 leader = get_leader(ulist);
190 if (leader->uid != leader_uid) {
191 leader_uid = leader->uid;
192 pr_leader_change(leader);
200 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
202 stopping |= lnd_sweep(ulist, 1, 1, player->cnum);
206 radar(leader->ef_type);
215 if (leader->ef_type != EF_SHIP)
223 player->argp[2] = ac < 2 ? "1" : player->argp[1];
224 sprintf(dp, "%d", leader->uid);
225 player->argp[1] = dp;
239 direrr("`%c' to stop", ", `%c' to view", NULL);
240 pr(", `i' to list %s, `f' to change %s,\n",
241 type == EF_SHIP ? "ships" : "units",
242 type == EF_SHIP ? "flagship" : "leader");
243 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
244 type == EF_SHIP ? "`s' to sonar, " : "");
245 pr("`d' to drop mines, and `m' to minesweep\n");
252 nav_map(int x, int y, int show_designations)
258 /* Note this is not re-entrant anyway, so we keep the buffers
260 static char *wmapbuf = NULL;
261 static char **wmap = NULL;
265 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
267 wmap = malloc(WORLD_Y * sizeof(*wmap));
268 if (wmap && wmapbuf) {
269 for (i = 0; i < WORLD_Y; i++)
270 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
276 if (!wmapbuf || !wmap) {
277 pr("Memory error, tell the deity.\n");
278 logerror("malloc failed in navi\n");
281 snxtsct_dist(&ns, x, y, 1);
282 blankfill(wmapbuf, &ns.range, 1);
283 while (nxtsct(&ns, §)) {
284 ptr = &wmap[ns.dy][ns.dx];
285 *ptr = dchr[sect.sct_type].d_mnem;
286 if (!show_designations &&
287 sect.sct_own != player->cnum &&
288 sect.sct_type != SCT_WATER &&
289 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
291 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
293 * We do it this way so that 'x' and 'X'
294 * bdesignations will show up. This can
295 * be used to mark mined sectors. So, the
296 * player will see the current des, UNLESS
297 * they've marked the sector 'x' or 'X',
298 * in which case they'll see that.
301 *ptr = player->bmap[sect.sct_uid];
304 writemap(player->cnum);
305 for (i = 0; i < ns.range.height; i++)
311 pr_leader_change(struct empobj *leader)
313 pr("Changing %s to %s\n",
314 leader->ef_type == EF_SHIP ? "flagship" : "leader",
315 unit_nameof(leader));
318 static struct empobj *
319 get_leader(struct emp_qelem *list)
321 return &((struct ulist *)(list->q_back))->unit.gen;
325 switch_leader(struct emp_qelem *list, int uid)
327 struct emp_qelem *qp, *save;
333 save = qp = list->q_back;
336 emp_insque(qp, list);
338 ulp = (struct ulist *)qp;
339 if (ulp->unit.gen.uid == uid || uid == -1)
341 } while (list->q_back != save);