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): the AI scouts

Olcay Seyganeb4762c24474
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23bd2c875..96a82d04d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,7 @@ found and fixed — EA's own, not port damage.**
- One AI decision read leftover memory, so identical matches played out differently.
- Difficulty and AI money change build pace again; every order was pinned at three seconds.
- The computer no longer shoots at what it cannot see. Its units, and its base defences, used to auto-target through the fog of war - a hard exemption written into the code for computer players only. Stealth works against it now, and so does anything it has not scouted.
+- It scouts. There was no such thing as scouting in the computer opponent - it never needed to look, because it could already see everything. One cheap unit now tours the enemy start positions and keeps going round for the rest of the match, replaced out of spare change when it dies. Every difficulty scouts; an opponent that never looks reads as broken, not as easy.
- Nor does it read your base off the map. Where your base is, what it is worth, which supply dock to expand to and where to aim a superweapon all came off a walk of your object list, in the shroud, from the first second of the match. The computer now only counts what it can see, plus the buildings it has already found - buildings do not walk away. Before it has scouted you, all it knows is where you started, which is on the map preview anyway.
## Attack-move actually attacks
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 67b3bf44a..108298cf7 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -55,7 +55,7 @@ class WorkOrder : public MemoryPoolObject,
public:
- WorkOrder():m_thing(NULL), m_factoryID(INVALID_ID), m_isResourceGatherer(false), m_numCompleted(0), m_numRequired(1), m_next(NULL) {};
+ WorkOrder():m_thing(NULL), m_factoryID(INVALID_ID), m_isResourceGatherer(false), m_isScout(false), m_numCompleted(0), m_numRequired(1), m_next(NULL) {};
Bool isWaitingToBuild( void ); ///< return true if nothing is yet building this unit
void validateFactory( Player *thisPlayer ); ///< verify factoryID still refers to an active object
@@ -69,6 +69,7 @@ public:
Int m_numRequired; ///< Number needed.
Bool m_required; ///< True if part of minimum requirement.
Bool m_isResourceGatherer; ///< True if resource gatherer.
+ Bool m_isScout; ///< True if this is the one unit kept for looking at the map.
protected:
@@ -225,6 +226,16 @@ protected:
virtual void xfer( Xfer *xfer );
virtual void loadPostProcess( void );
+ /** Look at the map. There was no concept of scouting in the AI at all - it did not need one
+ * while every strategic decision read the enemy's object list straight out of the game (see
+ * the observer index on getPlayerStructureBounds). One cheap unit, replaced when it dies,
+ * touring the enemy start positions: that is what turns fog back into information. */
+ virtual void doScouting(void);
+ Object *findScout(void); ///< a spare unit of ours that can do the touring
+ void queueScout(void); ///< ... or build the cheapest one that can
+ Bool scoutInQueue(void); ///< one is already on order
+ Bool nextScoutTarget(Coord3D *pos); ///< the next enemy start position to go and look at
+
virtual void doBaseBuilding(void);
virtual void checkReadyTeams(void);
virtual void checkQueuedTeams(void);
@@ -293,6 +304,10 @@ protected:
Player *m_player; ///< the Player we represent
+ ObjectID m_scoutID; ///< the unit currently touring the map for us
+ Int m_scoutTimer; ///< frames until the next scouting check
+ Int m_scoutTarget; ///< which player's start position the scout is walking to
+
Bool m_readyToBuildTeam; ///< True if the team select timer has expired.
Bool m_readyToBuildStructure; ///< True if the buildDelay timer has expired.
Int m_teamTimer; ///< Counts out the time between teams, as specified by ini.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 6993f69c6..814ac0bae 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -130,7 +130,10 @@ m_dozerQueuedForRepair(false),
m_supplySourceAttackCheckFrame(0),
m_attackedSupplyCenter(INVALID_ID),
m_teamSeconds(10),
-m_curWarehouseID(INVALID_ID)
+m_curWarehouseID(INVALID_ID),
+m_scoutID(INVALID_ID),
+m_scoutTimer(1),
+m_scoutTarget(0)
{
m_frameLastBuildingBuilt = TheGameLogic->getFrame();
p->setCanBuildUnits(false); // turn off ai production by default.
@@ -3161,6 +3164,8 @@ void AIPlayer::update( void )
updateBridgeRepair(); // Handle any bridge repairs.
+ doScouting(); // Keep one unit looking at the map.
+
}
//----------------------------------------------------------------------------------------------------------
@@ -3282,6 +3287,215 @@ Bool AIPlayer::dozerInQueue( void )
return false;
}
+//----------------------------------------------------------------------------------------------------------
+/** How often the scout is looked at. It only ever gets an order when it has arrived, so this is a
+ * check, not a repath. */
+static const Int SCOUT_CHECK_RATE = 2 * LOGICFRAMES_PER_SECOND;
+
+/** The cheapest thing this player could build that can walk somewhere and is not needed elsewhere.
+ * Faction-agnostic on purpose - it lands on the Ranger, the Red Guard, the Rebel and the
+ * Technical without a per-side table to keep, and on whatever a mod's cheapest unit is. */
+static const ThingTemplate *cheapestScoutTemplate( Player *player )
+{
+ const ThingTemplate *best = NULL;
+ Int bestCost = 0;
+
+ for( const ThingTemplate *t = TheThingFactory->firstTemplate(); t; t = t->friend_getNextTemplate() )
+ {
+ if( !t->isKindOf( KINDOF_INFANTRY ) && !t->isKindOf( KINDOF_VEHICLE ) )
+ continue;
+ if( t->isKindOf( KINDOF_DOZER ) || t->isKindOf( KINDOF_HARVESTER ) )
+ continue; // those have a job
+ if( t->isKindOf( KINDOF_AIRCRAFT ) || t->isKindOf( KINDOF_IMMOBILE ) )
+ continue;
+ if( !player->canBuild( t ) )
+ continue;
+
+ Int cost = t->calcCostToBuild( player );
+ if( cost <= 0 )
+ continue;
+ if( best == NULL || cost < bestCost )
+ {
+ best = t;
+ bestCost = cost;
+ }
+ }
+ return best;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * A unit of ours that is free to go and look at things. The default team is where anything built
+ * outside the team system lands - the scout queued below, and the odd leftover - so nothing here is
+ * taken off a team that has a mission.
+ */
+Object *AIPlayer::findScout( void )
+{
+ Team *team = m_player->getDefaultTeam();
+ if( team == NULL )
+ return NULL;
+
+ for( DLINK_ITERATOR<Object> iter = team->iterate_TeamMemberList(); !iter.done(); iter.advance() )
+ {
+ Object *obj = iter.cur();
+ if( obj == NULL || obj->isEffectivelyDead() )
+ continue;
+ //
+ // A positive test, not a list of exclusions: the default team also holds everything the
+ // player has in flight, and a missile is mobile, carries an AIUpdate and belongs to no
+ // team - the first draft of this went scouting with a Patriot missile several times a
+ // minute, and never with anything that could actually go and look.
+ //
+ if( !obj->isKindOf( KINDOF_INFANTRY ) && !obj->isKindOf( KINDOF_VEHICLE ) )
+ continue;
+ if( obj->isKindOf( KINDOF_PROJECTILE ) || obj->isKindOf( KINDOF_IMMOBILE ) )
+ continue;
+ if( obj->isKindOf( KINDOF_DOZER ) || obj->isKindOf( KINDOF_HARVESTER ) )
+ continue; // the economy is not a scout
+ if( obj->isKindOf( KINDOF_AIRCRAFT ) )
+ continue;
+ if( obj->getAI() == NULL )
+ continue;
+ return obj;
+ }
+ return NULL;
+}
+
+//----------------------------------------------------------------------------------------------------------
+Bool AIPlayer::scoutInQueue( void )
+{
+ for( DLINK_ITERATOR<TeamInQueue> iter = iterate_TeamBuildQueue(); !iter.done(); iter.advance() )
+ {
+ TeamInQueue *team = iter.cur();
+ if( team == NULL )
+ continue;
+ for( WorkOrder *order = team->m_workOrders; order; order = order->m_next )
+ if( order->m_isScout )
+ return true;
+ }
+ return false;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Put one cheap unit on order to do the looking. Same shape as queueDozer: a one-order priority
+ * team on the player's default team, so it never disturbs the team the AI is massing.
+ */
+void AIPlayer::queueScout( void )
+{
+ if( scoutInQueue() )
+ return;
+
+ const ThingTemplate *tTemplate = cheapestScoutTemplate( m_player );
+ if( tTemplate == NULL )
+ return;
+
+ // scouting must never be what stops a base going up, so it waits for spare change
+ if( m_player->getMoney()->countMoney() < 2 * tTemplate->calcCostToBuild( m_player ) )
+ return;
+
+ Bool canBuildUnits = m_player->getCanBuildUnits();
+ m_player->setCanBuildUnits( true );
+
+ Object *factory = findFactory( tTemplate, true );
+ if( factory )
+ {
+ WorkOrder *order = newInstance(WorkOrder);
+ order->m_thing = tTemplate;
+ order->m_factoryID = INVALID_ID;
+ order->m_numRequired = 1;
+ order->m_required = true;
+ order->m_isResourceGatherer = FALSE;
+ order->m_isScout = TRUE;
+ order->m_next = NULL;
+
+ TeamInQueue *team = newInstance(TeamInQueue);
+ prependTo_TeamBuildQueue( team );
+ team->m_priorityBuild = true;
+ team->m_workOrders = order;
+ team->m_frameStarted = TheGameLogic->getFrame();
+ team->m_team = m_player->getDefaultTeam();
+
+ AsciiString msg = "SCOUT - building one at the ";
+ msg.concat( factory->getTemplate()->getName() );
+ TheScriptEngine->AppendDebugMessage( msg, false );
+
+ m_teamDelay = 0;
+ startTraining( order, team->m_priorityBuild, team->m_team->getName() );
+ }
+
+ m_player->setCanBuildUnits( canBuildUnits );
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * The next enemy start position to go and look at. Everyone knows where the start positions are;
+ * what is on them is what has to be found out. The list is walked round and round, so the same one
+ * unit that opens the map keeps it up to date for the rest of the match.
+ */
+Bool AIPlayer::nextScoutTarget( Coord3D *pos )
+{
+ const Int count = ThePlayerList->getPlayerCount();
+ for( Int tries = 0; tries < count; ++tries )
+ {
+ m_scoutTarget = (m_scoutTarget + 1) % count;
+
+ Player *p = ThePlayerList->getNthPlayer( m_scoutTarget );
+ if( p == NULL || p == m_player )
+ continue;
+ if( m_player->getRelationship( p->getDefaultTeam() ) != ENEMIES )
+ continue;
+ if( playerStartPosition( m_scoutTarget, pos ) )
+ return TRUE;
+ }
+ return FALSE;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Keep one unit looking at the map. Cheap by construction: one unit, ordered only when it has
+ * arrived somewhere, replaced out of spare change when it dies. Every difficulty scouts - an AI
+ * that never looks reads as broken rather than as easy; what difficulty should change is how fast
+ * it acts on what it finds.
+ */
+void AIPlayer::doScouting( void )
+{
+ if( --m_scoutTimer > 0 )
+ return;
+ m_scoutTimer = SCOUT_CHECK_RATE;
+
+ Object *scout = TheGameLogic->findObjectByID( m_scoutID );
+ if( scout && (scout->isEffectivelyDead() || scout->getControllingPlayer() != m_player) )
+ scout = NULL;
+
+ if( scout == NULL )
+ {
+ m_scoutID = INVALID_ID;
+ scout = findScout();
+ if( scout )
+ {
+ m_scoutID = scout->getID();
+ // once per scout, so the log says whether this is working at all without saying it twice
+ DEBUG_LOG(("AI player %d scouting with a '%s'\n", m_player->getPlayerIndex(),
+ scout->getTemplate()->getName().str()));
+ }
+ }
+
+ if( scout == NULL )
+ {
+ queueScout();
+ return;
+ }
+
+ AIUpdateInterface *ai = scout->getAI();
+ if( ai == NULL || !ai->isIdle() )
+ return; // still on its way
+
+ Coord3D goal;
+ if( nextScoutTarget( &goal ) )
+ ai->aiMoveToPosition( &goal, CMD_FROM_AI );
+}
+
//----------------------------------------------------------------------------------------------------------
/**
* Queues up a dozer.
@@ -3440,7 +3654,7 @@ void AIPlayer::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 1;
+ XferVersion currentVersion = 2; // 2: the scout
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -3594,6 +3808,14 @@ void AIPlayer::xfer( Xfer *xfer )
xfer->xferBool( &m_baseCenterSet );
xfer->xferReal( &m_baseRadius );
+ // the scout, so a loaded game does not go blind or start a second one
+ if( version >= 2 )
+ {
+ xfer->xferObjectID( &m_scoutID );
+ xfer->xferInt( &m_scoutTimer );
+ xfer->xferInt( &m_scoutTarget );
+ }
+
xfer->xferUser( m_structuresToRepair, sizeof( ObjectID ) * MAX_STRUCTURES_TO_REPAIR );
xfer->xferObjectID( &m_repairDozer );
xfer->xferInt( &m_structuresInQueue );
@@ -3879,7 +4101,7 @@ void WorkOrder::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 1;
+ XferVersion currentVersion = 2; // 2: the scout flag
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -3904,6 +4126,10 @@ void WorkOrder::xfer( Xfer *xfer )
// is resource gatherer
xfer->xferBool( &m_isResourceGatherer );
+ // is the map-watching scout
+ if( version >= 2 )
+ xfer->xferBool( &m_isScout );
+
} // end xfer
// ------------------------------------------------------------------------------------------------