1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-08 09:37:53 +00:00
OpenMW/apps/openmw/mwlua/localscripts.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.6 KiB
C++
Raw Normal View History

2020-12-18 22:21:10 +00:00
#ifndef MWLUA_LOCALSCRIPTS_H
#define MWLUA_LOCALSCRIPTS_H
#include <memory>
#include <set>
#include <string>
#include <components/lua/luastate.hpp>
#include <components/lua/scriptscontainer.hpp>
#include "../mwbase/luamanager.hpp"
#include "object.hpp"
namespace MWLua
{
2022-08-17 17:09:38 +00:00
struct Context;
2020-12-18 22:21:10 +00:00
struct SelfObject : public LObject
{
class CachedStat
{
public:
using Setter = void (*)(int, std::string_view, const MWWorld::Ptr&, const sol::object&);
private:
Setter mSetter; // Function that updates a stat's property
int mIndex; // Optional index to disambiguate the stat
std::string_view mProp; // Name of the stat's property
public:
CachedStat(Setter setter, int index, std::string_view prop)
: mSetter(setter)
, mIndex(index)
, mProp(std::move(prop))
{
}
void operator()(const MWWorld::Ptr& ptr, const sol::object& object) const
{
mSetter(mIndex, mProp, ptr, object);
}
bool operator<(const CachedStat& other) const
{
return std::tie(mSetter, mIndex, mProp) < std::tie(other.mSetter, other.mIndex, other.mProp);
}
};
SelfObject(const LObject& obj)
: LObject(obj)
, mIsActive(false)
{
}
MWBase::LuaManager::ActorControls mControls;
std::map<CachedStat, sol::object> mStatsCache;
bool mIsActive;
};
2020-12-18 22:21:10 +00:00
class LocalScripts : public LuaUtil::ScriptsContainer
{
public:
static void initializeSelfPackage(const Context&);
2022-05-20 19:47:13 +00:00
LocalScripts(LuaUtil::LuaState* lua, const LObject& obj);
2020-12-18 22:21:10 +00:00
MWBase::LuaManager::ActorControls* getActorControls() { return &mData.mControls; }
const MWWorld::Ptr& getPtr() const { return mData.ptr(); }
2020-12-18 22:21:10 +00:00
struct OnActive
{
};
struct OnInactive
{
};
struct OnActivated
{
LObject mActivatingActor;
};
struct OnConsume
{
LObject mConsumable;
};
using EngineEvent = std::variant<OnActive, OnInactive, OnConsume, OnActivated>;
void receiveEngineEvent(const EngineEvent&);
2022-03-25 20:03:13 +00:00
void applyStatsCache();
2022-09-22 18:26:05 +00:00
2020-12-18 22:21:10 +00:00
protected:
SelfObject mData;
2021-04-23 00:49:12 +00:00
private:
EngineHandlerList mOnActiveHandlers{ "onActive" };
EngineHandlerList mOnInactiveHandlers{ "onInactive" };
EngineHandlerList mOnConsumeHandlers{ "onConsume" };
EngineHandlerList mOnActivatedHandlers{ "onActivated" };
2020-12-18 22:21:10 +00:00
};
}
#endif // MWLUA_LOCALSCRIPTS_H