12 changed files with 139 additions and 75 deletions
@ -0,0 +1,86 @@ |
|||||||
|
#include "lua/lua_event.hpp" |
||||||
|
|
||||||
|
#include <optional> |
||||||
|
#include <string_view> |
||||||
|
#include <utility> |
||||||
|
|
||||||
|
#include <sol/sol.hpp> |
||||||
|
|
||||||
|
#include "lua/lua_global.hpp" |
||||||
|
#include "monster.h" |
||||||
|
#include "player.h" |
||||||
|
#include "utils/log.hpp" |
||||||
|
|
||||||
|
namespace devilution { |
||||||
|
|
||||||
|
namespace lua { |
||||||
|
|
||||||
|
template <typename... Args> |
||||||
|
void CallLuaEvent(std::string_view name, Args &&...args) |
||||||
|
{ |
||||||
|
sol::table *events = GetLuaEvents(); |
||||||
|
if (events == nullptr) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const auto trigger = events->traverse_get<std::optional<sol::object>>(name, "trigger"); |
||||||
|
if (!trigger.has_value() || !trigger->is<sol::protected_function>()) { |
||||||
|
LogError("events.{}.trigger is not a function", name); |
||||||
|
return; |
||||||
|
} |
||||||
|
const sol::protected_function fn = trigger->as<sol::protected_function>(); |
||||||
|
SafeCallResult(fn(std::forward<Args>(args)...), /*optional=*/true); |
||||||
|
} |
||||||
|
|
||||||
|
void MonsterDataLoaded() |
||||||
|
{ |
||||||
|
CallLuaEvent("MonsterDataLoaded"); |
||||||
|
} |
||||||
|
void UniqueMonsterDataLoaded() |
||||||
|
{ |
||||||
|
CallLuaEvent("UniqueMonsterDataLoaded"); |
||||||
|
} |
||||||
|
void ItemDataLoaded() |
||||||
|
{ |
||||||
|
CallLuaEvent("ItemDataLoaded"); |
||||||
|
} |
||||||
|
void UniqueItemDataLoaded() |
||||||
|
{ |
||||||
|
CallLuaEvent("UniqueItemDataLoaded"); |
||||||
|
} |
||||||
|
|
||||||
|
void StoreOpened(std::string_view name) |
||||||
|
{ |
||||||
|
CallLuaEvent("StoreOpened", name); |
||||||
|
} |
||||||
|
|
||||||
|
void OnMonsterTakeDamage(const Monster *monster, int damage, int damageType) |
||||||
|
{ |
||||||
|
CallLuaEvent("OnMonsterTakeDamage", monster, damage, damageType); |
||||||
|
} |
||||||
|
|
||||||
|
void OnPlayerGainExperience(const Player *player, uint32_t exp) |
||||||
|
{ |
||||||
|
CallLuaEvent("OnPlayerGainExperience", player, exp); |
||||||
|
} |
||||||
|
void OnPlayerTakeDamage(const Player *player, int damage, int damageType) |
||||||
|
{ |
||||||
|
CallLuaEvent("OnPlayerTakeDamage", player, damage, damageType); |
||||||
|
} |
||||||
|
|
||||||
|
void LoadModsComplete() |
||||||
|
{ |
||||||
|
CallLuaEvent("LoadModsComplete"); |
||||||
|
} |
||||||
|
void GameDrawComplete() |
||||||
|
{ |
||||||
|
CallLuaEvent("GameDrawComplete"); |
||||||
|
} |
||||||
|
void GameStart() |
||||||
|
{ |
||||||
|
CallLuaEvent("GameStart"); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace lua
|
||||||
|
|
||||||
|
} // namespace devilution
|
||||||
@ -1,14 +1,31 @@ |
|||||||
#pragma once |
#pragma once |
||||||
|
|
||||||
|
#include <cstdint> |
||||||
#include <string_view> |
#include <string_view> |
||||||
|
|
||||||
namespace devilution { |
namespace devilution { |
||||||
|
|
||||||
/**
|
struct Player; |
||||||
* @brief Triggers a Lua event by name. |
struct Monster; |
||||||
* This is a minimal header for code that only needs to trigger events. |
|
||||||
*/ |
namespace lua { |
||||||
void LuaEvent(std::string_view name); |
|
||||||
void LuaEvent(std::string_view name, std::string_view arg); |
void MonsterDataLoaded(); |
||||||
|
void UniqueMonsterDataLoaded(); |
||||||
|
void ItemDataLoaded(); |
||||||
|
void UniqueItemDataLoaded(); |
||||||
|
|
||||||
|
void StoreOpened(std::string_view name); |
||||||
|
|
||||||
|
void OnMonsterTakeDamage(const Monster *monster, int damage, int damageType); |
||||||
|
|
||||||
|
void OnPlayerGainExperience(const Player *player, uint32_t exp); |
||||||
|
void OnPlayerTakeDamage(const Player *player, int damage, int damageType); |
||||||
|
|
||||||
|
void LoadModsComplete(); |
||||||
|
void GameDrawComplete(); |
||||||
|
void GameStart(); |
||||||
|
|
||||||
|
} // namespace lua
|
||||||
|
|
||||||
} // namespace devilution
|
} // namespace devilution
|
||||||
|
|||||||
Loading…
Reference in new issue