diff --git a/apps/openmw/mwworld/cells.cpp b/apps/openmw/mwworld/cells.cpp index 7d04cfc687..143ce557b5 100644 --- a/apps/openmw/mwworld/cells.cpp +++ b/apps/openmw/mwworld/cells.cpp @@ -4,6 +4,8 @@ #include +#include "world.hpp" + MWWorld::Ptr::CellStore *MWWorld::Cells::getCellStore (const ESM::Cell *cell) { if (cell->data.flags & ESM::Cell::Interior) @@ -33,8 +35,8 @@ MWWorld::Ptr::CellStore *MWWorld::Cells::getCellStore (const ESM::Cell *cell) } } -MWWorld::Cells::Cells (const ESMS::ESMStore& store, ESM::ESMReader& reader) -: mStore (store), mReader (reader) {} +MWWorld::Cells::Cells (const ESMS::ESMStore& store, ESM::ESMReader& reader, MWWorld::World& world) +: mStore (store), mReader (reader), mWorld (world) {} MWWorld::Ptr::CellStore *MWWorld::Cells::getExterior (int x, int y) { @@ -43,7 +45,21 @@ MWWorld::Ptr::CellStore *MWWorld::Cells::getExterior (int x, int y) if (result==mExteriors.end()) { - const ESM::Cell *cell = mStore.cells.findExt (x, y); + const ESM::Cell *cell = mStore.cells.searchExt (x, y); + + if (!cell) + { + // Cell isn't predefined. Make one on the fly. + ESM::Cell record; + + record.data.flags = 0; + record.data.gridX = x; + record.data.gridY = y; + record.water = 0; + record.mapColor = 0; + + cell = mWorld.createRecord (record); + } result = mExteriors.insert (std::make_pair ( std::make_pair (x, y), Ptr::CellStore (cell))).first; diff --git a/apps/openmw/mwworld/cells.hpp b/apps/openmw/mwworld/cells.hpp index 661969881f..0ecbd02d37 100644 --- a/apps/openmw/mwworld/cells.hpp +++ b/apps/openmw/mwworld/cells.hpp @@ -18,6 +18,8 @@ namespace ESM namespace MWWorld { + class World; + /// \brief Cell container class Cells { @@ -25,6 +27,7 @@ namespace MWWorld ESM::ESMReader& mReader; std::map mInteriors; std::map, Ptr::CellStore> mExteriors; + MWWorld::World& mWorld; Cells (const Cells&); Cells& operator= (const Cells&); @@ -33,7 +36,9 @@ namespace MWWorld public: - Cells (const ESMS::ESMStore& store, ESM::ESMReader& reader); + Cells (const ESMS::ESMStore& store, ESM::ESMReader& reader, MWWorld::World& world); + ///< \todo pass the dynamic part of the ESMStore isntead (once it is written) of the whole + /// world Ptr::CellStore *getExterior (int x, int y); diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index ee1a0ad2d4..906d92c9f1 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -214,7 +214,7 @@ namespace MWWorld const std::string& master, const boost::filesystem::path& resDir, bool newGame, Environment& environment, const std::string& encoding) : mScene (renderer,physEng), mPlayer (0), mGlobalVariables (0), - mSky (false), mEnvironment (environment), mNextDynamicRecord (0), mCells (mStore, mEsm) + mSky (false), mEnvironment (environment), mNextDynamicRecord (0), mCells (mStore, mEsm, *this) { mPhysEngine = physEng; @@ -722,4 +722,27 @@ namespace MWWorld return std::make_pair (stream.str(), created); } + + const ESM::Cell *World::createRecord (const ESM::Cell& record) + { + if (record.data.flags & ESM::Cell::Interior) + { + if (mStore.cells.searchInt (record.name)) + throw std::runtime_error ("failed creating interior cell"); + + ESM::Cell *cell = new ESM::Cell (record); + mStore.cells.intCells.insert (std::make_pair (record.name, cell)); + return cell; + } + else + { + if (mStore.cells.searchExt (record.data.gridX, record.data.gridY)) + throw std::runtime_error ("failed creating exterior cell"); + + ESM::Cell *cell = new ESM::Cell (record); + mStore.cells.extCells.insert ( + std::make_pair (std::make_pair (record.data.gridX, record.data.gridY), cell)); + return cell; + } + } } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index 90d6121634..89c6057fa8 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -207,6 +207,10 @@ namespace MWWorld std::pair createRecord (const ESM::Class& record); ///< Create a new recrod (of type class) in the ESM store. /// \return ID, pointer to created record + + const ESM::Cell *createRecord (const ESM::Cell& record); + ///< Create a new recrod (of type cell) in the ESM store. + /// \return ID, pointer to created record }; } diff --git a/components/esm_store/cell_store.hpp b/components/esm_store/cell_store.hpp index 1336cc2ab3..19cb34c014 100644 --- a/components/esm_store/cell_store.hpp +++ b/components/esm_store/cell_store.hpp @@ -189,6 +189,9 @@ namespace ESMS { assert (cell); + if (cell->context.filename.empty()) + return; // this is a dynamically generated cell -> skipping. + // Reopen the ESM reader and seek to the right position. cell->restore (esm); @@ -212,6 +215,9 @@ namespace ESMS { assert (cell); + if (cell->context.filename.empty()) + return; // this is a dynamically generated cell -> skipping. + // Reopen the ESM reader and seek to the right position. cell->restore(esm); diff --git a/components/esm_store/reclists.hpp b/components/esm_store/reclists.hpp index 7d3afbb96b..20a2e8ff95 100644 --- a/components/esm_store/reclists.hpp +++ b/components/esm_store/reclists.hpp @@ -305,15 +305,24 @@ namespace ESMS delete it->second; } + const ESM::Cell* searchInt(const std::string &id) const + { + IntCells::const_iterator iter = intCells.find(id); + + if (iter!=intCells.end()) + return iter->second; + + return 0; + } const ESM::Cell* findInt(const std::string &id) const { - IntCells::const_iterator it = intCells.find(id); + const ESM::Cell *cell = searchInt (id); - if(it == intCells.end()) + if (!cell) throw std::runtime_error ("Interior cell not found - " + id); - return it->second; + return cell; } const ESM::Cell *searchExt (int x, int y) const