2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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
43 #include "prototypes.h"
46 static int move_map(coord curx, coord cury, char *arg);
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)
56 coord curx, cury, oldx, oldy;
64 double mobility = start->sct_mobil;
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");
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);
87 pr("No owned path exists!\n");
89 len = path_find_route(buf, sizeof(buf),
90 start->sct_x, start->sct_y, dx, dy);
92 if (len < sizeof(buf))
93 strcpy(buf + len, "h");
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));
101 pr("Using best path '%s', movement cost %1.3f\n",
103 if (total_mcost * weight > mobility) {
104 pr("Not enough mobility to go all the way."
105 " Nothing moved.\n");
115 if (getsect(curx, cury, §) < 0) {
116 logerror("move_path: getsect %d,%d", curx, cury);
122 if (!movstr || *movstr == 0) {
124 map(curx, cury, NULL, NULL);
126 move_map(curx, cury, NULL);
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);
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);
137 pr("Can't get to %s from here!\n",
138 xyas(dx, dy, player->cnum));
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");
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));
151 if ((mv_cost * weight) > mobility) {
152 pr("Not enough mobility to go all the way. Nothing moved.\n");
156 pr("Using best path '%s', movement cost %1.3f\n",
162 if (!movstr || *movstr == 0) {
163 buf[0] = dirch[DIR_STOP];
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");
173 if (dir == DIR_MAP) {
174 parse(movstr, scanspace, argp, NULL, NULL, NULL);
176 map(curx, cury, argp[1], argp[2]);
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]);
189 * now see if we can move into the
190 * next sector. Mobility, terrain,
191 * or ownership may prevent us.
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");
201 if ((next.sct_type == SCT_SANCT) &&
202 (next.sct_own != player->cnum)) {
203 pr("Converts, huh?\n");
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");
217 sect_mcost *= weight;
218 if (sect_mcost > mobility) {
219 pr("Not enough mobility. ");
220 pr("You can't go there...\n");
224 mobility -= sect_mcost;
225 total_mcost += sect_mcost;
229 if (cury != start->sct_y)
231 if (curx != start->sct_x)
237 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
241 * Check and see if anyone will interdict us
243 if (takedam && chance(weight / 100.0) &&
244 ((curx != oldx) || (cury != oldy)))
245 *dam += ground_interdict(curx, cury, player->cnum,
251 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
255 return roundavg(total_mcost);
261 move_map(coord curx, coord cury, char *arg)
269 snxtsct_dist(&ns, curx, cury, 1);
271 while (i < 7 && nxtsct(&ns, §)) {
272 /* Nasty: this relies on the iteration order */
273 view[i] = dchr[sect.sct_type].d_mnem;
274 switch (sect.sct_type) {
282 if (sect.sct_own != player->cnum && !player->god)
286 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
290 writemap(player->cnum);
291 if (!getsect(curx, cury, §))
293 pr(" %c %c eff mob civ mil uw food work avail\n",
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]);
305 fly_map(coord curx, coord cury)
312 snxtsct_dist(&ns, curx, cury, 1);
314 while (i < 7 && nxtsct(&ns, §)) {
315 /* Nasty: this relies on the iteration order */
316 if (!(view[i] = player->bmap[sect.sct_uid]))
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]);
328 check_lmines(coord x, coord y, double weight)
333 getsect(x, y, §);
334 if (SCT_LANDMINES(§) > 0 &&
335 sect.sct_oldown != player->cnum &&
336 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
338 pr("Blammo! Landmines detected! in %s ",
339 xyas(sect.sct_x, sect.sct_y, player->cnum));
343 pr("%d damage sustained.\n", dam);