You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3mp/components/esm/inventorystate.cpp

106 lines
2.9 KiB
C++

#include "inventorystate.hpp"
#include "esmreader.hpp"
#include "esmwriter.hpp"
namespace
{
void read (ESM::ESMReader &esm, ESM::ObjectState& state, int& slot)
{
slot = -1;
esm.getHNOT (slot, "SLOT");
state.load (esm);
}
void write (ESM::ESMWriter &esm, const ESM::ObjectState& state, unsigned int type, int slot)
{
esm.writeHNT ("IOBJ", type);
if (slot!=-1)
esm.writeHNT ("SLOT", slot);
state.save (esm, true);
}
}
void ESM::InventoryState::load (ESMReader &esm)
{
while (esm.isNextSub ("IOBJ"))
{
unsigned int id = 0;
esm.getHT (id);
if (id==ESM::REC_LIGH)
{
LightState state;
int slot;
read (esm, state, slot);
if (state.mCount == 0)
continue;
mLights.push_back (std::make_pair (state, slot));
}
else
{
ObjectState state;
int slot;
read (esm, state, slot);
if (state.mCount == 0)
continue;
mItems.push_back (std::make_pair (state, std::make_pair (id, slot)));
}
}
while (esm.isNextSub("LEVM"))
{
std::string id = esm.getHString();
int count;
esm.getHNT (count, "COUN");
mLevelledItemMap[id] = count;
}
while (esm.isNextSub("MAGI"))
{
std::string id = esm.getHString();
std::vector<std::pair<float, float> > params;
while (esm.isNextSub("RAND"))
{
float rand, multiplier;
esm.getHT (rand);
esm.getHNT (multiplier, "MULT");
params.push_back(std::make_pair(rand, multiplier));
}
mPermanentMagicEffectMagnitudes[id] = params;
}
}
void ESM::InventoryState::save (ESMWriter &esm) const
{
for (std::vector<std::pair<ObjectState, std::pair<unsigned int, int> > >::const_iterator iter (mItems.begin()); iter!=mItems.end(); ++iter)
write (esm, iter->first, iter->second.first, iter->second.second);
for (std::vector<std::pair<LightState, int> >::const_iterator iter (mLights.begin());
iter!=mLights.end(); ++iter)
write (esm, iter->first, ESM::REC_LIGH, iter->second);
for (std::map<std::string, int>::const_iterator it = mLevelledItemMap.begin(); it != mLevelledItemMap.end(); ++it)
{
esm.writeHNString ("LEVM", it->first);
esm.writeHNT ("COUN", it->second);
}
for (TEffectMagnitudes::const_iterator it = mPermanentMagicEffectMagnitudes.begin(); it != mPermanentMagicEffectMagnitudes.end(); ++it)
{
esm.writeHNString("MAGI", it->first);
const std::vector<std::pair<float, float> >& params = it->second;
for (std::vector<std::pair<float, float> >::const_iterator pIt = params.begin(); pIt != params.end(); ++pIt)
{
esm.writeHNT ("RAND", pIt->first);
esm.writeHNT ("MULT", pIt->second);
}
}
}