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