From 4d81455020794cd7c7d9031e4f4cec2ddc5cadd6 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 5 May 2017 22:16:31 +0300 Subject: [PATCH] [General] Rework and simplify death reasons so they work with NPCs --- apps/openmw-mp/Player.cpp | 26 ------------------- apps/openmw-mp/Player.hpp | 9 ------- apps/openmw-mp/Script/Functions/Stats.cpp | 9 ++++++- apps/openmw-mp/Script/Functions/Stats.hpp | 2 ++ apps/openmw-mp/Script/ScriptFunctions.hpp | 2 +- .../player/ProcessorPlayerAttack.hpp | 20 -------------- .../player/ProcessorPlayerDeath.hpp | 21 +-------------- apps/openmw/mwclass/npc.cpp | 9 ++++++- apps/openmw/mwmp/LocalPlayer.cpp | 6 +++++ components/openmw-mp/Base/BasePlayer.hpp | 2 ++ .../Packets/Player/PacketPlayerDeath.hpp | 2 ++ 11 files changed, 30 insertions(+), 78 deletions(-) diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index e0265d65c..03b17e216 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -69,7 +69,6 @@ Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid) { handshakeState = false; loadState = NOTLOADED; - lastAttacker = 0; } Player::~Player() @@ -114,31 +113,6 @@ Player *Players::getPlayer(unsigned short id) return slots[id]; } -void Player::setLastAttackerId(unsigned short pid) -{ - lastAttacker = pid; -} - -void Player::resetLastAttacker() -{ - lastAttacker = id; -} - -unsigned short Player::getLastAttackerId() -{ - return lastAttacker; -} - -void Player::setLastAttackerTime(std::chrono::steady_clock::time_point time) -{ - lastAttackerTime = time; -} - -std::chrono::steady_clock::time_point Player::getLastAttackerTime() -{ - return lastAttackerTime; -} - CellController::TContainer *Player::getCells() { return &cells; diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 8d6d219b5..f5d807102 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -63,13 +63,6 @@ public: void setLoadState(int state); int getLoadState(); - void setLastAttackerId(unsigned short pid); - void resetLastAttacker(); - unsigned short getLastAttackerId(); - - void setLastAttackerTime(std::chrono::steady_clock::time_point time); - std::chrono::steady_clock::time_point getLastAttackerTime(); - virtual ~Player(); CellController::TContainer *getCells(); @@ -86,8 +79,6 @@ private: CellController::TContainer cells; bool handshakeState; int loadState; - unsigned short lastAttacker; - std::chrono::steady_clock::time_point lastAttackerTime; }; diff --git a/apps/openmw-mp/Script/Functions/Stats.cpp b/apps/openmw-mp/Script/Functions/Stats.cpp index 1f7bbeb56..8b5e66c6b 100644 --- a/apps/openmw-mp/Script/Functions/Stats.cpp +++ b/apps/openmw-mp/Script/Functions/Stats.cpp @@ -110,7 +110,6 @@ int StatsFunctions::GetIsMale(unsigned short pid) noexcept const char *StatsFunctions::GetBirthsign(unsigned short pid) noexcept { - Player *player; GET_PLAYER(pid, player, 0); @@ -133,6 +132,14 @@ bool StatsFunctions::IsCreatureName(unsigned short pid) noexcept return player->useCreatureName; } +const char *StatsFunctions::GetDeathReason(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + return player->deathReason.c_str(); +} + int StatsFunctions::GetLevel(unsigned short pid) noexcept { Player *player; diff --git a/apps/openmw-mp/Script/Functions/Stats.hpp b/apps/openmw-mp/Script/Functions/Stats.hpp index 14d7c3c1f..13456fda5 100644 --- a/apps/openmw-mp/Script/Functions/Stats.hpp +++ b/apps/openmw-mp/Script/Functions/Stats.hpp @@ -21,6 +21,7 @@ {"GetBirthsign", StatsFunctions::GetBirthsign},\ {"GetCreatureModel", StatsFunctions::GetCreatureModel},\ {"IsCreatureName", StatsFunctions::IsCreatureName},\ + {"GetDeathReason", StatsFunctions::GetDeathReason},\ \ {"GetLevel", StatsFunctions::GetLevel},\ {"GetLevelProgress", StatsFunctions::GetLevelProgress},\ @@ -100,6 +101,7 @@ public: static const char *GetBirthsign(unsigned short pid) noexcept; static const char *GetCreatureModel(unsigned short pid) noexcept; static bool IsCreatureName(unsigned short pid) noexcept; + static const char *GetDeathReason(unsigned short pid) noexcept; static int GetLevel(unsigned short pid) noexcept; static int GetLevelProgress(unsigned short pid) noexcept; diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index 28953ec45..b399220b3 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -112,7 +112,7 @@ public: {"OnServerExit", Function()}, {"OnPlayerConnect", Function()}, {"OnPlayerDisconnect", Function()}, - {"OnPlayerDeath", Function()}, + {"OnPlayerDeath", Function()}, {"OnPlayerResurrect", Function()}, {"OnPlayerCellChange", Function()}, {"OnPlayerAttributesChange", Function()}, diff --git a/apps/openmw-mp/processors/player/ProcessorPlayerAttack.hpp b/apps/openmw-mp/processors/player/ProcessorPlayerAttack.hpp index 610be3feb..1e22681b2 100644 --- a/apps/openmw-mp/processors/player/ProcessorPlayerAttack.hpp +++ b/apps/openmw-mp/processors/player/ProcessorPlayerAttack.hpp @@ -26,27 +26,7 @@ namespace mwmp if (!player.creatureStats.mDead) { - Player *target = Players::getPlayer(player.attack.target.guid); - - if (target == nullptr) - target = &player; - - LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player: %s attacked %s state: %d", player.npc.mName.c_str(), - target->npc.mName.c_str(), player.attack.pressed == 1); - if (player.attack.pressed == 0) - { - LOG_APPEND(Log::LOG_VERBOSE, "success: %d", player.attack.success == 1); - if (player.attack.success == 1) - { - LOG_APPEND(Log::LOG_VERBOSE, "damage: %d", player.attack.damage == 1); - target->setLastAttackerId(player.getId()); - target->setLastAttackerTime(std::chrono::steady_clock::now()); - } - } - - //packet.Send(player, true); player.sendToLoaded(&packet); - playerController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->RequestData(player.attack.target.guid); } } }; diff --git a/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp b/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp index d40c7db6c..09aca899c 100644 --- a/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp +++ b/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp @@ -17,34 +17,15 @@ namespace mwmp ProcessorPlayerDeath() { BPP_INIT(ID_PLAYER_DEATH) - avoidReading = true; } void Do(PlayerPacket &packet, Player &player) override { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player.npc.mName.c_str()); - Player *killer = Players::getPlayer(player.getLastAttackerId()); - - short reason = 0; // unknown; - double secondsSinceLastAttacker = std::chrono::duration_cast>( - std::chrono::steady_clock::now() - player.getLastAttackerTime()).count(); - - if (!killer) - killer = &player; - - if (secondsSinceLastAttacker < 3.0f) - reason = 1; // killed - else - reason = 2; //suicide - - player.resetLastAttacker(); - - player.creatureStats.mDead = true; - packet.Send(true); - Script::Call(player.getId(), reason, killer->getId()); + Script::Call(player.getId()); } }; } diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 6e6da347c..0482172e7 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -938,10 +938,17 @@ namespace MWClass localAttack->shouldSend = true; } - else if (ptr == MWMechanics::getPlayer()) + + if (ptr == MWMechanics::getPlayer()) { mwmp::Main::get().getLocalPlayer()->updateStatsDynamic(true); mwmp::Main::get().getLocalPlayer()->updatePosition(true); // fix position after getting damage; + + // Record the attacker as the LocalPlayer's death reason + if (getCreatureStats(ptr).isDead()) + { + mwmp::Main::get().getLocalPlayer()->deathReason = attacker.getClass().getName(attacker); + } } /* End of tes3mp addition diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index dc5420452..a49cca9ca 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -50,6 +50,8 @@ LocalPlayer::LocalPlayer() ignorePosPacket = false; attack.shouldSend = false; + + deathReason = "suicide"; } LocalPlayer::~LocalPlayer() @@ -533,12 +535,16 @@ void LocalPlayer::updateDeadState(bool forceUpdate) creatureStats.mDead = true; LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_PLAYER_DEATH to server about myself"); + LOG_APPEND(Log::LOG_INFO, "- deathReason was %s", deathReason.c_str()); getNetworking()->getPlayerPacket(ID_PLAYER_DEATH)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_DEATH)->Send(); isDead = true; } else if (ptrNpcStats->getHealth().getCurrent() > 0 && isDead) + { + deathReason = "suicide"; isDead = false; + } } void LocalPlayer::updateAnimFlags(bool forceUpdate) diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 4a8923a8a..883c89967 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -182,6 +182,8 @@ namespace mwmp std::string passw; std::string creatureModel; bool useCreatureName; + + std::string deathReason; }; } diff --git a/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp b/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp index 380287698..7d84575a2 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp @@ -22,6 +22,8 @@ namespace mwmp { PlayerPacket::Packet(bs, send); RW(player->creatureStats.mDead, send); + + RW(player->deathReason, send); } }; }