mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-20 06:23:52 +00:00
handle equipped items when serialising inventory state
This commit is contained in:
parent
bcc5894e2d
commit
d2ec3ffdc8
4 changed files with 71 additions and 21 deletions
|
@ -60,43 +60,53 @@ namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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))
|
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().
|
const T *record = MWBase::Environment::get().getWorld()->getStore().
|
||||||
get<T>().search (state.mRef.mRefID);
|
get<T>().search (state.mRef.mRefID);
|
||||||
|
|
||||||
if (!record)
|
if (!record)
|
||||||
return;
|
return ContainerStoreIterator (this);
|
||||||
|
|
||||||
LiveCellRef<T> ref (record);
|
LiveCellRef<T> ref (record);
|
||||||
ref.load (state);
|
ref.load (state);
|
||||||
ref.mRef.mRefNum.mContentFile = -1;
|
ref.mRef.mRefNum.mContentFile = -1;
|
||||||
collection.mList.push_back (ref);
|
collection.mList.push_back (ref);
|
||||||
|
|
||||||
|
return ContainerStoreIterator (this, --collection.mList.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void MWWorld::ContainerStore::storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state)
|
void MWWorld::ContainerStore::storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const
|
||||||
const
|
|
||||||
{
|
{
|
||||||
ref.save (state);
|
ref.save (state);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void MWWorld::ContainerStore::storeStates (const CellRefList<T>& collection,
|
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());
|
for (typename CellRefList<T>::List::const_iterator iter (collection.mList.begin());
|
||||||
iter!=collection.mList.end(); ++iter)
|
iter!=collection.mList.end(); ++iter)
|
||||||
{
|
{
|
||||||
ESM::ObjectState state;
|
ESM::ObjectState state;
|
||||||
storeState (*iter, 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";
|
const std::string MWWorld::ContainerStore::sGoldId = "gold_001";
|
||||||
|
|
||||||
MWWorld::ContainerStore::ContainerStore() : mCachedWeight (0), mWeightUpToDate (false) {}
|
MWWorld::ContainerStore::ContainerStore() : mCachedWeight (0), mWeightUpToDate (false) {}
|
||||||
|
@ -541,15 +551,15 @@ void MWWorld::ContainerStore::writeState (ESM::InventoryState& state) const
|
||||||
|
|
||||||
storeStates (potions, state.mItems);
|
storeStates (potions, state.mItems);
|
||||||
storeStates (appas, state.mItems);
|
storeStates (appas, state.mItems);
|
||||||
storeStates (armors, state.mItems);
|
storeStates (armors, state.mItems, true);
|
||||||
storeStates (books, state.mItems);
|
storeStates (books, state.mItems);
|
||||||
storeStates (clothes, state.mItems);
|
storeStates (clothes, state.mItems, true);
|
||||||
storeStates (ingreds, state.mItems);
|
storeStates (ingreds, state.mItems);
|
||||||
storeStates (lockpicks, state.mItems);
|
storeStates (lockpicks, state.mItems, true);
|
||||||
storeStates (miscItems, state.mItems);
|
storeStates (miscItems, state.mItems);
|
||||||
storeStates (probes, state.mItems);
|
storeStates (probes, state.mItems, true);
|
||||||
storeStates (repairs, state.mItems);
|
storeStates (repairs, state.mItems);
|
||||||
storeStates (weapons, state.mItems);
|
storeStates (weapons, state.mItems, true);
|
||||||
|
|
||||||
state.mLights.clear();
|
state.mLights.clear();
|
||||||
|
|
||||||
|
@ -558,7 +568,7 @@ void MWWorld::ContainerStore::writeState (ESM::InventoryState& state) const
|
||||||
{
|
{
|
||||||
ESM::LightState objectState;
|
ESM::LightState objectState;
|
||||||
storeState (*iter, 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
|
for (std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >::const_iterator
|
||||||
iter (state.mItems.begin()); iter!=state.mItems.end(); ++iter)
|
iter (state.mItems.begin()); iter!=state.mItems.end(); ++iter)
|
||||||
{
|
{
|
||||||
|
int slot = iter->second.second;
|
||||||
|
|
||||||
switch (iter->second.first)
|
switch (iter->second.first)
|
||||||
{
|
{
|
||||||
case ESM::REC_ALCH: getState (potions, iter->first); break;
|
case ESM::REC_ALCH: getState (potions, iter->first); break;
|
||||||
case ESM::REC_APPA: getState (appas, 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_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_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_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_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:
|
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);
|
void addInitialItem (const std::string& id, const std::string& owner, const std::string& faction, int count, bool topLevel=true);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void getState (CellRefList<T>& collection, const ESM::ObjectState& state);
|
ContainerStoreIterator getState (CellRefList<T>& collection,
|
||||||
|
const ESM::ObjectState& state);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const;
|
void storeState (const LiveCellRef<T>& ref, ESM::ObjectState& state) const;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void storeStates (const CellRefList<T>& collection,
|
void storeStates (const CellRefList<T>& collection,
|
||||||
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& states)
|
std::vector<std::pair<ESM::ObjectState, std::pair<unsigned int, int> > >& states,
|
||||||
const;
|
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:
|
public:
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,21 @@ void MWWorld::InventoryStore::initSlots (TSlots& slots_)
|
||||||
slots_.push_back (end());
|
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()
|
MWWorld::InventoryStore::InventoryStore()
|
||||||
: mSelectedEnchantItem(end())
|
: mSelectedEnchantItem(end())
|
||||||
, mUpdatesEnabled (true)
|
, mUpdatesEnabled (true)
|
||||||
|
@ -629,3 +644,10 @@ void MWWorld::InventoryStore::purgeEffect(short effectId)
|
||||||
{
|
{
|
||||||
mMagicEffects.add(MWMechanics::EffectKey(effectId), -mMagicEffects.get(MWMechanics::EffectKey(effectId)).mMagnitude);
|
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();
|
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:
|
public:
|
||||||
|
|
||||||
InventoryStore();
|
InventoryStore();
|
||||||
|
@ -185,6 +191,9 @@ namespace MWWorld
|
||||||
|
|
||||||
void purgeEffect (short effectId);
|
void purgeEffect (short effectId);
|
||||||
///< Remove a magic effect
|
///< Remove a magic effect
|
||||||
|
|
||||||
|
virtual void clear();
|
||||||
|
///< Empty container.
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue