Clean up path finding in unit_sub()

Don't claim the destination sector is unreachable when the best path
is longer than 1023 characters for land units or 99 characters for
ships.  Instead, report that the path is too long.  Up the limit for
ships to 1023 characters.
This commit is contained in:
Markus Armbruster 2011-03-26 14:34:53 +01:00
parent dac2c95dd5
commit 8f008bf849

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Ron Koenderink, 2007 * Ron Koenderink, 2007
* Markus Armbruster, 2009 * Markus Armbruster, 2009-2011
*/ */
#include <config.h> #include <config.h>
@ -138,9 +138,9 @@ unit_path(int together, struct empobj *unit, char *buf)
{ {
coord destx; coord destx;
coord desty; coord desty;
struct sctstr d_sect, sect; struct sctstr sect;
size_t len; size_t len;
char *cp; double c;
int mtype; int mtype;
if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP)) if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
@ -152,34 +152,17 @@ unit_path(int together, struct empobj *unit, char *buf)
pr("Cannot go to a destination sector if not all starting in the same sector\n"); pr("Cannot go to a destination sector if not all starting in the same sector\n");
return NULL; return NULL;
} }
if (!getsect(destx, desty, &d_sect)) {
pr("%d,%d is not a sector\n", destx, desty);
return NULL;
}
if (unit->ef_type == EF_SHIP) { if (unit->ef_type == EF_SHIP) {
if (path_find(unit->x, unit->y, d_sect.sct_x, d_sect.sct_y, c = path_find(unit->x, unit->y, destx, desty,
player->cnum, MOB_SAIL) < 0) player->cnum, MOB_SAIL);
cp = NULL; if (c < 0 || unit->mobil <= 0) {
else {
len = path_find_route(buf, 100, unit->x, unit->y,
d_sect.sct_x, d_sect.sct_y);
if (len >= 100)
cp = NULL;
else {
if (len == 0)
strcpy(buf, "h");
cp = buf;
}
}
if (!cp || unit->mobil <= 0) {
pr("Can't get to '%s' right now.\n", pr("Can't get to '%s' right now.\n",
xyas(d_sect.sct_x, d_sect.sct_y, player->cnum)); xyas(destx, desty, player->cnum));
return NULL; return NULL;
} }
} else { } else {
getsect(unit->x, unit->y, &sect); getsect(unit->x, unit->y, &sect);
mtype = lnd_mobtype((struct lndstr *)unit); mtype = lnd_mobtype((struct lndstr *)unit);
buf[0] = 0;
/* /*
* Note: passing sect.sct_own for actor is funny, but works: * Note: passing sect.sct_own for actor is funny, but works:
* its only effect is to confine the search to that nation's * its only effect is to confine the search to that nation's
@ -187,30 +170,30 @@ unit_path(int together, struct empobj *unit, char *buf)
* different for marching in allied land, and passing it would * different for marching in allied land, and passing it would
* break path finding there. * break path finding there.
*/ */
if (path_find(sect.sct_x, sect.sct_y, d_sect.sct_x, d_sect.sct_y, c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
sect.sct_own, mtype) < 0) if (c < 0) {
cp = NULL; buf[0] = 0;
else {
len = path_find_route(buf, 1024,
sect.sct_x, sect.sct_y,
d_sect.sct_x, d_sect.sct_y);
if (len + 1 >= 1024)
cp = NULL;
else {
strcpy(buf + len, "h");
cp = buf;
}
}
if (!cp) {
pr("No owned %s from %s to %s!\n", pr("No owned %s from %s to %s!\n",
mtype == MOB_RAIL ? "railway" : "path", mtype == MOB_RAIL ? "railway" : "path",
xyas(unit->x, unit->y, player->cnum), xyas(unit->x, unit->y, player->cnum),
xyas(d_sect.sct_x, d_sect.sct_y, player->cnum)); xyas(destx, desty, player->cnum));
return NULL; return NULL;
} }
pr("Using path '%s'\n", cp);
} }
return cp; len = path_find_route(buf, sizeof(buf), unit->x, unit->y, destx, desty);
if (len == 0 || unit->ef_type == EF_LAND) {
if (len + 1 < sizeof(buf))
strcpy(buf + len, "h");
len++;
}
if (len >= sizeof(buf)) {
pr("Can't handle path to %s, it's too long, sorry\n",
xyas(destx, desty, player->cnum));
return NULL;
}
if (unit->ef_type == EF_LAND)
pr("Using path '%s'\n", buf);
return buf;
} }
void void