From 1b01b232e6d623de16bff470baea9458bfd91152 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 5 Apr 2020 01:36:10 -0400 Subject: [PATCH] CCinematicCamera: Make use of const where applicable Makes read-only intent explicit. --- Runtime/Camera/CCinematicCamera.cpp | 222 +++++++++++++++++----------- 1 file changed, 134 insertions(+), 88 deletions(-) diff --git a/Runtime/Camera/CCinematicCamera.cpp b/Runtime/Camera/CCinematicCamera.cpp index fc98ad994..816fa1829 100644 --- a/Runtime/Camera/CCinematicCamera.cpp +++ b/Runtime/Camera/CCinematicCamera.cpp @@ -14,7 +14,7 @@ namespace urde { CCinematicCamera::CCinematicCamera(TUniqueId uid, std::string_view name, const CEntityInfo& info, const zeus::CTransform& xf, bool active, float shotDuration, float fovy, float znear, float zfar, float aspect, u32 flags) -: CGameCamera(uid, active, name, info, xf, fovy, znear, zfar, aspect, kInvalidUniqueId, flags & 0x20, 0) +: CGameCamera(uid, active, name, info, xf, fovy, znear, zfar, aspect, kInvalidUniqueId, (flags & 0x20) != 0, 0) , x1e8_duration(shotDuration) , x1f0_origFovy(fovy) , x1fc_origOrientation(zeus::CQuaternion(xf.basis)) @@ -35,8 +35,9 @@ void CCinematicCamera::Reset(const zeus::CTransform&, CStateManager& mgr) { void CCinematicCamera::WasDeactivated(CStateManager& mgr) { mgr.GetCameraManager()->RemoveCinemaCamera(GetUniqueId(), mgr); mgr.GetPlayer().GetMorphBall()->LoadMorphBallModel(mgr); - if (x21c_flags & 0x100) + if ((x21c_flags & 0x100) != 0) { mgr.SetCinematicPause(false); + } x188_viewPoints.clear(); x198_viewOrientations.clear(); x1a8_viewPointArrivals.clear(); @@ -48,25 +49,28 @@ void CCinematicCamera::WasDeactivated(CStateManager& mgr) { zeus::CVector3f CCinematicCamera::GetInterpolatedSplinePoint(const std::vector& points, int& idxOut, float tin) const { if (points.size() > 0) { - float cycleT = std::fmod(tin, x1e8_duration); - float durPerPoint = x1e8_duration / float(points.size() - 1); + const float cycleT = std::fmod(tin, x1e8_duration); + const float durPerPoint = x1e8_duration / float(points.size() - 1); idxOut = int(cycleT / durPerPoint); - float t = (cycleT - idxOut * durPerPoint) / durPerPoint; + const float t = (cycleT - float(idxOut) * durPerPoint) / durPerPoint; - if (points.size() == 1) + if (points.size() == 1) { return points.front(); - else if (points.size() == 2) + } + if (points.size() == 2) { return (points[1] - points[0]) * t + points[0]; + } zeus::CVector3f ptA; - if (idxOut > 0) + if (idxOut > 0) { ptA = points[idxOut - 1]; - else + } else { ptA = points[0] - (points[1] - points[0]); + } - zeus::CVector3f ptB = points[idxOut]; + const zeus::CVector3f ptB = points[idxOut]; zeus::CVector3f ptC; - if (idxOut + 1 >= points.size()) { + if (size_t(idxOut + 1) >= points.size()) { const zeus::CVector3f& tmpA = points[points.size() - 1]; const zeus::CVector3f& tmpB = points[points.size() - 2]; ptC = tmpA - (tmpB - tmpA); @@ -75,7 +79,7 @@ zeus::CVector3f CCinematicCamera::GetInterpolatedSplinePoint(const std::vector= points.size()) { + if (size_t(idxOut + 2) >= points.size()) { const zeus::CVector3f& tmpA = points[points.size() - 1]; const zeus::CVector3f& tmpB = points[points.size() - 2]; ptD = tmpA - (tmpB - tmpA); @@ -91,39 +95,50 @@ zeus::CVector3f CCinematicCamera::GetInterpolatedSplinePoint(const std::vector& rotations, float tin) const { - if (rotations.size() == 0) + if (rotations.empty()) { return x1fc_origOrientation; - else if (rotations.size() == 1) - return rotations.front(); + } - float cycleT = std::fmod(tin, x1e8_duration); - float durPerPoint = x1e8_duration / float(rotations.size() - 1); - int idx = int(cycleT / durPerPoint); - float t = (cycleT - idx * durPerPoint) / durPerPoint; + if (rotations.size() == 1) { + return rotations.front(); + } + + const float cycleT = std::fmod(tin, x1e8_duration); + const float durPerPoint = x1e8_duration / float(rotations.size() - 1); + const int idx = int(cycleT / durPerPoint); + const float t = (cycleT - float(idx) * durPerPoint) / durPerPoint; return zeus::CQuaternion::slerp(rotations[idx], rotations[idx + 1], t); } float CCinematicCamera::GetInterpolatedHFov(const std::vector& fovs, float tin) const { - if (fovs.size() == 0) + if (fovs.empty()) { return x1f0_origFovy; - else if (fovs.size() == 1) - return fovs.front(); + } - float cycleT = std::fmod(tin, x1e8_duration); - float durPerPoint = x1e8_duration / float(fovs.size() - 1); - int idx = int(cycleT / durPerPoint); - float t = (cycleT - idx * durPerPoint) / durPerPoint; + if (fovs.size() == 1) { + return fovs.front(); + } + + const float cycleT = std::fmod(tin, x1e8_duration); + const float durPerPoint = x1e8_duration / float(fovs.size() - 1); + const int idx = int(cycleT / durPerPoint); + const float t = (cycleT - float(idx) * durPerPoint) / durPerPoint; return (fovs[idx + 1] - fovs[idx]) * t + fovs[idx]; } float CCinematicCamera::GetMoveOutofIntoAlpha() const { - float startDist = 0.25f + x160_znear; - float endDist = 1.f * startDist; - float deltaMag = (GetTranslation() - x210_moveIntoEyePos).magnitude(); - if (deltaMag >= startDist && deltaMag <= endDist) + const float startDist = 0.25f + x160_znear; + const float endDist = 1.f * startDist; + const float deltaMag = (GetTranslation() - x210_moveIntoEyePos).magnitude(); + + if (deltaMag >= startDist && deltaMag <= endDist) { return (deltaMag - startDist) / (endDist - startDist); - if (deltaMag > endDist) + } + + if (deltaMag > endDist) { return 1.f; + } + return 0.f; } @@ -136,7 +151,7 @@ void CCinematicCamera::DeactivateSelf(CStateManager& mgr) { void CCinematicCamera::Think(float dt, CStateManager& mgr) { if (GetActive()) { zeus::CVector3f viewPoint = GetTranslation(); - if (x188_viewPoints.size() > 0) { + if (!x188_viewPoints.empty()) { int idx = 0; viewPoint = GetInterpolatedSplinePoint(x188_viewPoints, idx, x1ec_t); if (idx > x1f4_passedViewPoint) { @@ -145,58 +160,67 @@ void CCinematicCamera::Think(float dt, CStateManager& mgr) { } } - zeus::CQuaternion orientation = GetInterpolatedOrientation(x198_viewOrientations, x1ec_t); + const zeus::CQuaternion orientation = GetInterpolatedOrientation(x198_viewOrientations, x1ec_t); if ((x21c_flags & 0x1) == 0) { - if (x1b8_targets.size() > 0) { + if (!x1b8_targets.empty()) { int idx = 0; zeus::CVector3f target = GetInterpolatedSplinePoint(x1b8_targets, idx, x1ec_t); if (x1b8_targets.size() == 1) { - if (TCastToConstPtr act = mgr.GetObjectById(x1c8_targetArrivals.front())) + if (const TCastToConstPtr act = mgr.GetObjectById(x1c8_targetArrivals.front())) { target = act->GetTranslation(); - else + } else { x1ec_t = x1e8_duration; + } } if (idx > x1f8_passedTarget) { x1f8_passedTarget = idx; SendArrivedMsg(x1c8_targetArrivals[x1f8_passedTarget], mgr); } - zeus::CVector3f upVec = orientation.transform(zeus::skUp); - if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) + const zeus::CVector3f upVec = orientation.transform(zeus::skUp); + if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) { SetTranslation(target); - else + } else { SetTransform(zeus::lookAt(viewPoint, target, upVec)); + } } else { SetTransform(zeus::CTransform(orientation, viewPoint)); } } else { zeus::CVector3f target = mgr.GetPlayer().GetTranslation(); - if (mgr.GetPlayer().GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Morphed) + if (mgr.GetPlayer().GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Morphed) { target.z() += mgr.GetPlayer().GetMorphBall()->GetBallRadius(); - else + } else { target.z() += mgr.GetPlayer().GetEyeHeight(); + } - zeus::CVector3f upVec = orientation.transform(zeus::skUp); - if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) + const zeus::CVector3f upVec = orientation.transform(zeus::skUp); + if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) { SetTranslation(target); - else + } else { SetTransform(zeus::lookAt(viewPoint, target, upVec)); + } } x15c_currentFov = GetInterpolatedHFov(x1d8_viewHFovs, x1ec_t) / x168_aspect; x170_24_perspDirty = true; - if (x20c_lookAtId != kInvalidUniqueId) - if (TCastToPtr act = mgr.ObjectById(x20c_lookAtId)) - if (act->IsPlayerActor()) + if (x20c_lookAtId != kInvalidUniqueId) { + if (const TCastToPtr act = mgr.ObjectById(x20c_lookAtId)) { + if (act->IsPlayerActor()) { act->SetDrawFlags({5, 0, 3, zeus::CColor(1.f, GetMoveOutofIntoAlpha())}); + } + } + } x1ec_t += dt; if (x1ec_t > x1e8_duration) { - for (size_t i = x1f4_passedViewPoint + 1; i < x1a8_viewPointArrivals.size(); ++i) + for (size_t i = x1f4_passedViewPoint + 1; i < x1a8_viewPointArrivals.size(); ++i) { SendArrivedMsg(x1a8_viewPointArrivals[i], mgr); - for (size_t i = x1f8_passedTarget + 1; i < x1c8_targetArrivals.size(); ++i) + } + for (size_t i = x1f8_passedTarget + 1; i < x1c8_targetArrivals.size(); ++i) { SendArrivedMsg(x1c8_targetArrivals[i], mgr); + } DeactivateSelf(mgr); } } @@ -206,14 +230,15 @@ void CCinematicCamera::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CGameCamera::AcceptScriptMsg(msg, uid, mgr); switch (msg) { case EScriptObjectMessage::InitializedInArea: - if (x21c_flags & 0x4 || x21c_flags & 0x2) { + if ((x21c_flags & 0x4) != 0 || (x21c_flags & 0x2) != 0) { for (const SConnection& conn : x20_conns) { - TUniqueId id = mgr.GetIdForScript(conn.x8_objId); - if (TCastToPtr act = mgr.ObjectById(id)) { + const TUniqueId id = mgr.GetIdForScript(conn.x8_objId); + if (const TCastToConstPtr act = mgr.ObjectById(id)) { if (act->IsPlayerActor()) { x20c_lookAtId = id; - if (conn.x4_msg != EScriptObjectMessage::Deactivate && conn.x4_msg != EScriptObjectMessage::Reset) + if (conn.x4_msg != EScriptObjectMessage::Deactivate && conn.x4_msg != EScriptObjectMessage::Reset) { break; + } } } } @@ -221,19 +246,23 @@ void CCinematicCamera::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, break; case EScriptObjectMessage::Activate: CalculateWaypoints(mgr); - if ((x21c_flags & 1) == 0 && x220_24_ && x1b8_targets.empty()) + if ((x21c_flags & 1) == 0 && x220_24_ && x1b8_targets.empty()) { break; + } x1ec_t = 0.f; Think(0.f, mgr); mgr.GetCameraManager()->AddCinemaCamera(GetUniqueId(), mgr); x1f4_passedViewPoint = 0; - if (x1a8_viewPointArrivals.size() > 0) + if (!x1a8_viewPointArrivals.empty()) { SendArrivedMsg(x1a8_viewPointArrivals[x1f4_passedViewPoint], mgr); + } x1f8_passedTarget = 0; - if (x1c8_targetArrivals.size() > 0) + if (!x1c8_targetArrivals.empty()) { SendArrivedMsg(x1c8_targetArrivals[x1f8_passedTarget], mgr); - if (x21c_flags & 0x100) + } + if ((x21c_flags & 0x100) != 0) { mgr.SetCinematicPause(true); + } break; case EScriptObjectMessage::Deactivate: WasDeactivated(mgr); @@ -247,7 +276,7 @@ void CCinematicCamera::CalculateMoveOutofIntoEyePosition(bool outOfEye, CStateMa zeus::CQuaternion q(mgr.GetPlayer().GetTransform().basis); zeus::CVector3f eyePos = mgr.GetPlayer().GetEyePosition(); if (x20c_lookAtId != kInvalidUniqueId) { - if (TCastToConstPtr act = mgr.GetObjectById(x20c_lookAtId)) { + if (const TCastToConstPtr act = mgr.GetObjectById(x20c_lookAtId)) { if (act->IsPlayerActor()) { if (const CModelData* mData = act->GetModelData()) { if (const CAnimData* aData = mData->GetAnimationData()) { @@ -255,8 +284,9 @@ void CCinematicCamera::CalculateMoveOutofIntoEyePosition(bool outOfEye, CStateMa const CSegId lEye = aData->GetLocatorSegId("L_eye"sv); const CSegId rEye = aData->GetLocatorSegId("R_eye"sv); if (lEye.IsValid() && rEye.IsValid()) { - CCharAnimTime time = outOfEye ? CCharAnimTime(0.f) : root->VGetSteadyStateAnimInfo().GetDuration(); - CCharAnimTime* pTime = outOfEye ? nullptr : &time; + const CCharAnimTime time = + outOfEye ? CCharAnimTime(0.f) : root->VGetSteadyStateAnimInfo().GetDuration(); + const CCharAnimTime* pTime = outOfEye ? nullptr : &time; eyePos = ((act->GetTransform() * mData->GetScaledLocatorTransformDynamic("L_eye"sv, pTime)).origin + (act->GetTransform() * mData->GetScaledLocatorTransformDynamic("R_eye"sv, pTime)).origin) * 0.5f; @@ -287,8 +317,8 @@ void CCinematicCamera::CalculateMoveOutofIntoEyePosition(bool outOfEye, CStateMa } void CCinematicCamera::GenerateMoveOutofIntoPoints(bool outOfEye, CStateManager& mgr) { - zeus::CQuaternion q(mgr.GetPlayer().GetTransform().basis); - zeus::CVector3f eyePos = mgr.GetPlayer().GetEyePosition(); + const zeus::CQuaternion q(mgr.GetPlayer().GetTransform().basis); + const zeus::CVector3f eyePos = mgr.GetPlayer().GetEyePosition(); zeus::CVector3f behindDelta = q.transform({0.f, -g_tweakPlayerRes->xf0_cinematicMoveOutofIntoPlayerDistance, 0.f}); zeus::CVector3f behindPos = eyePos; if (!outOfEye) { @@ -309,20 +339,25 @@ void CCinematicCamera::GenerateMoveOutofIntoPoints(bool outOfEye, CStateManager& bool CCinematicCamera::PickRandomActiveConnection(const std::vector& conns, SConnection& randConn, CStateManager& mgr) { int count = 0; - for (const SConnection& conn : conns) - if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) - if (TCastToConstPtr act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) - if (act->GetActive()) + for (const SConnection& conn : conns) { + if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) { + if (const TCastToConstPtr act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) { + if (act->GetActive()) { ++count; + } + } + } + } - if (!count) + if (count == 0) { return false; + } - int randIdx = mgr.GetActiveRandom()->Next() % count; + const int randIdx = mgr.GetActiveRandom()->Next() % count; int idx = 0; - for (const SConnection& conn : conns) - if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) - if (TCastToConstPtr act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) + for (const SConnection& conn : conns) { + if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) { + if (const TCastToConstPtr act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) { if (act->GetActive()) { if (randIdx == idx) { randConn = conn; @@ -330,6 +365,9 @@ bool CCinematicCamera::PickRandomActiveConnection(const std::vector } ++idx; } + } + } + } return true; } @@ -338,10 +376,11 @@ void CCinematicCamera::CalculateWaypoints(CStateManager& mgr) { const SConnection* firstVP = nullptr; const SConnection* firstTarget = nullptr; for (const SConnection& conn : x20_conns) { - if (conn.x0_state == EScriptObjectState::CameraPath && conn.x4_msg == EScriptObjectMessage::Activate) + if (conn.x0_state == EScriptObjectState::CameraPath && conn.x4_msg == EScriptObjectMessage::Activate) { firstVP = &conn; - else if (conn.x0_state == EScriptObjectState::CameraTarget && conn.x4_msg == EScriptObjectMessage::Activate) + } else if (conn.x0_state == EScriptObjectState::CameraTarget && conn.x4_msg == EScriptObjectMessage::Activate) { firstTarget = &conn; + } } x188_viewPoints.clear(); @@ -359,27 +398,31 @@ void CCinematicCamera::CalculateWaypoints(CStateManager& mgr) { x220_24_ = false; - if ((x21c_flags & 0x2) != 0 && (x21c_flags & 0x200) == 0) + if ((x21c_flags & 0x2) != 0 && (x21c_flags & 0x200) == 0) { GenerateMoveOutofIntoPoints(true, mgr); + } if (firstVP) { TCastToConstPtr wp = mgr.GetObjectById(mgr.GetIdForScript(firstVP->x8_objId)); while (wp) { x188_viewPoints.push_back(wp->GetTranslation()); x198_viewOrientations.emplace_back(wp->GetTransform().basis); - if (TCastToConstPtr cwp = wp.GetPtr()) + if (const TCastToConstPtr cwp = wp.GetPtr()) { x1d8_viewHFovs.push_back(cwp->GetHFov()); - auto search = std::find_if(x1a8_viewPointArrivals.begin(), x1a8_viewPointArrivals.end(), - [&](TUniqueId id) { return id == wp->GetUniqueId(); }); - if (search == x1a8_viewPointArrivals.end()) { + } + const auto search = std::find_if(x1a8_viewPointArrivals.cbegin(), x1a8_viewPointArrivals.cend(), + [&wp](TUniqueId id) { return id == wp->GetUniqueId(); }); + if (search == x1a8_viewPointArrivals.cend()) { x1a8_viewPointArrivals.push_back(wp->GetUniqueId()); SConnection randConn; - if (PickRandomActiveConnection(wp->GetConnectionList(), randConn, mgr)) + if (PickRandomActiveConnection(wp->GetConnectionList(), randConn, mgr)) { wp = mgr.GetObjectById(mgr.GetIdForScript(randConn.x8_objId)); - else + } else { break; - } else + } + } else { break; + } } } @@ -387,22 +430,25 @@ void CCinematicCamera::CalculateWaypoints(CStateManager& mgr) { TCastToConstPtr tgt = mgr.GetObjectById(mgr.GetIdForScript(firstTarget->x8_objId)); while (tgt) { x1b8_targets.push_back(tgt->GetTranslation()); - auto search = std::find_if(x1c8_targetArrivals.begin(), x1c8_targetArrivals.end(), - [&](TUniqueId id) { return id == tgt->GetUniqueId(); }); - if (search == x1c8_targetArrivals.end()) { + const auto search = std::find_if(x1c8_targetArrivals.cbegin(), x1c8_targetArrivals.cend(), + [&tgt](TUniqueId id) { return id == tgt->GetUniqueId(); }); + if (search == x1c8_targetArrivals.cend()) { x1c8_targetArrivals.push_back(tgt->GetUniqueId()); SConnection randConn; - if (PickRandomActiveConnection(tgt->GetConnectionList(), randConn, mgr)) + if (PickRandomActiveConnection(tgt->GetConnectionList(), randConn, mgr)) { tgt = mgr.GetObjectById(mgr.GetIdForScript(randConn.x8_objId)); - else + } else { break; - } else + } + } else { break; + } } } - if ((x21c_flags & 0x4) != 0 && (x21c_flags & 0x200) == 0) + if ((x21c_flags & 0x4) != 0 && (x21c_flags & 0x200) == 0) { GenerateMoveOutofIntoPoints(false, mgr); + } } void CCinematicCamera::SendArrivedMsg(TUniqueId reciever, CStateManager& mgr) {