]> git.pond.sub.org Git - empserver/blob - src/lib/subs/paths.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / paths.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "path.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "file.h"
41 #include "prototypes.h"
42
43 int
44 getdir(s_char *prompt, s_char *stop_msg, s_char *view_msg, s_char *bomb_msg)
45 {
46         register int max_dir;
47         register int min_dir;
48         register int dir_num;
49         s_char  buf[1024];
50
51         if (stop_msg != 0)
52                 min_dir = DIR_STOP;
53         else
54                 min_dir = DIR_FIRST;
55         if (view_msg == 0)
56                 max_dir = DIR_LAST;
57         else
58                 max_dir = DIR_VIEW;
59         while (1) {
60                 if (getstring(prompt, buf) == 0 || *buf == 0)
61                         return -1;
62                 dir_num = chkdir(buf[0], min_dir, max_dir);
63                 if (dir_num >= min_dir)
64                         break;
65                 direrr(stop_msg, view_msg, bomb_msg);
66         }
67         return dir_num;
68 }
69
70 int
71 chkdir(s_char dir_char, int min_dir, int max_dir)
72 {
73         register int i;
74
75         for (i = min_dir; i <= max_dir; i++)
76                 if (dir_char == dirch[i])
77                         return i;
78         return -1;
79 }
80
81 void
82 direrr(s_char *stop_msg, s_char *view_msg, s_char *map_msg)
83 {
84         pr("Legal directions are:\n");
85         pr(" %c %c\n", dirch[DIR_UL], dirch[DIR_UR]);
86         pr("%c   %c\n", dirch[DIR_L], dirch[DIR_R]);
87         pr(" %c %c\n", dirch[DIR_DL], dirch[DIR_DR]);
88         if (stop_msg != 0)
89                 pr(stop_msg, dirch[DIR_STOP]);
90         if (view_msg != 0)
91                 pr(view_msg, dirch[DIR_VIEW]);
92         if (map_msg != 0)
93                 pr(map_msg, dirch[DIR_MAP]);
94 }
95
96 /*
97  * return pointer to path; prompt user until a stop char
98  * or a bomb char have been entered.  A "bomb" char in
99  * this context is actually "execute" for the partial
100  * move commands, and isn't valid for those commands
101  * which do not accept partial moves.
102  */
103 s_char *
104 getpath(s_char *buf, s_char *arg, coord x, coord y, int onlyown, int showdes, int showxy, int destinations)
105 {
106         s_char  *p = buf;
107         s_char  *bp;
108         s_char  prompt[128];
109         coord   dx, dy;
110         struct  sctstr sect, dsect;
111         coord   nx, ny;
112         int     dir;
113         s_char  *execute;
114         double  mv_cost;
115
116         if (arg) {
117                 strncpy(buf, arg, MAX_PATH_LEN - 1);
118                 buf[MAX_PATH_LEN - 1] = 0;
119         } else {
120                 *p = 0;
121         }
122
123         if (showxy)
124                 execute = " & '%c' to execute\n";
125         else
126                 execute = "\n";
127
128         getsect(x,y,&sect);
129         nx = x;
130         ny = y;
131
132 more:
133         while (*p) {
134                 if (sarg_xy(p, &dx, &dy)) {
135                         bp = 0;
136                         if (destinations == P_NONE) {
137                                 pr("Destination sectors not allowed here!\n");
138                                 *p = 0;
139                         }
140                         if (getsect(dx,dy,&dsect)) {
141                                 if (destinations == P_WALKING){
142                                         bp=BestLandPath(p, &sect,&dsect,
143                                                         &mv_cost, MOB_ROAD);
144                                 } else if (destinations == P_FLYING) {
145                                         bp=BestAirPath(p, nx, ny, dx, dy);
146                                 }
147                         } else {
148                                 pr("Invalid destination sector!\n");
149                                 *p = 0;
150                         }
151                         if (bp) {
152                                 pr("Using best path  '%s'\n", p);
153                                 pr("Using total path '%s'\n", buf);
154                                 return buf;
155                         } else {                        
156                                 pr("Can't get to %s from here!\n",
157                                         xyas(nx,ny,player->cnum));
158                                 *p = 0;
159                         }
160                         break;
161                 }
162                 dir = chkdir(*p, DIR_STOP, DIR_LAST);
163                 if (dir < 0) {
164                         pr("\"%c\" is not legal...", *p);
165                         direrr("'%c' to stop", (s_char *)0, execute);
166                         *p = 0;
167                         break;
168                 }
169                 nx = x + diroff[dir][0];
170                 ny = y + diroff[dir][1];
171                 getsect(nx, ny, &sect);
172                 if (onlyown && sect.sct_own != player->cnum) {
173                         *p = 0;
174                         pr("You don't own %s; you can't go there!\n",
175                                 xyas(nx, ny, player->cnum));
176                         break;
177                 }
178                 if (dir == DIR_STOP || dir == DIR_MAP) {
179                         p[1] = 0;
180                         return buf;
181                 }
182                 if (++p - buf == MAX_PATH_LEN) {
183                         pr("Path length may not exceed %d.\n", MAX_PATH_LEN);
184                         pr("Aborting...\n");
185                         *buf = 0;
186                         return buf;
187                 }
188                 x = nx;
189                 y = ny;
190         }
191         fly_map(x,y);
192         if (showdes) {
193                 getsect(x, y, &sect);
194                 sprintf(prompt, "<%c: %s> ", dchr[sect.sct_type].d_mnem,
195                         xyas(x, y, player->cnum));
196         } else {
197                 sprintf(prompt, "<%d: %s> ", (int)(p - buf),
198                         xyas(x, y, player->cnum));
199         }
200         if (!(bp = getstring(prompt, p)) || !*bp) {
201                 if (player->aborted)
202                         *buf = 0;
203                 return buf;
204         }
205         goto more;
206 }
207
208 /* ARGSUSED */
209 double
210 mcost(struct sctstr *sp, int own)
211 {
212     return sector_mcost(sp, MOB_ROAD);
213
214 /*
215         int     m_cost;
216
217         if (!(m_cost = dchr[sp->sct_type].d_mcst))
218                 return -1.0;
219         if (m_cost < 25)
220                 return (200.0 + (m_cost - 3.0) * sp->sct_effic) / 500.0;
221         else
222                 return (m_cost * 5.0 - (double)sp->sct_effic) / 25.0;
223  */
224 }
225
226 /*
227  * fly move cost
228  */
229 /* ARGSUSED */
230 double
231 fcost(struct sctstr *sp, natid own)
232 {
233         return 1.0;
234 }
235
236 /*
237  * nav move cost
238  */
239 /* ARGSUSED */
240 double
241 ncost(struct sctstr *sp, natid own)
242 {
243         return 1.0;
244 }
245
246 /*
247  * return end x,y of path, and the base
248  * movement cost it takes to get there.
249  */
250 double
251 pathtoxy(s_char *path, coord *xp, coord *yp, double (*cost) (struct sctstr *, natid))
252 {
253         struct  sctstr s;
254         s_char  *pp;
255         coord   x;
256         coord   y;
257         int     val;
258         double  m;
259         int     c;
260
261         x = *xp;
262         y = *yp;
263         m = 0.0;
264         for (pp = path; *pp; pp++) {
265                 if ((val = chkdir(*pp, DIR_STOP, DIR_LAST)) == 0)
266                         break;
267                 x += diroff[val][0];
268                 y += diroff[val][1];
269                 c = dirch[val];
270                 if (c == DIR_STOP)
271                         break;
272                 if (!getsect(x, y, &s))
273                         return -1.0;
274                 m += cost(&s, s.sct_own);
275         }
276         *xp = xnorm(x);
277         *yp = ynorm(y);
278         return m;
279 }
280
281 /*
282  * return true if "who" owns the path starting at x,y
283  */
284 int
285 chkpath(natid who, s_char *path, coord x, coord y)
286 {
287         s_char  *pp;
288         int     val;
289         struct  sctstr sect;
290
291         for (pp = path; *pp; pp++) {
292                 if ((val = chkdir(*pp, DIR_STOP, DIR_LAST)) == 0)
293                         break;
294                 x += diroff[val][0];
295                 y += diroff[val][1];
296                 if (!getsect(x, y, &sect) || sect.sct_own != who)
297                         return 0;
298         }
299         return 1;
300 }
301
302 void
303 pathrange(register coord cx, register coord cy, register s_char *pp, int border, struct range *range)
304 {
305         int     dir;
306
307         range->lx = cx;
308         range->hx = cx;
309         range->ly = cy;
310         range->hy = cy;
311         range->width = 0;
312         range->height = 0;
313         while ((dir = chkdir(*pp, DIR_FIRST, DIR_LAST)) >= 0) {
314                 pp++;
315                 cx += diroff[dir][0];
316                 cy += diroff[dir][1];
317                 if (cx < range->lx)
318                         range->lx = cx;
319                 if (cx > range->hx)
320                         range->hx = cx;
321                 if (cy < range->ly)
322                         range->ly = cy;
323                 if (cy > range->hy)
324                         range->hy = cy;
325         }
326         range->lx = xnorm(range->lx - border*2);
327         range->ly = ynorm(range->ly - border);
328         range->hx = xnorm(range->hx + border*2 + 1);
329         range->hy = ynorm(range->hy + border + 1);
330         xysize_range(range);
331 }
332