Skip to content

Commit

Permalink
Pathfind to area around queued structure to be built, instead of to t…
Browse files Browse the repository at this point in the history
…ile under structure.

In the following, truck T now pathfinds to x instead of y, when ordered to build a bunker at +. (This was already
the case since 0119eda, for repairing something at + or building a derrick at +.)
*
 *+
 x*y
T  *

Fixes ticket:3576.
  • Loading branch information
Cyp committed Sep 13, 2012
1 parent 47011db commit 88c1a3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/baseobject.h
Expand Up @@ -30,6 +30,7 @@ struct StructureBounds
{
StructureBounds() {}
StructureBounds(Vector2i const &map, Vector2i const &size) : map(map), size(size) {}
bool valid() { return size.x >= 0; }

Vector2i map; ///< Map coordinates of upper left corner of structure.
Vector2i size; ///< Size (in map coordinates) of the structure.
Expand Down
14 changes: 8 additions & 6 deletions src/fpath.cpp
Expand Up @@ -353,7 +353,7 @@ void fpathRemoveDroidData(int id)
}

static FPATH_RETVAL fpathRoute(MOVE_CONTROL *psMove, int id, int startX, int startY, int tX, int tY, PROPULSION_TYPE propulsionType,
DROID_TYPE droidType, FPATH_MOVETYPE moveType, int owner, bool acceptNearest, BASE_OBJECT *dstStructure)
DROID_TYPE droidType, FPATH_MOVETYPE moveType, int owner, bool acceptNearest, StructureBounds const &dstStructure)
{
objTrace(id, "called(*,id=%d,sx=%d,sy=%d,ex=%d,ey=%d,prop=%d,type=%d,move=%d,owner=%d)", id, startX, startY, tX, tY, (int)propulsionType, (int)droidType, (int)moveType, owner);

Expand Down Expand Up @@ -432,7 +432,7 @@ static FPATH_RETVAL fpathRoute(MOVE_CONTROL *psMove, int id, int startX, int sta
job.droidID = id;
job.destX = tX;
job.destY = tY;
job.dstStructure = getStructureBounds(dstStructure);
job.dstStructure = dstStructure;
job.droidType = droidType;
job.propulsion = propulsionType;
job.moveType = moveType;
Expand Down Expand Up @@ -481,18 +481,20 @@ FPATH_RETVAL fpathDroidRoute(DROID* psDroid, SDWORD tX, SDWORD tY, FPATH_MOVETYP
// Check whether the start and end points of the route are blocking tiles and find an alternative if they are.
Position startPos = psDroid->pos;
Position endPos = Position(tX, tY, 0);
BASE_OBJECT *dstStructure = worldTile(endPos)->psObject;
StructureBounds dstStructure = getStructureBounds(worldTile(endPos)->psObject);
startPos = findNonblockingPosition(startPos, getPropulsionStats(psDroid)->propulsionType, psDroid->player, moveType);
if (dstStructure == NULL) // If there's a structure over the destination, ignore it, otherwise pathfind from somewhere around the obstruction.
if (!dstStructure.valid()) // If there's a structure over the destination, ignore it, otherwise pathfind from somewhere around the obstruction.
{
endPos = findNonblockingPosition(endPos, getPropulsionStats(psDroid)->propulsionType, psDroid->player, moveType);
}
objTrace(psDroid->id, "Want to go to (%d, %d) -> (%d, %d), going (%d, %d) -> (%d, %d)", map_coord(psDroid->pos.x), map_coord(psDroid->pos.y), map_coord(tX), map_coord(tY), map_coord(startPos.x), map_coord(startPos.y), map_coord(endPos.x), map_coord(endPos.y));
switch (psDroid->order.type)
{
case DORDER_BUILD:
case DORDER_LINEBUILD: // build a number of structures in a row (walls + bridges)
dstStructure = getStructureBounds(psDroid->order.psStats, psDroid->order.pos, psDroid->order.direction); // Just need to get close enough to build (can be diagonally), do not need to reach the destination tile.
// Continue, do not break.
case DORDER_HELPBUILD: // help to build a structure
case DORDER_LINEBUILD: // 6 - build a number of structures in a row (walls + bridges)
case DORDER_DEMOLISH: // demolish a structure
case DORDER_REPAIR:
acceptNearest = false;
Expand Down Expand Up @@ -574,7 +576,7 @@ static int fpathResultQueueLength(void)
// Only used by fpathTest.
static FPATH_RETVAL fpathSimpleRoute(MOVE_CONTROL *psMove, int id, int startX, int startY, int tX, int tY)
{
return fpathRoute(psMove, id, startX, startY, tX, tY, PROPULSION_TYPE_WHEELED, DROID_WEAPON, FMT_BLOCK, 0, true, NULL);
return fpathRoute(psMove, id, startX, startY, tX, tY, PROPULSION_TYPE_WHEELED, DROID_WEAPON, FMT_BLOCK, 0, true, getStructureBounds((BASE_OBJECT *)NULL));
}

void fpathTest(int x, int y, int x2, int y2)
Expand Down

0 comments on commit 88c1a3c

Please sign in to comment.