From ce7202a1476c45a52662c5081500d97be8f8812c Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 10 Sep 2011 11:22:32 +0200 Subject: [PATCH] Issue #28: shifting around functionality among CellStore, Cells and CellList --- apps/openmw/mwworld/cells.cpp | 13 +++++++++---- components/esm/loadcell.cpp | 17 +++++++++++++++++ components/esm/loadcell.hpp | 3 +++ components/esm_store/cell_store.hpp | 29 +++++------------------------ components/esm_store/reclists.hpp | 11 ++++++++++- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/apps/openmw/mwworld/cells.cpp b/apps/openmw/mwworld/cells.cpp index 062408ffd2..67a2a75906 100644 --- a/apps/openmw/mwworld/cells.cpp +++ b/apps/openmw/mwworld/cells.cpp @@ -10,9 +10,12 @@ MWWorld::Ptr::CellStore *MWWorld::Cells::getExterior (int x, int y) if (result==mExteriors.end()) { - result = mExteriors.insert (std::make_pair (std::make_pair (x, y), Ptr::CellStore())).first; + const ESM::Cell *cell = mStore.cells.findExt (x, y); - result->second.loadExt (x, y, mStore, mReader); + result = mExteriors.insert (std::make_pair ( + std::make_pair (x, y), Ptr::CellStore (cell))).first; + + result->second.load (mStore, mReader); } return &result->second; @@ -24,9 +27,11 @@ MWWorld::Ptr::CellStore *MWWorld::Cells::getInterior (const std::string& name) if (result==mInteriors.end()) { - result = mInteriors.insert (std::make_pair (name, Ptr::CellStore())).first; + const ESM::Cell *cell = mStore.cells.findInt (name); - result->second.loadInt (name, mStore, mReader); + result = mInteriors.insert (std::make_pair (name, Ptr::CellStore (cell))).first; + + result->second.load (mStore, mReader); } return &result->second; diff --git a/components/esm/loadcell.cpp b/components/esm/loadcell.cpp index 2d10852603..80a0f3e5a4 100644 --- a/components/esm/loadcell.cpp +++ b/components/esm/loadcell.cpp @@ -1,5 +1,8 @@ #include "loadcell.hpp" +#include +#include + namespace ESM { @@ -47,6 +50,20 @@ void Cell::restore(ESMReader &esm) const esm.restoreContext(context); } +std::string Cell::getDescription() const +{ + if (data.flags & Interior) + { + return name; + } + else + { + std::ostringstream stream; + stream << data.gridX << ", " << data.gridY; + return stream.str(); + } +} + bool Cell::getNextRef(ESMReader &esm, CellRef &ref) { if (!esm.hasMoreSubs()) diff --git a/components/esm/loadcell.hpp b/components/esm/loadcell.hpp index bf3ec6f739..671f702cab 100644 --- a/components/esm/loadcell.hpp +++ b/components/esm/loadcell.hpp @@ -126,6 +126,9 @@ struct Cell // exactly. void restore(ESMReader &esm) const; + std::string getDescription() const; + ///< Return a short string describing the cell (mostly used for debugging/logging purpose) + /* Get the next reference in this cell, if any. Returns false when there are no more references in the cell. diff --git a/components/esm_store/cell_store.hpp b/components/esm_store/cell_store.hpp index 59464236fd..0daf7258e7 100644 --- a/components/esm_store/cell_store.hpp +++ b/components/esm_store/cell_store.hpp @@ -81,7 +81,8 @@ namespace ESMS class CellStore { public: - CellStore() : cell (0) {} + + CellStore (const ESM::Cell *cell_) : cell (cell_) {} const ESM::Cell *cell; @@ -107,31 +108,11 @@ namespace ESMS CellRefList statics; CellRefList weapons; - /** Look up and load an interior cell from the given ESM data - storage. */ - void loadInt(const std::string &name, const ESMStore &store, ESMReader &esm) + void load (const ESMStore &store, ESMReader &esm) { - std::cout << "loading cell '" << name << "'\n"; + std::cout << "loading cell " << cell->getDescription() << std::endl; - cell = store.cells.findInt(name); - - if(cell == NULL) - throw std::runtime_error("Cell not found - " + name); - - loadRefs(store, esm); - } - - /** Ditto for exterior cell. */ - void loadExt(int X, int Y, const ESMStore &store, ESMReader &esm) - { - std::cout << "loading exterior cell '" << X << ", " << Y << "'\n"; - - cell = store.cells.searchExt (X, Y); - - if(cell == NULL) - throw std::runtime_error("Exterior cell not found"); - - loadRefs(store, esm); + loadRefs (store, esm); } /// Call functor (ref) for each reference. functor must return a bool. Returning diff --git a/components/esm_store/reclists.hpp b/components/esm_store/reclists.hpp index 19ee18b628..008bdce0a0 100644 --- a/components/esm_store/reclists.hpp +++ b/components/esm_store/reclists.hpp @@ -318,7 +318,7 @@ namespace ESMS IntCells::const_iterator it = intCells.find(id); if(it == intCells.end()) - return NULL; + throw std::runtime_error ("Interior cell not found - " + id); return it->second; } @@ -338,6 +338,15 @@ namespace ESMS return it2->second; } + const ESM::Cell *findExt (int x, int y) const + { + const ESM::Cell *cell = searchExt (x, y); + + if (!cell) + throw std::runtime_error ("Exterior cell not found"); + + return cell; + } const ESM::Cell *searchExtByName (const std::string& id) const { for (ExtCells::const_iterator iter = extCells.begin(); iter!=extCells.end(); ++iter)