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(gameclient): reload bar only on slow-firing guns

Olcay Seygan2050f8e56d85
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b433b8892..9dd78876c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -825,7 +825,8 @@ A third pass went through the generals' powers, promotions and unit abilities.
- A unit that is inside a transport and also disabled wears the blue bar. Being carried and being knocked out were asked as one question, so the two together answered no: a squad EMP'd inside a bunker, or hacked while riding a Battle Bus, looked perfectly healthy from outside.
- A building being captured wears a capture clock under its health bar, filling in the colour of whoever is taking it. The building flashes while a rifleman works on it, which says somebody is at the door and nothing about whether that is nearly over: the choice between driving across to shoot him and carrying on with your fight was made blind. The bar runs from the moment he reaches the door to the moment the building changes hands, and Black Lotus hacking one gets the same bar.
- The progress bar stacked over the health bar is white, wherever it turns up - a factory turning out a tank, a silo charging, a supply drop counting down to its payout. It was yellow, which is a colour the game already spends on your own units and on damage, and a strip of it sitting directly above a coloured health bar read as part of the same bar.
-- A slow gun shows when it can fire again. A Nuke Cannon sat still for ten seconds after every shell with nothing on screen to say when the next would come. Now an amber bar over the health bar fills through the reload, on your own unit while it is selected or under the pointer. Only a wait longer than three seconds gets one: every tank gun in the game reloads in about two, and a bar that empties before you can read it is noise. Artillery, the Inferno Cannon, SCUD and Tomahawk launchers, the Scorpion's and the Comanche's missiles and the rocket buggy's clip all wear it.
+- A slow gun shows when it can fire again. A Nuke Cannon sat still for ten seconds after every shell with nothing on screen to say when the next would come. Now an amber bar over the health bar fills through the reload, on your own unit while it is selected or under the pointer. Only a gun that fires slower than one shot in three seconds gets one: every tank gun in the game reloads in about two, and a bar that empties before you can read it is noise. The Nuke Cannon, the Inferno Cannon, the artillery platform, SCUD and Tomahawk launchers, the Scorpion's missile and the Comanche's anti-tank missiles wear it.
+- The reload bar stays off the quick shooters. It used to go up whenever a wait ran past three seconds, and a reload after a fast clip does that too: a Paladin that zapped one infantryman with its point defence laser wore the bar for the next four seconds, and so did a Rocket Buggy after every volley and a Comanche after its rocket pods. Those fire about as often as a tank does, so the bar now goes by how many shots a gun gets off in a minute, reload included.
- Every garrisonable building shows how full it is, whoever holds it. An empty one shows it too, and so does your own Barracks: ten grey boxes over it for the whole match, because soldiers can walk in to heal. Untick Empty Building Slots on Options > Interface and a building wears its row only while somebody is inside.
- A vehicle's load stays private.
- What you have selected wears a white frame just outside its bar, so a selection is still readable in a crowd where every unit carries a bar - and the bar itself keeps its owner colour all the way round.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
index 850a305ca..e5df8508a 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
@@ -88,6 +88,12 @@ extern Bool Drawable_structureShowsHealthBar( Bool isBridge, Bool isUnowned, Boo
///< whether an object of these kinds is a thing that wears a health bar at all
extern Bool Drawable_kindShowsHealthBar( const KindOfMaskType& kinds );
+///< whether a weapon fires slowly enough (over three seconds a shot, clip and reload counted) to wear a reload bar
+extern Bool Drawable_weaponWearsReloadBar( Int clipSize, UnsignedInt delayFrames, UnsignedInt clipReloadFrames );
+
+///< how far a weapon's wait has come, 0 to 1, or -1 when there is no wait worth a bar
+extern Real Drawable_reloadBarFraction( UnsignedInt now, UnsignedInt started, UnsignedInt ready, UnsignedInt longest );
+
///< whether the health bar setting lets this object show one right now
extern Bool Drawable_healthBarModeShows( Int healthBarMode, Bool isSelected, Bool isMousedOver,
Bool isDamaged );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
index 8fcf77a67..d295ab452 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
@@ -4444,41 +4444,88 @@ static Int drawOwnCountdown( DisplayString *&countdown, Int seconds, Int x, Int
}
//-------------------------------------------------------------------------------------------------
-/** How far the slowest long reload on this object has come, 0 to 1, or -1 when none is running.
+/** Whether a weapon is slow enough to earn a reload bar, from its data alone: the time a full clip
+ * takes to fire and reload, divided by the shots in it, has to come to more than three seconds a
+ * shot. A clip of 0 is endless and its delay between shots is the whole wait.
*
- * Only a wait longer than three seconds counts. Every direct-fire tank gun in the game waits two
- * (the Laser General's Crusader 2.3), so a lower line would hang a bar on every main battle tank
- * that fires and empty it again before it could be read. Above it sit the weapons whose wait is
- * the whole decision: the Nuke Cannon's ten seconds, artillery, the Inferno Cannon, SCUD and
- * Tomahawk launchers, the Scorpion's and the Comanche's missiles, the rocket buggy's clip.
+ * Every direct-fire tank gun in the game waits two (the Laser General's Crusader 2.3). Above the
+ * line sit the guns whose wait is the decision: the Nuke Cannon's ten seconds, the Inferno Cannon,
+ * the artillery platform, the SCUD and Tomahawk launchers, the Scorpion's missile and the
+ * Comanche's anti-tank missiles. Below it go the weapons that empty a quick clip and then reload
+ * it - the Rocket Buggy's six rockets, the Comanche's rocket pods, the Paladin's point defence
+ * laser - which fire about as often as a tank does. The old test was the
+ * length of whichever wait was running, and those clip reloads all ran past three seconds, so a
+ * Paladin that touched an infantryman with its laser wore an amber bar for the next four. */
+//-------------------------------------------------------------------------------------------------
+Bool Drawable_weaponWearsReloadBar( Int clipSize, UnsignedInt delayFrames, UnsignedInt clipReloadFrames )
+{
+ const UnsignedInt RELOAD_BAR_MIN_FRAMES = 3 * LOGICFRAMES_PER_SECOND;
+ if( clipSize <= 0 )
+ return delayFrames > RELOAD_BAR_MIN_FRAMES;
+
+ return clipSize * delayFrames + clipReloadFrames > (UnsignedInt)clipSize * RELOAD_BAR_MIN_FRAMES;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** How far a wait from `started` to `ready` has come at `now`, 0 to 1, or -1 when it is not one
+ * worth a bar. `longest` is the longest wait the weapon's data allows. The start frame is only
+ * written when this weapon fires or reloads, so a launcher sharing its reload with another slot,
+ * or a Combat Bike handing its gun to a new rider, is told when it can fire next and keeps an old
+ * start - a wait that looked a minute long. The span is held to what the data allows. */
+//-------------------------------------------------------------------------------------------------
+Real Drawable_reloadBarFraction( UnsignedInt now, UnsignedInt started, UnsignedInt ready, UnsignedInt longest )
+{
+ const UnsignedInt RELOAD_BAR_MIN_FRAMES = 3 * LOGICFRAMES_PER_SECOND;
+
+ // 0x7fffffff is an empty clip that does not reload itself (a jet waiting for its airfield)
+ if( ready <= now || ready == 0x7fffffff )
+ return -1.0f;
+
+ UnsignedInt span = ready > started ? ready - started : longest;
+ if( span > longest )
+ span = longest;
+ if( span < ready - now )
+ span = ready - now;
+ if( span <= RELOAD_BAR_MIN_FRAMES )
+ return -1.0f;
+
+ return 1.0f - INT_TO_REAL( ready - now ) / INT_TO_REAL( span );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The slowest long reload on this object, 0 to 1, or -1 when none is running.
*
* A weapon that does less than one point of damage is a dummy that only drives an animation
* (the Battle Bus and Troop Crawler passengers' ten-second one, the angry mob's) and the SCUD
* Storm's, whose launch already has its own charge bar; none of those is a reload anyone waits on.
*
- * This reads the two frame numbers and nothing else. Weapon::getStatus() writes m_status, which
- * the logic CRC covers, so calling it from a draw would be the client writing logic state. */
+ * This reads frame numbers and template values and nothing else. Weapon::getStatus() writes
+ * m_status and WeaponTemplate::getDelayBetweenShots() draws from the logic random stream, so
+ * calling either from a draw would be the client writing logic state. */
//-------------------------------------------------------------------------------------------------
static Real reloadBarFraction( const Object *obj )
{
- const UnsignedInt RELOAD_BAR_MIN_FRAMES = 3 * LOGICFRAMES_PER_SECOND;
const UnsignedInt now = TheGameLogic->getFrame();
Real least = -1.0f;
for( Int i = 0; i < WEAPONSLOT_COUNT; ++i )
{
const Weapon *weapon = obj->getWeaponInWeaponSlot( (WeaponSlotType)i );
- if( weapon == NULL || weapon->getTemplate()->getPrimaryDamage( WeaponBonus() ) < 1.0f )
+ if( weapon == NULL )
+ continue;
+ const WeaponTemplate *tmpl = weapon->getTemplate();
+ if( tmpl->getPrimaryDamage( WeaponBonus() ) < 1.0f )
continue;
- // 0x7fffffff is an empty clip that does not reload itself (a jet waiting for its airfield)
- const UnsignedInt ready = weapon->getPossibleNextShotFrame();
- const UnsignedInt started = weapon->getLastReloadStartedFrame();
- if( ready <= now || ready == 0x7fffffff || ready - started <= RELOAD_BAR_MIN_FRAMES )
+ const UnsignedInt delay = (UnsignedInt)tmpl->getMaxDelayBetweenShots();
+ const UnsignedInt clipReload = (UnsignedInt)tmpl->getClipReloadTime( WeaponBonus() );
+ if( !Drawable_weaponWearsReloadBar( tmpl->getClipSize(), delay, clipReload ) )
continue;
- Real fraction = INT_TO_REAL( now - started ) / INT_TO_REAL( ready - started );
- if( least < 0.0f || fraction < least )
+ const UnsignedInt longest = tmpl->getClipSize() > 0 && clipReload > delay ? clipReload : delay;
+ const Real fraction = Drawable_reloadBarFraction( now, weapon->getLastReloadStartedFrame(),
+ weapon->getPossibleNextShotFrame(), longest );
+ if( fraction >= 0.0f && ( least < 0.0f || fraction < least ) )
least = fraction;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 63cb4f091..4f58a3bc9 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -7338,6 +7338,32 @@ TEST(the_spy_drone_wears_a_health_bar_and_a_toxin_field_does_not)
CHECK( Drawable_kindShowsHealthBar( healthBarKinds( KINDOF_INERT, KINDOF_SELECTABLE ) ) == FALSE );
}
+/** Only a slow gun wears the amber reload bar: over three seconds a shot, a clip's reload spread
+ over the shots in it. Frames, at 30 a second. */
+TEST(only_a_slow_firing_weapon_wears_a_reload_bar)
+{
+ CHECK( Drawable_weaponWearsReloadBar( 0, 300, 0 ) == TRUE ); // Nuke Cannon, 10 s
+ CHECK( Drawable_weaponWearsReloadBar( 0, 120, 0 ) == TRUE ); // Inferno Cannon, 4 s
+ CHECK( Drawable_weaponWearsReloadBar( 1, 0, 300 ) == TRUE ); // SCUD, one missile and a 10 s reload
+ CHECK( Drawable_weaponWearsReloadBar( 2, 6, 450 ) == TRUE ); // Scorpion's two missiles, 15 s reload
+ CHECK( Drawable_weaponWearsReloadBar( 0, 60, 0 ) == FALSE ); // a tank gun, 2 s
+ CHECK( Drawable_weaponWearsReloadBar( 0, 90, 0 ) == FALSE ); // exactly three seconds is not over three
+ CHECK( Drawable_weaponWearsReloadBar( 6, 6, 180 ) == FALSE ); // Rocket Buggy, six rockets, 6 s reload
+ CHECK( Drawable_weaponWearsReloadBar( 2, 7, 120 ) == FALSE ); // Paladin's point defence laser
+ CHECK( Drawable_weaponWearsReloadBar( 20, 6, 900 ) == FALSE ); // Comanche rocket pods
+}
+
+/** A launcher that shares its reload is told when it can fire next and keeps an old start frame;
+ the bar is measured against the longest wait its data allows, not a minute of nothing. */
+TEST(a_reload_bar_ignores_a_stale_start_frame)
+{
+ CHECK_NEAR( Drawable_reloadBarFraction( 150, 0, 300, 300 ), 0.5f, 0.001f );
+ CHECK_NEAR( Drawable_reloadBarFraction( 1150, 0, 1300, 300 ), 0.5f, 0.001f );
+ CHECK( Drawable_reloadBarFraction( 300, 0, 300, 300 ) < 0.0f ); // ready
+ CHECK( Drawable_reloadBarFraction( 10, 0, 0x7fffffff, 300 ) < 0.0f ); // empty, waits for an airfield
+ CHECK( Drawable_reloadBarFraction( 100, 100, 106, 450 ) < 0.0f ); // between two missiles
+}
+
/** Being carried is not a malfunction, so a unit inside a transport keeps its owner's colour.
Anything else that disables it turns the bar blue - and the pair together used to fail: the old
test asked `isDisabled() && !isDisabledByType(DISABLED_HELD)`, so a held unit that was then EMP'd