]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
Update copyright notice.
[empserver] / src / lib / subs / paths.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 "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 != 0)
61         pr(stop_msg, dirch[DIR_STOP]);
62     if (view_msg != 0)
63         pr(view_msg, dirch[DIR_VIEW]);
64     if (map_msg != 0)
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     char prompt[128];
98     coord dx, dy;
99     struct sctstr sect;
100     coord nx, ny;
101     int dir;
102
103     if (arg) {
104         strncpy(buf, arg, MAX_PATH_LEN - 1);
105         buf[MAX_PATH_LEN - 1] = 0;
106     } else {
107         *p = 0;
108     }
109
110     getsect(x, y, &sect);
111     nx = x;
112     ny = y;
113
114   more:
115     while (*p) {
116         if (sarg_xy(p, &dx, &dy)) {
117             bp = NULL;
118             switch (destinations) {
119             case P_NONE:
120                 pr("Destination sectors not allowed here!\n");
121                 break;
122             case P_FLYING:
123                 bp = BestAirPath(buf2, nx, ny, dx, dy);
124                 break;
125             case P_SAILING:
126                 bp = BestShipPath(buf2, nx, ny, dx, dy, player->cnum);
127                 break;
128             }
129             if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
130                 strcpy(p, bp);
131                 pr("Using best path  '%s'\n", p);
132                 pr("Using total path '%s'\n", buf);
133                 return buf;
134             } else {
135                 pr("Can't get to %s from here!\n",
136                    xyas(dx, dy, player->cnum));
137             }
138             break;
139         }
140         dir = chkdir(*p, DIR_STOP, DIR_LAST);
141         if (dir < 0) {
142             pr("\"%c\" is not legal...", *p);
143             direrr("'%c' to stop\n", NULL, NULL);
144             break;
145         }
146         nx = x + diroff[dir][0];
147         ny = y + diroff[dir][1];
148         getsect(nx, ny, &sect);
149         if (onlyown && sect.sct_own != player->cnum) {
150             pr("You don't own %s; you can't go there!\n",
151                xyas(nx, ny, player->cnum));
152             break;
153         }
154         if (dir == DIR_STOP) {
155             p[1] = 0;
156             return buf;
157         }
158         ++p;
159         x = nx;
160         y = ny;
161     }
162     fly_map(x, y);
163     if (showdes) {
164         getsect(x, y, &sect);
165         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
166                 xyas(x, y, player->cnum));
167     } else {
168         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
169                 xyas(x, y, player->cnum));
170     }
171     bp = getstring(prompt, buf2);
172     if (bp && p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
173         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
174         pr("Aborting...\n");
175         bp = NULL;
176     }
177     if (!bp)
178         return NULL;
179     strcpy(p, bp);
180     if (*bp)
181         goto more;
182     return buf;
183 }
184
185 /*
186  * fly move cost
187  */
188 /* ARGSUSED */
189 double
190 fcost(struct sctstr *sp, natid own)
191 {
192     return 1.0;
193 }
194
195 /*
196  * nav move cost
197  */
198 /* ARGSUSED */
199 double
200 ncost(struct sctstr *sp, natid own)
201 {
202     return 1.0;
203 }
204
205 /*
206  * return end x,y of path, and the base
207  * movement cost it takes to get there.
208  */
209 double
210 pathtoxy(char *path, coord *xp, coord *yp,
211          double (*cost)(struct sctstr *, natid))
212 {
213     struct sctstr s;
214     char *pp;
215     coord x;
216     coord y;
217     int val;
218     double m;
219
220     x = *xp;
221     y = *yp;
222     m = 0.0;
223     for (pp = path; *pp; pp++) {
224         if ((val = diridx(*pp)) == DIR_STOP)
225             break;
226         x += diroff[val][0];
227         y += diroff[val][1];
228         if (!getsect(x, y, &s))
229             return -1.0;
230         m += cost(&s, s.sct_own);
231     }
232     *xp = xnorm(x);
233     *yp = ynorm(y);
234     return m;
235 }
236
237 void
238 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
239 {
240     int dir;
241
242     range->lx = cx;
243     range->hx = cx;
244     range->ly = cy;
245     range->hy = cy;
246     range->width = 0;
247     range->height = 0;
248     for (; *pp; pp++) {
249         dir = diridx(*pp);
250         if (dir == DIR_STOP)
251             break;
252         cx += diroff[dir][0];
253         cy += diroff[dir][1];
254         if (cx < range->lx)
255             range->lx = cx;
256         if (cx > range->hx)
257             range->hx = cx;
258         if (cy < range->ly)
259             range->ly = cy;
260         if (cy > range->hy)
261             range->hy = cy;
262     }
263     range->lx = xnorm(range->lx - border * 2);
264     range->ly = ynorm(range->ly - border);
265     range->hx = xnorm(range->hx + border * 2 + 1);
266     range->hy = ynorm(range->hy + border + 1);
267     xysize_range(range);
268 }