mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-10 16:14:26 +00:00
moved global variable handling into a separate class
This commit is contained in:
parent
608ddd0a58
commit
083b11c740
@ -62,12 +62,15 @@ set(GAMESOUND_HEADER
|
|||||||
source_group(apps\\openmw\\mwsound FILES ${GAMESOUND} ${GAMESOUND_HEADER})
|
source_group(apps\\openmw\\mwsound FILES ${GAMESOUND} ${GAMESOUND_HEADER})
|
||||||
|
|
||||||
set(GAMEWORLD
|
set(GAMEWORLD
|
||||||
mwworld/world.cpp)
|
mwworld/world.cpp
|
||||||
|
mwworld/globals.cpp
|
||||||
|
)
|
||||||
set(GAMEWORLD_HEADER
|
set(GAMEWORLD_HEADER
|
||||||
mwworld/refdata.hpp
|
mwworld/refdata.hpp
|
||||||
mwworld/world.hpp
|
mwworld/world.hpp
|
||||||
mwworld/ptr.hpp
|
mwworld/ptr.hpp
|
||||||
mwworld/environment.hpp
|
mwworld/environment.hpp
|
||||||
|
mwworld/globals.hpp
|
||||||
)
|
)
|
||||||
source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER})
|
source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER})
|
||||||
|
|
||||||
|
@ -117,44 +117,34 @@ namespace MWScript
|
|||||||
|
|
||||||
int InterpreterContext::getGlobalShort (const std::string& name) const
|
int InterpreterContext::getGlobalShort (const std::string& name) const
|
||||||
{
|
{
|
||||||
Interpreter::Type_Data value = mEnvironment.mWorld->getGlobalVariable (name);
|
return mEnvironment.mWorld->getGlobalVariable (name).mShort;
|
||||||
return static_cast<Interpreter::Type_Short> (
|
|
||||||
*reinterpret_cast<Interpreter::Type_Integer *> (&value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int InterpreterContext::getGlobalLong (const std::string& name) const
|
int InterpreterContext::getGlobalLong (const std::string& name) const
|
||||||
{
|
{
|
||||||
// a global long is internally a float.
|
// a global long is internally a float.
|
||||||
Interpreter::Type_Data value = mEnvironment.mWorld->getGlobalVariable (name);
|
return mEnvironment.mWorld->getGlobalVariable (name).mLong;
|
||||||
return static_cast<Interpreter::Type_Integer> (
|
|
||||||
*reinterpret_cast<Interpreter::Type_Float *> (&value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float InterpreterContext::getGlobalFloat (const std::string& name) const
|
float InterpreterContext::getGlobalFloat (const std::string& name) const
|
||||||
{
|
{
|
||||||
Interpreter::Type_Data value = mEnvironment.mWorld->getGlobalVariable (name);
|
return mEnvironment.mWorld->getGlobalVariable (name).mFloat;
|
||||||
return *reinterpret_cast<Interpreter::Type_Float *> (&value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InterpreterContext::setGlobalShort (const std::string& name, int value)
|
void InterpreterContext::setGlobalShort (const std::string& name, int value)
|
||||||
{
|
{
|
||||||
mEnvironment.mWorld->getGlobalVariable (name) =
|
mEnvironment.mWorld->getGlobalVariable (name).mShort = value;
|
||||||
*reinterpret_cast<Interpreter::Type_Data *> (&value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InterpreterContext::setGlobalLong (const std::string& name, int value)
|
void InterpreterContext::setGlobalLong (const std::string& name, int value)
|
||||||
{
|
{
|
||||||
// a global long is internally a float.
|
// a global long is internally a float.
|
||||||
float value2 = float(value);
|
mEnvironment.mWorld->getGlobalVariable (name).mLong = value;
|
||||||
|
|
||||||
mEnvironment.mWorld->getGlobalVariable (name) =
|
|
||||||
*reinterpret_cast<Interpreter::Type_Data *> (&value2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InterpreterContext::setGlobalFloat (const std::string& name, float value)
|
void InterpreterContext::setGlobalFloat (const std::string& name, float value)
|
||||||
{
|
{
|
||||||
mEnvironment.mWorld->getGlobalVariable (name) =
|
mEnvironment.mWorld->getGlobalVariable (name).mFloat = value;
|
||||||
*reinterpret_cast<Interpreter::Type_Data *> (&value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InterpreterContext::isScriptRunning (const std::string& name) const
|
bool InterpreterContext::isScriptRunning (const std::string& name) const
|
||||||
|
149
apps/openmw/mwworld/globals.cpp
Normal file
149
apps/openmw/mwworld/globals.cpp
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <components/esm_store/store.hpp>
|
||||||
|
|
||||||
|
namespace MWWorld
|
||||||
|
{
|
||||||
|
Globals::Collection::const_iterator Globals::find (const std::string& name) const
|
||||||
|
{
|
||||||
|
Collection::const_iterator iter = mVariables.find (name);
|
||||||
|
|
||||||
|
if (iter==mVariables.end())
|
||||||
|
throw std::runtime_error ("unknown global variable: " + name);
|
||||||
|
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
Globals::Collection::iterator Globals::find (const std::string& name)
|
||||||
|
{
|
||||||
|
Collection::iterator iter = mVariables.find (name);
|
||||||
|
|
||||||
|
if (iter==mVariables.end())
|
||||||
|
throw std::runtime_error ("unknown global variable: " + name);
|
||||||
|
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
Globals::Globals (const ESMS::ESMStore& store)
|
||||||
|
{
|
||||||
|
for (ESMS::RecListT<ESM::Global>::MapType::const_iterator iter
|
||||||
|
(store.globals.list.begin()); iter != store.globals.list.end(); ++iter)
|
||||||
|
{
|
||||||
|
char type = ' ';
|
||||||
|
Data value;
|
||||||
|
|
||||||
|
switch (iter->second.type)
|
||||||
|
{
|
||||||
|
case ESM::VT_Short:
|
||||||
|
|
||||||
|
type = 's';
|
||||||
|
value.mShort = *reinterpret_cast<const Interpreter::Type_Integer *> (
|
||||||
|
&iter->second.value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ESM::VT_Int:
|
||||||
|
|
||||||
|
type = 'l';
|
||||||
|
value.mLong = *reinterpret_cast<const Interpreter::Type_Float *> (
|
||||||
|
&iter->second.value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ESM::VT_Float:
|
||||||
|
|
||||||
|
type = 'f';
|
||||||
|
value.mFloat = *reinterpret_cast<const Interpreter::Type_Float *> (
|
||||||
|
&iter->second.value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
throw std::runtime_error ("unsupported global variable type");
|
||||||
|
}
|
||||||
|
|
||||||
|
mVariables.insert (std::make_pair (iter->first, std::make_pair (type, value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mVariables.find ("dayspassed")==mVariables.end())
|
||||||
|
{
|
||||||
|
// vanilla Morrowind does not define dayspassed.
|
||||||
|
Data value;
|
||||||
|
value.mLong = 0;
|
||||||
|
|
||||||
|
mVariables.insert (std::make_pair ("dayspassed", std::make_pair ('l', value)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Globals::Data& Globals::operator[] (const std::string& name) const
|
||||||
|
{
|
||||||
|
Collection::const_iterator iter = find (name);
|
||||||
|
|
||||||
|
return iter->second.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
Globals::Data& Globals::operator[] (const std::string& name)
|
||||||
|
{
|
||||||
|
Collection::iterator iter = find (name);
|
||||||
|
|
||||||
|
return iter->second.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Globals::setInt (const std::string& name, int value)
|
||||||
|
{
|
||||||
|
Collection::iterator iter = find (name);
|
||||||
|
|
||||||
|
switch (iter->second.first)
|
||||||
|
{
|
||||||
|
case 's': iter->second.second.mShort = value; break;
|
||||||
|
case 'l': iter->second.second.mLong = value; break;
|
||||||
|
case 'f': iter->second.second.mFloat = value; break;
|
||||||
|
|
||||||
|
default: throw std::runtime_error ("unsupported global variable type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Globals::setFloat (const std::string& name, float value)
|
||||||
|
{
|
||||||
|
Collection::iterator iter = find (name);
|
||||||
|
|
||||||
|
switch (iter->second.first)
|
||||||
|
{
|
||||||
|
case 's': iter->second.second.mShort = value; break;
|
||||||
|
case 'l': iter->second.second.mLong = value; break;
|
||||||
|
case 'f': iter->second.second.mFloat = value; break;
|
||||||
|
|
||||||
|
default: throw std::runtime_error ("unsupported global variable type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Globals::getInt (const std::string& name) const
|
||||||
|
{
|
||||||
|
Collection::const_iterator iter = find (name);
|
||||||
|
|
||||||
|
switch (iter->second.first)
|
||||||
|
{
|
||||||
|
case 's': return iter->second.second.mShort;
|
||||||
|
case 'l': return iter->second.second.mLong;
|
||||||
|
case 'f': return iter->second.second.mFloat;
|
||||||
|
|
||||||
|
default: throw std::runtime_error ("unsupported global variable type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float Globals::getFloat (const std::string& name) const
|
||||||
|
{
|
||||||
|
Collection::const_iterator iter = find (name);
|
||||||
|
|
||||||
|
switch (iter->second.first)
|
||||||
|
{
|
||||||
|
case 's': return iter->second.second.mShort;
|
||||||
|
case 'l': return iter->second.second.mLong;
|
||||||
|
case 'f': return iter->second.second.mFloat;
|
||||||
|
|
||||||
|
default: throw std::runtime_error ("unsupported global variable type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
59
apps/openmw/mwworld/globals.hpp
Normal file
59
apps/openmw/mwworld/globals.hpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#ifndef GAME_MWWORLD_GLOBALS_H
|
||||||
|
#define GAME_MWWORLD_GLOBALS_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include <components/interpreter/types.hpp>
|
||||||
|
|
||||||
|
namespace ESMS
|
||||||
|
{
|
||||||
|
struct ESMStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace MWWorld
|
||||||
|
{
|
||||||
|
class Globals
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
union Data
|
||||||
|
{
|
||||||
|
Interpreter::Type_Float mFloat;
|
||||||
|
Interpreter::Type_Float mLong; // Why Morrowind, why? :(
|
||||||
|
Interpreter::Type_Integer mShort;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::map<std::string, std::pair<char, Data> > Collection;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Collection mVariables; // type, value
|
||||||
|
|
||||||
|
Collection::const_iterator find (const std::string& name) const;
|
||||||
|
|
||||||
|
Collection::iterator find (const std::string& name);
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Globals (const ESMS::ESMStore& store);
|
||||||
|
|
||||||
|
const Data& operator[] (const std::string& name) const;
|
||||||
|
|
||||||
|
Data& operator[] (const std::string& name);
|
||||||
|
|
||||||
|
void setInt (const std::string& name, int value);
|
||||||
|
///< Set value independently from real type.
|
||||||
|
|
||||||
|
void setFloat (const std::string& name, float value);
|
||||||
|
///< Set value independently from real type.
|
||||||
|
|
||||||
|
int getInt (const std::string& name) const;
|
||||||
|
///< Get value independently from real type.
|
||||||
|
|
||||||
|
float getFloat (const std::string& name) const;
|
||||||
|
///< Get value independently from real type.
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -143,7 +143,7 @@ namespace MWWorld
|
|||||||
|
|
||||||
World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir,
|
World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir,
|
||||||
const std::string& master, const std::string& startCell, bool newGame)
|
const std::string& master, const std::string& startCell, bool newGame)
|
||||||
: mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0)
|
: mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0)
|
||||||
{
|
{
|
||||||
boost::filesystem::path masterPath (dataDir);
|
boost::filesystem::path masterPath (dataDir);
|
||||||
masterPath /= master;
|
masterPath /= master;
|
||||||
@ -162,17 +162,12 @@ namespace MWWorld
|
|||||||
mPlayerPos = new MWRender::PlayerPos (mScene.getCamera(), mStore.npcs.find ("player"));
|
mPlayerPos = new MWRender::PlayerPos (mScene.getCamera(), mStore.npcs.find ("player"));
|
||||||
|
|
||||||
// global variables
|
// global variables
|
||||||
for (ESMS::RecListT<ESM::Global>::MapType::const_iterator iter
|
mGlobalVariables = new Globals (mStore);
|
||||||
(mStore.globals.list.begin());
|
|
||||||
iter != mStore.globals.list.end(); ++iter)
|
|
||||||
mGlobalVariables.insert (std::make_pair (iter->first, iter->second.value));
|
|
||||||
|
|
||||||
if (newGame)
|
if (newGame)
|
||||||
{
|
{
|
||||||
// set new game mark
|
// set new game mark
|
||||||
float newGameState = 1;
|
mGlobalVariables->setInt ("chargenstate", 1);
|
||||||
mGlobalVariables["chargenstate"] =
|
|
||||||
*reinterpret_cast<Interpreter::Type_Data *> (&newGameState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This connects the cell data with the rendering scene.
|
// This connects the cell data with the rendering scene.
|
||||||
@ -203,6 +198,7 @@ namespace MWWorld
|
|||||||
|
|
||||||
delete mPlayerPos;
|
delete mPlayerPos;
|
||||||
delete mSkyManager;
|
delete mSkyManager;
|
||||||
|
delete mGlobalVariables;
|
||||||
}
|
}
|
||||||
|
|
||||||
MWRender::PlayerPos& World::getPlayerPos()
|
MWRender::PlayerPos& World::getPlayerPos()
|
||||||
@ -226,14 +222,9 @@ namespace MWWorld
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Interpreter::Type_Data& World::getGlobalVariable (const std::string& name)
|
Globals::Data& World::getGlobalVariable (const std::string& name)
|
||||||
{
|
{
|
||||||
std::map<std::string, Interpreter::Type_Data>::iterator iter = mGlobalVariables.find (name);
|
return (*mGlobalVariables)[name];
|
||||||
|
|
||||||
if (iter==mGlobalVariables.end())
|
|
||||||
throw std::runtime_error ("unknown global variable: " + name);
|
|
||||||
|
|
||||||
return iter->second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ptr World::getPtr (const std::string& name, bool activeOnly)
|
Ptr World::getPtr (const std::string& name, bool activeOnly)
|
||||||
|
@ -8,13 +8,12 @@
|
|||||||
|
|
||||||
#include <components/esm_store/cell_store.hpp>
|
#include <components/esm_store/cell_store.hpp>
|
||||||
|
|
||||||
#include <components/interpreter/types.hpp>
|
|
||||||
|
|
||||||
#include "../mwrender/playerpos.hpp"
|
#include "../mwrender/playerpos.hpp"
|
||||||
#include "../mwrender/mwscene.hpp"
|
#include "../mwrender/mwscene.hpp"
|
||||||
|
|
||||||
#include "refdata.hpp"
|
#include "refdata.hpp"
|
||||||
#include "ptr.hpp"
|
#include "ptr.hpp"
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
namespace Render
|
namespace Render
|
||||||
{
|
{
|
||||||
@ -51,7 +50,7 @@ namespace MWWorld
|
|||||||
ESMS::ESMStore mStore;
|
ESMS::ESMStore mStore;
|
||||||
std::map<std::string, Ptr::CellStore> mInteriors;
|
std::map<std::string, Ptr::CellStore> mInteriors;
|
||||||
ScriptList mLocalScripts;
|
ScriptList mLocalScripts;
|
||||||
std::map<std::string, Interpreter::Type_Data> mGlobalVariables;
|
MWWorld::Globals *mGlobalVariables;
|
||||||
|
|
||||||
// not implemented
|
// not implemented
|
||||||
World (const World&);
|
World (const World&);
|
||||||
@ -80,7 +79,7 @@ namespace MWWorld
|
|||||||
bool hasCellChanged() const;
|
bool hasCellChanged() const;
|
||||||
///< Has the player moved to a different cell, since the last frame?
|
///< Has the player moved to a different cell, since the last frame?
|
||||||
|
|
||||||
Interpreter::Type_Data& getGlobalVariable (const std::string& name);
|
Globals::Data& getGlobalVariable (const std::string& name);
|
||||||
|
|
||||||
Ptr getPtr (const std::string& name, bool activeOnly);
|
Ptr getPtr (const std::string& name, bool activeOnly);
|
||||||
///< Return a pointer to a liveCellRef with the given name.
|
///< Return a pointer to a liveCellRef with the given name.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user