]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
(getpath): Fix message when no path to destination sector can be
[empserver] / src / lib / subs / paths.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  path.c: Routines associated with paths, directions, etc.
29  * 
30  *  Known contributors to this file:
31  *   
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "path.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "file.h"
41 #include "prototypes.h"
42
43 int
44 chkdir(s_char dir_char, int min_dir, int max_dir)
45 {
46     register int i;
47
48     for (i = min_dir; i <= max_dir; i++)
49         if (dir_char == dirch[i])
50             return i;
51     return -1;
52 }
53
54 void
55 direrr(s_char *stop_msg, s_char *view_msg, s_char *map_msg)
56 {
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]);
61     if (stop_msg != 0)
62         pr(stop_msg, dirch[DIR_STOP]);
63     if (view_msg != 0)
64         pr(view_msg, dirch[DIR_VIEW]);
65     if (map_msg != 0)
66         pr(map_msg, dirch[DIR_MAP]);
67 }
68
69 /*
70  * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
71  * DIR must be a valid direction.
72  */
73 int
74 diridx(char dir)
75 {
76     unsigned i = dir - 'a';
77
78     if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
79                     || dirindex[i] < 0))
80         return DIR_STOP;
81     return dirindex[i];
82 }
83
84 /*
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.
90  */
91 char *
92 getpath(char *buf, char *arg, coord x, coord y, int onlyown,
93         int showdes, int destinations)
94 {
95     char *p = buf;
96     char *bp;
97     char prompt[128];
98     coord dx, dy;
99     struct sctstr sect, dsect;
100     coord nx, ny;
101     int dir;
102     double mv_cost;
103
104     if (arg) {
105         strncpy(buf, arg, MAX_PATH_LEN - 1);
106         buf[MAX_PATH_LEN - 1] = 0;
107     } else {
108         *p = 0;
109     }
110
111     getsect(x, y, &sect);
112     nx = x;
113     ny = y;
114
115   more:
116     while (*p) {
117         if (sarg_xy(p, &dx, &dy)) {
118             bp = 0;
119             if (destinations == P_NONE) {
120                 pr("Destination sectors not allowed here!\n");
121                 *p = 0;
122             }
123             if (getsect(dx, dy, &dsect)) {
124                 if (destinations == P_WALKING) {
125                     bp = BestLandPath(p, &sect, &dsect,
126                                       &mv_cost, MOB_ROAD);
127                 } else if (destinations == P_FLYING) {
128                     bp = BestAirPath(p, nx, ny, dx, dy);
129                 }
130             } else {
131                 pr("Invalid destination sector!\n");
132                 *p = 0;
133             }
134             if (bp) {
135                 pr("Using best path  '%s'\n", p);
136                 pr("Using total path '%s'\n", buf);
137                 return buf;
138             } else {
139                 pr("Can't get to %s from here!\n",
140                    xyas(dx, dy, player->cnum));
141                 *p = 0;
142             }
143             break;
144         }
145         dir = chkdir(*p, DIR_STOP, DIR_LAST);
146         if (dir < 0) {
147             pr("\"%c\" is not legal...", *p);
148             direrr("'%c' to stop\n", NULL, NULL);
149             *p = 0;
150             break;
151         }
152         nx = x + diroff[dir][0];
153         ny = y + diroff[dir][1];
154         getsect(nx, ny, &sect);
155         if (onlyown && sect.sct_own != player->cnum) {
156             *p = 0;
157             pr("You don't own %s; you can't go there!\n",
158                xyas(nx, ny, player->cnum));
159             break;
160         }
161         if (dir == DIR_STOP || dir == DIR_MAP) {
162             p[1] = 0;
163             return buf;
164         }
165         if (++p - buf == MAX_PATH_LEN) {
166             pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
167             pr("Aborting...\n");
168             *buf = 0;
169             return buf;
170         }
171         x = nx;
172         y = ny;
173     }
174     fly_map(x, y);
175     if (showdes) {
176         getsect(x, y, &sect);
177         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
178                 xyas(x, y, player->cnum));
179     } else {
180         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
181                 xyas(x, y, player->cnum));
182     }
183     if (!(bp = getstring(prompt, p)) || !*bp) {
184         if (player->aborted)
185             *buf = 0;
186         return buf;
187     }
188     goto more;
189 }
190
191 /*
192  * fly move cost
193  */
194 /* ARGSUSED */
195 double
196 fcost(struct sctstr *sp, natid own)
197 {
198     return 1.0;
199 }
200
201 /*
202  * nav move cost
203  */
204 /* ARGSUSED */
205 double
206 ncost(struct sctstr *sp, natid own)
207 {
208     return 1.0;
209 }
210
211 /*
212  * return end x,y of path, and the base
213  * movement cost it takes to get there.
214  */
215 double
216 pathtoxy(s_char *path, coord *xp, coord *yp,
217          double (*cost) (struct sctstr *, natid))
218 {
219     struct sctstr s;
220     s_char *pp;
221     coord x;
222     coord y;
223     int val;
224     double m;
225
226     x = *xp;
227     y = *yp;
228     m = 0.0;
229     for (pp = path; *pp; pp++) {
230         if ((val = diridx(*pp)) == DIR_STOP)
231             break;
232         x += diroff[val][0];
233         y += diroff[val][1];
234         if (!getsect(x, y, &s))
235             return -1.0;
236         m += cost(&s, s.sct_own);
237     }
238     *xp = xnorm(x);
239     *yp = ynorm(y);
240     return m;
241 }
242
243 void
244 pathrange(coord cx, coord cy, s_char *pp, int border, struct range *range)
245 {
246     int dir;
247
248     range->lx = cx;
249     range->hx = cx;
250     range->ly = cy;
251     range->hy = cy;
252     range->width = 0;
253     range->height = 0;
254     for (; *pp; pp++) {
255         dir = diridx(*pp);
256         if (dir == DIR_STOP)
257             break;
258         cx += diroff[dir][0];
259         cy += diroff[dir][1];
260         if (cx < range->lx)
261             range->lx = cx;
262         if (cx > range->hx)
263             range->hx = cx;
264         if (cy < range->ly)
265             range->ly = cy;
266         if (cy > range->hy)
267             range->hy = cy;
268     }
269     range->lx = xnorm(range->lx - border * 2);
270     range->ly = ynorm(range->ly - border);
271     range->hx = xnorm(range->hx + border * 2 + 1);
272     range->hy = ynorm(range->hy + border + 1);
273     xysize_range(range);
274 }