Fixed and completely refactored game music playing and categories choosing

This commit is contained in:
NSDeathman 2026-03-11 13:27:47 +03:00
parent 515c22f50d
commit dca3bf2bc3
9 changed files with 772 additions and 658 deletions

View file

@ -18,6 +18,10 @@ void ConsoleSoundEngine::SetIsPlayingStreamingGameMusic(bool bVal)
{ {
m_bIsPlayingStreamingGameMusic=bVal; m_bIsPlayingStreamingGameMusic=bVal;
} }
bool ConsoleSoundEngine::GetIsPlayingMenuMusic()
{
return m_bIsPlayingMenuMusic;
}
bool ConsoleSoundEngine::GetIsPlayingEndMusic() bool ConsoleSoundEngine::GetIsPlayingEndMusic()
{ {
return m_bIsPlayingEndMusic; return m_bIsPlayingEndMusic;
@ -26,6 +30,10 @@ bool ConsoleSoundEngine::GetIsPlayingNetherMusic()
{ {
return m_bIsPlayingNetherMusic; return m_bIsPlayingNetherMusic;
} }
void ConsoleSoundEngine::SetIsPlayingMenuMusic(bool bVal)
{
m_bIsPlayingMenuMusic = bVal;
}
void ConsoleSoundEngine::SetIsPlayingEndMusic(bool bVal) void ConsoleSoundEngine::SetIsPlayingEndMusic(bool bVal)
{ {
m_bIsPlayingEndMusic=bVal; m_bIsPlayingEndMusic=bVal;

View file

@ -61,9 +61,11 @@ public:
virtual bool GetIsPlayingStreamingGameMusic() ; virtual bool GetIsPlayingStreamingGameMusic() ;
virtual void SetIsPlayingStreamingCDMusic(bool bVal) ; virtual void SetIsPlayingStreamingCDMusic(bool bVal) ;
virtual void SetIsPlayingStreamingGameMusic(bool bVal) ; virtual void SetIsPlayingStreamingGameMusic(bool bVal) ;
virtual bool GetIsPlayingMenuMusic();
virtual bool GetIsPlayingEndMusic() ; virtual bool GetIsPlayingEndMusic() ;
virtual bool GetIsPlayingNetherMusic() ; virtual bool GetIsPlayingNetherMusic() ;
virtual void SetIsPlayingEndMusic(bool bVal) ; virtual void SetIsPlayingEndMusic(bool bVal) ;
virtual void SetIsPlayingMenuMusic(bool bVal);
virtual void SetIsPlayingNetherMusic(bool bVal) ; virtual void SetIsPlayingNetherMusic(bool bVal) ;
static const WCHAR *wchSoundNames[eSoundType_MAX]; static const WCHAR *wchSoundNames[eSoundType_MAX];
static const WCHAR *wchUISoundNames[eSFX_MAX]; static const WCHAR *wchUISoundNames[eSFX_MAX];
@ -94,6 +96,7 @@ private:
bool m_bIsPlayingStreamingCDMusic; bool m_bIsPlayingStreamingCDMusic;
bool m_bIsPlayingStreamingGameMusic; bool m_bIsPlayingStreamingGameMusic;
bool m_bIsPlayingMenuMusic;
bool m_bIsPlayingEndMusic; bool m_bIsPlayingEndMusic;
bool m_bIsPlayingNetherMusic; bool m_bIsPlayingNetherMusic;
}; };

View file

@ -0,0 +1,97 @@
#include "stdafx.h"
#include "MusicTrackManager.h"
#include "../Minecraft.World/Random.h"
MusicTrackManager::MusicTrackManager(Random* rng)
: m_random(rng)
{
// The domains will be filled later via setDomainRange().
}
MusicTrackManager::~MusicTrackManager()
{
// unordered_map will automatically destroy its DomainInfo objects,
// which in turn delete[] the heard arrays.
}
void MusicTrackManager::setDomainRange(Domain domain, int minIdx, int maxIdx)
{
assert(minIdx <= maxIdx);
// Use emplace to construct the DomainInfo in-place.
// If the domain already exists, this will replace it (C++17).
m_domains.erase(domain); // Remove old entry if any
m_domains.emplace(domain, DomainInfo(minIdx, maxIdx));
}
int MusicTrackManager::selectTrack(Domain domain)
{
// Special case: if domain is None, return -1 (no track).
if (domain == Domain::None)
return -1;
DomainInfo& info = getInfo(domain);
// If range contains only one track, just return that track.
if (info.trackCount == 1)
return info.minIdx;
// Check whether all tracks have been heard.
bool allHeard = true;
for (int i = 0; i < info.trackCount; ++i)
{
if (!info.heard[i])
{
allHeard = false;
break;
}
}
// If all tracks have been heard, reset the heard flags.
if (allHeard)
{
std::memset(info.heard, 0, sizeof(bool) * info.trackCount);
}
// Try up to (trackCount/2 + 1) times to pick a track that hasn't been heard.
// This biases toward unplayed tracks but doesn't guarantee it if the random
// keeps hitting played ones. It's a compromise between fairness and performance.
const int maxAttempts = info.trackCount / 2 + 1;
for (int attempt = 0; attempt < maxAttempts; ++attempt)
{
int idx = m_random->nextInt(info.trackCount); // 0 .. trackCount-1
if (!info.heard[idx])
{
info.heard[idx] = true;
return info.minIdx + idx;
}
}
// Fallback: if we couldn't find an unplayed track (should be rare),
// just pick any random track and mark it as heard.
int fallbackIdx = m_random->nextInt(info.trackCount);
info.heard[fallbackIdx] = true;
return info.minIdx + fallbackIdx;
}
void MusicTrackManager::resetDomain(Domain domain)
{
DomainInfo& info = getInfo(domain);
std::memset(info.heard, 0, sizeof(bool) * info.trackCount);
}
// ---------- private helpers ----------
MusicTrackManager::DomainInfo& MusicTrackManager::getInfo(Domain domain)
{
auto it = m_domains.find(domain);
assert(it != m_domains.end() && "Domain not initialized. Call setDomainRange first.");
return it->second;
}
const MusicTrackManager::DomainInfo& MusicTrackManager::getInfo(Domain domain) const
{
auto it = m_domains.find(domain);
assert(it != m_domains.end() && "Domain not initialized.");
return it->second;
}

View file

@ -0,0 +1,117 @@
#pragma once
#include <unordered_map>
#include <cstring> // for memset
#include <cassert>
// Forward declaration of the random number generator used by SoundEngine.
// Replace with your actual random class if different.
class Random;
/**
* Manages selection of music tracks across different gameplay domains
* (Menu, Overworld Survival, Overworld Creative, Nether, End).
* Each domain maintains its own range of track indices and a history
* of recently played tracks to avoid immediate repetition.
*/
class MusicTrackManager
{
public:
// Enumeration of all music domains. The values match the original
// SoundEngine constants for easy conversion.
enum class Domain
{
Menu = -1,
OverworldSurvival = 0,
OverworldCreative = 1,
Nether = 2,
End = 3,
None = 4 // Special value for when no background music should play (e.g., during a music disc)
};
/**
* Constructor.
* @param rng Pointer to a random number generator (must remain valid).
*/
explicit MusicTrackManager(Random* rng);
/**
* Destructor frees all dynamically allocated heard-track arrays.
*/
~MusicTrackManager();
// Prevent copying (to avoid double deletion of internal arrays).
MusicTrackManager(const MusicTrackManager&) = delete;
MusicTrackManager& operator=(const MusicTrackManager&) = delete;
/**
* Sets the inclusive index range for a given domain.
* @param domain The music domain.
* @param minIdx First valid track index.
* @param maxIdx Last valid track index (must be >= minIdx).
*/
void setDomainRange(Domain domain, int minIdx, int maxIdx);
/**
* Selects a track index for the specified domain.
* Tries to return a track that has not been played recently;
* if all tracks have been played, resets the history and picks one randomly.
* For domains with only one track, that track is always returned.
* @param domain The domain for which to choose a track.
* @return A valid track index within the domain's range.
*/
int selectTrack(Domain domain);
/**
* Resets the heard history for a domain, marking all tracks as "not heard".
* Useful when switching domains or after a major game state change.
* @param domain The domain to reset.
*/
void resetDomain(Domain domain);
int getDomainMin(Domain d) const { return getInfo(d).minIdx; }
int getDomainMax(Domain d) const { return getInfo(d).maxIdx; }
private:
// Internal structure storing range and heard-array for a domain.
struct DomainInfo
{
int minIdx; // First track index (inclusive)
int maxIdx; // Last track index (inclusive)
int trackCount; // Number of tracks in this domain
bool* heard; // Dynamic array: heard[i] == true if track (minIdx + i) has been played recently
DomainInfo(int minVal = 0, int maxVal = 0)
: minIdx(minVal), maxIdx(maxVal), trackCount(maxVal - minVal + 1)
{
heard = new bool[trackCount](); // value-initialized to false
}
// Move constructor (optional, but needed if we want to store in unordered_map with emplace)
DomainInfo(DomainInfo&& other) noexcept
: minIdx(other.minIdx), maxIdx(other.maxIdx), trackCount(other.trackCount), heard(other.heard)
{
other.heard = nullptr; // prevent double deletion
}
// Destructor
~DomainInfo()
{
delete[] heard;
}
// No copy
DomainInfo(const DomainInfo&) = delete;
DomainInfo& operator=(const DomainInfo&) = delete;
};
// Map from Domain to its info.
std::unordered_map<Domain, DomainInfo> m_domains;
// Pointer to the random number generator.
Random* m_random;
// Helper to get the info for a domain (assumes domain exists).
DomainInfo& getInfo(Domain domain);
const DomainInfo& getInfo(Domain domain) const;
};

File diff suppressed because it is too large Load diff

View file

@ -3,6 +3,7 @@ class Mob;
class Options; class Options;
using namespace std; using namespace std;
#include "..\..\Minecraft.World\SoundTypes.h" #include "..\..\Minecraft.World\SoundTypes.h"
#include "MusicTrackManager.h"
#include "miniaudio.h" #include "miniaudio.h"
@ -17,6 +18,10 @@ enum eMUSICFILES
eStream_Overworld_hal4, eStream_Overworld_hal4,
eStream_Overworld_nuance1, eStream_Overworld_nuance1,
eStream_Overworld_nuance2, eStream_Overworld_nuance2,
eStream_Overworld_piano1,
eStream_Overworld_piano2,
eStream_Overworld_piano3, // <-- make piano3 the last overworld one
#ifndef _XBOX #ifndef _XBOX
// Add the new music tracks // Add the new music tracks
eStream_Overworld_Creative1, eStream_Overworld_Creative1,
@ -25,14 +30,12 @@ enum eMUSICFILES
eStream_Overworld_Creative4, eStream_Overworld_Creative4,
eStream_Overworld_Creative5, eStream_Overworld_Creative5,
eStream_Overworld_Creative6, eStream_Overworld_Creative6,
eStream_Overworld_Menu1, eStream_Overworld_Menu1,
eStream_Overworld_Menu2, eStream_Overworld_Menu2,
eStream_Overworld_Menu3, eStream_Overworld_Menu3,
eStream_Overworld_Menu4, eStream_Overworld_Menu4,
#endif #endif
eStream_Overworld_piano1,
eStream_Overworld_piano2,
eStream_Overworld_piano3, // <-- make piano3 the last overworld one
// Nether // Nether
eStream_Nether1, eStream_Nether1,
eStream_Nether2, eStream_Nether2,
@ -106,6 +109,7 @@ extern std::vector<MiniAudioSound*> m_activeSounds;
class SoundEngine : public ConsoleSoundEngine class SoundEngine : public ConsoleSoundEngine
{ {
static const int MAX_SAME_SOUNDS_PLAYING = 8; // 4J added static const int MAX_SAME_SOUNDS_PLAYING = 8; // 4J added
public: public:
SoundEngine(); SoundEngine();
void destroy() override; void destroy() override;
@ -125,10 +129,11 @@ public:
void addMusic(const wstring& name, File *file) override; void addMusic(const wstring& name, File *file) override;
void addStreaming(const wstring& name, File *file) override; void addStreaming(const wstring& name, File *file) override;
char *ConvertSoundPathToName(const wstring& name, bool bConvertSpaces=false) override; char *ConvertSoundPathToName(const wstring& name, bool bConvertSpaces=false) override;
bool isStreamingWavebankReady(); // 4J Added bool isStreamingWavebankReady();
int getMusicID(int iDomain); MusicTrackManager::Domain determineCurrentMusicDomain() const;
int getTrackForDomain(MusicTrackManager::Domain domain);
int getMusicID(const wstring& name); int getMusicID(const wstring& name);
void SetStreamingSounds(int iOverworldMin, int iOverWorldMax, int iNetherMin, int iNetherMax, int iEndMin, int iEndMax, int iCD1); void SetStreamingSounds(int iMenuMin, int iMenuMax, int iOverworldSurvivalMin, int iOverWorldSurvivalMax, int iOverworldCreativeMin, int iOverWorldCreativeMax, int iNetherMin, int iNetherMax, int iEndMin, int iEndMax, int iCD1);
void updateMiniAudio(); void updateMiniAudio();
void playMusicUpdate(); void playMusicUpdate();
@ -141,8 +146,10 @@ private:
int initAudioHardware(int iMinSpeakers) override int initAudioHardware(int iMinSpeakers) override
{ return iMinSpeakers;} { return iMinSpeakers;}
#endif #endif
int GetRandomishTrack(int iStart,int iEnd); Random* random;
MusicTrackManager m_musicTrackManager;
MusicTrackManager::Domain m_currentMusicDomain;
ma_engine m_engine; ma_engine m_engine;
ma_engine_config m_engineConfig; ma_engine_config m_engineConfig;
@ -157,8 +164,6 @@ private:
AUDIO_LISTENER m_ListenerA[MAX_LOCAL_PLAYERS]; AUDIO_LISTENER m_ListenerA[MAX_LOCAL_PLAYERS];
int m_validListenerCount; int m_validListenerCount;
Random *random;
int m_musicID; int m_musicID;
int m_iMusicDelay; int m_iMusicDelay;
int m_StreamState; int m_StreamState;
@ -174,12 +179,7 @@ private:
char m_szStreamName[255]; char m_szStreamName[255];
int CurrentSoundsPlaying[eSoundType_MAX+eSFX_MAX]; int CurrentSoundsPlaying[eSoundType_MAX+eSFX_MAX];
// streaming music files - will be different for mash-up packs
int m_iStream_Overworld_Min,m_iStream_Overworld_Max;
int m_iStream_Nether_Min,m_iStream_Nether_Max;
int m_iStream_End_Min,m_iStream_End_Max;
int m_iStream_CD_1; int m_iStream_CD_1;
bool *m_bHeardTrackA;
#ifdef __ORBIS__ #ifdef __ORBIS__
int32_t m_hBGMAudio; int32_t m_hBGMAudio;

View file

@ -3754,10 +3754,12 @@ void CMinecraftApp::HandleXuiActions(void)
// need to stop the streaming audio - by playing streaming audio from the default texture pack now // need to stop the streaming audio - by playing streaming audio from the default texture pack now
// reset the streaming sounds back to the normal ones // reset the streaming sounds back to the normal ones
#ifndef _XBOX #ifndef _XBOX
pMinecraft->soundEngine->SetStreamingSounds(eStream_Overworld_Calm1,eStream_Overworld_piano3, pMinecraft->soundEngine->SetStreamingSounds(eStream_Overworld_Menu1, eStream_Overworld_Menu4,
eStream_Nether1,eStream_Nether4, eStream_Overworld_Calm1, eStream_Overworld_piano3,
eStream_end_dragon,eStream_end_end, eStream_Overworld_Creative1, eStream_Overworld_Creative6,
eStream_CD_1); eStream_Nether1, eStream_Nether4,
eStream_end_dragon, eStream_end_end,
eStream_CD_1);
#endif #endif
pMinecraft->soundEngine->playStreaming(L"", 0, 0, 0, 1, 1); pMinecraft->soundEngine->playStreaming(L"", 0, 0, 0, 1, 1);

View file

@ -2064,10 +2064,12 @@ void UIController::NavigateToHomeMenu()
{ {
// need to stop the streaming audio - by playing streaming audio from the default texture pack now // need to stop the streaming audio - by playing streaming audio from the default texture pack now
// reset the streaming sounds back to the normal ones // reset the streaming sounds back to the normal ones
pMinecraft->soundEngine->SetStreamingSounds(eStream_Overworld_Calm1,eStream_Overworld_piano3, pMinecraft->soundEngine->SetStreamingSounds(eStream_Overworld_Menu1, eStream_Overworld_Menu4,
eStream_Nether1,eStream_Nether4, eStream_Overworld_Calm1, eStream_Overworld_piano3,
eStream_end_dragon,eStream_end_end, eStream_Overworld_Creative1, eStream_Overworld_Creative6,
eStream_CD_1); eStream_Nether1, eStream_Nether4,
eStream_end_dragon, eStream_end_end,
eStream_CD_1);
pMinecraft->soundEngine->playStreaming(L"", 0, 0, 0, 1, 1); pMinecraft->soundEngine->playStreaming(L"", 0, 0, 0, 1, 1);
// if(pDLCTexPack->m_pStreamedWaveBank!=nullptr) // if(pDLCTexPack->m_pStreamedWaveBank!=nullptr)

View file

@ -483,7 +483,7 @@ int DLCTexturePack::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD dwLicen
iEndStart=iOverworldC+iNetherC; iEndStart=iOverworldC+iNetherC;
iEndC=dlcFile->GetCountofType(DLCAudioFile::e_AudioType_End); iEndC=dlcFile->GetCountofType(DLCAudioFile::e_AudioType_End);
Minecraft::GetInstance()->soundEngine->SetStreamingSounds(iOverworldStart,iOverworldStart+iOverworldC-1, Minecraft::GetInstance()->soundEngine->SetStreamingSounds(iOverworldStart,iOverworldStart+iOverworldC-1, iOverworldStart, iOverworldStart + iOverworldC - 1, iOverworldStart, iOverworldStart + iOverworldC - 1,
iNetherStart,iNetherStart+iNetherC-1,iEndStart,iEndStart+iEndC-1,iEndStart+iEndC); // push the CD start to after iNetherStart,iNetherStart+iNetherC-1,iEndStart,iEndStart+iEndC-1,iEndStart+iEndC); // push the CD start to after
} }
#endif #endif