Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenSHC/Audio/mss/SoundFlagsAndLoopCount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenSHC {
namespace Audio {
namespace MSS {

typedef uint SoundFlagsAndLoopCount;
typedef int SoundFlagsAndLoopCount;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it is int? Some & operations naturally produce negative right-hand-sides if an int.

@TheRedDaemon TheRedDaemon Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It increased the matching and actually started producing the binary mask instead of just using a mov to a 16bit register.
But this function is another one of these annoying ones. A lot of branch optimization, gotos.
Always in the hope that there is a when: When we manage to match the function, I will try reverting this change and test around a bit.
If this goes through, all other methods need to be tested again, of course.

} // namespace MSS
} // namespace Audio
} // namespace OpenSHC
215 changes: 215 additions & 0 deletions src/OpenSHC/Audio/mss/SoundSystem/playRandomBackgroundMusicUnk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include "../SoundSystem.func.hpp"

#include "OpenSHC/Game/GameCore.func.hpp"
#include "OpenSHC/OS.func.hpp"
#include "OpenSHC/Random/RNG.func.hpp"
#include "OpenSHC/Audio/mss/SoundFlagsAndLoopCountBitwiseFlagEnum.hpp"

#include "OpenSHC/Globals/DAT_GameCore.hpp"
#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp"
#include "OpenSHC/Globals/DAT_SFXDefinedData.hpp"
#include "OpenSHC/Globals/DAT_SoundEffectsHelperData1.hpp"
#include "OpenSHC/Globals/SEC_RNG.hpp"

namespace OpenSHC {
namespace Audio {
namespace MSS {

// FUNCTION: STRONGHOLDCRUSADER 0x0047BF20
void SoundSystem::playRandomBackgroundMusicUnk()
{
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field0_0x0 != 5) {
return;
}

// NOTE: Current state has a mismatch with the bytes of this condition: The following early return is
// directly placed under it. So resolve this, one can move the first early return inside this if as or,
// which will produce the proper code for this position, but messes with the rest of the function in such
// a way that is produces a lower match. Due to the amount of time already spend, the function is kept in
// this state and maybe one day tackled again when the last once are cleaned up.
if (!MACRO_CALL_MEMBER(Game::GameCore_Func::getAreWeInAInGameMenu, DAT_GameCore::ptr)()
&& (DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_SCENARIO_DESCRIPTION
|| !DAT_GameCore::instance.field25_0x64)) {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field0_0x0 == 5) {
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field0_0x0 = 1;
}
return;
}

int bVar7 = true;
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.troopValueLevel == 0) {
switch (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.volumeLevel) {
case 1:
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 0) {
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker = 1;
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[23].musicFile);
} else {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[23].musicFile);
if (DAT_GameSynchronyState::instance.DAT_CurrentGameMode != Game::GM_SOLITARY) {
DAT_GameCore::instance.battleLevel2 += 2000;
}
}
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
return;

case 2:
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 0) {
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker = 1;
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[25].musicFile);
} else {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[25].musicFile);
if (DAT_GameSynchronyState::instance.DAT_CurrentGameMode != Game::GM_SOLITARY) {
DAT_GameCore::instance.battleLevel2 += 2000;
}
}
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
return;

case 5:
++DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker;
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker > 2) {
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker = 1;
if (DAT_GameSynchronyState::instance.DAT_CurrentGameMode != Game::GM_SOLITARY) {
DAT_GameCore::instance.battleLevel2 += 2000;
}
}
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 1) {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[25].musicFile);
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
} else {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[27].musicFile);
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
}
return;
}
} else if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.volumeLevel == 1) {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field6_0x18 == 0) {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 0) {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[30].musicFile);
} else if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 1) {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[31].musicFile);
} else {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[31].musicFile);
if (DAT_GameSynchronyState::instance.DAT_CurrentGameMode != Game::GM_SOLITARY) {
DAT_GameCore::instance.battleLevel2 += 5000;
}
}
++DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker;
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
return;
}
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field0_0x0 = 1;
bVar7 = false;
} else {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field6_0x18 == 0) {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 0) {
if (this->sec_Section1055_0x3274 != 25) {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[25].musicFile);
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
return;
}
} else {
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker < 2) {
++DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker;
if (DAT_SoundEffectsHelperData1::instance.SEC_Section1079.musicTracker == 2) {
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[34].musicFile);
}
DAT_SoundEffectsHelperData1::instance.field21_0x70 = 34;
} else {
int iVar5 = DAT_SoundEffectsHelperData1::instance.field21_0x70 + -34;
int iVar6 = 0;
if (iVar5 < 0 || 7 < iVar5) {
iVar5 = 0;
}

int aiStack_50[20];
for (int i = 0; i < 8; ++i) {
if (iVar5 == i || DAT_SFXDefinedData::instance.field2_0x3a4[iVar5][i] == 0) {
continue;
}
if (i == 4) {
aiStack_50[iVar6++] = i;
aiStack_50[iVar6++] = i;
aiStack_50[iVar6++] = i;
aiStack_50[iVar6++] = i;
aiStack_50[iVar6++] = i;
aiStack_50[iVar6++] = i;
continue;
}
int iVar1 = DAT_SFXDefinedData::instance.field3_0x4a4[i];
int field6_0x34 = DAT_SoundEffectsHelperData1::instance.field6_0x34;
if ((iVar1 == 1 && iVar1 == field6_0x34) || (iVar1 == 2 && iVar1 == field6_0x34)) {
continue;
}
aiStack_50[iVar6++] = i;
if (field6_0x34 == 1 && iVar1 == 2) {
aiStack_50[iVar6++] = i;
} else if (field6_0x34 == 2 && iVar1 == 1) {
aiStack_50[iVar6++] = i;
}
}

int sfxIndex;
if (iVar6 == 0) {
sfxIndex = iVar5 + 34;
} else {
MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber1, SEC_RNG::ptr)();
sfxIndex = aiStack_50[SEC_RNG::instance.currentNumber1 % iVar6] + 34;
}
MACRO_CALL_MEMBER(SoundSystem_Func::openSound, this)(
DAT_SFXDefinedData::instance.DAT_SFX_Pointers[sfxIndex].musicFile);
DAT_SoundEffectsHelperData1::instance.field21_0x70 = sfxIndex;
}
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
return;
}
} else {
DAT_SoundEffectsHelperData1::instance.SEC_Section1079.field0_0x0 = 1;
bVar7 = false;
}
}
if (this->musicFileHandle_0x178 != -1) {
DAT_SoundEffectsHelperData1::instance.field14_0x54 = TRUE;
MACRO_CALL(OS_Func::_ucrt_close)(this->musicFileHandle_0x178);
this->musicFileHandle_0x178 = -1;
DAT_SoundEffectsHelperData1::instance.field14_0x54 = FALSE;
}
if (bVar7) {
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 1) {
this->streamFlagsUnkAndLoopCount_0x34[0] &= ~FLAG_SOUND_LOOP_COUNT_FIELD;
}
} else {
if ((this->streamFlagsUnkAndLoopCount_0x34[0] & FLAG_SOUND_LOOP_COUNT_FIELD) == 0) {
++this->streamFlagsUnkAndLoopCount_0x34[0];
}
}
}

}
}
}
15 changes: 15 additions & 0 deletions src/OpenSHC/Game/GameCore/getAreWeInAInGameMenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "..\GameCore.func.hpp"

namespace OpenSHC {
namespace Game {

// FUNCTION: STRONGHOLDCRUSADER 0x0046BB60
BOOLEnum GameCore::getAreWeInAInGameMenu()
{
return this->currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING
|| this->currentMenuViewType == UI::Enums::MVT_BUILD_MENU
|| this->currentMenuViewType == UI::Enums::MVT_BUILDING_AND_STATUS_MENU;
}

}
}
4 changes: 2 additions & 2 deletions status/addresses-SHC-3BB0A8C1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10261,7 +10261,7 @@ SHC_3BB0A8C1_0x0046B975 | 0.0% | Pending
SHC_3BB0A8C1_0x0046B976 | 0.0% | Pending
SHC_3BB0A8C1_0x0046B980 | 0.0% | Pending
SHC_3BB0A8C1_0x0046BB20 | 0.0% | Pending
SHC_3BB0A8C1_0x0046BB60 | 0.0% | Pending
SHC_3BB0A8C1_0x0046BB60 | 100.0% | Reimplemented
SHC_3BB0A8C1_0x0046BB80 | 0.0% | Pending
SHC_3BB0A8C1_0x0046BBD0 | 0.0% | Pending
SHC_3BB0A8C1_0x0046BC90 | 0.0% | Pending
Expand Down Expand Up @@ -10938,7 +10938,7 @@ SHC_3BB0A8C1_0x0047B800 | 100.0% | Reimplemented
SHC_3BB0A8C1_0x0047B830 | 0.0% | Pending
SHC_3BB0A8C1_0x0047B870 | 0.0% | Pending
SHC_3BB0A8C1_0x0047B890 | 100.0% | Reimplemented
SHC_3BB0A8C1_0x0047BF20 | 0.0% | Pending
SHC_3BB0A8C1_0x0047BF20 | 97.5% | Reimplemented, but structural issue due to wrong hot path selection, which causes an early return body to not be placed at the end
SHC_3BB0A8C1_0x0047C400 | 100.0% | Reimplemented
SHC_3BB0A8C1_0x0047C5D0 | 0.0% | Pending
SHC_3BB0A8C1_0x0047C670 | 0.0% | Pending
Expand Down