2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * path.c: Routines associated with paths, directions, etc.
30 * Known contributors to this file:
41 #include "prototypes.h"
44 getdir(s_char *prompt, s_char *stop_msg, s_char *view_msg,
61 if (getstring(prompt, buf) == 0 || *buf == 0)
63 dir_num = chkdir(buf[0], min_dir, max_dir);
64 if (dir_num >= min_dir)
66 direrr(stop_msg, view_msg, bomb_msg);
72 chkdir(s_char dir_char, int min_dir, int max_dir)
76 for (i = min_dir; i <= max_dir; i++)
77 if (dir_char == dirch[i])
83 direrr(s_char *stop_msg, s_char *view_msg, s_char *map_msg)
85 pr("Legal directions are:\n");
86 pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
87 pr("%c %c\n", dirch[DIR_L], dirch[DIR_R]);
88 pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
90 pr(stop_msg, dirch[DIR_STOP]);
92 pr(view_msg, dirch[DIR_VIEW]);
94 pr(map_msg, dirch[DIR_MAP]);
98 * return pointer to path; prompt user until a stop char
99 * or a bomb char have been entered. A "bomb" char in
100 * this context is actually "execute" for the partial
101 * move commands, and isn't valid for those commands
102 * which do not accept partial moves.
105 getpath(s_char *buf, s_char *arg, coord x, coord y, int onlyown,
106 int showdes, int showxy, int destinations)
112 struct sctstr sect, dsect;
119 strncpy(buf, arg, MAX_PATH_LEN - 1);
120 buf[MAX_PATH_LEN - 1] = 0;
126 execute = " & '%c' to execute\n";
130 getsect(x, y, §);
136 if (sarg_xy(p, &dx, &dy)) {
138 if (destinations == P_NONE) {
139 pr("Destination sectors not allowed here!\n");
142 if (getsect(dx, dy, &dsect)) {
143 if (destinations == P_WALKING) {
144 bp = BestLandPath(p, §, &dsect,
146 } else if (destinations == P_FLYING) {
147 bp = BestAirPath(p, nx, ny, dx, dy);
150 pr("Invalid destination sector!\n");
154 pr("Using best path '%s'\n", p);
155 pr("Using total path '%s'\n", buf);
158 pr("Can't get to %s from here!\n",
159 xyas(nx, ny, player->cnum));
164 dir = chkdir(*p, DIR_STOP, DIR_LAST);
166 pr("\"%c\" is not legal...", *p);
167 direrr("'%c' to stop", (s_char *)0, execute);
171 nx = x + diroff[dir][0];
172 ny = y + diroff[dir][1];
173 getsect(nx, ny, §);
174 if (onlyown && sect.sct_own != player->cnum) {
176 pr("You don't own %s; you can't go there!\n",
177 xyas(nx, ny, player->cnum));
180 if (dir == DIR_STOP || dir == DIR_MAP) {
184 if (++p - buf == MAX_PATH_LEN) {
185 pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
195 getsect(x, y, §);
196 sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
197 xyas(x, y, player->cnum));
199 sprintf(prompt, "<%d: %s> ", (int)(p - buf),
200 xyas(x, y, player->cnum));
202 if (!(bp = getstring(prompt, p)) || !*bp) {
212 mcost(struct sctstr *sp, int own)
214 return sector_mcost(sp, MOB_ROAD);
219 if (!(m_cost = dchr[sp->sct_type].d_mcst))
222 return (200.0 + (m_cost - 3.0) * sp->sct_effic) / 500.0;
224 return (m_cost * 5.0 - (double)sp->sct_effic) / 25.0;
233 fcost(struct sctstr *sp, natid own)
243 ncost(struct sctstr *sp, natid own)
249 * return end x,y of path, and the base
250 * movement cost it takes to get there.
253 pathtoxy(s_char *path, coord *xp, coord *yp,
254 double (*cost) (struct sctstr *, natid))
267 for (pp = path; *pp; pp++) {
268 if ((val = chkdir(*pp, DIR_STOP, DIR_LAST)) == 0)
275 if (!getsect(x, y, &s))
277 m += cost(&s, s.sct_own);
285 * return true if "who" owns the path starting at x,y
288 chkpath(natid who, s_char *path, coord x, coord y)
294 for (pp = path; *pp; pp++) {
295 if ((val = chkdir(*pp, DIR_STOP, DIR_LAST)) == 0)
299 if (!getsect(x, y, §) || sect.sct_own != who)
306 pathrange(register coord cx, register coord cy, register s_char *pp,
307 int border, struct range *range)
317 while ((dir = chkdir(*pp, DIR_FIRST, DIR_LAST)) >= 0) {
319 cx += diroff[dir][0];
320 cy += diroff[dir][1];
330 range->lx = xnorm(range->lx - border * 2);
331 range->ly = ynorm(range->ly - border);
332 range->hx = xnorm(range->hx + border * 2 + 1);
333 range->hy = ynorm(range->hy + border + 1);