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 the nearest job and pick up unfinished structures

Olcay Seygan2f654ad329d9
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
index 1786fcfb5..7a2c6e671 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
@@ -49,6 +49,7 @@
#include "GameLogic/TerrainLogic.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/Module/AIUpdate.h"
+#include "GameLogic/Module/DozerAIUpdate.h"
#include "GameLogic/Module/BehaviorModule.h"
#include "GameLogic/Module/ContainModule.h"
#include "GameLogic/Module/CreateModule.h"
@@ -373,7 +374,17 @@ Object *BuildAssistant::buildObjectNow( Object *constructorObject, const ThingTe
if( ai )
{
- ai->aiIdle(CMD_FROM_AI); // stop any current behavior.
+ //
+ // a player's builder that is already on a job keeps it: the new structure is placed
+ // and waits at 0% (this builder, or the nearest free one, picks it up when idle -
+ // DozerPrimaryIdleState) instead of the newest order yanking the builder off a
+ // half-built structure. construct() sees the pending task and skips the retask.
+ //
+ DozerAIInterface *dozer = ai->getDozerAIInterface();
+ Bool keepsCurrentJob = owningPlayer->getPlayerType() == PLAYER_HUMAN &&
+ dozer && dozer->isTaskPending( DOZER_TASK_BUILD );
+ if( !keepsCurrentJob )
+ 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/Object.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
index f0a8f93bc..87030f023 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
@@ -3075,7 +3075,11 @@ Int Object::getConstructionSecondsRemaining() const
rate = 100.0f / INT_TO_REAL( max( 1, framesToBuild ) );
}
Real left = max( 0.0f, 100.0f - m_constructionPercent );
- return REAL_TO_INT_CEIL( left / rate / LOGICFRAMES_PER_SECOND );
+ // real seconds: the logic runs at the game-speed rate, not at a fixed 30 frames a second
+ Int logicFps = TheGameEngine ? TheGameEngine->getFramesPerSecondLimit() : 0;
+ if( logicFps <= 0 )
+ logicFps = LOGICFRAMES_PER_SECOND;
+ return REAL_TO_INT_CEIL( left / rate / logicFps );
}
//-------------------------------------------------------------------------------------------------
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 8881d56fb..a58141192 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp
@@ -964,6 +964,61 @@ enum
DOZER_PRIMARY_GO_HOME, ///< dozer has nothing else to do so it will return a command center
};
+//-------------------------------------------------------------------------------------------------
+/** The nearest unfinished structure of the dozer's player, within its bored range, that no
+ * builder is working on (never started, abandoned, or its builder is dead or busy elsewhere). */
+//-------------------------------------------------------------------------------------------------
+static Object *findUnfinishedStructureToContinue( Object *dozer )
+{
+ if( dozer == NULL || dozer->getAIUpdateInterface() == NULL )
+ return NULL;
+ const DozerAIInterface *dozerAI = dozer->getAIUpdateInterface()->getDozerAIInterface();
+ if( dozerAI == NULL )
+ return NULL;
+
+ PartitionFilterSamePlayer filter1( dozer->getControllingPlayer() );
+ PartitionFilterAcceptByKindOf filter2( MAKE_KINDOF_MASK( KINDOF_STRUCTURE ), KINDOFMASK_NONE );
+ PartitionFilterSameMapStatus filterMapStatus( dozer );
+ PartitionFilter *filters[] = { &filter1, &filter2, &filterMapStatus, NULL };
+ // "nearby" is the whole base, not the 15-cell bored range the repair scan uses
+ const Real UNFINISHED_SCAN_RANGE = 1500.0f;
+ Real range = dozerAI->getBoredRange();
+ if( range < UNFINISHED_SCAN_RANGE )
+ range = UNFINISHED_SCAN_RANGE;
+ ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( dozer->getPosition(),
+ range, FROM_CENTER_2D, filters );
+ MemoryPoolObjectHolder hold( iter );
+
+ Object *best = NULL;
+ Real bestDistSqr = 0.0f;
+ for( Object *obj = iter->first(); obj; obj = iter->next() )
+ {
+ if( !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) || obj->testStatus( OBJECT_STATUS_SOLD ) ||
+ obj->isEffectivelyDead() )
+ continue;
+
+ // somebody already on it?
+ Object *builder = TheGameLogic->findObjectByID( obj->getBuilderID() );
+ if( builder && builder != dozer && !builder->isEffectivelyDead() && builder->getAIUpdateInterface() )
+ {
+ DozerAIInterface *otherDozer = builder->getAIUpdateInterface()->getDozerAIInterface();
+ if( otherDozer && otherDozer->isTaskPending( DOZER_TASK_BUILD ) &&
+ otherDozer->getTaskTarget( DOZER_TASK_BUILD ) == obj->getID() )
+ continue;
+ }
+
+ Real distSqr = ThePartitionManager->getDistanceSquared( dozer, obj, FROM_CENTER_2D );
+ if( best == NULL || distSqr < bestDistSqr )
+ {
+ best = obj;
+ bestDistSqr = distSqr;
+ }
+ }
+
+ return best;
+
+} // end findUnfinishedStructureToContinue
+
//-------------------------------------------------------------------------------------------------
/** Dozer primary idle state */
//-------------------------------------------------------------------------------------------------
@@ -973,11 +1028,12 @@ class DozerPrimaryIdleState : public State
public:
- DozerPrimaryIdleState( StateMachine *machine ) : State( machine, "DozerPrimaryIdleState" )
+ DozerPrimaryIdleState( StateMachine *machine ) : State( machine, "DozerPrimaryIdleState" )
{
m_idleTooLongTimestamp = 0;
m_idlePlayerNumber = 0;
m_isMarkedAsIdle = FALSE;
+ m_nextUnfinishedScanFrame = 0;
}
virtual StateReturnType update( void );
virtual StateReturnType onEnter( void );
@@ -994,6 +1050,7 @@ protected:
UnsignedInt m_idleTooLongTimestamp; ///< when this is more than our idle too long time we try to do something about it
Int m_idlePlayerNumber; ///< Remeber what list we were added to.
Bool m_isMarkedAsIdle;
+ UnsignedInt m_nextUnfinishedScanFrame; ///< throttle for the unfinished-structure scan (not saved: a load just scans again)
};
EMPTY_DTOR(DozerPrimaryIdleState)
@@ -1103,7 +1160,25 @@ StateReturnType DozerPrimaryIdleState::update( void )
//
if( !ai->isIdle() )
m_idleTooLongTimestamp = TheGameLogic->getFrame();
-
+
+ //
+ // a builder that has just gone idle next to an unfinished structure nobody is working on
+ // (its own next one, an abandoned one, a dead dozer's) picks that job up. A second's
+ // grace lets a follow-up order from the player win; the scan itself runs once a second.
+ //
+ if( ai->isIdle() && dozerAI->isAnyTaskPending() == FALSE &&
+ TheGameLogic->getFrame() - m_idleTooLongTimestamp > LOGICFRAMES_PER_SECOND &&
+ TheGameLogic->getFrame() >= m_nextUnfinishedScanFrame )
+ {
+ m_nextUnfinishedScanFrame = TheGameLogic->getFrame() + LOGICFRAMES_PER_SECOND;
+ Object *unfinished = findUnfinishedStructureToContinue( dozer );
+ if( unfinished )
+ {
+ ai->aiResumeConstruction( unfinished, CMD_FROM_AI );
+ return STATE_CONTINUE;
+ }
+ }
+
//
// if we're just sitting here in idle, and there is no task pending ... after so long we'll
// try to go to the nearest command center
@@ -1737,8 +1812,11 @@ Object *DozerAIUpdate::construct( const ThingTemplate *what,
MAKE_MODELCONDITION_MASK(MODELCONDITION_AWAITING_CONSTRUCTION)
);
- // we have a construction pending
- newTask( DOZER_TASK_BUILD, obj );
+ // we have a construction pending - unless a human player's dozer is already building
+ // something, in which case the new structure waits at 0% for the next free builder
+ // (see BuildAssistant::buildObjectNow / DozerPrimaryIdleState)
+ if( !( isTaskPending( DOZER_TASK_BUILD ) && owningPlayer->getPlayerType() == PLAYER_HUMAN ) )
+ newTask( DOZER_TASK_BUILD, obj );
return obj;
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 06c80f4a6..40ff36c96 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/WorkerAIUpdate.cpp
@@ -450,8 +450,11 @@ Object *WorkerAIUpdate::construct( const ThingTemplate *what,
MAKE_MODELCONDITION_MASK(MODELCONDITION_AWAITING_CONSTRUCTION)
);
- // we have a construction pending
- newTask( DOZER_TASK_BUILD, obj );
+ // we have a construction pending - unless a human player's worker is already building
+ // something, in which case the new structure waits at 0% for the next free builder
+ // (see BuildAssistant::buildObjectNow / DozerPrimaryIdleState)
+ if( !( isTaskPending( DOZER_TASK_BUILD ) && owningPlayer->getPlayerType() == PLAYER_HUMAN ) )
+ newTask( DOZER_TASK_BUILD, obj );
return obj;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
index 7fe025094..49b43c16f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
@@ -106,7 +106,8 @@ ProductionUpdateModuleData::ProductionUpdateModuleData( void )
m_doorClosingTime = 0;
m_constructionCompleteDuration = 0;
m_quantityModifiers.clear();
- m_maxQueueEntries = 9;
+ // deep queues; objects that must build one at a time still say MaxQueueEntries = 1 in INI
+ m_maxQueueEntries = 100;
m_disabledTypesToProcess = MAKE_DISABLED_MASK(DISABLED_HELD);
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
index a77d4bca1..cc34a1373 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
@@ -58,6 +58,56 @@
//#include "GameLogic/PartitionManager.h"
#include "GameLogic/AI.h"
#include "GameLogic/Module/AIUpdate.h"
+#include "GameLogic/Module/DozerAIUpdate.h"
+#include "GameLogic/Module/SupplyTruckAIUpdate.h"
+
+//-------------------------------------------------------------------------------------------------
+/** Which builder takes a structure job: the free one nearest the site, or failing that the
+ * nearest one at all. Free = no build/repair task and not hauling supplies; a builder that
+ * is merely walking somewhere counts. Fed from the selection, or from every builder of the
+ * player when nothing is selected (the control bar's stand-in builder context). */
+//-------------------------------------------------------------------------------------------------
+struct BuilderPick
+{
+ Coord3D loc;
+ Object *idle;
+ Real idleDistSqr;
+ Object *any;
+ Real anyDistSqr;
+};
+
+static void considerBuilder( Object *candidate, BuilderPick *pick )
+{
+ if( candidate == NULL || candidate->isEffectivelyDead() )
+ return;
+ if( candidate->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) || candidate->testStatus( OBJECT_STATUS_SOLD ) )
+ return;
+ AIUpdateInterface *ai = candidate->getAI();
+ DozerAIInterface *dozer = ai ? ai->getDozerAIInterface() : NULL;
+ if( dozer == NULL )
+ return;
+
+ Real dx = candidate->getPosition()->x - pick->loc.x;
+ Real dy = candidate->getPosition()->y - pick->loc.y;
+ Real distSqr = dx*dx + dy*dy;
+ if( distSqr < pick->anyDistSqr )
+ {
+ pick->any = candidate;
+ pick->anyDistSqr = distSqr;
+ }
+ const SupplyTruckAIInterface *supply = ai->getSupplyTruckAIInterface();
+ Bool hauling = supply && supply->isCurrentlyFerryingSupplies();
+ if( !dozer->isAnyTaskPending() && !hauling && distSqr < pick->idleDistSqr )
+ {
+ pick->idle = candidate;
+ pick->idleDistSqr = distSqr;
+ }
+}
+
+static void considerBuilderProc( Object *obj, void *userData )
+{
+ considerBuilder( obj, (BuilderPick *)userData );
+}
#include "GameLogic/Module/BodyModule.h"
#include "GameLogic/Module/OpenContain.h"
#include "GameLogic/Module/ProductionUpdate.h"
@@ -1371,8 +1421,19 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
//-------------------------------------------------------------------------------------------------
case GameMessage::MSG_CANCEL_UNIT_CREATE:
{
- Object *producer = getSingleObjectFromSelection(currentlySelectedGroup);
+ Object *producer = NULL;
ProductionID productionID = (ProductionID)msg->getArgument( 0 )->integer;
+
+ // an explicit producer (right-click cancel, multi-select) must be one of the selected objects
+ if( msg->getArgumentCount() > 1 && currentlySelectedGroup )
+ {
+ ObjectID producerID = msg->getArgument( 1 )->objectID;
+ const VecObjectID& ids = currentlySelectedGroup->getAllIDs();
+ if( std::find( ids.begin(), ids.end(), producerID ) != ids.end() )
+ producer = TheGameLogic->findObjectByID( producerID );
+ }
+ else
+ producer = getSingleObjectFromSelection(currentlySelectedGroup);
// sanity
if( producer == NULL )
@@ -1408,6 +1469,30 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
loc = msg->getArgument( 1 )->location;
angle = msg->getArgument( 2 )->real;
+ //
+ // the job goes to the idle builder nearest the site - among the selected builders,
+ // or, with nothing selected (stand-in builder command bar), among all the player's
+ // builders. A builder already on a job is only taken when no idle one exists.
+ //
+ {
+ BuilderPick pick;
+ pick.loc = loc;
+ pick.idle = pick.any = NULL;
+ pick.idleDistSqr = pick.anyDistSqr = 1e30f;
+ if( currentlySelectedGroup )
+ {
+ const VecObjectID& ids = currentlySelectedGroup->getAllIDs();
+ for( VecObjectID::const_iterator it = ids.begin(); it != ids.end(); ++it )
+ considerBuilder( TheGameLogic->findObjectByID( *it ), &pick );
+ }
+ if( pick.any == NULL && thisPlayer )
+ thisPlayer->iterateObjects( considerBuilderProc, &pick );
+
+ Object *chosen = pick.idle ? pick.idle : pick.any;
+ if( chosen )
+ constructorObject = chosen;
+ }
+
if( place == NULL || constructorObject == NULL )
break; //These are not crashes, as the object may have died before this message came in