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): retaliate only at what a sight-bound unit can see

Olcay Seygan6bf7845b8703
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2404e0339..88e92e9bc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -337,6 +337,7 @@ found and fixed — EA's own, not port damage.**
- A bridge is ground you can see, not a wall. Sight judged a bridge by the valley floor under it, so a bridge over a gorge sat hidden behind the gorge's own edge and a tank on the bank could not see the deck it was about to cross. A standing deck counts at its own height now; a bridge knocked down to rubble goes back to the ground beneath it.
- The spy satellite, the radar van scan and the spy drone see everything in their circle again. They were blocked by hills and buildings like a tank on the ground, so a scan dropped on an enemy base came back with holes behind every building in it.
- High ground pays. A tank on a ridge now shoots, sees and picks its targets further down the slope than it could on the flat: three steps of range for every step of height over its target, up to twice its usual reach. Firing uphill costs nothing, and aircraft get no bonus from flying. Defences count too, and the range ring you see while placing one bends outward wherever the ground falls away, so you can site a Gattling Cannon on a hill and see exactly what it covers.
+- Rocket Buggies left on a hill stay on the hill. When a friend nearby got shot, every buggy around it went after the shooter, and after anything else in reach, even a tank down below that the edge of the hill hid. A buggy has to see what it fires at, so it drove to the lip to get a look, sat there unable to shoot, and then went down the slope and died. A unit that needs a clear view to fire now only answers back at what it can see from where you parked it. A buggy that picks up a salvage crate still drives to the crate.
- The five soldiers in an Overlord's battle bunker live through the Overlord's death again. A rule added in this build, that nobody gets out of a vehicle which is itself being carried, caught the bunker too, so all five died on the spot where they used to take half damage and climb out.
- Pro Rules hold for the nuke and the carpet bomb whoever fires them. The refusal stopped the recharge but not the missile: a computer player or a map script could launch a neutron missile the rules forbid, and because the timer never started, launch it again straight away.
- A Spectre gunship called in by the computer or a map script circles its target. Its aim point and its gun's aim point were only set for a gunship you called yourself, so theirs lined up on the corner of the map and walked across from there before the howitzer could fire.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIGuardRetaliate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIGuardRetaliate.h
index 292d95568..04a2b8fa9 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIGuardRetaliate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIGuardRetaliate.h
@@ -122,6 +122,9 @@ public:
Bool lookForInnerTarget(void);
static Real getStdGuardRange(const Object* obj);
+
+ ///< a unit that must see what it shoots retaliates only against what it sees from where it stands
+ static Bool canRetaliateFromHere(const Object* retaliator, Object* target);
};
//--------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuardRetaliate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuardRetaliate.cpp
index 8e4c21b18..6d5b08807 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuardRetaliate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGuardRetaliate.cpp
@@ -103,6 +103,10 @@ static Bool hasAttackedMeAndICanReturnFire( State *thisState, void* /*userData*/
return FALSE;
}
+ if (!AIGuardRetaliateMachine::canRetaliateFromHere(obj, target)) {
+ return FALSE;
+ }
+
CanAttackResult result = obj->getAbleToAttackSpecificObject(ATTACK_NEW_TARGET, target, CMD_FROM_AI);
if( result == ATTACKRESULT_POSSIBLE || result == ATTACKRESULT_POSSIBLE_AFTER_MOVING )
{
@@ -220,6 +224,27 @@ Bool AIGuardRetaliateMachine::isIdle() const
return visionRange;
}
+//--------------------------------------------------------------------------------------
+/** Retaliation picks a target nobody pointed at, so it must not take a unit somewhere the player
+ * did not put it just to get a shot. A rocket buggy parked on a plateau needs line of sight to
+ * fire, and the plateau's own edge hides the low ground: retaliating against a tank down there
+ * drove it to the lip, where it sat unable to fire, and then down the slope. Idle targeting
+ * already asks for sight (CAN_SEE in getNextMoodTarget); retaliation now asks the same. */
+//--------------------------------------------------------------------------------------
+static Bool attackNeedsSight(const Object* attacker)
+{
+ return TheAI->getAiData()->m_attackUsesLineOfSight && attacker->isKindOf(KINDOF_ATTACK_NEEDS_LINE_OF_SIGHT);
+}
+
+/*static*/ Bool AIGuardRetaliateMachine::canRetaliateFromHere(const Object* retaliator, Object* target)
+{
+ if (!attackNeedsSight(retaliator))
+ return true;
+
+ PartitionFilterLineOfSight sight(retaliator);
+ return sight.allow(target);
+}
+
//--------------------------------------------------------------------------------------
Bool AIGuardRetaliateMachine::lookForInnerTarget(void)
{
@@ -248,6 +273,7 @@ Bool AIGuardRetaliateMachine::lookForInnerTarget(void)
PartitionFilterPossibleToEnter f6(owner, CMD_FROM_AI);
PartitionFilterPossibleToHijack f7(owner, CMD_FROM_AI);
PartitionFilterRejectBuildings f8( owner );
+ PartitionFilterLineOfSight filterSight( owner );
PartitionFilter *filters[16];
Int count = 0;
@@ -274,6 +300,9 @@ Bool AIGuardRetaliateMachine::lookForInnerTarget(void)
filters[count++] = &f1;
filters[count++] = &f2;
filters[count++] = &f8; //Different than guard... we won't allow acquiring of structures (unless base defenses)
+ // see canRetaliateFromHere
+ if (attackNeedsSight(owner))
+ filters[count++] = &filterSight;
}
filters[count++] = &filterMapStatus;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
index 35653b89c..1d98f5ce6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Body/ActiveBody.cpp
@@ -45,6 +45,7 @@
#include "GameClient/InGameUI.h"
#include "GameClient/ParticleSys.h"
#include "GameLogic/AI.h"
+#include "GameLogic/AIGuardRetaliate.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/Armor.h"
#include "GameLogic/GameLogic.h"
@@ -762,7 +763,8 @@ void ActiveBody::attemptDamage( DamageInfo *damageInfo )
//If we have AI and we're mobile, then assist!
// A Stinger soldier only looks mobile: it belongs at its site, and retaliating sent it after
// whoever had shot a neighbour, several hundred out.
- if( !them->isKindOf( KINDOF_IMMOBILE ) && !them->isKindOf( KINDOF_SPAWNS_ARE_THE_WEAPONS ))
+ if( !them->isKindOf( KINDOF_IMMOBILE ) && !them->isKindOf( KINDOF_SPAWNS_ARE_THE_WEAPONS ) &&
+ AIGuardRetaliateMachine::canRetaliateFromHere( them, damager ) )
{
//But only if we can attack it!
CanAttackResult result = them->getAbleToAttackSpecificObject( ATTACK_NEW_TARGET, damager, CMD_FROM_AI );