From d2ec3ffdc89f55eb496eaf4623ad04a879637412 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 1 Feb 2014 17:07:08 +0100 Subject: [PATCH] handle equipped items when serialising inventory state --- apps/openmw/mwworld/containerstore.cpp | 48 ++++++++++++++++---------- apps/openmw/mwworld/containerstore.hpp | 13 +++++-- apps/openmw/mwworld/inventorystore.cpp | 22 ++++++++++++ apps/openmw/mwworld/inventorystore.hpp | 9 +++++ 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 7d12c7b6e..86cf3bbde 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -60,43 +60,53 @@ namespace } template -void MWWorld::ContainerStore::getState (CellRefList& collection, const ESM::ObjectState& state) +MWWorld::ContainerStoreIterator MWWorld::ContainerStore::getState (CellRefList& collection, + const ESM::ObjectState& state) { if (!LiveCellRef::checkState (state)) - return; // not valid anymore with current content files -> skip + return ContainerStoreIterator (this); // not valid anymore with current content files -> skip const T *record = MWBase::Environment::get().getWorld()->getStore(). get().search (state.mRef.mRefID); if (!record) - return; + return ContainerStoreIterator (this); LiveCellRef ref (record); ref.load (state); ref.mRef.mRefNum.mContentFile = -1; collection.mList.push_back (ref); + + return ContainerStoreIterator (this, --collection.mList.end()); } template -void MWWorld::ContainerStore::storeState (const LiveCellRef& ref, ESM::ObjectState& state) - const +void MWWorld::ContainerStore::storeState (const LiveCellRef& ref, ESM::ObjectState& state) const { ref.save (state); } template void MWWorld::ContainerStore::storeStates (const CellRefList& collection, - std::vector > >& states) const + std::vector > >& states, bool equipable) const { for (typename CellRefList::List::const_iterator iter (collection.mList.begin()); iter!=collection.mList.end(); ++iter) { ESM::ObjectState state; storeState (*iter, state); - states.push_back (std::make_pair (state, std::make_pair (T::sRecordId, -1))); + int slot = equipable ? getSlot (*iter) : -1; + states.push_back (std::make_pair (state, std::make_pair (T::sRecordId, slot))); } } +int MWWorld::ContainerStore::getSlot (const MWWorld::LiveCellRefBase& ref) const +{ + return -1; +} + +void MWWorld::ContainerStore::setSlot (const MWWorld::ContainerStoreIterator& iter, int slot) {} + const std::string MWWorld::ContainerStore::sGoldId = "gold_001"; MWWorld::ContainerStore::ContainerStore() : mCachedWeight (0), mWeightUpToDate (false) {} @@ -541,15 +551,15 @@ void MWWorld::ContainerStore::writeState (ESM::InventoryState& state) const storeStates (potions, state.mItems); storeStates (appas, state.mItems); - storeStates (armors, state.mItems); + storeStates (armors, state.mItems, true); storeStates (books, state.mItems); - storeStates (clothes, state.mItems); + storeStates (clothes, state.mItems, true); storeStates (ingreds, state.mItems); - storeStates (lockpicks, state.mItems); + storeStates (lockpicks, state.mItems, true); storeStates (miscItems, state.mItems); - storeStates (probes, state.mItems); + storeStates (probes, state.mItems, true); storeStates (repairs, state.mItems); - storeStates (weapons, state.mItems); + storeStates (weapons, state.mItems, true); state.mLights.clear(); @@ -558,7 +568,7 @@ void MWWorld::ContainerStore::writeState (ESM::InventoryState& state) const { ESM::LightState objectState; storeState (*iter, objectState); - state.mLights.push_back (std::make_pair (objectState, -1)); + state.mLights.push_back (std::make_pair (objectState, getSlot (*iter))); } } @@ -569,19 +579,21 @@ void MWWorld::ContainerStore::readState (const ESM::InventoryState& state) for (std::vector > >::const_iterator iter (state.mItems.begin()); iter!=state.mItems.end(); ++iter) { + int slot = iter->second.second; + switch (iter->second.first) { case ESM::REC_ALCH: getState (potions, iter->first); break; case ESM::REC_APPA: getState (appas, iter->first); break; - case ESM::REC_ARMO: getState (armors, iter->first); break; + case ESM::REC_ARMO: setSlot (getState (armors, iter->first), slot); break; case ESM::REC_BOOK: getState (books, iter->first); break; - case ESM::REC_CLOT: getState (clothes, iter->first); break; + case ESM::REC_CLOT: setSlot (getState (clothes, iter->first), slot); break; case ESM::REC_INGR: getState (ingreds, iter->first); break; - case ESM::REC_LOCK: getState (lockpicks, iter->first); break; + case ESM::REC_LOCK: setSlot (getState (lockpicks, iter->first), slot); break; case ESM::REC_MISC: getState (miscItems, iter->first); break; - case ESM::REC_PROB: getState (probes, iter->first); break; + case ESM::REC_PROB: setSlot (getState (probes, iter->first), slot); break; case ESM::REC_REPA: getState (repairs, iter->first); break; - case ESM::REC_WEAP: getState (weapons, iter->first); break; + case ESM::REC_WEAP: setSlot (getState (weapons, iter->first), slot); break; default: diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 5e305d408..2e01eb856 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -58,15 +58,22 @@ namespace MWWorld void addInitialItem (const std::string& id, const std::string& owner, const std::string& faction, int count, bool topLevel=true); template - void getState (CellRefList& collection, const ESM::ObjectState& state); + ContainerStoreIterator getState (CellRefList& collection, + const ESM::ObjectState& state); template void storeState (const LiveCellRef& ref, ESM::ObjectState& state) const; template void storeStates (const CellRefList& collection, - std::vector > >& states) - const; + std::vector > >& states, + bool equipable = false) const; + + virtual int getSlot (const MWWorld::LiveCellRefBase& ref) const; + ///< Return inventory slot that \a ref is in or -1 (if \a ref is not in a slot). + + virtual void setSlot (const MWWorld::ContainerStoreIterator& iter, int slot); + ///< Set slot for \a iter. Ignored if \a iter is an end iterator or if slot==-1. public: diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index 82b827e75..93573b401 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -42,6 +42,21 @@ void MWWorld::InventoryStore::initSlots (TSlots& slots_) slots_.push_back (end()); } +int MWWorld::InventoryStore::getSlot (const MWWorld::LiveCellRefBase& ref) const +{ + for (int i = 0; i (mSlots.size()); ++i) + if (mSlots[i].getType()!=-1 && mSlots[i]->getBase()==&ref) + return i; + + return -1; +} + +void MWWorld::InventoryStore::setSlot (const MWWorld::ContainerStoreIterator& iter, int slot) +{ + if (iter!=end() && slot>=0 && slot