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): break a capture when the AI pulls the capturer away

Olcay Seygan1d9b9016e8c2
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
index 584864842..271c9da80 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
@@ -695,6 +695,14 @@ protected:
public:
inline StateID getCurrentStateID() const { return getStateMachine()->getCurrentStateID(); } ///< return the id of the current state of the machine
+ /// A move an AI hands a busy unit lands in the temporary state and leaves the current one alone, so ask that first.
+ inline Bool isTurningToFace() const
+ {
+ StateID state = getStateMachine()->getTemporaryState();
+ if( state == INVALID_STATE_ID )
+ state = getCurrentStateID();
+ return state == AI_FACE_OBJECT || state == AI_FACE_POSITION;
+ }
/// @ todo -- srj sez: JBA NUKE THIS CODE, IT IS EVIL
inline void friend_addToWaypointGoalPath( const Coord3D *pathPoint ) { getStateMachine()->addToGoalPath(pathPoint); }
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/SpecialAbilityUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/SpecialAbilityUpdate.h
index b53b14735..ea8c6690d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/SpecialAbilityUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/SpecialAbilityUpdate.h
@@ -57,11 +57,17 @@ enum SpecialPowerType;
* whole ability restarts - so from that frame on, nothing this unit did could break the capture.
* Human orders still stopped it through the command-source check, but an AI's orders look
* exactly like the ability's own, so a bot pulling its infantry off a derrick under fire left
- * the building flashing and ticking with nobody on it. */
+ * the building flashing and ticking with nobody on it.
+ *
+ * The pair of flags that replaced it was no better. m_facingComplete is only set by isFacing(),
+ * which the update asks once, in the frame after the turn starts; the turn is still going then,
+ * so preparation begins and nothing asks again. Every capture that needed a real turn kept the
+ * exemption for its whole length. The AI's own state is the only record of whether the unit is
+ * turning right now, and a retreat order replaces it. */
//-------------------------------------------------------------------------------------------------
-inline Bool abilityBrokenByMovement( Bool isMoving, Bool powerInUse, Bool facingInitiated, Bool facingComplete )
+inline Bool abilityBrokenByMovement( Bool isMoving, Bool powerInUse, Bool isTurningToFace )
{
- return isMoving && powerInUse && !( facingInitiated && !facingComplete );
+ return isMoving && powerInUse && !isTurningToFace;
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp
index e9237f7ef..8cc2e928a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp
@@ -230,7 +230,7 @@ UpdateSleepTime SpecialAbilityUpdate::update( void )
onExit( false );
return calcSleepTime();
}
- if( abilityBrokenByMovement( ai->isMoving(), isPowerCurrentlyInUse(), m_facingInitiated, m_facingComplete ) )
+ if( abilityBrokenByMovement( ai->isMoving(), isPowerCurrentlyInUse(), ai->isTurningToFace() ) )
{
// Capture is broken by movement just as if we had been given a direct command (above check).
// However, the time of Facing the target is considered isPowerCurrentlyInUse, but isMoving. So let that slide.
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index d6dc1b0e4..1d46fa097 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -8564,23 +8564,21 @@ TEST(every_role_commits_at_its_own_moment_and_none_of_them_is_a_bonus)
itself orders. The exemption used to be "the facing has started", a flag that is never cleared
once set, so after the first turn nothing the unit did could break the capture: an AI ordering
its riflemen to run from a fight left the derrick they were on flashing and sounding its
- capture tick with nobody there, and it changed hands anyway. */
+ capture tick with nobody there, and it changed hands anyway. The flag pair that followed had the
+ same hole whenever the turn took more than a frame, so the exemption is now the AI state itself. */
TEST(walking_away_breaks_a_capture_but_turning_towards_it_does_not)
{
// standing on the building, working: nothing to break
- CHECK( !abilityBrokenByMovement( FALSE, TRUE, TRUE, TRUE ) );
+ CHECK( !abilityBrokenByMovement( FALSE, TRUE, FALSE ) );
// the ability's own turn towards the target: moving, but not leaving
- CHECK( !abilityBrokenByMovement( TRUE, TRUE, TRUE, FALSE ) );
+ CHECK( !abilityBrokenByMovement( TRUE, TRUE, TRUE ) );
- // the turn is over and the unit is moving again: it has left, whoever ordered it
- CHECK( abilityBrokenByMovement( TRUE, TRUE, TRUE, TRUE ) );
-
- // an ability that never faces its target (NeedToFaceTarget = No) is broken by movement too
- CHECK( abilityBrokenByMovement( TRUE, TRUE, FALSE, FALSE ) );
+ // a retreat order replaced the turn: it has left, whoever ordered it
+ CHECK( abilityBrokenByMovement( TRUE, TRUE, FALSE ) );
// and nothing is broken when no power is up: walking is just walking
- CHECK( !abilityBrokenByMovement( TRUE, FALSE, TRUE, TRUE ) );
+ CHECK( !abilityBrokenByMovement( TRUE, FALSE, FALSE ) );
}
/* Every AI seat a lobby offers has to survive the trip to the other machines. The host writes the