Skip to content

Commit

Permalink
Let idle trucks help with building structures as well as repairing them.
Browse files Browse the repository at this point in the history
Fixes ticket:737.
  • Loading branch information
Cyp committed May 12, 2016
1 parent 0fde4c8 commit 24c8416
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 83 deletions.
28 changes: 14 additions & 14 deletions src/action.cpp
Expand Up @@ -1340,9 +1340,9 @@ void actionUpdateDroid(DROID *psDroid)
break;
}
// moving to a location to build a structure
if (actionReachedBuildPos(psDroid, order->pos.x, order->pos.y, order->direction, order->psStats))
if (actionReachedBuildPos(psDroid, psDroid->actionPos.x, psDroid->actionPos.y, order->direction, order->psStats))
{
bool buildPosEmpty = actionRemoveDroidsFromBuildPos(psDroid->player, order->pos, order->direction, order->psStats);
bool buildPosEmpty = actionRemoveDroidsFromBuildPos(psDroid->player, psDroid->actionPos, order->direction, order->psStats);
if (!buildPosEmpty)
{
break;
Expand All @@ -1357,7 +1357,7 @@ void actionUpdateDroid(DROID *psDroid)
if (order->type == DORDER_BUILD && order->psObj == NULL)
{
// Starting a new structure
const Vector2i pos(order->pos.x, order->pos.y);
const Vector2i pos = psDroid->actionPos;

//need to check if something has already started building here?
//unless its a module!
Expand All @@ -1367,10 +1367,10 @@ void actionUpdateDroid(DROID *psDroid)
debug(LOG_NEVER, "DACTION_MOVETOBUILD: setUpBuildModule");
setUpBuildModule(psDroid);
}
else if (TileHasStructure(mapTile(map_coord(order->pos.x), map_coord(order->pos.y))))
else if (TileHasStructure(worldTile(pos)))
{
// structure on the build location - see if it is the same type
STRUCTURE *const psStruct = getTileStructure(map_coord(order->pos.x), map_coord(order->pos.y));
STRUCTURE *const psStruct = getTileStructure(map_coord(pos.x), map_coord(pos.y));
if (psStruct->pStructureType == order->psStats ||
(order->psStats->type == REF_WALL && psStruct->pStructureType->type == REF_WALLCORNER))
{
Expand Down Expand Up @@ -1417,7 +1417,7 @@ void actionUpdateDroid(DROID *psDroid)
psStructStats->type == REF_DEFENSE || psStructStats->type == REF_REARM_PAD))
{
// building a wall.
MAPTILE *const psTile = mapTile(map_coord(order->pos.x), map_coord(order->pos.y));
MAPTILE *const psTile = worldTile(psDroid->actionPos);
syncDebug("Reached build target: wall");
if (order->psObj == NULL
&& (TileHasStructure(psTile)
Expand All @@ -1426,7 +1426,7 @@ void actionUpdateDroid(DROID *psDroid)
if (TileHasStructure(psTile))
{
// structure on the build location - see if it is the same type
STRUCTURE *const psStruct = getTileStructure(map_coord(order->pos.x), map_coord(order->pos.y));
STRUCTURE *const psStruct = getTileStructure(map_coord(psDroid->actionPos.x), map_coord(psDroid->actionPos.y));
if (psStruct->pStructureType == order->psStats)
{
// same type - do a help build
Expand Down Expand Up @@ -1464,6 +1464,7 @@ void actionUpdateDroid(DROID *psDroid)
else
{
syncDebug("Reached build target: planned-help");
objTrace(psDroid->id, "DACTION_MOVETOBUILD: planned-help");
helpBuild = true;
}

Expand Down Expand Up @@ -1492,15 +1493,15 @@ void actionUpdateDroid(DROID *psDroid)
break;
}
if (DROID_STOPPED(psDroid) &&
!actionReachedBuildPos(psDroid, order->pos.x, order->pos.y, order->direction, order->psStats))
!actionReachedBuildPos(psDroid, psDroid->actionPos.x, psDroid->actionPos.y, order->direction, order->psStats))
{
objTrace(psDroid->id, "DACTION_BUILD: Starting to drive toward construction site");
moveDroidToNoFormation(psDroid, order->pos.x, order->pos.y);
moveDroidToNoFormation(psDroid, psDroid->actionPos.x, psDroid->actionPos.y);
}
else if (!DROID_STOPPED(psDroid) &&
psDroid->sMove.Status != MOVETURNTOTARGET &&
psDroid->sMove.Status != MOVESHUFFLE &&
actionReachedBuildPos(psDroid, order->pos.x, order->pos.y, order->direction, order->psStats))
actionReachedBuildPos(psDroid, psDroid->actionPos.x, psDroid->actionPos.y, order->direction, order->psStats))
{
objTrace(psDroid->id, "DACTION_BUILD: Stopped - at construction site");
moveStopDroid(psDroid);
Expand Down Expand Up @@ -1723,7 +1724,7 @@ void actionUpdateDroid(DROID *psDroid)
}
const int xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
const int ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if (xdiff * xdiff + ydiff * ydiff < REPAIR_RANGE)
if (xdiff * xdiff + ydiff * ydiff < REPAIR_RANGE * REPAIR_RANGE)
{
// Got to destination - start repair
//rotate turret to point at droid being repaired
Expand Down Expand Up @@ -1780,7 +1781,7 @@ void actionUpdateDroid(DROID *psDroid)
//check still next to the damaged droid
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->psActionTarget[0]->pos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->psActionTarget[0]->pos.y;
if (xdiff * xdiff + ydiff * ydiff > REPAIR_RANGE)
if (xdiff * xdiff + ydiff * ydiff > REPAIR_RANGE * REPAIR_RANGE)
{
if (order->type == DORDER_REPAIR)
{
Expand Down Expand Up @@ -2086,8 +2087,7 @@ static void actionDroidBase(DROID *psDroid, DROID_ACTION_DATA *psAction)
psDroid->action = DACTION_NONE;
break;
}
ASSERT_OR_RETURN(, order->type == DORDER_BUILD || order->type == DORDER_HELPBUILD ||
order->type == DORDER_LINEBUILD, "cannot start build action without a build order");
//ASSERT_OR_RETURN(, order->type == DORDER_BUILD || order->type == DORDER_HELPBUILD || order->type == DORDER_LINEBUILD, "cannot start build action without a build order");
ASSERT_OR_RETURN(, psAction->x > 0 && psAction->y > 0, "Bad build order position");
psDroid->action = DACTION_MOVETOBUILD;
psDroid->actionPos.x = psAction->x;
Expand Down
2 changes: 1 addition & 1 deletion src/action.h
Expand Up @@ -44,7 +44,7 @@ const char *getDroidActionName(DROID_ACTION action);
#define EMP_DISABLE_TIME (10000) // 10 secs

/** How far away the repair droid can be from the damaged droid to function. */
#define REPAIR_RANGE (TILE_UNITS * TILE_UNITS * 4)
#define REPAIR_RANGE (2 * TILE_UNITS)

/**
* Update the action state for a droid.
Expand Down
48 changes: 36 additions & 12 deletions src/droid.cpp
Expand Up @@ -106,6 +106,15 @@ static UDWORD calcDroidBaseBody(DROID *psDroid);

void cancelBuild(DROID *psDroid)
{
if (psDroid->order.type == DORDER_NONE || psDroid->order.type == DORDER_PATROL || psDroid->order.type == DORDER_HOLD || psDroid->order.type == DORDER_SCOUT || psDroid->order.type == DORDER_GUARD)
{
objTrace(psDroid->id, "Droid build action cancelled");
psDroid->order.psObj = nullptr;
psDroid->action = DACTION_NONE;
setDroidActionTarget(psDroid, nullptr, 0);
return; // Don't cancel orders.
}

if (orderDroidList(psDroid))
{
objTrace(psDroid->id, "Droid build order cancelled - changing to next order");
Expand Down Expand Up @@ -1064,7 +1073,7 @@ DroidStartBuild droidStartBuild(DROID *psDroid)
CHECK_DROID(psDroid);

/* See if we are starting a new structure */
if ((psDroid->order.psObj == NULL) &&
if (psDroid->order.psObj == nullptr &&
(psDroid->order.type == DORDER_BUILD ||
psDroid->order.type == DORDER_LINEBUILD))
{
Expand All @@ -1077,6 +1086,7 @@ DroidStartBuild droidStartBuild(DROID *psDroid)
ASSERT(false, "Cannot build \"%s\" for player %d.", psStructStat->name.toUtf8().constData(), psDroid->player);
intBuildFinished(psDroid);
cancelBuild(psDroid);
objTrace(psDroid->id, "DroidStartBuildFailed: not researched");
return DroidStartBuildFailed;
}

Expand All @@ -1085,12 +1095,14 @@ DroidStartBuild droidStartBuild(DROID *psDroid)
{
intBuildFinished(psDroid);
cancelBuild(psDroid);
objTrace(psDroid->id, "DroidStartBuildFailed: structure limits");
return DroidStartBuildFailed;
}
// Can't build on burning oil derricks.
if (psStructStat->type == REF_RESOURCE_EXTRACTOR && fireOnLocation(psDroid->order.pos.x, psDroid->order.pos.y))
{
// Don't cancel build, since we can wait for it to stop burning.
objTrace(psDroid->id, "DroidStartBuildPending: burning");
return DroidStartBuildPending;
}
//ok to build
Expand All @@ -1099,18 +1111,24 @@ DroidStartBuild droidStartBuild(DROID *psDroid)
{
intBuildFinished(psDroid);
cancelBuild(psDroid);
objTrace(psDroid->id, "DroidStartBuildFailed: buildStructureDir failed");
return DroidStartBuildFailed;
}
psStruct->body = (psStruct->body + 9) / 10; // Structures start at 10% health. Round up.
}
else
{
/* Check the structure is still there to build (joining a partially built struct) */
psStruct = (STRUCTURE *)psDroid->order.psObj;
if (psStruct && !droidNextToStruct(psDroid, (BASE_OBJECT *)psStruct))
psStruct = castStructure(psDroid->order.psObj);
if (psStruct == nullptr)
{
psStruct = castStructure(worldTile(psDroid->actionPos)->psObject);
}
if (psStruct && !droidNextToStruct(psDroid, psStruct))
{
/* Nope - stop building */
debug(LOG_NEVER, "not next to structure");
objTrace(psDroid->id, "DroidStartBuildSuccess: not next to structure");
}
}

Expand All @@ -1121,18 +1139,19 @@ DroidStartBuild droidStartBuild(DROID *psDroid)
{
psDroid->actionStarted = gameTime;
psDroid->actionPoints = 0;
setDroidTarget(psDroid, (BASE_OBJECT *)psStruct);
setDroidActionTarget(psDroid, (BASE_OBJECT *)psStruct, 0);
setDroidTarget(psDroid, psStruct);
setDroidActionTarget(psDroid, psStruct, 0);
objTrace(psDroid->id, "DroidStartBuild: set target");
}

if (psStruct->visible[selectedPlayer])
{
audio_PlayObjStaticTrackCallback(psDroid, ID_SOUND_CONSTRUCTION_START,
droidBuildStartAudioCallback);
audio_PlayObjStaticTrackCallback(psDroid, ID_SOUND_CONSTRUCTION_START, droidBuildStartAudioCallback);
}
}
CHECK_DROID(psDroid);

objTrace(psDroid->id, "DroidStartBuildSuccess");
return DroidStartBuildSuccess;
}

Expand Down Expand Up @@ -1172,14 +1191,19 @@ static void addConstructorEffect(STRUCTURE *psStruct)
bool droidUpdateBuild(DROID *psDroid)
{
UDWORD pointsToAdd, constructPoints;
STRUCTURE *psStruct;

CHECK_DROID(psDroid);
ASSERT_OR_RETURN(false, psDroid->action == DACTION_BUILD, "%s (order %s) has wrong action for construction: %s",
droidGetName(psDroid), getDroidOrderName(psDroid->order.type), getDroidActionName(psDroid->action));
ASSERT_OR_RETURN(false, psDroid->order.psObj != NULL, "Trying to update a construction, but no target!");

psStruct = (STRUCTURE *)psDroid->order.psObj;
STRUCTURE *psStruct = castStructure(psDroid->order.psObj);
if (psStruct == nullptr)
{
// Target missing, stop trying to build it.
psDroid->action = DACTION_NONE;
return false;
}

ASSERT_OR_RETURN(false, psStruct->type == OBJ_STRUCTURE, "target is not a structure");
ASSERT_OR_RETURN(false, psDroid->asBits[COMP_CONSTRUCT] < numConstructStats, "Invalid construct pointer for unit");

Expand All @@ -1189,8 +1213,8 @@ bool droidUpdateBuild(DROID *psDroid)
// Update the interface
intBuildFinished(psDroid);
// Check if line order build is completed, or we are not carrying out a line order build
if ((map_coord(psDroid->order.pos) == map_coord(psDroid->order.pos2))
|| psDroid->order.type != DORDER_LINEBUILD)
if (psDroid->order.type != DORDER_LINEBUILD ||
map_coord(psDroid->order.pos) == map_coord(psDroid->order.pos2))
{
cancelBuild(psDroid);
}
Expand Down

0 comments on commit 24c8416

Please sign in to comment.