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 spends its cash instead of sitting on it

Olcay Seygan6d2a0b028840
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aadfbaad2..aaccec98b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,11 @@ 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 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.
+- It puts more harvesters on a supply centre with more piles around it, instead of the same three numbers everywhere.
+
## The computer picks its fights
- Which enemy it goes after was the nearest one and nothing else, plus a rule with its sign the wrong way round: an opponent who had lost his units or his production had his distance treated as half the map, so the computer ignored the one it was about to beat. That is what dragged matches out.
- It now weighs distance, whether the target is crippled - an opportunity, not a distraction - and how much of what it can see that player is worth. Only a genuinely finished enemy is skipped.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index d5bd423c4..38f3f2a7b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -268,6 +268,14 @@ Bool aiShouldMass( Real waitingThreat, Real enemyVisibleThreat, Real massFractio
Real aiEnemyCost( Real distSqr, Bool crippled, Bool alreadyTargetedByAnotherAI,
Bool isAttackingMe, Real economyShare );
+/** Shorten a build delay when the money is piling up. The classic AI disease is sitting on twenty
+ * thousand cash while trickling one unit out at a time; the delays already move with wealth
+ * (TAiData's Wealthy/Poor rates), but only one step, and a hoard walks straight past it.
+ *
+ * Twice the threshold halves the delay, four times quarters it, and it stops there - this is a
+ * decision to spend faster, not a production multiplier, and D10 draws that line. */
+Int aiHoardAdjustedDelay( Int frames, Int money, Int hoardAt );
+
class TAiData : public Snapshot
{
public:
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index 8faa08df4..a5d0def26 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -1101,6 +1101,20 @@ Real aiEnemyCost( Real distSqr, Bool crippled, Bool alreadyTargetedByAnotherAI,
return (cost < 0.0f) ? 0.0f : cost;
}
+//-------------------------------------------------------------------------------------------------
+Int aiHoardAdjustedDelay( Int frames, Int money, Int hoardAt )
+{
+ if( hoardAt <= 0 || money <= hoardAt || frames <= 1 )
+ return frames;
+
+ Real scale = INT_TO_REAL( hoardAt ) / INT_TO_REAL( money ); // 2x the cash -> half the wait
+ if( scale < 0.25f )
+ scale = 0.25f; // and no faster than four times
+
+ Int out = REAL_TO_INT_CEIL( INT_TO_REAL( frames ) * scale );
+ return (out < 1) ? 1 : out;
+}
+
Real aiCounterScore( const AIEnemyComposition &enemy, const AITeamCapability &team )
{
Real score = 0.0f;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 6fe397dc3..e1c88caff 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -329,6 +329,32 @@ void AIPlayer::checkForSupplyCenter( BuildListInfo *info, Object *bldg )
}
resInfo = resInfo->m_next;
}
+ //
+ // Adaptive harvesters (B6): the three INI numbers do not know how much supply is actually
+ // parked next to this centre. One extra gatherer per warehouse beyond the first within the
+ // safe radius, capped at two extra - a centre with three piles beside it can keep more
+ // trucks busy, and one with a single pile cannot.
+ //
+ if( getSkillProfile()->m_adaptiveHarvesters )
+ {
+ Int warehouses = 0;
+ PartitionFilterAcceptByKindOf supplyOnly( MAKE_KINDOF_MASK( KINDOF_SUPPLY_SOURCE ), KINDOFMASK_NONE );
+ PartitionFilterAlive filterAlive;
+ PartitionFilter *filters[] = { &supplyOnly, &filterAlive, 0 };
+
+ MemoryPoolObjectHolder hold;
+ SimpleObjectIterator *nearby = ThePartitionManager->iterateObjectsInRange(
+ bldg->getPosition(), TheAI->getAiData()->m_supplyCenterSafeRadius, FROM_CENTER_2D, filters );
+ hold.hold( nearby );
+ for( Object *w = nearby->first(); w; w = nearby->next() )
+ ++warehouses;
+
+ Int extra = warehouses - 1;
+ if( extra < 0 ) extra = 0;
+ if( extra > 2 ) extra = 2;
+ desiredGatherers += extra;
+ }
+
info->setSupplyBuilding(true);
info->setCurrentGatherers(-1);
info->setDesiredGatherers(desiredGatherers+1); // get a freebie with the supply depots.
@@ -3054,13 +3080,15 @@ void AIPlayer::queueUnits( void )
*/
Int AIPlayer::computeStructureDelay( void )
{
- return computeBuildDelay( TheAI->getAiData()->m_structureSeconds,
+ return aiHoardAdjustedDelay( computeBuildDelay( TheAI->getAiData()->m_structureSeconds,
m_player->getMoney()->countMoney(),
TheAI->getAiData()->m_resourcesPoor,
TheAI->getAiData()->m_resourcesWealthy,
TheAI->getAiData()->m_structuresPoorMod,
TheAI->getAiData()->m_structuresWealthyMod,
- getBuildRateScale() );
+ getBuildRateScale() ),
+ m_player->getMoney()->countMoney(),
+ getSkillProfile()->m_cashHoardThreshold );
}
//----------------------------------------------------------------------------------------------------------
@@ -3100,13 +3128,15 @@ Int AIPlayer::computeBuildDelay( Real seconds, Int money, Int poorAt, Int wealth
*/
Int AIPlayer::computeTeamDelay( void )
{
- return computeBuildDelay( (Real)m_teamSeconds,
+ return aiHoardAdjustedDelay( computeBuildDelay( (Real)m_teamSeconds,
m_player->getMoney()->countMoney(),
TheAI->getAiData()->m_resourcesPoor,
TheAI->getAiData()->m_resourcesWealthy,
TheAI->getAiData()->m_teamPoorMod,
TheAI->getAiData()->m_teamWealthyMod,
- getBuildRateScale() );
+ getBuildRateScale() ),
+ m_player->getMoney()->countMoney(),
+ getSkillProfile()->m_cashHoardThreshold );
}
//----------------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 6f287242f..dbf6ca6b1 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -6012,3 +6012,38 @@ TEST(enemy_choice_prefers_the_weak_the_rich_and_the_near)
CHECK( aiEnemyCost( CLOSE_BY, FALSE, FALSE, FALSE, -5.0f ) <=
aiEnemyCost( CLOSE_BY, FALSE, FALSE, FALSE, 0.0f ) );
}
+
+
+/** B6: the classic AI disease is sitting on twenty thousand cash while trickling out one unit at a
+ time. The existing delays move with wealth, but only one step - past the Wealthy threshold a
+ hoard of any size buys the same discount. This is the second step, and it is a decision to
+ spend faster rather than a production multiplier, which is the line D10 draws. */
+TEST(a_cash_hoard_shortens_the_wait_but_only_so_far)
+{
+ // under the threshold nothing changes at all
+ CHECK_EQ( 300, aiHoardAdjustedDelay( 300, 4000, 5000 ) );
+ CHECK_EQ( 300, aiHoardAdjustedDelay( 300, 5000, 5000 ) );
+
+ // twice the threshold, half the wait
+ CHECK_EQ( 150, aiHoardAdjustedDelay( 300, 10000, 5000 ) );
+
+ // and it stops at four times, however big the pile gets
+ CHECK_EQ( 75, aiHoardAdjustedDelay( 300, 20000, 5000 ) );
+ CHECK_EQ( 75, aiHoardAdjustedDelay( 300, 200000, 5000 ) );
+ CHECK_EQ( 75, aiHoardAdjustedDelay( 300, 2000000, 5000 ) );
+
+ // no threshold is the bottom two rungs, and is EA's behaviour: never hurry
+ CHECK_EQ( 300, aiHoardAdjustedDelay( 300, 999999, 0 ) );
+
+ // a delay never goes to zero, or the AI would try to build on every frame
+ CHECK( aiHoardAdjustedDelay( 1, 999999, 100 ) >= 1 );
+ CHECK( aiHoardAdjustedDelay( 2, 999999, 100 ) >= 1 );
+
+ // the ladder switches it on at Medium and tightens it as you climb
+ TAiData data;
+ CHECK_EQ( 0, data.m_skill[ AISKILL_EASY ].m_cashHoardThreshold );
+ CHECK_EQ( 0, data.m_skill[ AISKILL_STEADY ].m_cashHoardThreshold );
+ CHECK( data.m_skill[ AISKILL_MEDIUM ].m_cashHoardThreshold > 0 );
+ CHECK( data.m_skill[ AISKILL_MERCILESS ].m_cashHoardThreshold <
+ data.m_skill[ AISKILL_MEDIUM ].m_cashHoardThreshold );
+}