From 93eb0794d6f7366df5fb375855b2e5e5888304c6 Mon Sep 17 00:00:00 2001 From: you Date: Sat, 31 Mar 2018 14:48:38 +0200 Subject: [PATCH] ObjectRef: Add add_velocity() (#3208) Allow changing the velocity of objects relatively to their current velocity --- builtin/game/falling.lua | 2 +- doc/lua_api.txt | 4 ++++ src/content_sao.h | 4 ++++ src/script/lua_api/l_object.cpp | 15 +++++++++++++++ src/script/lua_api/l_object.h | 3 +++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/builtin/game/falling.lua b/builtin/game/falling.lua index 974ba3959..4ebe39f5f 100644 --- a/builtin/game/falling.lua +++ b/builtin/game/falling.lua @@ -39,7 +39,7 @@ core.register_entity(":__builtin:falling_node", { on_activate = function(self, staticdata) self.object:set_armor_groups({immortal = 1}) - + local ds = core.deserialize(staticdata) if ds and ds.node then self:set_node(ds.node, ds.meta) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 69c4b3262..00f5e3f71 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -4029,6 +4029,10 @@ This is basically a reference to a C++ `ServerActiveObject` ##### LuaEntitySAO-only (no-op for other objects) * `set_velocity(vel)` * `vel` is a vector, e.g. `{x=0.0, y=2.3, z=1.0}` +* `add_velocity(vel)` + * `vel` is a vector, e.g. `{x=0.0, y=2.3, z=1.0}` + * In comparison to using get_velocity, adding the velocity and then using + set_velocity, add_velocity is supposed to avoid synchronization problems. * `get_velocity()`: returns the velocity, a vector * `set_acceleration(acc)` * `acc` is a vector diff --git a/src/content_sao.h b/src/content_sao.h index 509f477dd..3f75a7890 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -121,6 +121,10 @@ public: s16 getHP() const; /* LuaEntitySAO-specific */ void setVelocity(v3f velocity); + void addVelocity(v3f velocity) + { + m_velocity += velocity; + } v3f getVelocity(); void setAcceleration(v3f acceleration); v3f getAcceleration(); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index e4c478df7..b3c3bdf66 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -850,6 +850,20 @@ int ObjectRef::l_set_velocity(lua_State *L) return 0; } +// add_velocity(self, {x=num, y=num, z=num}) +int ObjectRef::l_add_velocity(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + LuaEntitySAO *co = getluaobject(ref); + if (!co) + return 0; + v3f pos = checkFloatPos(L, 2); + // Do it + co->addVelocity(pos); + return 0; +} + // get_velocity(self) int ObjectRef::l_get_velocity(lua_State *L) { @@ -1840,6 +1854,7 @@ const luaL_Reg ObjectRef::methods[] = { luamethod(ObjectRef, get_nametag_attributes), // LuaEntitySAO-only luamethod_aliased(ObjectRef, set_velocity, setvelocity), + luamethod(ObjectRef, add_velocity), luamethod_aliased(ObjectRef, get_velocity, getvelocity), luamethod_aliased(ObjectRef, set_acceleration, setacceleration), luamethod_aliased(ObjectRef, get_acceleration, getacceleration), diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index 58cfe7146..21c215c3f 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -161,6 +161,9 @@ private: // set_velocity(self, {x=num, y=num, z=num}) static int l_set_velocity(lua_State *L); + // add_velocity(self, {x=num, y=num, z=num}) + static int l_add_velocity(lua_State *L); + // get_velocity(self) static int l_get_velocity(lua_State *L); -- 2.25.1