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, player->cnum);
59 if (QEMPTY(&ship_list)) {
63 return do_unit_move(&ship_list, &together, &minmob, &maxmob);
67 do_unit_move(struct emp_qelem *ulist, int *together,
68 double *minmob, double *maxmob)
72 struct empobj *leader;
83 leader = get_leader(ulist);
84 leader_uid = leader->uid;
85 type = leader->ef_type;
87 type == EF_SHIP ? "Flagship" : "Leader",
90 if (player->argp[2]) {
91 strcpy(buf, player->argp[2]);
92 cp = unit_path(*together, leader, buf, sizeof(buf));
95 while (!QEMPTY(ulist)) {
98 if (cp == NULL || *cp == '\0' || stopping) {
101 shp_nav(ulist, minmob, maxmob, player->cnum);
103 lnd_mar(ulist, minmob, maxmob, player->cnum);
105 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
108 leader = get_leader(ulist);
109 if (leader->uid != leader_uid) {
110 leader_uid = leader->uid;
111 pr_leader_change(leader);
116 nav_map(leader->x, leader->y,
118 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
121 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
122 *minmob, xyas(leader->x, leader->y, player->cnum));
123 cp = getstring(prompt, buf);
124 /* Just in case any of our units were shelled while we were
125 * at the prompt, we call shp_nav() or lnd_mar() again.
128 shp_nav(ulist, minmob, maxmob, player->cnum);
130 lnd_mar(ulist, minmob, maxmob, player->cnum);
132 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
135 leader = get_leader(ulist);
136 if (leader->uid != leader_uid) {
137 leader_uid = leader->uid;
138 pr_leader_change(leader);
143 cp = unit_path(*together, leader, buf, sizeof(buf));
145 if (type == EF_SHIP) {
146 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
147 leader->tech, mchr[leader->type].m_vrnge);
149 if (cp == NULL || *cp == '\0')
150 cp = &dirch[DIR_STOP];
151 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
154 stopping |= shp_nav_one_sector(ulist, dir, player->cnum);
156 if (!moved && !lnd_abandon_askyn(ulist))
158 stopping |= lnd_mar_one_sector(ulist, dir, player->cnum);
164 ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
166 player->argp[0] = "";
168 } else if (ac == 1) {
169 sprintf(dp, "%d", leader->uid);
170 player->argp[1] = dp;
175 switch (*player->argp[0]) {
182 display_region_map(bmap_flag, type, leader->x, leader->y,
183 player->argp[1], player->argp[2]);
188 switch_leader(ulist, -1);
190 switch_leader(ulist, atoi(player->argp[1]));
191 leader = get_leader(ulist);
192 if (leader->uid != leader_uid) {
193 leader_uid = leader->uid;
194 pr_leader_change(leader);
202 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
204 lnd_sweep(ulist, 1, 1, player->cnum);
205 stopping |= lnd_check_mines(ulist);
209 radar(leader->ef_type);
218 if (leader->ef_type != EF_SHIP)
226 player->argp[2] = ac < 2 ? "1" : player->argp[1];
227 sprintf(dp, "%d", leader->uid);
228 player->argp[1] = dp;
242 direrr("`%c' to stop", ", `%c' to view", NULL);
243 pr(", `i' to list %s, `f' to change %s,\n",
244 type == EF_SHIP ? "ships" : "units",
245 type == EF_SHIP ? "flagship" : "leader");
246 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
247 type == EF_SHIP ? "`s' to sonar, " : "");
248 pr("`d' to drop mines, and `m' to minesweep\n");
255 nav_map(int x, int y, int show_designations)
261 /* Note this is not re-entrant anyway, so we keep the buffers
263 static char *wmapbuf = NULL;
264 static char **wmap = NULL;
268 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
270 wmap = malloc(WORLD_Y * sizeof(*wmap));
271 if (wmap && wmapbuf) {
272 for (i = 0; i < WORLD_Y; i++)
273 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
279 if (!wmapbuf || !wmap) {
280 pr("Memory error, tell the deity.\n");
281 logerror("malloc failed in navi\n");
284 snxtsct_dist(&ns, x, y, 1);
285 blankfill(wmapbuf, &ns.range, 1);
286 while (nxtsct(&ns, §)) {
287 ptr = &wmap[ns.dy][ns.dx];
288 *ptr = dchr[sect.sct_type].d_mnem;
289 if (!show_designations &&
290 sect.sct_own != player->cnum &&
291 sect.sct_type != SCT_WATER &&
292 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
294 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
296 * We do it this way so that 'x' and 'X'
297 * bdesignations will show up. This can
298 * be used to mark mined sectors. So, the
299 * player will see the current des, UNLESS
300 * they've marked the sector 'x' or 'X',
301 * in which case they'll see that.
304 *ptr = player->bmap[sect.sct_uid];
307 writemap(player->cnum);
308 for (i = 0; i < ns.range.height; i++)
314 pr_leader_change(struct empobj *leader)
316 pr("Changing %s to %s\n",
317 leader->ef_type == EF_SHIP ? "flagship" : "leader",
318 unit_nameof(leader));
321 static struct empobj *
322 get_leader(struct emp_qelem *list)
324 return &((struct ulist *)(list->q_back))->unit.gen;
328 switch_leader(struct emp_qelem *list, int uid)
330 struct emp_qelem *qp, *save;
336 save = qp = list->q_back;
339 emp_insque(qp, list);
341 ulp = (struct ulist *)qp;
342 if (ulp->unit.gen.uid == uid || uid == -1)
344 } while (list->q_back != save);