From 8808a3a32469c9b2b730c5f4417025111d106fcf Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 5 Apr 2020 05:36:25 -0400 Subject: [PATCH] CFireFlea: Unindent FindSafeRoute() Makes the function much nicer to follow by using guard clauses to unindent the code. --- Runtime/MP1/World/CFireFlea.cpp | 77 +++++++++++++++++---------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/Runtime/MP1/World/CFireFlea.cpp b/Runtime/MP1/World/CFireFlea.cpp index fc12173e0..6171fc72d 100644 --- a/Runtime/MP1/World/CFireFlea.cpp +++ b/Runtime/MP1/World/CFireFlea.cpp @@ -129,45 +129,46 @@ void CFireFlea::TargetPatrol(CStateManager& mgr, EStateMsg msg, float arg) { } zeus::CVector3f CFireFlea::FindSafeRoute(CStateManager& mgr, const zeus::CVector3f& forward) { - float mag = forward.magnitude(); - if (mag > 0.f) { - CRayCastResult res = mgr.RayStaticIntersection(GetTranslation(), forward.normalized(), 1.f, - CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); - if (res.IsValid() || CheckNearWater(mgr, forward.normalized())) { - zeus::CVector3f right = forward.normalized().cross(zeus::skUp).normalized(); - CRayCastResult res1 = mgr.RayStaticIntersection(GetTranslation(), right, 1.f, - CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); - if (res1.IsValid()) { - zeus::CVector3f left = -right; - CRayCastResult res2 = mgr.RayStaticIntersection(GetTranslation(), left, 1.f, - CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); - if (res2.IsValid()) { - zeus::CVector3f up = right.cross(forward.normalized()); - CRayCastResult res3 = mgr.RayStaticIntersection(GetTranslation(), up, 1.f, - CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); - if (res3.IsValid()) { - zeus::CVector3f down = -up; - CRayCastResult res4 = mgr.RayStaticIntersection(GetTranslation(), down, 1.f, - CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); - if (res4.IsInvalid()) { - return mag * down; - } else { - return -forward; - } - } else { - return mag * up; - } - } else { - return mag * left; - } - } else { - return mag * right; - } - } else { - return forward; - } + const float mag = forward.magnitude(); + if (mag <= 0.f) { + return {}; } - return {}; + + const CRayCastResult res = mgr.RayStaticIntersection(GetTranslation(), forward.normalized(), 1.f, + CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); + if (res.IsInvalid() && !CheckNearWater(mgr, forward.normalized())) { + return forward; + } + + const zeus::CVector3f right = forward.normalized().cross(zeus::skUp).normalized(); + const CRayCastResult res1 = + mgr.RayStaticIntersection(GetTranslation(), right, 1.f, CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); + if (res1.IsInvalid()) { + return mag * right; + } + + const zeus::CVector3f left = -right; + const CRayCastResult res2 = + mgr.RayStaticIntersection(GetTranslation(), left, 1.f, CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); + if (res2.IsInvalid()) { + return mag * left; + } + + const zeus::CVector3f up = right.cross(forward.normalized()); + const CRayCastResult res3 = + mgr.RayStaticIntersection(GetTranslation(), up, 1.f, CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); + if (res3.IsInvalid()) { + return mag * up; + } + + const zeus::CVector3f down = -up; + const CRayCastResult res4 = + mgr.RayStaticIntersection(GetTranslation(), down, 1.f, CMaterialFilter::MakeInclude({EMaterialTypes::Solid})); + if (res4.IsInvalid()) { + return mag * down; + } + + return -forward; } bool CFireFlea::CheckNearWater(const CStateManager& mgr, const zeus::CVector3f& dir) {