diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index 5aa203a49..ab0e6e7fa 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -8,12 +8,55 @@ #include "../mwworld/ptr.hpp" #include "../mwworld/actiontalk.hpp" #include "../mwworld/environment.hpp" - +#include "../mwworld/customdata.hpp" #include "../mwmechanics/mechanicsmanager.hpp" +namespace +{ + struct CustomData : public MWWorld::CustomData + { + MWMechanics::CreatureStats mCreatureStats; + + virtual MWWorld::CustomData *clone() const; + }; + + MWWorld::CustomData *CustomData::clone() const + { + return new CustomData (*this); + } +} + namespace MWClass { + void Creature::ensureCustomData (const MWWorld::Ptr& ptr) const + { + if (!ptr.getRefData().getCustomData()) + { + std::auto_ptr data (new CustomData); + + ESMS::LiveCellRef *ref = ptr.get(); + + // creature stats + data->mCreatureStats.mAttributes[0].set (ref->base->data.strength); + data->mCreatureStats.mAttributes[1].set (ref->base->data.intelligence); + data->mCreatureStats.mAttributes[2].set (ref->base->data.willpower); + data->mCreatureStats.mAttributes[3].set (ref->base->data.agility); + data->mCreatureStats.mAttributes[4].set (ref->base->data.speed); + data->mCreatureStats.mAttributes[5].set (ref->base->data.endurance); + data->mCreatureStats.mAttributes[6].set (ref->base->data.personality); + data->mCreatureStats.mAttributes[7].set (ref->base->data.luck); + data->mCreatureStats.mDynamic[0].set (ref->base->data.health); + data->mCreatureStats.mDynamic[1].set (ref->base->data.mana); + data->mCreatureStats.mDynamic[2].set (ref->base->data.fatigue); + + data->mCreatureStats.mLevel = ref->base->data.level; + + // store + ptr.getRefData().setCustomData (data.release()); + } + } + std::string Creature::getId (const MWWorld::Ptr& ptr) const { ESMS::LiveCellRef *ref = @@ -24,18 +67,18 @@ namespace MWClass void Creature::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const { - + /*ESMS::LiveCellRef *ref = ptr.get(); assert (ref->base != NULL); const std::string &model = ref->base->model; - + if (!model.empty()) {*/ MWRender::Actors& actors = renderingInterface.getActors(); actors.insertCreature(ptr); - + } void Creature::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics, MWWorld::Environment& environment) const @@ -72,31 +115,9 @@ namespace MWClass MWMechanics::CreatureStats& Creature::getCreatureStats (const MWWorld::Ptr& ptr) const { - if (!ptr.getRefData().getCreatureStats().get()) - { - boost::shared_ptr stats ( - new MWMechanics::CreatureStats); - - ESMS::LiveCellRef *ref = ptr.get(); - - stats->mAttributes[0].set (ref->base->data.strength); - stats->mAttributes[1].set (ref->base->data.intelligence); - stats->mAttributes[2].set (ref->base->data.willpower); - stats->mAttributes[3].set (ref->base->data.agility); - stats->mAttributes[4].set (ref->base->data.speed); - stats->mAttributes[5].set (ref->base->data.endurance); - stats->mAttributes[6].set (ref->base->data.personality); - stats->mAttributes[7].set (ref->base->data.luck); - stats->mDynamic[0].set (ref->base->data.health); - stats->mDynamic[1].set (ref->base->data.mana); - stats->mDynamic[2].set (ref->base->data.fatigue); - - stats->mLevel = ref->base->data.level; - - ptr.getRefData().getCreatureStats() = stats; - } + ensureCustomData (ptr); - return *ptr.getRefData().getCreatureStats(); + return dynamic_cast (*ptr.getRefData().getCustomData()).mCreatureStats; } boost::shared_ptr Creature::activate (const MWWorld::Ptr& ptr, diff --git a/apps/openmw/mwclass/creature.hpp b/apps/openmw/mwclass/creature.hpp index b7b654bc0..9afeffea8 100644 --- a/apps/openmw/mwclass/creature.hpp +++ b/apps/openmw/mwclass/creature.hpp @@ -10,6 +10,8 @@ namespace MWClass { class Creature : 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/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index e48b95d01..963b53f60 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -24,6 +24,7 @@ namespace struct CustomData : public MWWorld::CustomData { MWMechanics::NpcStats mNpcStats; + MWMechanics::CreatureStats mCreatureStats; virtual MWWorld::CustomData *clone() const; }; @@ -44,6 +45,7 @@ namespace MWClass ESMS::LiveCellRef *ref = ptr.get(); + // NPC stats if (!ref->base->faction.empty()) { // TODO research how initial rank is stored. The information in loadnpc.hpp are at @@ -54,6 +56,22 @@ namespace MWClass for (int i=0; i<27; ++i) data->mNpcStats.mSkill[i].setBase (ref->base->npdt52.skills[i]); + // creature stats + data->mCreatureStats.mAttributes[0].set (ref->base->npdt52.strength); + data->mCreatureStats.mAttributes[1].set (ref->base->npdt52.intelligence); + data->mCreatureStats.mAttributes[2].set (ref->base->npdt52.willpower); + data->mCreatureStats.mAttributes[3].set (ref->base->npdt52.agility); + data->mCreatureStats.mAttributes[4].set (ref->base->npdt52.speed); + data->mCreatureStats.mAttributes[5].set (ref->base->npdt52.endurance); + data->mCreatureStats.mAttributes[6].set (ref->base->npdt52.personality); + data->mCreatureStats.mAttributes[7].set (ref->base->npdt52.luck); + data->mCreatureStats.mDynamic[0].set (ref->base->npdt52.health); + data->mCreatureStats.mDynamic[1].set (ref->base->npdt52.mana); + data->mCreatureStats.mDynamic[2].set (ref->base->npdt52.fatigue); + + data->mCreatureStats.mLevel = ref->base->npdt52.level; + + // store ptr.getRefData().setCustomData (data.release()); } } @@ -113,31 +131,9 @@ namespace MWClass MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const { - if (!ptr.getRefData().getCreatureStats().get()) - { - boost::shared_ptr stats ( - new MWMechanics::CreatureStats); - - ESMS::LiveCellRef *ref = ptr.get(); - - stats->mAttributes[0].set (ref->base->npdt52.strength); - stats->mAttributes[1].set (ref->base->npdt52.intelligence); - stats->mAttributes[2].set (ref->base->npdt52.willpower); - stats->mAttributes[3].set (ref->base->npdt52.agility); - stats->mAttributes[4].set (ref->base->npdt52.speed); - stats->mAttributes[5].set (ref->base->npdt52.endurance); - stats->mAttributes[6].set (ref->base->npdt52.personality); - stats->mAttributes[7].set (ref->base->npdt52.luck); - stats->mDynamic[0].set (ref->base->npdt52.health); - stats->mDynamic[1].set (ref->base->npdt52.mana); - stats->mDynamic[2].set (ref->base->npdt52.fatigue); - - stats->mLevel = ref->base->npdt52.level; - - ptr.getRefData().getCreatureStats() = stats; - } + ensureCustomData (ptr); - return *ptr.getRefData().getCreatureStats(); + return dynamic_cast (*ptr.getRefData().getCustomData()).mCreatureStats; } MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const diff --git a/apps/openmw/mwworld/refdata.cpp b/apps/openmw/mwworld/refdata.cpp index ce695e945..2e4811367 100644 --- a/apps/openmw/mwworld/refdata.cpp +++ b/apps/openmw/mwworld/refdata.cpp @@ -14,7 +14,6 @@ namespace MWWorld mCount = refData.mCount; mPosition = refData.mPosition; - mCreatureStats = refData.mCreatureStats; mMovement = refData.mMovement; mContainerStore = refData.mContainerStore; @@ -128,11 +127,6 @@ namespace MWWorld mEnabled = true; } - boost::shared_ptr& RefData::getCreatureStats() - { - return mCreatureStats; - } - boost::shared_ptr& RefData::getMovement() { return mMovement; diff --git a/apps/openmw/mwworld/refdata.hpp b/apps/openmw/mwworld/refdata.hpp index ab7494343..d6984afd9 100644 --- a/apps/openmw/mwworld/refdata.hpp +++ b/apps/openmw/mwworld/refdata.hpp @@ -9,7 +9,6 @@ #include "../mwscript/locals.hpp" -#include "../mwmechanics/creaturestats.hpp" #include "../mwmechanics/movement.hpp" #include "containerstore.hpp" @@ -43,7 +42,6 @@ namespace MWWorld // assignment operator and destructor. As a consequence though copying a RefData object // 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 mMovement; boost::shared_ptr > mContainerStore; @@ -88,8 +86,6 @@ namespace MWWorld void disable(); - boost::shared_ptr& getCreatureStats(); - boost::shared_ptr& getMovement(); boost::shared_ptr >& getContainerStore();