diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index 234e56990..40f0684d6 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -17,68 +17,82 @@ LuaABM & LuaLBM */ -LuaABM::LuaABM(int id, - const std::string &name, - const std::vector &trigger_contents, - const std::vector &required_neighbors, - const std::vector &without_neighbors, - float trigger_interval, u32 trigger_chance, bool simple_catch_up, - s16 min_y, s16 max_y): - m_id(id), - m_name(name), - m_trigger_contents(trigger_contents), - m_required_neighbors(required_neighbors), - m_without_neighbors(without_neighbors), - m_trigger_interval(trigger_interval), - m_trigger_chance(trigger_chance), - m_simple_catch_up(simple_catch_up), - m_min_y(min_y), - m_max_y(max_y) -{ -} -const std::string &LuaABM::getName() const -{ - return m_name; -} -const std::vector &LuaABM::getTriggerContents() const -{ - return m_trigger_contents; -} -const std::vector &LuaABM::getRequiredNeighbors() const -{ - return m_required_neighbors; -} -const std::vector &LuaABM::getWithoutNeighbors() const -{ - return m_without_neighbors; -} -float LuaABM::getTriggerInterval() -{ - return m_trigger_interval; -} -u32 LuaABM::getTriggerChance() -{ - return m_trigger_chance; -} -bool LuaABM::getSimpleCatchUp() -{ - return m_simple_catch_up; -} -s16 LuaABM::getMinY() -{ - return m_min_y; -} -s16 LuaABM::getMaxY() -{ - return m_max_y; -} +class LuaABM : public ActiveBlockModifier { +private: + const int m_id; -void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, - u32 active_object_count, u32 active_object_count_wider) -{ - auto *script = env->getScriptIface(); - script->triggerABM(m_id, p, n, active_object_count, active_object_count_wider); -} + std::string m_name; + std::vector m_trigger_contents; + std::vector m_required_neighbors; + std::vector m_without_neighbors; + float m_trigger_interval; + u32 m_trigger_chance; + bool m_simple_catch_up; + s16 m_min_y; + s16 m_max_y; +public: + LuaABM(int id, + const std::string name, + const std::vector &trigger_contents, + const std::vector &required_neighbors, + const std::vector &without_neighbors, + float trigger_interval, u32 trigger_chance, bool simple_catch_up, + s16 min_y, s16 max_y): + m_id(id), + m_name(name), + m_trigger_contents(trigger_contents), + m_required_neighbors(required_neighbors), + m_without_neighbors(without_neighbors), + m_trigger_interval(trigger_interval), + m_trigger_chance(trigger_chance), + m_simple_catch_up(simple_catch_up), + m_min_y(min_y), + m_max_y(max_y) + { + } + virtual const std::string &getName() const + { + return m_name; + } + virtual const std::vector &getTriggerContents() const + { + return m_trigger_contents; + } + virtual const std::vector &getRequiredNeighbors() const + { + return m_required_neighbors; + } + virtual const std::vector &getWithoutNeighbors() const + { + return m_without_neighbors; + } + virtual float getTriggerInterval() + { + return m_trigger_interval; + } + virtual u32 getTriggerChance() + { + return m_trigger_chance; + } + virtual bool getSimpleCatchUp() + { + return m_simple_catch_up; + } + virtual s16 getMinY() + { + return m_min_y; + } + virtual s16 getMaxY() + { + return m_max_y; + } + virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, + u32 active_object_count, u32 active_object_count_wider) + { + auto *script = env->getScriptIface(); + script->triggerABM(m_id, p, n, active_object_count, active_object_count_wider); + } +}; class LuaLBM : public LoadingBlockModifierDef { @@ -184,6 +198,18 @@ bool ScriptApiEnv::read_nodenames(lua_State *L, int idx, std::vectorreplaceActiveBlockModifier(abm); + } + + return 0; +} + LuaABM *ScriptApiEnv::readABM(lua_State *L, int abm_index, int id) { std::string name; diff --git a/src/script/cpp_api/s_env.h b/src/script/cpp_api/s_env.h index 65b33da66..6c1fc8272 100644 --- a/src/script/cpp_api/s_env.h +++ b/src/script/cpp_api/s_env.h @@ -13,49 +13,9 @@ class ServerEnvironment; class MapBlock; +class LuaABM; struct ScriptCallbackState; -/* - LuaABM -*/ - -class LuaABM : public ActiveBlockModifier { -private: - const int m_id; - - std::string m_name; - std::vector m_trigger_contents; - std::vector m_required_neighbors; - std::vector m_without_neighbors; - float m_trigger_interval; - u32 m_trigger_chance; - bool m_simple_catch_up; - s16 m_min_y; - s16 m_max_y; - -public: - LuaABM(int id, - const std::string &name, - const std::vector &trigger_contents, - const std::vector &required_neighbors, - const std::vector &without_neighbors, - float trigger_interval, u32 trigger_chance, bool simple_catch_up, - s16 min_y, s16 max_y); - - virtual const std::string &getName() const; - virtual const std::vector &getTriggerContents() const; - virtual const std::vector &getRequiredNeighbors() const; - virtual const std::vector &getWithoutNeighbors() const; - virtual float getTriggerInterval(); - virtual u32 getTriggerChance(); - virtual bool getSimpleCatchUp(); - virtual s16 getMinY(); - virtual s16 getMaxY(); - - virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, - u32 active_object_count, u32 active_object_count_wider); -}; - class ScriptApiEnv : virtual public ScriptApiBase { public: @@ -92,8 +52,10 @@ public: void triggerLBM(int id, MapBlock *block, const std::unordered_set &positions, float dtime_s); - static LuaABM *readABM(lua_State *L, int abm_index, int id); + static int override_abm(lua_State * L, ServerEnvironment *env); private: + static LuaABM *readABM(lua_State *L, int abm_index, int id); + void readABMs(); void readLBMs(); diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 281e22868..9cd103095 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -1388,14 +1388,8 @@ int ModApiEnv::l_override_abm(lua_State *L) { GET_ENV_PTR; - int abm_id = lua_tonumber(L, 1); - - LuaABM *abm = ScriptApiEnv::readABM(L, 2, abm_id); - if (abm != nullptr) { - env->replaceActiveBlockModifier(abm); - } - - return 0; + // redirect to s_env.cpp to avoid need of public LuaABM definiton or reinterpret_cast + return ScriptApiEnv::override_abm(L, env); } void ModApiEnv::Initialize(lua_State *L, int top)