2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/World/CScriptSpiderBallWaypoint.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/CStateManager.hpp"
|
|
|
|
#include "Runtime/World/CActorParameters.hpp"
|
|
|
|
|
2019-09-21 13:07:13 +00:00
|
|
|
#include "TCastTo.hpp" // Generated file, do not modify include path
|
2017-08-08 22:12:14 +00:00
|
|
|
|
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,
|
2017-08-08 22:12:14 +00:00
|
|
|
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),
|
2017-08-08 22:12:14 +00:00
|
|
|
CActorParameters::None(), kInvalidUniqueId)
|
2018-12-08 05:30:43 +00:00
|
|
|
, xe8_(w1) {}
|
2017-08-08 22:12:14 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSpiderBallWaypoint::Accept(IVisitor& visitor) { visitor.Visit(this); }
|
2017-08-08 22:12:14 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSpiderBallWaypoint::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
|
|
|
|
CActor::AcceptScriptMsg(msg, uid, mgr);
|
2017-08-08 22:12:14 +00:00
|
|
|
|
2020-05-07 16:03:44 +00:00
|
|
|
if (msg == EScriptObjectMessage::InitializedInArea) {
|
2018-12-08 05:30:43 +00:00
|
|
|
BuildWaypointListAndBounds(mgr);
|
2020-05-07 16:03:44 +00:00
|
|
|
} else if (msg == EScriptObjectMessage::Arrived) {
|
2018-12-08 05:30:43 +00:00
|
|
|
SendScriptMsgs(EScriptObjectState::Arrived, mgr, EScriptObjectMessage::None);
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSpiderBallWaypoint::AccumulateBounds(const zeus::CVector3f& v) {
|
2020-05-07 16:03:44 +00:00
|
|
|
if (!xfc_aabox) {
|
2018-12-08 05:30:43 +00:00
|
|
|
xfc_aabox.emplace(v, v);
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
xfc_aabox->accumulateBounds(v);
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (uid != kInvalidUniqueId) {
|
|
|
|
static_cast<CScriptSpiderBallWaypoint*>(mgr.ObjectById(uid))->AddPreviousWaypoint(GetUniqueId());
|
|
|
|
++validConnections;
|
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:03:44 +00:00
|
|
|
if (validConnections == 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
AccumulateBounds(x34_transform.origin);
|
2020-05-07 16:03:44 +00:00
|
|
|
} else {
|
2018-12-08 05:30:43 +00:00
|
|
|
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);
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
curWaypoint->AccumulateBounds(x34_transform.origin);
|
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-09-18 21:04:50 +00:00
|
|
|
for (const auto& id : xec_waypoints) {
|
2018-12-08 05:30:43 +00:00
|
|
|
if (const CEntity* ent = mgr.GetObjectById(id)) {
|
2020-05-07 16:03:44 +00:00
|
|
|
if (checkActive == ECheckActiveWaypoint::SkipCheck) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return id;
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
|
|
|
if (ent->GetActive()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return id;
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
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) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (uid != kInvalidUniqueId) {
|
|
|
|
if (const CEntity* ent = mgr.GetObjectById(uid)) {
|
2020-05-07 16:03:44 +00:00
|
|
|
if (checkActive == ECheckActiveWaypoint::SkipCheck) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return ent->GetUniqueId();
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
|
|
|
if (ent->GetActive()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return ent->GetUniqueId();
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-08-08 22:12:14 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return kInvalidUniqueId;
|
2017-08-08 22:12:14 +00:00
|
|
|
}
|
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 {
|
2020-05-07 16:03:44 +00:00
|
|
|
const auto* wp = this;
|
|
|
|
while (wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::SkipCheck) != kInvalidUniqueId) {
|
2018-12-08 05:30:43 +00:00
|
|
|
wp = static_cast<const CScriptSpiderBallWaypoint*>(
|
|
|
|
mgr.GetObjectById(wp->PreviousWaypoint(mgr, ECheckActiveWaypoint::SkipCheck)));
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float minPointToBallDistSq = maxPointToBallDist * maxPointToBallDist;
|
2020-05-07 16:03:44 +00:00
|
|
|
const float deltaBetweenInterpDistSq = deltaBetweenInterpDist * deltaBetweenInterpDist;
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CVector3f lastPoint = wp->GetTranslation();
|
|
|
|
zeus::CVector3f lastDelta;
|
|
|
|
bool computeDelta = wp->GetActive();
|
2020-05-07 16:03:44 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
while (true) {
|
|
|
|
if (wp->NextWaypoint(mgr, ECheckActiveWaypoint::Check) != kInvalidUniqueId) {
|
|
|
|
if (computeDelta) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const auto* 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)));
|
2020-05-07 16:03:44 +00:00
|
|
|
|
|
|
|
const zeus::CVector3f thisDelta = wp->GetTranslation() - lastPoint;
|
|
|
|
const zeus::CVector3f lastPointToBall = ballPos - lastPoint;
|
|
|
|
if (prevWp->PreviousWaypoint(mgr, ECheckActiveWaypoint::Check) == kInvalidUniqueId) {
|
2018-12-08 05:30:43 +00:00
|
|
|
lastDelta = thisDelta;
|
2020-05-07 16:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const float pointToBallDistSq = lastPointToBall.magSquared();
|
2018-12-08 05:30:43 +00:00
|
|
|
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
|
|
|
}
|
2020-05-07 16:03:44 +00:00
|
|
|
|
|
|
|
const float projectedT = lastPointToBall.dot(thisDelta);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (projectedT >= 0.f) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const float normT = projectedT / thisDelta.magSquared();
|
2018-12-08 05:30:43 +00:00
|
|
|
if (normT < 1.f) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const zeus::CVector3f projectedPoint = zeus::CVector3f::lerp(lastPoint, wp->GetTranslation(), normT);
|
|
|
|
const float projToBallDistSq = (ballPos - projectedPoint).magSquared();
|
2018-12-08 05:30:43 +00:00
|
|
|
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) {
|
2020-05-07 16:03:44 +00:00
|
|
|
const float t = lastToProjDist / deltaBetweenInterpDist;
|
2018-12-08 05:30:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-07 16:03:44 +00:00
|
|
|
if ((ballPos - lastPoint).magSquared() >= minPointToBallDistSq) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-05-07 16:03:44 +00:00
|
|
|
closestWaypoint = wp;
|
2017-09-18 03:02:48 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace urde
|