1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-23 00:09:42 +00:00

Refactor raiserank and lowerrank

This commit is contained in:
Kindi 2023-06-14 03:38:22 +08:00
parent 825d862f52
commit dccf6a2b8c
3 changed files with 29 additions and 40 deletions

View file

@ -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<ESM::Faction>().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<ESM::Faction>().find(faction);
if (newRank < 0)
{
mFactionRank.erase(it);
mExpelled.erase(faction);
}
else if (newRank < static_cast<int>(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();

View file

@ -65,12 +65,10 @@ namespace MWMechanics
int getFactionRank(const ESM::RefId& faction) const;
const std::map<ESM::RefId, int>& 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<ESM::RefId>& getExpelled() const { return mExpelled; }
bool getExpelled(const ESM::RefId& factionID) const;

View file

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