Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(gameengine): builders take a connected tunnel to a far build site

Olcay Seygan34c626b16583
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f726f50da..fd1b60ba9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -555,6 +555,7 @@ A third pass went through the generals' powers, promotions and unit abilities.
- A tunnel network heals at one rate, however many entrances it has.
- GLA units take the tunnels on their own. Give a move order across the map, and if the way through your network is any shorter than the walk, the whole selection goes down the tunnel nearest it and comes up at the one nearest where you sent it, then walks the rest. An attack move does the same and comes up still attack-moving, and fresh units heading for a rally point across the map take the tunnels too, fighting on the way if they would have walked there fighting. The tunnels used to have to be almost a third shorter before anybody took them, so fresh units with a tunnel beside their barracks and one beside the rally point mostly walked anyway. Passing through does not count as resting: only units you put inside yourself are healed. The group decides together: fifteen Rebels with six places already taken all go through, where a first version sent ten underground and walked the other five. The green order line shows it: one line to the tunnel they go down and one from the tunnel they come up at to where you sent them. Twelve Rebels crossing a small map with a tunnel at each end got there in 21 seconds instead of 50, four Scorpions in 9 instead of 21. A tunnel that has just been hit is skipped, so nobody surfaces into the fire. So is one still going up or being sold: a tunnel you had only just placed used to count straight away, and units sent across the map queued up at a building site. A tunnel rising again out of a GLA hole counts as going up until it is finished, and units underground when the tunnel they went down is destroyed no longer come up at one. The computer does it too: a GLA team losing a fight near one of its tunnels falls back underground and comes up at home, which happened 10 to 25 times a match in four Brutal GLA mirrors on Twilight Flame, and an attack wave takes the tunnels when they come up nearer your base than its road does. And the computer now digs them on purpose: a tunnel at home, one at the expansion it holds, and one three quarters of the way down the road its next wave will take, as long as nothing it has seen there can shoot. It does not wait for a fat bank to do it: a tunnel costs 800 and pays back on the first wave. In four Brutal GLA mirrors on Twilight Flame, 10 of 49 attack waves went underground, where none had before, and beaten teams fell back through a tunnel 250 times. Units you put in a tunnel yourself stay put, and a network they have filled is left alone: the order walks. With one place free, everybody still goes through it one after the other, since a unit passing through leaves the moment it arrives. Replays recorded before this change play out differently.
- Units a move order takes through the tunnels come out still selected. Going underground took each one off your selection and nothing put it back, so a group sent across the map surfaced as nobody's and had to be picked up again by hand. A trip you order by clicking a tunnel still leaves the selection the way it always has, and picking something else while they are underground means they stay out of it.
+- Builders take the tunnels to work. A GLA Worker sent to build, repair or finish a structure goes through the network when that is shorter than the walk, and carries on with the job at the far end. The same holds for the builder of any side that has tunnels. Replays recorded before this change play out differently.
## The power bar tells the truth
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
index ef8a958e0..f11e8257d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
@@ -751,6 +751,7 @@ public:
and nothing ordered, for a unit that cannot go into a tunnel. */
Bool takeTunnelTrip( Object *entrance, const Coord3D *goal, TunnelTripEnd end, CommandSourceType cmdSource );
Bool hasTunnelTrip() const { return m_hasTunnelTrip; } ///< on its way through the tunnel network to a move order's goal
+ void endTunnelTrip(); ///< what the trip was for is gone: forget it, unless the unit is already underground
const Coord3D *getTunnelTripGoal() const { return &m_tunnelTripGoal; } ///< where the trip ends, once out of the far mouth
TunnelTripEnd getTunnelTripEnd() const { return m_tunnelTripEnd; } ///< how the leg from the far mouth is walked
#if defined(_DEBUG) || defined(_INTERNAL)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
index 2a74d45fb..a5bc160d2 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
@@ -385,7 +385,11 @@ Object *BuildAssistant::buildObjectNow( Object *constructorObject, const ThingTe
Bool keepsCurrentJob = owningPlayer->getPlayerType() == PLAYER_HUMAN &&
dozer && dozer->isTaskPending( DOZER_TASK_BUILD );
if( !keepsCurrentJob )
+ {
+ // a tunnel trip to the last order's goal with it, or the builder walks there first
+ ai->endTunnelTrip();
ai->aiIdle(CMD_FROM_AI); // stop any current behavior.
+ }
return ai->construct( what, pos, angle, owningPlayer, FALSE );
}
return NULL;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index 832dc295f..6e2cfc0cb 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -5247,6 +5247,18 @@ Bool AIUpdateInterface::takeTunnelTrip( Object *entrance, const Coord3D *goal, T
return TRUE;
}
+//----------------------------------------------------------------------------------------
+/**
+ * An AI order does not end a trip (aiDoCommand), so whoever idles a unit with one because the
+ * reason for the trip is gone says so here, or the unit walks on to the old goal the moment it is
+ * idle. Underground the trip stays: it is what brings the unit back up.
+ */
+void AIUpdateInterface::endTunnelTrip()
+{
+ if (getObject()->getContainedBy() == NULL)
+ m_hasTunnelTrip = FALSE;
+}
+
//----------------------------------------------------------------------------------------
/**
* Follow the path defined by the given array of points
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
index 8c651ac11..f5bac37c7 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
@@ -38,6 +38,7 @@
#include "Common/ThingFactory.h"
#include "Common/Player.h"
#include "Common/Money.h"
+#include "Common/TunnelTracker.h"
#include "Common/Radar.h"
#include "Common/RandomValue.h"
#include "Common/GameState.h"
@@ -252,7 +253,38 @@ StateReturnType DozerActionPickActionPosState::update( void )
machine->setGoalObject( goalObject );
machine->setGoalPosition( &goalPos );
ai->ignoreObstacle(goalObject);
- ai->aiMoveToPosition( &goalPos, CMD_FROM_AI );
+
+ //
+ // through the tunnel network when that is shorter, the way a move order goes; see
+ // TunnelTracker::findTunnelShortcut. The trip ends in a walk from the far mouth to goalPos
+ // (AIUpdateInterface::update), which is the walk the move state below is waiting on, so the job
+ // carries on from there. Every job comes through here, a build, a repair and a resumed site alike.
+ //
+ const Real walkX = goalPos.x - dozer->getPosition()->x;
+ const Real walkY = goalPos.y - dozer->getPosition()->y;
+ const TunnelTracker *tunnels = dozer->getControllingPlayer()->getTunnelSystem();
+ Object *entrance = tunnels->findTunnelShortcut( dozer->getPosition(), &goalPos,
+ (Real)sqrt( walkX * walkX + walkY * walkY ) );
+
+ //
+ // The shortcut is measured in straight lines, and the site was only ever known to be reachable
+ // from where the builder stood. A far mouth on the other side of a cliff or on another island
+ // would leave it walking at a wall with the job still in hand, so the ground between that mouth
+ // and the site is asked about first. The mouth is a structure, which the zone check reads as the
+ // terrain under it.
+ //
+ if( entrance != NULL &&
+ TheAI->pathfinder()->clientSafeQuickDoesPathExist( ai->getLocomotorSet(),
+ tunnels->findQuietTunnelNear( &goalPos )->getPosition(), &goalPos ) &&
+ ai->takeTunnelTrip( entrance, &goalPos, TUNNEL_TRIP_MOVE, CMD_FROM_AI ) )
+ {
+ DEBUG_LOG(("TUNNELBUILD frame %d: builder %d takes tunnel %d to task %d on %d\n", TheGameLogic->getFrame(),
+ dozer->getID(), entrance->getID(), m_task, goalObject->getID()));
+ }
+ else
+ {
+ ai->aiMoveToPosition( &goalPos, CMD_FROM_AI );
+ }
return STATE_SUCCESS;
@@ -359,6 +391,15 @@ StateReturnType DozerActionMoveToActionPosState::update( void )
return STATE_FAILURE;
}
+ //
+ // On a tunnel trip the builder is walking to the mouth, underground, or about to be sent on from
+ // the far mouth. Standing still inside the network is not a failed move, and a mouth that
+ // happens to stand beside the site is not an arrival: the job would be done from inside the
+ // tunnel. The trip ends in the walk to the goal position, and that walk is the one judged below.
+ //
+ if( ai->hasTunnelTrip() )
+ return STATE_CONTINUE;
+
// if distance between us and our goal position is close enough
//
// The scaffold used to go up here, on arrival. Arrival is not the start of work: the first
@@ -1736,14 +1777,22 @@ UpdateSleepTime DozerAIUpdate::update( void )
Object *targetObject = TheGameLogic->findObjectByID( taskTarget );
Bool invalidTask = FALSE;
+ //
+ // A tunnel on the way to the job is not a container the job is being done from: the move state
+ // waits the trip out (DozerActionMoveToActionPosState::update). Both checks below would end
+ // the job underground, the repair one because nothing contained may repair
+ // (ActionManager::canRepairObject), so they wait until the builder is back up.
+ //
+ const Bool underground = getObject()->getContainedBy() != NULL && hasTunnelTrip();
+
// validate the task and the target
- if( currentTask == DOZER_TASK_REPAIR &&
+ if( currentTask == DOZER_TASK_REPAIR && !underground &&
TheActionManager->canRepairObject( getObject(), targetObject, getLastCommandSource() ) == FALSE )
invalidTask = TRUE;
// a job cannot be done from inside a container; the build step took "not moving" for "arrived"
// and went on building from wherever the container stood
- if( getObject()->getContainedBy() )
+ if( getObject()->getContainedBy() && !underground )
invalidTask = TRUE;
// cancel the task if it's now invalid
@@ -2287,6 +2336,10 @@ void DozerAIUpdate::internalCancelTask( DozerTask task )
// stop the dozer from moving
AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
+
+ // and from walking on to where the job was, which is where a tunnel trip left pending would
+ // send it the moment it went idle
+ ai->endTunnelTrip();
ai->aiIdle( CMD_FROM_AI );
// The walk to the building ignored it as an obstacle, and only arriving cleared that. A move
@@ -2539,9 +2592,13 @@ void DozerAIUpdate::aiDoCommand(const AICommandParms* parms)
case AICMD_REPAIR:
{
- // if we have no task right now, go idle so we can immediately respond to this
+ // if we have no task right now, go idle so we can immediately respond to this, and drop a
+ // tunnel trip to the last order's goal or the idle builder walks there first
if( getCurrentTask() == DOZER_TASK_INVALID )
+ {
+ endTunnelTrip();
aiIdle( CMD_FROM_AI );
+ }
// do the repair
privateRepair(parms->m_obj, parms->m_cmdSource);
@@ -2555,7 +2612,10 @@ void DozerAIUpdate::aiDoCommand(const AICommandParms* parms)
// if we have no task right now, go idle so we can immediately respond to this
if( getCurrentTask() == DOZER_TASK_INVALID )
+ {
+ endTunnelTrip();
aiIdle( CMD_FROM_AI );
+ }
// do the command
privateResumeConstruction( parms->m_obj, parms->m_cmdSource );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp
index c6b5dc40b..943af1961 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp
@@ -313,13 +313,17 @@ UpdateSleepTime WorkerAIUpdate::update( void )
Object *targetObject = TheGameLogic->findObjectByID( taskTarget );
Bool invalidTask = FALSE;
+ // as in DozerAIUpdate::update: a tunnel taken on the way to the job is left to finish, and
+ // neither check below is asked until the worker is back up
+ const Bool underground = getObject()->getContainedBy() != NULL && hasTunnelTrip();
+
// validate the task and the target
- if( currentTask == DOZER_TASK_REPAIR &&
+ if( currentTask == DOZER_TASK_REPAIR && !underground &&
TheActionManager->canRepairObject( getObject(), targetObject, getLastCommandSource() ) == FALSE )
invalidTask = TRUE;
- // as in DozerAIUpdate::update: no job is done from inside a tunnel, a transport or a garrison
- if( getObject()->getContainedBy() )
+ // no job is done from inside a tunnel, a transport or a garrison
+ if( getObject()->getContainedBy() && !underground )
invalidTask = TRUE;
// cancel the task if it's now invalid
@@ -846,8 +850,22 @@ void WorkerAIUpdate::internalCancelTask( DozerTask task )
{
return;
}
+ // see DozerAIUpdate::internalCancelTask: a tunnel trip left pending would walk on to where the job was
+ const Bool onTunnelTrip = ai->hasTunnelTrip();
+ ai->endTunnelTrip();
+
+ //
+ // The move below does not stop a busy worker. From the AI it is only laid over the state the
+ // worker is in (AIUpdateInterface::privateMoveToPosition), it ends at once, and that state picks
+ // up where it left off. On a tunnel trip that state is the walk into the mouth: the worker went
+ // down with no trip and no job and stayed there. So a trip is ended with a real idle, which is
+ // also what sends a worker already underground out of the far mouth (AIUpdateInterface::update).
+ //
/// @todo we really need a stop command instead of making it move to it's current location
- ai->aiMoveToPosition( getObject()->getPosition(), CMD_FROM_AI );
+ if( onTunnelTrip || getObject()->getContainedBy() )
+ ai->aiIdle( CMD_FROM_AI );
+ else
+ ai->aiMoveToPosition( getObject()->getPosition(), CMD_FROM_AI );
// see DozerAIUpdate::internalCancelTask: the building stays an obstacle once the walk to it is off
if( ai->getIgnoredObstacleID() == cancelledTargetID )
@@ -1054,9 +1072,13 @@ void WorkerAIUpdate::aiDoCommand(const AICommandParms* parms)
case AICMD_REPAIR:
{
- // if we have no task right now, go idle so we can immediately respond to this
+ // if we have no task right now, go idle so we can immediately respond to this, and drop a
+ // tunnel trip to the last order's goal or the idle worker walks there first
if( getCurrentTask() == DOZER_TASK_INVALID )
+ {
+ endTunnelTrip();
aiIdle( CMD_FROM_AI );
+ }
// do the repair
privateRepair(parms->m_obj, parms->m_cmdSource);
@@ -1070,7 +1092,10 @@ void WorkerAIUpdate::aiDoCommand(const AICommandParms* parms)
// if we have no task right now, go idle so we can immediately respond to this
if( getCurrentTask() == DOZER_TASK_INVALID )
+ {
+ endTunnelTrip();
aiIdle( CMD_FROM_AI );
+ }
// do the command
privateResumeConstruction( parms->m_obj, parms->m_cmdSource );