openmw/components/esm3/cellid.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.3 KiB
C++
Raw Normal View History

#include "cellid.hpp"
#include "esmreader.hpp"
#include "esmwriter.hpp"
#include <components/misc/algorithm.hpp>
namespace ESM
{
void CellId::load(ESMReader& esm)
{
2023-01-19 17:31:45 +01:00
mWorldspace = esm.getHNString("SPAC");
if (esm.isNextSub("CIDX"))
{
esm.getHTSized<8>(mIndex);
mPaged = true;
2022-09-22 21:26:05 +03:00
}
else
2023-04-02 19:58:27 +02:00
{
mPaged = false;
2023-04-02 19:58:27 +02:00
mIndex.mX = 0;
mIndex.mY = 0;
}
2022-09-22 21:26:05 +03:00
}
void CellId::save(ESMWriter& esm) const
2022-09-22 21:26:05 +03:00
{
2023-01-19 17:31:45 +01:00
esm.writeHNString("SPAC", mWorldspace);
2014-02-23 21:39:18 +01:00
if (mPaged)
esm.writeHNT("CIDX", mIndex, 8);
}
CellId CellId::extractFromRefId(const ESM::RefId& id)
{
// This is bad and that code should not be merged.
const std::string& idString = id.getRefIdString();
CellId out;
if (idString[0] == '#' && idString.find(',')) // That is an exterior cell Id
{
int x, y;
std::stringstream stringStream = std::stringstream(idString);
char sharp = '#';
char comma = ',';
stringStream >> sharp >> x >> comma >> y;
out.mPaged = true;
out.mIndex = { x, y };
}
else
{
out.mPaged = false;
out.mWorldspace = Misc::StringUtils::lowerCase(idString);
}
return out;
}
bool operator==(const CellId& left, const CellId& right)
2014-02-23 21:39:18 +01:00
{
return left.mWorldspace == right.mWorldspace && left.mPaged == right.mPaged
&& (!left.mPaged || (left.mIndex.mX == right.mIndex.mX && left.mIndex.mY == right.mIndex.mY));
}
bool operator!=(const CellId& left, const CellId& right)
{
return !(left == right);
2022-09-22 21:26:05 +03:00
}
bool operator<(const CellId& left, const CellId& right)
{
if (left.mPaged < right.mPaged)
return true;
if (left.mPaged > right.mPaged)
return false;
if (left.mPaged)
2022-09-22 21:26:05 +03:00
{
if (left.mIndex.mX < right.mIndex.mX)
return true;
if (left.mIndex.mX > right.mIndex.mX)
return false;
2014-02-23 21:39:18 +01:00
if (left.mIndex.mY < right.mIndex.mY)
return true;
2014-02-23 21:39:18 +01:00
if (left.mIndex.mY > right.mIndex.mY)
return false;
2022-09-22 21:26:05 +03:00
}
return left.mWorldspace < right.mWorldspace;
}
}