2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * move.c: Move something somewhere.
30 * Known contributors to this file:
45 #include "prototypes.h"
47 static int move_map(s_char *what, coord curx, coord cury, s_char *arg);
50 move_ground(s_char *what, struct sctstr *start, struct sctstr *end,
51 double weight, s_char *path,
52 int (*map)(s_char *, coord, coord, s_char *), int exploring,
55 struct sctstr sect, ending_sect;
56 struct sctstr next, dsect;
57 coord curx, cury, oldx, oldy;
64 double mobility = (double)start->sct_mobil;
67 int takedam = (*dam), out = 0;
77 if (path && sarg_xy(path, &dx, &dy) && getsect(dx, dy, &ending_sect)) {
78 if ((ending_sect.sct_x == start->sct_x) &&
79 (ending_sect.sct_y == start->sct_y)) {
80 pr("Start sector is ending sector!\n");
83 pr("Looking for best path to %s\n", path);
84 path = BestLandPath(buf2, start, &ending_sect, &total_mcost,
86 if (exploring && (path != (s_char *)0)) /* take off the 'h' */
87 *(path + strlen(path) - 1) = '\0';
88 if (path == (s_char *)0)
89 pr("No owned path exists!\n");
91 pr("Using best path '%s', movement cost %1.3f\n",
93 strncpy(bpath, path, sizeof(bpath));
96 if ((total_mcost * weight) > mobility) {
97 pr("Not enough mobility to go all the way. Nothing moved.\n");
105 if (getsect(curx, cury, §) < 0) {
106 logerror("move_path: getsect %d,%d", curx, cury);
112 if (movstr == 0 || *movstr == 0) {
114 map(what, curx, cury, (s_char *)0);
116 move_map(what, curx, cury, (s_char *)0);
118 sprintf(prompt, "<%.1f: %c %s> ", mobility,
119 dchr[sect.sct_type].d_mnem,
120 xyas(sect.sct_x, sect.sct_y, player->cnum));
121 movstr = getstring(prompt, buf);
123 if (movstr && sarg_xy(movstr, &dx, &dy)) {
124 if (getsect(dx, dy, &dsect)) {
125 movstr = BestLandPath(buf2, §, &dsect, &mv_cost,
128 pr("Invalid destination sector!\n");
129 movstr = (s_char *)0;
132 if (movstr == (s_char *)0) {
133 pr("Can't get to %s from here!\n",
134 xyas(dx, dy, player->cnum));
135 movstr = (s_char *)0;
137 if ((mv_cost * weight) > mobility) {
138 pr("Not enough mobility to go all the way. Nothing moved.\n");
139 movstr = (s_char *)0;
141 pr("Using best path '%s', movement cost %1.3f\n",
143 strncpy(bpath, movstr, sizeof(bpath));
148 if (movstr == 0 || *movstr == 0) {
149 buf2[0] = dirch[DIR_STOP];
153 if ((dir = chkdir(*movstr, DIR_STOP, DIR_MAP)) < 0) {
154 pr("\"%c\" is not legal...", *movstr);
155 direrr("'%c' to stop ", "'%c' to view ", "& '%c' to map\n");
160 if (dir == DIR_MAP) {
162 map(what, curx, cury, movstr + 1);
165 } else if (dir == DIR_STOP)
167 else if (dir == DIR_VIEW) {
168 pr("%d%% %s with %d civilians.\n", sect.sct_effic,
169 dchr[sect.sct_type].d_name,
170 sect.sct_item[I_CIVIL]);
174 * now see if we can move into the
175 * next sector. Mobility, terrain,
176 * or ownership may prevent us.
178 tmpx = curx + diroff[dir][0];
179 tmpy = cury + diroff[dir][1];
180 if (getsect(tmpx, tmpy, &next) < 0) {
181 pr("You can't go there...\n");
186 if ((next.sct_type == SCT_SANCT) &&
187 (next.sct_own != player->cnum)) {
188 pr("Converts, huh?\n");
192 sect_mcost = sector_mcost(&next, MOB_ROAD);
193 if ((!player->owner && (!exploring
194 || next.sct_item[I_MILIT]
195 || next.sct_item[I_CIVIL]))
196 || sect_mcost == -1.0) {
197 /* already-owned, or prohibited terrain */
198 pr("You can't go there...\n");
202 sect_mcost *= weight;
203 if (sect_mcost > mobility) {
204 pr("Not enough mobility. ");
205 pr("You can't go there...\n");
209 mobility -= sect_mcost;
210 total_mcost += sect_mcost;
214 if (cury != start->sct_y)
216 if (curx != start->sct_x)
222 *dam += check_lmines(sect.sct_x, sect.sct_y, weight);
226 * Check and see if anyone will interdict us
228 if (takedam && chance(weight / 100.0) &&
229 ((curx != oldx) || (cury != oldy)))
230 (*dam) += ground_interdict(curx, cury, player->cnum,
236 intcost = (int)total_mcost;
239 if ((start->sct_x == end->sct_x) && (start->sct_y == end->sct_y)
243 if (chance(total_mcost - intcost))
251 move_map(s_char *what, coord curx, coord cury, s_char *arg)
262 np = getnatp(player->cnum);
263 rel_x = xrel(np, curx);
264 rel_y = yrel(np, cury);
265 sprintf(range, "%d:%d,%d:%d", rel_x - 2, rel_x + 2, rel_y - 1,
268 /* This is necessary, otherwise move_map would attempt to pay */
269 /* attention to the conditional arguments left behind by such */
270 /* a command as "tran p -1,-1 ?eff=100".. It'd then only see */
271 /* 100% efficienct sects, and get all screwed up --ts */
272 if (!snxtsct(&ns, range))
275 while (i < 7 && nxtsct(&ns, §)) {
276 view[i] = dchr[sect.sct_type].d_mnem;
277 switch (sect.sct_type) {
285 if (sect.sct_own != player->cnum && !player->god)
289 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
293 writemap(player->cnum);
294 if (!getsect(curx, cury, §))
296 pr(" %c %c eff mob civ mil uw food work avail\n",
298 pr(" %c %c %c %3d %3d %4d %4d %4d %4d %3d %3d\n",
299 view[2], view[3], view[4],
300 sect.sct_effic, sect.sct_mobil,
301 sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
302 sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
303 pr(" %c %c\n", view[5], view[6]);
308 fly_map(coord curx, coord cury)
318 np = getnatp(player->cnum);
319 rel_x = xrel(np, curx);
320 rel_y = yrel(np, cury);
321 sprintf(range, "%d:%d,%d:%d", rel_x - 2, rel_x + 2, rel_y - 1,
324 /* This is necessary, otherwise move_map would attempt to pay */
325 /* attention to the conditional arguments left behind by such */
326 /* a command as "tran p -1,-1 ?eff=100".. It'd then only see */
327 /* 100% efficienct sects, and get all screwed up --ts */
329 if (!snxtsct(&ns, range))
332 while (i < 7 && nxtsct(&ns, §)) {
333 if (!(view[i] = player->bmap[sctoff(ns.x, ns.y)]))
338 pr(" %c %c\n", view[0], view[1]);
339 pr(" %c %c %c\n", view[2], view[3], view[4]);
340 pr(" %c %c\n", view[5], view[6]);
345 check_lmines(coord x, coord y, double weight)
350 getsect(x, y, §);
351 if (sect.sct_mines > 0 &&
352 sect.sct_oldown != player->cnum &&
353 chance(DMINE_LHITCHANCE(sect.sct_mines)) && chance(weight / 100.0)) {
355 pr("Blammo! Landmines detected! in %s ",
356 xyas(sect.sct_x, sect.sct_y, player->cnum));
360 pr("%d damage sustained.\n", dam);