]> 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-2010, 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  *     Markus Armbruster, 2005-2009
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)
62         pr(stop_msg, dirch[DIR_STOP]);
63     if (view_msg)
64         pr(view_msg, dirch[DIR_VIEW]);
65     if (map_msg)
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
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                 bp = BestAirPath(buf2, x, y, dx, dy);
123                 break;
124             case P_SAILING:
125                 bp = BestShipPath(buf2, x, y, dx, dy, player->cnum);
126                 break;
127             }
128             if (bp && p + strlen(bp) + 1 < buf + MAX_PATH_LEN) {
129                 strcpy(p, bp);
130                 pr("Using best path  '%s'\n", p);
131                 pr("Using total path '%s'\n", buf);
132                 return buf;
133             } else {
134                 pr("Can't get to %s from here!\n",
135                    xyas(dx, dy, player->cnum));
136             }
137             break;
138         }
139         dir = chkdir(*p, DIR_STOP, DIR_LAST);
140         if (dir < 0) {
141             pr("\"%c\" is not legal...", *p);
142             direrr("'%c' to stop\n", NULL, NULL);
143             break;
144         }
145         nx = x + diroff[dir][0];
146         ny = y + diroff[dir][1];
147         getsect(nx, ny, &sect);
148         if (onlyown && sect.sct_own != player->cnum) {
149             pr("You don't own %s; you can't go there!\n",
150                xyas(nx, ny, player->cnum));
151             break;
152         }
153         if (dir == DIR_STOP) {
154             p[1] = 0;
155             return buf;
156         }
157         ++p;
158         x = sect.sct_x;
159         y = sect.sct_y;
160     }
161     fly_map(x, y);
162     if (showdes) {
163         getsect(x, y, &sect);
164         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
165                 xyas(x, y, player->cnum));
166     } else {
167         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
168                 xyas(x, y, player->cnum));
169     }
170     bp = getstring(prompt, buf2);
171     if (!bp)
172         return NULL;
173     if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
174         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
175         pr("Aborting...\n");
176         bp = NULL;
177     }
178     strcpy(p, bp);
179     if (*bp)
180         goto more;
181     return buf;
182 }
183
184 /*
185  * fly move cost
186  */
187 /* ARGSUSED */
188 double
189 fcost(struct sctstr *sp, natid own)
190 {
191     return 1.0;
192 }
193
194 /*
195  * nav move cost
196  */
197 /* ARGSUSED */
198 double
199 ncost(struct sctstr *sp, natid own)
200 {
201     return 1.0;
202 }
203
204 /*
205  * return end x,y of path, and the base
206  * movement cost it takes to get there.
207  */
208 double
209 pathtoxy(char *path, coord *xp, coord *yp,
210          double (*cost)(struct sctstr *, natid))
211 {
212     struct sctstr s;
213     char *pp;
214     coord x;
215     coord y;
216     int val;
217     double m;
218
219     x = *xp;
220     y = *yp;
221     m = 0.0;
222     for (pp = path; *pp; pp++) {
223         if ((val = diridx(*pp)) == DIR_STOP)
224             break;
225         x += diroff[val][0];
226         y += diroff[val][1];
227         if (!getsect(x, y, &s))
228             return -1.0;
229         m += cost(&s, s.sct_own);
230     }
231     *xp = xnorm(x);
232     *yp = ynorm(y);
233     return m;
234 }
235
236 void
237 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
238 {
239     int dir, lx, ly, hx, hy;
240
241     lx = hx = cx;
242     ly = hy = cy;
243     for (; *pp; pp++) {
244         dir = diridx(*pp);
245         if (dir == DIR_STOP)
246             break;
247         cx += diroff[dir][0];
248         cy += diroff[dir][1];
249         if (cx < lx)
250             lx = cx;
251         if (cx > hx)
252             hx = cx;
253         if (cy < ly)
254             ly = cy;
255         if (cy > hy)
256             hy = cy;
257     }
258
259     lx -= border * 2;
260     hx += border * 2;
261     ly -= border;
262     hy += border;
263
264     range->lx = xnorm(lx);
265     range->hx = ynorm(hx - lx < WORLD_X ? hx : lx - 1);
266     range->ly = ynorm(ly);
267     range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);
268     xysize_range(range);
269 }