]> git.pond.sub.org Git - empserver/blob - src/lib/subs/move.c
(bzero, bcopy): Obsolete BSDisms; remove. Remove some calls without
[empserver] / src / lib / subs / move.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  *  move.c: Move something somewhere.
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "item.h"
39 #include "file.h"
40 #include "deity.h"
41 #include "xy.h"
42 #include "path.h"
43 #include "nat.h"
44 #include "map.h"
45 #include "nsc.h"
46 #include "damage.h"
47 #include "prototypes.h"
48
49 static int move_map(s_char *what, coord curx, coord cury, s_char *arg);
50
51 int
52 move_ground(s_char *what, struct sctstr *start, struct sctstr *end,
53             double mobility, double weight, s_char *path,
54             int (*map) (s_char *, coord, coord, s_char *), int exploring,
55             int *dam)
56
57
58                                 /* RESULT */
59 {
60     struct sctstr sect, ending_sect;
61     struct sctstr next, dsect;
62     int vec[I_MAX + 1];
63     coord curx, cury, oldx, oldy;
64     coord tmpx, tmpy;
65     coord dx, dy;
66     s_char *movstr, *BestLandPath(s_char *, struct sctstr *,
67                                   struct sctstr *, double *, int);
68     double sect_mcost;
69     double total_mcost;
70     double mv_cost;
71     int dir;
72     int intcost;
73     int takedam = (*dam), out = 0;
74     s_char bpath[512];
75     s_char buf2[512];
76     s_char prompt[128];
77     s_char buf[1024];
78
79     if (mobility <= 0.0)
80         return -1;
81     *dam = 0;
82     if (path && sarg_xy(path, &dx, &dy) && getsect(dx, dy, &ending_sect)) {
83         if ((ending_sect.sct_x == start->sct_x) &&
84             (ending_sect.sct_y == start->sct_y)) {
85             pr("Start sector is ending sector!\n");
86             return -1;
87         }
88         pr("Looking for best path to %s\n", path);
89         path =
90             BestLandPath(buf2, start, &ending_sect, &total_mcost,
91                          MOB_ROAD);
92         if (exploring && (path != (s_char *)0)) /* take off the 'h' */
93             *(path + strlen(path) - 1) = '\0';
94         if (path == (s_char *)0)
95             pr("No owned path exists!\n");
96         else {
97             pr("Using best path '%s', movement cost %1.3f\n",
98                path, total_mcost);
99             strncpy(bpath, path, sizeof(bpath));
100             path = bpath;
101         }
102         if ((total_mcost * weight) > mobility) {
103             pr("Not enough mobility to go all the way. Nothing moved.\n");
104             *end = *start;
105             return -1;
106         }
107     }
108     movstr = path;
109     tmpx = start->sct_x;
110     curx = tmpx;
111     tmpy = start->sct_y;
112     cury = tmpy;
113     total_mcost = 0.0;
114     if (getsect(curx, cury, &sect) < 0) {
115         logerror("move_path: getsect %d,%d", curx, cury);
116         return -1;
117     }
118     for (;;) {
119         tmpx = curx;
120         tmpy = cury;
121         oldx = curx;
122         oldy = cury;
123         if (movstr == 0 || *movstr == 0) {
124             if (exploring) {
125                 map(what, curx, cury, (s_char *)0);
126             } else {
127                 move_map(what, curx, cury, (s_char *)0);
128             }
129             sprintf(prompt, "<%.1f: %c %s> ", mobility,
130                     dchr[sect.sct_type].d_mnem,
131                     xyas(sect.sct_x, sect.sct_y, player->cnum));
132             movstr = getstring(prompt, buf);
133         }
134         if (movstr && sarg_xy(movstr, &dx, &dy)) {
135             if (getsect(dx, dy, &dsect)) {
136                 movstr =
137                     BestLandPath(buf2, &sect, &dsect, &mv_cost, MOB_ROAD);
138             } else {
139                 pr("Invalid destination sector!\n");
140                 movstr = (s_char *)0;
141             }
142
143             if (movstr == (s_char *)0) {
144                 pr("Can't get to %s from here!\n",
145                    xyas(dx, dy, player->cnum));
146                 movstr = (s_char *)0;
147             } else {
148                 if ((mv_cost * weight) > mobility) {
149                     pr("Not enough mobility to go all the way. Nothing moved.\n");
150                     movstr = (s_char *)0;
151                 } else {
152                     pr("Using best path '%s', movement cost %1.3f\n",
153                        movstr, mv_cost);
154                     strncpy(bpath, movstr, sizeof(bpath));
155                     movstr = bpath;
156                 }
157             }
158         }
159         if (movstr == 0 || *movstr == 0)
160             movstr = dirch;
161         if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
162             pr("\"%c\" is not legal...", *movstr);
163             direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
164             *movstr = 0;
165             continue;
166         }
167         movstr++;
168         if (dir == DIR_MAP) {
169             if (!exploring)
170                 map(what, curx, cury, movstr + 1);
171             *movstr = 0;
172             continue;
173         } else if (dir == DIR_STOP)
174             break;
175         else if (dir == DIR_VIEW) {
176             pr("%d%% %s with %d civilians.\n", sect.sct_effic,
177                dchr[sect.sct_type].d_name,
178                getvar(V_CIVIL, (s_char *)&sect, EF_SECTOR));
179             continue;
180         }
181         /*
182          * now see if we can move into the
183          * next sector.  Mobility, terrain,
184          * or ownership may prevent us.
185          */
186         tmpx += diroff[dir][0];
187         tmpy += diroff[dir][1];
188         if (getsect(tmpx, tmpy, &next) < 0) {
189             pr("You can't go there...\n");
190             *movstr = 0;
191             continue;
192         }
193         if (!player->god) {
194             if ((next.sct_type == SCT_SANCT) &&
195                 (next.sct_own != player->cnum)) {
196                 pr("Converts, huh?\n");
197                 *end = next;
198                 intcost = (int)total_mcost;
199                 if (chance(total_mcost - intcost))
200                     intcost++;
201                 return intcost;
202             }
203             getvec(VT_ITEM, vec, (s_char *)&next, EF_SECTOR);
204             sect_mcost = sector_mcost(&next, MOB_ROAD);
205             if ((!player->owner && (!exploring ||
206                                     (vec[I_MILIT] || vec[I_CIVIL]))) ||
207                 sect_mcost == -1.0) {
208                 /* already-owned, or prohibited terrain */
209                 pr("You can't go there...\n");
210                 *movstr = 0;
211                 continue;
212             }
213             sect_mcost *= weight;
214             if (sect_mcost > mobility) {
215                 pr("Not enough mobility.  ");
216                 pr("You can't go there...\n");
217                 *movstr = 0;
218                 continue;
219             }
220             mobility -= sect_mcost;
221             total_mcost += sect_mcost;
222         }
223         curx = tmpx;
224         cury = tmpy;
225         if (cury != start->sct_y)
226             out = 1;
227         if (curx != start->sct_x)
228             out = 1;
229
230         sect = next;
231
232         if (takedam)
233             *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
234         if (*dam >= 100)
235             break;
236         /*
237          * Check and see if anyone will interdict us
238          */
239         if (takedam && chance(weight / 100.0) &&
240             ((curx != oldx) || (cury != oldy)))
241             (*dam) +=
242                 ground_interdict(curx, cury, player->cnum, "commodities");
243         if (*dam >= 100)
244             break;
245     }
246     *end = sect;
247     intcost = (int)total_mcost;
248     if (intcost < 0)
249         return -1;
250     if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
251         && !out)
252         return -1;
253
254     if (chance(total_mcost - intcost))
255         intcost++;
256     return intcost;
257 }
258
259
260 /*ARGSUSED*/
261 static int
262 move_map(s_char *what, coord curx, coord cury, s_char *arg)
263 {
264     struct nstr_sect ns;
265     struct natstr *np;
266     struct sctstr sect;
267     coord rel_x, rel_y;
268     s_char range[128];
269     s_char view[7];
270     int i;
271     int vec[I_MAX + 1];
272     int changed = 0;
273
274     np = getnatp(player->cnum);
275     rel_x = xrel(np, curx);
276     rel_y = yrel(np, cury);
277     sprintf(range, "%d:%d,%d:%d", rel_x - 2, rel_x + 2, rel_y - 1,
278             rel_y + 1);
279     player->condarg = 0;
280     /* This is necessary, otherwise move_map would attempt to pay */
281     /* attention to the conditional arguments left behind by such */
282     /* a command as "tran p -1,-1 ?eff=100".. It'd then only see  */
283     /* 100% efficienct sects, and get all screwed up         --ts */
284     if (!snxtsct(&ns, range))
285         return RET_FAIL;
286     i = 0;
287     while (i < 7 && nxtsct(&ns, &sect)) {
288         view[i] = dchr[sect.sct_type].d_mnem;
289         switch (sect.sct_type) {
290         case SCT_WATER:
291         case SCT_RURAL:
292         case SCT_MOUNT:
293         case SCT_WASTE:
294         case SCT_PLAINS:
295             break;
296         default:
297             if (sect.sct_own != player->cnum && !player->god)
298                 view[i] = '?';
299             break;
300         }
301         changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
302         i++;
303     }
304     if (changed)
305         writemap(player->cnum);
306     if (!getsect(curx, cury, &sect))
307         return RET_FAIL;
308     getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
309     pr("    %c %c      eff   mob   civ  mil   uw food  work  avail\n",
310        view[0], view[1]);
311     pr("   %c %c %c     %3d   %3d  %4d %4d %4d %4d   %3d   %3d\n",
312        view[2], view[3], view[4],
313        sect.sct_effic, sect.sct_mobil, vec[I_CIVIL], vec[I_MILIT],
314        vec[I_UW], vec[I_FOOD], sect.sct_work, sect.sct_avail);
315     pr("    %c %c\n", view[5], view[6]);
316     return RET_OK;
317 }
318
319 int
320 fly_map(coord curx, coord cury)
321 {
322     struct nstr_sect ns;
323     struct natstr *np;
324     struct sctstr sect;
325     coord rel_x, rel_y;
326     s_char view[7];
327     int i;
328     s_char range[128];
329
330     np = getnatp(player->cnum);
331     rel_x = xrel(np, curx);
332     rel_y = yrel(np, cury);
333     sprintf(range, "%d:%d,%d:%d", rel_x - 2, rel_x + 2, rel_y - 1,
334             rel_y + 1);
335     player->condarg = 0;
336     /* This is necessary, otherwise move_map would attempt to pay */
337     /* attention to the conditional arguments left behind by such */
338     /* a command as "tran p -1,-1 ?eff=100".. It'd then only see  */
339     /* 100% efficienct sects, and get all screwed up         --ts */
340
341     if (!snxtsct(&ns, range))
342         return RET_FAIL;
343     i = 0;
344     while (i < 7 && nxtsct(&ns, &sect)) {
345         if (!(view[i] = player->bmap[sctoff(ns.x, ns.y)]))
346             view[i] = ' ';
347         i++;
348     }
349
350     pr("    %c %c\n", view[0], view[1]);
351     pr("   %c %c %c\n", view[2], view[3], view[4]);
352     pr("    %c %c\n", view[5], view[6]);
353     return RET_OK;
354 }
355
356 int
357 check_lmines(coord x, coord y, double weight)
358 {
359     struct sctstr sect;
360     int mines;
361     int dam = 0;
362
363     getsect(x, y, &sect);
364     mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR);
365     if (mines > 0 &&
366         sect.sct_oldown != player->cnum &&
367         chance(DMINE_LHITCHANCE(mines)) && chance(weight / 100.0)) {
368         pr_beep();
369         pr("Blammo! Landmines detected! in %s  ",
370            xyas(sect.sct_x, sect.sct_y, player->cnum));
371         dam = roll(20);
372         --mines;
373         putvar(V_MINE, mines, (s_char *)&sect, EF_SECTOR);
374         putsect(&sect);
375         pr("%d damage sustained.\n", dam);
376     }
377     return dam;
378 }