2011-09-08 09:02:55 +00:00
|
|
|
#include "cells.hpp"
|
|
|
|
|
2012-07-03 11:15:20 +00:00
|
|
|
#include <components/esm_store/store.hpp>
|
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
|
2012-03-10 11:36:29 +00:00
|
|
|
#include "class.hpp"
|
|
|
|
#include "containerstore.hpp"
|
2011-09-27 08:08:07 +00:00
|
|
|
|
2011-09-22 10:44:17 +00:00
|
|
|
MWWorld::Ptr::CellStore *MWWorld::Cells::getCellStore (const ESM::Cell *cell)
|
|
|
|
{
|
|
|
|
if (cell->data.flags & ESM::Cell::Interior)
|
|
|
|
{
|
|
|
|
std::map<std::string, Ptr::CellStore>::iterator result = mInteriors.find (cell->name);
|
|
|
|
|
|
|
|
if (result==mInteriors.end())
|
|
|
|
{
|
|
|
|
result = mInteriors.insert (std::make_pair (cell->name, Ptr::CellStore (cell))).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::map<std::pair<int, int>, Ptr::CellStore>::iterator result =
|
|
|
|
mExteriors.find (std::make_pair (cell->data.gridX, cell->data.gridY));
|
|
|
|
|
|
|
|
if (result==mExteriors.end())
|
|
|
|
{
|
|
|
|
result = mExteriors.insert (std::make_pair (
|
|
|
|
std::make_pair (cell->data.gridX, cell->data.gridY), Ptr::CellStore (cell))).first;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 11:36:29 +00:00
|
|
|
void MWWorld::Cells::fillContainers (Ptr::CellStore& cellStore)
|
|
|
|
{
|
2012-06-29 16:54:23 +00:00
|
|
|
for (CellRefList<ESM::Container>::List::iterator iter (
|
2012-03-10 11:36:29 +00:00
|
|
|
cellStore.containers.list.begin());
|
|
|
|
iter!=cellStore.containers.list.end(); ++iter)
|
|
|
|
{
|
|
|
|
Ptr container (&*iter, &cellStore);
|
|
|
|
|
|
|
|
Class::get (container).getContainerStore (container).fill (
|
|
|
|
iter->base->inventory, mStore);
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
for (CellRefList<ESM::Creature>::List::iterator iter (
|
2012-03-10 11:36:29 +00:00
|
|
|
cellStore.creatures.list.begin());
|
|
|
|
iter!=cellStore.creatures.list.end(); ++iter)
|
|
|
|
{
|
|
|
|
Ptr container (&*iter, &cellStore);
|
|
|
|
|
|
|
|
Class::get (container).getContainerStore (container).fill (
|
|
|
|
iter->base->inventory, mStore);
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
for (CellRefList<ESM::NPC>::List::iterator iter (
|
2012-03-10 11:36:29 +00:00
|
|
|
cellStore.npcs.list.begin());
|
|
|
|
iter!=cellStore.npcs.list.end(); ++iter)
|
|
|
|
{
|
|
|
|
Ptr container (&*iter, &cellStore);
|
|
|
|
|
|
|
|
Class::get (container).getContainerStore (container).fill (
|
|
|
|
iter->base->inventory, mStore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 09:43:18 +00:00
|
|
|
MWWorld::Ptr MWWorld::Cells::getPtrAndCache (const std::string& name, Ptr::CellStore& cellStore)
|
|
|
|
{
|
|
|
|
Ptr ptr = getPtr (name, cellStore);
|
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
{
|
|
|
|
mIdCache[mIdCacheIndex].first = name;
|
|
|
|
mIdCache[mIdCacheIndex].second = &cellStore;
|
|
|
|
if (++mIdCacheIndex>=mIdCache.size())
|
|
|
|
mIdCacheIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
MWWorld::Cells::Cells (const ESMS::ESMStore& store, ESM::ESMReader& reader)
|
|
|
|
: mStore (store), mReader (reader),
|
2012-07-06 23:14:18 +00:00
|
|
|
mIdCache (20, std::pair<std::string, Ptr::CellStore *> ("", (Ptr::CellStore*)0)), /// \todo make cache size configurable
|
2012-06-21 09:43:18 +00:00
|
|
|
mIdCacheIndex (0)
|
|
|
|
{}
|
2011-09-08 09:02:55 +00:00
|
|
|
|
|
|
|
MWWorld::Ptr::CellStore *MWWorld::Cells::getExterior (int x, int y)
|
|
|
|
{
|
|
|
|
std::map<std::pair<int, int>, Ptr::CellStore>::iterator result =
|
|
|
|
mExteriors.find (std::make_pair (x, y));
|
|
|
|
|
|
|
|
if (result==mExteriors.end())
|
|
|
|
{
|
2011-09-27 08:08:07 +00:00
|
|
|
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;
|
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
cell = MWBase::Environment::get().getWorld()->createRecord (record);
|
2011-09-27 08:08:07 +00:00
|
|
|
}
|
2011-09-08 09:02:55 +00:00
|
|
|
|
2011-09-10 09:22:32 +00:00
|
|
|
result = mExteriors.insert (std::make_pair (
|
2012-07-03 10:30:50 +00:00
|
|
|
std::make_pair (x, y), CellStore (cell))).first;
|
2012-02-12 11:35:29 +00:00
|
|
|
}
|
2011-09-10 09:22:32 +00:00
|
|
|
|
2012-02-12 11:35:29 +00:00
|
|
|
if (result->second.mState!=Ptr::CellStore::State_Loaded)
|
2012-05-25 15:55:00 +00:00
|
|
|
{
|
2011-09-10 09:22:32 +00:00
|
|
|
result->second.load (mStore, mReader);
|
2012-03-10 11:36:29 +00:00
|
|
|
fillContainers (result->second);
|
2012-05-25 15:55:00 +00:00
|
|
|
}
|
2012-03-10 11:36:29 +00:00
|
|
|
|
2011-09-08 09:02:55 +00:00
|
|
|
return &result->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
MWWorld::Ptr::CellStore *MWWorld::Cells::getInterior (const std::string& name)
|
|
|
|
{
|
|
|
|
std::map<std::string, Ptr::CellStore>::iterator result = mInteriors.find (name);
|
|
|
|
|
|
|
|
if (result==mInteriors.end())
|
|
|
|
{
|
2011-09-10 09:22:32 +00:00
|
|
|
const ESM::Cell *cell = mStore.cells.findInt (name);
|
|
|
|
|
|
|
|
result = mInteriors.insert (std::make_pair (name, Ptr::CellStore (cell))).first;
|
2012-02-12 11:35:29 +00:00
|
|
|
}
|
2011-09-08 09:02:55 +00:00
|
|
|
|
2012-02-12 11:35:29 +00:00
|
|
|
if (result->second.mState!=Ptr::CellStore::State_Loaded)
|
2012-05-25 15:55:00 +00:00
|
|
|
{
|
2011-09-10 09:22:32 +00:00
|
|
|
result->second.load (mStore, mReader);
|
2012-03-10 11:36:29 +00:00
|
|
|
fillContainers (result->second);
|
2012-05-25 15:55:00 +00:00
|
|
|
}
|
2012-03-10 11:36:29 +00:00
|
|
|
|
2011-09-08 09:02:55 +00:00
|
|
|
return &result->second;
|
|
|
|
}
|
2011-09-22 09:54:08 +00:00
|
|
|
|
|
|
|
MWWorld::Ptr MWWorld::Cells::getPtr (const std::string& name, Ptr::CellStore& cell)
|
|
|
|
{
|
2011-09-24 09:45:59 +00:00
|
|
|
if (cell.mState==Ptr::CellStore::State_Unloaded)
|
|
|
|
cell.preload (mStore, mReader);
|
|
|
|
|
|
|
|
if (cell.mState==Ptr::CellStore::State_Preloaded)
|
|
|
|
{
|
|
|
|
std::string lowerCase;
|
|
|
|
|
|
|
|
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
|
|
|
|
(int(*)(int)) std::tolower);
|
|
|
|
|
|
|
|
if (std::binary_search (cell.mIds.begin(), cell.mIds.end(), lowerCase))
|
2012-06-19 14:42:10 +00:00
|
|
|
{
|
2011-09-24 09:45:59 +00:00
|
|
|
cell.load (mStore, mReader);
|
2012-06-19 14:42:10 +00:00
|
|
|
fillContainers (cell);
|
|
|
|
}
|
2011-09-24 09:45:59 +00:00
|
|
|
else
|
|
|
|
return Ptr();
|
|
|
|
}
|
2012-08-06 14:20:48 +00:00
|
|
|
MWWorld::Ptr ptr;
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Activator> *ref = cell.activators.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Potion> *ref = cell.potions.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Apparatus> *ref = cell.appas.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Armor> *ref = cell.armors.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Book> *ref = cell.books.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Clothing> *ref = cell.clothes.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Container> *ref = cell.containers.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Creature> *ref = cell.creatures.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Door> *ref = cell.doors.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Ingredient> *ref = cell.ingreds.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::CreatureLevList> *ref = cell.creatureLists.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::ItemLevList> *ref = cell.itemLists.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Light> *ref = cell.lights.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Tool> *ref = cell.lockpicks.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Miscellaneous> *ref = cell.miscItems.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::NPC> *ref = cell.npcs.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Probe> *ref = cell.probes.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Repair> *ref = cell.repairs.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Static> *ref = cell.statics.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-06-29 16:54:23 +00:00
|
|
|
if (MWWorld::LiveCellRef<ESM::Weapon> *ref = cell.weapons.find (name))
|
2012-08-06 14:20:48 +00:00
|
|
|
ptr = Ptr (ref, &cell);
|
2011-09-22 09:54:08 +00:00
|
|
|
|
2012-08-06 14:20:48 +00:00
|
|
|
if (!ptr.isEmpty() && ptr.getRefData().getCount() > 0) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2011-09-22 09:54:08 +00:00
|
|
|
return Ptr();
|
|
|
|
}
|
2011-09-22 10:44:17 +00:00
|
|
|
|
|
|
|
MWWorld::Ptr MWWorld::Cells::getPtr (const std::string& name)
|
|
|
|
{
|
2012-06-21 09:43:18 +00:00
|
|
|
// First check the cache
|
|
|
|
for (std::vector<std::pair<std::string, Ptr::CellStore *> >::iterator iter (mIdCache.begin());
|
|
|
|
iter!=mIdCache.end(); ++iter)
|
|
|
|
if (iter->first==name && iter->second)
|
|
|
|
{
|
|
|
|
Ptr ptr = getPtr (name, *iter->second);
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then check cells that are already listed
|
2011-09-22 10:44:17 +00:00
|
|
|
for (std::map<std::string, Ptr::CellStore>::iterator iter = mInteriors.begin();
|
|
|
|
iter!=mInteriors.end(); ++iter)
|
|
|
|
{
|
2012-06-21 09:43:18 +00:00
|
|
|
Ptr ptr = getPtrAndCache (name, iter->second);
|
2011-09-22 10:44:17 +00:00
|
|
|
if (!ptr.isEmpty())
|
2012-06-21 09:43:18 +00:00
|
|
|
return ptr;
|
2011-09-22 10:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::map<std::pair<int, int>, Ptr::CellStore>::iterator iter = mExteriors.begin();
|
|
|
|
iter!=mExteriors.end(); ++iter)
|
|
|
|
{
|
2012-06-21 09:43:18 +00:00
|
|
|
Ptr ptr = getPtrAndCache (name, iter->second);
|
2011-09-22 10:44:17 +00:00
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try the other cells
|
|
|
|
for (ESMS::CellList::IntCells::const_iterator iter = mStore.cells.intCells.begin();
|
|
|
|
iter!=mStore.cells.intCells.end(); ++iter)
|
|
|
|
{
|
|
|
|
Ptr::CellStore *cellStore = getCellStore (iter->second);
|
|
|
|
|
2012-06-21 09:43:18 +00:00
|
|
|
Ptr ptr = getPtrAndCache (name, *cellStore);
|
2011-09-22 10:44:17 +00:00
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ESMS::CellList::ExtCells::const_iterator iter = mStore.cells.extCells.begin();
|
|
|
|
iter!=mStore.cells.extCells.end(); ++iter)
|
|
|
|
{
|
|
|
|
Ptr::CellStore *cellStore = getCellStore (iter->second);
|
|
|
|
|
2012-06-21 09:43:18 +00:00
|
|
|
Ptr ptr = getPtrAndCache (name, *cellStore);
|
2011-09-22 10:44:17 +00:00
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// giving up
|
|
|
|
return Ptr();
|
|
|
|
}
|