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.

fix(gameengine): keep the retreat away from the AI's aircraft

Olcay Seygan649c97ad78ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43b8b2457..05f2349d7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -66,6 +66,7 @@ found and fixed — EA's own, not port damage.**
- Two levels. From Steady up, a unit that is personally finished pulls out of a fight its team is still winning. From Brutal up, the whole team breaks off when the exchange is lost. Easy never quits - that is part of what makes it easy.
- It only counts what it can see. An opponent that flinches from something it has not found is reading your unit list again.
- It weighs a fight by what is in it, not by who is nearby: only things that can shoot count, and buildings do not. Its own base is not a reason to feel safe and yours is not a reason to run.
+- Its aircraft are left out of it. A retreat order was what took a parked jet off the runway, so while a fight raged near the airfield the whole wing took off, flew at the base, landed, and did it again every few seconds instead of ever reaching you. Aircraft already fly home on their own when the load is spent.
- Matches finish. Two of these opponents used to fail to settle 65% of their games inside sixteen minutes; it is half that now, and they end in under seven minutes on average instead of ten.
## The computer builds against what you field
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index a8fcf84e4..2cd9a490d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -3818,6 +3818,35 @@ static void addToForce( const Object *obj, Real *health, Real *power )
*power += power2;
}
+//----------------------------------------------------------------------------------------------------------
+/** Who a "fall back to the base" order can be handed to.
+ *
+ * Not aircraft. A jet or a helicopter flies its own round trip - take off, spend the load, land
+ * to reload and heal - and a move order is what starts that cycle. Handing a parked aircraft one
+ * every decision interval had it take off, fly at the base centre, go idle, land, and take off
+ * again on the next tick, for as long as a fight near the base kept reading as lost: the
+ * airfield emptied and filled every few seconds and nothing ever reached the enemy. Everything
+ * that sends an aircraft home is already in JetAIUpdate - out of ammo, out of special ammo, idle
+ * for ReturnToBaseIdleTime - so the retreat has nothing to add and no business interrupting it.
+ *
+ * Split out with plain flags so the rule is testable without a running game.
+ */
+Bool AIRetreat_canBeOrderedHome( Bool hasAI, Bool isStructureOrImmobile, Bool ownsItsOwnLanding )
+{
+ return hasAI && !isStructureOrImmobile && !ownsItsOwnLanding;
+}
+
+/** The same rule, asked of a real object. */
+static Bool retreatCanOrderHome( const Object *obj )
+{
+ if( obj == NULL )
+ return FALSE;
+ const AIUpdateInterface *ai = obj->getAI();
+ return AIRetreat_canBeOrderedHome( ai != NULL,
+ obj->isKindOf( KINDOF_STRUCTURE ) || obj->isKindOf( KINDOF_IMMOBILE ),
+ ai != NULL && ai->getJetAIUpdate() != NULL );
+}
+
//----------------------------------------------------------------------------------------------------------
/** Look at every fight this AI is in and break off the ones it is losing.
*
@@ -3864,9 +3893,8 @@ void AIPlayer::doRetreats( void )
Object *obj = objIter.cur();
if( obj == NULL || obj->isEffectivelyDead() )
continue;
- if( obj->isKindOf( KINDOF_STRUCTURE ) || obj->isKindOf( KINDOF_IMMOBILE ) ||
- obj->isKindOf( KINDOF_PROJECTILE ) || obj->getAI() == NULL )
- continue;
+ if( obj->isKindOf( KINDOF_PROJECTILE ) || !retreatCanOrderHome( obj ) )
+ continue; // only the units that could actually be pulled out place the fight
centre.x += obj->getPosition()->x;
centre.y += obj->getPosition()->y;
count += 1.0f;
@@ -3940,9 +3968,7 @@ void AIPlayer::doRetreats( void )
for( DLINK_ITERATOR<Object> objIter = team->iterate_TeamMemberList(); !objIter.done(); objIter.advance() )
{
Object *obj = objIter.cur();
- if( obj == NULL || obj->isEffectivelyDead() || obj->getAI() == NULL )
- continue;
- if( obj->isKindOf( KINDOF_STRUCTURE ) || obj->isKindOf( KINDOF_IMMOBILE ) )
+ if( obj == NULL || obj->isEffectivelyDead() || !retreatCanOrderHome( obj ) )
continue;
if( !profile->m_retreatTeams )
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 422243073..bf343a53a 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -5945,6 +5945,28 @@ TEST(retreat_ratio_measures_the_exchange_not_the_health_bar)
}
+/** AIPlayer.cpp: who the retreat is allowed to order home. Aircraft are not: a move order is what
+ starts a jet's or a helicopter's own round trip, so one handed to a parked aircraft every
+ decision interval took it off the deck, flew it at the base centre, idled it and landed it
+ again, over and over, for as long as a fight near the base read as lost. JetAIUpdate already
+ owns every reason an aircraft goes home. */
+extern Bool AIRetreat_canBeOrderedHome( Bool hasAI, Bool isStructureOrImmobile, Bool ownsItsOwnLanding );
+
+TEST(the_retreat_never_orders_an_aircraft_home)
+{
+ // a tank in a fight it is losing: this is what the retreat is for
+ CHECK( AIRetreat_canBeOrderedHome( true, false, false ) );
+
+ // a jet or a comanche - anything running JetAIUpdate - is left alone, in the air or parked
+ CHECK( !AIRetreat_canBeOrderedHome( true, false, true ) );
+
+ // a building cannot fall back, and neither can anything without an AI to give the order to
+ CHECK( !AIRetreat_canBeOrderedHome( true, true, false ) );
+ CHECK( !AIRetreat_canBeOrderedHome( false, false, false ) );
+ CHECK( !AIRetreat_canBeOrderedHome( false, true, true ) );
+}
+
+
/** The second half of scouting: once every enemy has been placed there is nothing left to search for,
and the job becomes keeping the picture of their bases current. That is the stalest one per step
walked, recomputed at every arrival - which is what stopped the scout walking a blind lap of the