1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-04-01 15:36:42 +00:00

rewrote global variable storage (using ESM variant type now)

This commit is contained in:
Marc Zinnschlag 2013-12-10 15:09:58 +01:00
parent 1fdd43bbb7
commit 51bfa5cde3
4 changed files with 80 additions and 179 deletions

View file

@ -7,15 +7,14 @@
namespace MWWorld namespace MWWorld
{ {
std::vector<std::string> Globals::getGlobals () const std::vector<std::string> Globals::getGlobals() const
{ {
std::vector<std::string> retval; std::vector<std::string> ids;
Collection::const_iterator it;
for(it = mVariables.begin(); it != mVariables.end(); ++it){
retval.push_back(it->first);
}
return retval; for (Collection::const_iterator iter = mVariables.begin(); iter!=mVariables.end(); ++iter)
ids.push_back (iter->first);
return ids;
} }
Globals::Collection::const_iterator Globals::find (const std::string& name) const Globals::Collection::const_iterator Globals::find (const std::string& name) const
@ -38,112 +37,27 @@ namespace MWWorld
return iter; return iter;
} }
Globals::Globals (const MWWorld::ESMStore& store) void Globals::fill (const MWWorld::ESMStore& store)
{ {
const MWWorld::Store<ESM::Global> &globals = store.get<ESM::Global>(); mVariables.clear();
MWWorld::Store<ESM::Global>::iterator iter = globals.begin();
for (; iter != globals.end(); ++iter) const MWWorld::Store<ESM::Global>& globals = store.get<ESM::Global>();
for (MWWorld::Store<ESM::Global>::iterator iter = globals.begin(); iter!=globals.end();
++iter)
{ {
char type = ' '; mVariables.insert (std::make_pair (iter->mId, iter->mValue));
Data value;
switch (iter->mValue.getType())
{
case ESM::VT_Short:
type = 's';
value.mShort = iter->mValue.getInteger();
break;
case ESM::VT_Long:
type = 'l';
value.mLong = iter->mValue.getInteger();
break;
case ESM::VT_Float:
type = 'f';
value.mFloat = iter->mValue.getFloat();
break;
default:
throw std::runtime_error ("unsupported global variable type");
}
mVariables.insert (std::make_pair (iter->mId, std::make_pair (type, value)));
} }
} }
const Globals::Data& Globals::operator[] (const std::string& name) const const ESM::Variant& Globals::operator[] (const std::string& name) const
{ {
Collection::const_iterator iter = find (name); return find (name)->second;
return iter->second.second;
} }
Globals::Data& Globals::operator[] (const std::string& name) ESM::Variant& Globals::operator[] (const std::string& name)
{ {
Collection::iterator iter = find (name); return find (name)->second;
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");
}
} }
char Globals::getType (const std::string& name) const char Globals::getType (const std::string& name) const
@ -153,7 +67,13 @@ namespace MWWorld
if (iter==mVariables.end()) if (iter==mVariables.end())
return ' '; return ' ';
return iter->second.first; switch (iter->second.getType())
{
case ESM::VT_Short: return 's';
case ESM::VT_Long: return 'l';
case ESM::VT_Float: return 'f';
default: return ' ';
}
} }
} }

View file

@ -6,6 +6,7 @@
#include <map> #include <map>
#include <components/interpreter/types.hpp> #include <components/interpreter/types.hpp>
#include <components/esm/variant.hpp>
namespace MWWorld namespace MWWorld
{ {
@ -13,49 +14,29 @@ namespace MWWorld
class Globals class Globals
{ {
public:
union Data
{
Interpreter::Type_Float mFloat;
Interpreter::Type_Float mLong; // Why Morrowind, why? :(
Interpreter::Type_Float mShort;
};
typedef std::map<std::string, std::pair<char, Data> > Collection;
private: private:
typedef std::map<std::string, ESM::Variant> Collection;
Collection mVariables; // type, value Collection mVariables; // type, value
Collection::const_iterator find (const std::string& name) const; Collection::const_iterator find (const std::string& name) const;
Collection::iterator find (const std::string& name); Collection::iterator find (const std::string& name);
public:
Globals (const MWWorld::ESMStore& store);
const Data& operator[] (const std::string& name) const;
Data& operator[] (const std::string& name); public:
void setInt (const std::string& name, int value); const ESM::Variant& operator[] (const std::string& name) const;
///< Set value independently from real type.
ESM::Variant& operator[] (const std::string& name);
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.
char getType (const std::string& name) const; char getType (const std::string& name) const;
///< If there is no global variable with this name, ' ' is returned. ///< If there is no global variable with this name, ' ' is returned.
std::vector<std::string> getGlobals () const; std::vector<std::string> getGlobals() const;
void fill (const MWWorld::ESMStore& store);
///< Replace variables with variables from \a store with default values.
}; };
} }

View file

@ -172,9 +172,9 @@ namespace MWWorld
{ {
if (mSky && (isCellExterior() || isCellQuasiExterior())) if (mSky && (isCellExterior() || isCellQuasiExterior()))
{ {
mRendering->skySetHour (mGlobalVariables->getFloat ("gamehour")); mRendering->skySetHour (mGlobalVariables["gamehour"].getFloat());
mRendering->skySetDate (mGlobalVariables->getInt ("day"), mRendering->skySetDate (mGlobalVariables["day"].getInteger(),
mGlobalVariables->getInt ("month")); mGlobalVariables["month"].getInteger());
mRendering->skyEnable(); mRendering->skyEnable();
} }
@ -187,7 +187,7 @@ namespace MWWorld
const std::vector<std::string>& contentFiles, const std::vector<std::string>& contentFiles,
const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir, const boost::filesystem::path& resDir, const boost::filesystem::path& cacheDir,
ToUTF8::Utf8Encoder* encoder, const std::map<std::string,std::string>& fallbackMap, int mActivationDistanceOverride) ToUTF8::Utf8Encoder* encoder, const std::map<std::string,std::string>& fallbackMap, int mActivationDistanceOverride)
: mPlayer (0), mLocalScripts (mStore), mGlobalVariables (0), : mPlayer (0), mLocalScripts (mStore),
mSky (true), mCells (mStore, mEsm), mSky (true), mCells (mStore, mEsm),
mActivationDistanceOverride (mActivationDistanceOverride), mActivationDistanceOverride (mActivationDistanceOverride),
mFallback(fallbackMap), mPlayIntro(0), mTeleportEnabled(true), mLevitationEnabled(false), mFallback(fallbackMap), mPlayIntro(0), mTeleportEnabled(true), mLevitationEnabled(false),
@ -227,7 +227,7 @@ namespace MWWorld
mStore.setUp(); mStore.setUp();
mStore.movePlayerRecord(); mStore.movePlayerRecord();
mGlobalVariables = new Globals (mStore); mGlobalVariables.fill (mStore);
mWorldScene = new Scene(*mRendering, mPhysics); mWorldScene = new Scene(*mRendering, mPhysics);
} }
@ -254,12 +254,9 @@ namespace MWWorld
// FIXME: should be set to 1, but the sound manager won't pause newly started sounds // FIXME: should be set to 1, but the sound manager won't pause newly started sounds
mPlayIntro = 2; mPlayIntro = 2;
// global variables
*mGlobalVariables = Globals (mStore);
// set new game mark // set new game mark
mGlobalVariables->setInt ("chargenstate", 1); mGlobalVariables["chargenstate"].setInteger (1);
mGlobalVariables->setInt ("pcrace", 3); mGlobalVariables["pcrace"].setInteger (3);
// we don't want old weather to persist on a new game // we don't want old weather to persist on a new game
delete mWeatherManager; delete mWeatherManager;
@ -298,6 +295,8 @@ namespace MWWorld
mTeleportEnabled = true; mTeleportEnabled = true;
mPlayIntro = 0; mPlayIntro = 0;
mFacedDistance = FLT_MAX; mFacedDistance = FLT_MAX;
mGlobalVariables.fill (mStore);
} }
int World::countSavedGameRecords() const int World::countSavedGameRecords() const
@ -358,7 +357,6 @@ namespace MWWorld
{ {
delete mWeatherManager; delete mWeatherManager;
delete mWorldScene; delete mWorldScene;
delete mGlobalVariables;
delete mRendering; delete mRendering;
delete mPhysics; delete mPhysics;
@ -436,7 +434,7 @@ namespace MWWorld
else if (name=="month") else if (name=="month")
setMonth (value); setMonth (value);
else else
mGlobalVariables->setInt (name, value); mGlobalVariables[name].setInteger (value);
} }
void World::setGlobalFloat (const std::string& name, float value) void World::setGlobalFloat (const std::string& name, float value)
@ -448,27 +446,27 @@ namespace MWWorld
else if (name=="month") else if (name=="month")
setMonth (value); setMonth (value);
else else
mGlobalVariables->setFloat (name, value); mGlobalVariables[name].setFloat (value);
} }
int World::getGlobalInt (const std::string& name) const int World::getGlobalInt (const std::string& name) const
{ {
return mGlobalVariables->getInt (name); return mGlobalVariables[name].getInteger();
} }
float World::getGlobalFloat (const std::string& name) const float World::getGlobalFloat (const std::string& name) const
{ {
return mGlobalVariables->getFloat (name); return mGlobalVariables[name].getFloat();
} }
char World::getGlobalVariableType (const std::string& name) const char World::getGlobalVariableType (const std::string& name) const
{ {
return mGlobalVariables->getType (name); return mGlobalVariables.getType (name);
} }
std::vector<std::string> World::getGlobals () const std::vector<std::string> World::getGlobals() const
{ {
return mGlobalVariables->getGlobals(); return mGlobalVariables.getGlobals();
} }
std::string World::getCellName (const MWWorld::CellStore *cell) const std::string World::getCellName (const MWWorld::CellStore *cell) const
@ -618,14 +616,15 @@ namespace MWWorld
mWeatherManager->advanceTime (hours); mWeatherManager->advanceTime (hours);
hours += mGlobalVariables->getFloat ("gamehour"); hours += mGlobalVariables["gamehour"].getFloat();
setHour (hours); setHour (hours);
int days = hours / 24; int days = hours / 24;
if (days>0) if (days>0)
mGlobalVariables->setInt ("dayspassed", days + mGlobalVariables->getInt ("dayspassed")); mGlobalVariables["dayspassed"].setInteger (
days + mGlobalVariables["dayspassed"].getInteger());
} }
void World::setHour (double hour) void World::setHour (double hour)
@ -637,14 +636,14 @@ namespace MWWorld
hour = std::fmod (hour, 24); hour = std::fmod (hour, 24);
mGlobalVariables->setFloat ("gamehour", hour); mGlobalVariables["gamehour"].setFloat (hour);
mRendering->skySetHour (hour); mRendering->skySetHour (hour);
mWeatherManager->setHour (hour); mWeatherManager->setHour (hour);
if (days>0) if (days>0)
setDay (days + mGlobalVariables->getInt ("day")); setDay (days + mGlobalVariables["day"].getInteger());
} }
void World::setDay (int day) void World::setDay (int day)
@ -652,7 +651,7 @@ namespace MWWorld
if (day<1) if (day<1)
day = 1; day = 1;
int month = mGlobalVariables->getInt ("month"); int month = mGlobalVariables["month"].getInteger();
while (true) while (true)
{ {
@ -667,14 +666,14 @@ namespace MWWorld
else else
{ {
month = 0; month = 0;
mGlobalVariables->setInt ("year", mGlobalVariables->getInt ("year")+1); mGlobalVariables["year"].setInteger (mGlobalVariables["year"].getInteger()+1);
} }
day -= days; day -= days;
} }
mGlobalVariables->setInt ("day", day); mGlobalVariables["day"].setInteger (day);
mGlobalVariables->setInt ("month", month); mGlobalVariables["month"].setInteger (month);
mRendering->skySetDate (day, month); mRendering->skySetDate (day, month);
@ -691,30 +690,30 @@ namespace MWWorld
int days = getDaysPerMonth (month); int days = getDaysPerMonth (month);
if (mGlobalVariables->getInt ("day")>days) if (mGlobalVariables["day"].getInteger()>days)
mGlobalVariables->setInt ("day", days); mGlobalVariables["day"].setInteger (days);
mGlobalVariables->setInt ("month", month); mGlobalVariables["month"].setInteger (month);
if (years>0) if (years>0)
mGlobalVariables->setInt ("year", years+mGlobalVariables->getInt ("year")); mGlobalVariables["year"].setInteger (years+mGlobalVariables["year"].getInteger());
mRendering->skySetDate (mGlobalVariables->getInt ("day"), month); mRendering->skySetDate (mGlobalVariables["day"].getInteger(), month);
} }
int World::getDay() const int World::getDay() const
{ {
return mGlobalVariables->getInt("day"); return mGlobalVariables["day"].getInteger();
} }
int World::getMonth() const int World::getMonth() const
{ {
return mGlobalVariables->getInt("month"); return mGlobalVariables["month"].getInteger();
} }
int World::getYear() const int World::getYear() const
{ {
return mGlobalVariables->getInt("year"); return mGlobalVariables["year"].getInteger();
} }
std::string World::getMonthName (int month) const std::string World::getMonthName (int month) const
@ -739,8 +738,8 @@ namespace MWWorld
TimeStamp World::getTimeStamp() const TimeStamp World::getTimeStamp() const
{ {
return TimeStamp (mGlobalVariables->getFloat ("gamehour"), return TimeStamp (mGlobalVariables["gamehour"].getFloat(),
mGlobalVariables->getInt ("dayspassed")); mGlobalVariables["dayspassed"].getInteger());
} }
bool World::toggleSky() bool World::toggleSky()
@ -776,7 +775,7 @@ namespace MWWorld
float World::getTimeScaleFactor() const float World::getTimeScaleFactor() const
{ {
return mGlobalVariables->getFloat ("timescale"); return mGlobalVariables["timescale"].getFloat();
} }
void World::changeToInteriorCell (const std::string& cellName, const ESM::Position& position) void World::changeToInteriorCell (const std::string& cellName, const ESM::Position& position)
@ -1267,7 +1266,7 @@ namespace MWWorld
if (Misc::StringUtils::ciEqual (ids[i], record.mRace)) if (Misc::StringUtils::ciEqual (ids[i], record.mRace))
break; break;
mGlobalVariables->setInt ("pcrace", (i == ids.size()) ? 0 : i+1); mGlobalVariables["pcrace"].setInteger (i == ids.size() ? 0 : i+1);
const ESM::NPC *player = const ESM::NPC *player =
mPlayer->getPlayer().get<ESM::NPC>()->mBase; mPlayer->getPlayer().get<ESM::NPC>()->mBase;

View file

@ -11,6 +11,7 @@
#include "localscripts.hpp" #include "localscripts.hpp"
#include "timestamp.hpp" #include "timestamp.hpp"
#include "fallback.hpp" #include "fallback.hpp"
#include "globals.hpp"
#include "../mwbase/world.hpp" #include "../mwbase/world.hpp"
@ -64,7 +65,7 @@ namespace MWWorld
std::vector<ESM::ESMReader> mEsm; std::vector<ESM::ESMReader> mEsm;
MWWorld::ESMStore mStore; MWWorld::ESMStore mStore;
LocalScripts mLocalScripts; LocalScripts mLocalScripts;
MWWorld::Globals *mGlobalVariables; MWWorld::Globals mGlobalVariables;
MWWorld::PhysicsSystem *mPhysics; MWWorld::PhysicsSystem *mPhysics;
bool mSky; bool mSky;