2018-10-07 03:42:33 +00:00
|
|
|
#pragma once
|
2015-08-17 20:33:58 +00:00
|
|
|
|
2017-10-25 07:47:49 +00:00
|
|
|
#include <functional>
|
2019-09-28 02:53:03 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Runtime/GCNTypes.hpp"
|
|
|
|
#include "Runtime/IOStreams.hpp"
|
|
|
|
#include "Runtime/rstl.hpp"
|
|
|
|
|
|
|
|
#include <hecl/hecl.hpp>
|
|
|
|
#include <zeus/CMatrix3f.hpp>
|
|
|
|
#include <zeus/CMatrix4f.hpp>
|
|
|
|
#include <zeus/CTransform.hpp>
|
|
|
|
#include <zeus/CVector2f.hpp>
|
|
|
|
#include <zeus/CVector3f.hpp>
|
2015-08-17 20:33:58 +00:00
|
|
|
|
2017-12-30 01:09:45 +00:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
2017-11-24 08:23:28 +00:00
|
|
|
using namespace std::literals;
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2021-06-06 23:53:41 +00:00
|
|
|
using kUniqueIdType = u16;
|
|
|
|
static constexpr int kMaxEntities = 1024;
|
|
|
|
constexpr kUniqueIdType kUniqueIdSize = sizeof(u16);
|
|
|
|
constexpr kUniqueIdType kUniqueIdBits = kUniqueIdSize * 8;
|
|
|
|
constexpr kUniqueIdType kUniqueIdMax = UINT16_MAX;
|
|
|
|
constexpr kUniqueIdType kUniqueIdVersionMax = 64;
|
|
|
|
constexpr kUniqueIdType kUniqueIdVersionMask = kUniqueIdVersionMax - 1;
|
|
|
|
constexpr kUniqueIdType kUniqueIdValueMask = kMaxEntities - 1;
|
|
|
|
constexpr kUniqueIdType kUniqueIdValueBits = 10;
|
|
|
|
constexpr kUniqueIdType kUniqueIdVersionBits = 6;
|
2015-08-17 22:05:00 +00:00
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
using FourCC = hecl::FourCC;
|
2017-08-13 05:26:14 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
class CAssetId {
|
|
|
|
u64 id = UINT64_MAX;
|
|
|
|
|
2017-08-13 05:26:14 +00:00
|
|
|
public:
|
2019-09-30 12:58:32 +00:00
|
|
|
constexpr CAssetId() noexcept = default;
|
|
|
|
constexpr CAssetId(u64 v) noexcept { Assign(v); }
|
2018-12-08 05:30:43 +00:00
|
|
|
explicit CAssetId(CInputStream& in);
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] constexpr bool IsValid() const noexcept { return id != UINT64_MAX; }
|
|
|
|
[[nodiscard]] constexpr u64 Value() const noexcept { return id; }
|
2019-09-30 12:58:32 +00:00
|
|
|
constexpr void Assign(u64 v) noexcept { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); }
|
|
|
|
constexpr void Reset() noexcept { id = UINT64_MAX; }
|
2018-12-08 05:30:43 +00:00
|
|
|
void PutTo(COutputStream& out);
|
2020-03-22 08:44:43 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(CAssetId other) const noexcept { return id == other.id; }
|
|
|
|
[[nodiscard]] constexpr bool operator!=(CAssetId other) const noexcept { return !operator==(other); }
|
|
|
|
[[nodiscard]] constexpr bool operator<(CAssetId other) const noexcept { return id < other.id; }
|
2017-08-13 05:26:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//#define kInvalidAssetId CAssetId()
|
2015-08-23 06:42:29 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
struct SObjectTag {
|
|
|
|
FourCC type;
|
|
|
|
CAssetId id;
|
|
|
|
|
2019-09-30 13:00:15 +00:00
|
|
|
constexpr explicit operator bool() const noexcept { return id.IsValid(); }
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(const SObjectTag& other) const noexcept { return id == other.id; }
|
|
|
|
[[nodiscard]] constexpr bool operator!=(const SObjectTag& other) const noexcept { return !operator==(other); }
|
|
|
|
[[nodiscard]] constexpr bool operator<(const SObjectTag& other) const noexcept { return id < other.id; }
|
2019-09-30 12:58:32 +00:00
|
|
|
constexpr SObjectTag() noexcept = default;
|
|
|
|
constexpr SObjectTag(FourCC tp, CAssetId rid) noexcept : type(tp), id(rid) {}
|
2020-03-31 03:52:22 +00:00
|
|
|
explicit SObjectTag(CInputStream& in) {
|
2018-12-08 05:30:43 +00:00
|
|
|
in.readBytesToBuf(&type, 4);
|
|
|
|
id = CAssetId(in);
|
|
|
|
}
|
|
|
|
void readMLVL(CInputStream& in) {
|
|
|
|
id = CAssetId(in);
|
|
|
|
in.readBytesToBuf(&type, 4);
|
|
|
|
}
|
2015-08-22 01:58:41 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
struct TEditorId {
|
2020-05-08 23:39:19 +00:00
|
|
|
u32 id = UINT32_MAX;
|
2019-08-14 09:15:08 +00:00
|
|
|
|
2019-09-30 12:58:32 +00:00
|
|
|
constexpr TEditorId() noexcept = default;
|
|
|
|
constexpr TEditorId(u32 idin) noexcept : id(idin) {}
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] constexpr u8 LayerNum() const noexcept { return u8((id >> 26) & 0x3f); }
|
|
|
|
[[nodiscard]] constexpr u16 AreaNum() const noexcept { return u16((id >> 16) & 0x3ff); }
|
|
|
|
[[nodiscard]] constexpr u16 Id() const noexcept { return u16(id & 0xffff); }
|
2021-06-06 23:53:41 +00:00
|
|
|
[[nodiscard]] constexpr bool operator<(TEditorId other) const noexcept {
|
|
|
|
return (id & 0x3ffffff) < (other.id & 0x3ffffff);
|
|
|
|
}
|
2020-03-22 08:44:43 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(TEditorId other) const noexcept {
|
2019-09-30 12:58:32 +00:00
|
|
|
return (id & 0x3ffffff) == (other.id & 0x3ffffff);
|
|
|
|
}
|
2020-03-22 08:44:43 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(TEditorId other) const noexcept { return !operator==(other); }
|
2016-08-14 03:00:58 +00:00
|
|
|
};
|
2017-10-25 07:47:49 +00:00
|
|
|
|
2017-08-13 05:26:14 +00:00
|
|
|
#define kInvalidEditorId TEditorId()
|
2016-08-14 03:00:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
struct TUniqueId {
|
2021-06-06 23:53:41 +00:00
|
|
|
kUniqueIdType id = kUniqueIdMax;
|
2019-08-14 09:15:08 +00:00
|
|
|
|
2019-09-30 12:58:32 +00:00
|
|
|
constexpr TUniqueId() noexcept = default;
|
2021-06-06 23:53:41 +00:00
|
|
|
constexpr TUniqueId(kUniqueIdType value, kUniqueIdType version) noexcept
|
|
|
|
: id(value | (version << kUniqueIdValueBits)) {}
|
|
|
|
[[nodiscard]] constexpr kUniqueIdType Version() const noexcept {
|
|
|
|
return kUniqueIdType((id >> kUniqueIdValueBits) & kUniqueIdVersionMask);
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr kUniqueIdType Value() const noexcept { return kUniqueIdType(id & kUniqueIdValueMask); }
|
2020-03-22 08:44:43 +00:00
|
|
|
[[nodiscard]] constexpr bool operator<(TUniqueId other) const noexcept { return id < other.id; }
|
|
|
|
[[nodiscard]] constexpr bool operator==(TUniqueId other) const noexcept { return id == other.id; }
|
|
|
|
[[nodiscard]] constexpr bool operator!=(TUniqueId other) const noexcept { return !operator==(other); }
|
2017-08-10 13:40:07 +00:00
|
|
|
};
|
|
|
|
|
2017-08-13 05:26:14 +00:00
|
|
|
#define kInvalidUniqueId TUniqueId()
|
2021-06-07 00:07:45 +00:00
|
|
|
using EntityList = rstl::reserved_vector<TUniqueId, kMaxEntities>;
|
2017-08-13 05:26:14 +00:00
|
|
|
|
2017-01-15 03:59:37 +00:00
|
|
|
using TAreaId = s32;
|
|
|
|
|
2015-08-20 02:52:07 +00:00
|
|
|
#define kInvalidAreaId TAreaId(-1)
|
|
|
|
|
2016-09-10 04:50:00 +00:00
|
|
|
#if 0
|
|
|
|
template <class T, size_t N>
|
|
|
|
class TRoundRobin
|
|
|
|
{
|
|
|
|
rstl::reserved_vector<T, N> vals;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TRoundRobin(const T& val) : vals(N, val) {}
|
|
|
|
|
|
|
|
void PushBack(const T& val) { vals.push_back(val); }
|
|
|
|
|
|
|
|
size_t Size() const { return vals.size(); }
|
|
|
|
|
|
|
|
const T& GetLastValue() const { return vals.back(); }
|
|
|
|
|
|
|
|
void Clear() { vals.clear(); }
|
|
|
|
|
|
|
|
const T& GetValue(s32) const {}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <class T>
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] T GetAverage(const T* v, s32 count) noexcept {
|
2018-12-08 05:30:43 +00:00
|
|
|
T r = v[0];
|
|
|
|
for (s32 i = 1; i < count; ++i)
|
|
|
|
r += v[i];
|
2016-09-25 16:45:22 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return r / count;
|
2016-09-25 16:45:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <class T, size_t N>
|
|
|
|
class TReservedAverage : rstl::reserved_vector<T, N> {
|
2016-09-16 22:21:19 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
TReservedAverage() = default;
|
2016-09-16 22:21:19 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
TReservedAverage(const T& t) { rstl::reserved_vector<T, N>::resize(N, t); }
|
2016-09-25 16:45:22 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void AddValue(const T& t) {
|
|
|
|
if (this->size() < N) {
|
|
|
|
this->insert(this->begin(), t);
|
|
|
|
} else {
|
|
|
|
this->pop_back();
|
|
|
|
this->insert(this->begin(), t);
|
2016-09-16 22:21:19 +00:00
|
|
|
}
|
2021-06-06 23:53:41 +00:00
|
|
|
}
|
2016-09-16 22:21:19 +00:00
|
|
|
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] std::optional<T> GetAverage() const {
|
|
|
|
if (this->empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
return {metaforce::GetAverage<T>(this->data(), this->size())};
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-07-31 05:19:05 +00:00
|
|
|
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] std::optional<T> GetEntry(int i) const {
|
|
|
|
if (i >= this->size()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
return this->operator[](i);
|
|
|
|
}
|
2017-09-30 03:45:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void Clear() { this->clear(); }
|
|
|
|
|
2020-03-22 08:41:03 +00:00
|
|
|
[[nodiscard]] size_t Size() const { return this->size(); }
|
2016-09-25 16:45:22 +00:00
|
|
|
};
|
2016-09-16 22:21:19 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|
2017-10-25 07:47:49 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace std {
|
2016-09-10 04:50:00 +00:00
|
|
|
template <>
|
2021-04-10 08:42:06 +00:00
|
|
|
struct hash<metaforce::SObjectTag> {
|
|
|
|
size_t operator()(const metaforce::SObjectTag& tag) const noexcept { return tag.id.Value(); }
|
2017-08-13 05:26:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-04-10 08:42:06 +00:00
|
|
|
struct hash<metaforce::CAssetId> {
|
|
|
|
size_t operator()(const metaforce::CAssetId& id) const noexcept { return id.Value(); }
|
2015-08-23 06:42:29 +00:00
|
|
|
};
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace std
|
2019-04-07 05:14:48 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
FMT_CUSTOM_FORMATTER(metaforce::CAssetId, "{:08X}", obj.Value())
|
|
|
|
FMT_CUSTOM_FORMATTER(metaforce::TEditorId, "{:08X}", obj.id)
|
2021-06-06 23:53:41 +00:00
|
|
|
static_assert(sizeof(metaforce::kUniqueIdType) == sizeof(u16),
|
|
|
|
"TUniqueId size does not match expected size! Update TUniqueId format string!");
|
2021-04-10 08:42:06 +00:00
|
|
|
FMT_CUSTOM_FORMATTER(metaforce::TUniqueId, "{:04X}", obj.id)
|
|
|
|
FMT_CUSTOM_FORMATTER(metaforce::SObjectTag, "{} {}", obj.type, obj.id)
|
2019-07-28 01:21:31 +00:00
|
|
|
|
|
|
|
FMT_CUSTOM_FORMATTER(zeus::CVector3f, "({} {} {})", float(obj.x()), float(obj.y()), float(obj.z()))
|
|
|
|
FMT_CUSTOM_FORMATTER(zeus::CVector2f, "({} {})", float(obj.x()), float(obj.y()))
|
2021-06-06 23:53:41 +00:00
|
|
|
FMT_CUSTOM_FORMATTER(zeus::CMatrix3f,
|
|
|
|
"\n({} {} {})"
|
|
|
|
"\n({} {} {})"
|
|
|
|
"\n({} {} {})",
|
|
|
|
float(obj[0][0]), float(obj[1][0]), float(obj[2][0]), float(obj[0][1]), float(obj[1][1]),
|
|
|
|
float(obj[2][1]), float(obj[0][2]), float(obj[1][2]), float(obj[2][2]))
|
|
|
|
FMT_CUSTOM_FORMATTER(zeus::CMatrix4f,
|
|
|
|
"\n({} {} {} {})"
|
|
|
|
"\n({} {} {} {})"
|
|
|
|
"\n({} {} {} {})"
|
|
|
|
"\n({} {} {} {})",
|
|
|
|
float(obj[0][0]), float(obj[1][0]), float(obj[2][0]), float(obj[3][0]), float(obj[0][1]),
|
|
|
|
float(obj[1][1]), float(obj[2][1]), float(obj[3][1]), float(obj[0][2]), float(obj[1][2]),
|
|
|
|
float(obj[2][2]), float(obj[3][2]), float(obj[0][3]), float(obj[1][3]), float(obj[2][3]),
|
|
|
|
float(obj[3][3]))
|
|
|
|
FMT_CUSTOM_FORMATTER(zeus::CTransform,
|
|
|
|
"\n({} {} {} {})"
|
|
|
|
"\n({} {} {} {})"
|
|
|
|
"\n({} {} {} {})",
|
2019-07-20 04:27:21 +00:00
|
|
|
float(obj.basis[0][0]), float(obj.basis[1][0]), float(obj.basis[2][0]), float(obj.origin[0]),
|
|
|
|
float(obj.basis[0][1]), float(obj.basis[1][1]), float(obj.basis[2][1]), float(obj.origin[1]),
|
|
|
|
float(obj.basis[0][2]), float(obj.basis[1][2]), float(obj.basis[2][2]), float(obj.origin[2]))
|
|
|
|
|
2019-04-07 05:14:48 +00:00
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
#define URDE_MSAN 1
|
|
|
|
#endif
|
|
|
|
#endif
|