]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
Inline BestShipPath(), BestAirPath() glue
[empserver] / src / lib / subs / paths.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  path.c: Routines associated with paths, directions, etc.
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2005-2009
31  */
32
33 #include <config.h>
34
35 #include "file.h"
36 #include "optlist.h"
37 #include "path.h"
38 #include "player.h"
39 #include "prototypes.h"
40 #include "sect.h"
41
42 int
43 chkdir(char dir_char, int min_dir, int max_dir)
44 {
45     int i;
46
47     for (i = min_dir; i <= max_dir; i++)
48         if (dir_char == dirch[i])
49             return i;
50     return -1;
51 }
52
53 void
54 direrr(char *stop_msg, char *view_msg, char *map_msg)
55 {
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]);
60     if (stop_msg)
61         pr(stop_msg, dirch[DIR_STOP]);
62     if (view_msg)
63         pr(view_msg, dirch[DIR_VIEW]);
64     if (map_msg)
65         pr(map_msg, dirch[DIR_MAP]);
66 }
67
68 /*
69  * Map direction DIR to a direction index DIR_STOP..DIR_LAST.
70  * DIR must be a valid direction.
71  */
72 int
73 diridx(char dir)
74 {
75     unsigned i = dir - 'a';
76
77     if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
78                     || dirindex[i] < 0))
79         return DIR_STOP;
80     return dirindex[i];
81 }
82
83 /*
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.
89  */
90 char *
91 getpath(char *buf, char *arg, coord x, coord y, int onlyown,
92         int showdes, enum p_mode destinations)
93 {
94     char buf2[1024];
95     char *p = buf;
96     char *bp;
97     size_t len;
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
113 more:
114     while (*p) {
115         if (sarg_xy(p, &dx, &dy)) {
116             bp = NULL;
117             switch (destinations) {
118             case P_NONE:
119                 pr("Destination sectors not allowed here!\n");
120                 break;
121             case P_FLYING:
122                 if (path_find(x, y, dx, dy, 0, MOB_FLY) < 0)
123                     bp = NULL;
124                 else {
125                     len = path_find_route(buf2, 100, x, y, dx, dy);
126                     if (len >= 100)
127                         bp = NULL;
128                     else {
129                         if (len == 0)
130                             strcpy(buf2, "h");
131                         bp = buf2;
132                     }
133                 }
134                 break;
135             case P_SAILING:
136                 if (path_find(x, y, dx, dy, player->cnum, MOB_SAIL) < 0)
137                     bp = NULL;
138                 else {
139                     len = path_find_route(buf2, 100, x, y, dx, dy);
140                     if (len >= 100)
141                         bp = NULL;
142                     else {
143                         if (len == 0)
144                             strcpy(buf2, "h");
145                         bp = buf2;
146                     }
147                 }
148                 break;
149             }
150             if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
151                 strcpy(p, bp);
152                 pr("Using best path  '%s'\n", p);
153                 pr("Using total path '%s'\n", buf);
154                 return buf;
155             } else {
156                 pr("Can't get to %s from here!\n",
157                    xyas(dx, dy, player->cnum));
158             }
159             break;
160         }
161         dir = chkdir(*p, DIR_STOP, DIR_LAST);
162         if (dir < 0) {
163             pr("\"%c\" is not legal...", *p);
164             direrr("'%c' to stop\n", NULL, NULL);
165             break;
166         }
167         nx = x + diroff[dir][0];
168         ny = y + diroff[dir][1];
169         getsect(nx, ny, &sect);
170         if (onlyown && sect.sct_own != player->cnum) {
171             pr("You don't own %s; you can't go there!\n",
172                xyas(nx, ny, player->cnum));
173             break;
174         }
175         if (dir == DIR_STOP) {
176             p[1] = 0;
177             return buf;
178         }
179         ++p;
180         x = sect.sct_x;
181         y = sect.sct_y;
182     }
183     fly_map(x, y);
184     if (showdes) {
185         getsect(x, y, &sect);
186         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
187                 xyas(x, y, player->cnum));
188     } else {
189         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
190                 xyas(x, y, player->cnum));
191     }
192     bp = getstring(prompt, buf2);
193     if (!bp)
194         return NULL;
195     if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
196         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
197         pr("Aborting...\n");
198         return NULL;
199     }
200     strcpy(p, bp);
201     if (*bp)
202         goto more;
203     return buf;
204 }
205
206 /*
207  * fly move cost
208  */
209 /* ARGSUSED */
210 double
211 fcost(struct sctstr *sp, natid own)
212 {
213     return 1.0;
214 }
215
216 /*
217  * nav move cost
218  */
219 /* ARGSUSED */
220 double
221 ncost(struct sctstr *sp, natid own)
222 {
223     return 1.0;
224 }
225
226 /*
227  * return end x,y of path, and the base
228  * movement cost it takes to get there.
229  */
230 double
231 pathtoxy(char *path, coord *xp, coord *yp,
232          double (*cost)(struct sctstr *, natid))
233 {
234     struct sctstr s;
235     char *pp;
236     coord x;
237     coord y;
238     int val;
239     double m;
240
241     x = *xp;
242     y = *yp;
243     m = 0.0;
244     for (pp = path; *pp; pp++) {
245         if ((val = diridx(*pp)) == DIR_STOP)
246             break;
247         x += diroff[val][0];
248         y += diroff[val][1];
249         if (!getsect(x, y, &s))
250             return -1.0;
251         m += cost(&s, s.sct_own);
252     }
253     *xp = xnorm(x);
254     *yp = ynorm(y);
255     return m;
256 }
257
258 void
259 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
260 {
261     int dir, lx, ly, hx, hy;
262
263     lx = hx = cx;
264     ly = hy = cy;
265     for (; *pp; pp++) {
266         dir = diridx(*pp);
267         if (dir == DIR_STOP)
268             break;
269         cx += diroff[dir][0];
270         cy += diroff[dir][1];
271         if (cx < lx)
272             lx = cx;
273         if (cx > hx)
274             hx = cx;
275         if (cy < ly)
276             ly = cy;
277         if (cy > hy)
278             hy = cy;
279     }
280
281     lx -= border * 2;
282     hx += border * 2;
283     ly -= border;
284     hy += border;
285
286     range->lx = xnorm(lx);
287     range->hx = xnorm(hx - lx < WORLD_X ? hx : lx - 1);
288     range->ly = ynorm(ly);
289     range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);
290     xysize_range(range);
291 }