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