diff --git a/apps/openmw/mwworld/cellstore.cpp b/apps/openmw/mwworld/cellstore.cpp index cc614133aa..73f69e2206 100644 --- a/apps/openmw/mwworld/cellstore.cpp +++ b/apps/openmw/mwworld/cellstore.cpp @@ -104,6 +104,45 @@ namespace } } + template + void fixRestockingImpl(const T* base, RecordType& state) + { + // Workaround for old saves not containing negative quantities + for(const auto& baseItem : base->mInventory.mList) + { + if(baseItem.mCount < 0) + { + for(auto& item : state.mInventory.mItems) + { + if(item.mCount > 0 && Misc::StringUtils::ciEqual(baseItem.mItem, item.mRef.mRefID)) + item.mCount = -item.mCount; + } + } + } + } + + template + void fixRestocking(const T* base, RecordType& state) + {} + + template<> + void fixRestocking<>(const ESM::Creature* base, ESM::CreatureState& state) + { + fixRestockingImpl(base, state); + } + + template<> + void fixRestocking<>(const ESM::NPC* base, ESM::NpcState& state) + { + fixRestockingImpl(base, state); + } + + template<> + void fixRestocking<>(const ESM::Container* base, ESM::ContainerState& state) + { + fixRestockingImpl(base, state); + } + template void readReferenceCollection (ESM::ESMReader& reader, MWWorld::CellRefList& collection, const ESM::CellRef& cref, const std::map& contentFileMap, MWWorld::CellStore* cellstore) @@ -134,6 +173,9 @@ namespace if (!record) return; + if (state.mVersion < 15) + fixRestocking(record, state); + if (state.mRef.mRefNum.hasContentFile()) { for (typename MWWorld::CellRefList::List::iterator iter (collection.mList.begin());