2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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 * path.c: Routines associated with paths, directions, etc.
30 * Known contributors to this file:
40 #include "prototypes.h"
44 chkdir(char dir_char, int min_dir, int max_dir)
48 for (i = min_dir; i <= max_dir; i++)
49 if (dir_char == dirch[i])
55 direrr(char *stop_msg, char *view_msg, char *map_msg)
57 pr("Legal directions are:\n");
58 pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
59 pr("%c %c\n", dirch[DIR_L], dirch[DIR_R]);
60 pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
62 pr(stop_msg, dirch[DIR_STOP]);
64 pr(view_msg, dirch[DIR_VIEW]);
66 pr(map_msg, dirch[DIR_MAP]);
70 * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
71 * DIR must be a valid direction.
76 unsigned i = dir - 'a';
78 if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
85 * return pointer to path; prompt user until a stop char
86 * or a bomb char have been entered. A "bomb" char in
87 * this context is actually "execute" for the partial
88 * move commands, and isn't valid for those commands
89 * which do not accept partial moves.
92 getpath(char *buf, char *arg, coord x, coord y, int onlyown,
93 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 bp = BestAirPath(buf2, x, y, dx, dy);
125 bp = BestShipPath(buf2, x, y, dx, dy, player->cnum);
128 if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
130 pr("Using best path '%s'\n", p);
131 pr("Using total path '%s'\n", buf);
134 pr("Can't get to %s from here!\n",
135 xyas(dx, dy, player->cnum));
139 dir = chkdir(*p, DIR_STOP, DIR_LAST);
141 pr("\"%c\" is not legal...", *p);
142 direrr("'%c' to stop\n", NULL, NULL);
145 nx = x + diroff[dir][0];
146 ny = y + diroff[dir][1];
147 getsect(nx, ny, §);
148 if (onlyown && sect.sct_own != player->cnum) {
149 pr("You don't own %s; you can't go there!\n",
150 xyas(nx, ny, player->cnum));
153 if (dir == DIR_STOP) {
163 getsect(x, y, §);
164 sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
165 xyas(x, y, player->cnum));
167 sprintf(prompt, "<%d: %s> ", (int)(p - buf),
168 xyas(x, y, player->cnum));
170 bp = getstring(prompt, buf2);
173 if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
174 pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
189 fcost(struct sctstr *sp, natid own)
199 ncost(struct sctstr *sp, natid own)
205 * return end x,y of path, and the base
206 * movement cost it takes to get there.
209 pathtoxy(char *path, coord *xp, coord *yp,
210 double (*cost)(struct sctstr *, natid))
222 for (pp = path; *pp; pp++) {
223 if ((val = diridx(*pp)) == DIR_STOP)
227 if (!getsect(x, y, &s))
229 m += cost(&s, s.sct_own);
237 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
239 int dir, lx, ly, hx, hy;
247 cx += diroff[dir][0];
248 cy += diroff[dir][1];
264 range->lx = xnorm(lx);
265 range->hx = ynorm(hx - lx < WORLD_X ? hx : lx - 1);
266 range->ly = ynorm(ly);
267 range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);