2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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;
55 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
57 shp_sel(&ni_ship, &ship_list);
58 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
59 if (QEMPTY(&ship_list)) {
64 return do_unit_move(&ship_list, &together, &minmob, &maxmob);
68 do_unit_move(struct emp_qelem *ulist, int *together,
69 double *minmob, double *maxmob)
73 struct empobj *leader;
84 leader = get_leader(ulist);
85 leader_uid = leader->uid;
86 type = leader->ef_type;
88 type == EF_SHIP ? "Flagship" : "Leader",
91 if (player->argp[2]) {
92 strcpy(buf, player->argp[2]);
93 cp = unit_path(*together, leader, buf, sizeof(buf));
96 while (!QEMPTY(ulist)) {
99 if (cp == NULL || *cp == '\0' || stopping) {
102 shp_nav(ulist, minmob, maxmob, together, player->cnum);
104 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
106 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
109 leader = get_leader(ulist);
110 if (leader->uid != leader_uid) {
111 leader_uid = leader->uid;
112 pr_leader_change(leader);
117 nav_map(leader->x, leader->y,
119 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
122 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
123 *minmob, xyas(leader->x, leader->y, player->cnum));
124 cp = getstring(prompt, buf);
125 /* Just in case any of our units were shelled while we were
126 * at the prompt, we call shp_nav() or lnd_mar() again.
129 shp_nav(ulist, minmob, maxmob, together, player->cnum);
131 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
133 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
136 leader = get_leader(ulist);
137 if (leader->uid != leader_uid) {
138 leader_uid = leader->uid;
139 pr_leader_change(leader);
144 cp = unit_path(*together, leader, buf, sizeof(buf));
146 if (type == EF_SHIP) {
147 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
148 leader->tech, mchr[leader->type].m_vrnge);
150 if (cp == NULL || *cp == '\0')
151 cp = &dirch[DIR_STOP];
152 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
155 stopping |= shp_nav_one_sector(ulist, dir,
156 player->cnum, *together);
158 if (!moved && !lnd_abandon_askyn(ulist))
161 lnd_mar_one_sector(ulist, dir, player->cnum,
168 ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
170 player->argp[0] = "";
172 } else if (ac == 1) {
173 sprintf(dp, "%d", leader->uid);
174 player->argp[1] = dp;
179 switch (*player->argp[0]) {
186 display_region_map(bmap_flag, type, leader->x, leader->y,
187 player->argp[1], player->argp[2]);
192 switch_leader(ulist, -1);
194 switch_leader(ulist, atoi(player->argp[1]));
195 leader = get_leader(ulist);
196 if (leader->uid != leader_uid) {
197 leader_uid = leader->uid;
198 pr_leader_change(leader);
206 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
208 lnd_sweep(ulist, 1, 1, player->cnum);
209 stopping |= lnd_check_mines(ulist);
213 radar(leader->ef_type);
222 if (leader->ef_type != EF_SHIP)
230 player->argp[2] = ac < 2 ? "1" : player->argp[1];
231 sprintf(dp, "%d", leader->uid);
232 player->argp[1] = dp;
246 direrr("`%c' to stop", ", `%c' to view", NULL);
247 pr(", `i' to list %s, `f' to change %s,\n",
248 type == EF_SHIP ? "ships" : "units",
249 type == EF_SHIP ? "flagship" : "leader");
250 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
251 type == EF_SHIP ? "`s' to sonar, " : "");
252 pr("`d' to drop mines, and `m' to minesweep\n");
259 nav_map(int x, int y, int show_designations)
265 /* Note this is not re-entrant anyway, so we keep the buffers
267 static char *wmapbuf = NULL;
268 static char **wmap = NULL;
272 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
274 wmap = malloc(WORLD_Y * sizeof(*wmap));
275 if (wmap && wmapbuf) {
276 for (i = 0; i < WORLD_Y; i++)
277 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
283 if (!wmapbuf || !wmap) {
284 pr("Memory error, tell the deity.\n");
285 logerror("malloc failed in navi\n");
288 snxtsct_dist(&ns, x, y, 1);
289 blankfill(wmapbuf, &ns.range, 1);
290 while (nxtsct(&ns, §)) {
291 ptr = &wmap[ns.dy][ns.dx];
292 *ptr = dchr[sect.sct_type].d_mnem;
293 if (!show_designations &&
294 sect.sct_own != player->cnum &&
295 sect.sct_type != SCT_WATER &&
296 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
298 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
300 * We do it this way so that 'x' and 'X'
301 * bdesignations will show up. This can
302 * be used to mark mined sectors. So, the
303 * player will see the current des, UNLESS
304 * they've marked the sector 'x' or 'X',
305 * in which case they'll see that.
308 *ptr = player->bmap[sect.sct_uid];
311 writemap(player->cnum);
312 for (i = 0; i < ns.range.height; i++)
318 pr_leader_change(struct empobj *leader)
320 pr("Changing %s to %s\n",
321 leader->ef_type == EF_SHIP ? "flagship" : "leader",
322 unit_nameof(leader));
325 static struct empobj *
326 get_leader(struct emp_qelem *list)
328 return &((struct ulist *)(list->q_back))->unit.gen;
332 switch_leader(struct emp_qelem *list, int uid)
334 struct emp_qelem *qp, *save;
340 save = qp = list->q_back;
343 emp_insque(qp, list);
345 ulp = (struct ulist *)qp;
346 if (ulp->unit.gen.uid == uid || uid == -1)
348 } while (list->q_back != save);