1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

move logic to avoid need of public LuaABM definiton or reinterpret_cast

This commit is contained in:
SFENCE 2025-06-11 07:09:41 +02:00
parent 22fc23fc51
commit 636c603946
3 changed files with 93 additions and 111 deletions

View file

@ -17,8 +17,22 @@
LuaABM & LuaLBM LuaABM & LuaLBM
*/ */
LuaABM::LuaABM(int id, class LuaABM : public ActiveBlockModifier {
const std::string &name, private:
const int m_id;
std::string m_name;
std::vector<std::string> m_trigger_contents;
std::vector<std::string> m_required_neighbors;
std::vector<std::string> 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<std::string> &trigger_contents, const std::vector<std::string> &trigger_contents,
const std::vector<std::string> &required_neighbors, const std::vector<std::string> &required_neighbors,
const std::vector<std::string> &without_neighbors, const std::vector<std::string> &without_neighbors,
@ -34,51 +48,51 @@ LuaABM::LuaABM(int id,
m_simple_catch_up(simple_catch_up), m_simple_catch_up(simple_catch_up),
m_min_y(min_y), m_min_y(min_y),
m_max_y(max_y) m_max_y(max_y)
{ {
} }
const std::string &LuaABM::getName() const virtual const std::string &getName() const
{ {
return m_name; return m_name;
} }
const std::vector<std::string> &LuaABM::getTriggerContents() const virtual const std::vector<std::string> &getTriggerContents() const
{ {
return m_trigger_contents; return m_trigger_contents;
} }
const std::vector<std::string> &LuaABM::getRequiredNeighbors() const virtual const std::vector<std::string> &getRequiredNeighbors() const
{ {
return m_required_neighbors; return m_required_neighbors;
} }
const std::vector<std::string> &LuaABM::getWithoutNeighbors() const virtual const std::vector<std::string> &getWithoutNeighbors() const
{ {
return m_without_neighbors; return m_without_neighbors;
} }
float LuaABM::getTriggerInterval() virtual float getTriggerInterval()
{ {
return m_trigger_interval; return m_trigger_interval;
} }
u32 LuaABM::getTriggerChance() virtual u32 getTriggerChance()
{ {
return m_trigger_chance; return m_trigger_chance;
} }
bool LuaABM::getSimpleCatchUp() virtual bool getSimpleCatchUp()
{ {
return m_simple_catch_up; return m_simple_catch_up;
} }
s16 LuaABM::getMinY() virtual s16 getMinY()
{ {
return m_min_y; return m_min_y;
} }
s16 LuaABM::getMaxY() virtual s16 getMaxY()
{ {
return m_max_y; return m_max_y;
} }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
u32 active_object_count, u32 active_object_count_wider) u32 active_object_count, u32 active_object_count_wider)
{ {
auto *script = env->getScriptIface(); auto *script = env->getScriptIface();
script->triggerABM(m_id, p, n, active_object_count, active_object_count_wider); script->triggerABM(m_id, p, n, active_object_count, active_object_count_wider);
} }
};
class LuaLBM : public LoadingBlockModifierDef class LuaLBM : public LoadingBlockModifierDef
{ {
@ -184,6 +198,18 @@ bool ScriptApiEnv::read_nodenames(lua_State *L, int idx, std::vector<std::string
return true; return true;
} }
int ScriptApiEnv::override_abm(lua_State *L, ServerEnvironment *env)
{
int abm_id = lua_tonumber(L, 1);
LuaABM *abm = ScriptApiEnv::readABM(L, 2, abm_id);
if (abm != nullptr) {
env->replaceActiveBlockModifier(abm);
}
return 0;
}
LuaABM *ScriptApiEnv::readABM(lua_State *L, int abm_index, int id) LuaABM *ScriptApiEnv::readABM(lua_State *L, int abm_index, int id)
{ {
std::string name; std::string name;

View file

@ -13,49 +13,9 @@
class ServerEnvironment; class ServerEnvironment;
class MapBlock; class MapBlock;
class LuaABM;
struct ScriptCallbackState; struct ScriptCallbackState;
/*
LuaABM
*/
class LuaABM : public ActiveBlockModifier {
private:
const int m_id;
std::string m_name;
std::vector<std::string> m_trigger_contents;
std::vector<std::string> m_required_neighbors;
std::vector<std::string> 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<std::string> &trigger_contents,
const std::vector<std::string> &required_neighbors,
const std::vector<std::string> &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<std::string> &getTriggerContents() const;
virtual const std::vector<std::string> &getRequiredNeighbors() const;
virtual const std::vector<std::string> &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 class ScriptApiEnv : virtual public ScriptApiBase
{ {
public: public:
@ -92,8 +52,10 @@ public:
void triggerLBM(int id, MapBlock *block, void triggerLBM(int id, MapBlock *block,
const std::unordered_set<v3s16> &positions, float dtime_s); const std::unordered_set<v3s16> &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: private:
static LuaABM *readABM(lua_State *L, int abm_index, int id);
void readABMs(); void readABMs();
void readLBMs(); void readLBMs();

View file

@ -1388,14 +1388,8 @@ int ModApiEnv::l_override_abm(lua_State *L)
{ {
GET_ENV_PTR; GET_ENV_PTR;
int abm_id = lua_tonumber(L, 1); // redirect to s_env.cpp to avoid need of public LuaABM definiton or reinterpret_cast
return ScriptApiEnv::override_abm(L, env);
LuaABM *abm = ScriptApiEnv::readABM(L, 2, abm_id);
if (abm != nullptr) {
env->replaceActiveBlockModifier(abm);
}
return 0;
} }
void ModApiEnv::Initialize(lua_State *L, int top) void ModApiEnv::Initialize(lua_State *L, int top)