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
|
|
|
|
2023-03-06 01:31:58 +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-03-06 01:31:58 +00:00
|
|
|
|
2023-06-06 16:34:25 +00:00
|
|
|
CachedStat(Setter setter, Index index, std::string_view prop)
|
2023-03-06 01:31:58 +00:00
|
|
|
: mSetter(setter)
|
2023-06-06 16:34:25 +00:00
|
|
|
, mIndex(std::move(index))
|
2023-03-06 01:31:58 +00:00
|
|
|
, 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);
|
|
|
|
}
|
2023-03-22 00:27:22 +00:00
|
|
|
|
|
|
|
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
|
2023-03-22 00:27:22 +00:00
|
|
|
std::string_view mProp; // Name of the stat's property
|
2023-03-06 01:31:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-18 20:09:01 +00:00
|
|
|
MWBase::LuaManager::ActorControls* getActorControls() { return &mData.mControls; }
|
2023-06-12 19:25:48 +00:00
|
|
|
const MWWorld::Ptr& getPtrOrEmpty() const { return mData.ptrOrEmpty(); }
|
2020-12-18 22:21:10 +00:00
|
|
|
|
2023-03-22 00:27:22 +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);
|
|
|
|
}
|
2024-01-15 20:48:19 +00:00
|
|
|
void onSkillLevelUp(std::string_view skillId, std::string_view source)
|
|
|
|
{
|
|
|
|
callEngineHandlers(mOnSkillLevelUp, skillId, source);
|
|
|
|
}
|
2021-04-21 02:11:11 +00:00
|
|
|
|
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
|
|
|
|
2021-04-13 22:00:51 +00:00
|
|
|
private:
|
|
|
|
EngineHandlerList mOnActiveHandlers{ "onActive" };
|
|
|
|
EngineHandlerList mOnInactiveHandlers{ "onInactive" };
|
2021-04-21 02:11:11 +00:00
|
|
|
EngineHandlerList mOnConsumeHandlers{ "onConsume" };
|
2022-02-01 23:42:56 +00:00
|
|
|
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" };
|
2024-01-15 20:48:19 +00:00
|
|
|
EngineHandlerList mOnSkillLevelUp{ "_onSkillLevelUp" };
|
2020-12-18 22:21:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MWLUA_LOCALSCRIPTS_H
|