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 decides to expand, and defends what it takes

Olcay Seygana4f85bfa4423
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aaccec98b..70da39d6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,13 @@ found and fixed — EA's own, not port damage.**
- 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.
+## The computer expands, and defends what it takes
+
+- It never decided to expand. The machinery was all there - place a supply centre beside a pile, pick a pile worth taking, send a team to sit on it - and every piece of it only ran when a map script said so. An opponent that ran its starting piles dry simply stopped earning.
+- It now goes and takes the money that is lying around, out of spare cash so the army never pays for it.
+- From Brutal up the expansion comes with a defence structure facing the enemy, placed in the same job. An undefended expansion is a gift.
+- Measured: a third more army in the field and a quarter more money spent over the match.
+
## The computer spends its money
- It no longer sits on a pile of cash. Past a level that depends on the difficulty, the more money it has the faster it builds - twice the pile, half the wait, and it stops at four times. It is spending sooner, not building faster: the rate itself is untouched.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 258088455..dbd815465 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -247,6 +247,11 @@ protected:
* teams fought to the last man, which is the single most visible thing that made it look
* stupid. Two levels, because pulling out only whole teams still loses the units that were
* individually finished - Sins does exactly that, and keeps losing capital ships for it. */
+ /** B3: decide to expand, instead of waiting for a script to say so. buildBySupplies is a
+ * working expansion mechanism that only ever ran when the "Build supply center" script action
+ * fired - the AI never worked out for itself that it was running out of money. */
+ virtual void doExpansion(void);
+
virtual void doRetreats(void);
/** C2: hold a finished team at the rally point until there is a force worth sending. A string of
@@ -336,6 +341,7 @@ protected:
Int m_scoutTimer; ///< frames until the next scouting check
Int m_scoutTarget; ///< which player's start position the scout is walking to
Int m_retreatTimer; ///< frames until the next look at how the fights are going
+ Int m_expandTimer; ///< frames until the next look for somewhere to expand to
Bool m_readyToBuildTeam; ///< True if the team select timer has expired.
Bool m_readyToBuildStructure; ///< True if the buildDelay timer has expired.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index e1c88caff..e3b6d9df8 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -172,6 +172,7 @@ m_scoutID(INVALID_ID),
m_scoutTimer(1),
m_scoutTarget(0),
m_retreatTimer(1),
+m_expandTimer(1),
m_skillLevel(AISKILL_MERCILESS),
m_role(AIROLE_AGGRESSIVE)
{
@@ -3523,6 +3524,8 @@ void AIPlayer::update( void )
doRetreats(); // Break off the fights we are losing.
+ doExpansion(); // Go and take the money that is lying around.
+
}
//----------------------------------------------------------------------------------------------------------
@@ -3644,6 +3647,90 @@ Bool AIPlayer::dozerInQueue( void )
return false;
}
+//----------------------------------------------------------------------------------------------------------
+/** How often the AI looks for somewhere to expand to. Long, because an expansion is a building
+ * order and the base builder has its own rhythm to keep. */
+static const Int EXPANSION_CHECK_SECONDS = 60;
+
+//----------------------------------------------------------------------------------------------------------
+/** Notice that there is money to be had, and go and take it.
+ *
+ * Every piece of this already existed and only ever ran from a script: buildBySupplies places a
+ * structure beside a warehouse, findSupplyCenter picks a warehouse that is worth taking and is
+ * not already served, and guardSupplyCenter sends a team to sit on one. What was missing was
+ * any decision to call them - so an AI ran its starting piles dry and then simply stopped
+ * earning.
+ *
+ * The defence goes down in the same job: an undefended expansion is a gift, and the same
+ * buildBySupplies call places a base defence structure beside the warehouse facing the enemy
+ * (that branch of it is written for exactly this and had no caller either).
+ */
+//----------------------------------------------------------------------------------------------------------
+void AIPlayer::doExpansion( void )
+{
+ const AIDifficultyProfile *profile = getSkillProfile();
+ if( !profile->m_selfTriggeredExpansion )
+ return;
+
+ if( --m_expandTimer > 0 )
+ return;
+ m_expandTimer = EXPANSION_CHECK_SECONDS * LOGICFRAMES_PER_SECOND;
+
+ if( !m_player->getCanBuildBase() || !m_baseCenterSet )
+ return;
+
+ //
+ // What this faction calls a supply centre: whatever is already flagged as one on the build
+ // list. Faction-correct and mod-correct without a table, and if the AI has never had one it
+ // has nothing to expand with anyway.
+ //
+ AsciiString supplyCenterName;
+ for( BuildListInfo *info = m_player->getBuildList(); info; info = info->getNext() )
+ {
+ if( info->isSupplyBuilding() && info->getTemplateName().isNotEmpty() )
+ {
+ supplyCenterName = info->getTemplateName();
+ break;
+ }
+ }
+ if( supplyCenterName.isEmpty() )
+ return;
+
+ const ThingTemplate *tmpl = TheThingFactory->findTemplate( supplyCenterName, FALSE );
+ if( tmpl == NULL )
+ return;
+
+ // pay for it out of what is spare, not out of the army's money
+ const Int cost = tmpl->calcCostToBuild( m_player );
+ if( m_player->getMoney()->countMoney() < 2 * cost )
+ return;
+
+ //
+ // Somewhere worth going. findSupplyCenter already refuses a warehouse that is picked clean, one
+ // this AI already has a centre beside, and one that is closer to the enemy than to us.
+ //
+ const Int WORTH_TAKING = 1000;
+ if( findSupplyCenter( WORTH_TAKING ) == NULL )
+ return;
+
+ buildBySupplies( WORTH_TAKING, supplyCenterName );
+
+ if( profile->m_defendExpansions )
+ {
+ const AISideInfo *resInfo = TheAI->getAiData()->m_sideInfo;
+ while( resInfo )
+ {
+ if( resInfo->m_side == m_player->getSide() )
+ {
+ if( resInfo->m_baseDefenseStructure1.isNotEmpty() )
+ buildBySupplies( WORTH_TAKING, resInfo->m_baseDefenseStructure1 );
+ break;
+ }
+ resInfo = resInfo->m_next;
+ }
+ }
+}
+
//----------------------------------------------------------------------------------------------------------
/** How far around a fight to look for what is in it. Wide enough to catch the base defences and
* the second rank shooting into it, not so wide that a skirmish at the front counts the garrison
@@ -4374,7 +4461,10 @@ void AIPlayer::xfer( Xfer *xfer )
xfer->xferInt( &m_scoutTarget );
}
if( version >= 3 )
+ {
xfer->xferInt( &m_retreatTimer );
+ xfer->xferInt( &m_expandTimer );
+ }
// the ladder rung and the role, which are rolled once and must come back the same way
if( version >= 3 )