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:
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;
61 double mobility = start->sct_mobil;
75 if (path && sarg_xy(path, &dx, &dy) && getsect(dx, dy, &ending_sect)) {
76 if ((ending_sect.sct_x == start->sct_x) &&
77 (ending_sect.sct_y == start->sct_y)) {
78 pr("Start sector is ending sector!\n");
81 pr("Looking for best path to %s\n", path);
82 path = BestLandPath(buf2, start, &ending_sect, &total_mcost,
84 if (exploring && path) /* take off the 'h' */
85 path[strlen(path) - 1] = '\0';
87 pr("No owned path exists!\n");
89 pr("Using best path '%s', movement cost %1.3f\n",
91 strncpy(bpath, path, sizeof(bpath));
94 if ((total_mcost * weight) > mobility) {
95 pr("Not enough mobility to go all the way. Nothing moved.\n");
103 if (getsect(curx, cury, §) < 0) {
104 logerror("move_path: getsect %d,%d", curx, cury);
110 if (!movstr || *movstr == 0) {
112 map(curx, cury, NULL);
114 move_map(curx, cury, NULL);
116 sprintf(prompt, "<%.1f: %c %s> ", mobility,
117 dchr[sect.sct_type].d_mnem,
118 xyas(sect.sct_x, sect.sct_y, player->cnum));
119 movstr = getstring(prompt, buf);
121 if (movstr && sarg_xy(movstr, &dx, &dy)) {
122 if (getsect(dx, dy, &dsect)) {
123 movstr = BestLandPath(buf2, §, &dsect, &mv_cost,
126 pr("Invalid destination sector!\n");
130 if (movstr == NULL) {
131 pr("Can't get to %s from here!\n",
132 xyas(dx, dy, player->cnum));
134 if ((mv_cost * weight) > mobility) {
135 pr("Not enough mobility to go all the way. Nothing moved.\n");
138 pr("Using best path '%s', movement cost %1.3f\n",
140 strncpy(bpath, movstr, sizeof(bpath));
145 if (!movstr || *movstr == 0) {
146 buf2[0] = dirch[DIR_STOP];
150 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
151 pr("\"%c\" is not legal...", *movstr);
152 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
156 do movstr++; while (isspace(*movstr));
157 if (dir == DIR_MAP) {
159 map(curx, cury, movstr);
162 } else if (dir == DIR_STOP)
164 else if (dir == DIR_VIEW) {
165 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
166 dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
170 * now see if we can move into the
171 * next sector. Mobility, terrain,
172 * or ownership may prevent us.
174 tmpx = curx + diroff[dir][0];
175 tmpy = cury + diroff[dir][1];
176 if (getsect(tmpx, tmpy, &next) < 0) {
177 pr("You can't go there...\n");
182 if ((next.sct_type == SCT_SANCT) &&
183 (next.sct_own != player->cnum)) {
184 pr("Converts, huh?\n");
188 sect_mcost = sector_mcost(&next, MOB_MOVE);
189 if ((!player->owner && (!exploring
190 || next.sct_item[I_MILIT]
191 || next.sct_item[I_CIVIL]))
192 || sect_mcost == -1.0) {
193 /* already-owned, or prohibited terrain */
194 pr("You can't go there...\n");
198 sect_mcost *= weight;
199 if (sect_mcost > mobility) {
200 pr("Not enough mobility. ");
201 pr("You can't go there...\n");
205 mobility -= sect_mcost;
206 total_mcost += sect_mcost;
210 if (cury != start->sct_y)
212 if (curx != start->sct_x)
218 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
222 * Check and see if anyone will interdict us
224 if (takedam && chance(weight / 100.0) &&
225 ((curx != oldx) || (cury != oldy)))
226 *dam += ground_interdict(curx, cury, player->cnum,
232 intcost = (int)total_mcost;
235 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
239 if (chance(total_mcost - intcost))
247 move_map(coord curx, coord cury, char *arg)
255 snxtsct_dist(&ns, curx, cury, 1);
257 while (i < 7 && nxtsct(&ns, §)) {
258 /* Nasty: this relies on the iteration order */
259 view[i] = dchr[sect.sct_type].d_mnem;
260 switch (sect.sct_type) {
268 if (sect.sct_own != player->cnum && !player->god)
272 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
276 writemap(player->cnum);
277 if (!getsect(curx, cury, §))
279 pr(" %c %c eff mob civ mil uw food work avail\n",
281 pr(" %c %c %c %3d %3d %4d %4d %4d %4d %3d %3d\n",
282 view[2], view[3], view[4],
283 sect.sct_effic, sect.sct_mobil,
284 sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
285 sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
286 pr(" %c %c\n", view[5], view[6]);
291 fly_map(coord curx, coord cury)
298 snxtsct_dist(&ns, curx, cury, 1);
300 while (i < 7 && nxtsct(&ns, §)) {
301 /* Nasty: this relies on the iteration order */
302 if (!(view[i] = player->bmap[sect.sct_uid]))
307 pr(" %c %c\n", view[0], view[1]);
308 pr(" %c %c %c\n", view[2], view[3], view[4]);
309 pr(" %c %c\n", view[5], view[6]);
314 check_lmines(coord x, coord y, double weight)
319 getsect(x, y, §);
320 if (SCT_LANDMINES(§) > 0 &&
321 sect.sct_oldown != player->cnum &&
322 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
324 pr("Blammo! Landmines detected! in %s ",
325 xyas(sect.sct_x, sect.sct_y, player->cnum));
329 pr("%d damage sustained.\n", dam);