Added get_player_velocity() method. Fixes #1176

This commit is contained in:
Elia Argentieri 2015-06-25 14:57:10 +02:00 committed by est31
parent 7bbb9b066a
commit 5ebb4237e2
3 changed files with 92 additions and 72 deletions

@ -2470,6 +2470,7 @@ This is basically a reference to a C++ `ServerActiveObject`
##### Player-only (no-op for other objects) ##### Player-only (no-op for other objects)
* `get_player_name()`: returns `""` if is not a player * `get_player_name()`: returns `""` if is not a player
* `get_player_velocity()`: returns `nil` if is not a player otherwise a table {x, y, z} representing the player's instantaneous velocity in nodes/s
* `get_look_dir()`: get camera direction as a unit vector * `get_look_dir()`: get camera direction as a unit vector
* `get_look_pitch()`: pitch in radians * `get_look_pitch()`: pitch in radians
* `get_look_yaw()`: yaw in radians (wraps around pretty randomly as of now) * `get_look_yaw()`: yaw in radians (wraps around pretty randomly as of now)

@ -80,7 +80,7 @@ ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
{ {
luaL_checktype(L, narg, LUA_TUSERDATA); luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className); void *ud = luaL_checkudata(L, narg, className);
if(!ud) luaL_typerror(L, narg, className); if (!ud) luaL_typerror(L, narg, className);
return *(ObjectRef**)ud; // unbox pointer return *(ObjectRef**)ud; // unbox pointer
} }
@ -93,9 +93,9 @@ ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref) LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
{ {
ServerActiveObject *obj = getobject(ref); ServerActiveObject *obj = getobject(ref);
if(obj == NULL) if (obj == NULL)
return NULL; return NULL;
if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY) if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
return NULL; return NULL;
return (LuaEntitySAO*)obj; return (LuaEntitySAO*)obj;
} }
@ -103,9 +103,9 @@ LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref) PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
{ {
ServerActiveObject *obj = getobject(ref); ServerActiveObject *obj = getobject(ref);
if(obj == NULL) if (obj == NULL)
return NULL; return NULL;
if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER) if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
return NULL; return NULL;
return (PlayerSAO*)obj; return (PlayerSAO*)obj;
} }
@ -113,7 +113,7 @@ PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
Player* ObjectRef::getplayer(ObjectRef *ref) Player* ObjectRef::getplayer(ObjectRef *ref)
{ {
PlayerSAO *playersao = getplayersao(ref); PlayerSAO *playersao = getplayersao(ref);
if(playersao == NULL) if (playersao == NULL)
return NULL; return NULL;
return playersao->getPlayer(); return playersao->getPlayer();
} }
@ -160,7 +160,7 @@ int ObjectRef::l_getpos(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
v3f pos = co->getBasePosition() / BS; v3f pos = co->getBasePosition() / BS;
lua_newtable(L); lua_newtable(L);
lua_pushnumber(L, pos.X); lua_pushnumber(L, pos.X);
@ -179,7 +179,7 @@ int ObjectRef::l_setpos(lua_State *L)
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
//LuaEntitySAO *co = getluaobject(ref); //LuaEntitySAO *co = getluaobject(ref);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// pos // pos
v3f pos = checkFloatPos(L, 2); v3f pos = checkFloatPos(L, 2);
// Do it // Do it
@ -194,7 +194,7 @@ int ObjectRef::l_moveto(lua_State *L)
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
//LuaEntitySAO *co = getluaobject(ref); //LuaEntitySAO *co = getluaobject(ref);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// pos // pos
v3f pos = checkFloatPos(L, 2); v3f pos = checkFloatPos(L, 2);
// continuous // continuous
@ -212,15 +212,15 @@ int ObjectRef::l_punch(lua_State *L)
ObjectRef *puncher_ref = checkobject(L, 2); ObjectRef *puncher_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
ServerActiveObject *puncher = getobject(puncher_ref); ServerActiveObject *puncher = getobject(puncher_ref);
if(co == NULL) return 0; if (co == NULL) return 0;
if(puncher == NULL) return 0; if (puncher == NULL) return 0;
v3f dir; v3f dir;
if(lua_type(L, 5) != LUA_TTABLE) if (lua_type(L, 5) != LUA_TTABLE)
dir = co->getBasePosition() - puncher->getBasePosition(); dir = co->getBasePosition() - puncher->getBasePosition();
else else
dir = read_v3f(L, 5); dir = read_v3f(L, 5);
float time_from_last_punch = 1000000; float time_from_last_punch = 1000000;
if(lua_isnumber(L, 3)) if (lua_isnumber(L, 3))
time_from_last_punch = lua_tonumber(L, 3); time_from_last_punch = lua_tonumber(L, 3);
ToolCapabilities toolcap = read_tool_capabilities(L, 4); ToolCapabilities toolcap = read_tool_capabilities(L, 4);
dir.normalize(); dir.normalize();
@ -253,8 +253,8 @@ int ObjectRef::l_right_click(lua_State *L)
ObjectRef *ref2 = checkobject(L, 2); ObjectRef *ref2 = checkobject(L, 2);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
ServerActiveObject *co2 = getobject(ref2); ServerActiveObject *co2 = getobject(ref2);
if(co == NULL) return 0; if (co == NULL) return 0;
if(co2 == NULL) return 0; if (co2 == NULL) return 0;
// Do it // Do it
co->rightClick(co2); co->rightClick(co2);
return 0; return 0;
@ -269,7 +269,7 @@ int ObjectRef::l_set_hp(lua_State *L)
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
luaL_checknumber(L, 2); luaL_checknumber(L, 2);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
int hp = lua_tonumber(L, 2); int hp = lua_tonumber(L, 2);
/*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId() /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
<<" hp="<<hp<<std::endl;*/ <<" hp="<<hp<<std::endl;*/
@ -290,7 +290,7 @@ int ObjectRef::l_get_hp(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL){ if (co == NULL) {
// Default hp is 1 // Default hp is 1
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
@ -309,10 +309,10 @@ int ObjectRef::l_get_inventory(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
InventoryLocation loc = co->getInventoryLocation(); InventoryLocation loc = co->getInventoryLocation();
if(getServer(L)->getInventory(loc) != NULL) if (getServer(L)->getInventory(loc) != NULL)
InvRef::create(L, loc); InvRef::create(L, loc);
else else
lua_pushnil(L); // An object may have no inventory (nil) lua_pushnil(L); // An object may have no inventory (nil)
@ -325,7 +325,7 @@ int ObjectRef::l_get_wield_list(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
lua_pushstring(L, co->getWieldList().c_str()); lua_pushstring(L, co->getWieldList().c_str());
return 1; return 1;
@ -337,7 +337,7 @@ int ObjectRef::l_get_wield_index(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
lua_pushinteger(L, co->getWieldIndex() + 1); lua_pushinteger(L, co->getWieldIndex() + 1);
return 1; return 1;
@ -349,7 +349,7 @@ int ObjectRef::l_get_wielded_item(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL){ if (co == NULL) {
// Empty ItemStack // Empty ItemStack
LuaItemStack::create(L, ItemStack()); LuaItemStack::create(L, ItemStack());
return 1; return 1;
@ -365,7 +365,7 @@ int ObjectRef::l_set_wielded_item(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
ItemStack item = read_item(L, 2, getServer(L)); ItemStack item = read_item(L, 2, getServer(L));
bool success = co->setWieldedItem(item); bool success = co->setWieldedItem(item);
@ -382,7 +382,7 @@ int ObjectRef::l_set_armor_groups(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
ItemGroupList groups; ItemGroupList groups;
read_groups(L, 2, groups); read_groups(L, 2, groups);
@ -410,7 +410,7 @@ int ObjectRef::l_set_physics_override(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
PlayerSAO *co = (PlayerSAO *) getobject(ref); PlayerSAO *co = (PlayerSAO *) getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
if (lua_istable(L, 2)) { if (lua_istable(L, 2)) {
co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed); co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
@ -421,15 +421,15 @@ int ObjectRef::l_set_physics_override(lua_State *L)
co->m_physics_override_sent = false; co->m_physics_override_sent = false;
} else { } else {
// old, non-table format // old, non-table format
if(!lua_isnil(L, 2)){ if (!lua_isnil(L, 2)) {
co->m_physics_override_speed = lua_tonumber(L, 2); co->m_physics_override_speed = lua_tonumber(L, 2);
co->m_physics_override_sent = false; co->m_physics_override_sent = false;
} }
if(!lua_isnil(L, 3)){ if (!lua_isnil(L, 3)) {
co->m_physics_override_jump = lua_tonumber(L, 3); co->m_physics_override_jump = lua_tonumber(L, 3);
co->m_physics_override_sent = false; co->m_physics_override_sent = false;
} }
if(!lua_isnil(L, 4)){ if (!lua_isnil(L, 4)) {
co->m_physics_override_gravity = lua_tonumber(L, 4); co->m_physics_override_gravity = lua_tonumber(L, 4);
co->m_physics_override_sent = false; co->m_physics_override_sent = false;
} }
@ -465,19 +465,19 @@ int ObjectRef::l_set_animation(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
v2f frames = v2f(1, 1); v2f frames = v2f(1, 1);
if(!lua_isnil(L, 2)) if (!lua_isnil(L, 2))
frames = read_v2f(L, 2); frames = read_v2f(L, 2);
float frame_speed = 15; float frame_speed = 15;
if(!lua_isnil(L, 3)) if (!lua_isnil(L, 3))
frame_speed = lua_tonumber(L, 3); frame_speed = lua_tonumber(L, 3);
float frame_blend = 0; float frame_blend = 0;
if(!lua_isnil(L, 4)) if (!lua_isnil(L, 4))
frame_blend = lua_tonumber(L, 4); frame_blend = lua_tonumber(L, 4);
bool frame_loop = true; bool frame_loop = true;
if(lua_isboolean(L, 5)) if (lua_isboolean(L, 5))
frame_loop = lua_toboolean(L, 5); frame_loop = lua_toboolean(L, 5);
co->setAnimation(frames, frame_speed, frame_blend, frame_loop); co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
return 0; return 0;
@ -516,11 +516,11 @@ int ObjectRef::l_set_local_animation(lua_State *L)
// Do it // Do it
v2s32 frames[4]; v2s32 frames[4];
for (int i=0;i<4;i++) { for (int i=0;i<4;i++) {
if(!lua_isnil(L, 2+1)) if (!lua_isnil(L, 2+1))
frames[i] = read_v2s32(L, 2+i); frames[i] = read_v2s32(L, 2+i);
} }
float frame_speed = 30; float frame_speed = 30;
if(!lua_isnil(L, 6)) if (!lua_isnil(L, 6))
frame_speed = lua_tonumber(L, 6); frame_speed = lua_tonumber(L, 6);
if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed)) if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
@ -563,9 +563,9 @@ int ObjectRef::l_set_eye_offset(lua_State *L)
v3f offset_first = v3f(0, 0, 0); v3f offset_first = v3f(0, 0, 0);
v3f offset_third = v3f(0, 0, 0); v3f offset_third = v3f(0, 0, 0);
if(!lua_isnil(L, 2)) if (!lua_isnil(L, 2))
offset_first = read_v3f(L, 2); offset_first = read_v3f(L, 2);
if(!lua_isnil(L, 3)) if (!lua_isnil(L, 3))
offset_third = read_v3f(L, 3); offset_third = read_v3f(L, 3);
// Prevent abuse of offset values (keep player always visible) // Prevent abuse of offset values (keep player always visible)
@ -601,16 +601,16 @@ int ObjectRef::l_set_bone_position(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
std::string bone = ""; std::string bone = "";
if(!lua_isnil(L, 2)) if (!lua_isnil(L, 2))
bone = lua_tostring(L, 2); bone = lua_tostring(L, 2);
v3f position = v3f(0, 0, 0); v3f position = v3f(0, 0, 0);
if(!lua_isnil(L, 3)) if (!lua_isnil(L, 3))
position = read_v3f(L, 3); position = read_v3f(L, 3);
v3f rotation = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0);
if(!lua_isnil(L, 4)) if (!lua_isnil(L, 4))
rotation = read_v3f(L, 4); rotation = read_v3f(L, 4);
co->setBonePosition(bone, position, rotation); co->setBonePosition(bone, position, rotation);
return 0; return 0;
@ -626,7 +626,7 @@ int ObjectRef::l_get_bone_position(lua_State *L)
return 0; return 0;
// Do it // Do it
std::string bone = ""; std::string bone = "";
if(!lua_isnil(L, 2)) if (!lua_isnil(L, 2))
bone = lua_tostring(L, 2); bone = lua_tostring(L, 2);
v3f position = v3f(0, 0, 0); v3f position = v3f(0, 0, 0);
@ -738,9 +738,9 @@ int ObjectRef::l_set_properties(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref); ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
ObjectProperties *prop = co->accessObjectProperties(); ObjectProperties *prop = co->accessObjectProperties();
if(!prop) if (!prop)
return 0; return 0;
read_object_properties(L, 2, prop); read_object_properties(L, 2, prop);
co->notifyObjectPropertiesModified(); co->notifyObjectPropertiesModified();
@ -780,7 +780,7 @@ int ObjectRef::l_setvelocity(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
v3f pos = checkFloatPos(L, 2); v3f pos = checkFloatPos(L, 2);
// Do it // Do it
co->setVelocity(pos); co->setVelocity(pos);
@ -793,7 +793,7 @@ int ObjectRef::l_getvelocity(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
v3f v = co->getVelocity(); v3f v = co->getVelocity();
pushFloatPos(L, v); pushFloatPos(L, v);
@ -806,7 +806,7 @@ int ObjectRef::l_setacceleration(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// pos // pos
v3f pos = checkFloatPos(L, 2); v3f pos = checkFloatPos(L, 2);
// Do it // Do it
@ -820,7 +820,7 @@ int ObjectRef::l_getacceleration(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
v3f v = co->getAcceleration(); v3f v = co->getAcceleration();
pushFloatPos(L, v); pushFloatPos(L, v);
@ -833,7 +833,7 @@ int ObjectRef::l_setyaw(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
float yaw = luaL_checknumber(L, 2) * core::RADTODEG; float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
// Do it // Do it
co->setYaw(yaw); co->setYaw(yaw);
@ -846,7 +846,7 @@ int ObjectRef::l_getyaw(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
float yaw = co->getYaw() * core::DEGTORAD; float yaw = co->getYaw() * core::DEGTORAD;
lua_pushnumber(L, yaw); lua_pushnumber(L, yaw);
@ -859,7 +859,7 @@ int ObjectRef::l_settexturemod(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
std::string mod = luaL_checkstring(L, 2); std::string mod = luaL_checkstring(L, 2);
co->setTextureMod(mod); co->setTextureMod(mod);
@ -873,19 +873,19 @@ int ObjectRef::l_setsprite(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
v2s16 p(0,0); v2s16 p(0,0);
if(!lua_isnil(L, 2)) if (!lua_isnil(L, 2))
p = read_v2s16(L, 2); p = read_v2s16(L, 2);
int num_frames = 1; int num_frames = 1;
if(!lua_isnil(L, 3)) if (!lua_isnil(L, 3))
num_frames = lua_tonumber(L, 3); num_frames = lua_tonumber(L, 3);
float framelength = 0.2; float framelength = 0.2;
if(!lua_isnil(L, 4)) if (!lua_isnil(L, 4))
framelength = lua_tonumber(L, 4); framelength = lua_tonumber(L, 4);
bool select_horiz_by_yawpitch = false; bool select_horiz_by_yawpitch = false;
if(!lua_isnil(L, 5)) if (!lua_isnil(L, 5))
select_horiz_by_yawpitch = lua_toboolean(L, 5); select_horiz_by_yawpitch = lua_toboolean(L, 5);
co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch); co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
return 0; return 0;
@ -899,7 +899,7 @@ int ObjectRef::l_get_entity_name(lua_State *L)
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
log_deprecated(L,"Deprecated call to \"get_entity_name"); log_deprecated(L,"Deprecated call to \"get_entity_name");
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
std::string name = co->getName(); std::string name = co->getName();
lua_pushstring(L, name.c_str()); lua_pushstring(L, name.c_str());
@ -912,7 +912,7 @@ int ObjectRef::l_get_luaentity(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref); LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
luaentity_get(L, co->getId()); luaentity_get(L, co->getId());
return 1; return 1;
@ -936,7 +936,7 @@ int ObjectRef::l_get_player_name(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL){ if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
} }
@ -945,13 +945,28 @@ int ObjectRef::l_get_player_name(lua_State *L)
return 1; return 1;
} }
// get_player_velocity(self)
int ObjectRef::l_get_player_velocity(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if (player == NULL) {
lua_pushnil(L);
return 1;
}
// Do it
push_v3f(L, player->getSpeed() / BS);
return 1;
}
// get_look_dir(self) // get_look_dir(self)
int ObjectRef::l_get_look_dir(lua_State *L) int ObjectRef::l_get_look_dir(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
float pitch = player->getRadPitch(); float pitch = player->getRadPitch();
float yaw = player->getRadYaw(); float yaw = player->getRadYaw();
@ -966,7 +981,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadPitch()); lua_pushnumber(L, player->getRadPitch());
return 1; return 1;
@ -978,7 +993,7 @@ int ObjectRef::l_get_look_yaw(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL) return 0; if (player == NULL) return 0;
// Do it // Do it
lua_pushnumber(L, player->getRadYaw()); lua_pushnumber(L, player->getRadYaw());
return 1; return 1;
@ -990,7 +1005,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref); PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
float pitch = luaL_checknumber(L, 2) * core::RADTODEG; float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
// Do it // Do it
co->setPitch(pitch); co->setPitch(pitch);
@ -1003,7 +1018,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref); PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
float yaw = luaL_checknumber(L, 2) * core::RADTODEG; float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
// Do it // Do it
co->setYaw(yaw); co->setYaw(yaw);
@ -1016,7 +1031,7 @@ int ObjectRef::l_set_breath(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref); PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
u16 breath = luaL_checknumber(L, 2); u16 breath = luaL_checknumber(L, 2);
// Do it // Do it
co->setBreath(breath); co->setBreath(breath);
@ -1034,7 +1049,7 @@ int ObjectRef::l_get_breath(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref); PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0; if (co == NULL) return 0;
// Do it // Do it
u16 breath = co->getBreath(); u16 breath = co->getBreath();
lua_pushinteger (L, breath); lua_pushinteger (L, breath);
@ -1047,7 +1062,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL) return 0; if (player == NULL) return 0;
std::string formspec = luaL_checkstring(L, 2); std::string formspec = luaL_checkstring(L, 2);
player->inventory_formspec = formspec; player->inventory_formspec = formspec;
@ -1062,7 +1077,7 @@ int ObjectRef::l_get_inventory_formspec(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL) return 0; if (player == NULL) return 0;
std::string formspec = player->inventory_formspec; std::string formspec = player->inventory_formspec;
lua_pushlstring(L, formspec.c_str(), formspec.size()); lua_pushlstring(L, formspec.c_str(), formspec.size());
@ -1075,7 +1090,7 @@ int ObjectRef::l_get_player_control(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL){ if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
} }
@ -1109,7 +1124,7 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL){ if (player == NULL) {
lua_pushlstring(L, "", 0); lua_pushlstring(L, "", 0);
return 1; return 1;
} }
@ -1533,7 +1548,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
bool do_override = false; bool do_override = false;
float ratio = 0.0f; float ratio = 0.0f;
if (!lua_isnil(L, 2)){ if (!lua_isnil(L, 2)) {
do_override = true; do_override = true;
ratio = luaL_checknumber(L, 2); ratio = luaL_checknumber(L, 2);
} }
@ -1612,7 +1627,7 @@ ObjectRef::ObjectRef(ServerActiveObject *object):
ObjectRef::~ObjectRef() ObjectRef::~ObjectRef()
{ {
/*if(m_object) /*if (m_object)
infostream<<"ObjectRef destructing for id=" infostream<<"ObjectRef destructing for id="
<<m_object->getId()<<std::endl; <<m_object->getId()<<std::endl;
else else
@ -1706,6 +1721,7 @@ const luaL_reg ObjectRef::methods[] = {
luamethod(ObjectRef, is_player), luamethod(ObjectRef, is_player),
luamethod(ObjectRef, is_player_connected), luamethod(ObjectRef, is_player_connected),
luamethod(ObjectRef, get_player_name), luamethod(ObjectRef, get_player_name),
luamethod(ObjectRef, get_player_velocity),
luamethod(ObjectRef, get_look_dir), luamethod(ObjectRef, get_look_dir),
luamethod(ObjectRef, get_look_pitch), luamethod(ObjectRef, get_look_pitch),
luamethod(ObjectRef, get_look_yaw), luamethod(ObjectRef, get_look_yaw),

@ -183,6 +183,9 @@ private:
// get_player_name(self) // get_player_name(self)
static int l_get_player_name(lua_State *L); static int l_get_player_name(lua_State *L);
// get_player_velocity(self)
static int l_get_player_velocity(lua_State *L);
// get_look_dir(self) // get_look_dir(self)
static int l_get_look_dir(lua_State *L); static int l_get_look_dir(lua_State *L);