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;
80 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
86 leader = get_leader(ulist);
87 leader_uid = leader->uid;
88 type = leader->ef_type;
90 type == EF_SHIP ? "Flagship" : "Leader",
93 if (player->argp[2]) {
94 strcpy(buf, player->argp[2]);
95 cp = unit_path(*together, leader, buf);
99 while (!QEMPTY(ulist)) {
102 if (cp == NULL || *cp == '\0' || stopping) {
105 shp_nav(ulist, minmob, maxmob, together, player->cnum);
107 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
109 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
110 if (type == EF_SHIP && strlen(pathtaken) > 1) {
111 pathtaken[strlen(pathtaken) - 1] = '\0';
112 pr("Path taken: %s\n", pathtaken);
116 leader = get_leader(ulist);
117 if (leader->uid != leader_uid) {
118 leader_uid = leader->uid;
119 pr_leader_change(leader);
124 nav_map(leader->x, leader->y,
126 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
129 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
130 *minmob, xyas(leader->x, leader->y, player->cnum));
131 cp = getstring(prompt, buf);
132 /* Just in case any of our units were shelled while we were
133 * at the prompt, we call shp_nav() or lnd_mar() again.
136 shp_nav(ulist, minmob, maxmob, together, player->cnum);
138 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
140 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
141 if (type == EF_SHIP && strlen(pathtaken) > 1) {
142 pathtaken[strlen(pathtaken) - 1] = '\0';
143 pr("Path taken: %s\n", pathtaken);
147 leader = get_leader(ulist);
148 if (leader->uid != leader_uid) {
149 leader_uid = leader->uid;
150 pr_leader_change(leader);
155 cp = unit_path(*together, leader, cp);
157 if (type == EF_SHIP) {
158 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
159 leader->tech, mchr[leader->type].m_vrnge);
161 if (cp == NULL || *cp == '\0')
162 cp = &dirch[DIR_STOP];
163 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
165 if (type == EF_SHIP) {
166 stopping |= shp_nav_one_sector(ulist, dir,
167 player->cnum, *together);
174 lnd_mar_one_sector(ulist, dir, player->cnum,
179 ac = parse(cp, scanspace, player->argp, NULL, NULL, NULL);
181 player->argp[0] = "";
183 } else if (ac == 1) {
184 sprintf(dp, "%d", leader->uid);
185 player->argp[1] = dp;
190 switch (*player->argp[0]) {
197 display_region_map(bmap_flag, type, leader->x, leader->y,
198 player->argp[1], player->argp[2]);
203 switch_leader(ulist, -1);
205 switch_leader(ulist, atoi(player->argp[1]));
206 leader = get_leader(ulist);
207 if (leader->uid != leader_uid) {
208 leader_uid = leader->uid;
209 pr_leader_change(leader);
217 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
219 lnd_sweep(ulist, 1, 1, player->cnum);
220 stopping |= lnd_check_mines(ulist);
224 radar(leader->ef_type);
233 if (leader->ef_type != EF_SHIP)
241 player->argp[2] = ac < 2 ? "1" : player->argp[1];
242 sprintf(dp, "%d", leader->uid);
243 player->argp[1] = dp;
257 direrr("`%c' to stop", ", `%c' to view", NULL);
258 pr(", `i' to list %s, `f' to change %s,\n",
259 type == EF_SHIP ? "ships" : "units",
260 type == EF_SHIP ? "flagship" : "leader");
261 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
262 type == EF_SHIP ? "`s' to sonar, " : "");
263 pr("`d' to drop mines, and `m' to minesweep\n");
266 if (type == EF_SHIP && strlen(pathtaken) > 1) {
267 pathtaken[strlen(pathtaken) - 1] = '\0';
268 pr("Path taken: %s\n", pathtaken);
274 nav_map(int x, int y, int show_designations)
280 /* Note this is not re-entrant anyway, so we keep the buffers
282 static char *wmapbuf = NULL;
283 static char **wmap = NULL;
287 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
289 wmap = malloc(WORLD_Y * sizeof(*wmap));
290 if (wmap && wmapbuf) {
291 for (i = 0; i < WORLD_Y; i++)
292 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
298 if (!wmapbuf || !wmap) {
299 pr("Memory error, tell the deity.\n");
300 logerror("malloc failed in navi\n");
303 snxtsct_dist(&ns, x, y, 1);
304 blankfill(wmapbuf, &ns.range, 1);
305 while (nxtsct(&ns, §)) {
306 ptr = &wmap[ns.dy][ns.dx];
307 *ptr = dchr[sect.sct_type].d_mnem;
308 if (!show_designations &&
309 sect.sct_own != player->cnum &&
310 sect.sct_type != SCT_WATER &&
311 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
313 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
315 * We do it this way so that 'x' and 'X'
316 * bdesignations will show up. This can
317 * be used to mark mined sectors. So, the
318 * player will see the current des, UNLESS
319 * they've marked the sector 'x' or 'X',
320 * in which case they'll see that.
323 *ptr = player->bmap[sect.sct_uid];
326 writemap(player->cnum);
327 for (i = 0; i < ns.range.height; i++)
333 pr_leader_change(struct empobj *leader)
335 pr("Changing %s to %s\n",
336 leader->ef_type == EF_SHIP ? "flagship" : "leader",
337 unit_nameof(leader));
340 static struct empobj *
341 get_leader(struct emp_qelem *list)
343 return &((struct ulist *)(list->q_back))->unit.gen;
347 switch_leader(struct emp_qelem *list, int uid)
349 struct emp_qelem *qp, *save;
355 save = qp = list->q_back;
358 emp_insque(qp, list);
360 ulp = (struct ulist *)qp;
361 if (ulp->unit.gen.uid == uid || uid == -1)
363 } while (list->q_back != save);