ObjectRef: Add add_velocity() (#3208)
authoryou <ovvv@web.de>
Sat, 31 Mar 2018 12:48:38 +0000 (14:48 +0200)
committerSmallJoker <mk939@ymail.com>
Sat, 31 Mar 2018 12:50:17 +0000 (14:50 +0200)
Allow changing the velocity of objects relatively to their current velocity

builtin/game/falling.lua
doc/lua_api.txt
src/content_sao.h
src/script/lua_api/l_object.cpp
src/script/lua_api/l_object.h

index 974ba3959f747d7dcab6cdebd25bc7dd159957bf..4ebe39f5fcf8bee70cb9265d6937d30964706f28 100644 (file)
@@ -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)
index 69c4b32621b88947d7ec9c9e3e067bb9c983a520..00f5e3f71489592e7e474258e4183f14edec0f0f 100644 (file)
@@ -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
index 509f477dde0f60bfbb2e7c66bcace82fb00edbd2..3f75a789034d5a0a90a3cfba0493a09048dfed1e 100644 (file)
@@ -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();
index e4c478df702b1787c98c7daceed02090b9d9ecdc..b3c3bdf668fb2fe27a64e980ad48785ae604907e 100644 (file)
@@ -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),
index 58cfe7146c2b79222e2abbc8230642fffab9045c..21c215c3f8f5ca8c343f45466f8a87c26b91bce5 100644 (file)
@@ -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);