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): jets run out and come back instead of circling a target

Olcay Seygane3d9f7f0c925
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cef0e2741..d1aaaa4ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -246,6 +246,7 @@ found and fixed — EA's own, not port damage.**
- Overlords stop getting stuck on bridges. An Overlord is wide enough that its route often met a ramp at the corner, and the bank at the corners of the long bridge on Golden Oasis sits lower than the deck. The Overlord was taken up onto the bridge, then a moment later judged too low for it as the bank fell away, put back on the ground, and it drove on underneath the deck and rocked there for the rest of the match. One Overlord in eight sent over that bridge never crossed. The ramp mouth now accepts a tank coming up from a low bank: eight out of eight get across on every test, the last one 22 seconds sooner, and twenty Crusaders over the same bridge arrive about three seconds sooner on average. A replay recorded before this change plays back differently.
- A jet shot down in the air flies on before it falls. Its wings stopped working on the frame it was hit, so it dropped almost vertically and hit the ground under the spot it was killed at, which is the one thing a plane doing three hundred knots cannot do. It keeps its lift now and gives it up over the next second or so, carrying forward and nosing over as it goes.
- And the bombers come down at all. A B-52, a Spectre gunship, a cargo plane and an A-10 all left the falling speed out of their data, and nought means the lift exactly cancels gravity: shot down, they flew on at cruising height, burning, and blew up in the air still level. Every one of them now falls at half gravity or better, and the fireball waits for the ground instead of going off on a timer somewhere over the map. Measured on the way in: a B-52 falls for 2.8 seconds and lands, where it used to detonate at a hundred feet. On the way home it still did not: after the drop a bomber flies off the map on a precise course that is allowed three times its normal lift, which held a dead one up with lift to spare. A bomber shot down after its run now comes down like one shot down before it.
+- Jets that end up on top of their target swing wide and come back on a straight run. A Raptor that came over a tank too close to fire used to bank toward it and keep banking, with the tank sitting inside its turn: one ordered onto an Overlord driving along beside it circled the tank for two and a half seconds without a shot and needed 7.3 seconds to get its four missiles off. It now holds its heading until the tank is far enough behind to turn round on, and comes back lined up: 4 seconds. A target behind the wing or just past it gets the same treatment, where the jet used to turn in, find itself over the target again and fly out anyway. Over 42 staged passes with the Raptor, the MiG, the Aurora and the Stealth Fighter, aimed at targets beside, under and behind them, parked and driving, the slowest Raptor run went from 7.3 seconds to 6.4 and the slowest MiG run from 5.6 to 4.7; on average the Raptor empties its racks 0.3 seconds sooner and the MiG 0.26. The Aurora drops its bomb on the same frame as before, a jet lined up on its target flies the same run as before, and jets fighting aircraft are untouched. A replay recorded before this change, with jets attacking the ground, will not play back the same.
## The same game on both screens, and after loading
diff --git a/GeneralsMD/Code/Data/Scenarios/jetpass.txt b/GeneralsMD/Code/Data/Scenarios/jetpass.txt
new file mode 100644
index 000000000..f9570466d
--- /dev/null
+++ b/GeneralsMD/Code/Data/Scenarios/jetpass.txt
@@ -0,0 +1,43 @@
+# Four jets at cruise, each ordered onto a ground target that is 120 off its wing when the order lands,
+# the target driving away south: the pass that ends with the target under the plane.
+#
+# generals.exe -headless -quickstart -noshellmap -observer -multiInstance -noFPSLimit
+# -map "Maps\Twilight Flame\Twilight Flame.map" -autoskirmish 2 -takeover
+# -side 0 FactionAmerica -side 1 FactionChina -seed 7 -maxframes 700
+# -scenario jetpass -logPrefix jetpass_
+#
+# The jets reach x=2200 around frame 250. Measured 2026-10-02: before the fix the Raptor circled the
+# Overlord inside its own minimum range from frame 258 to 330 (a temporary per-frame trace) and the
+# Overlord still had 1100 health at frame 450, 900 at 500 and 700 at 650. After it the Raptor runs
+# out, turns and fires all four, and the Overlord has 700 at frame 400. The other three targets end
+# on the same health in both builds. The 42-pass sweep behind the changelog numbers (attack frame
+# 240 to 300, wing offset 0 to 180, parked and driving) is in the commit message of the fix.
+
+30 spawn 1 ChinaTankOverlord 1 2200 3300
+30 spawn 0 AmericaJetRaptor 1 1000 3420
+31 move 0 AmericaJetRaptor 4400 3420
+30 spawn 1 ChinaTankBattleMaster 1 2200 3650
+30 spawn 0 ChinaJetMIG 1 1000 3770
+31 move 0 ChinaJetMIG 4400 3770
+30 spawn 1 AmericaTankCrusader 1 2200 4000
+30 spawn 0 AmericaJetAurora 1 1000 4120
+31 move 0 AmericaJetAurora 4400 4120
+30 spawn 1 GLATankScorpion 1 2200 4350
+30 spawn 0 AmericaJetStealthFighter 1 1000 4470
+31 move 0 AmericaJetStealthFighter 4400 4470
+250 attack 0 AmericaJetRaptor 1 ChinaTankOverlord
+250 move 1 ChinaTankOverlord 2200 3000
+250 attack 0 ChinaJetMIG 1 ChinaTankBattleMaster
+250 move 1 ChinaTankBattleMaster 2200 3350
+250 attack 0 AmericaJetAurora 1 AmericaTankCrusader
+250 move 1 AmericaTankCrusader 2200 3700
+250 attack 0 AmericaJetStealthFighter 1 GLATankScorpion
+250 move 1 GLATankScorpion 2200 4050
+350 tally 1 ChinaTankOverlord
+400 tally 1 ChinaTankOverlord
+450 tally 1 ChinaTankOverlord
+500 tally 1 ChinaTankOverlord
+650 tally 1 ChinaTankOverlord
+650 tally 1 ChinaTankBattleMaster
+650 tally 1 AmericaTankCrusader
+650 tally 1 GLATankScorpion
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIStateMachine.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIStateMachine.h
index cca54aa4e..fcb374cee 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIStateMachine.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIStateMachine.h
@@ -927,8 +927,9 @@ public:
m_isAttackingObject(attackingObject),
m_canTurnInPlace(false),
m_isForceAttacking(forceAttacking),
- m_setLocomotor(false)
- {
+ m_setLocomotor(false),
+ m_isRunningOut(false)
+ {
}
virtual Bool isAttack() const { return TRUE; }
virtual StateReturnType onEnter();
@@ -940,10 +941,13 @@ protected:
virtual void xfer( Xfer *xfer );
virtual void loadPostProcess();
private:
+ Coord3D computeAttackRunGoal( Object *source, const Weapon *weapon, const Coord3D &targetPos, Real relAngle, Real aimDelta );
+
const Bool m_isAttackingObject;
Bool m_canTurnInPlace;
Bool m_setLocomotor;
Bool m_isForceAttacking;
+ Bool m_isRunningOut; ///< a jet holding its heading away from a target too close to turn onto
};
EMPTY_DTOR(AIAttackAimAtTargetState)
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
index 44319e0ff..096113127 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
@@ -5420,12 +5420,14 @@ void AIAttackAimAtTargetState::crc( Xfer *xfer )
void AIAttackAimAtTargetState::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 1;
+ XferVersion currentVersion = 2;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferBool(&m_canTurnInPlace);
xfer->xferBool(&m_setLocomotor);
+ if (version >= 2)
+ xfer->xferBool(&m_isRunningOut);
} // end xfer
@@ -5449,9 +5451,10 @@ StateReturnType AIAttackAimAtTargetState::onEnter()
Locomotor* curLoco = sourceAI->getCurLocomotor();
m_canTurnInPlace = curLoco ? curLoco->getMinSpeed() == 0.0f : false;
+ m_isRunningOut = FALSE;
-// if (!victim)
+// if (!victim)
// return STATE_CONTINUE; // Just continue till we get a victim.
// Ick. This was originally a safety to a single line that required victim, and was never meant
// as an early return to all cases. We now want to use preattack frames on ground position targets
@@ -5550,6 +5553,96 @@ static void announceIntendedShot(Object *shooter, Object *victim)
TheGameLogic->getFrame());
}
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Would a plane that turns toward its target as hard as it can get the target inside its aim cone while it
+ * is still minDist or more away? relX is how far the target lies ahead of the nose, relY how far to the
+ * side, both in the plane's own frame; cosAimDelta is the cosine of the weapon's aim cone. The turn is
+ * walked round the circle of turnRadius on the target's side in 5 degree steps. A target inside that
+ * circle never enters the cone at all, which is the orbit a jet flew round a tank it could never point its
+ * nose at; one that enters it too close is the pass that ends with the target under the plane. Plain
+ * arithmetic with a fixed rotation step, so every machine walks the same points. No Object involved, so
+ * test_gameengine can link straight to it, the same trick as AIAttackMove_leashBroken.
+ */
+Bool AIAttackAim_needsRunOut( Real relX, Real relY, Real turnRadius, Real cosAimDelta, Real minDist )
+{
+ const Real STEP_COS = 0.99619470f; // cos(5 deg)
+ const Real STEP_SIN = 0.08715574f; // sin(5 deg)
+ const Real side = relY < 0.0f ? -1.0f : 1.0f;
+
+ // heading (hx, hy) and the point on the turn circle the plane is at, centre (0, side*R)
+ Real hx = 1.0f, hy = 0.0f;
+ for (Int step = 0; step < 72; ++step)
+ {
+ const Real px = turnRadius * hy * side;
+ const Real py = side * turnRadius * (1.0f - hx);
+ const Real tx = relX - px;
+ const Real ty = relY - py;
+ const Real distSqr = tx*tx + ty*ty;
+ const Real along = tx*hx + ty*hy;
+ // in the cone, or the turn has just swung the nose past it (a cone narrower than a step)
+ const Bool swungPast = side*(hx*ty - hy*tx) <= 0.0f;
+ if (along > 0.0f && (swungPast || along*along >= cosAimDelta*cosAimDelta*distSqr))
+ return distSqr < minDist*minDist;
+
+ const Real nx = hx*STEP_COS - hy*side*STEP_SIN;
+ hy = hy*STEP_COS + hx*side*STEP_SIN;
+ hx = nx;
+ }
+ return TRUE;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Where a jet steers while it lines up a ground target. Straight at it, unless turning toward it would
+ * not bring it into the aim cone before it is inside the weapon's minimum range: beside the cockpit, or
+ * just behind. Turning in from there only ever ends with the target under the plane, so the jet holds its
+ * heading instead until the target is the minimum range and a full turn circle away, then comes back round
+ * with that much straight run left to fire in. Aircraft against aircraft keep the straight chase.
+ */
+Coord3D AIAttackAimAtTargetState::computeAttackRunGoal( Object *source, const Weapon *weapon, const Coord3D &targetPos,
+ Real relAngle, Real aimDelta )
+{
+ const Locomotor *loco = source->getAI()->getCurLocomotor();
+ const Object *victim = m_isAttackingObject ? getMachineGoalObject() : NULL;
+ // a building with a gun and no turret aims through here too, and has no locomotor at all
+ if (weapon == NULL || loco == NULL || loco->getAppearance() != LOCO_WINGS || !source->isAboveTerrain() ||
+ (victim && victim->isAirborneTarget()))
+ {
+ m_isRunningOut = FALSE;
+ return targetPos;
+ }
+
+ const Coord3D *pos = source->getPosition();
+ const Coord3D *dir = source->getUnitDirectionVector2D();
+ const Real dx = targetPos.x - pos->x;
+ const Real dy = targetPos.y - pos->y;
+
+ // a hard turn bleeds speed toward the locomotor's minimum, so the circle tightens as it goes
+ const Real speed = 0.5f*(max(source->getPhysics()->getForwardSpeed2D(), loco->getMinSpeed()) + loco->getMinSpeed());
+ const Real turnRadius = speed / loco->getMaxTurnRate(source->getBodyModule()->getDamageState());
+
+ // range is measured between the bounding spheres, so the minimum range seen from the centres is longer
+ Real minRange = weapon->getTemplate()->getMinimumAttackRange() + source->getGeometryInfo().getBoundingSphereRadius();
+ if (victim)
+ minRange += victim->getGeometryInfo().getBoundingSphereRadius();
+ const Real runOutDist = minRange + 2.0f*turnRadius;
+
+ if (m_isRunningOut)
+ m_isRunningOut = dx*dx + dy*dy < sqr(runOutDist);
+ else if (fabs(relAngle) >= aimDelta)
+ m_isRunningOut = AIAttackAim_needsRunOut(dx*dir->x + dy*dir->y, dir->x*dy - dir->y*dx, turnRadius,
+ Cos(aimDelta), minRange);
+
+ if (!m_isRunningOut)
+ return targetPos;
+
+ Coord3D ahead = *pos;
+ ahead.x += dir->x * runOutDist;
+ ahead.y += dir->y * runOutDist;
+ return ahead;
+}
+
//----------------------------------------------------------------------------------------------------------
/**
* Orient the machine's owner to face towards the given target
@@ -5643,7 +5736,8 @@ StateReturnType AIAttackAimAtTargetState::update()
}
else
{
- sourceAI->setLocomotorGoalPositionExplicit(m_isAttackingObject ? *victim->getPosition() : *getMachineGoalPosition());
+ const Coord3D &targetPos = m_isAttackingObject ? *victim->getPosition() : *getMachineGoalPosition();
+ sourceAI->setLocomotorGoalPositionExplicit(computeAttackRunGoal(source, weapon, targetPos, relAngle, aimDelta));
}
if (fabs(relAngle) < aimDelta /*&& !m_preAttackFrames*/ )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
index 034cb23e8..b26af59cc 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
@@ -73,6 +73,7 @@
#include "GameLogic/Weapon.h"
#include "GameLogic/Module/AIUpdate.h"
+#include "GameLogic/Locomotor.h"
#include "GameLogic/Module/AssistedTargetingUpdate.h"
#include "GameLogic/Module/ProjectileStreamUpdate.h"
#include "GameLogic/Module/PhysicsUpdate.h"
@@ -2310,9 +2311,22 @@ Bool Weapon::computeApproachTarget(const Object *source, const Object *target, c
// +/-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;
- dir.z = -dir.z;
+ // A jet holds its heading over the top and comes back round from past the target.
+ // Steering for the far side turned it toward a target beside it and kept it there: the
+ // far side moved as the jet turned, and a Raptor circled a slow tank inside its own
+ // minimum range for two and a half seconds.
+ const AIUpdateInterface *ai = source->getAI();
+ if (ai && ai->getCurLocomotor() && ai->getCurLocomotor()->getAppearance() == LOCO_WINGS &&
+ !(target && target->isAirborneTarget()))
+ {
+ dir = *source->getUnitDirectionVector2D();
+ }
+ else
+ {
+ dir.x = -dir.x;
+ dir.y = -dir.y;
+ dir.z = -dir.z;
+ }
}
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 388d129bb..95ab3ba5e 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -1537,6 +1537,43 @@ TEST(attack_move_leashes_ground_units_but_never_aircraft)
CHECK( !AIAttackMove_leashBroken( false, true, 500.0f*cell, 0.0f, LEASH ) );
}
+/* AIStates.cpp: a jet aiming at a ground target it cannot turn onto holds its heading and
+ comes back round, rather than circling it with the target forever beside the cockpit. */
+extern Bool AIAttackAim_needsRunOut( Real relX, Real relY, Real turnRadius, Real cosAimDelta, Real minDist );
+
+TEST(jet_runs_out_from_a_target_it_cannot_turn_onto)
+{
+ const Real R = 80.0f;
+ const Real COS30 = 0.8660254f; /* the Raptor's and the MiG's 30 degree aim cone */
+
+ /* inside the turn circle on either side: no turn ever brings it into the cone, even with no
+ minimum range at all. This is the orbit. */
+ CHECK( AIAttackAim_needsRunOut( 0.0f, 100.0f, R, COS30, 0.0f ) );
+ CHECK( AIAttackAim_needsRunOut( 0.0f, -100.0f, R, COS30, 0.0f ) );
+ CHECK( AIAttackAim_needsRunOut( 20.0f, 70.0f, R, COS30, 0.0f ) );
+
+ /* just outside it the turn does bring it into the cone, but close: fine for a weapon with no
+ minimum range, a pass that ends over the top of it for one with 100. */
+ CHECK( !AIAttackAim_needsRunOut( 0.0f, 170.0f, R, COS30, 0.0f ) );
+ CHECK( AIAttackAim_needsRunOut( 0.0f, 170.0f, R, COS30, 100.0f ) );
+
+ /* already in the cone: the answer is just whether it is far enough away to shoot. */
+ CHECK( AIAttackAim_needsRunOut( 150.0f, 0.0f, R, COS30, 151.0f ) );
+ CHECK( !AIAttackAim_needsRunOut( 150.0f, 0.0f, R, COS30, 149.0f ) );
+
+ /* far ahead or far behind, the turn leaves room: chase it as before. */
+ CHECK( !AIAttackAim_needsRunOut( 400.0f, 20.0f, R, COS30, 140.0f ) );
+ CHECK( !AIAttackAim_needsRunOut( -300.0f, 0.0f, R, COS30, 140.0f ) );
+
+ /* just behind: the turn comes round with it closer than the minimum range, so run out. */
+ CHECK( AIAttackAim_needsRunOut( -100.0f, 0.0f, R, COS30, 170.0f ) );
+
+ /* a wider cone forgives a tighter spot: the Aurora's 45 degrees takes a target the 30 degree
+ weapons have to run out for. */
+ CHECK( AIAttackAim_needsRunOut( 60.0f, 60.0f, R, COS30, 0.0f ) );
+ CHECK( !AIAttackAim_needsRunOut( 60.0f, 60.0f, R, 0.7071068f, 0.0f ) );
+}
+
/* 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