From dccf6a2b8c56c10feddc3c49aaf6fd0ccd905b26 Mon Sep 17 00:00:00 2001 From: Kindi Date: Wed, 14 Jun 2023 03:38:22 +0800 Subject: [PATCH] Refactor raiserank and lowerrank --- apps/openmw/mwmechanics/npcstats.cpp | 45 ++++++++++-------------- apps/openmw/mwmechanics/npcstats.hpp | 6 ++-- apps/openmw/mwscript/statsextensions.cpp | 18 +++++----- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/apps/openmw/mwmechanics/npcstats.cpp b/apps/openmw/mwmechanics/npcstats.cpp index e9812e7931..7acf5f7555 100644 --- a/apps/openmw/mwmechanics/npcstats.cpp +++ b/apps/openmw/mwmechanics/npcstats.cpp @@ -80,32 +80,6 @@ int MWMechanics::NpcStats::getFactionRank(const ESM::RefId& faction) const return -1; } -void MWMechanics::NpcStats::raiseRank(const ESM::RefId& faction) -{ - auto it = mFactionRank.find(faction); - if (it != mFactionRank.end()) - { - // Does the next rank exist? - const ESM::Faction* factionPtr = MWBase::Environment::get().getESMStore()->get().find(faction); - if (it->second + 1 < 10 && !factionPtr->mRanks[it->second + 1].empty()) - it->second += 1; - } -} - -void MWMechanics::NpcStats::lowerRank(const ESM::RefId& faction) -{ - auto it = mFactionRank.find(faction); - if (it != mFactionRank.end()) - { - it->second = it->second - 1; - if (it->second < 0) - { - mFactionRank.erase(it); - mExpelled.erase(faction); - } - } -} - void MWMechanics::NpcStats::joinFaction(const ESM::RefId& faction) { auto it = mFactionRank.find(faction); @@ -113,6 +87,25 @@ void MWMechanics::NpcStats::joinFaction(const ESM::RefId& faction) mFactionRank[faction] = 0; } +void MWMechanics::NpcStats::setFactionRank(const ESM::RefId& faction, int newRank) +{ + auto it = mFactionRank.find(faction); + if (it != mFactionRank.end()) + { + const ESM::Faction* factionPtr = MWBase::Environment::get().getESMStore()->get().find(faction); + if (newRank < 0) + { + mFactionRank.erase(it); + mExpelled.erase(faction); + } + else if (newRank < static_cast(factionPtr->mData.mRankData.size())) + do + it->second = newRank; + // Does the new rank exist? + while (newRank > 0 && factionPtr->mRanks[newRank--].empty()); + } +} + bool MWMechanics::NpcStats::getExpelled(const ESM::RefId& factionID) const { return mExpelled.find(factionID) != mExpelled.end(); diff --git a/apps/openmw/mwmechanics/npcstats.hpp b/apps/openmw/mwmechanics/npcstats.hpp index 57f4bbd1e4..c43f21250e 100644 --- a/apps/openmw/mwmechanics/npcstats.hpp +++ b/apps/openmw/mwmechanics/npcstats.hpp @@ -65,12 +65,10 @@ namespace MWMechanics int getFactionRank(const ESM::RefId& faction) const; const std::map& getFactionRanks() const; - /// Increase the rank in this faction by 1, if such a rank exists. - void raiseRank(const ESM::RefId& faction); - /// Lower the rank in this faction by 1, if such a rank exists. - void lowerRank(const ESM::RefId& faction); /// Join this faction, setting the initial rank to 0. void joinFaction(const ESM::RefId& faction); + /// Sets the rank in this faction to a specified value, if such a rank exists. + void setFactionRank(const ESM::RefId& faction, int value); const std::set& getExpelled() const { return mExpelled; } bool getExpelled(const ESM::RefId& factionID) const; diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp index 877fa79099..b04ab0b9f9 100644 --- a/apps/openmw/mwscript/statsextensions.cpp +++ b/apps/openmw/mwscript/statsextensions.cpp @@ -613,7 +613,8 @@ namespace MWScript } else { - player.getClass().getNpcStats(player).raiseRank(factionID); + int currentRank = player.getClass().getNpcStats(player).getFactionRank(factionID); + player.getClass().getNpcStats(player).setFactionRank(factionID, currentRank + 1); } } } @@ -644,7 +645,8 @@ namespace MWScript if (!factionID.empty()) { MWWorld::Ptr player = MWMechanics::getPlayer(); - player.getClass().getNpcStats(player).lowerRank(factionID); + int currentRank = player.getClass().getNpcStats(player).getFactionRank(factionID); + player.getClass().getNpcStats(player).setFactionRank(factionID, currentRank - 1); } } }; @@ -987,14 +989,12 @@ namespace MWScript // Otherwise take rank from base NPC record, increase it and put it to NPC data. int currentRank = ptr.getClass().getNpcStats(ptr).getFactionRank(factionID); if (currentRank >= 0) - ptr.getClass().getNpcStats(ptr).raiseRank(factionID); + ptr.getClass().getNpcStats(ptr).setFactionRank(factionID, currentRank + 1); else { int rank = ptr.getClass().getPrimaryFactionRank(ptr); - rank++; ptr.getClass().getNpcStats(ptr).joinFaction(factionID); - for (int i = 0; i < rank; i++) - ptr.getClass().getNpcStats(ptr).raiseRank(factionID); + ptr.getClass().getNpcStats(ptr).setFactionRank(factionID, rank + 1); } } }; @@ -1023,14 +1023,12 @@ namespace MWScript if (currentRank == 0) return; else if (currentRank > 0) - ptr.getClass().getNpcStats(ptr).lowerRank(factionID); + ptr.getClass().getNpcStats(ptr).setFactionRank(factionID, currentRank - 1); else { int rank = ptr.getClass().getPrimaryFactionRank(ptr); - rank--; ptr.getClass().getNpcStats(ptr).joinFaction(factionID); - for (int i = 0; i < rank; i++) - ptr.getClass().getNpcStats(ptr).raiseRank(factionID); + ptr.getClass().getNpcStats(ptr).setFactionRank(factionID, std::max(0, rank - 1)); } } };