2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2011
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;
79 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
85 leader = get_leader(ulist);
86 leader_uid = leader->uid;
87 type = leader->ef_type;
89 type == EF_SHIP ? "Flagship" : "Leader",
92 if (player->argp[2]) {
93 strcpy(buf, player->argp[2]);
94 cp = unit_path(*together, leader, buf);
98 while (!QEMPTY(ulist)) {
101 if (cp == NULL || *cp == '\0' || stopping) {
104 shp_nav(ulist, minmob, maxmob, together, player->cnum);
106 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
108 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
109 if (type == EF_SHIP && strlen(pathtaken) > 1) {
110 pathtaken[strlen(pathtaken) - 1] = '\0';
111 pr("Path taken: %s\n", pathtaken);
115 leader = get_leader(ulist);
116 if (leader->uid != leader_uid) {
117 leader_uid = leader->uid;
118 pr_leader_change(leader);
123 nav_map(leader->x, leader->y,
125 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
128 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
129 *minmob, xyas(leader->x, leader->y, player->cnum));
130 cp = getstring(prompt, buf);
131 /* Just in case any of our units were shelled while we were
132 * at the prompt, we call shp_nav() or lnd_mar() again.
135 shp_nav(ulist, minmob, maxmob, together, player->cnum);
137 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
139 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
140 if (type == EF_SHIP && strlen(pathtaken) > 1) {
141 pathtaken[strlen(pathtaken) - 1] = '\0';
142 pr("Path taken: %s\n", pathtaken);
146 leader = get_leader(ulist);
147 if (leader->uid != leader_uid) {
148 leader_uid = leader->uid;
149 pr_leader_change(leader);
154 cp = unit_path(*together, leader, cp);
156 if (type == EF_SHIP) {
157 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
158 leader->tech, mchr[leader->type].m_vrnge);
160 if (cp == NULL || *cp == '\0')
161 cp = &dirch[DIR_STOP];
162 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
164 if (type == EF_SHIP) {
165 stopping |= shp_nav_one_sector(ulist, dir,
166 player->cnum, *together);
173 lnd_mar_one_sector(ulist, dir, player->cnum,
178 ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
180 player->argp[0] = "";
182 } else if (ac == 1) {
183 sprintf(dp, "%d", leader->uid);
184 player->argp[1] = dp;
189 switch (*player->argp[0]) {
196 display_region_map(bmap_flag, type, leader->x, leader->y,
197 player->argp[1], player->argp[2]);
202 switch_leader(ulist, -1);
204 switch_leader(ulist, atoi(player->argp[1]));
205 leader = get_leader(ulist);
206 if (leader->uid != leader_uid) {
207 leader_uid = leader->uid;
208 pr_leader_change(leader);
216 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
218 lnd_sweep(ulist, 1, 1, player->cnum);
219 stopping |= lnd_check_mines(ulist);
223 radar(leader->ef_type);
232 if (leader->ef_type != EF_SHIP)
240 player->argp[2] = ac < 2 ? "1" : player->argp[1];
241 sprintf(dp, "%d", leader->uid);
242 player->argp[1] = dp;
256 direrr("`%c' to stop", ", `%c' to view", NULL);
257 pr(", `i' to list %s, `f' to change %s,\n",
258 type == EF_SHIP ? "ships" : "units",
259 type == EF_SHIP ? "flagship" : "leader");
260 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
261 type == EF_SHIP ? "`s' to sonar, " : "");
262 pr("`d' to drop mines, and `m' to minesweep\n");
265 if (type == EF_SHIP && strlen(pathtaken) > 1) {
266 pathtaken[strlen(pathtaken) - 1] = '\0';
267 pr("Path taken: %s\n", pathtaken);
273 nav_map(int x, int y, int show_designations)
279 /* Note this is not re-entrant anyway, so we keep the buffers
281 static char *wmapbuf = NULL;
282 static char **wmap = NULL;
286 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
288 wmap = malloc(WORLD_Y * sizeof(*wmap));
289 if (wmap && wmapbuf) {
290 for (i = 0; i < WORLD_Y; i++)
291 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
297 if (!wmapbuf || !wmap) {
298 pr("Memory error, tell the deity.\n");
299 logerror("malloc failed in navi\n");
302 snxtsct_dist(&ns, x, y, 1);
303 blankfill(wmapbuf, &ns.range, 1);
304 while (nxtsct(&ns, §)) {
305 ptr = &wmap[ns.dy][ns.dx];
306 *ptr = dchr[sect.sct_type].d_mnem;
307 if (!show_designations &&
308 sect.sct_own != player->cnum &&
309 sect.sct_type != SCT_WATER &&
310 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
312 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
314 * We do it this way so that 'x' and 'X'
315 * bdesignations will show up. This can
316 * be used to mark mined sectors. So, the
317 * player will see the current des, UNLESS
318 * they've marked the sector 'x' or 'X',
319 * in which case they'll see that.
322 *ptr = player->bmap[sect.sct_uid];
325 writemap(player->cnum);
326 for (i = 0; i < ns.range.height; i++)
332 pr_leader_change(struct empobj *leader)
334 pr("Changing %s to %s\n",
335 leader->ef_type == EF_SHIP ? "flagship" : "leader",
336 unit_nameof(leader));
339 static struct empobj *
340 get_leader(struct emp_qelem *list)
342 return &((struct ulist *)(list->q_back))->unit.gen;
346 switch_leader(struct emp_qelem *list, int uid)
348 struct emp_qelem *qp, *save;
354 save = qp = list->q_back;
357 emp_insque(qp, list);
359 ulp = (struct ulist *)qp;
360 if (ulp->unit.gen.uid == uid || uid == -1)
362 } while (list->q_back != save);