// pos
v3f pos = checkFloatPos(L, 2);
// continuous
- bool continuous = lua_toboolean(L, 3);
+ bool continuous = readParam<bool>(L, 3);
// Do it
co->moveTo(pos, continuous);
return 0;
ObjectRef *puncher_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
ServerActiveObject *puncher = getobject(puncher_ref);
- if (co == NULL) return 0;
- if (puncher == NULL) return 0;
+ if (!co || !puncher)
+ return 0;
v3f dir;
if (lua_type(L, 5) != LUA_TTABLE)
dir = co->getBasePosition() - puncher->getBasePosition();
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
dir.normalize();
- s16 src_original_hp = co->getHP();
- s16 dst_origin_hp = puncher->getHP();
+ u16 src_original_hp = co->getHP();
+ u16 dst_origin_hp = puncher->getHP();
// Do it
co->punch(dir, &toolcap, puncher, time_from_last_punch);
// If the punched is a player, and its HP changed
if (src_original_hp != co->getHP() &&
co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
- getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
+ getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co,
+ PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
}
// If the puncher is a player, and its HP changed
lua_pushvalue(L, 3);
lua_getfield(L, -1, "type");
- if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) {
+ if (lua_isstring(L, -1) &&
+ !reason.setTypeFromString(readParam<std::string>(L, -1))) {
errorstream << "Bad type given!" << std::endl;
}
lua_pop(L, 1);
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
+ if (reason.hasLuaReference())
+ luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
+
// Return
return 0;
}
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
- if (co == NULL) return 0;
- // Do it
+ if (!co)
+ return 0;
+
lua_pushstring(L, co->getWieldList().c_str());
return 1;
}
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
- if (co == NULL) return 0;
- // Do it
+ if (!co)
+ return 0;
+
lua_pushinteger(L, co->getWieldIndex() + 1);
return 1;
}
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
- if (co == NULL) {
+ if (!co) {
// Empty ItemStack
LuaItemStack::create(L, ItemStack());
return 1;
}
- // Do it
+
LuaItemStack::create(L, co->getWieldedItem());
return 1;
}
// Do it
v2f frames = v2f(1, 1);
if (!lua_isnil(L, 2))
- frames = read_v2f(L, 2);
+ frames = readParam<v2f>(L, 2);
float frame_speed = 15;
if (!lua_isnil(L, 3))
frame_speed = lua_tonumber(L, 3);
frame_blend = lua_tonumber(L, 4);
bool frame_loop = true;
if (lua_isboolean(L, 5))
- frame_loop = lua_toboolean(L, 5);
+ frame_loop = readParam<bool>(L, 5);
co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
return 0;
}
return 2;
}
+// send_mapblock(self, pos)
+int ObjectRef::l_send_mapblock(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+
+ RemotePlayer *player = getplayer(ref);
+ if (!player)
+ return 0;
+ v3s16 p = read_v3s16(L, 2);
+
+ session_t peer_id = player->getPeerId();
+ bool r = getServer(L)->SendBlock(peer_id, p);
+
+ lua_pushboolean(L, r);
+ return 1;
+}
+
// set_animation_frame_speed(self, frame_speed)
int ObjectRef::l_set_animation_frame_speed(lua_State *L)
{
// Do it
std::string bone;
if (!lua_isnil(L, 2))
- bone = lua_tostring(L, 2);
+ bone = readParam<std::string>(L, 2);
v3f position = v3f(0, 0, 0);
if (!lua_isnil(L, 3))
position = check_v3f(L, 3);
// Do it
std::string bone;
if (!lua_isnil(L, 2))
- bone = lua_tostring(L, 2);
+ bone = readParam<std::string>(L, 2);
v3f position = v3f(0, 0, 0);
v3f rotation = v3f(0, 0, 0);
bone = "";
if (!lua_isnil(L, 3))
- bone = lua_tostring(L, 3);
+ bone = readParam<std::string>(L, 3);
position = v3f(0, 0, 0);
if (!lua_isnil(L, 4))
position = read_v3f(L, 4);
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
- if (co == NULL) return 0;
+ if (!co)
+ return 0;
+
ObjectProperties *prop = co->accessObjectProperties();
if (!prop)
return 0;
- read_object_properties(L, 2, prop, getServer(L)->idef());
- if (prop->hp_max < co->getHP()) {
- PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
- co->setHP(prop->hp_max, reason);
- if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
- getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
- }
+
+ read_object_properties(L, 2, co, prop, getServer(L)->idef());
co->notifyObjectPropertiesModified();
return 0;
}
return 1;
}
+// set_rotation(self, {x=num, y=num, z=num})
+// Each 'num' is in radians
+int ObjectRef::l_set_rotation(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if (!co)
+ return 0;
+
+ v3f rotation = check_v3f(L, 2) * core::RADTODEG;
+ co->setRotation(rotation);
+ return 0;
+}
+
+// get_rotation(self)
+// returns: {x=num, y=num, z=num}
+// Each 'num' is in radians
+int ObjectRef::l_get_rotation(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if (!co)
+ return 0;
+
+ lua_newtable(L);
+ v3f rotation = co->getRotation() * core::DEGTORAD;
+ push_v3f(L, rotation);
+ return 1;
+}
+
// set_yaw(self, radians)
int ObjectRef::l_set_yaw(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
+
if (co == NULL) return 0;
- float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
- // Do it
- co->setYaw(yaw);
+ if (isNaN(L, 2))
+ throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
+
+ float yaw = readParam<float>(L, 2) * core::RADTODEG;
+ co->setRotation(v3f(0, yaw, 0));
return 0;
}
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
- if (co == NULL) return 0;
- // Do it
- float yaw = co->getYaw() * core::DEGTORAD;
+ if (!co)
+ return 0;
+
+ float yaw = co->getRotation().Y * core::DEGTORAD;
lua_pushnumber(L, yaw);
return 1;
}
// Do it
v2s16 p(0,0);
if (!lua_isnil(L, 2))
- p = read_v2s16(L, 2);
+ p = readParam<v2s16>(L, 2);
int num_frames = 1;
if (!lua_isnil(L, 3))
num_frames = lua_tonumber(L, 3);
framelength = lua_tonumber(L, 4);
bool select_horiz_by_yawpitch = false;
if (!lua_isnil(L, 5))
- select_horiz_by_yawpitch = lua_toboolean(L, 5);
+ select_horiz_by_yawpitch = readParam<bool>(L, 5);
co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
return 0;
}
return 1;
}
+// add_player_velocity(self, {x=num, y=num, z=num})
+int ObjectRef::l_add_player_velocity(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ v3f vel = checkFloatPos(L, 2);
+
+ RemotePlayer *player = getplayer(ref);
+ PlayerSAO *co = getplayersao(ref);
+ if (!player || !co)
+ return 0;
+
+ session_t peer_id = player->getPeerId();
+ if (peer_id == PEER_ID_INEXISTENT)
+ return 0;
+ // Do it
+ co->setMaxSpeedOverride(vel);
+ getServer(L)->SendPlayerSpeed(peer_id, vel);
+ return 0;
+}
+
// get_look_dir(self)
int ObjectRef::l_get_look_dir(lua_State *L)
{
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- float pitch = co->getRadPitchDep();
+ float pitch = co->getRadLookPitchDep();
float yaw = co->getRadYawDep();
v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
std::sin(yaw));
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadPitchDep());
+ lua_pushnumber(L, co->getRadLookPitchDep());
return 1;
}
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadPitch());
+ lua_pushnumber(L, co->getRadLookPitch());
return 1;
}
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
// Do it
- lua_pushnumber(L, co->getRadYaw());
+ lua_pushnumber(L, co->getRadRotation().Y);
return 1;
}
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
- float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
+ float pitch = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setPitchAndSend(pitch);
+ co->setLookPitchAndSend(pitch);
return 1;
}
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
- float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
+ float yaw = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setYawAndSend(yaw);
+ co->setPlayerYawAndSend(yaw);
return 1;
}
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
- float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
+ float pitch = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setPitchAndSend(pitch);
+ co->setLookPitchAndSend(pitch);
return 1;
}
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) return 0;
- float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
+ float yaw = readParam<float>(L, 2) * core::RADTODEG;
// Do it
- co->setYawAndSend(yaw);
+ co->setPlayerYawAndSend(yaw);
return 1;
}
// set_attribute(self, attribute, value)
int ObjectRef::l_set_attribute(lua_State *L)
{
+ log_deprecated(L,
+ "Deprecated call to set_attribute, use MetaDataRef methods instead.");
+
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL)
// get_attribute(self, attribute)
int ObjectRef::l_get_attribute(lua_State *L)
{
+ log_deprecated(L,
+ "Deprecated call to get_attribute, use MetaDataRef methods instead.");
+
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL)
if (player == NULL)
return 0;
- std::string name = lua_tostring(L, 2);
+ std::string name = readParam<std::string>(L, 2);
getServer(L)->hudSetHotbarImage(player, name);
return 1;
if (player == NULL)
return 0;
- std::string name = lua_tostring(L, 2);
+ std::string name = readParam<std::string>(L, 2);
getServer(L)->hudSetHotbarSelectedImage(player, name);
return 1;
while (lua_next(L, 4) != 0) {
// key at index -2 and value at index -1
if (lua_isstring(L, -1))
- params.emplace_back(lua_tostring(L, -1));
+ params.emplace_back(readParam<std::string>(L, -1));
else
params.emplace_back("");
// removes value, keeps key for next iteration
bool clouds = true;
if (lua_isboolean(L, 5))
- clouds = lua_toboolean(L, 5);
+ clouds = readParam<bool>(L, 5);
getServer(L)->setSky(player, bgcolor, type, params, clouds);
lua_pushboolean(L, true);
s16 i = 1;
for (const std::string ¶m : params) {
lua_pushlstring(L, param.c_str(), param.size());
- lua_rawseti(L, -2, i);
- i++;
+ lua_rawseti(L, -2, i++);
}
lua_pushboolean(L, clouds);
return 4;
float ratio = 0.0f;
if (!lua_isnil(L, 2)) {
do_override = true;
- ratio = luaL_checknumber(L, 2);
+ ratio = readParam<float>(L, 2);
}
if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
lua_pop(L, 1); // drop metatable
+ markAliasDeprecated(methods);
luaL_openlib(L, 0, methods, 0); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
const char ObjectRef::className[] = "ObjectRef";
-const luaL_Reg ObjectRef::methods[] = {
+luaL_Reg ObjectRef::methods[] = {
// ServerActiveObject
luamethod(ObjectRef, remove),
luamethod_aliased(ObjectRef, get_pos, getpos),
luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
luamethod_aliased(ObjectRef, set_yaw, setyaw),
luamethod_aliased(ObjectRef, get_yaw, getyaw),
+ luamethod(ObjectRef, set_rotation),
+ luamethod(ObjectRef, get_rotation),
luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
luamethod_aliased(ObjectRef, set_sprite, setsprite),
luamethod(ObjectRef, get_entity_name),
luamethod(ObjectRef, is_player_connected),
luamethod(ObjectRef, get_player_name),
luamethod(ObjectRef, get_player_velocity),
+ luamethod(ObjectRef, add_player_velocity),
luamethod(ObjectRef, get_look_dir),
luamethod(ObjectRef, get_look_pitch),
luamethod(ObjectRef, get_look_yaw),
luamethod(ObjectRef, get_local_animation),
luamethod(ObjectRef, set_eye_offset),
luamethod(ObjectRef, get_eye_offset),
+ luamethod(ObjectRef, send_mapblock),
{0,0}
};