From 79e723ad06eaeca8d073eb0d78e6c5bc756dc029 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 23 Feb 2014 16:46:07 +0100 Subject: [PATCH] encapsulated mIds --- apps/openmw/mwworld/cells.cpp | 63 ++----------------------- apps/openmw/mwworld/cellstore.cpp | 78 +++++++++++++++++++++++++++++++ apps/openmw/mwworld/cellstore.hpp | 10 +++- 3 files changed, 91 insertions(+), 60 deletions(-) diff --git a/apps/openmw/mwworld/cells.cpp b/apps/openmw/mwworld/cells.cpp index c3bc135a1c..8ce7d4fe80 100644 --- a/apps/openmw/mwworld/cells.cpp +++ b/apps/openmw/mwworld/cells.cpp @@ -167,7 +167,7 @@ MWWorld::Ptr MWWorld::Cells::getPtr (const std::string& name, CellStore& cell, if (cell.getState()==CellStore::State_Preloaded) { - if (std::binary_search (cell.mIds.begin(), cell.mIds.end(), name)) + if (cell.hasId (name)) { cell.load (mStore, mReader); } @@ -175,65 +175,10 @@ MWWorld::Ptr MWWorld::Cells::getPtr (const std::string& name, CellStore& cell, return Ptr(); } - if (MWWorld::LiveCellRef *ref = cell.mActivators.find (name)) - return Ptr (ref, &cell); + Ptr ptr = cell.search (name); - if (MWWorld::LiveCellRef *ref = cell.mPotions.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mAppas.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mArmors.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mBooks.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mClothes.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mContainers.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mCreatures.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mDoors.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mIngreds.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mCreatureLists.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mItemLists.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mLights.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mLockpicks.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mMiscItems.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mNpcs.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mProbes.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mRepairs.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mStatics.find (name)) - return Ptr (ref, &cell); - - if (MWWorld::LiveCellRef *ref = cell.mWeapons.find (name)) - return Ptr (ref, &cell); + if (!ptr.isEmpty()) + return ptr; if (searchInContainers) return cell.searchInContainer (name); diff --git a/apps/openmw/mwworld/cellstore.cpp b/apps/openmw/mwworld/cellstore.cpp index 16f317f086..e95fd226b0 100644 --- a/apps/openmw/mwworld/cellstore.cpp +++ b/apps/openmw/mwworld/cellstore.cpp @@ -1,6 +1,7 @@ #include "cellstore.hpp" #include +#include #include #include @@ -155,6 +156,83 @@ namespace MWWorld return mState; } + bool CellStore::hasId (const std::string& id) const + { + if (mState==State_Unloaded) + return false; + + if (mState==State_Preloaded) + return std::binary_search (mIds.begin(), mIds.end(), id); + + /// \todo address const-issues + return const_cast (this)->search (id).isEmpty(); + } + + Ptr CellStore::search (const std::string& id) + { + if (LiveCellRef *ref = mActivators.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mPotions.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mAppas.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mArmors.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mBooks.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mClothes.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mContainers.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mCreatures.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mDoors.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mIngreds.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mCreatureLists.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mItemLists.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mLights.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mLockpicks.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mMiscItems.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mNpcs.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mProbes.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mRepairs.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mStatics.find (id)) + return Ptr (ref, this); + + if (LiveCellRef *ref = mWeapons.find (id)) + return Ptr (ref, this); + + return Ptr(); + } + void CellStore::load (const MWWorld::ESMStore &store, std::vector &esm) { if (mState!=State_Loaded) diff --git a/apps/openmw/mwworld/cellstore.hpp b/apps/openmw/mwworld/cellstore.hpp index ef16c64c25..66f5bbf8da 100644 --- a/apps/openmw/mwworld/cellstore.hpp +++ b/apps/openmw/mwworld/cellstore.hpp @@ -14,6 +14,7 @@ namespace ESM namespace MWWorld { + class Ptr; /// A list of cell references template @@ -63,6 +64,7 @@ namespace MWWorld const ESM::Cell *mCell; State mState; + std::vector mIds; public: @@ -72,7 +74,13 @@ namespace MWWorld State getState() const; - std::vector mIds; + bool hasId (const std::string& id) const; + ///< May return true for deleted IDs when in preload state. Will return false, if cell is + /// unloaded. + + Ptr search (const std::string& id); + ///< Will return an empty Ptr if cell is not loaded. Does not check references in + /// containers. float mWaterLevel;