mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-25 06:35:30 +00:00
handle equipped items when serialising inventory state
This commit is contained in:
parent
bcc5894e2d
commit
d2ec3ffdc8
@ -60,43 +60,53 @@ namespace
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void MWWorld::ContainerStore::getState (CellRefList<T>& collection, const ESM::ObjectState& state)
|
||||
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::getState (CellRefList<T>& collection,
|
||||
const ESM::ObjectState& state)
|
||||
{
|
||||
if (!LiveCellRef<T>::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<T>().search (state.mRef.mRefID);
|
||||
|
||||
if (!record)
|
||||
return;
|
||||
return ContainerStoreIterator (this);
|
||||
|
||||
LiveCellRef<T> ref (record);
|
||||
ref.load (state);
|
||||
ref.mRef.mRefNum.mContentFile = -1;
|
||||
collection.mList.push_back (ref);
|
||||
|
||||
return ContainerStoreIterator (this, --collection.mList.end());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void MWWorld::ContainerStore::storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state)
|
||||
const
|
||||
void MWWorld::ContainerStore::storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const
|
||||
{
|
||||
ref.save (state);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void MWWorld::ContainerStore::storeStates (const CellRefList<T>& collection,
|
||||
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& states) const
|
||||
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& states, bool equipable) const
|
||||
{
|
||||
for (typename CellRefList<T>::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<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >::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:
|
||||
|
||||
|
@ -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<typename T>
|
||||
void getState (CellRefList<T>& collection, const ESM::ObjectState& state);
|
||||
ContainerStoreIterator getState (CellRefList<T>& collection,
|
||||
const ESM::ObjectState& state);
|
||||
|
||||
template<typename T>
|
||||
void storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const;
|
||||
|
||||
template<typename T>
|
||||
void storeStates (const CellRefList<T>& collection,
|
||||
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& states)
|
||||
const;
|
||||
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& 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:
|
||||
|
||||
|
@ -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<static_cast<int> (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<Slots)
|
||||
mSlots[slot] = iter;
|
||||
}
|
||||
|
||||
MWWorld::InventoryStore::InventoryStore()
|
||||
: mSelectedEnchantItem(end())
|
||||
, mUpdatesEnabled (true)
|
||||
@ -629,3 +644,10 @@ void MWWorld::InventoryStore::purgeEffect(short effectId)
|
||||
{
|
||||
mMagicEffects.add(MWMechanics::EffectKey(effectId), -mMagicEffects.get(MWMechanics::EffectKey(effectId)).mMagnitude);
|
||||
}
|
||||
|
||||
void MWWorld::InventoryStore::clear()
|
||||
{
|
||||
mSlots.clear();
|
||||
initSlots (mSlots);
|
||||
ContainerStore::clear();
|
||||
}
|
||||
|
@ -105,6 +105,12 @@ namespace MWWorld
|
||||
|
||||
void fireEquipmentChangedEvent();
|
||||
|
||||
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:
|
||||
|
||||
InventoryStore();
|
||||
@ -185,6 +191,9 @@ namespace MWWorld
|
||||
|
||||
void purgeEffect (short effectId);
|
||||
///< Remove a magic effect
|
||||
|
||||
virtual void clear();
|
||||
///< Empty container.
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user