Merge branch 'cellstore_optimize' into 'master'

Optimize CellStore modifications

See merge request OpenMW/openmw!3013
simplify_debugging
psi29a 2 years ago
commit 42bf639916

@ -430,7 +430,7 @@ namespace MWWorld
{
mMovedHere.insert(std::make_pair(object.getBase(), from));
}
updateMergedRefs();
requestMergedRefsUpdate();
}
MWWorld::Ptr CellStore::moveTo(const Ptr& object, CellStore* cellToMoveTo)
@ -443,10 +443,6 @@ namespace MWWorld
throw std::runtime_error(
"moveTo: can't move object from a non-loaded cell (how did you get this object anyway?)");
// Ensure that the object actually exists in the cell
if (searchViaRefNum(object.getCellRef().getRefNum()).isEmpty())
throw std::runtime_error("moveTo: object is not in this cell");
MWBase::Environment::get().getWorldModel()->registerPtr(MWWorld::Ptr(object.getBase(), cellToMoveTo));
MovedRefTracker::iterator found = mMovedHere.find(object.getBase());
@ -466,14 +462,14 @@ namespace MWWorld
originalCell->moveTo(object, cellToMoveTo);
}
updateMergedRefs();
requestMergedRefsUpdate();
return MWWorld::Ptr(object.getBase(), cellToMoveTo);
}
cellToMoveTo->moveFrom(object, this);
mMovedToAnotherCell.insert(std::make_pair(object.getBase(), cellToMoveTo));
updateMergedRefs();
requestMergedRefsUpdate();
MWWorld::Ptr ptr(object.getBase(), cellToMoveTo);
const Class& cls = ptr.getClass();
if (cls.hasInventoryStore(ptr))
@ -513,13 +509,19 @@ namespace MWWorld
const std::map<LiveCellRefBase*, MWWorld::CellStore*>& mMovedToAnotherCell;
};
void CellStore::updateMergedRefs()
void CellStore::requestMergedRefsUpdate()
{
mMergedRefs.clear();
mRechargingItemsUpToDate = false;
mMergedRefsNeedsUpdate = true;
}
void CellStore::updateMergedRefs() const
{
mMergedRefs.clear();
MergeVisitor visitor(mMergedRefs, mMovedHere, mMovedToAnotherCell);
CellStoreImp::forEachInternal(visitor, *this);
CellStoreImp::forEachInternal(visitor, const_cast<CellStore&>(*this));
visitor.merge();
mMergedRefsNeedsUpdate = false;
}
bool CellStore::movedHere(const MWWorld::Ptr& ptr) const
@ -659,13 +661,6 @@ namespace MWWorld
}
};
Ptr CellStore::searchViaRefNum(const ESM::RefNum& refNum)
{
RefNumSearchVisitor searchVisitor(refNum);
forEach(searchVisitor);
return searchVisitor.mFound;
}
float CellStore::getWaterLevel() const
{
if (isExterior())
@ -681,6 +676,8 @@ namespace MWWorld
std::size_t CellStore::count() const
{
if (mMergedRefsNeedsUpdate)
updateMergedRefs();
return mMergedRefs.size();
}
@ -847,7 +844,7 @@ namespace MWWorld
ESM::visit([&](auto&& cell) { loadRefs(cell, refNumToID); }, mCellVariant);
updateMergedRefs();
requestMergedRefsUpdate();
}
bool CellStore::isExterior() const
@ -1033,10 +1030,6 @@ namespace MWWorld
}
}
// Do another update here to make sure objects referred to by MVRF tags can be found
// This update is only needed for old saves that used the old copy&delete way of moving objects
updateMergedRefs();
while (reader.isNextSub("MVRF"))
{
reader.cacheSubName();
@ -1050,7 +1043,7 @@ namespace MWWorld
}
// Search for the reference. It might no longer exist if its content file was removed.
Ptr movedRef = searchViaRefNum(refnum);
Ptr movedRef = MWBase::Environment::get().getWorldModel()->getPtr(refnum);
if (movedRef.isEmpty())
{
Log(Debug::Warning) << "Warning: Dropping moved ref tag for " << refnum.mIndex

@ -128,7 +128,7 @@ namespace MWWorld
mHasState = true;
CellRefList<T>& list = get<T>();
LiveCellRefBase* ret = &list.insert(*ref);
updateMergedRefs();
requestMergedRefsUpdate();
return ret;
}
@ -166,11 +166,6 @@ namespace MWWorld
Ptr searchViaActorId(int id);
///< Will return an empty Ptr if cell is not loaded.
Ptr searchViaRefNum(const ESM::RefNum& refNum);
///< Will return an empty Ptr if cell is not loaded. Does not check references in
/// containers.
/// @note Triggers CellStore hasState flag.
float getWaterLevel() const;
bool movedHere(const MWWorld::Ptr& ptr) const;
@ -203,6 +198,8 @@ namespace MWWorld
if (mState != State_Loaded)
return false;
if (mMergedRefsNeedsUpdate)
updateMergedRefs();
if (mMergedRefs.empty())
return true;
@ -230,6 +227,9 @@ namespace MWWorld
if (mState != State_Loaded)
return false;
if (mMergedRefsNeedsUpdate)
updateMergedRefs();
for (unsigned int i = 0; i < mMergedRefs.size(); ++i)
{
if (!isAccessible(mMergedRefs[i]->mData, mMergedRefs[i]->mRef))
@ -252,6 +252,8 @@ namespace MWWorld
if (mState != State_Loaded)
return false;
if (mMergedRefsNeedsUpdate)
updateMergedRefs();
if (mMergedRefs.empty())
return true;
@ -373,7 +375,8 @@ namespace MWWorld
// Merged list of ref's currently in this cell - i.e. with added refs from mMovedHere, removed refs from
// mMovedToAnotherCell
std::vector<LiveCellRefBase*> mMergedRefs;
mutable std::vector<LiveCellRefBase*> mMergedRefs;
mutable bool mMergedRefsNeedsUpdate = false;
// Get the Ptr for the given ref which originated from this cell (possibly moved to another cell at this point).
Ptr getCurrentPtr(MWWorld::LiveCellRefBase* ref);
@ -382,7 +385,8 @@ namespace MWWorld
void moveFrom(const MWWorld::Ptr& object, MWWorld::CellStore* from);
/// Repopulate mMergedRefs.
void updateMergedRefs();
void requestMergedRefsUpdate();
void updateMergedRefs() const;
// (item, max charge)
typedef std::vector<std::pair<LiveCellRefBase*, float>> TRechargingItems;

@ -1880,14 +1880,7 @@ namespace MWWorld
facedObject = rayToObject.mHitObject;
if (facedObject.isEmpty() && rayToObject.mHitRefnum.isSet())
{
for (CellStore* cellstore : mWorldScene->getActiveCells())
{
facedObject = cellstore->searchViaRefNum(rayToObject.mHitRefnum);
if (!facedObject.isEmpty())
break;
}
}
facedObject = MWBase::Environment::get().getWorldModel()->getPtr(rayToObject.mHitRefnum);
if (rayToObject.mHit)
mDistanceToFacedObject = (rayToObject.mRatio * maxDistance) - camDist;
else
@ -1904,14 +1897,7 @@ namespace MWWorld
res.mHitNormal = rayRes.mHitNormalWorld;
res.mHitObject = rayRes.mHitObject;
if (res.mHitObject.isEmpty() && rayRes.mHitRefnum.isSet())
{
for (CellStore* cellstore : mWorldScene->getActiveCells())
{
res.mHitObject = cellstore->searchViaRefNum(rayRes.mHitRefnum);
if (!res.mHitObject.isEmpty())
break;
}
}
res.mHitObject = MWBase::Environment::get().getWorldModel()->getPtr(rayRes.mHitRefnum);
return res.mHit;
}

Loading…
Cancel
Save