]> git.pond.sub.org Git - empserver/blob - src/lib/subs/move.c
748548edb1d521c15646b94dc9037bebd2f14977
[empserver] / src / lib / subs / move.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  move.c: Move something somewhere.
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2011
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "damage.h"
37 #include "file.h"
38 #include "map.h"
39 #include "nsc.h"
40 #include "path.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "sect.h"
44
45 static int move_map(coord curx, coord cury, char *arg);
46
47 int
48 move_ground(struct sctstr *start, struct sctstr *end,
49             double weight, char *path,
50             int (*map)(coord, coord, char *, char *),
51             int exploring, int *dam)
52 {
53     struct sctstr sect;
54     struct sctstr next;
55     coord curx, cury, oldx, oldy;
56     coord tmpx, tmpy;
57     coord dx, dy;
58     char *movstr;
59     double sect_mcost;
60     double total_mcost;
61     double mv_cost;
62     size_t len;
63     double mobility = start->sct_mobil;
64     int dir;
65     char scanspace[1024];
66     char *argp[128];
67     int ac;
68     int intcost;
69     int takedam = *dam;
70     int out = 0;
71     char prompt[128];
72     char buf[1024];
73
74     *end = *start;
75     if (mobility <= 0.0)
76         return -1;
77     *dam = 0;
78     if (path && sarg_xy(path, &dx, &dy)) {
79         if (dx == start->sct_x && dy == start->sct_y) {
80             pr("Start sector is ending sector!\n");
81             return -1;
82         }
83         pr("Looking for best path to %s\n", path);
84         total_mcost = path_find(start->sct_x, start->sct_y, dx, dy,
85                                 player->cnum, MOB_MOVE);
86         path = NULL;
87         if (total_mcost < 0)
88             pr("No owned path exists!\n");
89         else {
90             len = path_find_route(buf, sizeof(buf),
91                                   start->sct_x, start->sct_y, dx, dy);
92             if (!exploring) {
93                 if (len < sizeof(buf))
94                     strcpy(buf + len, "h");
95                 len++;
96             }
97             if (len >= sizeof(buf))
98                 pr("Can't handle path to %s, it's too long, sorry.\n",
99                    xyas(dx, dy, player->cnum));
100             else {
101                 path = buf;
102                 pr("Using best path '%s', movement cost %1.3f\n",
103                    path, total_mcost);
104                 if (total_mcost * weight > mobility) {
105                     pr("Not enough mobility to go all the way."
106                        " Nothing moved.\n");
107                     return -1;
108                 }
109             }
110         }
111     }
112     movstr = path;
113     curx = start->sct_x;
114     cury = start->sct_y;
115     total_mcost = 0.0;
116     if (getsect(curx, cury, &sect) < 0) {
117         logerror("move_path: getsect %d,%d", curx, cury);
118         return -1;
119     }
120     for (;;) {
121         oldx = curx;
122         oldy = cury;
123         if (!movstr || *movstr == 0) {
124             if (exploring) {
125                 map(curx, cury, NULL, NULL);
126             } else {
127                 move_map(curx, cury, NULL);
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             mv_cost = path_find(sect.sct_x, sect.sct_y, dx, dy,
136                                 player->cnum, MOB_MOVE);
137             if (mv_cost < 0) {
138                 pr("Can't get to %s from here!\n",
139                    xyas(dx, dy, player->cnum));
140                 movstr = NULL;
141             } else {
142                 len = path_find_route(buf, sizeof(buf),
143                                       sect.sct_x, sect.sct_y, dx, dy);
144                 if (len < sizeof(buf))
145                     strcpy(buf + len, "h");
146                 len++;
147                 if (len >= sizeof(buf)) {
148                     pr("Can't handle path to %s, it's too long, sorry.\n",
149                        xyas(dx, dy, player->cnum));
150                     movstr = NULL;
151                 } else {
152                     if ((mv_cost * weight) > mobility) {
153                         pr("Not enough mobility to go all the way. Nothing moved.\n");
154                         movstr = NULL;
155                     } else {
156                         movstr = buf;
157                         pr("Using best path '%s', movement cost %1.3f\n",
158                            movstr, mv_cost);
159                     }
160                 }
161             }
162         }
163         if (!movstr || *movstr == 0) {
164             buf[0] = dirch[DIR_STOP];
165             buf[1] = 0;
166             movstr = buf;
167         }
168         if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
169             pr("\"%c\" is not legal...", *movstr);
170             direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
171             *movstr = 0;
172             continue;
173         }
174         if (dir == DIR_MAP) {
175             ac = parse(movstr, scanspace, argp, NULL, NULL, NULL);
176             if (ac == 1) {
177                 pr("Use of '%c' without a space before its argument is deprecated.\n"
178                    "Support for it will go away in a future release\n",
179                    *movstr);
180                 argp[1] = argp[0] + 1;
181             }
182             if (!exploring)
183                 map(curx, cury, argp[1], argp[2]);
184             *movstr = 0;
185             continue;
186         }
187         movstr++;
188         if (dir == DIR_STOP)
189             break;
190         if (dir == DIR_VIEW) {
191             pr("%d%% %s with %d civilians.\n", sect.sct_effic,
192                dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
193             continue;
194         }
195         /*
196          * now see if we can move into the
197          * next sector.  Mobility, terrain,
198          * or ownership may prevent us.
199          */
200         tmpx = curx + diroff[dir][0];
201         tmpy = cury + diroff[dir][1];
202         if (getsect(tmpx, tmpy, &next) < 0) {
203             pr("You can't go there...\n");
204             *movstr = 0;
205             continue;
206         }
207         if (!player->god) {
208             if ((next.sct_type == SCT_SANCT) &&
209                 (next.sct_own != player->cnum)) {
210                 pr("Converts, huh?\n");
211                 *movstr = 0;
212                 continue;
213             }
214             sect_mcost = sector_mcost(&next, MOB_MOVE);
215             if ((!player->owner && (!exploring
216                                     || next.sct_item[I_MILIT]
217                                     || next.sct_item[I_CIVIL]))
218                 || sect_mcost == -1.0) {
219                 /* already-owned, or prohibited terrain */
220                 pr("You can't go there...\n");
221                 *movstr = 0;
222                 continue;
223             }
224             sect_mcost *= weight;
225             if (sect_mcost > mobility) {
226                 pr("Not enough mobility.  ");
227                 pr("You can't go there...\n");
228                 *movstr = 0;
229                 continue;
230             }
231             mobility -= sect_mcost;
232             total_mcost += sect_mcost;
233         }
234         curx = next.sct_x;
235         cury = next.sct_y;
236         if (cury != start->sct_y)
237             out = 1;
238         if (curx != start->sct_x)
239             out = 1;
240
241         sect = next;
242
243         if (takedam)
244             *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
245         if (*dam >= 100)
246             break;
247         /*
248          * Check and see if anyone will interdict us
249          */
250         if (takedam && chance(weight / 100.0) &&
251             ((curx != oldx) || (cury != oldy)))
252             *dam += ground_interdict(curx, cury, player->cnum,
253                                      "commodities");
254         if (*dam >= 100)
255             break;
256     }
257     *end = sect;
258     intcost = (int)total_mcost;
259     if (intcost < 0)
260         return -1;
261     if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
262         && !out)
263         return -1;
264
265     if (chance(total_mcost - intcost))
266         intcost++;
267     return intcost;
268 }
269
270
271 /*ARGSUSED*/
272 static int
273 move_map(coord curx, coord cury, char *arg)
274 {
275     struct nstr_sect ns;
276     struct sctstr sect;
277     char view[7];
278     int i;
279     int changed = 0;
280
281     snxtsct_dist(&ns, curx, cury, 1);
282     i = 0;
283     while (i < 7 && nxtsct(&ns, &sect)) {
284         /* Nasty: this relies on the iteration order */
285         view[i] = dchr[sect.sct_type].d_mnem;
286         switch (sect.sct_type) {
287         case SCT_WATER:
288         case SCT_RURAL:
289         case SCT_MOUNT:
290         case SCT_WASTE:
291         case SCT_PLAINS:
292             break;
293         default:
294             if (sect.sct_own != player->cnum && !player->god)
295                 view[i] = '?';
296             break;
297         }
298         changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
299         i++;
300     }
301     if (changed)
302         writemap(player->cnum);
303     if (!getsect(curx, cury, &sect))
304         return RET_FAIL;
305     pr("    %c %c      eff   mob   civ  mil   uw food  work  avail\n",
306        view[0], view[1]);
307     pr("   %c %c %c     %3d   %3d  %4d %4d %4d %4d   %3d   %3d\n",
308        view[2], view[3], view[4],
309        sect.sct_effic, sect.sct_mobil,
310        sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
311        sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
312     pr("    %c %c\n", view[5], view[6]);
313     return RET_OK;
314 }
315
316 int
317 fly_map(coord curx, coord cury)
318 {
319     struct nstr_sect ns;
320     struct sctstr sect;
321     char view[7];
322     int i;
323
324     snxtsct_dist(&ns, curx, cury, 1);
325     i = 0;
326     while (i < 7 && nxtsct(&ns, &sect)) {
327         /* Nasty: this relies on the iteration order */
328         if (!(view[i] = player->bmap[sect.sct_uid]))
329             view[i] = ' ';
330         i++;
331     }
332
333     pr("    %c %c\n", view[0], view[1]);
334     pr("   %c %c %c\n", view[2], view[3], view[4]);
335     pr("    %c %c\n", view[5], view[6]);
336     return RET_OK;
337 }
338
339 int
340 check_lmines(coord x, coord y, double weight)
341 {
342     struct sctstr sect;
343     int dam = 0;
344
345     getsect(x, y, &sect);
346     if (SCT_LANDMINES(&sect) > 0 &&
347         sect.sct_oldown != player->cnum &&
348         chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
349         pr_beep();
350         pr("Blammo! Landmines detected! in %s  ",
351            xyas(sect.sct_x, sect.sct_y, player->cnum));
352         dam = roll(20);
353         --sect.sct_mines;
354         putsect(&sect);
355         pr("%d damage sustained.\n", dam);
356     }
357     return dam;
358 }