/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lua_api/l_object.h"
#include <cmath>
#include "lua_api/l_internal.h"
#include "lua_api/l_inventory.h"
#include "lua_api/l_item.h"
#include "lua_api/l_playermeta.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "log.h"
#include "tool.h"
#include "remoteplayer.h"
#include "server.h"
#include "hud.h"
#include "scripting_server.h"
#include "server/luaentity_sao.h"
#include "server/player_sao.h"
#include "server/serverinventorymgr.h"
/*
ObjectRef
*/
ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud) luaL_typerror(L, narg, className);
return *(ObjectRef**)ud; // unbox pointer
}
ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
{
ServerActiveObject *co = ref->m_object;
if (co && co->isGone())
return NULL;
return co;
}
LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
{
ServerActiveObject *obj = getobject(ref);
if (obj == NULL)
return NULL;
if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
return NULL;
return (LuaEntitySAO*)obj;
}
PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
{
ServerActiveObject *obj = getobject(ref);
if (obj == NULL)
return NULL;
if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
return NULL;
return (PlayerSAO*)obj;
}
RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
{
PlayerSAO *playersao = getplayersao(ref);
if (playersao == NULL)
return NULL;
return playersao->getPlayer();
}
// Exported functions
// garbage collector
int ObjectRef::gc_object(lua_State *L) {
ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
//infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
delete o;
return 0;
}
// remove(self)
int ObjectRef::l_remove(lua_State *L)
{
GET_ENV_PTR;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL)
return 0;
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
return 0;
co->clearChildAttachments();
co->clearParentAttachment();
verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
co->m_pending_removal = true;
return 0;
}
// get_pos(self)
// returns: {x=num, y=num, z=num}
int ObjectRef::l_get_pos(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
push_v3f(L, co->getBasePosition() / BS);
return 1;
}
// set_pos(self, pos)
int ObjectRef::l_set_pos(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// pos
v3f pos = checkFloatPos(L, 2);
// Do it
co->setPos(pos);
return 0;
}
// move_to(self, pos, continuous=false)
int ObjectRef::l_move_to(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// pos
v3f pos = checkFloatPos(L, 2);
// continuous
bool continuous = readParam<bool>(L, 3);
// Do it
co->moveTo(pos, continuous);
return 0;
}
// punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
int ObjectRef::l_punch(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ObjectRef *puncher_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
ServerActiveObject *puncher = getobject(puncher_ref);
if (!co || !puncher)
return 0;
v3f dir;
if (lua_type(L, 5) != LUA_TTABLE)
dir = co->getBasePosition() - puncher->getBasePosition();
else
dir = read_v3f(L, 5);
float time_from_last_punch = 1000000;
if (lua_isnumber(L, 3))
time_from_last_punch = lua_tonumber(L, 3);
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
dir.normalize();
u16 src_original_hp = co->getHP();
u16 dst_origin_hp = puncher->getHP();
// Do it
u16 wear = co->punch(dir, &toolcap, puncher, time_from_last_punch);
lua_pushnumber(L, wear);
// 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));
}
// If the puncher is a player, and its HP changed
if (dst_origin_hp != puncher->getHP() &&
puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
}
return 1;
}
// right_click(self, clicker); clicker = an another ObjectRef
int ObjectRef::l_right_click(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ObjectRef *ref2 = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
ServerActiveObject *co2 = getobject(ref2);
if (co == NULL) return 0;
if (co2 == NULL) return 0;
// Do it
co->rightClick(co2);
return 0;
}
// set_hp(self, hp)
// hp = number of hitpoints (2 * number of hearts)
// returns: nil
int ObjectRef::l_set_hp(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
// Get Object
ObjectRef *ref = checkobject(L, 1);
luaL_checknumber(L, 2);
ServerActiveObject *co = getobject(ref);
if (co == NULL)
return 0;
// Get HP
int hp = lua_tonumber(L, 2);
// Get Reason
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
reason.from_mod = true;
if (lua_istable(L, 3)) {
lua_pushvalue(L, 3);
lua_getfield(L, -1, "type");
if (lua_isstring(L, -1) &&
!reason.setTypeFromString(readParam<std::string>(L, -1))) {
errorstream << "Bad type given!" << std::endl;
}
lua_pop(L, 1);
reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
}
// Do it
co->setHP(hp, reason);
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;
}
// get_hp(self)
// returns: number of hitpoints (2 * number of hearts)
// 0 if not applicable to this type of object
int ObjectRef::l_get_hp(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) {
// Default hp is 1
lua_pushnumber(L, 1);
return 1;
}
int hp = co->getHP();
/*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
<<" hp="<<hp<<std::endl;*/
// Return
lua_pushnumber(L, hp);
return 1;
}
// get_inventory(self)
int ObjectRef::l_get_inventory(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
InventoryLocation loc = co->getInventoryLocation();
if (getServerInventoryMgr(L)->getInventory(loc) != NULL)
InvRef::create(L, loc);
else
lua_pushnil(L); // An object may have no inventory (nil)
return 1;
}
// get_wield_list(self)
int ObjectRef::l_get_wield_list(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (!co)
return 0;
lua_pushstring(L, co->getWieldList().c_str());
return 1;
}
// get_wield_index(self)
int ObjectRef::l_get_wield_index(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (!co)
return 0;
lua_pushinteger(L, co->getWieldIndex() + 1);
return 1;
}
// get_wielded_item(self)
int ObjectRef::l_get_wielded_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (!co) {
// Empty ItemStack
LuaItemStack::create(L, ItemStack());
return 1;
}
ItemStack selected_item;
co->getWieldedItem(&selected_item, nullptr);
LuaItemStack::create(L, selected_item);
return 1;
}
// set_wielded_item(self, itemstack or itemstring or table or nil)
int ObjectRef::l_set_wielded_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
ItemStack item = read_item(L, 2, getServer(L)->idef());
bool success = co->setWieldedItem(item);
if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
getServer(L)->SendInventory((PlayerSAO *)co, true);
}
lua_pushboolean(L, success);
return 1;
}
// set_armor_groups(self, groups)
int ObjectRef::l_set_armor_groups(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
ItemGroupList groups;
read_groups(L, 2, groups);
co->setArmorGroups(groups);
return 0;
}
// get_armor_groups(self)
int ObjectRef::l_get_armor_groups(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL)
return 0;
// Do it
push_groups(L, co->getArmorGroups());
return 1;
}
// set_physics_override(self, physics_override_speed, physics_override_jump,
// physics_override_gravity, sneak, sneak_glitch, new_move)
int ObjectRef::l_set_physics_override(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
PlayerSAO *co = (PlayerSAO *) getobject(ref);
if (co == NULL) return 0;
// Do it
if (lua_istable(L, 2)) {
co->m_physics_override_speed = getfloatfield_default(
L, 2, "speed", co->m_physics_override_speed);
co->m_physics_override_jump = getfloatfield_default(
L, 2, "jump", co->m_physics_override_jump);
co->m_physics_override_gravity = getfloatfield_default(
L, 2, "gravity", co->m_physics_override_gravity);
co->m_physics_override_sneak = getboolfield_default(
L, 2, "sneak", co->m_physics_override_sneak);
co->m_physics_override_sneak_glitch = getboolfield_default(
L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
co->m_physics_override_new_move = getboolfield_default(
L, 2, "new_move", co->m_physics_override_new_move);
co->m_physics_override_sent = false;
} else {
// old, non-table format
if (!lua_isnil(L, 2)) {
co->m_physics_override_speed = lua_tonumber(L, 2);
co->m_physics_override_sent = false;
}
if (!lua_isnil(L, 3)) {
co->m_physics_override_jump = lua_tonumber(L, 3);
co->m_physics_override_sent = false;
}
if (!lua_isnil(L, 4)) {
co->m_physics_override_gravity = lua_tonumber(L, 4);
co->m_physics_override_sent = false;
}
}
return 0;
}
// get_physics_override(self)
int ObjectRef::l_get_physics_override(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
PlayerSAO *co = (PlayerSAO *)getobject(ref);
if (co == NULL)
return 0;
// Do it
lua_newtable(L);
lua_pushnumber(L, co->m_physics_override_speed);
lua_setfield(L, -2, "speed");
lua_pushnumber(L, co->m_physics_override_jump);
lua_setfield(L, -2, "jump");
lua_pushnumber(L, co->m_physics_override_gravity);
lua_setfield(L, -2, "gravity");
lua_pushboolean(L, co->m_physics_override_sneak);
lua_setfield(L, -2, "sneak");
lua_pushboolean(L, co->m_physics_override_sneak_glitch);
lua_setfield(L, -2, "sneak_glitch");
lua_pushboolean(L, co->m_physics_override_new_move);
lua_setfield(L, -2, "new_move");
return 1;
}
// set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
int ObjectRef::l_set_animation(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
|