]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
Fix pathrange() for paths spanning whole world (with border)
[empserver] / src / lib / subs / paths.c
1 /*
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
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 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.
25  *
26  *  ---
27  *
28  *  path.c: Routines associated with paths, directions, etc.
29  * 
30  *  Known contributors to this file:
31  *   
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "optlist.h"
38 #include "path.h"
39 #include "player.h"
40 #include "prototypes.h"
41 #include "sect.h"
42
43 int
44 chkdir(char dir_char, int min_dir, int max_dir)
45 {
46     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(char *stop_msg, char *view_msg, 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, enum p_mode destinations)
94 {
95     char buf2[1024];
96     char *p = buf;
97     char *bp;
98     char prompt[128];
99     coord dx, dy;
100     struct sctstr sect;
101     coord nx, ny;
102     int dir;
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 = NULL;
119             switch (destinations) {
120             case P_NONE:
121                 pr("Destination sectors not allowed here!\n");
122                 break;
123             case P_FLYING:
124                 bp = BestAirPath(buf2, nx, ny, dx, dy);
125                 break;
126             case P_SAILING:
127                 bp = BestShipPath(buf2, nx, ny, dx, dy, player->cnum);
128                 break;
129             }
130             if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
131                 strcpy(p, bp);
132                 pr("Using best path  '%s'\n", p);
133                 pr("Using total path '%s'\n", buf);
134                 return buf;
135             } else {
136                 pr("Can't get to %s from here!\n",
137                    xyas(dx, dy, player->cnum));
138             }
139             break;
140         }
141         dir = chkdir(*p, DIR_STOP, DIR_LAST);
142         if (dir < 0) {
143             pr("\"%c\" is not legal...", *p);
144             direrr("'%c' to stop\n", NULL, NULL);
145             break;
146         }
147         nx = x + diroff[dir][0];
148         ny = y + diroff[dir][1];
149         getsect(nx, ny, &sect);
150         if (onlyown && sect.sct_own != player->cnum) {
151             pr("You don't own %s; you can't go there!\n",
152                xyas(nx, ny, player->cnum));
153             break;
154         }
155         if (dir == DIR_STOP) {
156             p[1] = 0;
157             return buf;
158         }
159         ++p;
160         x = nx;
161         y = ny;
162     }
163     fly_map(x, y);
164     if (showdes) {
165         getsect(x, y, &sect);
166         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
167                 xyas(x, y, player->cnum));
168     } else {
169         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
170                 xyas(x, y, player->cnum));
171     }
172     bp = getstring(prompt, buf2);
173     if (!bp)
174         return NULL;
175     if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
176         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
177         pr("Aborting...\n");
178         bp = NULL;
179     }
180     strcpy(p, bp);
181     if (*bp)
182         goto more;
183     return buf;
184 }
185
186 /*
187  * fly move cost
188  */
189 /* ARGSUSED */
190 double
191 fcost(struct sctstr *sp, natid own)
192 {
193     return 1.0;
194 }
195
196 /*
197  * nav move cost
198  */
199 /* ARGSUSED */
200 double
201 ncost(struct sctstr *sp, natid own)
202 {
203     return 1.0;
204 }
205
206 /*
207  * return end x,y of path, and the base
208  * movement cost it takes to get there.
209  */
210 double
211 pathtoxy(char *path, coord *xp, coord *yp,
212          double (*cost)(struct sctstr *, natid))
213 {
214     struct sctstr s;
215     char *pp;
216     coord x;
217     coord y;
218     int val;
219     double m;
220
221     x = *xp;
222     y = *yp;
223     m = 0.0;
224     for (pp = path; *pp; pp++) {
225         if ((val = diridx(*pp)) == DIR_STOP)
226             break;
227         x += diroff[val][0];
228         y += diroff[val][1];
229         if (!getsect(x, y, &s))
230             return -1.0;
231         m += cost(&s, s.sct_own);
232     }
233     *xp = xnorm(x);
234     *yp = ynorm(y);
235     return m;
236 }
237
238 void
239 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
240 {
241     int dir, lx, ly, hx, hy;
242
243     lx = hx = cx;
244     ly = hy = cy;
245     for (; *pp; pp++) {
246         dir = diridx(*pp);
247         if (dir == DIR_STOP)
248             break;
249         cx += diroff[dir][0];
250         cy += diroff[dir][1];
251         if (cx < lx)
252             lx = cx;
253         if (cx > hx)
254             hx = cx;
255         if (cy < ly)
256             ly = cy;
257         if (cy > hy)
258             hy = cy;
259     }
260
261     lx -= border * 2;
262     hx += border * 2;
263     ly -= border;
264     hy += border;
265
266     range->lx = xnorm(lx);
267     range->hx = ynorm(hx - lx < WORLD_X ? hx : lx - 1);
268     range->ly = ynorm(ly);
269     range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);
270     xysize_range(range);
271 }