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(gamelogic): save the attack move's ammo count, and audit the fork's AI diff

Olcay Seygand64b6af81583
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a86981117..c6aa81b62 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -93,6 +93,11 @@ What you get now:
whatever it happened to pick — usually the nearest thing — while something else emptied a magazine
into its flank. It now turns onto whatever is actually hurting it, as long as it can reach it, and
goes back to the advance once that is dealt with.
+- **Loading a save does not scramble what your attack-moving units do next.** The count of rounds a
+ unit had spent on its current fight — which is how the game tells a real firing pass from a failed
+ approach — was not among the things written into a save file, and on load it came back as whatever
+ happened to be sitting in memory. Units resuming an attack-move from a save could pause a second
+ before finding their next target, or not, at random. It is saved now.
## Your units stop shooting corpses
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
index 17333110c..1ffb46411 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
@@ -3526,6 +3526,7 @@ AIAttackMoveToState::AIAttackMoveToState( StateMachine *machine ) : AIMoveToStat
m_engageOrigin.zero();
m_victimID = INVALID_ID;
m_engageStartFrame = 0;
+ m_ammoAtEngage = 0;
m_frameToScanOn = 0;
m_reengageGoalDistSqr = 0.0f;
m_groupSpeed = FAST_AS_POSSIBLE;
@@ -3554,7 +3555,7 @@ void AIAttackMoveToState::crc( Xfer *xfer )
void AIAttackMoveToState::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 4;
+ XferVersion currentVersion = 5;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -3577,6 +3578,12 @@ void AIAttackMoveToState::xfer( Xfer *xfer )
xfer->xferReal(&m_reengageGoalDistSqr);
xfer->xferReal(&m_groupSpeed);
}
+ if (version>=5) {
+ // m_isEngaging has been saved since version 3, so a load could resume a fight with this
+ // still holding whatever was in the block - and stopEngaging reads it to decide whether the
+ // fight was real, which sets m_frameToScanOn. Uninitialized memory driving a logic clock.
+ xfer->xferInt(&m_ammoAtEngage);
+ }
xfer->xferSnapshot(m_attackMoveMachine);
} // end xfer
@@ -3723,6 +3730,38 @@ void AIAttackMoveToState::startEngaging( Object *victim )
m_attackMoveMachine->setState( AI_ATTACK_OBJECT );
}
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Did that fight ever really start? With no Object involved so test_gameengine can link straight
+ * to it - the same trick as AIAttackMove_leashBroken above.
+ *
+ * A fight that ended quickly with the victim still standing is normally one we could not touch -
+ * unreachable, unattackable, out of ammo - and the caller moves on for a moment rather than pick
+ * the same target straight back up. But "quickly" is a ground unit's measure. An aircraft's whole
+ * firing pass takes a handful of frames and routinely leaves the target alive, so every successful
+ * pass was being read as a failure and charged a re-acquire delay, which is exactly backwards when
+ * what you want is the load spent as fast as possible. So ask whether we actually shot, and only
+ * fall back on the clock when we did not.
+ *
+ * Spending rounds is the only positive evidence: an ammoAtEngage of zero therefore reads as "never
+ * fired", which is what a save written before the count was recorded loads as. That is the
+ * conservative answer - a re-acquire delay of a second - not an uninitialized one.
+ */
+Bool AIAttackMove_engageWasADud( Bool victimStillAlive, Int ammoAtEngage, Int ammoNow,
+ UnsignedInt engageStartFrame, UnsignedInt now, Int dudFrames )
+{
+ if (!victimStillAlive)
+ return FALSE; // it died: whatever we did to it worked
+
+ if (ammoNow < ammoAtEngage)
+ return FALSE; // we spent rounds on it, so the fight was real however short
+
+ if (now < engageStartFrame)
+ return FALSE; // engaged in the future: not a dud, and never wrap the subtraction
+
+ return (now - engageStartFrame) < (UnsignedInt)dudFrames;
+}
+
//----------------------------------------------------------------------------------------------------------
/**
* The fight is over (victim dead, out of reach, or leashed off). Put back what startEngaging
@@ -3749,19 +3788,9 @@ void AIAttackMoveToState::stopEngaging( void )
Object *victim = TheGameLogic->findObjectByID(m_victimID);
- //
- // A fight that ended quickly with the victim still standing is normally one we could not touch
- // - unreachable, unattackable, out of ammo - and we move on for a moment rather than pick the
- // same target straight back up. But "quickly" is a ground unit's measure. An aircraft's whole
- // firing pass takes a handful of frames and routinely leaves the target alive, so every
- // successful pass was being read as a failure and charged a re-acquire delay, which is exactly
- // backwards when what you want is the load spent as fast as possible.
- //
- // So ask whether we actually shot instead of how long we stood there.
- //
- Bool firedAtIt = countRemainingAmmo( owner ) < m_ammoAtEngage;
- if (victim && !victim->isEffectivelyDead() && !firedAtIt &&
- now - m_engageStartFrame < ATTACK_MOVE_DUD_ENGAGE_FRAMES)
+ if (AIAttackMove_engageWasADud( victim != NULL && !victim->isEffectivelyDead(),
+ m_ammoAtEngage, countRemainingAmmo( owner ),
+ m_engageStartFrame, now, ATTACK_MOVE_DUD_ENGAGE_FRAMES ))
{
// we could not touch it (unreachable, cannot attack it, out of ammo) - move on for a second.
m_frameToScanOn = now + ATTACK_MOVE_REACQUIRE_DELAY;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 3400677da..43a6d6d00 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -627,6 +627,51 @@ TEST(attack_move_leashes_ground_units_but_never_aircraft)
CHECK( !AIAttackMove_leashBroken( false, true, 500.0f*cell, 0.0f, LEASH ) );
}
+/* AIStates.cpp: telling a firing pass from a failed approach when the attack move
+ disengages. A fight the unit spent rounds on was real however short; only one it
+ never fired in is charged the re-acquire delay. The ammunition count is the state
+ that was left uninitialized and unsaved - see the zero cases below. */
+extern Bool AIAttackMove_engageWasADud( Bool victimStillAlive, Int ammoAtEngage, Int ammoNow,
+ UnsignedInt engageStartFrame, UnsignedInt now, Int dudFrames );
+
+TEST(attack_move_charges_the_reacquire_delay_only_for_a_fight_that_never_happened)
+{
+ const Int DUD = 15; /* ATTACK_MOVE_DUD_ENGAGE_FRAMES, protected on the state */
+
+ /* stood next to it for a moment, fired nothing, it is still alive: a dud. */
+ CHECK( AIAttackMove_engageWasADud( true, 8, 8, 1000, 1000, DUD ) );
+ CHECK( AIAttackMove_engageWasADud( true, 8, 8, 1000, 1014, DUD ) );
+
+ /* the same non-fight, but long enough that it was a real attempt, not a bounce. */
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 8, 1000, 1015, DUD ) );
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 8, 1000, 9999, DUD ) );
+
+ /* it died. whatever we did worked, so go straight back to scanning. */
+ CHECK( !AIAttackMove_engageWasADud( false, 8, 8, 1000, 1000, DUD ) );
+
+ /* one round spent inside two frames is the aircraft firing pass: a real fight,
+ and charging it a re-acquire delay is exactly backwards - the load is what
+ sends the aircraft home, so it should be spent as fast as it can be. */
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 7, 1000, 1002, DUD ) );
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 0, 1000, 1001, DUD ) );
+
+ /* an ammunition count of zero at engage time can never read as "we fired": the
+ count now is a sum of remaining rounds and is never negative. That is what a
+ save written before the count existed, and the constructor, both load as - so
+ the answer there is the conservative "dud", not whatever was in the block. */
+ CHECK( AIAttackMove_engageWasADud( true, 0, 0, 1000, 1000, DUD ) );
+ CHECK( AIAttackMove_engageWasADud( true, 0, 12, 1000, 1000, DUD ) );
+ CHECK( !AIAttackMove_engageWasADud( true, 0, 12, 1000, 1015, DUD ) );
+
+ /* a unit with no ammunition at all on either side of a fight it could not start. */
+ CHECK( AIAttackMove_engageWasADud( true, 0, 0, 0, 0, DUD ) );
+
+ /* both frames are unsigned: an engage stamped after 'now' must not wrap the
+ subtraction into a small number and read as a fresh dud. */
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 8, 1000, 999, DUD ) );
+ CHECK( !AIAttackMove_engageWasADud( true, 8, 8, 0xffffffff, 0, DUD ) );
+}
+
/* AIStates.cpp: on attack move, whoever is shooting us wins the target selection
over whatever the scan would otherwise have picked. */
extern Bool AIAttackMove_shouldRetaliate( UnsignedInt lastDamageFrame, UnsignedInt now, Int windowFrames,