1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-24 17:23:53 +00:00
openmw/components/esm3/cellid.cpp

110 lines
2.6 KiB
C++
Raw Normal View History

#include "cellid.hpp"
#include "esmreader.hpp"
#include "esmwriter.hpp"
#include <components/misc/algorithm.hpp>
namespace ESM
{
2022-09-22 18:26:05 +00:00
void CellId::load(ESMReader& esm)
{
2023-01-19 16:31:45 +00:00
mWorldspace = esm.getHNString("SPAC");
2022-09-22 18:26:05 +00:00
if (esm.isNextSub("CIDX"))
{
esm.getHT(mIndex.mX, mIndex.mY);
2022-09-22 18:26:05 +00:00
mPaged = true;
}
else
2023-04-02 17:58:27 +00:00
{
2022-09-22 18:26:05 +00:00
mPaged = false;
2023-04-02 17:58:27 +00:00
mIndex.mX = 0;
mIndex.mY = 0;
}
2022-09-22 18:26:05 +00:00
}
2022-09-22 18:26:05 +00:00
void CellId::save(ESMWriter& esm) const
{
2023-01-19 16:31:45 +00:00
esm.writeHNString("SPAC", mWorldspace);
2014-02-23 20:39:18 +00:00
2022-09-22 18:26:05 +00:00
if (mPaged)
esm.writeHNT("CIDX", mIndex, 8);
}
2014-02-23 20:39:18 +00:00
struct VisitCellRefId
{
CellId operator()(const ESM::EmptyRefId)
{
CellId out;
out.mPaged = true;
out.mIndex = {};
return out;
}
CellId operator()(const ESM::StringRefId& id)
{
CellId out;
out.mPaged = false;
out.mWorldspace = id.getValue();
out.mIndex = { 0, 0 };
return out;
}
CellId operator()(const ESM::ESM3ExteriorCellRefId& id)
{
CellId out;
out.mPaged = true;
out.mIndex = { id.getX(), id.getY() };
return out;
}
template <typename T>
CellId operator()(const T& id)
{
throw std::runtime_error("cannot extract CellId from this Id type");
}
};
CellId CellId::extractFromRefId(const ESM::RefId& id)
{
// This is bad and that code should not be merged.
return visit(VisitCellRefId(), id);
}
2022-09-22 18:26:05 +00:00
bool operator==(const CellId& left, const CellId& right)
{
return left.mWorldspace == right.mWorldspace && left.mPaged == right.mPaged
&& (!left.mPaged || (left.mIndex.mX == right.mIndex.mX && left.mIndex.mY == right.mIndex.mY));
}
2022-09-22 18:26:05 +00:00
bool operator!=(const CellId& left, const CellId& right)
{
return !(left == right);
}
2022-09-22 18:26:05 +00:00
bool operator<(const CellId& left, const CellId& right)
{
2022-09-22 18:26:05 +00:00
if (left.mPaged < right.mPaged)
return true;
2022-09-22 18:26:05 +00:00
if (left.mPaged > right.mPaged)
return false;
2022-09-22 18:26:05 +00:00
if (left.mPaged)
{
if (left.mIndex.mX < right.mIndex.mX)
return true;
if (left.mIndex.mX > right.mIndex.mX)
return false;
2022-09-22 18:26:05 +00:00
if (left.mIndex.mY < right.mIndex.mY)
return true;
if (left.mIndex.mY > right.mIndex.mY)
return false;
}
return left.mWorldspace < right.mWorldspace;
}
}