2022-12-06 00:11:19 +01:00
|
|
|
#include "worldmodel.hpp"
|
2011-09-08 11:02:55 +02:00
|
|
|
|
2023-05-27 14:56:28 +02:00
|
|
|
#include <algorithm>
|
2023-05-27 01:17:20 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <optional>
|
2024-06-15 00:30:14 +02:00
|
|
|
#include <stdexcept>
|
2023-05-27 14:56:28 +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>
|
2023-05-22 14:23:06 +02:00
|
|
|
#include <components/settings/values.hpp>
|
2014-01-23 11:29:40 +01:00
|
|
|
|
2014-02-23 20:11:05 +01:00
|
|
|
#include "cellstore.hpp"
|
2012-10-01 19:17:04 +04:00
|
|
|
#include "esmstore.hpp"
|
2011-09-27 10:08:07 +02:00
|
|
|
|
2023-06-13 17:04:22 +02:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/luamanager.hpp"
|
|
|
|
|
2023-05-29 19:27:06 +02:00
|
|
|
namespace MWWorld
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2023-07-03 01:21:03 +02:00
|
|
|
const ESM::RefId draftCellId = ESM::RefId::index(ESM::REC_CSTA, 0);
|
|
|
|
|
2023-05-29 19:27:06 +02:00
|
|
|
template <class T>
|
|
|
|
CellStore& emplaceCellStore(ESM::RefId id, const T& cell, ESMStore& store, ESM::ReadersCache& readers,
|
|
|
|
std::unordered_map<ESM::RefId, CellStore>& cells)
|
|
|
|
{
|
2023-07-23 18:24:55 +02:00
|
|
|
const auto [it, inserted] = cells.emplace(
|
|
|
|
std::piecewise_construct, std::forward_as_tuple(id), std::forward_as_tuple(Cell(cell), store, readers));
|
|
|
|
assert(inserted);
|
|
|
|
return it->second;
|
2023-05-29 19:27:06 +02:00
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
2023-07-26 21:26:37 +02:00
|
|
|
CellStore* emplaceInteriorCellStore(std::string_view name, ESMStore& store, ESM::ReadersCache& readers,
|
|
|
|
std::unordered_map<ESM::RefId, CellStore>& cells)
|
|
|
|
{
|
|
|
|
if (const ESM::Cell* cell = store.get<ESM::Cell>().search(name))
|
|
|
|
return &emplaceCellStore(cell->mId, *cell, store, readers, cells);
|
|
|
|
if (const ESM4::Cell* cell = store.get<ESM4::Cell>().searchCellName(name);
|
|
|
|
cell != nullptr && !cell->isExterior())
|
|
|
|
{
|
|
|
|
return &emplaceCellStore(cell->mId, *cell, store, readers, cells);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
const ESM::Cell* createEsmCell(ESM::ExteriorCellLocation location, ESMStore& store)
|
|
|
|
{
|
2023-09-04 12:16:47 +04:00
|
|
|
ESM::Cell record = {};
|
2023-05-27 01:17:20 +02:00
|
|
|
record.mData.mFlags = ESM::Cell::HasWater;
|
|
|
|
record.mData.mX = location.mX;
|
|
|
|
record.mData.mY = location.mY;
|
|
|
|
record.updateId();
|
|
|
|
return store.insert(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ESM4::Cell* createEsm4Cell(ESM::ExteriorCellLocation location, ESMStore& store)
|
|
|
|
{
|
2023-09-04 12:16:47 +04:00
|
|
|
ESM4::Cell record = {};
|
2023-05-27 01:17:20 +02:00
|
|
|
record.mParent = location.mWorldspace;
|
|
|
|
record.mX = location.mX;
|
|
|
|
record.mY = location.mY;
|
|
|
|
return store.insert(record);
|
|
|
|
}
|
|
|
|
|
2023-06-13 17:04:22 +02:00
|
|
|
std::tuple<Cell, bool> createExteriorCell(ESM::ExteriorCellLocation location, ESMStore& store)
|
2023-05-27 01:17:20 +02:00
|
|
|
{
|
|
|
|
if (ESM::isEsm4Ext(location.mWorldspace))
|
|
|
|
{
|
|
|
|
if (store.get<ESM4::World>().search(location.mWorldspace) == nullptr)
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Exterior ESM4 world is not found: " + location.mWorldspace.toDebugString());
|
|
|
|
const ESM4::Cell* cell = store.get<ESM4::Cell>().searchExterior(location);
|
2023-06-13 17:04:22 +02:00
|
|
|
bool created = cell == nullptr;
|
|
|
|
if (created)
|
2023-05-27 01:17:20 +02:00
|
|
|
cell = createEsm4Cell(location, store);
|
|
|
|
assert(cell != nullptr);
|
2023-06-13 17:04:22 +02:00
|
|
|
return { MWWorld::Cell(*cell), created };
|
2023-05-27 01:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ESM::Cell* cell = store.get<ESM::Cell>().search(location.mX, location.mY);
|
2023-06-13 17:04:22 +02:00
|
|
|
bool created = cell == nullptr;
|
|
|
|
if (created)
|
2023-05-27 01:17:20 +02:00
|
|
|
cell = createEsmCell(location, store);
|
|
|
|
assert(cell != nullptr);
|
2023-06-13 17:04:22 +02:00
|
|
|
return { Cell(*cell), created };
|
2023-05-27 01:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Cell> createCell(ESM::RefId id, const ESMStore& store)
|
|
|
|
{
|
|
|
|
if (const ESM4::Cell* cell = store.get<ESM4::Cell>().search(id))
|
|
|
|
return Cell(*cell);
|
|
|
|
if (const ESM::Cell* cell = store.get<ESM::Cell>().search(id))
|
|
|
|
return Cell(*cell);
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2024-06-24 12:47:24 +02:00
|
|
|
|
|
|
|
CellStore* getOrCreateExterior(const ESM::ExteriorCellLocation& location,
|
|
|
|
std::map<ESM::ExteriorCellLocation, MWWorld::CellStore*>& exteriors, ESMStore& store,
|
|
|
|
ESM::ReadersCache& readers, std::unordered_map<ESM::RefId, CellStore>& cells, bool triggerEvent)
|
|
|
|
{
|
|
|
|
if (const auto it = exteriors.find(location); it != exteriors.end())
|
|
|
|
{
|
|
|
|
assert(it->second != nullptr);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
auto [cell, created] = createExteriorCell(location, store);
|
|
|
|
const ESM::RefId id = cell.getId();
|
|
|
|
CellStore* const cellStore = &emplaceCellStore(id, std::move(cell), store, readers, cells);
|
|
|
|
exteriors.emplace(location, cellStore);
|
|
|
|
if (created && triggerEvent)
|
|
|
|
MWBase::Environment::get().getLuaManager()->exteriorCreated(*cellStore);
|
|
|
|
return cellStore;
|
|
|
|
}
|
2023-05-29 19:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::getOrInsertCellStore(const ESM::Cell& cell)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-05-26 21:58:35 +02:00
|
|
|
const auto it = mCells.find(cell.mId);
|
2023-05-26 21:48:38 +02:00
|
|
|
if (it != mCells.end())
|
|
|
|
return it->second;
|
2023-05-26 22:50:16 +02:00
|
|
|
return insertCellStore(cell);
|
|
|
|
}
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
MWWorld::CellStore& MWWorld::WorldModel::insertCellStore(const ESM::Cell& cell)
|
|
|
|
{
|
2023-05-29 19:27:06 +02:00
|
|
|
CellStore& cellStore = emplaceCellStore(cell.mId, cell, mStore, mReaders, mCells);
|
2023-05-26 21:58:35 +02:00
|
|
|
if (cell.mData.mFlags & ESM::Cell::Interior)
|
|
|
|
mInteriors.emplace(cell.mName, &cellStore);
|
2011-09-22 12:44:17 +02:00
|
|
|
else
|
2023-05-26 21:48:38 +02:00
|
|
|
mExteriors.emplace(
|
2023-05-26 21:58:35 +02:00
|
|
|
ESM::ExteriorCellLocation(cell.getGridX(), cell.getGridY(), ESM::Cell::sDefaultWorldspaceId), &cellStore);
|
2023-05-26 21:48:38 +02:00
|
|
|
return cellStore;
|
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
|
|
|
{
|
2023-05-28 11:56:02 +02:00
|
|
|
mPtrRegistry.clear();
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-05-26 19:23:43 +02:00
|
|
|
Ptr ptr = cellStore.getPtr(name);
|
2012-06-21 11:43:18 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-05-27 14:37:57 +02:00
|
|
|
MWWorld::WorldModel::WorldModel(MWWorld::ESMStore& store, ESM::ReadersCache& readers)
|
2022-06-01 22:53:18 +02:00
|
|
|
: mStore(store)
|
|
|
|
, mReaders(readers)
|
2023-05-22 14:23:06 +02:00
|
|
|
, mIdCache(Settings::cells().mPointersCacheSize, { ESM::RefId(), nullptr })
|
2021-04-10 11:20:12 +04:00
|
|
|
{
|
2023-07-03 01:21:03 +02:00
|
|
|
mDraftCell.mId = draftCellId;
|
2021-04-10 11:20:12 +04:00
|
|
|
}
|
2011-09-08 11:02:55 +02:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
namespace MWWorld
|
2011-09-08 11:02:55 +02:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore& WorldModel::getExterior(ESM::ExteriorCellLocation location, bool forceLoad) const
|
2012-05-25 17:55:00 +02:00
|
|
|
{
|
2024-06-24 12:47:24 +02:00
|
|
|
CellStore* cellStore = getOrCreateExterior(location, mExteriors, mStore, mReaders, mCells, true);
|
2023-06-04 22:27:33 +02:00
|
|
|
|
|
|
|
if (forceLoad && cellStore->getState() != CellStore::State_Loaded)
|
|
|
|
cellStore->load();
|
|
|
|
|
|
|
|
return *cellStore;
|
2012-05-25 17:55:00 +02:00
|
|
|
}
|
2012-03-10 12:36:29 +01:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore* WorldModel::findInterior(std::string_view name, bool forceLoad) const
|
2011-09-08 11:02:55 +02:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
const auto it = mInteriors.find(name);
|
2023-06-04 22:27:33 +02:00
|
|
|
CellStore* cellStore = nullptr;
|
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
if (it == mInteriors.end())
|
2023-06-04 22:27:33 +02:00
|
|
|
{
|
2023-07-26 21:26:37 +02:00
|
|
|
cellStore = emplaceInteriorCellStore(name, mStore, mReaders, mCells);
|
|
|
|
if (cellStore == nullptr)
|
|
|
|
return cellStore;
|
2023-07-23 18:24:55 +02:00
|
|
|
mInteriors.emplace(name, cellStore);
|
2023-06-04 22:27:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(it->second != nullptr);
|
|
|
|
cellStore = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceLoad && cellStore->getState() != CellStore::State_Loaded)
|
|
|
|
cellStore->load();
|
|
|
|
|
|
|
|
return cellStore;
|
2012-02-12 12:35:29 +01:00
|
|
|
}
|
2023-04-10 14:32:24 +02:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore& WorldModel::getInterior(std::string_view name, bool forceLoad) const
|
2023-02-19 17:42:37 +01:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore* const cellStore = findInterior(name, forceLoad);
|
|
|
|
if (cellStore == nullptr)
|
|
|
|
throw std::runtime_error("Interior cell is not found: '" + std::string(name) + "'");
|
|
|
|
return *cellStore;
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
|
|
|
CellStore* WorldModel::findCell(ESM::RefId id, bool forceLoad) const
|
2023-02-19 17:42:37 +01:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
auto it = mCells.find(id);
|
|
|
|
if (it != mCells.end())
|
2023-07-23 18:25:20 +02:00
|
|
|
{
|
|
|
|
CellStore& cellStore = it->second;
|
|
|
|
if (forceLoad && cellStore.getState() != CellStore::State_Loaded)
|
|
|
|
cellStore.load();
|
|
|
|
return &cellStore;
|
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
2023-07-03 01:21:03 +02:00
|
|
|
if (id == draftCellId)
|
|
|
|
{
|
|
|
|
CellStore& cellStore = emplaceCellStore(id, Cell(mDraftCell), mStore, mReaders, mCells);
|
|
|
|
cellStore.load();
|
|
|
|
return &cellStore;
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
if (const auto* exteriorId = id.getIf<ESM::ESM3ExteriorCellRefId>())
|
|
|
|
return &getExterior(
|
|
|
|
ESM::ExteriorCellLocation(exteriorId->getX(), exteriorId->getY(), ESM::Cell::sDefaultWorldspaceId),
|
|
|
|
forceLoad);
|
|
|
|
|
|
|
|
std::optional<Cell> cell = createCell(id, mStore);
|
|
|
|
if (!cell.has_value())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
CellStore& cellStore = emplaceCellStore(id, std::move(*cell), mStore, mReaders, mCells);
|
|
|
|
|
|
|
|
if (cellStore.isExterior())
|
|
|
|
mExteriors.emplace(ESM::ExteriorCellLocation(cellStore.getCell()->getGridX(),
|
|
|
|
cellStore.getCell()->getGridY(), cellStore.getCell()->getWorldSpace()),
|
|
|
|
&cellStore);
|
|
|
|
else
|
|
|
|
mInteriors.emplace(cellStore.getCell()->getNameId(), &cellStore);
|
|
|
|
|
|
|
|
if (forceLoad && cellStore.getState() != CellStore::State_Loaded)
|
|
|
|
cellStore.load();
|
|
|
|
|
|
|
|
return &cellStore;
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
|
|
|
CellStore& WorldModel::getCell(ESM::RefId id, bool forceLoad) const
|
2023-02-19 17:42:37 +01:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore* const result = findCell(id, forceLoad);
|
|
|
|
if (result == nullptr)
|
|
|
|
throw std::runtime_error("Cell does not exist: " + id.toDebugString());
|
|
|
|
return *result;
|
2023-02-19 17:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-07-03 01:21:03 +02:00
|
|
|
CellStore& WorldModel::getDraftCell() const
|
|
|
|
{
|
|
|
|
return getCell(draftCellId);
|
|
|
|
}
|
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore* WorldModel::findCell(std::string_view name, bool forceLoad) const
|
2023-05-13 19:30:18 +02:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
if (CellStore* const cellStore = findInterior(name, forceLoad))
|
|
|
|
return cellStore;
|
2023-04-10 14:32:24 +02:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
// try named exteriors
|
|
|
|
const ESM::Cell* cell = mStore.get<ESM::Cell>().searchExtByName(name);
|
2023-04-10 14:32:24 +02:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
if (cell == nullptr)
|
2022-12-24 01:56:27 +01:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
// treat "Wilderness" like an empty string
|
|
|
|
static const std::string& defaultName
|
|
|
|
= mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString();
|
|
|
|
if (Misc::StringUtils::ciEqual(name, defaultName))
|
|
|
|
cell = mStore.get<ESM::Cell>().searchExtByName({});
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
|
|
|
if (cell == nullptr)
|
|
|
|
{
|
|
|
|
// now check for regions
|
|
|
|
const Store<ESM::Region>& regions = mStore.get<ESM::Region>();
|
|
|
|
const auto region = std::find_if(regions.begin(), regions.end(),
|
|
|
|
[&](const ESM::Region& v) { return Misc::StringUtils::ciEqual(name, v.mName); });
|
|
|
|
if (region != regions.end())
|
|
|
|
cell = mStore.get<ESM::Cell>().searchExtByRegion(region->mId);
|
|
|
|
}
|
|
|
|
|
2023-07-26 21:26:37 +02:00
|
|
|
if (cell != nullptr)
|
|
|
|
return &getExterior(
|
|
|
|
ESM::ExteriorCellLocation(cell->getGridX(), cell->getGridY(), ESM::Cell::sDefaultWorldspaceId),
|
|
|
|
forceLoad);
|
|
|
|
|
|
|
|
if (const ESM4::Cell* cell4 = mStore.get<ESM4::Cell>().searchCellName(name);
|
|
|
|
cell4 != nullptr && cell4->isExterior())
|
|
|
|
{
|
|
|
|
return &getExterior(cell4->getExteriorCellLocation(), forceLoad);
|
|
|
|
}
|
2023-05-27 01:17:20 +02:00
|
|
|
|
2023-07-26 21:26:37 +02:00
|
|
|
return nullptr;
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
2023-01-22 12:18:20 +01:00
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
CellStore& WorldModel::getCell(std::string_view name, bool forceLoad) const
|
|
|
|
{
|
|
|
|
CellStore* const result = findCell(name, forceLoad);
|
|
|
|
if (result == nullptr)
|
|
|
|
throw std::runtime_error(std::string("Can't find cell with name ") + std::string(name));
|
|
|
|
return *result;
|
|
|
|
}
|
2024-06-15 00:30:14 +02:00
|
|
|
|
|
|
|
void WorldModel::registerPtr(const Ptr& ptr)
|
|
|
|
{
|
|
|
|
if (ptr.mRef == nullptr)
|
|
|
|
throw std::logic_error("Ptr with nullptr mRef is not allowed to be registered");
|
|
|
|
mPtrRegistry.insert(ptr);
|
|
|
|
ptr.mRef->mWorldModel = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorldModel::deregisterLiveCellRef(LiveCellRefBase& ref) noexcept
|
|
|
|
{
|
|
|
|
mPtrRegistry.remove(ref);
|
|
|
|
ref.mWorldModel = nullptr;
|
|
|
|
}
|
2022-12-24 01:56:27 +01:00
|
|
|
}
|
|
|
|
|
2023-07-30 21:55:36 +02:00
|
|
|
MWWorld::Ptr MWWorld::WorldModel::getPtrByRefId(const ESM::RefId& name)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-05-26 19:26:23 +02:00
|
|
|
for (const auto& [cachedId, cellStore] : mIdCache)
|
|
|
|
{
|
|
|
|
if (cachedId != name || cellStore == nullptr)
|
|
|
|
continue;
|
|
|
|
Ptr ptr = cellStore->getPtr(name);
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
2012-06-21 11:43:18 +02:00
|
|
|
|
|
|
|
// 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>();
|
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
for (auto iter = cells.extBegin(); iter != cells.extEnd(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-05-26 22:50:16 +02:00
|
|
|
if (mCells.contains(iter->mId))
|
|
|
|
continue;
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, insertCellStore(*iter));
|
2011-09-22 12:44:17 +02:00
|
|
|
|
|
|
|
if (!ptr.isEmpty())
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
for (auto iter = cells.intBegin(); iter != cells.intEnd(); ++iter)
|
2011-09-22 12:44:17 +02:00
|
|
|
{
|
2023-05-26 22:50:16 +02:00
|
|
|
if (mCells.contains(iter->mId))
|
|
|
|
continue;
|
2011-09-22 12:44:17 +02:00
|
|
|
|
2023-05-26 22:50:16 +02:00
|
|
|
Ptr ptr = getPtrAndCache(name, insertCellStore(*iter));
|
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-05-26 22:50:16 +02:00
|
|
|
CellStore& cellStore = getOrInsertCellStore(*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
|
|
|
{
|
2023-05-28 17:37:40 +02:00
|
|
|
std::vector<Ptr> result;
|
|
|
|
for (auto& [cellId, cellStore] : mCells)
|
|
|
|
{
|
|
|
|
if (cellStore.getState() == CellStore::State_Unloaded)
|
|
|
|
cellStore.preload();
|
|
|
|
if (cellStore.getState() == CellStore::State_Preloaded)
|
|
|
|
{
|
|
|
|
if (!cellStore.hasId(id))
|
|
|
|
continue;
|
|
|
|
cellStore.load();
|
|
|
|
}
|
|
|
|
cellStore.forEach([&](const Ptr& ptr) {
|
|
|
|
if (ptr.getCellRef().getRefId() == id)
|
|
|
|
result.push_back(ptr);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
2020-10-20 09:22:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 00:11:19 +01:00
|
|
|
int MWWorld::WorldModel::countSavedGameRecords() const
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
2023-05-27 14:56:28 +02:00
|
|
|
return std::count_if(mCells.begin(), mCells.end(), [](const auto& v) { return v.second.hasState(); });
|
2014-01-23 11:29:40 +01:00
|
|
|
}
|
|
|
|
|
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-05-26 19:26:23 +02:00
|
|
|
for (auto& [id, cellStore] : mCells)
|
|
|
|
if (cellStore.hasState())
|
2014-04-28 11:29:57 +02:00
|
|
|
{
|
2023-05-26 19:26:23 +02:00
|
|
|
writeCell(writer, cellStore);
|
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
|
|
|
}
|
|
|
|
|
2024-06-24 12:47:24 +02:00
|
|
|
struct MWWorld::WorldModel::GetCellStoreCallback : public CellStore::GetCellStoreCallback
|
2015-12-06 19:53:06 +01:00
|
|
|
{
|
|
|
|
public:
|
2024-06-24 12:47:24 +02:00
|
|
|
GetCellStoreCallback(WorldModel& worldModel)
|
2022-12-15 20:56:17 +01:00
|
|
|
: mWorldModel(worldModel)
|
2015-12-06 19:53:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-24 12:47:24 +02:00
|
|
|
WorldModel& mWorldModel;
|
2015-12-06 19:53:06 +01:00
|
|
|
|
2024-06-24 12:47:24 +02:00
|
|
|
CellStore* getCellStore(const ESM::RefId& cellId) override
|
|
|
|
{
|
|
|
|
if (const auto* exteriorId = cellId.getIf<ESM::ESM3ExteriorCellRefId>())
|
|
|
|
{
|
|
|
|
ESM::ExteriorCellLocation location(exteriorId->getX(), exteriorId->getY(), ESM::Cell::sDefaultWorldspaceId);
|
|
|
|
return getOrCreateExterior(
|
|
|
|
location, mWorldModel.mExteriors, mWorldModel.mStore, mWorldModel.mReaders, mWorldModel.mCells, false);
|
|
|
|
}
|
|
|
|
return mWorldModel.findCell(cellId);
|
|
|
|
}
|
2015-12-06 19:53:06 +01:00
|
|
|
};
|
|
|
|
|
2023-07-30 01:22:47 +02:00
|
|
|
bool MWWorld::WorldModel::readRecord(ESM::ESMReader& reader, uint32_t type)
|
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
|
|
|
|
2024-06-24 12:47:24 +02:00
|
|
|
GetCellStoreCallback callback(*this);
|
|
|
|
|
|
|
|
CellStore* const cellStore = callback.getCellStore(state.mId);
|
|
|
|
|
2023-05-27 01:17:20 +02:00
|
|
|
if (cellStore == nullptr)
|
2014-01-23 11:29:40 +01:00
|
|
|
{
|
2023-05-27 01:17:20 +02:00
|
|
|
Log(Debug::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
|
|
|
|
2023-07-30 01:22:47 +02:00
|
|
|
cellStore->readReferences(reader, &callback);
|
2014-01-23 11:29:40 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-01-24 18:21:52 +01:00
|
|
|
}
|