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,
52 struct sctstr sect, ending_sect;
53 struct sctstr next, dsect;
54 coord curx, cury, oldx, oldy;
62 double mobility = start->sct_mobil;
76 if (path && sarg_xy(path, &dx, &dy) && getsect(dx, dy, &ending_sect)) {
77 if ((ending_sect.sct_x == start->sct_x) &&
78 (ending_sect.sct_y == start->sct_y)) {
79 pr("Start sector is ending sector!\n");
82 pr("Looking for best path to %s\n", path);
84 total_mcost = path_find(start->sct_x, start->sct_y,
85 ending_sect.sct_x, ending_sect.sct_y,
86 start->sct_own, MOB_MOVE);
87 if (total_mcost < 0) {
91 len = path_find_route(buf2, 1024,
92 start->sct_x, start->sct_y,
93 ending_sect.sct_x, ending_sect.sct_y);
97 strcpy(buf2 + len, "h");
101 if (exploring && path) /* take off the 'h' */
102 path[strlen(path) - 1] = '\0';
104 pr("No owned path exists!\n");
106 pr("Using best path '%s', movement cost %1.3f\n",
108 strncpy(bpath, path, sizeof(bpath));
111 if ((total_mcost * weight) > mobility) {
112 pr("Not enough mobility to go all the way. Nothing moved.\n");
120 if (getsect(curx, cury, §) < 0) {
121 logerror("move_path: getsect %d,%d", curx, cury);
127 if (!movstr || *movstr == 0) {
129 map(curx, cury, NULL);
131 move_map(curx, cury, NULL);
133 sprintf(prompt, "<%.1f: %c %s> ", mobility,
134 dchr[sect.sct_type].d_mnem,
135 xyas(sect.sct_x, sect.sct_y, player->cnum));
136 movstr = getstring(prompt, buf);
138 if (movstr && sarg_xy(movstr, &dx, &dy)) {
139 if (getsect(dx, dy, &dsect)) {
141 mv_cost = path_find(sect.sct_x, sect.sct_y,
142 dsect.sct_x, dsect.sct_y,
143 sect.sct_own, MOB_MOVE);
148 len = path_find_route(buf2, 1024,
149 sect.sct_x, sect.sct_y,
150 dsect.sct_x, dsect.sct_y);
154 strcpy(buf2 + len, "h");
159 pr("Invalid destination sector!\n");
163 if (movstr == NULL) {
164 pr("Can't get to %s from here!\n",
165 xyas(dx, dy, player->cnum));
167 if ((mv_cost * weight) > mobility) {
168 pr("Not enough mobility to go all the way. Nothing moved.\n");
171 pr("Using best path '%s', movement cost %1.3f\n",
173 strncpy(bpath, movstr, sizeof(bpath));
178 if (!movstr || *movstr == 0) {
179 buf2[0] = dirch[DIR_STOP];
183 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
184 pr("\"%c\" is not legal...", *movstr);
185 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
189 do movstr++; while (isspace(*movstr));
190 if (dir == DIR_MAP) {
192 map(curx, cury, movstr);
195 } else if (dir == DIR_STOP)
197 else if (dir == DIR_VIEW) {
198 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
199 dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
203 * now see if we can move into the
204 * next sector. Mobility, terrain,
205 * or ownership may prevent us.
207 tmpx = curx + diroff[dir][0];
208 tmpy = cury + diroff[dir][1];
209 if (getsect(tmpx, tmpy, &next) < 0) {
210 pr("You can't go there...\n");
215 if ((next.sct_type == SCT_SANCT) &&
216 (next.sct_own != player->cnum)) {
217 pr("Converts, huh?\n");
221 sect_mcost = sector_mcost(&next, MOB_MOVE);
222 if ((!player->owner && (!exploring
223 || next.sct_item[I_MILIT]
224 || next.sct_item[I_CIVIL]))
225 || sect_mcost == -1.0) {
226 /* already-owned, or prohibited terrain */
227 pr("You can't go there...\n");
231 sect_mcost *= weight;
232 if (sect_mcost > mobility) {
233 pr("Not enough mobility. ");
234 pr("You can't go there...\n");
238 mobility -= sect_mcost;
239 total_mcost += sect_mcost;
243 if (cury != start->sct_y)
245 if (curx != start->sct_x)
251 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
255 * Check and see if anyone will interdict us
257 if (takedam && chance(weight / 100.0) &&
258 ((curx != oldx) || (cury != oldy)))
259 *dam += ground_interdict(curx, cury, player->cnum,
265 intcost = (int)total_mcost;
268 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
272 if (chance(total_mcost - intcost))
280 move_map(coord curx, coord cury, char *arg)
288 snxtsct_dist(&ns, curx, cury, 1);
290 while (i < 7 && nxtsct(&ns, §)) {
291 /* Nasty: this relies on the iteration order */
292 view[i] = dchr[sect.sct_type].d_mnem;
293 switch (sect.sct_type) {
301 if (sect.sct_own != player->cnum && !player->god)
305 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
309 writemap(player->cnum);
310 if (!getsect(curx, cury, §))
312 pr(" %c %c eff mob civ mil uw food work avail\n",
314 pr(" %c %c %c %3d %3d %4d %4d %4d %4d %3d %3d\n",
315 view[2], view[3], view[4],
316 sect.sct_effic, sect.sct_mobil,
317 sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
318 sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
319 pr(" %c %c\n", view[5], view[6]);
324 fly_map(coord curx, coord cury)
331 snxtsct_dist(&ns, curx, cury, 1);
333 while (i < 7 && nxtsct(&ns, §)) {
334 /* Nasty: this relies on the iteration order */
335 if (!(view[i] = player->bmap[sect.sct_uid]))
340 pr(" %c %c\n", view[0], view[1]);
341 pr(" %c %c %c\n", view[2], view[3], view[4]);
342 pr(" %c %c\n", view[5], view[6]);
347 check_lmines(coord x, coord y, double weight)
352 getsect(x, y, §);
353 if (SCT_LANDMINES(§) > 0 &&
354 sect.sct_oldown != player->cnum &&
355 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
357 pr("Blammo! Landmines detected! in %s ",
358 xyas(sect.sct_x, sect.sct_y, player->cnum));
362 pr("%d damage sustained.\n", dam);