diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index e2bf81f2d..e2a463cac 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -441,7 +441,8 @@ static void load_script(const Content& content, T& def) { def.name, scriptfile, def.scriptFile, - def.rt.funcsset + def.rt.funcsset, + def.rt.eventNames ); } } diff --git a/src/items/ItemDef.hpp b/src/items/ItemDef.hpp index ee37215a6..3f16e04b1 100644 --- a/src/items/ItemDef.hpp +++ b/src/items/ItemDef.hpp @@ -15,6 +15,9 @@ struct ItemFuncsSet { bool on_block_break_by : 1; }; +struct ItemFuncNamesCache { +}; + enum class ItemIconType { NONE, // invisible (core:empty) must not be rendered SPRITE, // textured quad: icon is `atlas_name:texture_name` @@ -75,6 +78,8 @@ struct ItemDef { bool emissive = false; std::set tags; + + ItemFuncNamesCache eventNames; } rt {}; ItemDef(const std::string& name); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 4af1a4287..ca1ab3164 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -1,8 +1,5 @@ #include "scripting.hpp" -#include -#include - #include "scripting_commons.hpp" #include "content/Content.hpp" #include "content/ContentPack.hpp" @@ -28,6 +25,9 @@ #include "world/World.hpp" #include "interfaces/Process.hpp" +#include +#include + using namespace scripting; static debug::Logger logger("scripting"); @@ -391,15 +391,15 @@ void scripting::on_blocks_tick(const Block& block, int tps) { } void scripting::update_block(const Block& block, const glm::ivec3& pos) { - std::string name = block.name + ".update"; - lua::emit_event(lua::get_main_state(), name, [pos](auto L) { + lua::emit_event(lua::get_main_state(), block.rt.eventNames.update, + [pos](auto L) { return lua::pushivec_stack(L, pos); }); } void scripting::random_update_block(const Block& block, const glm::ivec3& pos) { - std::string name = block.name + ".randupdate"; - lua::emit_event(lua::get_main_state(), name, [pos](auto L) { + lua::emit_event(lua::get_main_state(), block.rt.eventNames.randomUpdate, + [pos](auto L) { return lua::pushivec_stack(L, pos); }); } @@ -660,7 +660,8 @@ void scripting::load_content_script( const std::string& prefix, const io::path& file, const std::string& fileName, - BlockFuncsSet& funcsset + BlockFuncsSet& funcsset, + BlockFuncNamesCache& namesCache ) { int env = *senv; lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName)); @@ -686,6 +687,9 @@ void scripting::load_content_script( register_event(env, "on_block_present", prefix + ".blockpresent"); funcsset.onblockremoved = register_event(env, "on_block_removed", prefix + ".blockremoved"); + + namesCache.randomUpdate = prefix + ".update"; + namesCache.randomUpdate = prefix + ".randupdate"; } void scripting::load_content_script( @@ -693,7 +697,8 @@ void scripting::load_content_script( const std::string& prefix, const io::path& file, const std::string& fileName, - ItemFuncsSet& funcsset + ItemFuncsSet& funcsset, + ItemFuncNamesCache& namesCache ) { int env = *senv; lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName)); diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 298130c99..64fa1a3d1 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -23,7 +23,9 @@ struct ItemDef; class Inventory; class UiDocument; struct BlockFuncsSet; +struct BlockFuncNamesCache; struct ItemFuncsSet; +struct ItemFuncNamesCache; struct WorldFuncsSet; struct UserComponent; struct uidocscript; @@ -168,7 +170,8 @@ namespace scripting { const std::string& prefix, const io::path& file, const std::string& fileName, - BlockFuncsSet& funcsset + BlockFuncsSet& funcsset, + BlockFuncNamesCache& namesCache ); /// @brief Load script associated with an Item @@ -182,7 +185,8 @@ namespace scripting { const std::string& prefix, const io::path& file, const std::string& fileName, - ItemFuncsSet& funcsset + ItemFuncsSet& funcsset, + ItemFuncNamesCache& namesCache ); /// @brief Load component script diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index 96620c8f9..99b9dee70 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -54,6 +54,11 @@ struct BlockFuncsSet { bool onblockremoved : 1; }; +struct BlockFuncNamesCache { + std::string update; + std::string randomUpdate; +}; + struct CoordSystem { std::array axes; /// @brief Grid 3d position fix offset (for negative vectors) @@ -296,6 +301,8 @@ public: blockid_t surfaceReplacement = 0; std::set tags; + + BlockFuncNamesCache eventNames; } rt {}; Block(const std::string& name);