Define hardcoded global variable names as constants

7220-lua-add-a-general-purpose-lexical-parser
elsid 2 years ago
parent d22d3a9ea4
commit f09789002a
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -16,6 +16,7 @@
#include <osg/Timer>
#include "../mwworld/doorstate.hpp"
#include "../mwworld/globalvariablename.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/spellcaststate.hpp"
@ -160,19 +161,19 @@ namespace MWBase
virtual void getDoorMarkers(MWWorld::CellStore* cell, std::vector<DoorMarker>& out) = 0;
///< get a list of teleport door markers for a given cell, to be displayed on the local map
virtual void setGlobalInt(std::string_view name, int value) = 0;
virtual void setGlobalInt(MWWorld::GlobalVariableName name, int value) = 0;
///< Set value independently from real type.
virtual void setGlobalFloat(std::string_view name, float value) = 0;
virtual void setGlobalFloat(MWWorld::GlobalVariableName name, float value) = 0;
///< Set value independently from real type.
virtual int getGlobalInt(std::string_view name) const = 0;
virtual int getGlobalInt(MWWorld::GlobalVariableName name) const = 0;
///< Get value independently from real type.
virtual float getGlobalFloat(std::string_view name) const = 0;
virtual float getGlobalFloat(MWWorld::GlobalVariableName name) const = 0;
///< Get value independently from real type.
virtual char getGlobalVariableType(std::string_view name) const = 0;
virtual char getGlobalVariableType(MWWorld::GlobalVariableName name) const = 0;
///< Return ' ', if there is no global variable with this name.
virtual std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const = 0;

@ -10,6 +10,7 @@
#include "../mwbase/world.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/globals.hpp"
#include "../mwscript/interpretercontext.hpp"
@ -135,9 +136,9 @@ namespace MWDialogue
StampedJournalEntry StampedJournalEntry::makeFromQuest(
const ESM::RefId& topic, int index, const MWWorld::Ptr& actor)
{
int day = MWBase::Environment::get().getWorld()->getGlobalInt("dayspassed");
int month = MWBase::Environment::get().getWorld()->getGlobalInt("month");
int dayOfMonth = MWBase::Environment::get().getWorld()->getGlobalInt("day");
const int day = MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sDaysPassed);
const int month = MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sMonth);
const int dayOfMonth = MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sDay);
return StampedJournalEntry(topic, idFromIndex(topic, index), day, month, dayOfMonth, actor);
}

@ -13,6 +13,8 @@
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/globals.hpp"
#include "backgroundimage.hpp"
#include "confirmationdialog.hpp"
#include "savegamedialog.hpp"
@ -232,7 +234,7 @@ namespace MWGui
buttons.emplace_back("newgame");
if (state == MWBase::StateManager::State_Running
&& MWBase::Environment::get().getWorld()->getGlobalInt("chargenstate") == -1
&& MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sCharGenState) == -1
&& MWBase::Environment::get().getWindowManager()->isSavingAllowed())
buttons.emplace_back("savegame");

@ -63,6 +63,7 @@
#include "../mwworld/cellstore.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/globals.hpp"
#include "../mwworld/player.hpp"
#include "../mwmechanics/actorutil.hpp"
@ -1627,7 +1628,8 @@ namespace MWGui
bool WindowManager::getRestEnabled()
{
// Enable rest dialogue if character creation finished
if (mRestAllowed == false && MWBase::Environment::get().getWorld()->getGlobalFloat("chargenstate") == -1)
if (mRestAllowed == false
&& MWBase::Environment::get().getWorld()->getGlobalFloat(MWWorld::Globals::sCharGenState) == -1)
mRestAllowed = true;
return mRestAllowed;
}

@ -15,6 +15,7 @@
#include "../mwbase/world.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/globals.hpp"
#include "../mwworld/inventorystore.hpp"
#include "../mwworld/player.hpp"
@ -310,7 +311,7 @@ namespace MWInput
if (!checkAllowedToUseItems())
return;
if (MWBase::Environment::get().getWorld()->getGlobalFloat("chargenstate") != -1)
if (MWBase::Environment::get().getWorld()->getGlobalFloat(MWWorld::Globals::sCharGenState) != -1)
return;
if (!MWBase::Environment::get().getWindowManager()->isGuiMode())
@ -325,7 +326,7 @@ namespace MWInput
return;
}
if (MWBase::Environment::get().getWorld()->getGlobalFloat("chargenstate") != -1)
if (MWBase::Environment::get().getWorld()->getGlobalFloat(MWWorld::Globals::sCharGenState) != -1)
return;
if (!checkAllowedToUseItems())

@ -6,6 +6,8 @@
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/globals.hpp"
#include <set>
namespace ESM
@ -39,7 +41,10 @@ namespace MWLua
// Note that game time generally goes faster than the simulation time.
double getGameTime() const;
double getGameTimeScale() const { return MWBase::Environment::get().getWorld()->getTimeScaleFactor(); }
void setGameTimeScale(double s) { MWBase::Environment::get().getWorld()->setGlobalFloat("timescale", s); }
void setGameTimeScale(double s)
{
MWBase::Environment::get().getWorld()->setGlobalFloat(MWWorld::Globals::sTimeScale, s);
}
ObjectIdList getActivatorsInScene() const { return mActivatorsInScene.mList; }
ObjectIdList getActorsInScene() const { return mActorsInScene.mList; }

@ -18,6 +18,7 @@
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/globals.hpp"
#include "../mwworld/inventorystore.hpp"
#include "actorutil.hpp"
@ -483,7 +484,7 @@ namespace MWMechanics
{
healthdmg = true;
// GLOB instead of GMST because it gets updated during a quest
damage *= MWBase::Environment::get().getWorld()->getGlobalFloat("werewolfclawmult");
damage *= MWBase::Environment::get().getWorld()->getGlobalFloat(MWWorld::Globals::sWerewolfClawMult);
}
if (healthdmg)
{

@ -15,6 +15,7 @@
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/globals.hpp"
#include "../mwworld/inventorystore.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/ptr.hpp"
@ -1779,7 +1780,8 @@ namespace MWMechanics
if (ptr.getClass().isNpc() && target.getClass().isNpc())
{
if (target.getClass().getNpcStats(target).isWerewolf()
|| (target == getPlayer() && MWBase::Environment::get().getWorld()->getGlobalInt("pcknownwerewolf")))
|| (target == getPlayer()
&& MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sPCKnownWerewolf)))
{
const ESM::GameSetting* iWerewolfFightMod
= MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find(
@ -1909,7 +1911,7 @@ namespace MWMechanics
if (detected)
{
windowManager->messageBox("#{sWerewolfAlarmMessage}");
MWBase::Environment::get().getWorld()->setGlobalInt("pcknownwerewolf", 1);
MWBase::Environment::get().getWorld()->setGlobalInt(MWWorld::Globals::sPCKnownWerewolf, 1);
if (reported)
{

@ -33,6 +33,7 @@
#include "../mwworld/cellstore.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
#include "../mwworld/globals.hpp"
#include "../mwworld/scene.hpp"
#include "../mwworld/worldmodel.hpp"
@ -341,7 +342,7 @@ void MWState::StateManager::saveGame(const std::string& description, const Slot*
void MWState::StateManager::quickSave(std::string name)
{
if (!(mState == State_Running
&& MWBase::Environment::get().getWorld()->getGlobalInt("chargenstate") == -1 // char gen
&& MWBase::Environment::get().getWorld()->getGlobalInt(MWWorld::Globals::sCharGenState) == -1 // char gen
&& MWBase::Environment::get().getWindowManager()->isSavingAllowed()))
{
// You can not save your game right now

@ -48,12 +48,12 @@ namespace MWWorld
{
void DateTimeManager::setup(Globals& globalVariables)
{
mGameHour = globalVariables["gamehour"].getFloat();
mDaysPassed = globalVariables["dayspassed"].getInteger();
mDay = globalVariables["day"].getInteger();
mMonth = globalVariables["month"].getInteger();
mYear = globalVariables["year"].getInteger();
mTimeScale = globalVariables["timescale"].getFloat();
mGameHour = globalVariables[Globals::sGameHour].getFloat();
mDaysPassed = globalVariables[Globals::sDaysPassed].getInteger();
mDay = globalVariables[Globals::sDay].getInteger();
mMonth = globalVariables[Globals::sMonth].getInteger();
mYear = globalVariables[Globals::sYear].getInteger();
mTimeScale = globalVariables[Globals::sTimeScale].getFloat();
}
void DateTimeManager::setHour(double hour)
@ -145,11 +145,11 @@ namespace MWWorld
if (days > 0)
mDaysPassed += days;
globalVariables["gamehour"].setFloat(mGameHour);
globalVariables["dayspassed"].setInteger(mDaysPassed);
globalVariables["day"].setInteger(mDay);
globalVariables["month"].setInteger(mMonth);
globalVariables["year"].setInteger(mYear);
globalVariables[Globals::sGameHour].setFloat(mGameHour);
globalVariables[Globals::sDaysPassed].setInteger(mDaysPassed);
globalVariables[Globals::sDay].setInteger(mDay);
globalVariables[Globals::sMonth].setInteger(mMonth);
globalVariables[Globals::sYear].setInteger(mYear);
}
std::string_view DateTimeManager::getMonthName(int month) const
@ -170,32 +170,32 @@ namespace MWWorld
return setting->mValue.getString();
}
bool DateTimeManager::updateGlobalFloat(std::string_view name, float value)
bool DateTimeManager::updateGlobalFloat(GlobalVariableName name, float value)
{
if (name == "gamehour")
if (name == Globals::sGameHour)
{
setHour(value);
return true;
}
else if (name == "day")
else if (name == Globals::sDay)
{
setDay(static_cast<int>(value));
return true;
}
else if (name == "month")
else if (name == Globals::sMonth)
{
setMonth(static_cast<int>(value));
return true;
}
else if (name == "year")
else if (name == Globals::sYear)
{
mYear = static_cast<int>(value);
}
else if (name == "timescale")
else if (name == Globals::sTimeScale)
{
mTimeScale = value;
}
else if (name == "dayspassed")
else if (name == Globals::sDaysPassed)
{
mDaysPassed = static_cast<int>(value);
}
@ -203,32 +203,32 @@ namespace MWWorld
return false;
}
bool DateTimeManager::updateGlobalInt(std::string_view name, int value)
bool DateTimeManager::updateGlobalInt(GlobalVariableName name, int value)
{
if (name == "gamehour")
if (name == Globals::sGameHour)
{
setHour(static_cast<float>(value));
return true;
}
else if (name == "day")
else if (name == Globals::sDay)
{
setDay(value);
return true;
}
else if (name == "month")
else if (name == Globals::sMonth)
{
setMonth(value);
return true;
}
else if (name == "year")
else if (name == Globals::sYear)
{
mYear = value;
}
else if (name == "timescale")
else if (name == Globals::sTimeScale)
{
mTimeScale = static_cast<float>(value);
}
else if (name == "dayspassed")
else if (name == Globals::sDaysPassed)
{
mDaysPassed = value;
}

@ -3,6 +3,8 @@
#include <string_view>
#include "globalvariablename.hpp"
namespace ESM
{
struct EpochTimeStamp;
@ -35,8 +37,8 @@ namespace MWWorld
void advanceTime(double hours, Globals& globalVariables);
void setup(Globals& globalVariables);
bool updateGlobalInt(std::string_view name, int value);
bool updateGlobalFloat(std::string_view name, float value);
bool updateGlobalInt(GlobalVariableName name, int value);
bool updateGlobalFloat(GlobalVariableName name, float value);
};
}

@ -41,19 +41,19 @@ namespace MWWorld
}
}
const ESM::Variant& Globals::operator[](std::string_view name) const
const ESM::Variant& Globals::operator[](GlobalVariableName name) const
{
return find(name)->second.mValue;
return find(name.getValue())->second.mValue;
}
ESM::Variant& Globals::operator[](std::string_view name)
ESM::Variant& Globals::operator[](GlobalVariableName name)
{
return find(name)->second.mValue;
return find(name.getValue())->second.mValue;
}
char Globals::getType(std::string_view name) const
char Globals::getType(GlobalVariableName name) const
{
Collection::const_iterator iter = mVariables.find(name);
Collection::const_iterator iter = mVariables.find(name.getValue());
if (iter == mVariables.end())
return ' ';

@ -1,15 +1,16 @@
#ifndef GAME_MWWORLD_GLOBALS_H
#define GAME_MWWORLD_GLOBALS_H
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <components/esm3/loadglob.hpp>
#include <components/misc/strings/algorithm.hpp>
#include "globalvariablename.hpp"
namespace ESM
{
class ESMWriter;
@ -38,11 +39,27 @@ namespace MWWorld
Collection::iterator find(std::string_view name);
public:
const ESM::Variant& operator[](std::string_view name) const;
ESM::Variant& operator[](std::string_view name);
char getType(std::string_view name) const;
static constexpr GlobalVariableName sDaysPassed{ "dayspassed" };
static constexpr GlobalVariableName sGameHour{ "gamehour" };
static constexpr GlobalVariableName sDay{ "day" };
static constexpr GlobalVariableName sMonth{ "month" };
static constexpr GlobalVariableName sYear{ "year" };
static constexpr GlobalVariableName sTimeScale{ "timescale" };
static constexpr GlobalVariableName sCharGenState{ "chargenstate" };
static constexpr GlobalVariableName sPCHasCrimeGold{ "pchascrimegold" };
static constexpr GlobalVariableName sPCHasGoldDiscount{ "pchasgolddiscount" };
static constexpr GlobalVariableName sCrimeGoldDiscount{ "crimegolddiscount" };
static constexpr GlobalVariableName sCrimeGoldTurnIn{ "crimegoldturnin" };
static constexpr GlobalVariableName sPCHasTurnIn{ "pchasturnin" };
static constexpr GlobalVariableName sPCKnownWerewolf{ "pcknownwerewolf" };
static constexpr GlobalVariableName sWerewolfClawMult{ "werewolfclawmult" };
static constexpr GlobalVariableName sPCRace{ "pcrace" };
const ESM::Variant& operator[](GlobalVariableName name) const;
ESM::Variant& operator[](GlobalVariableName name);
char getType(GlobalVariableName name) const;
///< If there is no global variable with this name, ' ' is returned.
void fill(const MWWorld::ESMStore& store);

@ -0,0 +1,44 @@
#ifndef OPENMW_MWWORLD_GLOBALVARIABLENAME_H
#define OPENMW_MWWORLD_GLOBALVARIABLENAME_H
#include <cstddef>
#include <string>
#include <string_view>
namespace MWWorld
{
class Globals;
class GlobalVariableName
{
public:
GlobalVariableName(const std::string& value)
: mValue(value)
{
}
GlobalVariableName(std::string_view value)
: mValue(value)
{
}
std::string_view getValue() const { return mValue; }
friend bool operator==(const GlobalVariableName& lhs, const GlobalVariableName& rhs) noexcept
{
return lhs.mValue == rhs.mValue;
}
private:
std::string_view mValue;
explicit constexpr GlobalVariableName(const char* value)
: mValue(value)
{
}
friend Globals;
};
}
#endif

@ -1,6 +1,7 @@
#include "worldimp.hpp"
#include <charconv>
#include <vector>
#include <osg/ComputeBoundsVisitor>
#include <osg/Group>
@ -256,10 +257,10 @@ namespace MWWorld
if (!bypass)
{
// set new game mark
mGlobalVariables["chargenstate"].setInteger(1);
mGlobalVariables[Globals::sCharGenState].setInteger(1);
}
else
mGlobalVariables["chargenstate"].setInteger(-1);
mGlobalVariables[Globals::sCharGenState].setInteger(-1);
if (bypass && !mStartCell.empty())
{
@ -488,31 +489,29 @@ namespace MWWorld
}
}
std::map<std::string, ESM::Variant> globals;
// vanilla Morrowind does not define dayspassed.
globals["dayspassed"] = ESM::Variant(1); // but the addons start counting at 1 :(
globals["werewolfclawmult"] = ESM::Variant(25.f);
globals["pcknownwerewolf"] = ESM::Variant(0);
// following should exist in all versions of MW, but not necessarily in TCs
globals["gamehour"] = ESM::Variant(0.f);
globals["timescale"] = ESM::Variant(30.f);
globals["day"] = ESM::Variant(1);
globals["month"] = ESM::Variant(1);
globals["year"] = ESM::Variant(1);
globals["pcrace"] = ESM::Variant(0);
globals["pchascrimegold"] = ESM::Variant(0);
globals["pchasgolddiscount"] = ESM::Variant(0);
globals["crimegolddiscount"] = ESM::Variant(0);
globals["crimegoldturnin"] = ESM::Variant(0);
globals["pchasturnin"] = ESM::Variant(0);
const std::vector<std::pair<GlobalVariableName, ESM::Variant>> globals{
// vanilla Morrowind does not define dayspassed.
{ Globals::sDaysPassed, ESM::Variant(1) }, // but the addons start counting at 1 :(
{ Globals::sWerewolfClawMult, ESM::Variant(25.f) },
{ Globals::sPCKnownWerewolf, ESM::Variant(0) },
// following should exist in all versions of MW, but not necessarily in TCs
{ Globals::sGameHour, ESM::Variant(0) },
{ Globals::sTimeScale, ESM::Variant(30.f) },
{ Globals::sDay, ESM::Variant(1) },
{ Globals::sYear, ESM::Variant(1) },
{ Globals::sPCRace, ESM::Variant(0) },
{ Globals::sPCHasCrimeGold, ESM::Variant(0) },
{ Globals::sCrimeGoldDiscount, ESM::Variant(0) },
{ Globals::sCrimeGoldTurnIn, ESM::Variant(0) },
{ Globals::sPCHasTurnIn, ESM::Variant(0) },
};
for (const auto& params : globals)
{
if (!mStore.get<ESM::Global>().search(ESM::RefId::stringRefId(params.first)))
if (!mStore.get<ESM::Global>().search(ESM::RefId::stringRefId(params.first.getValue())))
{
ESM::Global record;
record.mId = ESM::RefId::stringRefId(params.first);
record.mId = ESM::RefId::stringRefId(params.first.getValue());
record.mValue = params.second;
record.mRecordFlags = 0;
mStore.insertStatic(record);
@ -591,7 +590,7 @@ namespace MWWorld
return mLocalScripts;
}
void World::setGlobalInt(std::string_view name, int value)
void World::setGlobalInt(GlobalVariableName name, int value)
{
bool dateUpdated = mCurrentDate->updateGlobalInt(name, value);
if (dateUpdated)
@ -600,7 +599,7 @@ namespace MWWorld
mGlobalVariables[name].setInteger(value);
}
void World::setGlobalFloat(std::string_view name, float value)
void World::setGlobalFloat(GlobalVariableName name, float value)
{
bool dateUpdated = mCurrentDate->updateGlobalFloat(name, value);
if (dateUpdated)
@ -609,17 +608,17 @@ namespace MWWorld
mGlobalVariables[name].setFloat(value);
}
int World::getGlobalInt(std::string_view name) const
int World::getGlobalInt(GlobalVariableName name) const
{
return mGlobalVariables[name].getInteger();
}
float World::getGlobalFloat(std::string_view name) const
float World::getGlobalFloat(GlobalVariableName name) const
{
return mGlobalVariables[name].getFloat();
}
char World::getGlobalVariableType(std::string_view name) const
char World::getGlobalVariableType(GlobalVariableName name) const
{
return mGlobalVariables.getType(name);
}
@ -3619,13 +3618,13 @@ namespace MWWorld
turnIn = std::max(1, turnIn);
}
mGlobalVariables["pchascrimegold"].setInteger((bounty <= playerGold) ? 1 : 0);
mGlobalVariables[Globals::sPCHasCrimeGold].setInteger((bounty <= playerGold) ? 1 : 0);
mGlobalVariables["pchasgolddiscount"].setInteger((discount <= playerGold) ? 1 : 0);
mGlobalVariables["crimegolddiscount"].setInteger(discount);
mGlobalVariables[Globals::sPCHasGoldDiscount].setInteger((discount <= playerGold) ? 1 : 0);
mGlobalVariables[Globals::sCrimeGoldDiscount].setInteger(discount);
mGlobalVariables["crimegoldturnin"].setInteger(turnIn);
mGlobalVariables["pchasturnin"].setInteger((turnIn <= playerGold) ? 1 : 0);
mGlobalVariables[Globals::sCrimeGoldTurnIn].setInteger(turnIn);
mGlobalVariables[Globals::sPCHasTurnIn].setInteger((turnIn <= playerGold) ? 1 : 0);
}
void World::confiscateStolenItems(const Ptr& ptr)

@ -249,19 +249,19 @@ namespace MWWorld
void getDoorMarkers(MWWorld::CellStore* cell, std::vector<DoorMarker>& out) override;
///< get a list of teleport door markers for a given cell, to be displayed on the local map
void setGlobalInt(std::string_view name, int value) override;
void setGlobalInt(GlobalVariableName name, int value) override;
///< Set value independently from real type.
void setGlobalFloat(std::string_view name, float value) override;
void setGlobalFloat(GlobalVariableName name, float value) override;
///< Set value independently from real type.
int getGlobalInt(std::string_view name) const override;
int getGlobalInt(GlobalVariableName name) const override;
///< Get value independently from real type.
float getGlobalFloat(std::string_view name) const override;
float getGlobalFloat(GlobalVariableName name) const override;
///< Get value independently from real type.
char getGlobalVariableType(std::string_view name) const override;
char getGlobalVariableType(GlobalVariableName name) const override;
///< Return ' ', if there is no global variable with this name.
std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const override;

Loading…
Cancel
Save