From 7e5b929ea28c255c6404535e8eba28093618c59b Mon Sep 17 00:00:00 2001 From: Koncord Date: Sat, 20 Jan 2018 19:45:24 +0800 Subject: [PATCH] [General] Rework PacketPlayerFaction --- apps/openmw-mp/Factions.cpp | 21 +++++----------- apps/openmw-mp/Factions.hpp | 8 ++---- apps/openmw/mwmp/LocalPlayer.cpp | 13 +++++----- components/openmw-mp/Base/BasePlayer.hpp | 25 ++++++++++++------- .../Packets/Player/PacketPlayerFaction.cpp | 11 ++++---- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/apps/openmw-mp/Factions.cpp b/apps/openmw-mp/Factions.cpp index 1dedbf17f..20a701f10 100644 --- a/apps/openmw-mp/Factions.cpp +++ b/apps/openmw-mp/Factions.cpp @@ -14,7 +14,6 @@ void Factions::Init(LuaState &lua) { lua.getState()->new_usertype("Factions", "addFaction", &Factions::addFaction, - "changesAction", sol::property(&Factions::getFactionChangesAction, &Factions::setFactionChangesAction), "getFaction", &Factions::getFaction, "setFaction", &Factions::setFaction, "clear", &Factions::clear, @@ -40,20 +39,9 @@ void Factions::processUpdate() clear(); } -mwmp::FactionChanges::Type Factions::getFactionChangesAction() const +void Factions::addFaction(const Faction &faction) { - return player->factionChanges.action; -} - -void Factions::setFactionChangesAction(mwmp::FactionChanges::Type action) -{ - player->factionChanges.action = action; - setChanged(); -} - -void Factions::addFaction(Faction faction) -{ - player->factionChanges.factions.push_back(faction.faction); + player->factionChanges.factions.emplace_back(faction.faction); setChanged(); } @@ -63,7 +51,7 @@ Faction Factions::getFaction(int id) const return Faction(player->factionChanges.factions.at(id)); } -void Factions::setFaction(int id, Faction faction) +void Factions::setFaction(int id, const Faction &faction) { player->factionChanges.factions.at(id) = faction.faction; setChanged(); @@ -112,6 +100,7 @@ int Faction::getFactionRank() const void Faction::setFactionRank(unsigned int rank) { + faction.rankChanged(); faction.rank = rank; } @@ -122,6 +111,7 @@ bool Faction::getFactionExpulsionState() const void Faction::setFactionExpulsionState(bool expulsionState) { + faction.expulsionChanged(); faction.isExpelled = expulsionState; } @@ -132,5 +122,6 @@ int Faction::getFactionReputation() const void Faction::setFactionReputation(int reputation) { + faction.reputationChanged(); faction.reputation = reputation; } diff --git a/apps/openmw-mp/Factions.hpp b/apps/openmw-mp/Factions.hpp index f695d3620..3f4e04e7a 100644 --- a/apps/openmw-mp/Factions.hpp +++ b/apps/openmw-mp/Factions.hpp @@ -42,13 +42,9 @@ public: explicit Factions(Player *player); ~Factions(); - - mwmp::FactionChanges::Type getFactionChangesAction() const; - void setFactionChangesAction(mwmp::FactionChanges::Type action); - - void addFaction(Faction faction); + void addFaction(const Faction &faction); Faction getFaction(int id) const; - void setFaction(int id, Faction faction); + void setFaction(int id, const Faction &faction); size_t size() const; void clear(); diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 050e5f65f..84c8e06dc 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -1083,7 +1083,7 @@ void LocalPlayer::setFactions() if (!ptrNpcStats.isInFaction(faction.factionId)) ptrNpcStats.joinFaction(faction.factionId); - if (factionChanges.action == mwmp::FactionChanges::Type::Rank) + if (faction.isRankChanged()) { // While the faction rank is different in the packet than in the NpcStats, // adjust the NpcStats accordingly @@ -1095,7 +1095,8 @@ void LocalPlayer::setFactions() ptrNpcStats.lowerRank(faction.factionId); } } - else if (factionChanges.action == mwmp::FactionChanges::Type::Expulsion) + + if (faction.isExpulsionChanged()) { // If the expelled state is different in the packet than in the NpcStats, // adjust the NpcStats accordingly @@ -1108,7 +1109,7 @@ void LocalPlayer::setFactions() } } - else if (factionChanges.action == mwmp::FactionChanges::Type::Reputation) + if (faction.isReputationChanged()) ptrNpcStats.setFactionReputation(faction.factionId, faction.reputation); } } @@ -1326,11 +1327,11 @@ void LocalPlayer::sendJournalIndex(const std::string& quest, int index) void LocalPlayer::sendFactionRank(const std::string& factionId, int rank) { factionChanges.factions.clear(); - factionChanges.action = FactionChanges::Type::Rank; mwmp::Faction faction; faction.factionId = factionId; faction.rank = rank; + faction.rankChanged(); factionChanges.factions.push_back(faction); @@ -1341,11 +1342,11 @@ void LocalPlayer::sendFactionRank(const std::string& factionId, int rank) void LocalPlayer::sendFactionExpulsionState(const std::string& factionId, bool isExpelled) { factionChanges.factions.clear(); - factionChanges.action = FactionChanges::Type::Expulsion; mwmp::Faction faction; faction.factionId = factionId; faction.isExpelled = isExpelled; + faction.expulsionChanged(); factionChanges.factions.push_back(faction); @@ -1356,11 +1357,11 @@ void LocalPlayer::sendFactionExpulsionState(const std::string& factionId, bool i void LocalPlayer::sendFactionReputation(const std::string& factionId, int reputation) { factionChanges.factions.clear(); - factionChanges.action = FactionChanges::Type::Reputation; mwmp::Faction faction; faction.factionId = factionId; faction.reputation = reputation; + faction.reputationChanged(); factionChanges.factions.push_back(faction); diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 7bf9483de..6e365611a 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -59,10 +59,26 @@ namespace mwmp struct Faction { + enum ChangesMask + { + Rank = 1, + Expulsion = 2, + Reputation = 4 + }; + + inline bool isRankChanged() const { return (changes & ChangesMask::Rank) > 0; } + inline bool isExpulsionChanged() const { return (changes & ChangesMask::Expulsion) > 0; } + inline bool isReputationChanged() const { return (changes & ChangesMask::Reputation) > 0; } + inline void rankChanged() { changes |= ChangesMask::Rank; }; + inline void expulsionChanged() { changes |= ChangesMask::Expulsion; }; + inline void reputationChanged() { changes |= ChangesMask::Reputation; }; + std::string factionId; int rank; int reputation; bool isExpelled; + + uint8_t changes = 0; }; struct Topic @@ -118,15 +134,6 @@ namespace mwmp struct FactionChanges { std::vector factions; - - enum class Type: uint8_t - { - Rank = 0, - Expulsion, - Reputation - }; - - Type action; }; struct TopicChanges diff --git a/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp b/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp index 4ac3dc12a..de088e8d3 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerFaction.cpp @@ -12,9 +12,6 @@ PacketPlayerFaction::PacketPlayerFaction(RakNet::RakPeerInterface *peer) : Playe void PacketPlayerFaction::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); - - RW(player->factionChanges.action, send); - uint32_t count; if (send) @@ -30,15 +27,17 @@ void PacketPlayerFaction::Packet(RakNet::BitStream *bs, bool send) for (auto &&faction : player->factionChanges.factions) { + RW(faction.changes, send, true); + RW(faction.factionId, send, true); - if (player->factionChanges.action == FactionChanges::Type::Rank) + if (faction.isRankChanged()) RW(faction.rank, send); - if (player->factionChanges.action == FactionChanges::Type::Expulsion) + if (faction.isExpulsionChanged()) RW(faction.isExpelled, send); - if (player->factionChanges.action == FactionChanges::Type::Reputation) + if (faction.isReputationChanged()) RW(faction.reputation, send); } }