Skip to content

Commit

Permalink
Fix that intersecting music zones could cause issues
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Sep 16, 2018
1 parent 69574b0 commit e820843
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/audio/AudioWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,25 @@ namespace World
m_musicContext->playSegment(m_Segments.at(loweredName), timing);
m_playingSegment = loweredName;
}
m_CurrentMusicTheme = "";
return true;
}

bool AudioWorld::playMusicTheme(const std::string& name)
{
if (m_MusicVM->getDATFile().hasSymbolName(Utils::uppered(name)))
auto uname = Utils::uppered(name);
if (m_CurrentMusicTheme == uname) return true;

if (m_MusicVM->getDATFile().hasSymbolName(uname))
{
size_t i = m_MusicVM->getDATFile().getSymbolIndexByName(Utils::uppered(name));
size_t i = m_MusicVM->getDATFile().getSymbolIndexByName(uname);
Daedalus::GameState::MusicThemeHandle h = m_MusicVM->getGameState().createMusicTheme();
Daedalus::GEngineClasses::C_MusicTheme& mt = m_MusicVM->getGameState().getMusicTheme(h);
m_MusicVM->initializeInstance(ZMemory::toBigHandle(h), i, Daedalus::IC_MusicTheme);

return playSegment(mt.file, getTiming(mt.transSubType));
bool result = playSegment(mt.file, getTiming(mt.transSubType));
if (result) m_CurrentMusicTheme = uname;
return result;
}
return false;
}
Expand Down
21 changes: 16 additions & 5 deletions src/audio/AudioWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

#include <glm/glm.hpp>

#include <dmusic/PlayingContext.h>
#include <audio/AudioEngine.h>
#include <daedalus/DaedalusStdlib.h>
#include <dmusic/PlayingContext.h>
#include <handle/HandleDef.h>
#include <memory/Config.h>
#include <utils/Utils.h>
#include <vdfs/fileIndex.h>
#include <audio/AudioEngine.h>

typedef struct ALCcontext_struct ALCcontext;

Expand All @@ -34,7 +34,7 @@ namespace Daedalus
{
struct C_SFX;
}
}
} // namespace Daedalus

namespace Logic
{
Expand Down Expand Up @@ -88,6 +88,12 @@ namespace World
*/
bool playMusicTheme(const std::string& name);

/**
* Returns the currently playing music theme, or an
* empty string if no music themes are playing
*/
const std::string& currentMusicTheme() const { return m_CurrentMusicTheme; }

/**
* Returns the names of the currently loaded segments
*/
Expand Down Expand Up @@ -116,7 +122,7 @@ namespace World
*/
const VDFS::FileIndex& m_VDFSIndex;

Daedalus::DaedalusVM* m_SoundVM = nullptr, *m_MusicVM = nullptr;
Daedalus::DaedalusVM *m_SoundVM = nullptr, *m_MusicVM = nullptr;

struct Sound : public Handle::HandleTypeDescriptor<Handle::SfxHandle>
{
Expand Down Expand Up @@ -188,5 +194,10 @@ namespace World
* Contains all loaded sounds by name
*/
std::map<std::string, Handle::SfxHandle> m_SoundMap;

/**
* The currently playing music theme
*/
std::string m_CurrentMusicTheme;
};
}
} // namespace World
6 changes: 4 additions & 2 deletions src/logic/MusicController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void MusicController::initFromVobDescriptor(const ZenLoad::zCVobData& vob)
m_bbox[1] = vob.bbox[1] * 0.01;

m_zoneName = vob.vobName;
m_instancePrefix = vob.vobName.substr(vob.vobName.find('_') + 1);
m_instancePrefix = Utils::uppered(vob.vobName.substr(vob.vobName.find('_') + 1));

LogInfo() << "Music: controller created for " << m_zoneName;
}
Expand Down Expand Up @@ -84,14 +84,16 @@ void MusicController::onUpdate(float deltaTime)
}

EMusicTime time = m_World.getEngine()->getGameClock().isDaytime() ? MT_Day : MT_Ngt;
const auto& audioWorld = m_World.getEngine()->getAudioWorld();
const auto& currentTheme = audioWorld.currentMusicTheme();

// We need to play a new segment in either one of two scenarios:
// if the character has just entered the zone or
// if the character was already in the zone but the time of day
// changed.
// FIXME: It'll also be needed to check if the character is
// threatened or fighting to play the correct music.
if ((!m_isPlaying && isInBoundingBox())
if (((!m_isPlaying || currentTheme.find(m_instancePrefix) != 0) && isInBoundingBox())
|| (m_isPlaying && m_currentTime != time))
{
m_isPlaying = true;
Expand Down

0 comments on commit e820843

Please sign in to comment.