#ifndef GAME_MWWORLD_PTR_H #define GAME_MWWORLD_PTR_H #include #include #include #include #include #include "../mwmechanics/creaturestats.hpp" #include "refdata.hpp" namespace MWWorld { /// \brief Pointer to a LiveCellRef class Ptr { public: typedef ESMS::CellStore CellStore; boost::any mPtr; ESM::CellRef *mCellRef; RefData *mRefData; CellStore *mCell; public: Ptr() : mCellRef (0), mRefData (0), mCell (0) {} bool isEmpty() const { return mPtr.empty(); } template Ptr (ESMS::LiveCellRef *liveCellRef, CellStore *cell) { mPtr = liveCellRef; mCellRef = &liveCellRef->ref; mRefData = &liveCellRef->mData; mCell = cell; } template ESMS::LiveCellRef *get() const { return boost::any_cast*> (mPtr); } ESM::CellRef& getCellRef() const { assert (mCellRef); return *mCellRef; } RefData& getRefData() const { assert (mRefData); return *mRefData; } Ptr::CellStore *getCell() const { assert (mCell); return mCell; } /// Throws an exception, if the ID type does not support creature stats. MWMechanics::CreatureStats& getCreatureStats() const { RefData& data = getRefData(); if (!data.getCreatureStats().get()) { if (mPtr.type()==typeid (ESMS::LiveCellRef *)) { boost::shared_ptr stats; data.getCreatureStats() = stats; ESMS::LiveCellRef *ref = 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); } else if (mPtr.type()==typeid (ESMS::LiveCellRef *)) { boost::shared_ptr stats; data.getCreatureStats() = stats; ESMS::LiveCellRef *ref = 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); } else throw std::runtime_error ( "CreatureStats not available for this ID type"); } return *data.getCreatureStats(); } }; } #endif