From 19fd404b7ba0fd32e45473f91ae3c361758b7513 Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sun, 14 Oct 2018 14:44:32 +0300 Subject: [PATCH] Support soundgen calls for activators (feature #4285) --- CHANGELOG.md | 1 + apps/openmw/mwclass/activator.cpp | 70 +++++++++++++++++++++++++++++++ apps/openmw/mwclass/activator.hpp | 4 ++ 3 files changed, 75 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 320e486937..ddf20fa7f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -155,6 +155,7 @@ Feature #4012: Editor: Write a log file if OpenCS crashes Feature #4222: 360° screenshots Feature #4256: Implement ToggleBorders (TB) console command + Feature #4285: Support soundgen calls for activators Feature #4324: Add CFBundleIdentifier in Info.plist to allow for macOS function key shortcuts Feature #4345: Add equivalents for the command line commands to Launcher Feature #4404: Editor: All EnumDelegate fields should have their items sorted alphabetically diff --git a/apps/openmw/mwclass/activator.cpp b/apps/openmw/mwclass/activator.cpp index 8df262f240..7f53709d6c 100644 --- a/apps/openmw/mwclass/activator.cpp +++ b/apps/openmw/mwclass/activator.cpp @@ -1,6 +1,7 @@ #include "activator.hpp" #include +#include #include "../mwbase/environment.hpp" #include "../mwbase/windowmanager.hpp" @@ -134,4 +135,73 @@ namespace MWClass return MWWorld::Ptr(cell.insert(ref), &cell); } + + std::string Activator::getSoundIdFromSndGen(const MWWorld::Ptr &ptr, const std::string &name) const + { + std::string model = getModel(ptr); + if (model.empty()) + return std::string(); + + const MWWorld::Store &creaturestore = MWBase::Environment::get().getWorld()->getStore().get(); + std::string creatureId; + + for (const ESM::Creature &iter : creaturestore) + { + if (iter.mModel.empty()) + continue; + + if (Misc::StringUtils::ciEqual(model, "meshes\\" + iter.mModel)) + { + creatureId = !iter.mOriginal.empty() ? iter.mOriginal : iter.mId; + break; + } + } + + if (creatureId.empty()) + return std::string(); + + const MWWorld::Store &store = MWBase::Environment::get().getWorld()->getStore().get(); + + int type = getSndGenTypeFromName(name); + std::vector sounds; + + MWWorld::Store::iterator sound = store.begin(); + + while (sound != store.end()) + { + if (type == sound->mType && !sound->mCreature.empty() && (Misc::StringUtils::ciEqual(creatureId, sound->mCreature))) + sounds.push_back(&*sound); + ++sound; + } + + if (!sounds.empty()) + return sounds[Misc::Rng::rollDice(sounds.size())]->mSound; + + if (type == ESM::SoundGenerator::Land) + return "Body Fall Large"; + + return std::string(); + } + + int Activator::getSndGenTypeFromName(const std::string &name) const + { + if (name == "left") + return 0; + if (name == "right") + return 1; + if (name == "swimleft") + return 2; + if (name == "swimright") + return 3; + if (name == "moan") + return 4; + if (name == "roar") + return 5; + if (name == "scream") + return 6; + if (name == "land") + return 7; + + throw std::runtime_error(std::string("Unexpected soundgen type: ")+name); + } } diff --git a/apps/openmw/mwclass/activator.hpp b/apps/openmw/mwclass/activator.hpp index 3f333f4cbe..0f66b74b45 100644 --- a/apps/openmw/mwclass/activator.hpp +++ b/apps/openmw/mwclass/activator.hpp @@ -44,6 +44,10 @@ namespace MWClass ///< Whether or not to use animated variant of model (default false) virtual bool isActivator() const; + + virtual std::string getSoundIdFromSndGen(const MWWorld::Ptr &ptr, const std::string &name) const; + + virtual int getSndGenTypeFromName(const std::string &name) const; }; }