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
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 *), int exploring,
55 coord curx, cury, oldx, oldy;
63 double mobility = start->sct_mobil;
75 if (path && sarg_xy(path, &dx, &dy)) {
76 if (dx == start->sct_x && dy == start->sct_y) {
77 pr("Start sector is ending sector!\n");
80 pr("Looking for best path to %s\n", path);
81 total_mcost = path_find(start->sct_x, start->sct_y, dx, dy,
82 player->cnum, MOB_MOVE);
85 pr("No owned path exists!\n");
87 len = path_find_route(buf, sizeof(buf),
88 start->sct_x, start->sct_y, dx, dy);
90 if (len < sizeof(buf))
91 strcpy(buf + len, "h");
94 if (len >= sizeof(buf))
95 pr("Can't handle path to %s, it's too long, sorry.\n",
96 xyas(dx, dy, player->cnum));
99 pr("Using best path '%s', movement cost %1.3f\n",
101 if (total_mcost * weight > mobility) {
102 pr("Not enough mobility to go all the way."
103 " Nothing moved.\n");
113 if (getsect(curx, cury, §) < 0) {
114 logerror("move_path: getsect %d,%d", curx, cury);
120 if (!movstr || *movstr == 0) {
122 map(curx, cury, NULL);
124 move_map(curx, cury, NULL);
126 sprintf(prompt, "<%.1f: %c %s> ", mobility,
127 dchr[sect.sct_type].d_mnem,
128 xyas(sect.sct_x, sect.sct_y, player->cnum));
129 movstr = getstring(prompt, buf);
131 if (movstr && sarg_xy(movstr, &dx, &dy)) {
132 mv_cost = path_find(sect.sct_x, sect.sct_y, dx, dy,
133 player->cnum, MOB_MOVE);
135 pr("Can't get to %s from here!\n",
136 xyas(dx, dy, player->cnum));
139 len = path_find_route(buf, sizeof(buf),
140 sect.sct_x, sect.sct_y, dx, dy);
141 if (len < sizeof(buf))
142 strcpy(buf + len, "h");
144 if (len >= sizeof(buf)) {
145 pr("Can't handle path to %s, it's too long, sorry.\n",
146 xyas(dx, dy, player->cnum));
149 if ((mv_cost * weight) > mobility) {
150 pr("Not enough mobility to go all the way. Nothing moved.\n");
154 pr("Using best path '%s', movement cost %1.3f\n",
160 if (!movstr || *movstr == 0) {
161 buf[0] = dirch[DIR_STOP];
165 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
166 pr("\"%c\" is not legal...", *movstr);
167 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
171 do movstr++; while (isspace(*movstr));
172 if (dir == DIR_MAP) {
174 map(curx, cury, movstr);
177 } else if (dir == DIR_STOP)
179 else if (dir == DIR_VIEW) {
180 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
181 dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
185 * now see if we can move into the
186 * next sector. Mobility, terrain,
187 * or ownership may prevent us.
189 tmpx = curx + diroff[dir][0];
190 tmpy = cury + diroff[dir][1];
191 if (getsect(tmpx, tmpy, &next) < 0) {
192 pr("You can't go there...\n");
197 if ((next.sct_type == SCT_SANCT) &&
198 (next.sct_own != player->cnum)) {
199 pr("Converts, huh?\n");
203 sect_mcost = sector_mcost(&next, MOB_MOVE);
204 if ((!player->owner && (!exploring
205 || next.sct_item[I_MILIT]
206 || next.sct_item[I_CIVIL]))
207 || sect_mcost == -1.0) {
208 /* already-owned, or prohibited terrain */
209 pr("You can't go there...\n");
213 sect_mcost *= weight;
214 if (sect_mcost > mobility) {
215 pr("Not enough mobility. ");
216 pr("You can't go there...\n");
220 mobility -= sect_mcost;
221 total_mcost += sect_mcost;
225 if (cury != start->sct_y)
227 if (curx != start->sct_x)
233 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
237 * Check and see if anyone will interdict us
239 if (takedam && chance(weight / 100.0) &&
240 ((curx != oldx) || (cury != oldy)))
241 *dam += ground_interdict(curx, cury, player->cnum,
247 intcost = (int)total_mcost;
250 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
254 if (chance(total_mcost - intcost))
262 move_map(coord curx, coord cury, char *arg)
270 snxtsct_dist(&ns, curx, cury, 1);
272 while (i < 7 && nxtsct(&ns, §)) {
273 /* Nasty: this relies on the iteration order */
274 view[i] = dchr[sect.sct_type].d_mnem;
275 switch (sect.sct_type) {
283 if (sect.sct_own != player->cnum && !player->god)
287 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
291 writemap(player->cnum);
292 if (!getsect(curx, cury, §))
294 pr(" %c %c eff mob civ mil uw food work avail\n",
296 pr(" %c %c %c %3d %3d %4d %4d %4d %4d %3d %3d\n",
297 view[2], view[3], view[4],
298 sect.sct_effic, sect.sct_mobil,
299 sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
300 sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
301 pr(" %c %c\n", view[5], view[6]);
306 fly_map(coord curx, coord cury)
313 snxtsct_dist(&ns, curx, cury, 1);
315 while (i < 7 && nxtsct(&ns, §)) {
316 /* Nasty: this relies on the iteration order */
317 if (!(view[i] = player->bmap[sect.sct_uid]))
322 pr(" %c %c\n", view[0], view[1]);
323 pr(" %c %c %c\n", view[2], view[3], view[4]);
324 pr(" %c %c\n", view[5], view[6]);
329 check_lmines(coord x, coord y, double weight)
334 getsect(x, y, §);
335 if (SCT_LANDMINES(§) > 0 &&
336 sect.sct_oldown != player->cnum &&
337 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
339 pr("Blammo! Landmines detected! in %s ",
340 xyas(sect.sct_x, sect.sct_y, player->cnum));
344 pr("%d damage sustained.\n", dam);