From 1d504a665fc09b6eabf4e135ec98e78293b4f70c Mon Sep 17 00:00:00 2001 From: David Cernat Date: Wed, 5 Jul 2017 07:05:52 +0300 Subject: [PATCH] [General] Add RANK, EXPULSION and BOTH actions to PlayerFaction packets --- apps/openmw-mp/Script/Functions/Factions.cpp | 10 ++++ apps/openmw-mp/Script/Functions/Factions.hpp | 2 + apps/openmw/mwgui/jailscreen.cpp | 1 - .../mwmechanics/mechanicsmanagerimp.cpp | 2 +- apps/openmw/mwmp/LocalPlayer.cpp | 51 +++++++++++++------ apps/openmw/mwmp/LocalPlayer.hpp | 3 +- apps/openmw/mwscript/statsextensions.cpp | 10 ++-- components/openmw-mp/Base/BasePlayer.hpp | 9 ++++ .../Packets/Player/PacketPlayerFaction.cpp | 8 ++- 9 files changed, 72 insertions(+), 24 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Factions.cpp b/apps/openmw-mp/Script/Functions/Factions.cpp index a5e945a13..a15b53d5c 100644 --- a/apps/openmw-mp/Script/Functions/Factions.cpp +++ b/apps/openmw-mp/Script/Functions/Factions.cpp @@ -14,6 +14,14 @@ unsigned int FactionFunctions::GetFactionChangesSize(unsigned short pid) noexcep return player->factionChanges.count; } +unsigned char FactionFunctions::GetFactionChangesAction(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, 0); + + return player->factionChanges.action; +} + void FactionFunctions::AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept { Player *player; @@ -59,6 +67,8 @@ void FactionFunctions::SendFactionChanges(unsigned short pid) noexcept Player *player; GET_PLAYER(pid, player, ); + player->factionChangesBuffer.action = mwmp::FactionChanges::BOTH; + std::swap(player->factionChanges, player->factionChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION)->setPlayer(player); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION)->Send(false); diff --git a/apps/openmw-mp/Script/Functions/Factions.hpp b/apps/openmw-mp/Script/Functions/Factions.hpp index 7292594c2..d7d633254 100644 --- a/apps/openmw-mp/Script/Functions/Factions.hpp +++ b/apps/openmw-mp/Script/Functions/Factions.hpp @@ -3,6 +3,7 @@ #define FACTIONAPI \ {"GetFactionChangesSize", FactionFunctions::GetFactionChangesSize},\ + {"GetFactionChangesAction", FactionFunctions::GetFactionChangesAction},\ \ {"AddFaction", FactionFunctions::AddFaction},\ \ @@ -17,6 +18,7 @@ class FactionFunctions public: static unsigned int GetFactionChangesSize(unsigned short pid) noexcept; + static unsigned char GetFactionChangesAction(unsigned short pid) noexcept; static void AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept; diff --git a/apps/openmw/mwgui/jailscreen.cpp b/apps/openmw/mwgui/jailscreen.cpp index 50c7474cc..c35260aed 100644 --- a/apps/openmw/mwgui/jailscreen.cpp +++ b/apps/openmw/mwgui/jailscreen.cpp @@ -7,7 +7,6 @@ */ #include "../mwmp/Main.hpp" #include "../mwmp/LocalPlayer.hpp" -#include /* End of tes3mp addition */ diff --git a/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp b/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp index 733f0d2c3..bd0ad9a06 100644 --- a/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp +++ b/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp @@ -1252,7 +1252,7 @@ namespace MWMechanics Send an ID_PLAYER_FACTION packet every time a player is expelled from a faction */ - mwmp::Main::get().getLocalPlayer()->sendFaction(Misc::StringUtils::lowerCase(factionID), playerRanks.at(Misc::StringUtils::lowerCase(factionID)), true); + mwmp::Main::get().getLocalPlayer()->sendFactionExpulsionState(Misc::StringUtils::lowerCase(factionID), true); /* End of tes3mp addition */ diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 4d678fd04..f9333b225 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -955,24 +955,30 @@ void LocalPlayer::setFactions() if (!ptrNpcStats.isInFaction(faction.factionId)) ptrNpcStats.joinFaction(faction.factionId); - // While the faction rank is different in the packet than in the NpcStats, - // adjust the NpcStats accordingly - while (faction.rank != ptrNpcStats.getFactionRanks().at(faction.factionId)) + if (factionChanges.action == mwmp::FactionChanges::RANK || factionChanges.action == mwmp::FactionChanges::BOTH) { - if (faction.rank > ptrNpcStats.getFactionRanks().at(faction.factionId)) - ptrNpcStats.raiseRank(faction.factionId); - else - ptrNpcStats.lowerRank(faction.factionId); + // While the faction rank is different in the packet than in the NpcStats, + // adjust the NpcStats accordingly + while (faction.rank != ptrNpcStats.getFactionRanks().at(faction.factionId)) + { + if (faction.rank > ptrNpcStats.getFactionRanks().at(faction.factionId)) + ptrNpcStats.raiseRank(faction.factionId); + else + ptrNpcStats.lowerRank(faction.factionId); + } } - // If the expelled state is different in the packet than in the NpcStats, - // adjust the NpcStats accordingly - if (faction.isExpelled != ptrNpcStats.getExpelled(faction.factionId)) + if (factionChanges.action == mwmp::FactionChanges::EXPULSION || factionChanges.action == mwmp::FactionChanges::BOTH) { - if (faction.isExpelled) - ptrNpcStats.expell(faction.factionId); - else - ptrNpcStats.clearExpelled(faction.factionId); + // If the expelled state is different in the packet than in the NpcStats, + // adjust the NpcStats accordingly + if (faction.isExpelled != ptrNpcStats.getExpelled(faction.factionId)) + { + if (faction.isExpelled) + ptrNpcStats.expell(faction.factionId); + else + ptrNpcStats.clearExpelled(faction.factionId); + } } } } @@ -1151,13 +1157,28 @@ void LocalPlayer::sendJournalIndex(const std::string& quest, int index) getNetworking()->getPlayerPacket(ID_PLAYER_JOURNAL)->Send(); } -void LocalPlayer::sendFaction(const std::string& factionId, int rank, bool isExpelled) +void LocalPlayer::sendFactionRank(const std::string& factionId, int rank) { factionChanges.factions.clear(); + factionChanges.action = FactionChanges::RANK; mwmp::Faction faction; faction.factionId = factionId; faction.rank = rank; + + factionChanges.factions.push_back(faction); + + getNetworking()->getPlayerPacket(ID_PLAYER_FACTION)->setPlayer(this); + getNetworking()->getPlayerPacket(ID_PLAYER_FACTION)->Send(); +} + +void LocalPlayer::sendFactionExpulsionState(const std::string& factionId, bool isExpelled) +{ + factionChanges.factions.clear(); + factionChanges.action = FactionChanges::EXPULSION; + + mwmp::Faction faction; + faction.factionId = factionId; faction.isExpelled = isExpelled; factionChanges.factions.push_back(faction); diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 425b825c6..c67723b75 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -73,7 +73,8 @@ namespace mwmp void sendSpellRemoval(const ESM::Spell &spell); void sendJournalEntry(const std::string& quest, int index, const MWWorld::Ptr& actor); void sendJournalIndex(const std::string& quest, int index); - void sendFaction(const std::string& factionId, int rank, bool isExpelled); + void sendFactionRank(const std::string& factionId, int rank); + void sendFactionExpulsionState(const std::string& factionId, bool isExpelled); void sendTopic(const std::string& topic); void sendKill(const std::string& refId, int number); void sendBook(const std::string& bookId); diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp index 0c8189df1..e782fd091 100644 --- a/apps/openmw/mwscript/statsextensions.cpp +++ b/apps/openmw/mwscript/statsextensions.cpp @@ -609,7 +609,7 @@ namespace MWScript Send an ID_PLAYER_FACTION packet every time a player joins a faction */ - mwmp::Main::get().getLocalPlayer()->sendFaction(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID), false); + mwmp::Main::get().getLocalPlayer()->sendFactionRank(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID)); /* End of tes3mp addition */ @@ -658,7 +658,7 @@ namespace MWScript Send an ID_PLAYER_FACTION packet every time a player rises in a faction */ - mwmp::Main::get().getLocalPlayer()->sendFaction(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID), player.getClass().getNpcStats(player).getExpelled(factionID)); + mwmp::Main::get().getLocalPlayer()->sendFactionRank(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID)); /* End of tes3mp addition */ @@ -700,7 +700,7 @@ namespace MWScript Send an ID_PLAYER_FACTION packet every time a player falls in a faction */ - mwmp::Main::get().getLocalPlayer()->sendFaction(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID), player.getClass().getNpcStats(player).getExpelled(factionID)); + mwmp::Main::get().getLocalPlayer()->sendFactionRank(factionID, player.getClass().getNpcStats(player).getFactionRanks().at(factionID)); /* End of tes3mp addition */ @@ -1035,7 +1035,7 @@ namespace MWScript Send an ID_PLAYER_FACTION packet every time a player is expelled from a faction */ - mwmp::Main::get().getLocalPlayer()->sendFaction(Misc::StringUtils::lowerCase(factionID), player.getClass().getNpcStats(player).getFactionRanks().at(Misc::StringUtils::lowerCase(factionID)), true); + mwmp::Main::get().getLocalPlayer()->sendFactionExpulsionState(Misc::StringUtils::lowerCase(factionID), true); /* End of tes3mp addition */ @@ -1072,7 +1072,7 @@ namespace MWScript Send an ID_PLAYER_FACTION packet every time a player is no longer expelled from a faction */ if (factionID != "") - mwmp::Main::get().getLocalPlayer()->sendFaction(Misc::StringUtils::lowerCase(factionID), player.getClass().getNpcStats(player).getFactionRanks().at(Misc::StringUtils::lowerCase(factionID)), false); + mwmp::Main::get().getLocalPlayer()->sendFactionExpulsionState(Misc::StringUtils::lowerCase(factionID), false); /* End of tes3mp addition */ diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 6dfb4c372..adffe849d 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -87,6 +87,15 @@ namespace mwmp { std::vector factions; unsigned int count; + + enum FACTION_ACTION + { + RANK = 0, + EXPULSION = 1, + BOTH = 2 + }; + + int action; // 0 - Rank, 1 - Expulsion state, 2 - Both }; struct TopicChanges diff --git a/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp b/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp index 2b5c6a387..554b6dc0a 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp @@ -13,6 +13,8 @@ void PacketPlayerFaction::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); + RW(player->factionChanges.action, send); + if (send) player->factionChanges.count = (unsigned int)(player->factionChanges.factions.size()); else @@ -28,7 +30,11 @@ void PacketPlayerFaction::Packet(RakNet::BitStream *bs, bool send) faction = player->factionChanges.factions.at(i); RW(faction.factionId, send, 1); - RW(faction.rank, send); + + if (player->factionChanges.action == FactionChanges::BOTH || player->factionChanges.action == FactionChanges::RANK) + RW(faction.rank, send); + + if (player->factionChanges.action == FactionChanges::BOTH || player->factionChanges.action == FactionChanges::EXPULSION) RW(faction.isExpelled, send); if (!send)