diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/luaentity_sao.cpp | 13 | ||||
-rw-r--r-- | src/server/luaentity_sao.h | 5 | ||||
-rw-r--r-- | src/server/mods.cpp | 8 | ||||
-rw-r--r-- | src/server/player_sao.cpp | 45 | ||||
-rw-r--r-- | src/server/player_sao.h | 10 | ||||
-rw-r--r-- | src/server/serveractiveobject.h | 7 | ||||
-rw-r--r-- | src/server/serverinventorymgr.cpp | 32 | ||||
-rw-r--r-- | src/server/unit_sao.cpp | 31 |
8 files changed, 93 insertions, 58 deletions
diff --git a/src/server/luaentity_sao.cpp b/src/server/luaentity_sao.cpp index 3bcbe107b..82f6da231 100644 --- a/src/server/luaentity_sao.cpp +++ b/src/server/luaentity_sao.cpp @@ -108,7 +108,12 @@ void LuaEntitySAO::addedToEnvironment(u32 dtime_s) m_env->getScriptIface()-> luaentity_Activate(m_id, m_init_state, dtime_s); } else { + // It's an unknown object + // Use entitystring as infotext for debugging m_prop.infotext = m_init_name; + // Set unknown object texture + m_prop.textures.clear(); + m_prop.textures.emplace_back("unknown_object.png"); } } @@ -300,10 +305,11 @@ void LuaEntitySAO::getStaticData(std::string *result) const *result = os.str(); } -u16 LuaEntitySAO::punch(v3f dir, +u32 LuaEntitySAO::punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, - float time_from_last_punch) + float time_from_last_punch, + u16 initial_wear) { if (!m_registered) { // Delete unknown LuaEntities when punched @@ -321,7 +327,8 @@ u16 LuaEntitySAO::punch(v3f dir, m_armor_groups, toolcap, &tool_item, - time_from_last_punch); + time_from_last_punch, + initial_wear); bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher, time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0); diff --git a/src/server/luaentity_sao.h b/src/server/luaentity_sao.h index 6883ae1b9..87b664a8b 100644 --- a/src/server/luaentity_sao.h +++ b/src/server/luaentity_sao.h @@ -44,9 +44,10 @@ public: bool isStaticAllowed() const { return m_prop.static_save; } bool shouldUnload() const { return true; } void getStaticData(std::string *result) const; - u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr, + u32 punch(v3f dir, const ToolCapabilities *toolcap = nullptr, ServerActiveObject *puncher = nullptr, - float time_from_last_punch = 1000000.0f); + float time_from_last_punch = 1000000.0f, + u16 initial_wear = 0); void rightClick(ServerActiveObject *clicker); void setPos(const v3f &pos); void moveTo(v3f pos, bool continuous); diff --git a/src/server/mods.cpp b/src/server/mods.cpp index 83fa12da9..609d8c346 100644 --- a/src/server/mods.cpp +++ b/src/server/mods.cpp @@ -61,12 +61,8 @@ void ServerModManager::loadMods(ServerScripting *script) infostream << std::endl; // Load and run "mod" scripts for (const ModSpec &mod : m_sorted_mods) { - if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) { - throw ModError("Error loading mod \"" + mod.name + - "\": Mod name does not follow naming " - "conventions: " - "Only characters [a-z0-9_] are allowed."); - } + mod.checkAndLog(); + std::string script_path = mod.path + DIR_DELIM + "init.lua"; auto t = porting::getTimeMs(); script->loadMod(script_path, mod.name); diff --git a/src/server/player_sao.cpp b/src/server/player_sao.cpp index 0d31f2e0b..d076d5783 100644 --- a/src/server/player_sao.cpp +++ b/src/server/player_sao.cpp @@ -167,7 +167,6 @@ void PlayerSAO::step(float dtime, bool send_recommended) if (m_breath == 0) { PlayerHPChangeReason reason(PlayerHPChangeReason::DROWNING); setHP(m_hp - c.drowning, reason); - m_env->getGameDef()->SendPlayerHPOrDie(this, reason); } } } @@ -216,7 +215,6 @@ void PlayerSAO::step(float dtime, bool send_recommended) s32 newhp = (s32)m_hp - (s32)damage_per_second; PlayerHPChangeReason reason(PlayerHPChangeReason::NODE_DAMAGE, nodename); setHP(newhp, reason); - m_env->getGameDef()->SendPlayerHPOrDie(this, reason); } } @@ -411,10 +409,11 @@ void PlayerSAO::setLookPitchAndSend(const float pitch) m_env->getGameDef()->SendMovePlayer(m_peer_id); } -u16 PlayerSAO::punch(v3f dir, +u32 PlayerSAO::punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, - float time_from_last_punch) + float time_from_last_punch, + u16 initial_wear) { if (!toolcap) return 0; @@ -432,7 +431,7 @@ u16 PlayerSAO::punch(v3f dir, s32 old_hp = getHP(); HitParams hitparams = getHitParams(m_armor_groups, toolcap, - time_from_last_punch); + time_from_last_punch, initial_wear); PlayerSAO *playersao = m_player->getPlayerSAO(); @@ -464,33 +463,33 @@ void PlayerSAO::rightClick(ServerActiveObject *clicker) m_env->getScriptIface()->on_rightclickplayer(this, clicker); } -void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason) +void PlayerSAO::setHP(s32 target_hp, const PlayerHPChangeReason &reason, bool from_client) { - if (hp == (s32)m_hp) - return; // Nothing to do + target_hp = rangelim(target_hp, 0, U16_MAX); - if (m_hp <= 0 && hp < (s32)m_hp) - return; // Cannot take more damage + if (target_hp == m_hp) + return; // Nothing to do - { - s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - m_hp, reason); - if (hp_change == 0) - return; + s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, target_hp - (s32)m_hp, reason); - hp = m_hp + hp_change; - } + s32 hp = (s32)m_hp + std::min(hp_change, U16_MAX); // Protection against s32 overflow + hp = rangelim(hp, 0, U16_MAX); - s32 oldhp = m_hp; - hp = rangelim(hp, 0, m_prop.hp_max); + if (hp > m_prop.hp_max) + hp = m_prop.hp_max; - if (hp < oldhp && isImmortal()) - return; // Do not allow immortal players to be damaged - - m_hp = hp; + if (hp < m_hp && isImmortal()) + hp = m_hp; // Do not allow immortal players to be damaged // Update properties on death - if ((hp == 0) != (oldhp == 0)) + if ((hp == 0) != (m_hp == 0)) m_properties_sent = false; + + if (hp != m_hp) { + m_hp = hp; + m_env->getGameDef()->HandlePlayerHPChange(this, reason); + } else if (from_client) + m_env->getGameDef()->SendPlayerHP(this); } void PlayerSAO::setBreath(const u16 breath, bool send) diff --git a/src/server/player_sao.h b/src/server/player_sao.h index 8e2d8803f..96d8f7189 100644 --- a/src/server/player_sao.h +++ b/src/server/player_sao.h @@ -109,10 +109,14 @@ public: Interaction interface */ - u16 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, - float time_from_last_punch); + u32 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, + float time_from_last_punch, u16 initial_wear = 0); void rightClick(ServerActiveObject *clicker); - void setHP(s32 hp, const PlayerHPChangeReason &reason); + void setHP(s32 hp, const PlayerHPChangeReason &reason) override + { + return setHP(hp, reason, false); + } + void setHP(s32 hp, const PlayerHPChangeReason &reason, bool from_client); void setHPRaw(u16 hp) { m_hp = hp; } u16 getBreath() const { return m_breath; } void setBreath(const u16 breath, bool send = true); diff --git a/src/server/serveractiveobject.h b/src/server/serveractiveobject.h index 51f445914..5b0ee2d9b 100644 --- a/src/server/serveractiveobject.h +++ b/src/server/serveractiveobject.h @@ -145,11 +145,12 @@ public: virtual bool shouldUnload() const { return true; } - // Returns tool wear - virtual u16 punch(v3f dir, + // Returns added tool wear + virtual u32 punch(v3f dir, const ToolCapabilities *toolcap = nullptr, ServerActiveObject *puncher = nullptr, - float time_from_last_punch = 1000000.0f) + float time_from_last_punch = 1000000.0f, + u16 initial_wear = 0) { return 0; } virtual void rightClick(ServerActiveObject *clicker) {} diff --git a/src/server/serverinventorymgr.cpp b/src/server/serverinventorymgr.cpp index 2a80c9bbe..63d1645cb 100644 --- a/src/server/serverinventorymgr.cpp +++ b/src/server/serverinventorymgr.cpp @@ -39,24 +39,29 @@ ServerInventoryManager::~ServerInventoryManager() Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc) { + // No m_env check here: allow creation and modification of detached inventories + switch (loc.type) { case InventoryLocation::UNDEFINED: case InventoryLocation::CURRENT_PLAYER: break; case InventoryLocation::PLAYER: { + if (!m_env) + return nullptr; + RemotePlayer *player = m_env->getPlayer(loc.name.c_str()); if (!player) return NULL; + PlayerSAO *playersao = player->getPlayerSAO(); - if (!playersao) - return NULL; - return playersao->getInventory(); + return playersao ? playersao->getInventory() : nullptr; } break; case InventoryLocation::NODEMETA: { + if (!m_env) + return nullptr; + NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p); - if (!meta) - return NULL; - return meta->getInventory(); + return meta ? meta->getInventory() : nullptr; } break; case InventoryLocation::DETACHED: { auto it = m_detached_inventories.find(loc.name); @@ -151,14 +156,15 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name) const std::string &owner = inv_it->second.owner; if (!owner.empty()) { - RemotePlayer *player = m_env->getPlayer(owner.c_str()); + if (m_env) { + RemotePlayer *player = m_env->getPlayer(owner.c_str()); - if (player && player->getPeerId() != PEER_ID_INEXISTENT) - m_env->getGameDef()->sendDetachedInventory( - nullptr, name, player->getPeerId()); - - } else { - // Notify all players about the change + if (player && player->getPeerId() != PEER_ID_INEXISTENT) + m_env->getGameDef()->sendDetachedInventory( + nullptr, name, player->getPeerId()); + } + } else if (m_env) { + // Notify all players about the change as soon ServerEnv exists m_env->getGameDef()->sendDetachedInventory( nullptr, name, PEER_ID_INEXISTENT); } diff --git a/src/server/unit_sao.cpp b/src/server/unit_sao.cpp index 2371640ca..9a49b0f43 100644 --- a/src/server/unit_sao.cpp +++ b/src/server/unit_sao.cpp @@ -84,8 +84,11 @@ void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotatio void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation) { - *position = m_bone_position[bone].X; - *rotation = m_bone_position[bone].Y; + auto it = m_bone_position.find(bone); + if (it != m_bone_position.end()) { + *position = it->second.X; + *rotation = it->second.Y; + } } // clang-format off @@ -124,6 +127,19 @@ void UnitSAO::sendOutdatedData() void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation, bool force_visible) { + auto *obj = parent_id ? m_env->getActiveObject(parent_id) : nullptr; + if (obj) { + // Do checks to avoid circular references + // The chain of wanted parent must not refer or contain "this" + for (obj = obj->getParent(); obj; obj = obj->getParent()) { + if (obj == this) { + warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent " + << parent_id << " but former is an (in)direct parent of latter." << std::endl; + return; + } + } + } + // Attachments need to be handled on both the server and client. // If we just attach on the server, we can only copy the position of the parent. // Attachments are still sent to clients at an interval so players might see them @@ -134,16 +150,21 @@ void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position int old_parent = m_attachment_parent_id; m_attachment_parent_id = parent_id; + + // The detach callbacks might call to setAttachment() again. + // Ensure the attachment params are applied after this callback is run. + if (parent_id != old_parent) + onDetach(old_parent); + + m_attachment_parent_id = parent_id; m_attachment_bone = bone; m_attachment_position = position; m_attachment_rotation = rotation; m_force_visible = force_visible; m_attachment_sent = false; - if (parent_id != old_parent) { - onDetach(old_parent); + if (parent_id != old_parent) onAttach(parent_id); - } } void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position, |