]> 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-2018, 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 "optlist.h"
36 #include "path.h"
37 #include "player.h"
38 #include "prototypes.h"
39 #include "sect.h"
40
41 int
42 chkdir(char dir_char, int min_dir, int max_dir)
43 {
44     int i;
45
46     for (i = min_dir; i <= max_dir; i++)
47         if (dir_char == dirch[i])
48             return i;
49     return -1;
50 }
51
52 void
53 direrr(char *stop_msg, char *view_msg, char *map_msg)
54 {
55     pr("Legal directions are:\n");
56     pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
57     pr("%c   %c\n", dirch[DIR_L], dirch[DIR_R]);
58     pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
59     if (stop_msg)
60         pr(stop_msg, dirch[DIR_STOP]);
61     if (view_msg)
62         pr(view_msg, dirch[DIR_VIEW]);
63     if (map_msg)
64         pr(map_msg, dirch[DIR_MAP]);
65 }
66
67 /*
68  * Map direction @dir to a direction index DIR_STOP..DIR_LAST.
69  * @dir must be a valid direction.
70  */
71 int
72 diridx(char dir)
73 {
74     unsigned i = dir - 'a';
75
76     if (CANT_HAPPEN(i >= sizeof(dirindex) / sizeof(*dirindex)
77                     || dirindex[i] < 0))
78         return DIR_STOP;
79     return dirindex[i];
80 }
81
82 /*
83  * return pointer to path; prompt user until a stop char
84  * or a bomb char have been entered.  A "bomb" char in
85  * this context is actually "execute" for the partial
86  * move commands, and isn't valid for those commands
87  * which do not accept partial moves.
88  */
89 char *
90 getpath(char *buf, char *arg, coord x, coord y, int onlyown, int showdes,
91         int mobtype)
92 {
93     char buf2[1024];
94     char *p = buf;
95     char *bp;
96     size_t len;
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
112 more:
113     while (*p) {
114         if (sarg_xy(p, &dx, &dy)) {
115             if (path_find(x, y, dx, dy, player->cnum, mobtype) < 0) {
116                 pr("Can't get to %s from here!\n",
117                    xyas(dx, dy, player->cnum));
118                 break;
119             }
120             len = path_find_route(p, buf + MAX_PATH_LEN - p, x, y, dx, dy);
121             if (p + len >= buf + MAX_PATH_LEN) {
122                 pr("Can't handle path to %s, it's too long, sorry.\n",
123                    xyas(dx, dy, player->cnum));
124                 break;
125             }
126             pr("Using best path  '%s'\n", p);
127             pr("Using total path '%s'\n", buf);
128             return buf;
129         }
130         dir = chkdir(*p, DIR_STOP, DIR_LAST);
131         if (dir < 0) {
132             pr("\"%c\" is not legal...", *p);
133             direrr("'%c' to stop\n", NULL, NULL);
134             break;
135         }
136         nx = x + diroff[dir][0];
137         ny = y + diroff[dir][1];
138         getsect(nx, ny, &sect);
139         if (onlyown && sect.sct_own != player->cnum) {
140             pr("You don't own %s; you can't go there!\n",
141                xyas(nx, ny, player->cnum));
142             break;
143         }
144         if (dir == DIR_STOP) {
145             *p = 0;
146             return buf;
147         }
148         ++p;
149         x = sect.sct_x;
150         y = sect.sct_y;
151     }
152     fly_map(x, y);
153     if (showdes) {
154         getsect(x, y, &sect);
155         sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
156                 xyas(x, y, player->cnum));
157     } else {
158         sprintf(prompt, "<%d: %s> ", (int)(p - buf),
159                 xyas(x, y, player->cnum));
160     }
161     bp = getstring(prompt, buf2);
162     if (!bp)
163         return NULL;
164     if (p + strlen(bp) + 1 >= buf + MAX_PATH_LEN) {
165         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
166         pr("Aborting...\n");
167         return NULL;
168     }
169     strcpy(p, bp);
170     if (*bp)
171         goto more;
172     return buf[0] ? buf : NULL;
173 }
174
175 /*
176  * fly move cost
177  */
178 /* ARGSUSED */
179 double
180 fcost(struct sctstr *sp, natid own)
181 {
182     return 1.0;
183 }
184
185 /*
186  * nav move cost
187  */
188 /* ARGSUSED */
189 double
190 ncost(struct sctstr *sp, natid own)
191 {
192     return 1.0;
193 }
194
195 /*
196  * return end x,y of path, and the base
197  * movement cost it takes to get there.
198  */
199 double
200 pathtoxy(char *path, coord *xp, coord *yp,
201          double (*cost)(struct sctstr *, natid))
202 {
203     struct sctstr s;
204     char *pp;
205     coord x;
206     coord y;
207     int val;
208     double m;
209
210     x = *xp;
211     y = *yp;
212     m = 0.0;
213     for (pp = path; *pp; pp++) {
214         if ((val = diridx(*pp)) == DIR_STOP)
215             break;
216         x += diroff[val][0];
217         y += diroff[val][1];
218         if (!getsect(x, y, &s))
219             return -1.0;
220         m += cost(&s, s.sct_own);
221     }
222     *xp = xnorm(x);
223     *yp = ynorm(y);
224     return m;
225 }
226
227 void
228 pathrange(coord cx, coord cy, char *pp, int border, struct range *range)
229 {
230     int dir, lx, ly, hx, hy;
231
232     lx = hx = cx;
233     ly = hy = cy;
234     for (; *pp; pp++) {
235         dir = diridx(*pp);
236         if (dir == DIR_STOP)
237             break;
238         cx += diroff[dir][0];
239         cy += diroff[dir][1];
240         if (cx < lx)
241             lx = cx;
242         if (cx > hx)
243             hx = cx;
244         if (cy < ly)
245             ly = cy;
246         if (cy > hy)
247             hy = cy;
248     }
249
250     lx -= border * 2;
251     hx += border * 2;
252     ly -= border;
253     hy += border;
254
255     range->lx = xnorm(lx);
256     range->hx = xnorm(hx - lx < WORLD_X ? hx : lx - 1);
257     range->ly = ynorm(ly);
258     range->hy = ynorm(hy - ly < WORLD_Y ? hy : ly - 1);
259     xysize_range(range);
260 }