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:
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)
104 strncpy(buf, arg, MAX_PATH_LEN - 1);
105 buf[MAX_PATH_LEN - 1] = 0;
110 getsect(x, y, §);
116 if (sarg_xy(p, &dx, &dy)) {
118 switch (destinations) {
120 pr("Destination sectors not allowed here!\n");
123 bp = BestAirPath(buf2, nx, ny, dx, dy);
126 bp = BestShipPath(buf2, nx, ny, dx, dy, player->cnum);
129 if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
131 pr("Using best path '%s'\n", p);
132 pr("Using total path '%s'\n", buf);
135 pr("Can't get to %s from here!\n",
136 xyas(dx, dy, player->cnum));
140 dir = chkdir(*p, DIR_STOP, DIR_LAST);
142 pr("\"%c\" is not legal...", *p);
143 direrr("'%c' to stop\n", NULL, NULL);
146 nx = x + diroff[dir][0];
147 ny = y + diroff[dir][1];
148 getsect(nx, ny, §);
149 if (onlyown && sect.sct_own != player->cnum) {
150 pr("You don't own %s; you can't go there!\n",
151 xyas(nx, ny, player->cnum));
154 if (dir == DIR_STOP) {
164 getsect(x, y, §);
165 sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
166 xyas(x, y, player->cnum));
168 sprintf(prompt, "<%d: %s> ", (int)(p - buf),
169 xyas(x, y, player->cnum));
171 bp = getstring(prompt, buf2);
172 if (bp && p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
173 pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
190 fcost(struct sctstr *sp, natid own)
200 ncost(struct sctstr *sp, natid own)
206 * return end x,y of path, and the base
207 * movement cost it takes to get there.
210 pathtoxy(char *path, coord *xp, coord *yp,
211 double (*cost)(struct sctstr *, natid))
223 for (pp = path; *pp; pp++) {
224 if ((val = diridx(*pp)) == DIR_STOP)
228 if (!getsect(x, y, &s))
230 m += cost(&s, s.sct_own);
238 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
252 cx += diroff[dir][0];
253 cy += diroff[dir][1];
263 range->lx = xnorm(range->lx - border * 2);
264 range->ly = ynorm(range->ly - border);
265 range->hx = xnorm(range->hx + border * 2 + 1);
266 range->hy = ynorm(range->hy + border + 1);