diff --git a/apps/essimporter/convertinventory.cpp b/apps/essimporter/convertinventory.cpp index 2f03cfaf41..69a2ea4120 100644 --- a/apps/essimporter/convertinventory.cpp +++ b/apps/essimporter/convertinventory.cpp @@ -9,15 +9,14 @@ namespace ESSImport void convertInventory(const Inventory& inventory, ESM::InventoryState& state) { - int index = 0; + uint32_t index = 0; for (const auto& item : inventory.mItems) { ESM::ObjectState objstate; objstate.blank(); objstate.mRef = item; objstate.mRef.mRefID = ESM::RefId::stringRefId(item.mId); - objstate.mCount = std::abs(item.mCount); // restocking items have negative count in the savefile - // openmw handles them differently, so no need to set any flags + objstate.mCount = item.mCount; state.mItems.push_back(objstate); if (item.mRelativeEquipmentSlot != -1) // Note we should really write the absolute slot here, which we do not know about diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index d82125a5ce..b55a524a48 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -111,12 +111,12 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::getState( } void MWWorld::ContainerStore::storeEquipmentState( - const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const + const MWWorld::LiveCellRefBase& ref, size_t index, ESM::InventoryState& inventory) const { } void MWWorld::ContainerStore::readEquipmentState( - const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory) + const MWWorld::ContainerStoreIterator& iter, size_t index, const ESM::InventoryState& inventory) { } @@ -128,7 +128,7 @@ void MWWorld::ContainerStore::storeState(const LiveCellRef& ref, ESM::ObjectS template void MWWorld::ContainerStore::storeStates( - const CellRefList& collection, ESM::InventoryState& inventory, int& index, bool equipable) const + const CellRefList& collection, ESM::InventoryState& inventory, size_t& index, bool equipable) const { for (const LiveCellRef& liveCellRef : collection.mList) { @@ -926,7 +926,7 @@ void MWWorld::ContainerStore::writeState(ESM::InventoryState& state) const { state.mItems.clear(); - int index = 0; + size_t index = 0; storeStates(potions, state, index); storeStates(appas, state, index); storeStates(armors, state, index, true); @@ -947,12 +947,12 @@ void MWWorld::ContainerStore::readState(const ESM::InventoryState& inventory) mModified = true; mResolved = true; - int index = 0; + size_t index = 0; for (const ESM::ObjectState& state : inventory.mItems) { int type = MWBase::Environment::get().getESMStore()->find(state.mRef.mRefID); - int thisIndex = index++; + size_t thisIndex = index++; switch (type) { diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 889fcf7463..fb2722dde8 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -161,16 +161,16 @@ namespace MWWorld void storeState(const LiveCellRef& ref, ESM::ObjectState& state) const; template - void storeStates( - const CellRefList& collection, ESM::InventoryState& inventory, int& index, bool equipable = false) const; + void storeStates(const CellRefList& collection, ESM::InventoryState& inventory, size_t& index, + bool equipable = false) const; void updateRechargingItems(); virtual void storeEquipmentState( - const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const; + const MWWorld::LiveCellRefBase& ref, size_t index, ESM::InventoryState& inventory) const; virtual void readEquipmentState( - const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory); + const MWWorld::ContainerStoreIterator& iter, size_t index, const ESM::InventoryState& inventory); public: ContainerStore(); diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index 9b3470a835..095f5d3cc1 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -46,32 +46,32 @@ void MWWorld::InventoryStore::initSlots(TSlots& slots_) } void MWWorld::InventoryStore::storeEquipmentState( - const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const + const MWWorld::LiveCellRefBase& ref, size_t index, ESM::InventoryState& inventory) const { - for (int i = 0; i < static_cast(mSlots.size()); ++i) + for (int32_t i = 0; i < MWWorld::InventoryStore::Slots; ++i) + { if (mSlots[i].getType() != -1 && mSlots[i]->getBase() == &ref) - { - inventory.mEquipmentSlots[index] = i; - } + inventory.mEquipmentSlots[static_cast(index)] = i; + } if (mSelectedEnchantItem.getType() != -1 && mSelectedEnchantItem->getBase() == &ref) inventory.mSelectedEnchantItem = index; } void MWWorld::InventoryStore::readEquipmentState( - const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory) + const MWWorld::ContainerStoreIterator& iter, size_t index, const ESM::InventoryState& inventory) { if (index == inventory.mSelectedEnchantItem) mSelectedEnchantItem = iter; - std::map::const_iterator found = inventory.mEquipmentSlots.find(index); + auto found = inventory.mEquipmentSlots.find(index); if (found != inventory.mEquipmentSlots.end()) { if (found->second < 0 || found->second >= MWWorld::InventoryStore::Slots) throw std::runtime_error("Invalid slot index in inventory state"); // make sure the item can actually be equipped in this slot - int slot = found->second; + int32_t slot = found->second; std::pair, bool> allowedSlots = iter->getClass().getEquipmentSlots(*iter); if (!allowedSlots.first.size()) return; diff --git a/apps/openmw/mwworld/inventorystore.hpp b/apps/openmw/mwworld/inventorystore.hpp index 6df5fa1e5a..0af6ee2b28 100644 --- a/apps/openmw/mwworld/inventorystore.hpp +++ b/apps/openmw/mwworld/inventorystore.hpp @@ -81,9 +81,9 @@ namespace MWWorld void fireEquipmentChangedEvent(); void storeEquipmentState( - const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const override; + const MWWorld::LiveCellRefBase& ref, size_t index, ESM::InventoryState& inventory) const override; void readEquipmentState( - const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory) override; + const MWWorld::ContainerStoreIterator& iter, size_t index, const ESM::InventoryState& inventory) override; ContainerStoreIterator findSlot(int slot) const; diff --git a/components/esm3/inventorystate.cpp b/components/esm3/inventorystate.cpp index e58d0335bf..c3af101edc 100644 --- a/components/esm3/inventorystate.cpp +++ b/components/esm3/inventorystate.cpp @@ -7,22 +7,25 @@ namespace ESM { + namespace + { + constexpr uint32_t sInvalidSlot = -1; + } void InventoryState::load(ESMReader& esm) { // obsolete - int index = 0; + uint32_t index = 0; while (esm.isNextSub("IOBJ")) { - int unused; // no longer used - esm.getHT(unused); + esm.skip(4); ObjectState state; // obsolete if (esm.isNextSub("SLOT")) { - int slot; + int32_t slot; esm.getHT(slot); mEquipmentSlots[index] = slot; } @@ -38,9 +41,9 @@ namespace ESM ++index; } - int itemsCount = 0; + uint32_t itemsCount = 0; esm.getHNOT(itemsCount, "ICNT"); - for (int i = 0; i < itemsCount; i++) + for (; itemsCount > 0; --itemsCount) { ObjectState state; @@ -62,7 +65,7 @@ namespace ESM { // Get its name ESM::RefId id = esm.getRefId(); - int count; + int32_t count; std::string parentGroup; // Then get its count esm.getHNT(count, "COUN"); @@ -91,9 +94,9 @@ namespace ESM while (esm.isNextSub("EQUI")) { esm.getSubHeader(); - int equipIndex; + int32_t equipIndex; esm.getT(equipIndex); - int slot; + int32_t slot; esm.getT(slot); mEquipmentSlots[equipIndex] = slot; } @@ -101,20 +104,24 @@ namespace ESM if (esm.isNextSub("EQIP")) { esm.getSubHeader(); - int slotsCount = 0; + uint32_t slotsCount = 0; esm.getT(slotsCount); - for (int i = 0; i < slotsCount; i++) + for (; slotsCount > 0; --slotsCount) { - int equipIndex; + int32_t equipIndex; esm.getT(equipIndex); - int slot; + int32_t slot; esm.getT(slot); mEquipmentSlots[equipIndex] = slot; } } - mSelectedEnchantItem = -1; - esm.getHNOT(mSelectedEnchantItem, "SELE"); + uint32_t selectedEnchantItem = sInvalidSlot; + esm.getHNOT(selectedEnchantItem, "SELE"); + if (selectedEnchantItem == sInvalidSlot) + mSelectedEnchantItem.reset(); + else + mSelectedEnchantItem = selectedEnchantItem; // Old saves had restocking levelled items in a special map // This turns items from that map into negative quantities @@ -132,7 +139,7 @@ namespace ESM void InventoryState::save(ESMWriter& esm) const { - int itemsCount = static_cast(mItems.size()); + uint32_t itemsCount = static_cast(mItems.size()); if (itemsCount > 0) { esm.writeHNT("ICNT", itemsCount); @@ -149,34 +156,32 @@ namespace ESM esm.writeHNString("LGRP", it->first.second); } - for (TEffectMagnitudes::const_iterator it = mPermanentMagicEffectMagnitudes.begin(); - it != mPermanentMagicEffectMagnitudes.end(); ++it) + for (const auto& [id, params] : mPermanentMagicEffectMagnitudes) { - esm.writeHNRefId("MAGI", it->first); + esm.writeHNRefId("MAGI", id); - const std::vector>& params = it->second; - for (std::vector>::const_iterator pIt = params.begin(); pIt != params.end(); ++pIt) + for (const auto& [rand, mult] : params) { - esm.writeHNT("RAND", pIt->first); - esm.writeHNT("MULT", pIt->second); + esm.writeHNT("RAND", rand); + esm.writeHNT("MULT", mult); } } - int slotsCount = static_cast(mEquipmentSlots.size()); + uint32_t slotsCount = static_cast(mEquipmentSlots.size()); if (slotsCount > 0) { esm.startSubRecord("EQIP"); esm.writeT(slotsCount); - for (std::map::const_iterator it = mEquipmentSlots.begin(); it != mEquipmentSlots.end(); ++it) + for (const auto& [index, slot] : mEquipmentSlots) { - esm.writeT(it->first); - esm.writeT(it->second); + esm.writeT(index); + esm.writeT(slot); } esm.endRecord("EQIP"); } - if (mSelectedEnchantItem != -1) - esm.writeHNT("SELE", mSelectedEnchantItem); + if (mSelectedEnchantItem) + esm.writeHNT("SELE", *mSelectedEnchantItem); } } diff --git a/components/esm3/inventorystate.hpp b/components/esm3/inventorystate.hpp index a0fd948951..050d1eb92f 100644 --- a/components/esm3/inventorystate.hpp +++ b/components/esm3/inventorystate.hpp @@ -2,6 +2,7 @@ #define OPENMW_ESM_INVENTORYSTATE_H #include +#include #include "objectstate.hpp" #include @@ -19,20 +20,15 @@ namespace ESM std::vector mItems; // - std::map mEquipmentSlots; + std::map mEquipmentSlots; - std::map, int> mLevelledItemMap; + std::map, int32_t> mLevelledItemMap; - typedef std::map>> TEffectMagnitudes; - TEffectMagnitudes mPermanentMagicEffectMagnitudes; + std::map>> mPermanentMagicEffectMagnitudes; - int mSelectedEnchantItem; // For inventories only + std::optional mSelectedEnchantItem; // For inventories only - InventoryState() - : mSelectedEnchantItem(-1) - { - } - virtual ~InventoryState() {} + virtual ~InventoryState() = default; virtual void load(ESMReader& esm); virtual void save(ESMWriter& esm) const;