OpenMW
apps/openmw/mwworld/cellstore.hpp
Go to the documentation of this file.
00001 #ifndef GAME_MWWORLD_CELLSTORE_H
00002 #define GAME_MWWORLD_CELLSTORE_H
00003 
00004 #include <deque>
00005 #include <algorithm>
00006 
00007 #include "livecellref.hpp"
00008 #include "esmstore.hpp"
00009 
00010 namespace MWWorld
00011 {
00012 
00014   template <typename X>
00015   struct CellRefList
00016   {
00017     typedef LiveCellRef<X> LiveRef;
00018     typedef std::list<LiveRef> List;
00019     List mList;
00020 
00021     // Search for the given reference in the given reclist from
00022     // ESMStore. Insert the reference into the list if a match is
00023     // found. If not, throw an exception.
00024     // Moved to cpp file, as we require a custom compare operator for it,
00025     // and the build will fail with an ugly three-way cyclic header dependence
00026     // so we need to pass the instantiation of the method to the lnker, when
00027     // all methods are known.
00028     void load(ESM::CellRef &ref, const MWWorld::ESMStore &esmStore);
00029 
00030     LiveRef *find (const std::string& name)
00031     {
00032         for (typename std::list<LiveRef>::iterator iter (mList.begin()); iter!=mList.end(); ++iter)
00033         {
00034             if (iter->mData.getCount() > 0 && iter->mRef.mRefID == name)
00035                 return &*iter;
00036         }
00037 
00038         return 0;
00039     }
00040 
00041     LiveRef &insert(const LiveRef &item) {
00042         mList.push_back(item);
00043         return mList.back();
00044     }
00045   };
00046 
00048   class CellStore
00049   {
00050   public:
00051 
00052     enum State
00053     {
00054         State_Unloaded, State_Preloaded, State_Loaded
00055     };
00056 
00057     CellStore (const ESM::Cell *cell_);
00058 
00059     const ESM::Cell *mCell;
00060     State mState;
00061     std::vector<std::string> mIds;
00062 
00063     float mWaterLevel;
00064 
00065     // Lists for each individual object type
00066     CellRefList<ESM::Activator>         mActivators;
00067     CellRefList<ESM::Potion>            mPotions;
00068     CellRefList<ESM::Apparatus>         mAppas;
00069     CellRefList<ESM::Armor>             mArmors;
00070     CellRefList<ESM::Book>              mBooks;
00071     CellRefList<ESM::Clothing>          mClothes;
00072     CellRefList<ESM::Container>         mContainers;
00073     CellRefList<ESM::Creature>          mCreatures;
00074     CellRefList<ESM::Door>              mDoors;
00075     CellRefList<ESM::Ingredient>        mIngreds;
00076     CellRefList<ESM::CreatureLevList>   mCreatureLists;
00077     CellRefList<ESM::ItemLevList>       mItemLists;
00078     CellRefList<ESM::Light>             mLights;
00079     CellRefList<ESM::Lockpick>          mLockpicks;
00080     CellRefList<ESM::Miscellaneous>     mMiscItems;
00081     CellRefList<ESM::NPC>               mNpcs;
00082     CellRefList<ESM::Probe>             mProbes;
00083     CellRefList<ESM::Repair>            mRepairs;
00084     CellRefList<ESM::Static>            mStatics;
00085     CellRefList<ESM::Weapon>            mWeapons;
00086 
00087     void load (const MWWorld::ESMStore &store, std::vector<ESM::ESMReader> &esm);
00088 
00089     void preload (const MWWorld::ESMStore &store, std::vector<ESM::ESMReader> &esm);
00090 
00094     template<class Functor>
00095     bool forEach (Functor& functor)
00096     {
00097         return
00098             forEachImp (functor, mActivators) &&
00099             forEachImp (functor, mPotions) &&
00100             forEachImp (functor, mAppas) &&
00101             forEachImp (functor, mArmors) &&
00102             forEachImp (functor, mBooks) &&
00103             forEachImp (functor, mClothes) &&
00104             forEachImp (functor, mContainers) &&
00105             forEachImp (functor, mCreatures) &&
00106             forEachImp (functor, mDoors) &&
00107             forEachImp (functor, mIngreds) &&
00108             forEachImp (functor, mCreatureLists) &&
00109             forEachImp (functor, mItemLists) &&
00110             forEachImp (functor, mLights) &&
00111             forEachImp (functor, mLockpicks) &&
00112             forEachImp (functor, mMiscItems) &&
00113             forEachImp (functor, mNpcs) &&
00114             forEachImp (functor, mProbes) &&
00115             forEachImp (functor, mRepairs) &&
00116             forEachImp (functor, mStatics) &&
00117             forEachImp (functor, mWeapons);
00118     }
00119 
00120     bool operator==(const CellStore &cell) {
00121         return  mCell->mName == cell.mCell->mName &&
00122                 mCell->mData.mX == cell.mCell->mData.mX &&
00123                 mCell->mData.mY == cell.mCell->mData.mY;
00124     }
00125 
00126     bool operator!=(const CellStore &cell) {
00127         return !(*this == cell);
00128     }
00129 
00130     bool isExterior() const {
00131         return mCell->isExterior();
00132     }
00133 
00134     Ptr searchInContainer (const std::string& id);
00135 
00136   private:
00137 
00138     template<class Functor, class List>
00139     bool forEachImp (Functor& functor, List& list)
00140     {
00141         for (typename List::List::iterator iter (list.mList.begin()); iter!=list.mList.end();
00142             ++iter)
00143         {
00144             if (!iter->mData.getCount())
00145                 continue;
00146             if (!functor (MWWorld::Ptr(&*iter, this)))
00147                 return false;
00148         }
00149         return true;
00150     }
00151 
00153     void listRefs(const MWWorld::ESMStore &store, std::vector<ESM::ESMReader> &esm);
00154 
00155     void loadRefs(const MWWorld::ESMStore &store, std::vector<ESM::ESMReader> &esm);
00156 
00157   };
00158 }
00159 
00160 #endif