2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * move.c: Move something somewhere.
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2012
42 #include "prototypes.h"
45 static int move_map(coord curx, coord cury, char *arg);
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)
55 coord curx, cury, oldx, oldy;
63 double mobility = start->sct_mobil;
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");
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);
86 pr("No owned path exists!\n");
88 len = path_find_route(buf, sizeof(buf),
89 start->sct_x, start->sct_y, dx, dy);
91 if (len < sizeof(buf))
92 strcpy(buf + len, "h");
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));
100 pr("Using best path '%s', movement cost %1.3f\n",
102 if (total_mcost * weight > mobility) {
103 pr("Not enough mobility to go all the way."
104 " Nothing moved.\n");
114 if (!getsect(curx, cury, §)) {
115 logerror("move_path: getsect %d,%d", curx, cury);
121 if (!movstr || *movstr == 0) {
123 map(curx, cury, NULL, NULL);
125 move_map(curx, cury, NULL);
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);
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);
136 pr("Can't get to %s from here!\n",
137 xyas(dx, dy, player->cnum));
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");
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));
150 if ((mv_cost * weight) > mobility) {
151 pr("Not enough mobility to go all the way. Nothing moved.\n");
155 pr("Using best path '%s', movement cost %1.3f\n",
161 if (!movstr || *movstr == 0) {
162 buf[0] = dirch[DIR_STOP];
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");
172 if (dir == DIR_MAP) {
173 parse(movstr, scanspace, argp, NULL, NULL, NULL);
175 map(curx, cury, argp[1], argp[2]);
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]);
188 * now see if we can move into the
189 * next sector. Mobility, terrain,
190 * or ownership may prevent us.
192 tmpx = curx + diroff[dir][0];
193 tmpy = cury + diroff[dir][1];
194 if (!getsect(tmpx, tmpy, &next)) {
195 pr("You can't go there...\n");
200 if ((next.sct_type == SCT_SANCT) &&
201 (next.sct_own != player->cnum)) {
202 pr("Converts, huh?\n");
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");
216 sect_mcost *= weight;
217 if (sect_mcost > mobility) {
218 pr("Not enough mobility. ");
219 pr("You can't go there...\n");
223 mobility -= sect_mcost;
224 total_mcost += sect_mcost;
228 if (cury != start->sct_y)
230 if (curx != start->sct_x)
236 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
240 * Check and see if anyone will interdict us
242 if (takedam && chance(weight / 100.0) &&
243 ((curx != oldx) || (cury != oldy)))
244 *dam += ground_interdict(curx, cury, player->cnum,
250 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
254 return roundavg(total_mcost);
260 move_map(coord curx, coord cury, char *arg)
268 snxtsct_dist(&ns, curx, cury, 1);
270 while (i < 7 && nxtsct(&ns, §)) {
271 /* Nasty: this relies on the iteration order */
272 view[i] = dchr[sect.sct_type].d_mnem;
273 switch (sect.sct_type) {
281 if (sect.sct_own != player->cnum && !player->god)
285 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
289 writemap(player->cnum);
290 if (!getsect(curx, cury, §))
292 pr(" %c %c eff mob civ mil uw food work avail\n",
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]);
304 fly_map(coord curx, coord cury)
311 snxtsct_dist(&ns, curx, cury, 1);
313 while (i < 7 && nxtsct(&ns, §)) {
314 /* Nasty: this relies on the iteration order */
315 if (!(view[i] = player->bmap[sect.sct_uid]))
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]);
327 check_lmines(coord x, coord y, double weight)
332 getsect(x, y, §);
333 if (SCT_LANDMINES(§) > 0 &&
334 sect.sct_oldown != player->cnum &&
335 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
337 pr("Blammo! Landmines detected! in %s ",
338 xyas(sect.sct_x, sect.sct_y, player->cnum));
342 pr("%d damage sustained.\n", dam);