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): keep a random sound's last pick across the tick's queue

Olcay Seyganf88967143b56
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/AudioEventRTS.h b/GeneralsMD/Code/GameEngine/Include/Common/AudioEventRTS.h
index 20ebb2e6c..1481f9bb5 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/AudioEventRTS.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/AudioEventRTS.h
@@ -152,6 +152,16 @@ public:
void setUninterruptable( Bool uninterruptable ) { m_uninterruptable = uninterruptable; }
void setSkipAttack( Bool skipAttack ) { m_skipAttack = skipAttack; } ///< start on the loop itself, for a loop picked up again mid-way
+ /** LOGIC-THREAD.md M5. A random list remembers its last pick in the caller's event so it does not
+ * play one variant twice running, and a sound a tick asks for picks after the tick. The caller is
+ * held here until then: the pick reads its memory and writes the new pick back, as an immediate
+ * add did, unless the caller died first. holdCallerForPick returns a slot, or -1 when the table is
+ * full; heldCaller answers NULL for a slot whose event was destroyed. forgetHeldCallers runs once
+ * the tick's effects ran or were dropped. */
+ static Int holdCallerForPick( AudioEventRTS *caller );
+ static AudioEventRTS *heldCaller( Int slot );
+ static void forgetHeldCallers( void );
+
// This will retrieve the appropriate position based on type.
const Coord3D *getCurrentPosition( void );
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp
index ca2474fd2..288f3739b 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp
@@ -307,9 +307,43 @@ AudioEventRTS& AudioEventRTS::operator=( const AudioEventRTS& right )
}
+//-------------------------------------------------------------------------------------------------
+namespace
+{
+ // Plain data so a static AudioEventRTS destroyed at exit finds it intact. ponytail: a fixed table;
+ // a tick asking for more random sounds than this loses the memory for the rest, as M5 did for all.
+ const Int MAX_HELD_CALLERS = 512;
+ AudioEventRTS *s_heldCallers[ MAX_HELD_CALLERS ];
+ Int s_heldCallerCount = 0;
+}
+
+//-------------------------------------------------------------------------------------------------
+Int AudioEventRTS::holdCallerForPick( AudioEventRTS *caller )
+{
+ if( s_heldCallerCount == MAX_HELD_CALLERS )
+ return -1;
+ s_heldCallers[ s_heldCallerCount ] = caller;
+ return s_heldCallerCount++;
+}
+
+//-------------------------------------------------------------------------------------------------
+AudioEventRTS *AudioEventRTS::heldCaller( Int slot )
+{
+ return slot < 0 ? NULL : s_heldCallers[ slot ];
+}
+
+//-------------------------------------------------------------------------------------------------
+void AudioEventRTS::forgetHeldCallers( void )
+{
+ s_heldCallerCount = 0;
+}
+
//-------------------------------------------------------------------------------------------------
AudioEventRTS::~AudioEventRTS()
{
+ for( Int i = 0; i < s_heldCallerCount; ++i )
+ if( s_heldCallers[ i ] == this )
+ s_heldCallers[ i ] = NULL;
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
index 6293a2d70..e09a225e3 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/GameAudio.cpp
@@ -461,13 +461,17 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd)
// A list played in order steps on from the index the caller's event holds; the build writes the
// step into its own copy, so the caller's event takes it here. A random list remembers its last
- // pick to avoid playing it twice running, and that pick is made after the tick, too late for the
- // caller's event: a looping random sound logic restarts can repeat a variant now.
+ // pick to avoid playing it twice running, and that pick is made after the tick: the caller is held
+ // until then, so the pick reads its memory as it stands and leaves the new pick in it.
const AudioEventInfo *info = eventToAdd->getAudioEventInfo();
- if (info->m_soundType != AT_Music && info->m_soundType != AT_Streaming
- && !info->m_sounds.empty() && !BitTest(info->m_control, AC_RANDOM)) {
- AudioEventRTS *caller = const_cast<AudioEventRTS*>(eventToAdd);
- caller->setPlayingAudioIndex(caller->getPlayingAudioIndex() + 1);
+ AudioEventRTS *caller = const_cast<AudioEventRTS*>(eventToAdd);
+ Int callerSlot = -1;
+ if (info->m_soundType != AT_Music && info->m_soundType != AT_Streaming && !info->m_sounds.empty()) {
+ if (BitTest(info->m_control, AC_RANDOM)) {
+ callerSlot = AudioEventRTS::holdCallerForPick(caller);
+ } else {
+ caller->setPlayingAudioIndex(caller->getPlayingAudioIndex() + 1);
+ }
}
// A scripted sound picks its file from the logic seed without moving it; pin the seed it would
@@ -478,7 +482,7 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd)
CopyGameLogicSeed(seed.data());
}
- LogicThread::callAfterTick([this, copy, handle, logical, seed]() mutable {
+ LogicThread::callAfterTick([this, copy, handle, logical, seed, callerSlot]() mutable {
std::vector<AudioHandle>::iterator queued = std::find(m_handlesQueuedFromTick.begin(), m_handlesQueuedFromTick.end(), handle);
if (queued != m_handlesQueuedFromTick.end()) {
m_handlesQueuedFromTick.erase(queued);
@@ -486,7 +490,14 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd)
if (logical) {
PinGameLogicSeedForUnchanged(seed.data());
}
+ AudioEventRTS *liveCaller = AudioEventRTS::heldCaller(callerSlot);
+ if (liveCaller) {
+ copy.setPlayingAudioIndex(liveCaller->getPlayingAudioIndex());
+ }
addAudioEventNow(&copy, handle);
+ if (liveCaller) {
+ liveCaller->setPlayingAudioIndex(copy.getPlayingAudioIndex());
+ }
if (logical) {
PinGameLogicSeedForUnchanged(NULL);
}
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
index 8cfa17901..c4364fe00 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
@@ -23,6 +23,7 @@
#include "PreRTS.h"
#include "Common/LogicThread.h"
+#include "Common/AudioEventRTS.h"
#include "Common/EarlyCommandLine.h"
#include "Common/MessageStream.h"
#include "GameClient/ControlBar.h"
@@ -100,6 +101,7 @@ namespace
for( size_t i = 0; i < s_effects.size(); ++i )
s_effects[ i ]();
s_effects.clear();
+ AudioEventRTS::forgetHeldCallers();
TheGameClient->flushDestroyedDrawables();
ControlBar::flushLogicCalls();
}
@@ -295,6 +297,7 @@ void LogicThread::callAfterTick( const std::function<void()>& call )
void LogicThread::discardEffects( void )
{
s_effects.clear();
+ AudioEventRTS::forgetHeldCallers();
}
//-------------------------------------------------------------------------------------------------