Skip to content

Commit

Permalink
Move MusicZoneManager, enable (de)serialization for savegames
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Sep 22, 2018
1 parent 0285540 commit 84b22ce
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/engine/GameSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ std::unique_ptr<World::WorldInstance> GameSession::createWorld(const std::string
const json& worldJson,
const json& scriptEngine,
const json& dialogManager,
const json& logManager)
const json& logManager,
const json& musicManager)
{
std::string worldFile = _worldFile;

Expand All @@ -102,7 +103,7 @@ std::unique_ptr<World::WorldInstance> GameSession::createWorld(const std::string
return nullptr;
}
}
if (!world.init(worldFile, worldJson, scriptEngine, dialogManager, logManager)) // expensive operation
if (!world.init(worldFile, worldJson, scriptEngine, dialogManager, logManager, musicManager)) // expensive operation
{
LogError() << "Failed to init world file: " << worldFile;
return nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/engine/GameSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ namespace Engine
const json& worldJson = json(),
const json& scriptEngine = json(),
const json& dialogManager = json(),
const json& logManager = json());
const json& logManager = json(),
const json& musicManager = json());

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

Expand Down
65 changes: 64 additions & 1 deletion src/engine/MusicZoneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <logic/ScriptEngine.h>

using namespace Engine;
using namespace nlohmann;

static const std::array<const std::string, 6> instanceSuffixes =
{
Expand Down Expand Up @@ -69,7 +70,8 @@ void MusicZoneManager::onUpdate()
if (isInside(playerPos, zone.bbox[0] * .01f, zone.bbox[1] * .01f))
{
if (!currentZone
|| currentZone->oCZoneMusic.priority < zone.oCZoneMusic.priority)
|| (currentZone->oCZoneMusic.priority < zone.oCZoneMusic.priority
&& zone.oCZoneMusic.enabled))
{
currentZone = &zone;
}
Expand All @@ -93,4 +95,65 @@ void MusicZoneManager::onUpdate()
// Try playing the zone's theme
playTheme(instancePrefix, MT_Std, time);
}
}

static json serializeVector(const ZMath::float3& vec)
{
json result;
result["x"] = vec.x;
result["y"] = vec.y;
result["z"] = vec.z;
return result;
}

void MusicZoneManager::exportMusicZoneManager(json& result)
{
result["defaultZone"] = m_defaultZonePrefix;
for(const auto& zone : m_zones)
{
json z;
z["name"] = zone.vobName;
z["ellipsoid"] = zone.oCZoneMusic.ellipsoid;
z["enabled"] = zone.oCZoneMusic.enabled;
z["loop"] = zone.oCZoneMusic.loop;
z["priority"] = zone.oCZoneMusic.priority;
z["reverbLevel"] = zone.oCZoneMusic.reverbLevel;
z["volumeLevel"] = zone.oCZoneMusic.volumeLevel;

z["bbox"] = json::array({
serializeVector(zone.bbox[0]),
serializeVector(zone.bbox[1])
});

result["zones"].push_back(z);
}
}

static ZMath::float3 deserializeVector(const json& j)
{
return {j["x"].get<float>(), j["y"].get<float>(), j["z"].get<float>()};
}

void MusicZoneManager::importMusicZoneManager(const nlohmann::json& json)
{
m_zones.clear();
m_defaultZonePrefix = json["defaultZone"].get<std::string>();

for(const auto& zone : json["zones"])
{
ZenLoad::zCVobData vob;
vob.vobType = ZenLoad::zCVobData::VT_oCZoneMusic;
vob.vobName = zone["name"].get<std::string>();
vob.oCZoneMusic.ellipsoid = zone["ellipsoid"].get<bool>();
vob.oCZoneMusic.enabled = zone["enabled"].get<bool>();
vob.oCZoneMusic.loop = zone["loop"].get<bool>();
vob.oCZoneMusic.priority = zone["priority"].get<int>();
vob.oCZoneMusic.reverbLevel = zone["reverbLevel"].get<float>();
vob.oCZoneMusic.volumeLevel = zone["volumeLevel"].get<float>();

vob.bbox[0] = deserializeVector(zone["bbox"][0]);
vob.bbox[1] = deserializeVector(zone["bbox"][1]);

m_zones.emplace_back(vob);
}
}
4 changes: 4 additions & 0 deletions src/engine/MusicZoneManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <json.hpp>

#include <engine/World.h>

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

void exportMusicZoneManager(nlohmann::json& json);
void importMusicZoneManager(const nlohmann::json& json);

private:
World::WorldInstance& m_world;
std::vector<ZenLoad::zCVobData> m_zones;
Expand Down
22 changes: 17 additions & 5 deletions src/engine/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WorldInstance::ClassContents
, dialogManager(world)
, bspTree(world)
, pfxManager(world)
, musicZoneManager(world)
{}

WorldMesh worldMesh;
Expand All @@ -61,6 +62,7 @@ class WorldInstance::ClassContents
Content::Sky sky;
Logic::DialogManager dialogManager;
Logic::PfxManager pfxManager;
Engine::MusicZoneManager musicZoneManager;
};

struct LoadSection
Expand All @@ -81,7 +83,6 @@ WorldInstance::WorldInstance(Engine::BaseEngine& engine)
: m_pEngine(&engine)
, m_Allocators(std::make_unique<WorldAllocators>(engine))
, m_ClassContents(std::make_unique<ClassContents>(*this))
, m_MusicZoneManager(std::make_unique<Engine::MusicZoneManager>(*this))
{
Logic::MusicController::disableDebugDraw();
}
Expand Down Expand Up @@ -135,7 +136,8 @@ bool WorldInstance::init(const std::string& zen,
const json& worldJson,
const json& scriptEngine,
const json& dialogManager,
const json& logManager)
const json& logManager,
const json& musicManager)
{
m_ZenFile = zen;
Engine::BaseEngine& engine = *m_pEngine;
Expand Down Expand Up @@ -378,7 +380,7 @@ bool WorldInstance::init(const std::string& zen,

VobTypes::MusicVobInformation mus = VobTypes::asMusicVob(*this, e);
mus.musicController->initFromVobDescriptor(v);
m_MusicZoneManager->addZone(v);
m_ClassContents->musicZoneManager.addZone(v);

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

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

if(!musicManager.empty())
{
m_ClassContents->musicZoneManager.importMusicZoneManager(musicManager);
}

m_pEngine->getHud().getLoadingScreen().setSectionProgress(40);

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

m_MusicZoneManager->onUpdate();
m_ClassContents->musicZoneManager.onUpdate();
}

void WorldInstance::removeEntity(Handle::EntityHandle h)
Expand Down Expand Up @@ -1209,3 +1216,8 @@ Logic::CameraController* WorldInstance::getCameraController()
Logic::Controller* ptr = getCameraComp<Components::LogicComponent>().m_pLogicController;
return dynamic_cast<Logic::CameraController*>(ptr);
}

Engine::MusicZoneManager& WorldInstance::getMusicZoneManager()
{
return m_ClassContents->musicZoneManager;
}
5 changes: 3 additions & 2 deletions src/engine/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ namespace World
const json& worldJson = json(),
const json& scriptEngine = json(),
const json& dialogManager = json(),
const json& logManager = json());
const json& logManager = json(),
const json& musicManager = json());

/**
* Creates an entity with the given components and returns its handle
Expand Down Expand Up @@ -219,6 +220,7 @@ namespace World
Logic::DialogManager& getDialogManager();
Logic::PfxManager& getPfxManager();
Animations::AnimationLibrary& getAnimationLibrary();
Engine::MusicZoneManager& getMusicZoneManager();

/**
* HUD's print-screen manager
Expand Down Expand Up @@ -412,6 +414,5 @@ namespace World
*/
std::unique_ptr<WorldAllocators> m_Allocators;
std::unique_ptr<ClassContents> m_ClassContents;
std::unique_ptr<Engine::MusicZoneManager> m_MusicZoneManager;
};
}
10 changes: 9 additions & 1 deletion src/logic/SavegameManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "SavegameManager.h"
#include <fstream>
#include "engine/GameEngine.h"
#include "engine/MusicZoneManager.h"
#include "ui/Hud.h"
#include "ui/LoadingScreen.h"
#include <json/json.hpp>
Expand Down Expand Up @@ -244,11 +245,13 @@ std::string Engine::SavegameManager::loadSaveGameSlot(int index)
json scriptEngine = json::parse(SavegameManager::readFileInSlot(index, "scriptengine.json"));
json dialogManager = json::parse(SavegameManager::readFileInSlot(index, "dialogmanager.json"));
json logManager = json::parse(SavegameManager::readFileInSlot(index, "logmanager.json"));
json musicManager = json::parse(SavegameManager::readFileInSlot(index, "musicmanager.json"));
engine->getSession().setCurrentSlot(index);
engine->getGameClock().setTotalSeconds(timePlayed);
using UniqueWorld = std::unique_ptr<World::WorldInstance>;
std::shared_ptr<UniqueWorld> pWorld;
pWorld = std::make_shared<UniqueWorld>(engine->getSession().createWorld("", worldJson, scriptEngine, dialogManager, logManager));
pWorld = std::make_shared<UniqueWorld>(engine->getSession().createWorld("",
worldJson, scriptEngine, dialogManager, logManager, musicManager));

auto registerWorld = [index, pWorld](BaseEngine * engine)
{
Expand Down Expand Up @@ -338,6 +341,11 @@ void Engine::SavegameManager::saveToSlot(int index, std::string savegameName)
json scriptEngine;
mainWorld.getScriptEngine().exportScriptEngine(scriptEngine);
Engine::SavegameManager::writeFileInSlot(index, "scriptengine.json", Utils::iso_8859_1_to_utf8(scriptEngine.dump(4)));

json musicManager;
mainWorld.getMusicZoneManager().exportMusicZoneManager(musicManager);
Engine::SavegameManager::writeFileInSlot(index, "musicmanager.json", Utils::iso_8859_1_to_utf8(musicManager.dump(4)));

gameEngine->getSession().setCurrentSlot(index);
}

Expand Down

0 comments on commit 84b22ce

Please sign in to comment.