From c6761e347073355d4f8491044e68880be81399cb Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Wed, 25 Jan 2012 16:56:49 +0100 Subject: [PATCH] replaced NPC stats in ref data with new custom data implementation --- apps/openmw/mwclass/npc.cpp | 58 ++++++++++++++++-------- apps/openmw/mwclass/npc.hpp | 4 +- apps/openmw/mwscript/statsextensions.cpp | 1 + apps/openmw/mwworld/refdata.cpp | 6 --- apps/openmw/mwworld/refdata.hpp | 4 -- 5 files changed, 42 insertions(+), 31 deletions(-) diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 2b98a3d60..e48b95d01 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -1,6 +1,8 @@ #include "npc.hpp" +#include + #include #include "../mwmechanics/creaturestats.hpp" @@ -18,10 +20,44 @@ namespace { const Ogre::Radian kOgrePi (Ogre::Math::PI); const Ogre::Radian kOgrePiOverTwo (Ogre::Math::PI / Ogre::Real(2.0)); + + struct CustomData : public MWWorld::CustomData + { + MWMechanics::NpcStats mNpcStats; + + virtual MWWorld::CustomData *clone() const; + }; + + MWWorld::CustomData *CustomData::clone() const + { + return new CustomData (*this); + } } namespace MWClass { + void Npc::ensureCustomData (const MWWorld::Ptr& ptr) const + { + if (!ptr.getRefData().getCustomData()) + { + std::auto_ptr data (new CustomData); + + ESMS::LiveCellRef *ref = ptr.get(); + + if (!ref->base->faction.empty()) + { + // TODO research how initial rank is stored. The information in loadnpc.hpp are at + // best very unclear. + data->mNpcStats.mFactionRank[ref->base->faction] = 0; + } + + for (int i=0; i<27; ++i) + data->mNpcStats.mSkill[i].setBase (ref->base->npdt52.skills[i]); + + ptr.getRefData().setCustomData (data.release()); + } + } + std::string Npc::getId (const MWWorld::Ptr& ptr) const { ESMS::LiveCellRef *ref = @@ -106,27 +142,9 @@ namespace MWClass MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const { - if (!ptr.getRefData().getNpcStats().get()) - { - boost::shared_ptr stats ( - new MWMechanics::NpcStats); - - ESMS::LiveCellRef *ref = ptr.get(); - - if (!ref->base->faction.empty()) - { - // TODO research how initial rank is stored. The information in loadnpc.hpp are at - // best very unclear. - stats->mFactionRank[ref->base->faction] = 0; - } - - for (int i=0; i<27; ++i) - stats->mSkill[i].setBase (ref->base->npdt52.skills[i]); - - ptr.getRefData().getNpcStats() = stats; - } + ensureCustomData (ptr); - return *ptr.getRefData().getNpcStats(); + return dynamic_cast (*ptr.getRefData().getCustomData()).mNpcStats; } boost::shared_ptr Npc::activate (const MWWorld::Ptr& ptr, diff --git a/apps/openmw/mwclass/npc.hpp b/apps/openmw/mwclass/npc.hpp index cc9dbef7f..63f5c42dd 100644 --- a/apps/openmw/mwclass/npc.hpp +++ b/apps/openmw/mwclass/npc.hpp @@ -2,12 +2,14 @@ #define GAME_MWCLASS_NPC_H #include "../mwworld/class.hpp" - +#include "../mwworld/customdata.hpp" namespace MWClass { class Npc : public MWWorld::Class { + void ensureCustomData (const MWWorld::Ptr& ptr) const; + public: virtual std::string getId (const MWWorld::Ptr& ptr) const; diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp index ca82830d9..0e97a39cf 100644 --- a/apps/openmw/mwscript/statsextensions.cpp +++ b/apps/openmw/mwscript/statsextensions.cpp @@ -10,6 +10,7 @@ #include "../mwworld/class.hpp" #include "../mwmechanics/creaturestats.hpp" +#include "../mwmechanics/npcstats.hpp" #include "interpretercontext.hpp" #include "ref.hpp" diff --git a/apps/openmw/mwworld/refdata.cpp b/apps/openmw/mwworld/refdata.cpp index 359cd393e..ce695e945 100644 --- a/apps/openmw/mwworld/refdata.cpp +++ b/apps/openmw/mwworld/refdata.cpp @@ -15,7 +15,6 @@ namespace MWWorld mPosition = refData.mPosition; mCreatureStats = refData.mCreatureStats; - mNpcStats = refData.mNpcStats; mMovement = refData.mMovement; mContainerStore = refData.mContainerStore; @@ -134,11 +133,6 @@ namespace MWWorld return mCreatureStats; } - boost::shared_ptr& RefData::getNpcStats() - { - return mNpcStats; - } - boost::shared_ptr& RefData::getMovement() { return mMovement; diff --git a/apps/openmw/mwworld/refdata.hpp b/apps/openmw/mwworld/refdata.hpp index c07432abd..ab7494343 100644 --- a/apps/openmw/mwworld/refdata.hpp +++ b/apps/openmw/mwworld/refdata.hpp @@ -10,7 +10,6 @@ #include "../mwscript/locals.hpp" #include "../mwmechanics/creaturestats.hpp" -#include "../mwmechanics/npcstats.hpp" #include "../mwmechanics/movement.hpp" #include "containerstore.hpp" @@ -45,7 +44,6 @@ namespace MWWorld // manually will probably give unexcepted results. This is not a problem since RefData // are never copied outside of container operations. boost::shared_ptr mCreatureStats; - boost::shared_ptr mNpcStats; boost::shared_ptr mMovement; boost::shared_ptr > mContainerStore; @@ -92,8 +90,6 @@ namespace MWWorld boost::shared_ptr& getCreatureStats(); - boost::shared_ptr& getNpcStats(); - boost::shared_ptr& getMovement(); boost::shared_ptr >& getContainerStore();