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