From c075496748faeb2574ae22983c194d3b40437813 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 5 Jul 2018 22:24:51 +0300 Subject: [PATCH] [General] Replace deathReason in death packets with a killer variable Add serverside script functions for determining the killers of both players and actors. Use unsigned ints for script functions returning an object or actor's refNumIndex or mpNum. Remove updateDeadState() from LocalPlayer and make its code part of updateStatsDynamic() for simplicity. --- apps/openmw-mp/Script/Functions/Actors.cpp | 38 +++++++++- apps/openmw-mp/Script/Functions/Actors.hpp | 57 +++++++++++++-- apps/openmw-mp/Script/Functions/Mechanics.cpp | 73 +++++++++++++++++++ apps/openmw-mp/Script/Functions/Mechanics.hpp | 63 +++++++++++++++- apps/openmw-mp/Script/Functions/Objects.cpp | 8 +- apps/openmw-mp/Script/Functions/Objects.hpp | 8 +- apps/openmw-mp/Script/Functions/Stats.cpp | 8 -- apps/openmw-mp/Script/Functions/Stats.hpp | 12 --- apps/openmw-mp/Script/ScriptFunctions.cpp | 2 +- .../player/ProcessorPlayerDeath.hpp | 4 - apps/openmw/mwclass/creature.cpp | 2 +- apps/openmw/mwclass/npc.cpp | 10 +-- apps/openmw/mwmechanics/spellcasting.cpp | 8 +- apps/openmw/mwmp/LocalActor.cpp | 7 +- apps/openmw/mwmp/LocalPlayer.cpp | 44 +++++------ apps/openmw/mwmp/LocalPlayer.hpp | 1 - components/openmw-mp/Base/BaseActor.hpp | 2 +- components/openmw-mp/Base/BasePlayer.hpp | 2 +- .../Packets/Actor/PacketActorDeath.cpp | 15 +++- .../Packets/Player/PacketPlayerAttack.cpp | 4 - .../Packets/Player/PacketPlayerClass.hpp | 4 - .../Packets/Player/PacketPlayerDeath.cpp | 29 ++++++++ .../Packets/Player/PacketPlayerDeath.hpp | 17 +---- 23 files changed, 307 insertions(+), 111 deletions(-) create mode 100644 components/openmw-mp/Packets/Player/PacketPlayerDeath.cpp diff --git a/apps/openmw-mp/Script/Functions/Actors.cpp b/apps/openmw-mp/Script/Functions/Actors.cpp index 2d721744a..071909d2d 100644 --- a/apps/openmw-mp/Script/Functions/Actors.cpp +++ b/apps/openmw-mp/Script/Functions/Actors.cpp @@ -63,12 +63,12 @@ const char *ActorFunctions::GetActorRefId(unsigned int i) noexcept return readActorList->baseActors.at(i).refId.c_str(); } -int ActorFunctions::GetActorRefNumIndex(unsigned int i) noexcept +unsigned int ActorFunctions::GetActorRefNumIndex(unsigned int i) noexcept { return readActorList->baseActors.at(i).refNumIndex; } -int ActorFunctions::GetActorMpNum(unsigned int i) noexcept +unsigned int ActorFunctions::GetActorMpNum(unsigned int i) noexcept { return readActorList->baseActors.at(i).mpNum; } @@ -168,9 +168,39 @@ double ActorFunctions::GetActorEquipmentItemEnchantmentCharge(unsigned int i, un return readActorList->baseActors.at(i).equipmentItems[slot].enchantmentCharge; } -const char *ActorFunctions::GetActorDeathReason(unsigned int i) noexcept +bool ActorFunctions::DoesActorHavePlayerKiller(unsigned int i) noexcept { - return readActorList->baseActors.at(i).deathReason.c_str(); + return readActorList->baseActors.at(i).killer.isPlayer; +} + +int ActorFunctions::GetActorKillerPid(unsigned int i) noexcept +{ + Player *player = Players::getPlayer(readActorList->baseActors.at(i).killer.guid); + + if (player != nullptr) + return player->getId(); + + return -1; +} + +const char *ActorFunctions::GetActorKillerRefId(unsigned int i) noexcept +{ + return readActorList->baseActors.at(i).killer.refId.c_str(); +} + +unsigned int ActorFunctions::GetActorKillerRefNumIndex(unsigned int i) noexcept +{ + return readActorList->baseActors.at(i).killer.refNumIndex; +} + +unsigned int ActorFunctions::GetActorKillerMpNum(unsigned int i) noexcept +{ + return readActorList->baseActors.at(i).killer.mpNum; +} + +const char *ActorFunctions::GetActorKillerName(unsigned int i) noexcept +{ + return readActorList->baseActors.at(i).killer.name.c_str(); } bool ActorFunctions::DoesActorHavePosition(unsigned int i) noexcept diff --git a/apps/openmw-mp/Script/Functions/Actors.hpp b/apps/openmw-mp/Script/Functions/Actors.hpp index 44d104cd3..e80f2f3d8 100644 --- a/apps/openmw-mp/Script/Functions/Actors.hpp +++ b/apps/openmw-mp/Script/Functions/Actors.hpp @@ -36,7 +36,12 @@ {"GetActorEquipmentItemCharge", ActorFunctions::GetActorEquipmentItemCharge},\ {"GetActorEquipmentItemEnchantmentCharge", ActorFunctions::GetActorEquipmentItemEnchantmentCharge},\ \ - {"GetActorDeathReason", ActorFunctions::GetActorDeathReason},\ + {"DoesActorHavePlayerKiller", ActorFunctions::DoesActorHavePlayerKiller},\ + {"GetActorKillerPid", ActorFunctions::GetActorKillerPid},\ + {"GetActorKillerRefId", ActorFunctions::GetActorKillerRefId},\ + {"GetActorKillerRefNumIndex", ActorFunctions::GetActorKillerRefNumIndex},\ + {"GetActorKillerMpNum", ActorFunctions::GetActorKillerMpNum},\ + {"GetActorKillerName", ActorFunctions::GetActorKillerName},\ \ {"DoesActorHavePosition", ActorFunctions::DoesActorHavePosition},\ {"DoesActorHaveStatsDynamic", ActorFunctions::DoesActorHaveStatsDynamic},\ @@ -147,7 +152,7 @@ public: * \param i The index of the actor. * \return The refNumIndex. */ - static int GetActorRefNumIndex(unsigned int i) noexcept; + static unsigned int GetActorRefNumIndex(unsigned int i) noexcept; /** * \brief Get the mpNum of the actor at a certain index in the read actor list. @@ -155,7 +160,7 @@ public: * \param i The index of the actor. * \return The mpNum. */ - static int GetActorMpNum(unsigned int i) noexcept; + static unsigned int GetActorMpNum(unsigned int i) noexcept; /** * \brief Get the X position of the actor at a certain index in the read actor list. @@ -318,12 +323,52 @@ public: static double GetActorEquipmentItemEnchantmentCharge(unsigned int i, unsigned short slot) noexcept; /** - * \brief Get the death reason of the actor at a certain index in the read actor list. + * \brief Check whether the killer of the actor at a certain index in the read actor list is a player. * * \param i The index of the actor. - * \return The death reason. + * \return Whether the actor was killed by a player. */ - static const char *GetActorDeathReason(unsigned int i) noexcept; + static bool DoesActorHavePlayerKiller(unsigned int i) noexcept; + + /** + * \brief Get the player ID of the killer of the actor at a certain index in the read actor list. + * + * \param i The index of the actor. + * \return The player ID of the killer. + */ + static int GetActorKillerPid(unsigned int i) noexcept; + + /** + * \brief Get the refId of the actor killer of the actor at a certain index in the read actor list. + * + * \param i The index of the actor. + * \return The refId of the killer. + */ + static const char *GetActorKillerRefId(unsigned int i) noexcept; + + /** + * \brief Get the refNumIndex of the actor killer of the actor at a certain index in the read actor list. + * + * \param i The index of the actor. + * \return The refNumIndex of the killer. + */ + static unsigned int GetActorKillerRefNumIndex(unsigned int i) noexcept; + + /** + * \brief Get the mpNum of the actor killer of the actor at a certain index in the read actor list. + * + * \param i The index of the actor. + * \return The mpNum of the killer. + */ + static unsigned int GetActorKillerMpNum(unsigned int i) noexcept; + + /** + * \brief Get the name of the actor killer of the actor at a certain index in the read actor list. + * + * \param i The index of the actor. + * \return The name of the killer. + */ + static const char *GetActorKillerName(unsigned int i) noexcept; /** * \brief Check whether there is any positional data for the actor at a certain index in diff --git a/apps/openmw-mp/Script/Functions/Mechanics.cpp b/apps/openmw-mp/Script/Functions/Mechanics.cpp index f8d63be84..178dae2c4 100644 --- a/apps/openmw-mp/Script/Functions/Mechanics.cpp +++ b/apps/openmw-mp/Script/Functions/Mechanics.cpp @@ -68,6 +68,59 @@ double MechanicsFunctions::GetMarkRotZ(unsigned short pid) noexcept return player->markPosition.rot[2]; } +bool MechanicsFunctions::DoesPlayerHavePlayerKiller(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, false); + + return player->killer.isPlayer; +} + +int MechanicsFunctions::GetPlayerKillerPid(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + Player *killer = Players::getPlayer(player->killer.guid); + + if (killer != nullptr) + return killer->getId(); + + return -1; +} + +const char *MechanicsFunctions::GetPlayerKillerRefId(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ""); + + return player->killer.refId.c_str(); +} + +unsigned int MechanicsFunctions::GetPlayerKillerRefNumIndex(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + return player->killer.refNumIndex; +} + +unsigned int MechanicsFunctions::GetPlayerKillerMpNum(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + return player->killer.mpNum; +} + +const char *MechanicsFunctions::GetPlayerKillerName(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ""); + + return player->killer.name.c_str(); +} + const char *MechanicsFunctions::GetSelectedSpellId(unsigned short pid) noexcept { Player *player; @@ -160,3 +213,23 @@ void MechanicsFunctions::Resurrect(unsigned short pid, unsigned int type) noexce mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_RESURRECT)->Send(false); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_RESURRECT)->Send(true); } + +// All methods below are deprecated versions of methods from above + +const char *MechanicsFunctions::GetDeathReason(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + if (player->killer.isPlayer) + { + Player *killerPlayer = Players::getPlayer(player->killer.guid); + + if (killerPlayer != nullptr) + return killerPlayer->npc.mName.c_str(); + } + else if (!player->killer.name.empty()) + return player->killer.name.c_str(); + + return "suicide"; +} diff --git a/apps/openmw-mp/Script/Functions/Mechanics.hpp b/apps/openmw-mp/Script/Functions/Mechanics.hpp index 737c03750..b56311e63 100644 --- a/apps/openmw-mp/Script/Functions/Mechanics.hpp +++ b/apps/openmw-mp/Script/Functions/Mechanics.hpp @@ -14,6 +14,13 @@ {"GetMarkRotZ", MechanicsFunctions::GetMarkRotZ},\ {"GetSelectedSpellId", MechanicsFunctions::GetSelectedSpellId},\ \ + {"DoesPlayerHavePlayerKiller", MechanicsFunctions::DoesPlayerHavePlayerKiller},\ + {"GetPlayerKillerPid", MechanicsFunctions::GetPlayerKillerPid},\ + {"GetPlayerKillerRefId", MechanicsFunctions::GetPlayerKillerRefId},\ + {"GetPlayerKillerRefNumIndex", MechanicsFunctions::GetPlayerKillerRefNumIndex},\ + {"GetPlayerKillerMpNum", MechanicsFunctions::GetPlayerKillerMpNum},\ + {"GetPlayerKillerName", MechanicsFunctions::GetPlayerKillerName},\ + \ {"SetMarkCell", MechanicsFunctions::SetMarkCell},\ {"SetMarkPos", MechanicsFunctions::SetMarkPos},\ {"SetMarkRot", MechanicsFunctions::SetMarkRot},\ @@ -23,7 +30,9 @@ {"SendSelectedSpell", MechanicsFunctions::SendSelectedSpell},\ \ {"Jail", MechanicsFunctions::Jail},\ - {"Resurrect", MechanicsFunctions::Resurrect} + {"Resurrect", MechanicsFunctions::Resurrect},\ + \ + {"GetDeathReason", MechanicsFunctions::GetDeathReason} class MechanicsFunctions { @@ -93,6 +102,54 @@ public: */ static const char *GetSelectedSpellId(unsigned short pid) noexcept; + /** + * \brief Check whether the killer of a certain player is also a player. + * + * \param pid The player ID of the killed player. + * \return Whether the player was killed by another player. + */ + static bool DoesPlayerHavePlayerKiller(unsigned short pid) noexcept; + + /** + * \brief Get the player ID of the killer of a certain player. + * + * \param pid The player ID of the killed player. + * \return The player ID of the killer. + */ + static int GetPlayerKillerPid(unsigned short pid) noexcept; + + /** + * \brief Get the refId of the actor killer of a certain player. + * + * \param pid The player ID of the killed player. + * \return The refId of the killer. + */ + static const char *GetPlayerKillerRefId(unsigned short pid) noexcept; + + /** + * \brief Get the refNumIndex of the actor killer of a certain player. + * + * \param pid The player ID of the killed player. + * \return The refNumIndex of the killer. + */ + static unsigned int GetPlayerKillerRefNumIndex(unsigned short pid) noexcept; + + /** + * \brief Get the mpNum of the actor killer of a certain player. + * + * \param pid The player ID of the killed player. + * \return The mpNum of the killer. + */ + static unsigned int GetPlayerKillerMpNum(unsigned short pid) noexcept; + + /** + * \brief Get the name of the actor killer of a certain player. + * + * \param pid The player ID of the killed player. + * \return The name of the killer. + */ + static const char *GetPlayerKillerName(unsigned short pid) noexcept; + /** * \brief Set the Mark cell of a player. * @@ -197,6 +254,10 @@ public: * \return void */ static void Resurrect(unsigned short pid, unsigned int type) noexcept; + + // All methods below are deprecated versions of methods from above + + static const char *GetDeathReason(unsigned short pid) noexcept; }; #endif //OPENMW_MECHANICSAPI_HPP diff --git a/apps/openmw-mp/Script/Functions/Objects.cpp b/apps/openmw-mp/Script/Functions/Objects.cpp index a1a21dcde..f55d3fe42 100644 --- a/apps/openmw-mp/Script/Functions/Objects.cpp +++ b/apps/openmw-mp/Script/Functions/Objects.cpp @@ -63,12 +63,12 @@ const char *ObjectFunctions::GetObjectRefId(unsigned int i) noexcept return readObjectList->baseObjects.at(i).refId.c_str(); } -int ObjectFunctions::GetObjectRefNumIndex(unsigned int i) noexcept +unsigned int ObjectFunctions::GetObjectRefNumIndex(unsigned int i) noexcept { return readObjectList->baseObjects.at(i).refNumIndex; } -int ObjectFunctions::GetObjectMpNum(unsigned int i) noexcept +unsigned int ObjectFunctions::GetObjectMpNum(unsigned int i) noexcept { return readObjectList->baseObjects.at(i).mpNum; } @@ -143,12 +143,12 @@ const char *ObjectFunctions::GetObjectSummonerRefId(unsigned int i) noexcept return readObjectList->baseObjects.at(i).master.refId.c_str(); } -int ObjectFunctions::GetObjectSummonerRefNumIndex(unsigned int i) noexcept +unsigned int ObjectFunctions::GetObjectSummonerRefNumIndex(unsigned int i) noexcept { return readObjectList->baseObjects.at(i).master.refNumIndex; } -int ObjectFunctions::GetObjectSummonerMpNum(unsigned int i) noexcept +unsigned int ObjectFunctions::GetObjectSummonerMpNum(unsigned int i) noexcept { return readObjectList->baseObjects.at(i).master.mpNum; } diff --git a/apps/openmw-mp/Script/Functions/Objects.hpp b/apps/openmw-mp/Script/Functions/Objects.hpp index cc4289694..d1af735a2 100644 --- a/apps/openmw-mp/Script/Functions/Objects.hpp +++ b/apps/openmw-mp/Script/Functions/Objects.hpp @@ -179,7 +179,7 @@ public: * \param i The index of the object. * \return The refNumIndex. */ - static int GetObjectRefNumIndex(unsigned int i) noexcept; + static unsigned int GetObjectRefNumIndex(unsigned int i) noexcept; /** * \brief Get the mpNum of the object at a certain index in the read object list's object changes. @@ -187,7 +187,7 @@ public: * \param i The index of the object. * \return The mpNum. */ - static int GetObjectMpNum(unsigned int i) noexcept; + static unsigned int GetObjectMpNum(unsigned int i) noexcept; /** * \brief Get the count of the object at a certain index in the read object list's object changes. @@ -317,7 +317,7 @@ public: * \param i The index of the object. * \return The refNumIndex of the summoner. */ - static int GetObjectSummonerRefNumIndex(unsigned int i) noexcept; + static unsigned int GetObjectSummonerRefNumIndex(unsigned int i) noexcept; /** * \brief Get the mpNum of the actor summoner of the object at a certain index in the read object list's @@ -326,7 +326,7 @@ public: * \param i The index of the object. * \return The mpNum of the summoner. */ - static int GetObjectSummonerMpNum(unsigned int i) noexcept; + static unsigned int GetObjectSummonerMpNum(unsigned int i) noexcept; /** * \brief Get the X position of the object at a certain index in the read object list's object diff --git a/apps/openmw-mp/Script/Functions/Stats.cpp b/apps/openmw-mp/Script/Functions/Stats.cpp index 9a3cb2c0c..56908dcba 100644 --- a/apps/openmw-mp/Script/Functions/Stats.cpp +++ b/apps/openmw-mp/Script/Functions/Stats.cpp @@ -253,14 +253,6 @@ int StatsFunctions::GetBounty(unsigned short pid) noexcept return player->npcStats.mBounty; } -const char *StatsFunctions::GetDeathReason(unsigned short pid) noexcept -{ - Player *player; - GET_PLAYER(pid, player, 0); - - return player->deathReason.c_str(); -} - void StatsFunctions::SetName(unsigned short pid, const char *name) noexcept { Player *player; diff --git a/apps/openmw-mp/Script/Functions/Stats.hpp b/apps/openmw-mp/Script/Functions/Stats.hpp index e85aa7a97..cd502adb1 100644 --- a/apps/openmw-mp/Script/Functions/Stats.hpp +++ b/apps/openmw-mp/Script/Functions/Stats.hpp @@ -38,8 +38,6 @@ \ {"GetBounty", StatsFunctions::GetBounty},\ \ - {"GetDeathReason", StatsFunctions::GetDeathReason},\ - \ {"SetName", StatsFunctions::SetName},\ {"SetRace", StatsFunctions::SetRace},\ {"SetHead", StatsFunctions::SetHead},\ @@ -316,16 +314,6 @@ public: */ static int GetBounty(unsigned short pid) noexcept; - /** - * \brief Get the reason for a player's death. - * - * As of now, the reason is either "suicide" or the name of the killer. - * - * \param pid The player ID. - * \return The reason. - */ - static const char *GetDeathReason(unsigned short pid) noexcept; - /** * \brief Set the name of a player. * diff --git a/apps/openmw-mp/Script/ScriptFunctions.cpp b/apps/openmw-mp/Script/ScriptFunctions.cpp index 56ec7b6ef..033447fae 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.cpp +++ b/apps/openmw-mp/Script/ScriptFunctions.cpp @@ -132,7 +132,7 @@ const char *ScriptFunctions::GetProtocolVersion() noexcept int ScriptFunctions::GetAvgPing(unsigned short pid) noexcept { Player *player; - GET_PLAYER(pid, player,-1); + GET_PLAYER(pid, player, -1); return mwmp::Networking::get().getAvgPing(player->guid); } diff --git a/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp b/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp index 1b76a77df..6fba0021c 100644 --- a/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp +++ b/apps/openmw-mp/processors/player/ProcessorPlayerDeath.hpp @@ -1,7 +1,3 @@ -// -// Created by koncord on 01.04.17. -// - #ifndef OPENMW_PROCESSORPLAYERDEATH_HPP #define OPENMW_PROCESSORPLAYERDEATH_HPP diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index 2fef3148f..edac34db2 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -561,7 +561,7 @@ namespace MWClass { if (getCreatureStats(ptr).isDead()) { - mwmp::Main::get().getCellController()->getLocalActor(ptr)->deathReason = attacker.getClass().getName(attacker); + mwmp::Main::get().getCellController()->getLocalActor(ptr)->killer = MechanicsHelper::getTarget(attacker); } } /* diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 13ca67c3f..a9bfae49e 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -975,20 +975,20 @@ namespace MWClass 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); + mwmp::Main::get().getLocalPlayer()->killer = MechanicsHelper::getTarget(attacker); } + + mwmp::Main::get().getLocalPlayer()->updateStatsDynamic(true); + mwmp::Main::get().getLocalPlayer()->updatePosition(true); // fix position after getting damage; } else if (mwmp::Main::get().getCellController()->isLocalActor(ptr)) { if (getCreatureStats(ptr).isDead()) { - mwmp::Main::get().getCellController()->getLocalActor(ptr)->deathReason = attacker.getClass().getName(attacker); + mwmp::Main::get().getCellController()->getLocalActor(ptr)->killer = MechanicsHelper::getTarget(attacker); } } /* diff --git a/apps/openmw/mwmechanics/spellcasting.cpp b/apps/openmw/mwmechanics/spellcasting.cpp index 0285a3e09..e4d5a7843 100644 --- a/apps/openmw/mwmechanics/spellcasting.cpp +++ b/apps/openmw/mwmechanics/spellcasting.cpp @@ -544,13 +544,13 @@ namespace MWMechanics if (target == MWMechanics::getPlayer()) { - mwmp::Main::get().getLocalPlayer()->deathReason = isSuicide ? - "suicide" : caster.getClass().getName(caster); + mwmp::Main::get().getLocalPlayer()->killer = isSuicide ? + MechanicsHelper::getTarget(target) : MechanicsHelper::getTarget(caster); } else if (mwmp::Main::get().getCellController()->isLocalActor(target)) { - mwmp::Main::get().getCellController()->getLocalActor(target)->deathReason = isSuicide ? - "suicide" : caster.getClass().getName(caster); + mwmp::Main::get().getCellController()->getLocalActor(target)->killer = isSuicide ? + MechanicsHelper::getTarget(target) : MechanicsHelper::getTarget(caster); } } /* diff --git a/apps/openmw/mwmp/LocalActor.cpp b/apps/openmw/mwmp/LocalActor.cpp index 922720bf8..df0b5d32a 100644 --- a/apps/openmw/mwmp/LocalActor.cpp +++ b/apps/openmw/mwmp/LocalActor.cpp @@ -183,16 +183,15 @@ void LocalActor::updateStatsDynamic(bool forceUpdate) if (creatureStats.mDead && !wasDead) { - if (deathReason.empty()) - deathReason = "suicide"; + if (MechanicsHelper::isEmptyTarget(killer)) + killer = MechanicsHelper::getTarget(ptr); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_ACTOR_DEATH about %s-%i-%i to server", refId.c_str(), refNumIndex, mpNum); - LOG_APPEND(Log::LOG_INFO, "- deathReason was %s", deathReason.c_str()); mwmp::Main::get().getNetworking()->getActorList()->addDeathActor(*this); - deathReason = ""; + MechanicsHelper::clearTarget(killer); } wasDead = creatureStats.mDead; diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 283fdfbe4..5a3acc15d 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -61,7 +61,6 @@ LocalPlayer::LocalPlayer() attack.shouldSend = false; - deathReason = "suicide"; isChangingRegion = false; jailProgressText = ""; @@ -102,7 +101,6 @@ void LocalPlayer::update() updatePosition(); updateAnimFlags(); updateAttack(); - updateDeadState(); updateEquipment(); updateStatsDynamic(); updateAttributes(); @@ -234,9 +232,27 @@ void LocalPlayer::updateStatsDynamic(bool forceUpdate) magicka.writeState(creatureStats.mDynamic[1]); fatigue.writeState(creatureStats.mDynamic[2]); + creatureStats.mDead = ptrCreatureStats->isDead(); + exchangeFullInfo = false; getNetworking()->getPlayerPacket(ID_PLAYER_STATS_DYNAMIC)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_STATS_DYNAMIC)->Send(); + + static bool wasDead = false; + + if (creatureStats.mDead && !wasDead) + { + if (MechanicsHelper::isEmptyTarget(killer)) + killer = MechanicsHelper::getTarget(getPlayerPtr()); + + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_PLAYER_DEATH about myself to server"); + getNetworking()->getPlayerPacket(ID_PLAYER_DEATH)->setPlayer(this); + getNetworking()->getPlayerPacket(ID_PLAYER_DEATH)->Send(); + + MechanicsHelper::clearTarget(killer); + } + + wasDead = creatureStats.mDead; } } @@ -572,30 +588,6 @@ void LocalPlayer::updateAttack() } } -void LocalPlayer::updateDeadState(bool forceUpdate) -{ - MWWorld::Ptr ptrPlayer = getPlayerPtr(); - - MWMechanics::NpcStats *ptrNpcStats = &ptrPlayer.getClass().getNpcStats(ptrPlayer); - static bool isDead = false; - - if (ptrNpcStats->isDead() && !isDead) - { - 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) { MWBase::World *world = MWBase::Environment::get().getWorld(); diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 0231ccc4b..4e67a8605 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -31,7 +31,6 @@ namespace mwmp void updateEquipment(bool forceUpdate = false); void updateInventory(bool forceUpdate = false); void updateAttack(); - void updateDeadState(bool forceUpdate = false); void updateAnimFlags(bool forceUpdate = false); void addItems(); diff --git a/components/openmw-mp/Base/BaseActor.hpp b/components/openmw-mp/Base/BaseActor.hpp index 0441c8b7d..d41cfbdb4 100644 --- a/components/openmw-mp/Base/BaseActor.hpp +++ b/components/openmw-mp/Base/BaseActor.hpp @@ -39,7 +39,7 @@ namespace mwmp Animation animation; Attack attack; - std::string deathReason; + Target killer; bool hasAiTarget; Target aiTarget; diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 4b2c8dac1..33e48f0b9 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -306,7 +306,7 @@ namespace mwmp bool isChangingRegion; - std::string deathReason; + Target killer; int jailDays; bool ignoreJailTeleportation; diff --git a/components/openmw-mp/Packets/Actor/PacketActorDeath.cpp b/components/openmw-mp/Packets/Actor/PacketActorDeath.cpp index 0e8982fb1..8d55e287d 100644 --- a/components/openmw-mp/Packets/Actor/PacketActorDeath.cpp +++ b/components/openmw-mp/Packets/Actor/PacketActorDeath.cpp @@ -11,5 +11,18 @@ PacketActorDeath::PacketActorDeath(RakNet::RakPeerInterface *peer) : ActorPacket void PacketActorDeath::Actor(BaseActor &actor, bool send) { - RW(actor.deathReason, send); + RW(actor.killer.isPlayer, send); + + if (actor.killer.isPlayer) + { + RW(actor.killer.guid, send); + } + else + { + RW(actor.killer.refId, send, true); + RW(actor.killer.refNumIndex, send); + RW(actor.killer.mpNum, send); + + RW(actor.killer.name, send, true); + } } diff --git a/components/openmw-mp/Packets/Player/PacketPlayerAttack.cpp b/components/openmw-mp/Packets/Player/PacketPlayerAttack.cpp index 6b5807060..2aa17e61c 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerAttack.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerAttack.cpp @@ -1,7 +1,3 @@ -// -// Created by koncord on 13.01.16. -// - #include #include "PacketPlayerAttack.hpp" diff --git a/components/openmw-mp/Packets/Player/PacketPlayerClass.hpp b/components/openmw-mp/Packets/Player/PacketPlayerClass.hpp index 33c685f65..f8f6c1f3b 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerClass.hpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerClass.hpp @@ -1,7 +1,3 @@ -// -// Created by koncord on 29.08.16. -// - #ifndef OPENMW_PACKETPLAYERCLASS_HPP #define OPENMW_PACKETPLAYERCLASS_HPP diff --git a/components/openmw-mp/Packets/Player/PacketPlayerDeath.cpp b/components/openmw-mp/Packets/Player/PacketPlayerDeath.cpp new file mode 100644 index 000000000..8e0e5e511 --- /dev/null +++ b/components/openmw-mp/Packets/Player/PacketPlayerDeath.cpp @@ -0,0 +1,29 @@ +#include "PacketPlayerDeath.hpp" +#include + +using namespace mwmp; + +PacketPlayerDeath::PacketPlayerDeath(RakNet::RakPeerInterface *peer) : PlayerPacket(peer) +{ + packetID = ID_PLAYER_DEATH; +} + +void PacketPlayerDeath::Packet(RakNet::BitStream *bs, bool send) +{ + PlayerPacket::Packet(bs, send); + + RW(player->killer.isPlayer, send); + + if (player->killer.isPlayer) + { + RW(player->killer.guid, send); + } + else + { + RW(player->killer.refId, send, true); + RW(player->killer.refNumIndex, send); + RW(player->killer.mpNum, send); + + RW(player->killer.name, send, true); + } +} diff --git a/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp b/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp index ba48a56ee..fd2d903e1 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerDeath.hpp @@ -1,29 +1,16 @@ -// -// Created by koncord on 13.01.16. -// - #ifndef OPENMW_PACKETPLAYERDEATH_HPP #define OPENMW_PACKETPLAYERDEATH_HPP - #include -#include namespace mwmp { class PacketPlayerDeath: public PlayerPacket { public: - PacketPlayerDeath(RakNet::RakPeerInterface *peer) : PlayerPacket(peer) - { - packetID = ID_PLAYER_DEATH; - } - void Packet(RakNet::BitStream *bs, bool send) - { - PlayerPacket::Packet(bs, send); + PacketPlayerDeath(RakNet::RakPeerInterface *peer); - RW(player->deathReason, send, true); - } + virtual void Packet(RakNet::BitStream *bs, bool send); }; }