metaforce/Runtime/World/CPathFindArea.cpp

273 lines
9.5 KiB
C++
Raw Permalink Normal View History

#include "Runtime/World/CPathFindArea.hpp"
#include "Runtime/CToken.hpp"
#include "Runtime/IVParamObj.hpp"
2016-03-16 19:53:06 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-02-14 07:51:18 +00:00
2018-05-26 03:07:29 +00:00
static logvisor::Module Log("CPathFindArea");
2018-12-08 05:30:43 +00:00
CPFAreaOctree::CPFAreaOctree(CMemoryInStream& in) {
x0_isLeaf = in.readUint32Big();
x4_aabb.readBoundingBoxBig(in);
x1c_center.readBig(in);
2019-10-25 05:44:37 +00:00
for (auto& ptr : x28_children)
ptr = reinterpret_cast<CPFAreaOctree*>(in.readUint32Big());
2018-12-08 05:30:43 +00:00
x48_regions.set_size(in.readUint32Big());
x48_regions.set_data(reinterpret_cast<CPFRegion**>(in.readUint32Big()));
2018-02-14 07:51:18 +00:00
}
2018-12-08 05:30:43 +00:00
void CPFAreaOctree::Fixup(CPFArea& area) {
x0_isLeaf = x0_isLeaf != 0 ? 1 : 0;
if (x0_isLeaf) {
if (x48_regions.empty())
return;
x48_regions.set_data(&area.x160_octreeRegionLookup[reinterpret_cast<uintptr_t>(x48_regions.data())]);
return;
}
2019-10-25 05:44:37 +00:00
for (auto & ptr : x28_children) {
if ((reinterpret_cast<uintptr_t>(ptr) & 0x80000000) == 0)
ptr = &area.x158_octree[reinterpret_cast<uintptr_t>(ptr)];
2018-12-08 05:30:43 +00:00
else
2019-10-25 05:44:37 +00:00
ptr = nullptr;
2018-12-08 05:30:43 +00:00
}
2018-02-14 07:51:18 +00:00
}
2019-10-25 05:44:37 +00:00
u32 CPFAreaOctree::GetChildIndex(const zeus::CVector3f& point) const {
u32 idx = 0x0u;
2018-12-08 05:30:43 +00:00
if (point.x() > x1c_center.x())
2019-10-25 05:44:37 +00:00
idx = 0x1u;
2018-12-08 05:30:43 +00:00
if (point.y() > x1c_center.y())
2019-10-25 05:44:37 +00:00
idx |= 0x2u;
2018-12-08 05:30:43 +00:00
if (point.z() > x1c_center.z())
2019-10-25 05:44:37 +00:00
idx |= 0x4u;
2018-12-08 05:30:43 +00:00
return idx;
}
2018-12-08 05:30:43 +00:00
rstl::prereserved_vector<CPFRegion*>* CPFAreaOctree::GetRegionList(const zeus::CVector3f& point) {
if (x0_isLeaf)
return &x48_regions;
return x28_children[GetChildIndex(point)]->GetRegionList(point);
2018-03-03 05:49:13 +00:00
}
2018-03-01 06:17:16 +00:00
void CPFAreaOctree::GetRegionListList(rstl::reserved_vector<rstl::prereserved_vector<CPFRegion*>*, 32>& listOut,
2018-12-08 05:30:43 +00:00
const zeus::CVector3f& point, float padding) {
if (listOut.size() >= listOut.capacity())
return;
if (x0_isLeaf) {
listOut.push_back(&x48_regions);
return;
}
2019-10-25 05:44:37 +00:00
for (CPFAreaOctree* ch : x28_children) {
2018-12-08 05:30:43 +00:00
if (ch->IsPointInsidePaddedAABox(point, padding))
ch->GetRegionListList(listOut, point, padding);
}
2018-03-01 06:17:16 +00:00
}
2018-12-08 05:30:43 +00:00
bool CPFAreaOctree::IsPointInsidePaddedAABox(const zeus::CVector3f& point, float padding) const {
return point.x() >= x4_aabb.min.x() - padding && point.x() <= x4_aabb.max.x() + padding &&
point.y() >= x4_aabb.min.y() - padding && point.y() <= x4_aabb.max.y() + padding &&
point.z() >= x4_aabb.min.z() - padding && point.z() <= x4_aabb.max.z() + padding;
2018-03-01 06:17:16 +00:00
}
2018-12-15 06:29:41 +00:00
CPFOpenList::CPFOpenList() {
x40_region.SetData(&x90_regionData);
Clear();
}
2018-12-08 05:30:43 +00:00
void CPFOpenList::Clear() {
x40_region.Data()->SetOpenMore(&x40_region);
x40_region.Data()->SetOpenLess(&x40_region);
x0_bitSet.Clear();
2018-03-03 05:49:13 +00:00
}
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
void CPFOpenList::Push(CPFRegion* reg) {
x0_bitSet.Add(reg->GetIndex());
CPFRegion* other = x40_region.Data()->GetOpenMore();
while (other != &x40_region && reg->Data()->GetCost() > other->Data()->GetCost())
other = other->Data()->GetOpenMore();
other->Data()->GetOpenLess()->Data()->SetOpenMore(reg);
reg->Data()->SetOpenLess(other->Data()->GetOpenLess());
other->Data()->SetOpenLess(reg);
reg->Data()->SetOpenMore(other);
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
CPFRegion* CPFOpenList::Pop() {
CPFRegion* reg = x40_region.Data()->GetOpenMore();
if (reg != &x40_region) {
2018-03-03 05:49:13 +00:00
x0_bitSet.Rmv(reg->GetIndex());
reg->Data()->GetOpenMore()->Data()->SetOpenLess(reg->Data()->GetOpenLess());
reg->Data()->GetOpenLess()->Data()->SetOpenMore(reg->Data()->GetOpenMore());
reg->Data()->SetOpenMore(nullptr);
reg->Data()->SetOpenLess(nullptr);
2018-12-08 05:30:43 +00:00
return reg;
}
return nullptr;
2018-03-03 05:49:13 +00:00
}
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
void CPFOpenList::Pop(CPFRegion* reg) {
x0_bitSet.Rmv(reg->GetIndex());
reg->Data()->GetOpenMore()->Data()->SetOpenLess(reg->Data()->GetOpenLess());
reg->Data()->GetOpenLess()->Data()->SetOpenMore(reg->Data()->GetOpenMore());
reg->Data()->SetOpenMore(nullptr);
reg->Data()->SetOpenLess(nullptr);
2018-02-14 07:51:18 +00:00
}
bool CPFOpenList::Test(const CPFRegion* reg) const { return x0_bitSet.Test(reg->GetIndex()); }
2018-05-26 03:07:29 +00:00
2018-12-08 05:30:43 +00:00
CPFArea::CPFArea(std::unique_ptr<u8[]>&& buf, u32 len) {
CMemoryInStream r(buf.get(), len);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 version = r.readUint32Big();
if (version != 4)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("Unexpected PATH version {}, should be 4"), version);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numNodes = r.readUint32Big();
x140_nodes.reserve(numNodes);
for (u32 i = 0; i < numNodes; ++i)
x140_nodes.emplace_back(r);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numLinks = r.readUint32Big();
x148_links.reserve(numLinks);
for (u32 i = 0; i < numLinks; ++i)
x148_links.emplace_back(r);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numRegions = r.readUint32Big();
x150_regions.reserve(numRegions);
for (u32 i = 0; i < numRegions; ++i)
x150_regions.emplace_back(r);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
x178_regionDatas.resize(numRegions);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 maxRegionNodes = 0;
for (CPFRegion& region : x150_regions)
region.Fixup(*this, maxRegionNodes);
maxRegionNodes = std::max(maxRegionNodes, 4u);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
x10_tmpPolyPoints.reserve(maxRegionNodes);
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numBitfieldWords = (numRegions * (numRegions - 1) / 2 + 31) / 32;
x168_connectionsGround.reserve(numBitfieldWords);
for (u32 i = 0; i < numBitfieldWords; ++i)
x168_connectionsGround.push_back(r.readUint32Big());
x170_connectionsFlyers.reserve(numBitfieldWords);
for (u32 i = 0; i < numBitfieldWords; ++i)
x170_connectionsFlyers.push_back(r.readUint32Big());
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
r.seek(((((numRegions * numRegions) + 31) / 32) - numBitfieldWords) * 2 * sizeof(u32));
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numRegionLookups = r.readUint32Big();
x160_octreeRegionLookup.reserve(numRegionLookups);
for (u32 i = 0; i < numRegionLookups; ++i)
x160_octreeRegionLookup.push_back(reinterpret_cast<CPFRegion*>(r.readUint32Big()));
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
for (CPFRegion*& rl : x160_octreeRegionLookup)
rl = &x150_regions[reinterpret_cast<uintptr_t>(rl)];
2018-02-14 07:51:18 +00:00
2018-12-08 05:30:43 +00:00
u32 numOctreeNodes = r.readUint32Big();
x158_octree.reserve(numOctreeNodes);
for (u32 i = 0; i < numOctreeNodes; ++i)
x158_octree.emplace_back(r);
for (CPFAreaOctree& node : x158_octree)
node.Fixup(*this);
2016-03-16 19:53:06 +00:00
}
2018-12-08 05:30:43 +00:00
rstl::prereserved_vector<CPFRegion*>* CPFArea::GetOctreeRegionList(const zeus::CVector3f& point) {
if (x30_hasCachedRegionList && zeus::close_enough(point, x24_cachedRegionListPoint))
return x20_cachedRegionList;
return x158_octree.back().GetRegionList(point);
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
u32 CPFArea::FindRegions(rstl::reserved_vector<CPFRegion*, 4>& regions, const zeus::CVector3f& point, u32 flags,
u32 indexMask) {
2019-10-25 05:44:37 +00:00
bool isFlyer = (flags & 0x2u) != 0;
bool isSwimmer = (flags & 0x4u) != 0;
2018-12-08 05:30:43 +00:00
for (CPFRegion* region : *GetOctreeRegionList(point)) {
2019-10-25 05:44:37 +00:00
if (region->GetFlags() & 0xffu & flags && (region->GetFlags() >> 16u) & 0xffu & indexMask &&
2018-12-08 05:30:43 +00:00
region->IsPointInside(point) && (isFlyer || isSwimmer || region->PointHeight(point) < 3.f)) {
regions.push_back(region);
if (regions.size() == regions.capacity())
break;
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
}
return u32(regions.size());
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
CPFRegion* CPFArea::FindClosestRegion(const zeus::CVector3f& point, u32 flags, u32 indexMask, float padding) {
rstl::reserved_vector<rstl::prereserved_vector<CPFRegion*>*, 32> regionListList;
x158_octree.back().GetRegionListList(regionListList, point, padding);
2019-10-25 05:44:37 +00:00
bool isFlyer = (flags & 0x2u) != 0;
2018-12-08 05:30:43 +00:00
CPFRegion* ret = nullptr;
for (rstl::prereserved_vector<CPFRegion*>* list : regionListList) {
for (CPFRegion* region : *list) {
2019-01-31 07:52:21 +00:00
if (region->Data()->GetCookie() != x34_regionFindCookie) {
2019-10-25 05:44:37 +00:00
if (region->GetFlags() & 0xffu & flags && (region->GetFlags() >> 16u) & 0xffu & indexMask &&
2018-12-08 05:30:43 +00:00
region->IsPointInsidePaddedAABox(point, padding) && (isFlyer || region->PointHeight(point) < 3.f)) {
if (region->FindBestPoint(x10_tmpPolyPoints, point, flags, padding * padding)) {
ret = region;
padding = region->Data()->GetBestPointDistanceSquared() == 0.0
? 0.f
: std::sqrt(region->Data()->GetBestPointDistanceSquared());
x4_closestPoint = region->Data()->GetBestPoint();
}
2018-03-01 06:17:16 +00:00
}
2019-01-31 07:52:21 +00:00
region->Data()->SetCookie(x34_regionFindCookie);
2018-12-08 05:30:43 +00:00
}
2018-03-01 06:17:16 +00:00
}
2018-12-08 05:30:43 +00:00
}
2019-01-31 07:52:21 +00:00
++x34_regionFindCookie;
2018-12-08 05:30:43 +00:00
return ret;
2018-03-01 06:17:16 +00:00
}
2018-03-03 05:49:13 +00:00
zeus::CVector3f CPFArea::FindClosestReachablePoint(rstl::reserved_vector<CPFRegion*, 4>& regs,
2018-12-08 05:30:43 +00:00
const zeus::CVector3f& point, u32 flags, u32 indexMask) {
zeus::CVector3f ret;
float closestDistSq = FLT_MAX;
for (CPFRegion& reg : x150_regions) {
2019-10-25 05:44:37 +00:00
if (reg.GetFlags() & 0xffu & flags && (reg.GetFlags() >> 16u) & 0xffu & indexMask) {
2018-12-08 05:30:43 +00:00
for (CPFRegion* oreg : regs) {
if (PathExists(oreg, &reg, flags)) {
float distSq = (reg.GetCentroid() - point).magSquared();
if (distSq < closestDistSq) {
closestDistSq = distSq;
ret = reg.GetCentroid();
break;
}
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
}
2018-03-03 05:49:13 +00:00
}
2018-12-08 05:30:43 +00:00
}
return ret;
2018-03-01 06:17:16 +00:00
}
2018-12-08 05:30:43 +00:00
bool CPFArea::PathExists(const CPFRegion* r1, const CPFRegion* r2, u32 flags) const {
2019-10-25 05:44:37 +00:00
if (r1 == r2 || (flags & 0x4u) != 0)
2018-12-08 05:30:43 +00:00
return true;
u32 i1 = r1->GetIndex();
u32 i2 = r2->GetIndex();
if (i1 > i2)
std::swap(i1, i2);
u32 remRegions = u32(x150_regions.size()) - i1;
u32 remConnections = remRegions * (remRegions - 1) / 2;
u32 totalConnections = u32(x150_regions.size()) * (u32(x150_regions.size()) - 1) / 2;
u32 bit = totalConnections - remConnections + i2 - (i1 + 1);
auto d = std::div(bit, 32);
2019-10-25 05:44:37 +00:00
if ((flags & 0x2u) != 0)
return ((x170_connectionsFlyers[d.quot] >> u32(d.rem)) & 0x1u) != 0;
2018-12-08 05:30:43 +00:00
else
2019-10-25 05:44:37 +00:00
return ((x168_connectionsGround[d.quot] >> u32(d.rem)) & 0x1u) != 0;
}
2021-04-10 08:42:06 +00:00
CFactoryFnReturn FPathFindAreaFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference*) {
2018-12-08 05:30:43 +00:00
return TToken<CPFArea>::GetIObjObjectFor(std::make_unique<CPFArea>(std::move(in), len));
2016-03-16 19:53:06 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce