From 79532cf797d6f7afa7f2efc034a6a444ffc6f723 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sat, 8 Oct 2016 15:15:43 +0800 Subject: [PATCH] Detecting reason of death --- apps/openmw-mp/Networking.cpp | 34 +++++++++++++++++------ apps/openmw-mp/Player.cpp | 18 ++++++++++++ apps/openmw-mp/Player.hpp | 5 ++++ apps/openmw-mp/Script/ScriptFunctions.hpp | 2 +- 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index de3798db7..76df2b304 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -247,17 +247,22 @@ void Networking::Update(RakNet::Packet *packet) { myPacket->Read(player); -#if defined(DEBUG) - cout << "Player: " << player->Npc()->mName << " atk state: " << (player->GetAttack()->pressed == 1) << - endl; + Player *target = Players::GetPlayer(player->GetAttack()->target); + + if (target == nullptr) + target = player; + + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player: %s attaked %s state: %d", player->Npc()->mName.c_str(), + target->Npc()->mName.c_str(), player->GetAttack()->pressed == 1); if (player->GetAttack()->pressed == 0) { - cout << "success: " << (player->GetAttack()->success == 1); + LOG_APPEND(Log::LOG_VERBOSE, "success: %d", player->GetAttack()->success == 1); if (player->GetAttack()->success == 1) - cout << " damage: " << player->GetAttack()->damage; - cout << endl; + { + LOG_APPEND(Log::LOG_VERBOSE, "damage: %d", player->GetAttack()->damage == 1); + player->setLastAttackerID(target->GetID()); + } } -#endif myPacket->Send(player, true); controller->GetPacket(ID_GAME_DYNAMICSTATS)->RequestData(player->GetAttack()->target); @@ -278,10 +283,23 @@ void Networking::Update(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_GAME_DIE from %s", player->Npc()->mName.c_str()); + Player *killer = Players::GetPlayer(player->getLastAttackerID()); + + short reason = 0; // unknown; + + if (!killer) + killer = player; + else if(killer->GetID() == player->GetID()) + reason = 2; //suicide + else + reason = 1; //killed + + player->resetLastAttacker(); + player->CreatureStats()->mDead = true; myPacket->Send(player, true); - Script::Call(player->GetID()); + Script::Call(player->GetID(), reason, killer->GetID()); break; } diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 19c733ca8..15e7204bb 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -65,6 +65,7 @@ Player::Player(RakNet::RakNetGUID id) : BasePlayer(id) { handshake = false; loaded = false; + lastAttacker = 0; } Player::~Player() @@ -104,5 +105,22 @@ int Player::LoadedState() Player *Players::GetPlayer(unsigned short id) { + if (slots.find(id) == slots.end()) + return nullptr; return slots[id]; } + +void Player::setLastAttackerID(unsigned short pid) +{ + lastAttacker = pid; +} + +void Player::resetLastAttacker() +{ + lastAttacker = id; +} + +unsigned short Player::getLastAttackerID() +{ + return lastAttacker; +} diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 7fbc5275b..9eecfbf92 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -57,10 +57,15 @@ public: void Loaded(int state); int LoadedState(); + void setLastAttackerID(unsigned short pid); + void resetLastAttacker(); + unsigned short getLastAttackerID(); + virtual ~Player(); private: bool handshake; int loaded; + unsigned short lastAttacker; }; #endif //OPENMW_PLAYER_HPP diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index 8b8db7df8..c6282a4cc 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -98,7 +98,7 @@ public: {"OnServerExit", Function()}, {"OnPlayerConnect", Function()}, {"OnPlayerDisconnect", Function()}, - {"OnPlayerDeath", Function()}, + {"OnPlayerDeath", Function()}, {"OnPlayerResurrect", Function()}, {"OnPlayerChangeCell", Function()}, {"OnPlayerChangeAttributes", Function()},