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
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-2011
41 #include "prototypes.h"
44 static int move_map(coord curx, coord cury, char *arg);
47 move_ground(struct sctstr *start, struct sctstr *end,
48 double weight, char *path,
49 int (*map)(coord, coord, char *), int exploring,
54 coord curx, cury, oldx, oldy;
62 double mobility = start->sct_mobil;
74 if (path && sarg_xy(path, &dx, &dy)) {
75 if (dx == start->sct_x && dy == start->sct_y) {
76 pr("Start sector is ending sector!\n");
79 pr("Looking for best path to %s\n", path);
80 total_mcost = path_find(start->sct_x, start->sct_y, dx, dy,
81 player->cnum, MOB_MOVE);
84 pr("No owned path exists!\n");
86 len = path_find_route(buf, sizeof(buf),
87 start->sct_x, start->sct_y, dx, dy);
89 if (len < sizeof(buf))
90 strcpy(buf + len, "h");
93 if (len >= sizeof(buf))
94 pr("Can't handle path to %s, it's too long, sorry.\n",
95 xyas(dx, dy, player->cnum));
98 pr("Using best path '%s', movement cost %1.3f\n",
100 if (total_mcost * weight > mobility) {
101 pr("Not enough mobility to go all the way."
102 " Nothing moved.\n");
112 if (getsect(curx, cury, §) < 0) {
113 logerror("move_path: getsect %d,%d", curx, cury);
119 if (!movstr || *movstr == 0) {
121 map(curx, cury, NULL);
123 move_map(curx, cury, NULL);
125 sprintf(prompt, "<%.1f: %c %s> ", mobility,
126 dchr[sect.sct_type].d_mnem,
127 xyas(sect.sct_x, sect.sct_y, player->cnum));
128 movstr = getstring(prompt, buf);
130 if (movstr && sarg_xy(movstr, &dx, &dy)) {
131 mv_cost = path_find(sect.sct_x, sect.sct_y, dx, dy,
132 player->cnum, MOB_MOVE);
134 pr("Can't get to %s from here!\n",
135 xyas(dx, dy, player->cnum));
138 len = path_find_route(buf, sizeof(buf),
139 sect.sct_x, sect.sct_y, dx, dy);
140 if (len < sizeof(buf))
141 strcpy(buf + len, "h");
143 if (len >= sizeof(buf)) {
144 pr("Can't handle path to %s, it's too long, sorry.\n",
145 xyas(dx, dy, player->cnum));
148 if ((mv_cost * weight) > mobility) {
149 pr("Not enough mobility to go all the way. Nothing moved.\n");
153 pr("Using best path '%s', movement cost %1.3f\n",
159 if (!movstr || *movstr == 0) {
160 buf[0] = dirch[DIR_STOP];
164 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
165 pr("\"%c\" is not legal...", *movstr);
166 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
170 do movstr++; while (isspace(*movstr));
171 if (dir == DIR_MAP) {
173 map(curx, cury, movstr);
176 } else if (dir == DIR_STOP)
178 else if (dir == DIR_VIEW) {
179 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
180 dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
184 * now see if we can move into the
185 * next sector. Mobility, terrain,
186 * or ownership may prevent us.
188 tmpx = curx + diroff[dir][0];
189 tmpy = cury + diroff[dir][1];
190 if (getsect(tmpx, tmpy, &next) < 0) {
191 pr("You can't go there...\n");
196 if ((next.sct_type == SCT_SANCT) &&
197 (next.sct_own != player->cnum)) {
198 pr("Converts, huh?\n");
202 sect_mcost = sector_mcost(&next, MOB_MOVE);
203 if ((!player->owner && (!exploring
204 || next.sct_item[I_MILIT]
205 || next.sct_item[I_CIVIL]))
206 || sect_mcost == -1.0) {
207 /* already-owned, or prohibited terrain */
208 pr("You can't go there...\n");
212 sect_mcost *= weight;
213 if (sect_mcost > mobility) {
214 pr("Not enough mobility. ");
215 pr("You can't go there...\n");
219 mobility -= sect_mcost;
220 total_mcost += sect_mcost;
224 if (cury != start->sct_y)
226 if (curx != start->sct_x)
232 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
236 * Check and see if anyone will interdict us
238 if (takedam && chance(weight / 100.0) &&
239 ((curx != oldx) || (cury != oldy)))
240 *dam += ground_interdict(curx, cury, player->cnum,
246 intcost = (int)total_mcost;
249 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
253 if (chance(total_mcost - intcost))
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);