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 * path.c: Routines associated with paths, directions, etc.
29 * Known contributors to this file:
30 * Markus Armbruster, 2005-2009
39 #include "prototypes.h"
43 chkdir(char dir_char, int min_dir, int max_dir)
47 for (i = min_dir; i <= max_dir; i++)
48 if (dir_char == dirch[i])
54 direrr(char *stop_msg, char *view_msg, char *map_msg)
56 pr("Legal directions are:\n");
57 pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
58 pr("%c %c\n", dirch[DIR_L], dirch[DIR_R]);
59 pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
61 pr(stop_msg, dirch[DIR_STOP]);
63 pr(view_msg, dirch[DIR_VIEW]);
65 pr(map_msg, dirch[DIR_MAP]);
69 * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
70 * DIR must be a valid direction.
75 unsigned i = dir - 'a';
77 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
84 * return pointer to path; prompt user until a stop char
85 * or a bomb char have been entered. A "bomb" char in
86 * this context is actually "execute" for the partial
87 * move commands, and isn't valid for those commands
88 * which do not accept partial moves.
91 getpath(char *buf, char *arg, coord x, coord y, int onlyown,
92 int showdes, enum p_mode destinations)
105 strncpy(buf, arg, MAX_PATH_LEN - 1);
106 buf[MAX_PATH_LEN - 1] = 0;
111 getsect(x, y, §);
115 if (sarg_xy(p, &dx, &dy)) {
117 switch (destinations) {
119 pr("Destination sectors not allowed here!\n");
122 if (path_find(x, y, dx, dy, 0, MOB_FLY) < 0)
125 len = path_find_route(buf2, 100, x, y, dx, dy);
136 if (path_find(x, y, dx, dy, player->cnum, MOB_SAIL) < 0)
139 len = path_find_route(buf2, 100, x, y, dx, dy);
150 if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
152 pr("Using best path '%s'\n", p);
153 pr("Using total path '%s'\n", buf);
156 pr("Can't get to %s from here!\n",
157 xyas(dx, dy, player->cnum));
161 dir = chkdir(*p, DIR_STOP, DIR_LAST);
163 pr("\"%c\" is not legal...", *p);
164 direrr("'%c' to stop\n", NULL, NULL);
167 nx = x + diroff[dir][0];
168 ny = y + diroff[dir][1];
169 getsect(nx, ny, §);
170 if (onlyown && sect.sct_own != player->cnum) {
171 pr("You don't own %s; you can't go there!\n",
172 xyas(nx, ny, player->cnum));
175 if (dir == DIR_STOP) {
185 getsect(x, y, §);
186 sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
187 xyas(x, y, player->cnum));
189 sprintf(prompt, "<%d: %s> ", (int)(p - buf),
190 xyas(x, y, player->cnum));
192 bp = getstring(prompt, buf2);
195 if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
196 pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
211 fcost(struct sctstr *sp, natid own)
221 ncost(struct sctstr *sp, natid own)
227 * return end x,y of path, and the base
228 * movement cost it takes to get there.
231 pathtoxy(char *path, coord *xp, coord *yp,
232 double (*cost)(struct sctstr *, natid))
244 for (pp = path; *pp; pp++) {
245 if ((val = diridx(*pp)) == DIR_STOP)
249 if (!getsect(x, y, &s))
251 m += cost(&s, s.sct_own);
259 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
261 int dir, lx, ly, hx, hy;
269 cx += diroff[dir][0];
270 cy += diroff[dir][1];
286 range->lx = xnorm(lx);
287 range->hx = xnorm(hx - lx < WORLD_X ? hx : lx - 1);
288 range->ly = ynorm(ly);
289 range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);