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): repair targeting, retaliation and contain defects

Olcay Seygan54cad5315b8a
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PoisonedBehavior.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PoisonedBehavior.h
index d3185240e..5a2614804 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PoisonedBehavior.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/PoisonedBehavior.h
@@ -93,6 +93,7 @@ private:
UnsignedInt m_poisonOverallStopFrame;
Real m_poisonDamageAmount;
DeathType m_deathType;
+ ObjectID m_poisonSource; ///< who poisoned us, so the damage-over-time kill is credited
};
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuard.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuard.cpp
index 92fb98901..d4f791a30 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuard.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuard.cpp
@@ -502,7 +502,10 @@ void AIGuardOuterState::xfer( Xfer *xfer )
/** Load post process */
// ------------------------------------------------------------------------------------------------
void AIGuardOuterState::loadPostProcess( void )
-{ AIGuardOuterState
+{
+ // the stray class name that used to sit here turned the next line into a declaration
+ // (AIGuardOuterState onEnter();), so this whole function was a no-op and a guarding unit
+ // never rebuilt m_attackState after a load - it just dropped the chase.
onEnter();
} // end loadPostProcess
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AITNGuard.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AITNGuard.cpp
index efc7db9ef..dd960a6c0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AITNGuard.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AITNGuard.cpp
@@ -458,7 +458,8 @@ void AITNGuardOuterState::xfer( Xfer *xfer )
/** Load post process */
// ------------------------------------------------------------------------------------------------
void AITNGuardOuterState::loadPostProcess( void )
-{ AITNGuardOuterState
+{
+ // see AIGuardOuterState::loadPostProcess - the stray class name made this a no-op.
onEnter();
} // end loadPostProcess
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/PoisonedBehavior.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/PoisonedBehavior.cpp
index 2fa223f66..e6f40f7c6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/PoisonedBehavior.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/PoisonedBehavior.cpp
@@ -76,6 +76,7 @@ PoisonedBehavior::PoisonedBehavior( Thing *thing, const ModuleData* moduleData )
m_poisonOverallStopFrame = 0;
m_poisonDamageAmount = 0.0f;
m_deathType = DEATH_POISONED;
+ m_poisonSource = INVALID_ID;
setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
}
@@ -122,7 +123,10 @@ UpdateSleepTime PoisonedBehavior::update()
// If it is time to do damage, then do it and reset the damage timer
DamageInfo damage;
damage.in.m_amount = m_poisonDamageAmount;
- damage.in.m_sourceID = INVALID_ID;
+ // credit the original poisoner. This used to be INVALID_ID, so a kill by the toxin's
+ // damage-over-time scored for nobody: no experience, no score, and the victim's "attacked
+ // by" reaction (and the TeamAttackedByType script condition) never fired.
+ damage.in.m_sourceID = m_poisonSource;
damage.in.m_damageType = DAMAGE_UNRESISTABLE; // Not poison, as that will infect us again
damage.in.m_damageFXOverride = DAMAGE_POISON; // but this will ensure that the right effect is played
damage.in.m_deathType = m_deathType;
@@ -173,6 +177,7 @@ void PoisonedBehavior::startPoisonedEffects( const DamageInfo *damageInfo )
m_poisonDamageFrame = now + d->m_poisonDamageIntervalData;
m_deathType = damageInfo->in.m_deathType;
+ m_poisonSource = damageInfo->in.m_sourceID;
Drawable *myDrawable = getObject()->getDrawable();
if( myDrawable )
@@ -188,6 +193,7 @@ void PoisonedBehavior::stopPoisonedEffects()
m_poisonDamageFrame = 0;
m_poisonOverallStopFrame = 0;
m_poisonDamageAmount = 0.0f;
+ m_poisonSource = INVALID_ID;
Drawable *myDrawable = getObject()->getDrawable();
if( myDrawable )
@@ -214,7 +220,7 @@ void PoisonedBehavior::xfer( Xfer *xfer )
{
// version
- const XferVersion currentVersion = 2;
+ const XferVersion currentVersion = 3;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -235,6 +241,11 @@ void PoisonedBehavior::xfer( Xfer *xfer )
xfer->xferUser(&m_deathType, sizeof(m_deathType));
}
+ if (version >= 3)
+ {
+ xfer->xferObjectID( &m_poisonSource );
+ }
+
} // end xfer
// ------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
index 2bd186fc2..a4399a056 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
@@ -400,10 +400,13 @@ void ActiveBody::attemptDamage( DamageInfo *damageInfo )
{
AIUpdateInterface *ai = obj->getAI();
+ const ContainedItemsList *riders = contain->getContainedItemsList();
- if( ai->isMoving() )
+ // a rider-change vehicle whose rider is already gone has an empty list, and
+ // nothing guarantees an AI module here: both used to be dereferenced blind.
+ if( ai == NULL || riders == NULL || riders->empty() || ai->isMoving() )
{
- //Bike is moving, so just blow it up instead.
+ //Bike is moving (or there is nobody left to remove), so just blow it up instead.
if (damager)
damager->scoreTheKill( obj );
obj->kill();
@@ -411,7 +414,7 @@ void ActiveBody::attemptDamage( DamageInfo *damageInfo )
else
{
//Removing the rider will scuttle the bike.
- Object *rider = *(contain->getContainedItemsList()->begin());
+ Object *rider = *(riders->begin());
ai->aiEvacuateInstantly( TRUE, CMD_FROM_AI );
//Kill the rider.
@@ -461,18 +464,35 @@ void ActiveBody::attemptDamage( DamageInfo *damageInfo )
const ContainedItemsList* items = contain->getContainedItemsList();
if (items)
{
- for( ContainedItemsList::const_iterator it = items->begin(); (it != items->end()) && (numKilled < killsToMake); it++ )
+ //
+ // Snapshot the victims by ID before killing any of them: kill() can run a death
+ // module that removes the rider (or a sibling) from this very std::list, which
+ // invalidated the iterator we were still holding. The old loop also read
+ // getControllingPlayer() off the object AFTER killing it.
+ //
+ std::vector<ObjectID> victims;
+ for( ContainedItemsList::const_iterator it = items->begin();
+ (it != items->end()) && ((Int)victims.size() < killsToMake); ++it )
{
- Object* thingToKill = *it;
- if (!thingToKill->isEffectivelyDead() )
- {
- if (damager)
- damager->scoreTheKill( thingToKill );
- thingToKill->kill();
- ++numKilled;
- thingToKill->getControllingPlayer()->getAcademyStats()->recordClearedGarrisonedBuilding();
- }
- } // next contained item
+ Object* candidate = *it;
+ if( candidate && !candidate->isEffectivelyDead() )
+ victims.push_back( candidate->getID() );
+ }
+
+ for( std::vector<ObjectID>::const_iterator vi = victims.begin(); vi != victims.end(); ++vi )
+ {
+ Object* thingToKill = TheGameLogic->findObjectByID( *vi );
+ if( thingToKill == NULL || thingToKill->isEffectivelyDead() )
+ continue; // a previous kill took this one with it
+
+ Player* victimPlayer = thingToKill->getControllingPlayer();
+ if (damager)
+ damager->scoreTheKill( thingToKill );
+ thingToKill->kill();
+ ++numKilled;
+ if( victimPlayer )
+ victimPlayer->getAcademyStats()->recordClearedGarrisonedBuilding();
+ }
} // if items
} // if a garrisonable thing
@@ -832,15 +852,20 @@ void ActiveBody::attemptHealing( DamageInfo *damageInfo )
// do the damage simplistic damage ADDITION
internalChangeHealth( amount );
- // record the actual damage done from this, and when it happened
+ // record the actual healing done from this, and when it happened.
+ // health goes UP here, so the clipped amount is current - prev; this was copied verbatim
+ // from the damage path and handed every caller a negative number.
damageInfo->out.m_actualDamageDealt = amount;
- damageInfo->out.m_actualDamageClipped = m_prevHealth - m_currentHealth;
+ damageInfo->out.m_actualDamageClipped = m_currentHealth - m_prevHealth;
- //then copy the whole DamageInfo struct for easy lookup
- //(object pointer loses scope as soon as atteptdamage's caller ends)
- m_lastDamageInfo = *damageInfo;
- m_lastDamageCleared = false;
- m_lastDamageTimestamp = TheGameLogic->getFrame();
+ //
+ // Only the healing timestamp is stamped here. This used to overwrite m_lastDamageInfo and
+ // m_lastDamageTimestamp with the heal record as well, which lied to everything that means
+ // "was recently attacked, and by whom": AIGuard/AITNGuard nemesis acquisition,
+ // TunnelContain, AIPlayer's supply-source-attacked check and Drawable's recoil.
+ // AIGuardRetaliate was the only consumer that defended itself (it tests for
+ // DAMAGE_HEALING explicitly), which is what gave the intent away.
+ //
m_lastHealingTimestamp = TheGameLogic->getFrame();
// if our health has gone UP then do run the damage module callback
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/MobNexusContain.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/MobNexusContain.cpp
index 3737d4556..fbfa098ce 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/MobNexusContain.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/MobNexusContain.cpp
@@ -348,7 +348,7 @@ UpdateSleepTime MobNexusContain::update()
ContainedItemsList::const_iterator it;
it = items->begin();
- while( *it )
+ while( it != items->end() ) // was while(*it), which dereferences end()
{
Object *object = *it;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OpenContain.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OpenContain.cpp
index a8d0c1f93..0c7356540 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OpenContain.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OpenContain.cpp
@@ -1448,7 +1448,7 @@ void OpenContain::processDamageToContained(Real percentDamage)
ContainedItemsList::const_iterator it;
it = items->begin();
- while( *it )
+ while( it != items->end() ) // was while(*it), which dereferences end()
{
Object *object = *it;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp
index fe08575e1..8f0ad351c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/OverlordContain.cpp
@@ -555,7 +555,7 @@ void OverlordContain::clientVisibleContainedFlashAsSelected()
ContainedItemsList::const_iterator it;
it = items->begin();
- while( *it )
+ while( it != items->end() ) // was while(*it), which dereferences end()
{
Object *object = *it;
if ( object && object->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/TransportContain.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/TransportContain.cpp
index 896941428..3766e4c75 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/TransportContain.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Contain/TransportContain.cpp
@@ -240,7 +240,7 @@ void TransportContain::letRidersUpgradeWeaponSet( void )
ContainedItemsList::const_iterator it;
it = riderList->begin();
- while( *it )
+ while( it != riderList->end() ) // was while(*it), which dereferences end()
{
Object *rider = *it;
@@ -494,7 +494,7 @@ UpdateSleepTime TransportContain::update()
ContainedItemsList::const_iterator it;
it = items->begin();
- while( *it )
+ while( it != items->end() ) // was while(*it), which dereferences end()
{
Object *object = *it;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ExperienceTracker.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ExperienceTracker.cpp
index f9ae82384..537aff5bc 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ExperienceTracker.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ExperienceTracker.cpp
@@ -96,9 +96,12 @@ void ExperienceTracker::setMinVeterancyLevel( VeterancyLevel newLevel )
{
VeterancyLevel oldLevel = m_currentLevel;
m_currentLevel = newLevel;
- m_currentExperience = m_parent->getTemplate()->getExperienceRequired(m_currentLevel); //Minimum for this level
+ // m_parent was dereferenced on the line above the null check that followed it
if (m_parent)
+ {
+ m_currentExperience = m_parent->getTemplate()->getExperienceRequired(m_currentLevel); //Minimum for this level
m_parent->onVeterancyLevelChanged( oldLevel, newLevel );
+ }
}
}
@@ -112,9 +115,12 @@ void ExperienceTracker::setVeterancyLevel( VeterancyLevel newLevel, Bool provide
{
VeterancyLevel oldLevel = m_currentLevel;
m_currentLevel = newLevel;
- m_currentExperience = m_parent->getTemplate()->getExperienceRequired(m_currentLevel); //Minimum for this level
+ // same deref-before-null-check as setMinVeterancyLevel above
if (m_parent)
+ {
+ m_currentExperience = m_parent->getTemplate()->getExperienceRequired(m_currentLevel); //Minimum for this level
m_parent->onVeterancyLevelChanged( oldLevel, newLevel, provideFeedback );
+ }
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/StealthUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/StealthUpdate.cpp
index 260f86224..cb67ffdb1 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/StealthUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/StealthUpdate.cpp
@@ -304,7 +304,10 @@ Bool StealthUpdate::allowedToStealth( Object *stealthOwner ) const
if( flags & STEALTH_ONLY_WITH_BLACK_MARKET && m_nextBlackMarketCheckFrame < now )
{
//randomize timer a little incase we have a whole bunch on the same frame.
- m_nextBlackMarketCheckFrame += data->m_blackMarketCheckFrames + GameLogicRandomValue( 0, 10 );
+ // set from now, don't accumulate: after the object slept for a while the timer was far
+ // behind the current frame, and advancing it one interval per call meant the full
+ // iterateObjects player scan below ran EVERY frame until it caught up.
+ m_nextBlackMarketCheckFrame = now + data->m_blackMarketCheckFrames + GameLogicRandomValue( 0, 10 );
//If we can't find an active black market, then we can't stealth.
Bool blackMarket = FALSE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
index 8d921ee74..5d2065d6c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
@@ -1875,7 +1875,10 @@ void Weapon::setClipPercentFull(Real percent, Bool allowReduction)
if (ammo > m_ammoInClip || (allowReduction && ammo < m_ammoInClip))
{
m_ammoInClip = ammo;
- m_status = m_ammoInClip ? OUT_OF_AMMO : READY_TO_FIRE;
+ // this test used to be inverted: a full clip was marked OUT_OF_AMMO and an empty one
+ // READY_TO_FIRE. getStatus() recomputes from m_ammoInClip so it rarely showed in play,
+ // but m_status is xfer'd and feeds the net CRC.
+ m_status = m_ammoInClip ? READY_TO_FIRE : OUT_OF_AMMO;
//CRCDEBUG_LOG(("Weapon::setClipPercentFull() just set m_status to %d (ammo in clip is %d)\n", m_status, m_ammoInClip));
m_whenLastReloadStarted = TheGameLogic->getFrame();
m_whenWeCanFireAgain = m_whenLastReloadStarted;
@@ -2046,9 +2049,10 @@ Bool Weapon::computeApproachTarget(const Object *source, const Object *target, c
{
// Don't do a 180 degree turn.
Real angle = atan2(-dir.y, -dir.x);
- Real relAngle = source->getOrientation()- angle;
- if (relAngle>2*PI) relAngle -= 2*PI;
- if (relAngle<-2*PI) relAngle += 2*PI;
+ // both inputs are already in [-PI,PI], so the difference is in [-2PI,2PI] and the
+ // old >2PI / <-2PI clamps could never fire: two headings 0.3 rad apart around the
+ // +/-PI seam measured as ~6 rad and read as "facing away".
+ Real relAngle = stdAngleDiff( source->getOrientation(), angle );
if (fabs(relAngle)<PI/2) {
dir.x = -dir.x;
dir.y = -dir.y;
@@ -2549,7 +2553,9 @@ Bool Weapon::privateFireWeapon(
}
// it's a mine, but doesn't have LandMineInterface...
- if( !found && victimObj->isKindOf( KINDOF_MINE ) || victimObj->isKindOf( KINDOF_BOOBY_TRAP ) || victimObj->isKindOf( KINDOF_DEMOTRAP ) )
+ // && binds tighter than ||, so the !found guard used to cover only the MINE arm and a
+ // booby/demo trap already disarmed above was destroyed a second time.
+ if( !found && ( victimObj->isKindOf( KINDOF_MINE ) || victimObj->isKindOf( KINDOF_BOOBY_TRAP ) || victimObj->isKindOf( KINDOF_DEMOTRAP ) ) )
{
VeterancyLevel v = sourceObj->getVeterancyLevel();
FXList::doFXPos(m_template->getFireFX(v), victimObj->getPosition(), victimObj->getTransformMatrix(), 0, victimObj->getPosition(), 0);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
index 771c8a87e..75004a817 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
@@ -642,9 +642,12 @@ CanAttackResult WeaponSet::getAbleToUseWeaponAgainstTarget( AbleToAttackType att
const Object *containedBy = source->getContainedBy();
Bool hasAWeaponInRange = FALSE;
Bool hasAWeapon = FALSE;
- for (Int slot = 0; slot < WEAPONSLOT_COUNT - 1; ++slot)
+ for (Int slot = 0; slot < WEAPONSLOT_COUNT; ++slot)
{
- Weapon *weaponToTestForRange = m_weapons[ m_curWeapon ];
+ // test the weapon in THIS slot: the loop used to read m_curWeapon every time (and stop one
+ // slot short), so an immobile source or a garrisoned soldier only ever had its current
+ // weapon range-checked and refused legitimate targets its other weapons could reach.
+ Weapon *weaponToTestForRange = m_weapons[ slot ];
if ( weaponToTestForRange )
{
hasAWeapon = TRUE;
@@ -926,9 +929,12 @@ Bool WeaponSet::chooseBestWeaponForTarget(const Object* obj, const Object* victi
break;
case PREFER_LONGEST_RANGE:
{
+ // >= like PREFER_MOST_DAMAGE above: the loop runs backwards precisely so that
+ // ties fall to the primary weapon (see the comment on the for). With a strict
+ // > the first slot examined - the LAST one - kept equal-range ties instead.
if( !weaponIsReady )
{
- if (attackRange > longestRangeBackup)
+ if (attackRange >= longestRangeBackup)
{
longestRangeBackup = attackRange;
currentDecisionBackup = (WeaponSlotType)i;
@@ -937,7 +943,7 @@ Bool WeaponSet::chooseBestWeaponForTarget(const Object* obj, const Object* victi
}
else
{
- if (attackRange > longestRange)
+ if (attackRange >= longestRange)
{
longestRange = attackRange;
currentDecision = (WeaponSlotType)i;