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): stop the column pumping the brake behind traffic

Olcay Seygandd9fb8200c1e
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h
index 07361af9a..64b26a6f8 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h
@@ -198,6 +198,16 @@ extern Real Crowd_remaining( const Object *obj );
1236 for this. */
extern Real Crowd_brakeSpeed( Real speed, Real blockerSpeed, Real gap, Int frames );
+/** The throttle a unit is allowed this frame, given the one it held last frame.
+
+ A brake is taken the moment it is asked for; coming off one is a ramp. Every input to the
+ speed cap is read raw once a frame - who is in front of us, how fast he is going, how much air
+ is left - and all three of them flicker: the blocker slips out of the lookahead cone for a
+ frame and the cap jumps back to full speed, the unit lunges, the blocker is inside the cone
+ again and the brake goes back on. That is a column pumping the pedal several times a second,
+ and it feeds itself, because the unit behind reads our speed and does the same harder. */
+extern Real Crowd_releaseCap( Real held, Real want, Real filter );
+
/** The inside of a bend counts as being further ahead than it is.
A unit on the inside of a turn has the least room and is the easiest to squeeze, so it is
let out of the corner first and the outside units, which have room to keep rolling, flow
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
index bc372de04..f468ca392 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h
@@ -855,6 +855,8 @@ private:
band itself is not: a filter, a stuck count and a half-finished backing-out manoeuvre are all
rebuilt within a second of the load, and a saved one restarts a jam that is over. */
Real m_crowdSepSmooth; ///< low-passedsideways push from the neighbours.
+ Real m_crowdCap; ///< the speed cap held from last frame: a brake lowers it at once, only a ramp raises it.
+ Bool m_crowdCapValid; ///< FALSE until there is a held cap to ramp up from.
Real m_crowdAim; ///< low-passeddirection the steering point is taken in, radians.
Bool m_crowdAimValid; ///< FALSEuntil the filter has something to start from.
/* Being stuck, and getting out of it. None of this is part of the crowd model: a unit that has
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/CrowdModel.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/CrowdModel.cpp
index 1f9b0749c..b11ebb95d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/CrowdModel.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/CrowdModel.cpp
@@ -520,6 +520,14 @@ Real Crowd_brakeSpeed( Real speed, Real blockerSpeed, Real gap, Int frames )
return (want < speed) ? want : speed;
}
+//-------------------------------------------------------------------------------------------------
+Real Crowd_releaseCap( Real held, Real want, Real filter )
+{
+ if (want <= held)
+ return want;
+ return held + (want - held) * filter;
+}
+
//-------------------------------------------------------------------------------------------------
Real Crowd_bendBonus( const CrowdCorridor *corr, Int sample, Real lat )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
index 577974dd9..b0dc29a3f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp
@@ -323,6 +323,8 @@ AIUpdateInterface::AIUpdateInterface( Thing *thing, const ModuleData* moduleData
m_crowdQueued = 0;
m_crowdSide = 1;
m_crowdSepSmooth = 0.0f;
+ m_crowdCap = 0.0f;
+ m_crowdCapValid = FALSE;
m_crowdAim = 0.0f;
m_crowdAimValid = FALSE;
m_noProgress = 0;
@@ -1640,6 +1642,7 @@ static const Int CROWD_BRAKE_FRAMES = 8; ///< frames of closing time a brake i
static const Int CROWD_FAN_FRAMES = 12; ///< held up this long before spreading out
static const Real CROWD_FAN_RATE = 0.8f; ///< and then sideways at this much a frame
static const Real CROWD_SEP_FILTER = 0.13f; ///< how much of a frame's sideways push is believed (~4/sec)
+static const Real CROWD_RELEASE_FILTER = 0.15f; ///< how fast the throttle comes back once a brake lifts (~half a second to full)
static const Real CROWD_AIM_CRUISE = 0.23f; ///< how fast the aim follows the route while driving (~7/sec)
static const Real CROWD_AIM_URGENT = 0.67f; ///< and while manoeuvring (~20/sec), where lag is worse than twitch
static const Real CROWD_AIM_DEAD = 0.035f; ///< two degrees: hold the wheel still rather than chase the noise
@@ -2340,6 +2343,8 @@ void AIUpdateInterface::crowdReleaseCorridor( void )
m_crowdQueued = 0;
m_crowdHoldFrame = 0;
m_crowdSepSmooth = 0.0f;
+ m_crowdCap = 0.0f;
+ m_crowdCapValid = FALSE;
m_crowdAimValid = FALSE;
/* The backing-out manoeuvre survives the route. Being wedged is the one thing a new route does
not cure - the new one starts in the same hole - and a unit that repaths every second while
@@ -3567,8 +3572,25 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed )
goalPos.z = TheTerrainLogic->getGroundHeight( goalPos.x, goalPos.y );
- if (cap < speed)
- speed = cap;
+ /* Brake now, come off the brake slowly. Everything above decides the cap from one frame's worth
+ of neighbours, and that answer is not steady: the blocker slips out of the lookahead cone for
+ a frame, or drifts a foot sideways past the lane test, and the cap goes from his speed to ours
+ and back. The unit lunges and brakes several times a second, the unit behind reads that speed
+ and does the same harder, and the whole column shunts. The lane, the sideways push and the
+ aim are all filtered for exactly this reason; the throttle was the one number still handed to
+ the locomotor raw. */
+ if (!m_crowdCapValid)
+ {
+ m_crowdCap = cap;
+ m_crowdCapValid = TRUE;
+ }
+ else
+ {
+ m_crowdCap = Crowd_releaseCap( m_crowdCap, cap, CROWD_RELEASE_FILTER );
+ }
+
+ if (m_crowdCap < speed)
+ speed = m_crowdCap;
if (TheGlobalData->m_showLanes && (now % LOGICFRAMES_PER_SECOND) == 0)
{
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 159b91fba..ea2d02767 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -12027,6 +12027,41 @@ TEST(crowd_brake_only_reads_closing_time)
CHECK_NEAR( Crowd_brakeSpeed( full, 0.0f, 0.0f, 0 ), full, 0.0001f );
}
+TEST(crowd_throttle_brakes_at_once_and_releases_slowly)
+{
+ const Real filter = 0.15f;
+ const Real full = 2.0f;
+
+ // a brake is taken whole, however hard it is
+ CHECK_NEAR( Crowd_releaseCap( full, 0.5f, filter ), 0.5f, 0.0001f );
+ CHECK_NEAR( Crowd_releaseCap( full, 0.0f, filter ), 0.0f, 0.0001f );
+ CHECK_NEAR( Crowd_releaseCap( full, full, filter ), full, 0.0001f );
+
+ // coming off one is a ramp, and it never overshoots what was asked for
+ Real held = 0.0f;
+ for (Int frame = 0; frame < 60; frame++)
+ {
+ const Real next = Crowd_releaseCap( held, full, filter );
+ CHECK( next > held );
+ CHECK( next <= full + 0.0001f );
+ held = next;
+ }
+ CHECK_NEAR( held, full, 0.01f );
+
+ /* The thing the ramp is for: a blocker flickering in and out of the lookahead cone every other
+ frame used to hand the locomotor full speed on every other frame. Half a second of that must
+ not average anywhere near full speed. */
+ held = full;
+ Real sum = 0.0f;
+ const Int frames = 15;
+ for (Int frame = 0; frame < frames; frame++)
+ {
+ held = Crowd_releaseCap( held, (frame % 2) ? full : 0.5f, filter );
+ sum += held;
+ }
+ CHECK( sum / (Real)frames < 0.7f );
+}
+
//-------------------------------------------------------------------------------------------------
// A shadow request is filled in field by field by whoever makes it, and the debris draw module
// never touched the name at all. The shadow manager reads that name the moment its first byte is