2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * move.c: Move something somewhere.
30 * Known contributors to this file:
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,
53 struct sctstr sect, ending_sect;
54 struct sctstr next, dsect;
55 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);
83 path = BestLandPath(buf2, start, &ending_sect, &total_mcost,
85 if (exploring && path) /* take off the 'h' */
86 path[strlen(path) - 1] = '\0';
88 pr("No owned path exists!\n");
90 pr("Using best path '%s', movement cost %1.3f\n",
92 strncpy(bpath, path, sizeof(bpath));
95 if ((total_mcost * weight) > mobility) {
96 pr("Not enough mobility to go all the way. Nothing moved.\n");
104 if (getsect(curx, cury, §) < 0) {
105 logerror("move_path: getsect %d,%d", curx, cury);
111 if (!movstr || *movstr == 0) {
113 map(curx, cury, NULL);
115 move_map(curx, cury, NULL);
117 sprintf(prompt, "<%.1f: %c %s> ", mobility,
118 dchr[sect.sct_type].d_mnem,
119 xyas(sect.sct_x, sect.sct_y, player->cnum));
120 movstr = getstring(prompt, buf);
122 if (movstr && sarg_xy(movstr, &dx, &dy)) {
123 if (getsect(dx, dy, &dsect)) {
124 movstr = BestLandPath(buf2, §, &dsect, &mv_cost,
127 pr("Invalid destination sector!\n");
131 if (movstr == NULL) {
132 pr("Can't get to %s from here!\n",
133 xyas(dx, dy, player->cnum));
135 if ((mv_cost * weight) > mobility) {
136 pr("Not enough mobility to go all the way. Nothing moved.\n");
139 pr("Using best path '%s', movement cost %1.3f\n",
141 strncpy(bpath, movstr, sizeof(bpath));
146 if (!movstr || *movstr == 0) {
147 buf2[0] = dirch[DIR_STOP];
151 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
152 pr("\"%c\" is not legal...", *movstr);
153 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
157 do movstr++; while (isspace(*movstr));
158 if (dir == DIR_MAP) {
160 map(curx, cury, movstr);
163 } else if (dir == DIR_STOP)
165 else if (dir == DIR_VIEW) {
166 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
167 dchr[sect.sct_type].d_name, sect.sct_item[I_CIVIL]);
171 * now see if we can move into the
172 * next sector. Mobility, terrain,
173 * or ownership may prevent us.
175 tmpx = curx + diroff[dir][0];
176 tmpy = cury + diroff[dir][1];
177 if (getsect(tmpx, tmpy, &next) < 0) {
178 pr("You can't go there...\n");
183 if ((next.sct_type == SCT_SANCT) &&
184 (next.sct_own != player->cnum)) {
185 pr("Converts, huh?\n");
189 sect_mcost = sector_mcost(&next, MOB_MOVE);
190 if ((!player->owner && (!exploring
191 || next.sct_item[I_MILIT]
192 || next.sct_item[I_CIVIL]))
193 || sect_mcost == -1.0) {
194 /* already-owned, or prohibited terrain */
195 pr("You can't go there...\n");
199 sect_mcost *= weight;
200 if (sect_mcost > mobility) {
201 pr("Not enough mobility. ");
202 pr("You can't go there...\n");
206 mobility -= sect_mcost;
207 total_mcost += sect_mcost;
211 if (cury != start->sct_y)
213 if (curx != start->sct_x)
219 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
223 * Check and see if anyone will interdict us
225 if (takedam && chance(weight / 100.0) &&
226 ((curx != oldx) || (cury != oldy)))
227 *dam += ground_interdict(curx, cury, player->cnum,
233 intcost = (int)total_mcost;
236 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
240 if (chance(total_mcost - intcost))
248 move_map(coord curx, coord cury, char *arg)
256 snxtsct_dist(&ns, curx, cury, 1);
258 while (i < 7 && nxtsct(&ns, §)) {
259 /* Nasty: this relies on the iteration order */
260 view[i] = dchr[sect.sct_type].d_mnem;
261 switch (sect.sct_type) {
269 if (sect.sct_own != player->cnum && !player->god)
273 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
277 writemap(player->cnum);
278 if (!getsect(curx, cury, §))
280 pr(" %c %c eff mob civ mil uw food work avail\n",
282 pr(" %c %c %c %3d %3d %4d %4d %4d %4d %3d %3d\n",
283 view[2], view[3], view[4],
284 sect.sct_effic, sect.sct_mobil,
285 sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
286 sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
287 pr(" %c %c\n", view[5], view[6]);
292 fly_map(coord curx, coord cury)
299 snxtsct_dist(&ns, curx, cury, 1);
301 while (i < 7 && nxtsct(&ns, §)) {
302 /* Nasty: this relies on the iteration order */
303 if (!(view[i] = player->bmap[sect.sct_uid]))
308 pr(" %c %c\n", view[0], view[1]);
309 pr(" %c %c %c\n", view[2], view[3], view[4]);
310 pr(" %c %c\n", view[5], view[6]);
315 check_lmines(coord x, coord y, double weight)
320 getsect(x, y, §);
321 if (sect.sct_mines > 0 &&
322 sect.sct_oldown != player->cnum &&
323 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
325 pr("Blammo! Landmines detected! in %s ",
326 xyas(sect.sct_x, sect.sct_y, player->cnum));
330 pr("%d damage sustained.\n", dam);