diff --git a/apps/opencs/model/world/refcollection.cpp b/apps/opencs/model/world/refcollection.cpp index 124e697de8..5d2ed2cfc5 100644 --- a/apps/opencs/model/world/refcollection.cpp +++ b/apps/opencs/model/world/refcollection.cpp @@ -175,6 +175,9 @@ void CSMWorld::RefCollection::load(ESM::ESMReader& reader, int cellIndex, bool b ref.mIdNum = mNextId; // FIXME: fragile ref.mId = ESM::RefId::stringRefId(getNewId()); + if (!base && ref.mRefNum.mIndex >= mHighestUsedRefNum) + mHighestUsedRefNum = ref.mRefNum.mIndex; + cache.emplace(ref.mRefNum, ref.mIdNum); auto record = std::make_unique>(); @@ -222,6 +225,11 @@ std::string CSMWorld::RefCollection::getNewId() return "ref#" + std::to_string(mNextId++); } +uint32_t CSMWorld::RefCollection::getNextRefNum() +{ + return ++mHighestUsedRefNum; +} + unsigned int CSMWorld::RefCollection::extractIdNum(std::string_view id) const { std::string::size_type separator = id.find_last_of('#'); @@ -283,6 +291,7 @@ void CSMWorld::RefCollection::appendBlankRecord(const ESM::RefId& id, UniversalI record->get().mId = id; record->get().mIdNum = extractIdNum(id.getRefIdString()); + record->get().mRefNum.mIndex = getNextRefNum(); Collection::appendRecord(std::move(record)); } @@ -298,15 +307,13 @@ void CSMWorld::RefCollection::cloneRecord( copy->get().mId = destination; copy->get().mIdNum = extractIdNum(destination.getRefIdString()); + copy->get().mRefNum.mIndex = getNextRefNum(); if (copy->get().mRefNum.hasContentFile()) { mRefIndex.insert(std::make_pair(static_cast*>(copy.get())->get().mIdNum, index)); copy->get().mRefNum.mContentFile = -1; - copy->get().mRefNum.mIndex = index; } - else - copy->get().mRefNum.mIndex = copy->get().mIdNum; insertRecord(std::move(copy), getAppendIndex(destination, type)); // call RefCollection::insertRecord() } diff --git a/apps/opencs/model/world/refcollection.hpp b/apps/opencs/model/world/refcollection.hpp index d3d200e6c2..f70167f922 100644 --- a/apps/opencs/model/world/refcollection.hpp +++ b/apps/opencs/model/world/refcollection.hpp @@ -1,6 +1,7 @@ #ifndef CSM_WOLRD_REFCOLLECTION_H #define CSM_WOLRD_REFCOLLECTION_H +#include #include #include #include @@ -40,9 +41,12 @@ namespace CSMWorld std::map mRefIndex; // CellRef index keyed by CSMWorld::CellRef::mIdNum int mNextId; + uint32_t mHighestUsedRefNum = 0; unsigned int extractIdNum(std::string_view id) const; + uint32_t getNextRefNum(); + int getIntIndex(unsigned int id) const; int searchId(unsigned int id) const;