[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.
0.6.3
David Cernat 7 years ago
parent 934e592bdb
commit c075496748

@ -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

@ -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

@ -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";
}

@ -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

@ -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;
}

@ -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

@ -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;

@ -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.
*

@ -1,7 +1,3 @@
//
// Created by koncord on 01.04.17.
//
#ifndef OPENMW_PROCESSORPLAYERDEATH_HPP
#define OPENMW_PROCESSORPLAYERDEATH_HPP

@ -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);
}
}
/*

@ -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);
}
}
/*

@ -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);
}
}
/*

@ -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;

@ -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();

@ -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();

@ -39,7 +39,7 @@ namespace mwmp
Animation animation;
Attack attack;
std::string deathReason;
Target killer;
bool hasAiTarget;
Target aiTarget;

@ -306,7 +306,7 @@ namespace mwmp
bool isChangingRegion;
std::string deathReason;
Target killer;
int jailDays;
bool ignoreJailTeleportation;

@ -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);
}
}

@ -1,7 +1,3 @@
//
// Created by koncord on 13.01.16.
//
#include <components/openmw-mp/NetworkMessages.hpp>
#include "PacketPlayerAttack.hpp"

@ -1,7 +1,3 @@
//
// Created by koncord on 29.08.16.
//
#ifndef OPENMW_PACKETPLAYERCLASS_HPP
#define OPENMW_PACKETPLAYERCLASS_HPP

@ -0,0 +1,29 @@
#include "PacketPlayerDeath.hpp"
#include <components/openmw-mp/NetworkMessages.hpp>
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);
}
}

@ -1,29 +1,16 @@
//
// Created by koncord on 13.01.16.
//
#ifndef OPENMW_PACKETPLAYERDEATH_HPP
#define OPENMW_PACKETPLAYERDEATH_HPP
#include <components/openmw-mp/Packets/Player/PlayerPacket.hpp>
#include <components/openmw-mp/NetworkMessages.hpp>
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);
};
}

Loading…
Cancel
Save