LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref) { ServerActiveObject *obj = getobject(ref); if (obj == NULL) return NULL; if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY && obj->getType() != ACTIVEOBJECT_TYPE_LUACREATURE && obj->getType() != ACTIVEOBJECT_TYPE_LUAITEM && obj->getType() != ACTIVEOBJECT_TYPE_LUAFALLING) return NULL; return (LuaEntitySAO*)obj; }
// 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; std::set<int> child_ids = co->getAttachmentChildIds(); std::set<int>::iterator it; for (it = child_ids.begin(); it != child_ids.end(); ++it) { ServerActiveObject *child = env->getActiveObject(*it); if (child) child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0)); } /* verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl; */ co->m_removed = true; return 0; }
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; }
// remove(self) int ObjectRef::l_remove(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; if(co->getType() == ACTIVEOBJECT_TYPE_PLAYER) return 0; verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl; co->m_removed = true; 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 == 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(); s16 src_original_hp = co->getHP(); s16 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)->getPeerID(), co->getHP() == 0); } // 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)->getPeerID(), puncher->getHP() == 0); } return 0; }
// 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)); } lua_pushboolean(L, success); return 1; }
void UnitSAO::onDetach(int parent_id) { if (!parent_id) return; ServerActiveObject *parent = m_env->getActiveObject(parent_id); if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY) m_env->getScriptIface()->luaentity_on_detach(m_id, parent); if (!parent || parent->isGone()) return; // Do not try to notify soon gone parent if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this); }
// This is probably very useless void spawnRandomObjects(MapBlock *block) { for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++) for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++) { bool last_node_walkable = false; for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++) { v3s16 p(x0,y0,z0); MapNode n = block->getNodeNoEx(p); if(n.getContent() == CONTENT_IGNORE) continue; if(content_features(n).liquid_type != LIQUID_NONE) continue; if(content_features(n).walkable) { last_node_walkable = true; continue; } if(last_node_walkable) { // If block contains light information if(content_features(n).param_type == CPT_LIGHT) { if(n.getLight(LIGHTBANK_DAY) <= 5) { if(myrand() % 1000 == 0) { v3f pos_f = intToFloat(p+block->getPosRelative(), BS); pos_f.Y -= BS*0.4; ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f); std::string data = obj->getStaticData(); StaticObject s_obj(obj->getType(), obj->getBasePosition(), data); // Add one block->m_static_objects.insert(0, s_obj); delete obj; block->setChangedFlag(); } } } } last_node_walkable = false; } } }
// 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; }
// 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); // 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; }
// 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; }
// 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; const UNORDERED_SET<int> &child_ids = co->getAttachmentChildIds(); UNORDERED_SET<int>::const_iterator it; for (it = child_ids.begin(); it != child_ids.end(); ++it) { // Child can be NULL if it was deleted earlier if (ServerActiveObject *child = env->getActiveObject(*it)) child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0)); } verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl; co->m_removed = true; return 0; }
/* Convert objects that are not in active blocks to static. If m_known_by_count != 0, active object is not deleted, but static data is still updated. If force_delete is set, active object is deleted nevertheless. It shall only be set so in the destructor of the environment. */ void ServerEnvironment::deactivateFarObjects(bool force_delete) { core::list<u16> objects_to_remove; for(core::map<u16, ServerActiveObject*>::Iterator i = m_active_objects.getIterator(); i.atEnd()==false; i++) { ServerActiveObject* obj = i.getNode()->getValue(); u16 id = i.getNode()->getKey(); v3f objectpos = obj->getBasePosition(); // This shouldn't happen but check it if(obj == NULL) { dstream<<"WARNING: NULL object found in ServerEnvironment" <<std::endl; assert(0); continue; } // The block in which the object resides in v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS)); // If block is active, don't remove if(m_active_blocks.contains(blockpos_o)) continue; /* Update the static data */ // Delete old static object MapBlock *oldblock = NULL; if(obj->m_static_exists) { MapBlock *block = m_map->getBlockNoCreateNoEx (obj->m_static_block); if(block) { block->m_static_objects.remove(id); oldblock = block; } } // Create new static object std::string staticdata = obj->getStaticData(); StaticObject s_obj(obj->getType(), objectpos, staticdata); // Add to the block where the object is located in v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS)); // Get or generate the block MapBlock *block = m_map->emergeBlock(blockpos); /*MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos); if(block == NULL) { // Block not found. Is the old block still ok? if(oldblock) block = oldblock; // Load from disk or generate else block = m_map->emergeBlock(blockpos); }*/ if(block) { block->m_static_objects.insert(0, s_obj); block->setChangedFlag(); obj->m_static_exists = true; obj->m_static_block = block->getPos(); } else{ dstream<<"WARNING: ServerEnv: Could not find or generate " <<"a block for storing static object"<<std::endl; obj->m_static_exists = false; continue; } /* Delete active object if not known by some client, else set pending deactivation */ // If known by some client, don't delete. if(obj->m_known_by_count > 0 && force_delete == false) { obj->m_pending_deactivation = true; continue; } /*dstream<<"INFO: Server: Stored static data. Deleting object." <<std::endl;*/ // Delete active object delete obj; // Id to be removed from m_active_objects objects_to_remove.push_back(id); } // Remove references from m_active_objects for(core::list<u16>::Iterator i = objects_to_remove.begin(); i != objects_to_remove.end(); i++) { m_active_objects.remove(*i); } }