metaforce/Runtime/World/CScriptSpiderBallWaypoint.cpp

193 lines
8.7 KiB
C++
Raw Normal View History

#include "Runtime/World/CScriptSpiderBallWaypoint.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/World/CActorParameters.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2018-12-08 05:30:43 +00:00
namespace urde {
2017-11-13 06:19:18 +00:00
CScriptSpiderBallWaypoint::CScriptSpiderBallWaypoint(TUniqueId uid, std::string_view name, const CEntityInfo& info,
const zeus::CTransform& xf, bool active, u32 w1)
2017-12-19 03:05:50 +00:00
: CActor(uid, active, name, info, xf, CModelData::CModelDataNull(), CMaterialList(EMaterialTypes::NoStepLogic),
CActorParameters::None(), kInvalidUniqueId)
2018-12-08 05:30:43 +00:00
, xe8_(w1) {}
2018-12-08 05:30:43 +00:00
void CScriptSpiderBallWaypoint::Accept(IVisitor& visitor) { visitor.Visit(this); }
2018-12-08 05:30:43 +00:00
void CScriptSpiderBallWaypoint::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
CActor::AcceptScriptMsg(msg, uid, mgr);
2018-12-08 05:30:43 +00:00
if (msg == EScriptObjectMessage::InitializedInArea)
BuildWaypointListAndBounds(mgr);
else if (msg == EScriptObjectMessage::Arrived)
SendScriptMsgs(EScriptObjectState::Arrived, mgr, EScriptObjectMessage::None);
}
2018-12-08 05:30:43 +00:00
void CScriptSpiderBallWaypoint::AccumulateBounds(const zeus::CVector3f& v) {
if (!xfc_aabox)
xfc_aabox.emplace(v, v);
xfc_aabox->accumulateBounds(v);
}
2018-12-08 05:30:43 +00:00
void CScriptSpiderBallWaypoint::BuildWaypointListAndBounds(CStateManager& mgr) {
u32 validConnections = 0;
for (const SConnection& conn : x20_conns) {
if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) {
TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
if (uid != kInvalidUniqueId) {
static_cast<CScriptSpiderBallWaypoint*>(mgr.ObjectById(uid))->AddPreviousWaypoint(GetUniqueId());
++validConnections;
}
}
2018-12-08 05:30:43 +00:00
}
if (validConnections == 0)
AccumulateBounds(x34_transform.origin);
else {
CScriptSpiderBallWaypoint* curWaypoint = this;
TUniqueId uid = curWaypoint->NextWaypoint(mgr, ECheckActiveWaypoint::SkipCheck);
while (uid != kInvalidUniqueId) {
curWaypoint = static_cast<CScriptSpiderBallWaypoint*>(mgr.ObjectById(uid));
uid = curWaypoint->NextWaypoint(mgr, ECheckActiveWaypoint::SkipCheck);
}
2018-12-08 05:30:43 +00:00
curWaypoint->AccumulateBounds(x34_transform.origin);
}
}
2018-12-08 05:30:43 +00:00
void CScriptSpiderBallWaypoint::AddPreviousWaypoint(TUniqueId uid) { xec_waypoints.push_back(uid); }
TUniqueId CScriptSpiderBallWaypoint::PreviousWaypoint(const CStateManager& mgr,
ECheckActiveWaypoint checkActive) const {
for (TUniqueId id : xec_waypoints) {
if (const CEntity* ent = mgr.GetObjectById(id)) {
if (checkActive == ECheckActiveWaypoint::SkipCheck)
return id;
else if (ent->GetActive())
return id;
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-09-18 03:02:48 +00:00
2018-12-08 05:30:43 +00:00
return kInvalidUniqueId;
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
TUniqueId CScriptSpiderBallWaypoint::NextWaypoint(const CStateManager& mgr, ECheckActiveWaypoint checkActive) const {
for (const SConnection& conn : x20_conns) {
if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) {
TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
if (uid != kInvalidUniqueId) {
if (const CEntity* ent = mgr.GetObjectById(uid)) {
if (checkActive == ECheckActiveWaypoint::SkipCheck)
return ent->GetUniqueId();
else if (ent->GetActive())
return ent->GetUniqueId();
}
2018-12-08 05:30:43 +00:00
}
}
2018-12-08 05:30:43 +00:00
}
2018-12-08 05:30:43 +00:00
return kInvalidUniqueId;
}
2017-09-18 03:02:48 +00:00
void CScriptSpiderBallWaypoint::GetClosestPointAlongWaypoints(CStateManager& mgr, const zeus::CVector3f& ballPos,
2018-12-08 05:30:43 +00:00
float maxPointToBallDist,
const CScriptSpiderBallWaypoint*& closestWaypoint,
zeus::CVector3f& closestPoint,
zeus::CVector3f& deltaBetweenPoints,
float deltaBetweenInterpDist,
zeus::CVector3f& interpDeltaBetweenPoints) const {
const CScriptSpiderBallWaypoint* wp = this;
while (wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::SkipCheck) != kInvalidUniqueId)
wp = static_cast<const CScriptSpiderBallWaypoint*>(
mgr.GetObjectById(wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::SkipCheck)));
float minPointToBallDistSq = maxPointToBallDist * maxPointToBallDist;
float deltaBetweenInterpDistSq = deltaBetweenInterpDist * deltaBetweenInterpDist;
zeus::CVector3f lastPoint = wp->GetTranslation();
zeus::CVector3f lastDelta;
bool computeDelta = wp->GetActive();
while (true) {
if (wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check) != kInvalidUniqueId) {
if (computeDelta) {
const CScriptSpiderBallWaypoint* prevWp = wp;
2017-09-18 03:02:48 +00:00
wp = static_cast<const CScriptSpiderBallWaypoint*>(
2018-12-08 05:30:43 +00:00
mgr.GetObjectById(wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check)));
zeus::CVector3f thisDelta = wp->GetTranslation() - lastPoint;
zeus::CVector3f lastPointToBall = ballPos - lastPoint;
if (prevWp->PreviousWaypoint(mgr, ECheckActiveWaypoint::Check) == kInvalidUniqueId)
lastDelta = thisDelta;
float pointToBallDistSq = lastPointToBall.magSquared();
if (pointToBallDistSq < minPointToBallDistSq) {
minPointToBallDistSq = pointToBallDistSq;
closestPoint = lastPoint;
deltaBetweenPoints = thisDelta;
interpDeltaBetweenPoints = (thisDelta.normalized() + lastDelta.normalized()) * 0.5f;
closestWaypoint = wp;
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
float projectedT = lastPointToBall.dot(thisDelta);
if (projectedT >= 0.f) {
float normT = projectedT / thisDelta.magSquared();
if (normT < 1.f) {
zeus::CVector3f projectedPoint = zeus::CVector3f::lerp(lastPoint, wp->GetTranslation(), normT);
float projToBallDistSq = (ballPos - projectedPoint).magSquared();
if (projToBallDistSq < minPointToBallDistSq) {
minPointToBallDistSq = projToBallDistSq;
closestPoint = projectedPoint;
deltaBetweenPoints = thisDelta;
interpDeltaBetweenPoints = deltaBetweenPoints;
closestWaypoint = wp;
float lastToProjDist = (lastPoint - projectedPoint).magnitude();
if (lastToProjDist < deltaBetweenInterpDistSq) {
interpDeltaBetweenPoints =
zeus::CVector3f::lerp(0.5f * (thisDelta.normalized() + lastDelta.normalized()),
thisDelta.normalized(), lastToProjDist / deltaBetweenInterpDist);
} else if (wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check) != kInvalidUniqueId) {
lastToProjDist = (projectedPoint - wp->GetTranslation()).magnitude();
if (lastToProjDist < deltaBetweenInterpDist) {
float t = lastToProjDist / deltaBetweenInterpDist;
interpDeltaBetweenPoints =
zeus::CVector3f::lerp(((static_cast<const CScriptSpiderBallWaypoint*>(
mgr.GetObjectById(wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check)))
->GetTranslation() -
wp->GetTranslation())
.normalized() +
thisDelta.normalized()) *
0.5f,
thisDelta.normalized(), t);
}
}
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
lastDelta = thisDelta;
lastPoint = wp->GetTranslation();
computeDelta = true;
} else {
wp = static_cast<const CScriptSpiderBallWaypoint*>(
mgr.GetObjectById(wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check)));
lastPoint = wp->GetTranslation();
computeDelta = true;
}
} else {
if (wp->NextWaypoint(mgr, ECheckActiveWaypoint::SkipCheck) != kInvalidUniqueId) {
wp = static_cast<const CScriptSpiderBallWaypoint*>(
mgr.GetObjectById(wp->NextWaypoint(mgr, ECheckActiveWaypoint::SkipCheck)));
computeDelta = false;
} else {
break;
}
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
}
if ((ballPos - lastPoint).magSquared() < minPointToBallDistSq) {
closestPoint = lastPoint;
if (wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::Check) != kInvalidUniqueId) {
wp = static_cast<const CScriptSpiderBallWaypoint*>(
mgr.GetObjectById(wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::SkipCheck)));
deltaBetweenPoints = lastPoint - wp->GetTranslation();
interpDeltaBetweenPoints = deltaBetweenPoints;
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
closestWaypoint = wp;
}
2017-09-18 03:02:48 +00:00
}
2018-12-08 05:30:43 +00:00
} // namespace urde