Skip to content

Commit 84b22ce

Browse files
committed
Move MusicZoneManager, enable (de)serialization for savegames
1 parent 0285540 commit 84b22ce

7 files changed

Lines changed: 102 additions & 12 deletions

File tree

src/engine/GameSession.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ std::unique_ptr<World::WorldInstance> GameSession::createWorld(const std::string
8080
const json& worldJson,
8181
const json& scriptEngine,
8282
const json& dialogManager,
83-
const json& logManager)
83+
const json& logManager,
84+
const json& musicManager)
8485
{
8586
std::string worldFile = _worldFile;
8687

@@ -102,7 +103,7 @@ std::unique_ptr<World::WorldInstance> GameSession::createWorld(const std::string
102103
return nullptr;
103104
}
104105
}
105-
if (!world.init(worldFile, worldJson, scriptEngine, dialogManager, logManager)) // expensive operation
106+
if (!world.init(worldFile, worldJson, scriptEngine, dialogManager, logManager, musicManager)) // expensive operation
106107
{
107108
LogError() << "Failed to init world file: " << worldFile;
108109
return nullptr;

src/engine/GameSession.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ namespace Engine
8282
const json& worldJson = json(),
8383
const json& scriptEngine = json(),
8484
const json& dialogManager = json(),
85-
const json& logManager = json());
85+
const json& logManager = json(),
86+
const json& musicManager = json());
8687

8788
Handle::WorldHandle registerWorld(std::unique_ptr<World::WorldInstance> pWorldInstance);
8889

src/engine/MusicZoneManager.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <logic/ScriptEngine.h>
88

99
using namespace Engine;
10+
using namespace nlohmann;
1011

1112
static const std::array<const std::string, 6> instanceSuffixes =
1213
{
@@ -69,7 +70,8 @@ void MusicZoneManager::onUpdate()
6970
if (isInside(playerPos, zone.bbox[0] * .01f, zone.bbox[1] * .01f))
7071
{
7172
if (!currentZone
72-
|| currentZone->oCZoneMusic.priority < zone.oCZoneMusic.priority)
73+
|| (currentZone->oCZoneMusic.priority < zone.oCZoneMusic.priority
74+
&& zone.oCZoneMusic.enabled))
7375
{
7476
currentZone = &zone;
7577
}
@@ -93,4 +95,65 @@ void MusicZoneManager::onUpdate()
9395
// Try playing the zone's theme
9496
playTheme(instancePrefix, MT_Std, time);
9597
}
98+
}
99+
100+
static json serializeVector(const ZMath::float3& vec)
101+
{
102+
json result;
103+
result["x"] = vec.x;
104+
result["y"] = vec.y;
105+
result["z"] = vec.z;
106+
return result;
107+
}
108+
109+
void MusicZoneManager::exportMusicZoneManager(json& result)
110+
{
111+
result["defaultZone"] = m_defaultZonePrefix;
112+
for(const auto& zone : m_zones)
113+
{
114+
json z;
115+
z["name"] = zone.vobName;
116+
z["ellipsoid"] = zone.oCZoneMusic.ellipsoid;
117+
z["enabled"] = zone.oCZoneMusic.enabled;
118+
z["loop"] = zone.oCZoneMusic.loop;
119+
z["priority"] = zone.oCZoneMusic.priority;
120+
z["reverbLevel"] = zone.oCZoneMusic.reverbLevel;
121+
z["volumeLevel"] = zone.oCZoneMusic.volumeLevel;
122+
123+
z["bbox"] = json::array({
124+
serializeVector(zone.bbox[0]),
125+
serializeVector(zone.bbox[1])
126+
});
127+
128+
result["zones"].push_back(z);
129+
}
130+
}
131+
132+
static ZMath::float3 deserializeVector(const json& j)
133+
{
134+
return {j["x"].get<float>(), j["y"].get<float>(), j["z"].get<float>()};
135+
}
136+
137+
void MusicZoneManager::importMusicZoneManager(const nlohmann::json& json)
138+
{
139+
m_zones.clear();
140+
m_defaultZonePrefix = json["defaultZone"].get<std::string>();
141+
142+
for(const auto& zone : json["zones"])
143+
{
144+
ZenLoad::zCVobData vob;
145+
vob.vobType = ZenLoad::zCVobData::VT_oCZoneMusic;
146+
vob.vobName = zone["name"].get<std::string>();
147+
vob.oCZoneMusic.ellipsoid = zone["ellipsoid"].get<bool>();
148+
vob.oCZoneMusic.enabled = zone["enabled"].get<bool>();
149+
vob.oCZoneMusic.loop = zone["loop"].get<bool>();
150+
vob.oCZoneMusic.priority = zone["priority"].get<int>();
151+
vob.oCZoneMusic.reverbLevel = zone["reverbLevel"].get<float>();
152+
vob.oCZoneMusic.volumeLevel = zone["volumeLevel"].get<float>();
153+
154+
vob.bbox[0] = deserializeVector(zone["bbox"][0]);
155+
vob.bbox[1] = deserializeVector(zone["bbox"][1]);
156+
157+
m_zones.emplace_back(vob);
158+
}
96159
}

src/engine/MusicZoneManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <vector>
4+
#include <json.hpp>
45

56
#include <engine/World.h>
67

@@ -26,6 +27,9 @@ namespace Engine
2627
void setDefaultZone(const std::string& zone) { m_defaultZonePrefix = zone; }
2728
void onUpdate();
2829

30+
void exportMusicZoneManager(nlohmann::json& json);
31+
void importMusicZoneManager(const nlohmann::json& json);
32+
2933
private:
3034
World::WorldInstance& m_world;
3135
std::vector<ZenLoad::zCVobData> m_zones;

src/engine/World.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class WorldInstance::ClassContents
5050
, dialogManager(world)
5151
, bspTree(world)
5252
, pfxManager(world)
53+
, musicZoneManager(world)
5354
{}
5455

5556
WorldMesh worldMesh;
@@ -61,6 +62,7 @@ class WorldInstance::ClassContents
6162
Content::Sky sky;
6263
Logic::DialogManager dialogManager;
6364
Logic::PfxManager pfxManager;
65+
Engine::MusicZoneManager musicZoneManager;
6466
};
6567

6668
struct LoadSection
@@ -81,7 +83,6 @@ WorldInstance::WorldInstance(Engine::BaseEngine& engine)
8183
: m_pEngine(&engine)
8284
, m_Allocators(std::make_unique<WorldAllocators>(engine))
8385
, m_ClassContents(std::make_unique<ClassContents>(*this))
84-
, m_MusicZoneManager(std::make_unique<Engine::MusicZoneManager>(*this))
8586
{
8687
Logic::MusicController::disableDebugDraw();
8788
}
@@ -135,7 +136,8 @@ bool WorldInstance::init(const std::string& zen,
135136
const json& worldJson,
136137
const json& scriptEngine,
137138
const json& dialogManager,
138-
const json& logManager)
139+
const json& logManager,
140+
const json& musicManager)
139141
{
140142
m_ZenFile = zen;
141143
Engine::BaseEngine& engine = *m_pEngine;
@@ -378,7 +380,7 @@ bool WorldInstance::init(const std::string& zen,
378380

379381
VobTypes::MusicVobInformation mus = VobTypes::asMusicVob(*this, e);
380382
mus.musicController->initFromVobDescriptor(v);
381-
m_MusicZoneManager->addZone(v);
383+
m_ClassContents->musicZoneManager.addZone(v);
382384

383385
/* Sets an increased factor to allow detection of very large
384386
music zones. For example, Khorinis's zone would be disabled
@@ -394,7 +396,7 @@ bool WorldInstance::init(const std::string& zen,
394396
else if (v.objectClass == "oCZoneMusicDefault:oCZoneMusic:zCVob")
395397
{
396398
std::string zoneName = v.vobName.substr(v.vobName.find('_') + 1);
397-
m_MusicZoneManager->setDefaultZone(zoneName);
399+
m_ClassContents->musicZoneManager.setDefaultZone(zoneName);
398400

399401
LogInfo() << "Found default music zone: " << v.vobName;
400402
}
@@ -561,6 +563,11 @@ bool WorldInstance::init(const std::string& zen,
561563
engine.getSession().getLogManager().importLogManager(logManager);
562564
}
563565

566+
if(!musicManager.empty())
567+
{
568+
m_ClassContents->musicZoneManager.importMusicZoneManager(musicManager);
569+
}
570+
564571
m_pEngine->getHud().getLoadingScreen().setSectionProgress(40);
565572

566573
LogInfo() << "Running startup-scripts";
@@ -749,7 +756,7 @@ void WorldInstance::onFrameUpdate(double deltaTime, float updateRangeSquared, co
749756
ddDrawAxis(fpPosition.x, fpPosition.y, fpPosition.z, 0.5f);
750757
}*/
751758

752-
m_MusicZoneManager->onUpdate();
759+
m_ClassContents->musicZoneManager.onUpdate();
753760
}
754761

755762
void WorldInstance::removeEntity(Handle::EntityHandle h)
@@ -1209,3 +1216,8 @@ Logic::CameraController* WorldInstance::getCameraController()
12091216
Logic::Controller* ptr = getCameraComp<Components::LogicComponent>().m_pLogicController;
12101217
return dynamic_cast<Logic::CameraController*>(ptr);
12111218
}
1219+
1220+
Engine::MusicZoneManager& WorldInstance::getMusicZoneManager()
1221+
{
1222+
return m_ClassContents->musicZoneManager;
1223+
}

src/engine/World.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ namespace World
9999
const json& worldJson = json(),
100100
const json& scriptEngine = json(),
101101
const json& dialogManager = json(),
102-
const json& logManager = json());
102+
const json& logManager = json(),
103+
const json& musicManager = json());
103104

104105
/**
105106
* Creates an entity with the given components and returns its handle
@@ -219,6 +220,7 @@ namespace World
219220
Logic::DialogManager& getDialogManager();
220221
Logic::PfxManager& getPfxManager();
221222
Animations::AnimationLibrary& getAnimationLibrary();
223+
Engine::MusicZoneManager& getMusicZoneManager();
222224

223225
/**
224226
* HUD's print-screen manager
@@ -412,6 +414,5 @@ namespace World
412414
*/
413415
std::unique_ptr<WorldAllocators> m_Allocators;
414416
std::unique_ptr<ClassContents> m_ClassContents;
415-
std::unique_ptr<Engine::MusicZoneManager> m_MusicZoneManager;
416417
};
417418
}

src/logic/SavegameManager.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "SavegameManager.h"
22
#include <fstream>
33
#include "engine/GameEngine.h"
4+
#include "engine/MusicZoneManager.h"
45
#include "ui/Hud.h"
56
#include "ui/LoadingScreen.h"
67
#include <json/json.hpp>
@@ -244,11 +245,13 @@ std::string Engine::SavegameManager::loadSaveGameSlot(int index)
244245
json scriptEngine = json::parse(SavegameManager::readFileInSlot(index, "scriptengine.json"));
245246
json dialogManager = json::parse(SavegameManager::readFileInSlot(index, "dialogmanager.json"));
246247
json logManager = json::parse(SavegameManager::readFileInSlot(index, "logmanager.json"));
248+
json musicManager = json::parse(SavegameManager::readFileInSlot(index, "musicmanager.json"));
247249
engine->getSession().setCurrentSlot(index);
248250
engine->getGameClock().setTotalSeconds(timePlayed);
249251
using UniqueWorld = std::unique_ptr<World::WorldInstance>;
250252
std::shared_ptr<UniqueWorld> pWorld;
251-
pWorld = std::make_shared<UniqueWorld>(engine->getSession().createWorld("", worldJson, scriptEngine, dialogManager, logManager));
253+
pWorld = std::make_shared<UniqueWorld>(engine->getSession().createWorld("",
254+
worldJson, scriptEngine, dialogManager, logManager, musicManager));
252255

253256
auto registerWorld = [index, pWorld](BaseEngine * engine)
254257
{
@@ -338,6 +341,11 @@ void Engine::SavegameManager::saveToSlot(int index, std::string savegameName)
338341
json scriptEngine;
339342
mainWorld.getScriptEngine().exportScriptEngine(scriptEngine);
340343
Engine::SavegameManager::writeFileInSlot(index, "scriptengine.json", Utils::iso_8859_1_to_utf8(scriptEngine.dump(4)));
344+
345+
json musicManager;
346+
mainWorld.getMusicZoneManager().exportMusicZoneManager(musicManager);
347+
Engine::SavegameManager::writeFileInSlot(index, "musicmanager.json", Utils::iso_8859_1_to_utf8(musicManager.dump(4)));
348+
341349
gameEngine->getSession().setCurrentSlot(index);
342350
}
343351

0 commit comments

Comments
 (0)