From 2346c3528d5b7e7db98c55f08837f16d77e9039a Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 31 Jan 2015 02:08:37 +0100 Subject: [PATCH] Remove NpcStats::mProfit and store it in the script instead Also use the Profit label when the "minimumProfit" script variable exists, rather than hardcoding to NPCs. --- apps/openmw/mwgui/companionitemmodel.cpp | 38 +++++++++++++++++------- apps/openmw/mwgui/companionitemmodel.hpp | 2 ++ apps/openmw/mwgui/companionwindow.cpp | 29 ++++++++++++------ apps/openmw/mwmechanics/npcstats.cpp | 13 -------- apps/openmw/mwmechanics/npcstats.hpp | 6 ---- apps/openmw/mwscript/locals.cpp | 15 ++++++++++ apps/openmw/mwscript/locals.hpp | 8 +++-- components/esm/npcstats.cpp | 13 ++++---- components/esm/npcstats.hpp | 1 - 9 files changed, 76 insertions(+), 49 deletions(-) diff --git a/apps/openmw/mwgui/companionitemmodel.cpp b/apps/openmw/mwgui/companionitemmodel.cpp index b8be9dcb8..983ef5017 100644 --- a/apps/openmw/mwgui/companionitemmodel.cpp +++ b/apps/openmw/mwgui/companionitemmodel.cpp @@ -3,6 +3,22 @@ #include "../mwmechanics/npcstats.hpp" #include "../mwworld/class.hpp" +namespace +{ + + void modifyProfit(const MWWorld::Ptr& actor, int diff) + { + std::string script = actor.getClass().getScript(actor); + if (!script.empty()) + { + int profit = actor.getRefData().getLocals().getIntVar(script, "minimumprofit"); + profit += diff; + actor.getRefData().getLocals().setVarByInt(script, "minimumprofit", profit); + } + } + +} + namespace MWGui { CompanionItemModel::CompanionItemModel(const MWWorld::Ptr &actor) @@ -12,23 +28,25 @@ namespace MWGui MWWorld::Ptr CompanionItemModel::copyItem (const ItemStack& item, size_t count, bool setNewOwner=false) { - if (mActor.getClass().isNpc()) - { - MWMechanics::NpcStats& stats = mActor.getClass().getNpcStats(mActor); - stats.modifyProfit(item.mBase.getClass().getValue(item.mBase) * count); - } + if (hasProfit(mActor)) + modifyProfit(mActor, item.mBase.getClass().getValue(item.mBase) * count); return InventoryItemModel::copyItem(item, count, setNewOwner); } void CompanionItemModel::removeItem (const ItemStack& item, size_t count) { - if (mActor.getClass().isNpc()) - { - MWMechanics::NpcStats& stats = mActor.getClass().getNpcStats(mActor); - stats.modifyProfit(-item.mBase.getClass().getValue(item.mBase) * count); - } + if (hasProfit(mActor)) + modifyProfit(mActor, -item.mBase.getClass().getValue(item.mBase) * count); InventoryItemModel::removeItem(item, count); } + + bool CompanionItemModel::hasProfit(const MWWorld::Ptr &actor) + { + std::string script = actor.getClass().getScript(actor); + if (script.empty()) + return false; + return actor.getRefData().getLocals().hasVar(script, "minimumprofit"); + } } diff --git a/apps/openmw/mwgui/companionitemmodel.hpp b/apps/openmw/mwgui/companionitemmodel.hpp index 172fa9508..4c77ee12f 100644 --- a/apps/openmw/mwgui/companionitemmodel.hpp +++ b/apps/openmw/mwgui/companionitemmodel.hpp @@ -15,6 +15,8 @@ namespace MWGui virtual MWWorld::Ptr copyItem (const ItemStack& item, size_t count, bool setNewOwner); virtual void removeItem (const ItemStack& item, size_t count); + + bool hasProfit(const MWWorld::Ptr& actor); }; } diff --git a/apps/openmw/mwgui/companionwindow.cpp b/apps/openmw/mwgui/companionwindow.cpp index 53b14691b..8f709ec8d 100644 --- a/apps/openmw/mwgui/companionwindow.cpp +++ b/apps/openmw/mwgui/companionwindow.cpp @@ -17,6 +17,21 @@ #include "draganddrop.hpp" #include "countdialog.hpp" +namespace +{ + + int getProfit(const MWWorld::Ptr& actor) + { + std::string script = actor.getClass().getScript(actor); + if (!script.empty()) + { + return actor.getRefData().getLocals().getIntVar(script, "minimumprofit"); + } + return 0; + } + +} + namespace MWGui { @@ -116,13 +131,12 @@ void CompanionWindow::updateEncumbranceBar() float encumbrance = mPtr.getClass().getEncumbrance(mPtr); mEncumbranceBar->setValue(encumbrance, capacity); - if (mPtr.getTypeName() != typeid(ESM::NPC).name()) - mProfitLabel->setCaption(""); - else + if (mModel && mModel->hasProfit(mPtr)) { - MWMechanics::NpcStats& stats = mPtr.getClass().getNpcStats(mPtr); - mProfitLabel->setCaptionWithReplacing("#{sProfitValue} " + MyGUI::utility::toString(stats.getProfit())); + mProfitLabel->setCaptionWithReplacing("#{sProfitValue} " + MyGUI::utility::toString(getProfit(mPtr))); } + else + mProfitLabel->setCaption(""); } void CompanionWindow::onCloseButtonClicked(MyGUI::Widget* _sender) @@ -132,7 +146,7 @@ void CompanionWindow::onCloseButtonClicked(MyGUI::Widget* _sender) void CompanionWindow::exit() { - if (mPtr.getTypeName() == typeid(ESM::NPC).name() && mPtr.getClass().getNpcStats(mPtr).getProfit() < 0) + if (mModel && mModel->hasProfit(mPtr) && getProfit(mPtr) < 0) { std::vector buttons; buttons.push_back("#{sCompanionWarningButtonOne}"); @@ -148,9 +162,6 @@ void CompanionWindow::onMessageBoxButtonClicked(int button) { if (button == 0) { - mPtr.getRefData().getLocals().setVarByInt(mPtr.getClass().getScript(mPtr), - "minimumprofit", mPtr.getClass().getNpcStats(mPtr).getProfit()); - MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Companion); // Important for Calvus' contract script to work properly MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Dialogue); diff --git a/apps/openmw/mwmechanics/npcstats.cpp b/apps/openmw/mwmechanics/npcstats.cpp index c3a4d2312..c517b4df8 100644 --- a/apps/openmw/mwmechanics/npcstats.cpp +++ b/apps/openmw/mwmechanics/npcstats.cpp @@ -31,7 +31,6 @@ MWMechanics::NpcStats::NpcStats() , mReputation(0) , mCrimeId(-1) , mWerewolfKills (0) -, mProfit(0) , mTimeToStartDrowning(20.0) { mSkillIncreases.resize (ESM::Attribute::Length, 0); @@ -448,16 +447,6 @@ void MWMechanics::NpcStats::addWerewolfKill() ++mWerewolfKills; } -int MWMechanics::NpcStats::getProfit() const -{ - return mProfit; -} - -void MWMechanics::NpcStats::modifyProfit(int diff) -{ - mProfit += diff; -} - float MWMechanics::NpcStats::getTimeToStartDrowning() const { return mTimeToStartDrowning; @@ -501,7 +490,6 @@ void MWMechanics::NpcStats::writeState (ESM::NpcStats& state) const state.mReputation = mReputation; state.mWerewolfKills = mWerewolfKills; - state.mProfit = mProfit; state.mLevelProgress = mLevelProgress; for (int i=0; igetLocals(script); + int index = locals.getIndex(var); + return (index != -1); + } + catch (const Compiler::SourceException&) + { + return false; + } + } + int Locals::getIntVar(const std::string &script, const std::string &var) { const Compiler::Locals& locals = MWBase::Environment::get().getScriptManager()->getLocals(script); diff --git a/apps/openmw/mwscript/locals.hpp b/apps/openmw/mwscript/locals.hpp index 9fa4214ac..bd95835ac 100644 --- a/apps/openmw/mwscript/locals.hpp +++ b/apps/openmw/mwscript/locals.hpp @@ -24,11 +24,15 @@ namespace MWScript bool isEmpty() const; void configure (const ESM::Script& script); + /// @note var needs to be in lowercase bool setVarByInt(const std::string& script, const std::string& var, int val); - int getIntVar (const std::string& script, const std::string& var); - ///< if var does not exist, returns 0 + + bool hasVar(const std::string& script, const std::string& var); + + /// if var does not exist, returns 0 /// @note var needs to be in lowercase + int getIntVar (const std::string& script, const std::string& var); void write (ESM::Locals& locals, const std::string& script) const; diff --git a/components/esm/npcstats.cpp b/components/esm/npcstats.cpp index e305ddab0..cc1d6b3dd 100644 --- a/components/esm/npcstats.cpp +++ b/components/esm/npcstats.cpp @@ -57,12 +57,13 @@ void ESM::NpcStats::load (ESMReader &esm) mWerewolfKills = 0; esm.getHNOT (mWerewolfKills, "WKIL"); - mProfit = 0; - esm.getHNOT (mProfit, "PROF"); + // No longer used + if (esm.isNextSub("PROF")) + esm.skipHSub(); // int profit // No longer used. Now part of CreatureStats. - float attackStrength = 0; - esm.getHNOT (attackStrength, "ASTR"); + if (esm.isNextSub("ASTR")) + esm.skipHSub(); // attackStrength mLevelProgress = 0; esm.getHNOT (mLevelProgress, "LPRO"); @@ -132,9 +133,6 @@ void ESM::NpcStats::save (ESMWriter &esm) const if (mWerewolfKills) esm.writeHNT ("WKIL", mWerewolfKills); - if (mProfit) - esm.writeHNT ("PROF", mProfit); - if (mLevelProgress) esm.writeHNT ("LPRO", mLevelProgress); @@ -158,7 +156,6 @@ void ESM::NpcStats::blank() mBounty = 0; mReputation = 0; mWerewolfKills = 0; - mProfit = 0; mLevelProgress = 0; for (int i=0; i<8; ++i) mSkillIncrease[i] = 0; diff --git a/components/esm/npcstats.hpp b/components/esm/npcstats.hpp index 1eb1a3d1a..0138ab209 100644 --- a/components/esm/npcstats.hpp +++ b/components/esm/npcstats.hpp @@ -40,7 +40,6 @@ namespace ESM int mBounty; int mReputation; int mWerewolfKills; - int mProfit; int mLevelProgress; int mSkillIncrease[8]; std::vector mUsedIds; // lower case IDs