Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ddf44ab

Browse files
HybridDogSmallJoker
authored andcommittedMar 31, 2018
Allow changing the velocity of objects relatively (#3208)
Allow changing the velocity of objects relatively to their current velocity
1 parent 0a8ca59 commit ddf44ab

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed
 

‎builtin/game/falling.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ core.register_entity(":__builtin:falling_node", {
3939

4040
on_activate = function(self, staticdata)
4141
self.object:set_armor_groups({immortal = 1})
42-
42+
4343
local ds = core.deserialize(staticdata)
4444
if ds and ds.node then
4545
self:set_node(ds.node, ds.meta)

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,10 @@ This is basically a reference to a C++ `ServerActiveObject`
40294029
##### LuaEntitySAO-only (no-op for other objects)
40304030
* `set_velocity(vel)`
40314031
* `vel` is a vector, e.g. `{x=0.0, y=2.3, z=1.0}`
4032+
* `add_velocity(vel)`
4033+
* `vel` is a vector, e.g. `{x=0.0, y=2.3, z=1.0}`
4034+
* In comparison to using get_velocity, adding the velocity and then using
4035+
set_velocity, add_velocity is supposed to avoid synchronization problems.
40324036
* `get_velocity()`: returns the velocity, a vector
40334037
* `set_acceleration(acc)`
40344038
* `acc` is a vector

‎src/content_sao.h

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ class LuaEntitySAO : public UnitSAO
121121
s16 getHP() const;
122122
/* LuaEntitySAO-specific */
123123
void setVelocity(v3f velocity);
124+
void addVelocity(v3f velocity)
125+
{
126+
m_velocity += velocity;
127+
}
124128
v3f getVelocity();
125129
void setAcceleration(v3f acceleration);
126130
v3f getAcceleration();

‎src/script/lua_api/l_object.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,20 @@ int ObjectRef::l_set_velocity(lua_State *L)
850850
return 0;
851851
}
852852

853+
// add_velocity(self, {x=num, y=num, z=num})
854+
int ObjectRef::l_add_velocity(lua_State *L)
855+
{
856+
NO_MAP_LOCK_REQUIRED;
857+
ObjectRef *ref = checkobject(L, 1);
858+
LuaEntitySAO *co = getluaobject(ref);
859+
if (!co)
860+
return 0;
861+
v3f pos = checkFloatPos(L, 2);
862+
// Do it
863+
co->addVelocity(pos);
864+
return 0;
865+
}
866+
853867
// get_velocity(self)
854868
int ObjectRef::l_get_velocity(lua_State *L)
855869
{
@@ -1840,6 +1854,7 @@ const luaL_Reg ObjectRef::methods[] = {
18401854
luamethod(ObjectRef, get_nametag_attributes),
18411855
// LuaEntitySAO-only
18421856
luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1857+
luamethod(ObjectRef, add_velocity),
18431858
luamethod_aliased(ObjectRef, get_velocity, getvelocity),
18441859
luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
18451860
luamethod_aliased(ObjectRef, get_acceleration, getacceleration),

‎src/script/lua_api/l_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class ObjectRef : public ModApiBase {
161161
// set_velocity(self, {x=num, y=num, z=num})
162162
static int l_set_velocity(lua_State *L);
163163

164+
// add_velocity(self, {x=num, y=num, z=num})
165+
static int l_add_velocity(lua_State *L);
166+
164167
// get_velocity(self)
165168
static int l_get_velocity(lua_State *L);
166169

0 commit comments

Comments
 (0)
Please sign in to comment.