// 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 == NULL) return 0; if (puncher == NULL) 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 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)); } // 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 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; }
// 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; ObjectRef *ref = checkobject(L, 1); luaL_checknumber(L, 2); ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; int hp = lua_tonumber(L, 2); /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId() <<" hp="<<hp<<std::endl;*/ // Do it co->setHP(hp); if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)co)->getPeerID(), co->getHP() == 0); } // Return return 0; }
// set_properties(self, properties) int ObjectRef::l_set_properties(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) 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); } co->notifyObjectPropertiesModified(); return 0; }