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