2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * navi.c: Navigate ships and such
30 * Known contributors to this file:
31 * Ken Stevens, 1995 (rewritten)
32 * Ron Koenderink, 2006-2007
44 static void pr_leader_change(struct empobj *leader);
45 static struct empobj *get_leader(struct emp_qelem *list);
46 static void switch_leader(struct emp_qelem *list, int uid);
51 struct nstr_item ni_ship;
52 struct emp_qelem ship_list;
53 double minmob, maxmob;
56 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
58 shp_sel(&ni_ship, &ship_list);
59 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
60 if (QEMPTY(&ship_list)) {
65 return do_unit_move(&ship_list, &together, &minmob, &maxmob);
69 do_unit_move(struct emp_qelem *ulist, int *together,
70 double *minmob, double *maxmob)
74 struct empobj *leader;
81 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
87 leader = get_leader(ulist);
88 leader_uid = leader->uid;
89 type = leader->ef_type;
91 type == EF_SHIP ? "Flagship" : "Leader",
94 if (player->argp[2]) {
95 strcpy(buf, player->argp[2]);
96 if (!(cp = unit_path(*together, leader, buf)))
101 while (!QEMPTY(ulist)) {
104 if (cp == NULL || *cp == '\0' || stopping) {
107 shp_nav(ulist, minmob, maxmob, together, player->cnum);
109 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
111 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
112 if (type == EF_SHIP && strlen(pathtaken) > 1) {
113 pathtaken[strlen(pathtaken) - 1] = '\0';
114 pr("Path taken: %s\n", pathtaken);
118 leader = get_leader(ulist);
119 if (leader->uid != leader_uid) {
120 leader_uid = leader->uid;
121 pr_leader_change(leader);
126 nav_map(leader->x, leader->y,
128 ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
131 sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
132 *minmob, xyas(leader->x, leader->y, player->cnum));
133 cp = getstring(prompt, buf);
134 /* Just in case any of our units were shelled while we were
135 * at the prompt, we call shp_nav() or lnd_mar() again.
138 shp_nav(ulist, minmob, maxmob, together, player->cnum);
140 lnd_mar(ulist, minmob, maxmob, together, player->cnum);
142 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
143 if (type == EF_SHIP && strlen(pathtaken) > 1) {
144 pathtaken[strlen(pathtaken) - 1] = '\0';
145 pr("Path taken: %s\n", pathtaken);
149 leader = get_leader(ulist);
150 if (leader->uid != leader_uid) {
151 leader_uid = leader->uid;
152 pr_leader_change(leader);
156 if (!(cp = unit_path(*together, leader, buf)))
159 if (type == EF_SHIP) {
160 rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
161 leader->tech, mchr[leader->type].m_vrnge);
163 if (cp == NULL || *cp == '\0')
164 cp = &dirch[DIR_STOP];
165 dir = chkdir(*cp, DIR_STOP, DIR_LAST);
167 if (type == EF_SHIP) {
168 stopping |= shp_nav_one_sector(ulist, dir,
169 player->cnum, *together);
176 lnd_mar_one_sector(ulist, dir, player->cnum,
181 ac = parse(cp, scanspace, player->argp, NULL, NULL, NULL);
183 player->argp[0] = "";
185 } else if (ac == 1) {
186 sprintf(dp, "%d", leader->uid);
187 player->argp[1] = dp;
192 switch (*player->argp[0]) {
199 do_map(bmap_flag, leader->ef_type, player->argp[1],
205 switch_leader(ulist, -1);
207 switch_leader(ulist, atoi(player->argp[1]));
208 leader = get_leader(ulist);
209 if (leader->uid != leader_uid) {
210 leader_uid = leader->uid;
211 pr_leader_change(leader);
219 stopping |= shp_sweep(ulist, 1, 1, player->cnum);
221 lnd_sweep(ulist, 1, 1, player->cnum);
222 stopping |= lnd_check_mines(ulist);
226 radar(leader->ef_type);
235 if (leader->ef_type != EF_SHIP)
243 player->argp[2] = ac < 2 ? "1" : player->argp[1];
244 sprintf(dp, "%d", leader->uid);
245 player->argp[1] = dp;
259 direrr("`%c' to stop", ", `%c' to view", NULL);
260 pr(", `i' to list %s, `f' to change %s,\n",
261 type == EF_SHIP ? "ships" : "units",
262 type == EF_SHIP ? "flagship" : "leader");
263 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
264 type == EF_SHIP ? "`s' to sonar, " : "");
265 pr("`d' to drop mines, and `m' to minesweep\n");
268 if (type == EF_SHIP && strlen(pathtaken) > 1) {
269 pathtaken[strlen(pathtaken) - 1] = '\0';
270 pr("Path taken: %s\n", pathtaken);
276 nav_map(int x, int y, int show_designations)
282 /* Note this is not re-entrant anyway, so we keep the buffers
284 static char *wmapbuf = NULL;
285 static char **wmap = NULL;
289 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
291 wmap = malloc(WORLD_Y * sizeof(*wmap));
292 if (wmap && wmapbuf) {
293 for (i = 0; i < WORLD_Y; i++)
294 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
300 if (!wmapbuf || !wmap) {
301 pr("Memory error, tell the deity.\n");
302 logerror("malloc failed in navi\n");
305 snxtsct_dist(&ns, x, y, 1);
306 blankfill(wmapbuf, &ns.range, 1);
307 while (nxtsct(&ns, §)) {
308 ptr = &wmap[ns.dy][ns.dx];
309 *ptr = dchr[sect.sct_type].d_mnem;
310 if (!show_designations &&
311 sect.sct_own != player->cnum &&
312 sect.sct_type != SCT_WATER &&
313 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
315 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
317 * We do it this way so that 'x' and 'X'
318 * bdesignations will show up. This can
319 * be used to mark mined sectors. So, the
320 * player will see the current des, UNLESS
321 * they've marked the sector 'x' or 'X',
322 * in which case they'll see that.
325 *ptr = player->bmap[sect.sct_uid];
328 writemap(player->cnum);
329 for (i = 0; i < ns.range.height; i++)
335 pr_leader_change(struct empobj *leader)
337 pr("Changing %s to %s\n",
338 leader->ef_type == EF_SHIP ? "flagship" : "leader",
342 static struct empobj *
343 get_leader(struct emp_qelem *list)
345 return &((struct ulist *)(list->q_back))->unit.gen;
349 switch_leader(struct emp_qelem *list, int uid)
351 struct emp_qelem *qp, *save;
357 save = qp = list->q_back;
360 emp_insque(qp, list);
362 ulp = (struct ulist *)qp;
363 if (ulp->unit.gen.uid == uid || uid == -1)
365 } while (list->q_back != save);