1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-07 12:54:00 +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.

111 lines
3.7 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>
2023-06-06 16:34:25 +00:00
#include <utility>
2020-12-18 22:21:10 +00:00
#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:
2023-09-15 15:26:58 +00:00
using Index = std::variant<int, ESM::RefId, std::monostate>;
2023-06-06 16:34:25 +00:00
using Setter = void (*)(const Index&, std::string_view, const MWWorld::Ptr&, const sol::object&);
2023-06-06 16:34:25 +00:00
CachedStat(Setter setter, Index index, std::string_view prop)
: mSetter(setter)
2023-06-06 16:34:25 +00:00
, mIndex(std::move(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);
}
private:
Setter mSetter; // Function that updates a stat's property
2023-06-06 16:34:25 +00:00
Index mIndex; // Optional index to disambiguate the stat
std::string_view mProp; // Name of the stat's property
};
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& getPtrOrEmpty() const { return mData.ptrOrEmpty(); }
2020-12-18 22:21:10 +00:00
void setActive(bool active);
void onConsume(const LObject& consumable) { callEngineHandlers(mOnConsumeHandlers, consumable); }
void onActivated(const LObject& actor) { callEngineHandlers(mOnActivatedHandlers, actor); }
2023-06-18 19:19:34 +00:00
void onTeleported() { callEngineHandlers(mOnTeleportedHandlers); }
2024-01-26 21:39:33 +00:00
void onAnimationTextKey(std::string_view groupname, std::string_view key)
{
callEngineHandlers(mOnAnimationTextKeyHandlers, groupname, key);
}
void onPlayAnimation(std::string_view groupname, const sol::table& options)
{
callEngineHandlers(mOnPlayAnimationHandlers, groupname, options);
}
2024-01-14 19:33:23 +00:00
void onSkillUse(std::string_view skillId, int useType, float scale)
{
callEngineHandlers(mOnSkillUse, skillId, useType, scale);
}
void onSkillLevelUp(std::string_view skillId, std::string_view source)
{
callEngineHandlers(mOnSkillLevelUp, skillId, source);
}
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" };
2023-06-18 19:19:34 +00:00
EngineHandlerList mOnTeleportedHandlers{ "onTeleported" };
2024-01-26 21:39:33 +00:00
EngineHandlerList mOnAnimationTextKeyHandlers{ "_onAnimationTextKey" };
EngineHandlerList mOnPlayAnimationHandlers{ "_onPlayAnimation" };
2024-01-14 19:33:23 +00:00
EngineHandlerList mOnSkillUse{ "_onSkillUse" };
EngineHandlerList mOnSkillLevelUp{ "_onSkillLevelUp" };
2020-12-18 22:21:10 +00:00
};
}
#endif // MWLUA_LOCALSCRIPTS_H