2014-01-14 11:25:35 +00:00
|
|
|
#include "cellid.hpp"
|
|
|
|
|
|
|
|
#include "esmreader.hpp"
|
|
|
|
#include "esmwriter.hpp"
|
2023-02-19 20:18:15 +00:00
|
|
|
#include <components/misc/algorithm.hpp>
|
2024-02-27 19:47:46 +00:00
|
|
|
#include <components/misc/concepts.hpp>
|
2014-01-14 11:25:35 +00:00
|
|
|
|
2022-04-11 22:18:39 +00:00
|
|
|
namespace ESM
|
|
|
|
{
|
2024-02-27 19:47:46 +00:00
|
|
|
template <Misc::SameAsWithoutCvref<CellId::CellIndex> T>
|
|
|
|
void decompose(T&& v, const auto& f)
|
|
|
|
{
|
|
|
|
f(v.mX, v.mY);
|
|
|
|
}
|
2022-04-11 22:18:39 +00:00
|
|
|
|
|
|
|
void CellId::load(ESMReader& esm)
|
2014-01-14 11:25:35 +00:00
|
|
|
{
|
2023-01-19 16:31:45 +00:00
|
|
|
mWorldspace = esm.getHNString("SPAC");
|
2014-01-14 11:25:35 +00:00
|
|
|
|
2023-12-20 11:28:34 +00:00
|
|
|
mIndex.mX = 0;
|
|
|
|
mIndex.mY = 0;
|
2024-02-27 19:47:46 +00:00
|
|
|
mPaged = esm.getOptionalComposite("CIDX", mIndex);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2014-01-14 11:25:35 +00:00
|
|
|
|
|
|
|
void CellId::save(ESMWriter& esm) const
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2023-01-19 16:31:45 +00:00
|
|
|
esm.writeHNString("SPAC", mWorldspace);
|
2014-02-23 20:39:18 +00:00
|
|
|
|
|
|
|
if (mPaged)
|
2024-02-27 19:47:46 +00:00
|
|
|
esm.writeNamedComposite("CIDX", mIndex);
|
2014-02-23 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 13:01:52 +00:00
|
|
|
struct VisitCellRefId
|
2023-02-19 20:18:15 +00:00
|
|
|
{
|
2023-03-25 16:45:49 +00:00
|
|
|
CellId operator()(const ESM::EmptyRefId)
|
|
|
|
{
|
|
|
|
CellId out;
|
|
|
|
out.mPaged = true;
|
|
|
|
out.mIndex = {};
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:01:52 +00:00
|
|
|
CellId operator()(const ESM::StringRefId& id)
|
|
|
|
{
|
|
|
|
CellId out;
|
|
|
|
out.mPaged = false;
|
|
|
|
out.mWorldspace = id.getValue();
|
|
|
|
out.mIndex = { 0, 0 };
|
|
|
|
return out;
|
|
|
|
}
|
2023-04-01 09:31:05 +00:00
|
|
|
CellId operator()(const ESM::ESM3ExteriorCellRefId& id)
|
2023-02-19 20:18:15 +00:00
|
|
|
{
|
2023-03-22 13:01:52 +00:00
|
|
|
CellId out;
|
2023-02-19 20:18:15 +00:00
|
|
|
out.mPaged = true;
|
2023-04-01 09:31:05 +00:00
|
|
|
out.mIndex = { id.getX(), id.getY() };
|
2023-03-22 13:01:52 +00:00
|
|
|
return out;
|
2023-02-19 20:18:15 +00:00
|
|
|
}
|
2023-03-22 13:01:52 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
CellId operator()(const T& id)
|
2023-02-19 20:18:15 +00:00
|
|
|
{
|
2023-03-22 13:01:52 +00:00
|
|
|
throw std::runtime_error("cannot extract CellId from this Id type");
|
2023-02-19 20:18:15 +00:00
|
|
|
}
|
2023-03-22 13:01:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CellId CellId::extractFromRefId(const ESM::RefId& id)
|
|
|
|
{
|
|
|
|
// This is bad and that code should not be merged.
|
2023-02-19 20:18:15 +00:00
|
|
|
|
2023-03-22 13:01:52 +00:00
|
|
|
return visit(VisitCellRefId(), id);
|
2023-02-19 20:18:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 22:18:39 +00:00
|
|
|
bool operator==(const CellId& left, const CellId& right)
|
2014-02-23 20:39:18 +00:00
|
|
|
{
|
|
|
|
return left.mWorldspace == right.mWorldspace && left.mPaged == right.mPaged
|
|
|
|
&& (!left.mPaged || (left.mIndex.mX == right.mIndex.mX && left.mIndex.mY == right.mIndex.mY));
|
|
|
|
}
|
2015-07-17 18:49:10 +00:00
|
|
|
|
2022-04-11 22:18:39 +00:00
|
|
|
bool operator!=(const CellId& left, const CellId& right)
|
2015-07-17 18:49:10 +00:00
|
|
|
{
|
|
|
|
return !(left == right);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2015-07-17 18:49:10 +00: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 18:26:05 +00:00
|
|
|
{
|
2015-07-17 18:49:10 +00:00
|
|
|
if (left.mIndex.mX < right.mIndex.mX)
|
|
|
|
return true;
|
|
|
|
if (left.mIndex.mX > right.mIndex.mX)
|
|
|
|
return false;
|
|
|
|
|
2014-02-23 20:39:18 +00:00
|
|
|
if (left.mIndex.mY < right.mIndex.mY)
|
2015-07-17 18:49:10 +00:00
|
|
|
return true;
|
2014-02-23 20:39:18 +00:00
|
|
|
if (left.mIndex.mY > right.mIndex.mY)
|
2015-07-17 18:49:10 +00:00
|
|
|
return false;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 18:49:10 +00:00
|
|
|
return left.mWorldspace < right.mWorldspace;
|
|
|
|
}
|
2022-04-11 22:18:39 +00:00
|
|
|
|
|
|
|
}
|