2022-12-06 00:11:19 +01:00
|
|
|
#include "worldmodel.hpp"
|
2011-09-08 11:02:55 +02:00
|
|
|
|
2018-08-14 23:05:43 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2014-01-23 11:29:40 +01:00
|
|
|
#include <components/esm/defs.hpp>
|
2023-02-22 23:20:15 +01:00
|
|
|
#include <components/esm3/cellid.hpp>
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/cellref.hpp>
|
|
|
|
#include <components/esm3/cellstate.hpp>
|
|
|
|
#include <components/esm3/esmreader.hpp>
|
|
|
|
#include <components/esm3/esmwriter.hpp>
|
2022-12-24 01:56:27 +01:00
|
|
|
#include <components/esm3/loadregn.hpp>
|
2023-05-02 22:58:15 +02:00
|
|
|
#include <components/esm4/loadwrld.hpp>
|
2015-07-09 19:22:04 +02:00
|
|
|
#include <components/loadinglistener/loadinglistener.hpp>
|
2017-05-25 13:09:40 +04:00
|
|
|
#include <components/settings/settings.hpp>
|
2014-01-23 11:29:40 +01:00
|
|
|
|
2012-07-03 12:30:50 +02:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
|
2014-02-23 20:11:05 +01:00
|
|
|
#include "cellstore.hpp"
|
2022-12-24 01:56:27 +01:00
|
|
|
#include "cellutils.hpp"
|
2012-10-01 19:17:04 +04:00
|
|
|
#include "esmstore.hpp"
|
2011-09-27 10:08:07 +02:00
|
|
|
|
2020-10-20 09:22:43 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-01-19 17:31:45 +01:00
|
|
|
template <class Visitor, class Key, class Comp>
|
2023-02-18 23:42:20 +01:00
|
|
|
bool forEachInStore(
|
|
|
|
const ESM::RefId& id, Visitor&& visitor, std::unordered_map<Key, MWWorld::CellStore, Comp>& cellStore)
|
2020-10-20 09:22:43 +00:00
|
|
|
{
|
|
|
|
for (auto& cell : cellStore)
|
|
|
|
{
|
|
|
|
if (cell.second.getState() == MWWorld::CellStore::State_Unloaded)
|
|
|
|
cell.second.preload();
|
|
|
|
if (cell.second.getState() == MWWorld::CellStore::State_Preloaded)
|
|
|
|
{
|
|
|
|
if (cell.second.hasId(id))
|
|
|
|
{
|
|
|
|
cell.second.load();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool cont = cell.second.forEach([&](MWWorld::Ptr ptr) {
|
2022-01-23 20:55:17 +01:00
|
|
|
if (ptr.getCellRef().getRefId() == id)
|
2020-10-20 09:22:43 +00:00
|
|
|
{
|
|
|
|
return visitor(ptr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!cont)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PtrCollector
|
|
|
|
{
|
|
|
|
std::vector<MWWorld::Ptr> mPtrs;
|
|
|
|
|
|
|
|
bool operator()(MWWorld::Ptr ptr)
|
|
|
|
{
|
|
|
|
mPtrs.emplace_back(ptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getCellStore(const ESM::Cell* cell)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-02-18 23:42:20 +01:00
|
|
|
CellStore* cellStore = &mCells.emplace(cell->mId, CellStore(MWWorld::Cell(*cell), mStore, mReaders)).first->second;
|
2012-09-17 11:37:50 +04:00
|
|
|
if (cell->mData.mFlags & ESM::Cell::Interior)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-01-21 17:45:23 +01:00
|
|
|
auto result = mInteriors.find(cell->mName);
|
2011-09-22 12:44:17 +02:00
|
|
|
|
|
|
|
if (result == mInteriors.end())
|
2023-02-18 23:42:20 +01:00
|
|
|
result = mInteriors.emplace(cell->mName, cellStore).first;
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
return *result->second;
|
2011-09-22 12:44:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-02 22:37:18 +02:00
|
|
|
ESM::ExteriorCellIndex extIndex = { cell->getGridX(), cell->getGridY(), ESM::Cell::sDefaultWorldspaceId };
|
|
|
|
std::map<ESM::ExteriorCellIndex, CellStore*>::iterator result = mExteriors.find(extIndex);
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-05-02 22:37:18 +02:00
|
|
|
if (result == mExteriors.end())
|
|
|
|
result = mExteriors.emplace(extIndex, cellStore).first;
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
return *result->second;
|
2011-09-22 12:44:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
void MWWorld::WorldModel::clear()
|
2013-05-15 17:54:18 +02:00
|
|
|
{
|
2022-12-23 19:57:12 +01:00
|
|
|
mPtrIndex.clear();
|
|
|
|
mPtrIndexUpdateCounter = 0;
|
2023-01-09 10:45:07 +01:00
|
|
|
mLastGeneratedRefnum = ESM::RefNum{};
|
2013-05-15 17:54:18 +02:00
|
|
|
mInteriors.clear();
|
|
|
|
mExteriors.clear();
|
2023-02-20 00:18:34 +01:00
|
|
|
mCells.clear();
|
2023-02-17 19:20:29 +01:00
|
|
|
std::fill(mIdCache.begin(), mIdCache.end(), std::make_pair(ESM::RefId(), (MWWorld::CellStore*)nullptr));
|
2013-05-15 17:54:18 +02:00
|
|
|
mIdCacheIndex = 0;
|
|
|
|
}
|
|
|
|
|
2022-12-23 19:57:12 +01:00
|
|
|
void MWWorld::WorldModel::registerPtr(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
mPtrIndex[ptr.getCellRef().getOrAssignRefNum(mLastGeneratedRefnum)] = ptr;
|
|
|
|
mPtrIndexUpdateCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MWWorld::WorldModel::deregisterPtr(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
mPtrIndex.erase(ptr.getCellRef().getRefNum());
|
|
|
|
mPtrIndexUpdateCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
MWWorld::Ptr MWWorld::WorldModel::getPtr(const ESM::RefNum& refNum) const
|
|
|
|
{
|
|
|
|
auto it = mPtrIndex.find(refNum);
|
|
|
|
if (it != mPtrIndex.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return MWWorld::Ptr();
|
|
|
|
}
|
|
|
|
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
MWWorld::Ptr MWWorld::WorldModel::getPtrAndCache(const ESM::RefId& name, CellStore& cellStore)
|
2012-06-21 11:43:18 +02:00
|
|
|
{
|
|
|
|
Ptr ptr = getPtr(name, cellStore);
|
|
|
|
|
2013-08-15 14:45:13 +02:00
|
|
|
if (!ptr.isEmpty() && ptr.isInCell())
|
2012-06-21 11:43:18 +02:00
|
|
|
{
|
|
|
|
mIdCache[mIdCacheIndex].first = name;
|
|
|
|
mIdCache[mIdCacheIndex].second = &cellStore;
|
|
|
|
if (++mIdCacheIndex >= mIdCache.size())
|
|
|
|
mIdCacheIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
void MWWorld::WorldModel::writeCell(ESM::ESMWriter& writer, CellStore& cell) const
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
2014-02-24 10:03:04 +01:00
|
|
|
if (cell.getState() != CellStore::State_Loaded)
|
2015-12-06 18:03:55 +01:00
|
|
|
cell.load();
|
2014-02-24 10:03:04 +01:00
|
|
|
|
2014-01-23 11:29:40 +01:00
|
|
|
ESM::CellState cellState;
|
|
|
|
|
|
|
|
cell.saveState(cellState);
|
|
|
|
|
|
|
|
writer.startRecord(ESM::REC_CSTA);
|
2023-02-21 23:26:40 +01:00
|
|
|
|
|
|
|
writer.writeCellId(cellState.mId);
|
2014-01-23 11:29:40 +01:00
|
|
|
cellState.save(writer);
|
2014-05-11 02:07:28 +02:00
|
|
|
cell.writeFog(writer);
|
2014-01-23 12:51:42 +01:00
|
|
|
cell.writeReferences(writer);
|
2014-01-23 11:29:40 +01:00
|
|
|
writer.endRecord(ESM::REC_CSTA);
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
MWWorld::WorldModel::WorldModel(const MWWorld::ESMStore& store, ESM::ReadersCache& readers)
|
2022-06-01 22:53:18 +02:00
|
|
|
: mStore(store)
|
|
|
|
, mReaders(readers)
|
2023-02-20 23:18:05 +01:00
|
|
|
, mIdCache(
|
|
|
|
std::clamp(Settings::Manager::getInt("pointers cache size", "Cells"), 40, 1000), { ESM::RefId(), nullptr })
|
2021-04-10 11:20:12 +04:00
|
|
|
{
|
|
|
|
}
|
2011-09-08 11:02:55 +02:00
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getExterior(ESM::ExteriorCellIndex cellIndex)
|
2011-09-08 11:02:55 +02:00
|
|
|
{
|
2023-05-02 22:37:18 +02:00
|
|
|
std::map<ESM::ExteriorCellIndex, CellStore*>::iterator result;
|
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
result = mExteriors.find(cellIndex);
|
2011-09-27 10:08:07 +02:00
|
|
|
|
2023-05-02 22:37:18 +02:00
|
|
|
if (result == mExteriors.end())
|
|
|
|
{
|
2023-05-09 22:08:17 +02:00
|
|
|
if (!ESM::isEsm4Ext(cellIndex.mWorldspace))
|
2011-09-27 10:08:07 +02:00
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
const ESM::Cell* cell = mStore.get<ESM::Cell>().search(cellIndex.mX, cellIndex.mY);
|
2023-04-22 13:59:53 +02:00
|
|
|
|
|
|
|
if (!cell)
|
|
|
|
{
|
|
|
|
// Cell isn't predefined. Make one on the fly.
|
|
|
|
ESM::Cell record;
|
|
|
|
record.mData.mFlags = ESM::Cell::HasWater;
|
2023-05-05 10:31:06 +02:00
|
|
|
record.mData.mX = cellIndex.mX;
|
|
|
|
record.mData.mY = cellIndex.mY;
|
2023-04-22 13:59:53 +02:00
|
|
|
record.mWater = 0;
|
|
|
|
record.mMapColor = 0;
|
|
|
|
record.updateId();
|
|
|
|
|
|
|
|
cell = MWBase::Environment::get().getESMStore()->insert(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
CellStore* cellStore
|
|
|
|
= &mCells.emplace(cell->mId, CellStore(MWWorld::Cell(*cell), mStore, mReaders)).first->second;
|
2023-05-05 10:31:06 +02:00
|
|
|
result = mExteriors.emplace(cellIndex, cellStore).first;
|
2011-09-27 10:08:07 +02:00
|
|
|
}
|
2023-05-02 22:37:18 +02:00
|
|
|
else
|
2023-04-22 13:59:53 +02:00
|
|
|
{
|
2023-05-02 22:37:18 +02:00
|
|
|
const Store<ESM4::Cell>& cell4Store = mStore.get<ESM4::Cell>();
|
2023-05-05 10:31:06 +02:00
|
|
|
bool exteriorExists = mStore.get<ESM4::World>().search(cellIndex.mWorldspace);
|
|
|
|
const ESM4::Cell* cell = cell4Store.searchExterior(cellIndex);
|
2023-05-02 22:37:18 +02:00
|
|
|
if (exteriorExists)
|
2023-04-22 13:59:53 +02:00
|
|
|
{
|
|
|
|
if (!cell)
|
|
|
|
{
|
|
|
|
ESM4::Cell record;
|
2023-05-05 10:31:06 +02:00
|
|
|
record.mParent = cellIndex.mWorldspace;
|
|
|
|
record.mX = cellIndex.mX;
|
|
|
|
record.mY = cellIndex.mY;
|
2023-04-22 13:59:53 +02:00
|
|
|
record.mCellFlags = !ESM4::CELL_Interior;
|
|
|
|
cell = MWBase::Environment::get().getESMStore()->insert(record);
|
|
|
|
}
|
|
|
|
CellStore* cellStore
|
|
|
|
= &mCells.emplace(cell->mId, CellStore(MWWorld::Cell(*cell), mStore, mReaders)).first->second;
|
2023-05-05 10:31:06 +02:00
|
|
|
result = mExteriors.emplace(cellIndex, cellStore).first;
|
2023-05-02 22:37:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
throw std::runtime_error("exterior not found: '" + cellIndex.mWorldspace.toDebugString() + "'");
|
2023-04-22 13:59:53 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 12:35:29 +01:00
|
|
|
}
|
2023-02-18 23:42:20 +01:00
|
|
|
if (result->second->getState() != CellStore::State_Loaded)
|
2012-05-25 17:55:00 +02:00
|
|
|
{
|
2023-02-18 23:42:20 +01:00
|
|
|
result->second->load();
|
2012-05-25 17:55:00 +02:00
|
|
|
}
|
2012-03-10 12:36:29 +01:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
return *result->second;
|
2011-09-08 11:02:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 14:32:24 +02:00
|
|
|
MWWorld::CellStore* MWWorld::WorldModel::getInteriorOrNull(std::string_view name)
|
2011-09-08 11:02:55 +02:00
|
|
|
{
|
2023-01-21 17:45:23 +01:00
|
|
|
auto result = mInteriors.find(name);
|
2011-09-08 11:02:55 +02:00
|
|
|
if (result == mInteriors.end())
|
|
|
|
{
|
2023-02-18 23:42:20 +01:00
|
|
|
CellStore* newCellStore = nullptr;
|
2023-04-10 14:32:24 +02:00
|
|
|
if (const ESM::Cell* cell = mStore.get<ESM::Cell>().search(name))
|
2023-02-18 23:42:20 +01:00
|
|
|
newCellStore = &mCells.emplace(cell->mId, CellStore(MWWorld::Cell(*cell), mStore, mReaders)).first->second;
|
2023-04-10 14:32:24 +02:00
|
|
|
else if (const ESM4::Cell* cell4 = mStore.get<ESM4::Cell>().searchCellName(name))
|
2023-02-18 23:42:20 +01:00
|
|
|
newCellStore
|
|
|
|
= &mCells.emplace(cell4->mId, CellStore(MWWorld::Cell(*cell4), mStore, mReaders)).first->second;
|
2023-04-10 14:32:24 +02:00
|
|
|
if (!newCellStore)
|
|
|
|
return nullptr; // Cell not found
|
2023-02-18 23:42:20 +01:00
|
|
|
result = mInteriors.emplace(name, newCellStore).first;
|
2012-02-12 12:35:29 +01:00
|
|
|
}
|
2011-09-08 11:02:55 +02:00
|
|
|
|
2023-02-18 23:42:20 +01:00
|
|
|
if (result->second->getState() != CellStore::State_Loaded)
|
|
|
|
result->second->load();
|
2012-03-10 12:36:29 +01:00
|
|
|
|
2023-02-18 23:42:20 +01:00
|
|
|
return result->second;
|
2011-09-08 11:02:55 +02:00
|
|
|
}
|
2011-09-22 11:54:08 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getInterior(std::string_view name)
|
2023-04-10 14:32:24 +02:00
|
|
|
{
|
|
|
|
CellStore* res = getInteriorOrNull(name);
|
|
|
|
if (res == nullptr)
|
|
|
|
throw std::runtime_error("Interior not found: '" + std::string(name) + "'");
|
2023-04-20 15:55:40 +02:00
|
|
|
return *res;
|
2023-04-10 14:32:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getCell(const ESM::RefId& id)
|
2023-02-19 17:42:37 +01:00
|
|
|
{
|
|
|
|
auto result = mCells.find(id);
|
|
|
|
if (result != mCells.end())
|
2023-04-20 15:55:40 +02:00
|
|
|
return result->second;
|
2023-02-19 17:42:37 +01:00
|
|
|
|
2023-04-08 13:33:56 +02:00
|
|
|
if (const auto* exteriorId = id.getIf<ESM::ESM3ExteriorCellRefId>())
|
2023-05-05 10:31:06 +02:00
|
|
|
return getExterior(
|
|
|
|
ESM::ExteriorCellIndex(exteriorId->getX(), exteriorId->getY(), ESM::Cell::sDefaultWorldspaceId));
|
2023-02-19 17:42:37 +01:00
|
|
|
|
|
|
|
const ESM4::Cell* cell4 = mStore.get<ESM4::Cell>().search(id);
|
|
|
|
CellStore* newCellStore = nullptr;
|
|
|
|
if (!cell4)
|
|
|
|
{
|
2023-04-06 12:52:52 +02:00
|
|
|
const ESM::Cell* cell = mStore.get<ESM::Cell>().find(id);
|
2023-02-19 17:42:37 +01:00
|
|
|
newCellStore = &mCells.emplace(cell->mId, CellStore(MWWorld::Cell(*cell), mStore, mReaders)).first->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newCellStore = &mCells.emplace(cell4->mId, CellStore(MWWorld::Cell(*cell4), mStore, mReaders)).first->second;
|
|
|
|
}
|
|
|
|
if (newCellStore->getCell()->isExterior())
|
|
|
|
{
|
|
|
|
std::pair<int, int> coord
|
|
|
|
= std::make_pair(newCellStore->getCell()->getGridX(), newCellStore->getCell()->getGridY());
|
2023-05-02 22:37:18 +02:00
|
|
|
ESM::ExteriorCellIndex extIndex = { coord.first, coord.second, newCellStore->getCell()->getWorldSpace() };
|
|
|
|
mExteriors.emplace(extIndex, newCellStore);
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-19 19:19:11 +01:00
|
|
|
mInteriors.emplace(newCellStore->getCell()->getNameId(), newCellStore);
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
|
|
|
if (newCellStore->getState() != CellStore::State_Loaded)
|
|
|
|
{
|
|
|
|
newCellStore->load();
|
|
|
|
}
|
2023-04-20 15:55:40 +02:00
|
|
|
return *newCellStore;
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getCell(std::string_view name)
|
2022-12-24 01:56:27 +01:00
|
|
|
{
|
2023-04-10 14:32:24 +02:00
|
|
|
if (CellStore* res = getInteriorOrNull(name)) // first try interiors
|
2023-04-20 15:55:40 +02:00
|
|
|
return *res;
|
2023-04-10 14:32:24 +02:00
|
|
|
|
|
|
|
// try named exteriors
|
|
|
|
const ESM::Cell* cell = mStore.get<ESM::Cell>().searchExtByName(name);
|
|
|
|
|
2022-12-24 01:56:27 +01:00
|
|
|
if (!cell)
|
|
|
|
{
|
|
|
|
// treat "Wilderness" like an empty string
|
2023-01-19 17:31:45 +01:00
|
|
|
static const std::string& defaultName
|
|
|
|
= mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString();
|
2023-01-25 20:59:10 +01:00
|
|
|
if (Misc::StringUtils::ciEqual(name, defaultName))
|
|
|
|
cell = mStore.get<ESM::Cell>().searchExtByName({});
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
|
|
|
if (!cell)
|
|
|
|
{
|
|
|
|
// now check for regions
|
|
|
|
for (const ESM::Region& region : mStore.get<ESM::Region>())
|
|
|
|
{
|
2023-01-25 20:59:10 +01:00
|
|
|
if (Misc::StringUtils::ciEqual(name, region.mName))
|
2022-12-24 01:56:27 +01:00
|
|
|
{
|
|
|
|
cell = mStore.get<ESM::Cell>().searchExtByRegion(region.mId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cell)
|
2023-01-19 17:31:45 +01:00
|
|
|
throw std::runtime_error(std::string("Can't find cell with name ") + std::string(name));
|
2023-01-22 12:18:20 +01:00
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
return getExterior(ESM::ExteriorCellIndex(cell->getGridX(), cell->getGridY(), ESM::Cell::sDefaultWorldspaceId));
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getCellByPosition(
|
2023-04-10 14:32:24 +02:00
|
|
|
const osg::Vec3f& pos, MWWorld::CellStore* cellInSameWorldSpace)
|
2022-12-24 01:56:27 +01:00
|
|
|
{
|
2023-04-10 14:32:24 +02:00
|
|
|
if (cellInSameWorldSpace && !cellInSameWorldSpace->isExterior())
|
2023-04-20 15:55:40 +02:00
|
|
|
return *cellInSameWorldSpace;
|
2023-04-10 14:32:24 +02:00
|
|
|
const osg::Vec2i cellIndex = positionToCellIndex(pos.x(), pos.y());
|
2023-04-22 13:59:53 +02:00
|
|
|
ESM::RefId exteriorWorldspace
|
|
|
|
= cellInSameWorldSpace ? cellInSameWorldSpace->getCell()->getWorldSpace() : ESM::Cell::sDefaultWorldspaceId;
|
2023-05-05 10:31:06 +02:00
|
|
|
return getExterior(ESM::ExteriorCellIndex(cellIndex.x(), cellIndex.y(), exteriorWorldspace));
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
|
|
|
|
2023-02-19 12:47:33 +01:00
|
|
|
MWWorld::Ptr MWWorld::WorldModel::getPtr(const ESM::RefId& name, CellStore& cell)
|
2011-09-22 11:54:08 +02:00
|
|
|
{
|
2014-02-23 14:26:36 +01:00
|
|
|
if (cell.getState() == CellStore::State_Unloaded)
|
2015-12-06 18:03:55 +01:00
|
|
|
cell.preload();
|
2011-09-24 11:45:59 +02:00
|
|
|
|
2014-02-23 14:26:36 +01:00
|
|
|
if (cell.getState() == CellStore::State_Preloaded)
|
2011-09-24 11:45:59 +02:00
|
|
|
{
|
2014-02-23 16:46:07 +01:00
|
|
|
if (cell.hasId(name))
|
2012-06-19 16:42:10 +02:00
|
|
|
{
|
2015-12-06 18:03:55 +01:00
|
|
|
cell.load();
|
2012-06-19 16:42:10 +02:00
|
|
|
}
|
2011-09-24 11:45:59 +02:00
|
|
|
else
|
|
|
|
return Ptr();
|
|
|
|
}
|
2013-04-14 17:37:39 +02:00
|
|
|
|
2014-02-23 16:46:07 +01:00
|
|
|
Ptr ptr = cell.search(name);
|
2011-09-22 11:54:08 +02:00
|
|
|
|
2015-12-17 20:34:50 +01:00
|
|
|
if (!ptr.isEmpty() && MWWorld::CellStore::isAccessible(ptr.getRefData(), ptr.getCellRef()))
|
2014-02-23 16:46:07 +01:00
|
|
|
return ptr;
|
2013-08-15 14:45:13 +02:00
|
|
|
|
2011-09-22 11:54:08 +02:00
|
|
|
return Ptr();
|
|
|
|
}
|
2011-09-22 12:44:17 +02:00
|
|
|
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
MWWorld::Ptr MWWorld::WorldModel::getPtr(const ESM::RefId& name)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2012-06-21 11:43:18 +02:00
|
|
|
// First check the cache
|
2021-04-10 11:20:12 +04:00
|
|
|
for (IdCache::iterator iter(mIdCache.begin()); iter != mIdCache.end(); ++iter)
|
2012-06-21 11:43:18 +02:00
|
|
|
if (iter->first == name && iter->second)
|
|
|
|
{
|
|
|
|
Ptr ptr = getPtr(name, *iter->second);
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then check cells that are already listed
|
2014-06-28 17:44:52 +02:00
|
|
|
// Search in reverse, this is a workaround for an ambiguous chargen_plank reference in the vanilla game.
|
|
|
|
// there is one at -22,16 and one at -2,-9, the latter should be used.
|
2023-05-02 22:37:18 +02:00
|
|
|
for (auto iter = mExteriors.rbegin(); iter != mExteriors.rend(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-05-02 22:37:18 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, *iter->second);
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
2011-09-22 12:44:17 +02:00
|
|
|
}
|
|
|
|
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
for (auto iter = mInteriors.begin(); iter != mInteriors.end(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-02-18 23:42:20 +01:00
|
|
|
Ptr ptr = getPtrAndCache(name, *iter->second);
|
2011-09-22 12:44:17 +02:00
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try the other cells
|
2012-11-06 12:36:21 +04:00
|
|
|
const MWWorld::Store<ESM::Cell>& cells = mStore.get<ESM::Cell>();
|
|
|
|
MWWorld::Store<ESM::Cell>::iterator iter;
|
|
|
|
|
2013-04-14 17:37:39 +02:00
|
|
|
for (iter = cells.extBegin(); iter != cells.extEnd(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& cellStore = getCellStore(&(*iter));
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, cellStore);
|
2011-09-22 12:44:17 +02:00
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-04-14 17:37:39 +02:00
|
|
|
for (iter = cells.intBegin(); iter != cells.intEnd(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& cellStore = getCellStore(&(*iter));
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, cellStore);
|
2011-09-22 12:44:17 +02:00
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// giving up
|
|
|
|
return Ptr();
|
|
|
|
}
|
2014-01-01 02:22:11 +01:00
|
|
|
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
void MWWorld::WorldModel::getExteriorPtrs(const ESM::RefId& name, std::vector<MWWorld::Ptr>& out)
|
2014-01-01 02:22:11 +01:00
|
|
|
{
|
2015-02-04 16:41:14 +01:00
|
|
|
const MWWorld::Store<ESM::Cell>& cells = mStore.get<ESM::Cell>();
|
|
|
|
for (MWWorld::Store<ESM::Cell>::iterator iter = cells.extBegin(); iter != cells.extEnd(); ++iter)
|
2014-01-01 02:22:11 +01:00
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& cellStore = getCellStore(&(*iter));
|
2015-02-04 16:41:14 +01:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, cellStore);
|
2015-02-04 16:41:14 +01:00
|
|
|
|
2014-01-01 02:22:11 +01:00
|
|
|
if (!ptr.isEmpty())
|
|
|
|
out.push_back(ptr);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 11:29:40 +01:00
|
|
|
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
std::vector<MWWorld::Ptr> MWWorld::WorldModel::getAll(const ESM::RefId& id)
|
2020-10-20 09:22:43 +00:00
|
|
|
{
|
|
|
|
PtrCollector visitor;
|
2023-02-18 23:42:20 +01:00
|
|
|
forEachInStore(id, visitor, mCells);
|
2020-10-20 09:22:43 +00:00
|
|
|
return visitor.mPtrs;
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
int MWWorld::WorldModel::countSavedGameRecords() const
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2023-02-18 23:42:20 +01:00
|
|
|
for (auto iter(mCells.begin()); iter != mCells.end(); ++iter)
|
2014-02-24 10:03:04 +01:00
|
|
|
if (iter->second.hasState())
|
2014-01-23 11:29:40 +01:00
|
|
|
++count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
void MWWorld::WorldModel::write(ESM::ESMWriter& writer, Loading::Listener& progress) const
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
2023-02-18 23:42:20 +01:00
|
|
|
for (auto iter(mCells.begin()); iter != mCells.end(); ++iter)
|
2014-02-24 10:03:04 +01:00
|
|
|
if (iter->second.hasState())
|
2014-04-28 11:29:57 +02:00
|
|
|
{
|
2014-01-23 11:29:40 +01:00
|
|
|
writeCell(writer, iter->second);
|
2015-01-11 18:01:06 +01:00
|
|
|
progress.increaseProgress();
|
2014-04-28 11:29:57 +02:00
|
|
|
}
|
2014-01-23 11:29:40 +01:00
|
|
|
}
|
|
|
|
|
2015-12-06 19:53:06 +01:00
|
|
|
struct GetCellStoreCallback : public MWWorld::CellStore::GetCellStoreCallback
|
|
|
|
{
|
|
|
|
public:
|
2022-12-15 20:56:17 +01:00
|
|
|
GetCellStoreCallback(MWWorld::WorldModel& worldModel)
|
|
|
|
: mWorldModel(worldModel)
|
2015-12-06 19:53:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:56:17 +01:00
|
|
|
MWWorld::WorldModel& mWorldModel;
|
2015-12-06 19:53:06 +01:00
|
|
|
|
2023-02-19 17:42:37 +01:00
|
|
|
MWWorld::CellStore* getCellStore(const ESM::RefId& cellId) override
|
2015-12-06 19:53:06 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
return &mWorldModel.getCell(cellId);
|
2015-12-06 19:53:06 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-10-09 10:21:12 +04:00
|
|
|
return nullptr;
|
2015-12-06 19:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
bool MWWorld::WorldModel::readRecord(ESM::ESMReader& reader, uint32_t type, const std::map<int, int>& contentFileMap)
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
|
|
|
if (type == ESM::REC_CSTA)
|
|
|
|
{
|
|
|
|
ESM::CellState state;
|
2023-02-21 23:26:40 +01:00
|
|
|
state.mId = reader.getCellId();
|
2014-01-23 11:29:40 +01:00
|
|
|
|
2020-11-13 11:39:47 +04:00
|
|
|
CellStore* cellStore = nullptr;
|
2014-01-23 11:29:40 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
cellStore = &getCell(state.mId);
|
2014-01-23 11:29:40 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// silently drop cells that don't exist anymore
|
2023-02-21 23:26:40 +01:00
|
|
|
Log(Debug::Warning) << "Warning: Dropping state for cell " << state.mId << " (cell no longer exists)";
|
2014-06-01 23:11:38 +02:00
|
|
|
reader.skipRecord();
|
|
|
|
return true;
|
2014-01-23 11:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
state.load(reader);
|
|
|
|
cellStore->loadState(state);
|
2014-01-27 13:27:42 +01:00
|
|
|
|
2014-05-11 02:07:28 +02:00
|
|
|
if (state.mHasFogOfWar)
|
|
|
|
cellStore->readFog(reader);
|
|
|
|
|
2014-02-23 14:26:36 +01:00
|
|
|
if (cellStore->getState() != CellStore::State_Loaded)
|
2015-12-06 18:03:55 +01:00
|
|
|
cellStore->load();
|
2014-01-27 13:27:42 +01:00
|
|
|
|
2015-12-06 19:53:06 +01:00
|
|
|
GetCellStoreCallback callback(*this);
|
|
|
|
|
|
|
|
cellStore->readReferences(reader, contentFileMap, &callback);
|
2014-01-23 11:29:40 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-01-24 18:21:52 +01:00
|
|
|
}
|