From fd5c6f9e593e730711948070a537dacac8946301 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 12 Apr 2020 14:54:47 -0400 Subject: [PATCH] CScriptPlatform: Make use of TCastToConstPtr where applicable Makes the read-only intent more explicit to the reader. --- Runtime/World/CScriptPlatform.cpp | 218 ++++++++++++++++++------------ 1 file changed, 134 insertions(+), 84 deletions(-) diff --git a/Runtime/World/CScriptPlatform.cpp b/Runtime/World/CScriptPlatform.cpp index 11d92062e..cebbf646b 100644 --- a/Runtime/World/CScriptPlatform.cpp +++ b/Runtime/World/CScriptPlatform.cpp @@ -46,37 +46,45 @@ CScriptPlatform::CScriptPlatform( CMaterialList(EMaterialTypes::Solid), CMaterialList(EMaterialTypes::NoStaticCollision, EMaterialTypes::NoPlatformCollision, EMaterialTypes::Platform))); xf8_24_movable = false; - if (HasModelData() && GetModelData()->HasAnimData()) + if (HasModelData() && GetModelData()->HasAnimData()) { GetModelData()->GetAnimationData()->EnableLooping(true); - if (x304_treeGroupContainer) + } + if (x304_treeGroupContainer) { x314_treeGroup = std::make_unique(x304_treeGroupContainer->GetObj(), x68_material); + } } void CScriptPlatform::Accept(IVisitor& visitor) { visitor.Visit(this); } void CScriptPlatform::DragSlave(CStateManager& mgr, rstl::reserved_vector& draggedSet, CActor* actor, const zeus::CVector3f& delta) { - if (std::find(draggedSet.begin(), draggedSet.end(), actor->GetUniqueId().Value()) == draggedSet.end()) { - draggedSet.push_back(actor->GetUniqueId().Value()); - zeus::CTransform newXf = actor->GetTransform(); - newXf.origin += delta; - actor->SetTransform(newXf); - if (TCastToPtr plat = actor) - plat->DragSlaves(mgr, draggedSet, delta); + if (std::find(draggedSet.begin(), draggedSet.end(), actor->GetUniqueId().Value()) != draggedSet.end()) { + return; + } + + draggedSet.push_back(actor->GetUniqueId().Value()); + zeus::CTransform newXf = actor->GetTransform(); + newXf.origin += delta; + actor->SetTransform(newXf); + if (const TCastToPtr plat = actor) { + plat->DragSlaves(mgr, draggedSet, delta); } } void CScriptPlatform::DragSlaves(CStateManager& mgr, rstl::reserved_vector& draggedSet, const zeus::CVector3f& delta) { - for (SRiders& rider : x328_slavesStatic) - if (TCastToPtr act = mgr.ObjectById(rider.x0_uid)) + for (SRiders& rider : x328_slavesStatic) { + if (const TCastToPtr act = mgr.ObjectById(rider.x0_uid)) { DragSlave(mgr, draggedSet, act.GetPtr(), delta); + } + } for (auto it = x338_slavesDynamic.begin(); it != x338_slavesDynamic.end();) { - if (TCastToPtr act = mgr.ObjectById(it->x0_uid)) { + if (const TCastToPtr act = mgr.ObjectById(it->x0_uid)) { DragSlave(mgr, draggedSet, act.GetPtr(), delta); ++it; - } else + } else { it = x338_slavesDynamic.erase(it); + } } } @@ -95,9 +103,9 @@ void CScriptPlatform::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, C } case EScriptObjectMessage::Next: { x25a_targetWaypoint = GetNext(x258_currentWaypoint, mgr); - if (x25a_targetWaypoint == kInvalidUniqueId) + if (x25a_targetWaypoint == kInvalidUniqueId) { mgr.SendScriptMsg(this, GetUniqueId(), EScriptObjectMessage::Stop); - else if (TCastToPtr wp = mgr.ObjectById(x25a_targetWaypoint)) { + } else if (const TCastToPtr wp = mgr.ObjectById(x25a_targetWaypoint)) { x25c_currentSpeed = 0.f; Stop(); x270_dragDelta = wp->GetTranslation() - GetTranslation(); @@ -116,10 +124,11 @@ void CScriptPlatform::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, C } case EScriptObjectMessage::Start: { x25a_targetWaypoint = GetNext(x258_currentWaypoint, mgr); - if (x25a_targetWaypoint == kInvalidUniqueId) + if (x25a_targetWaypoint == kInvalidUniqueId) { mgr.SendScriptMsg(this, GetUniqueId(), EScriptObjectMessage::Stop); - else if (TCastToPtr wp = mgr.ObjectById(x25a_targetWaypoint)) + } else if (const TCastToConstPtr wp = mgr.ObjectById(x25a_targetWaypoint)) { x25c_currentSpeed = wp->GetSpeed(); + } break; } case EScriptObjectMessage::Reset: { @@ -128,10 +137,11 @@ void CScriptPlatform::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, C break; } case EScriptObjectMessage::Increment: { - if (GetActive()) + if (GetActive()) { CScriptColorModulate::FadeInHelper(mgr, GetUniqueId(), x268_fadeInTime); - else + } else { mgr.SendScriptMsg(this, GetUniqueId(), EScriptObjectMessage::Activate); + } break; } case EScriptObjectMessage::Decrement: @@ -180,7 +190,7 @@ void CScriptPlatform::MoveRiders(CStateManager& mgr, float dt, bool active, std: continue; } act->SetTranslation(newPos); - if (TCastToPtr player = act.GetPtr()) { + if (const TCastToConstPtr player = act.GetPtr()) { if (player->GetOrbitState() != CPlayer::EPlayerOrbitState::NoOrbit) { ++it; continue; @@ -199,9 +209,11 @@ void CScriptPlatform::MoveRiders(CStateManager& mgr, float dt, bool active, std: rstl::reserved_vector CScriptPlatform::BuildNearListFromRiders(CStateManager& mgr, const std::vector& movedRiders) { rstl::reserved_vector ret; - for (const SRiders& rider : movedRiders) - if (TCastToPtr act = mgr.ObjectById(rider.x0_uid)) + for (const SRiders& rider : movedRiders) { + if (const TCastToConstPtr act = mgr.ObjectById(rider.x0_uid)) { ret.push_back(act->GetUniqueId()); + } + } return ret; } @@ -214,9 +226,11 @@ void CScriptPlatform::PreThink(float dt, CStateManager& mgr) { zeus::CTransform oldXf = x34_transform; CMotionState mState = GetMotionState(); if (GetActive()) { - for (SRiders& rider : x318_riders) - if (TCastToPtr act = mgr.ObjectById(rider.x0_uid)) + for (SRiders& rider : x318_riders) { + if (const TCastToConstPtr act = mgr.ObjectById(rider.x0_uid)) { rider.x8_transform.origin = x34_transform.transposeRotate(act->GetTranslation() - GetTranslation()); + } + } x27c_rotDelta = Move(dt, mgr); } @@ -243,16 +257,20 @@ void CScriptPlatform::PreThink(float dt, CStateManager& mgr) { } void CScriptPlatform::Think(float dt, CStateManager& mgr) { - if (!GetActive()) + if (!GetActive()) { return; + } if (HasModelData() && GetModelData()->HasAnimData()) { - if (!x356_25_controlledAnimation) + if (!x356_25_controlledAnimation) { UpdateAnimation(dt, mgr, true); + } + if (x356_28_rainSplashes && mgr.GetWorld()->GetNeededEnvFx() == EEnvFxType::Rain) { if (HasModelData() && !GetModelData()->IsNull() && mgr.GetEnvFxManager()->IsSplashActive() && - mgr.GetEnvFxManager()->GetRainMagnitude() != 0.f) + mgr.GetEnvFxManager()->GetRainMagnitude() != 0.f) { mgr.GetActorModelParticles()->AddRainSplashGenerator(*this, mgr, x34c_maxRainSplashes, x350_rainGenRate, 0.f); + } } } @@ -261,8 +279,9 @@ void CScriptPlatform::Think(float dt, CStateManager& mgr) { DragSlaves(mgr, draggedSet, x270_dragDelta); } - if (x356_24_dead) + if (x356_24_dead) { return; + } if (HealthInfo(mgr)->GetHP() <= 0.f) { x356_24_dead = true; @@ -272,33 +291,42 @@ void CScriptPlatform::Think(float dt, CStateManager& mgr) { void CScriptPlatform::PreRender(CStateManager& mgr, const zeus::CFrustum& frustum) { CActor::PreRender(mgr, frustum); + if (!xe4_30_outOfFrustum && !zeus::close_enough(x348_xrayAlpha, 1.f)) { - CModelFlags flags(5, 0, 3, {1.f, x348_xrayAlpha}); + const CModelFlags flags(5, 0, 3, {1.f, x348_xrayAlpha}); if (mgr.GetPlayerState()->GetActiveVisor(mgr) == CPlayerState::EPlayerVisor::XRay && !x356_30_disableXrayAlpha) { xb4_drawFlags = flags; x356_29_setXrayDrawFlags = true; } else if (x356_29_setXrayDrawFlags) { x356_29_setXrayDrawFlags = false; - if (xb4_drawFlags == flags && !x356_30_disableXrayAlpha) + if (xb4_drawFlags == flags && !x356_30_disableXrayAlpha) { xb4_drawFlags = CModelFlags(0, 0, 3, zeus::skWhite); + } } } - if (!mgr.GetObjectById(x354_boundsTrigger)) + + if (!mgr.GetObjectById(x354_boundsTrigger)) { x354_boundsTrigger = kInvalidUniqueId; + } } void CScriptPlatform::Render(CStateManager& mgr) { - bool xray = mgr.GetPlayerState()->GetActiveVisor(mgr) == CPlayerState::EPlayerVisor::XRay; - if (xray && !x356_31_xrayFog) + const bool xray = mgr.GetPlayerState()->GetActiveVisor(mgr) == CPlayerState::EPlayerVisor::XRay; + if (xray && !x356_31_xrayFog) { g_Renderer->SetWorldFog(ERglFogMode::None, 0.f, 1.f, zeus::skBlack); + } + CPhysicsActor::Render(mgr); - if (xray && !x356_31_xrayFog) + + if (xray && !x356_31_xrayFog) { mgr.SetupFogForArea(x4_areaId); + } } std::optional CScriptPlatform::GetTouchBounds() const { - if (x314_treeGroup) + if (x314_treeGroup) { return {x314_treeGroup->CalculateAABox(GetTransform())}; + } return {CPhysicsActor::GetBoundingBox()}; } @@ -310,38 +338,45 @@ zeus::CTransform CScriptPlatform::GetPrimitiveTransform() const { } const CCollisionPrimitive* CScriptPlatform::GetCollisionPrimitive() const { - if (!x314_treeGroup) + if (!x314_treeGroup) { return CPhysicsActor::GetCollisionPrimitive(); + } return x314_treeGroup.get(); } zeus::CVector3f CScriptPlatform::GetOrbitPosition(const CStateManager& mgr) const { return GetAimPosition(mgr, 0.f); } zeus::CVector3f CScriptPlatform::GetAimPosition(const CStateManager& mgr, float dt) const { - if (auto tb = GetTouchBounds()) + if (auto tb = GetTouchBounds()) { return {tb->center()}; + } return CPhysicsActor::GetAimPosition(mgr, dt); } zeus::CAABox CScriptPlatform::GetSortingBounds(const CStateManager& mgr) const { - if (x354_boundsTrigger != kInvalidUniqueId) - if (TCastToConstPtr trig = mgr.GetObjectById(x354_boundsTrigger)) + if (x354_boundsTrigger != kInvalidUniqueId) { + if (const TCastToConstPtr trig = mgr.GetObjectById(x354_boundsTrigger)) { return trig->GetTriggerBoundsWR(); + } + } return CActor::GetSortingBounds(mgr); } bool CScriptPlatform::IsRider(TUniqueId id) const { - for (const SRiders& rider : x318_riders) - if (rider.x0_uid == id) + for (const SRiders& rider : x318_riders) { + if (rider.x0_uid == id) { return true; + } + } return false; } bool CScriptPlatform::IsSlave(TUniqueId id) const { auto search = std::find_if(x328_slavesStatic.begin(), x328_slavesStatic.end(), [id](const SRiders& rider) { return rider.x0_uid == id; }); - if (search != x328_slavesStatic.end()) + if (search != x328_slavesStatic.end()) { return true; + } search = std::find_if(x338_slavesDynamic.begin(), x338_slavesDynamic.end(), [id](const SRiders& rider) { return rider.x0_uid == id; }); return search != x338_slavesDynamic.end(); @@ -351,7 +386,7 @@ void CScriptPlatform::BuildSlaveList(CStateManager& mgr) { x328_slavesStatic.reserve(GetConnectionList().size()); for (const SConnection& conn : GetConnectionList()) { if (conn.x0_state == EScriptObjectState::Play && conn.x4_msg == EScriptObjectMessage::Activate) { - if (TCastToPtr act = mgr.ObjectById(mgr.GetIdForScript(conn.x8_objId))) { + if (const TCastToPtr act = mgr.ObjectById(mgr.GetIdForScript(conn.x8_objId))) { act->AddMaterial(EMaterialTypes::PlatformSlave, mgr); zeus::CTransform xf = act->GetTransform(); xf.origin = act->GetTranslation() - GetTranslation(); @@ -359,19 +394,22 @@ void CScriptPlatform::BuildSlaveList(CStateManager& mgr) { } } else if (conn.x0_state == EScriptObjectState::InheritBounds && conn.x4_msg == EScriptObjectMessage::Activate) { auto list = mgr.GetIdListForScript(conn.x8_objId); - for (auto it = list.first; it != list.second; ++it) - if (TCastToConstPtr(mgr.GetObjectById(it->second))) + for (auto it = list.first; it != list.second; ++it) { + if (TCastToConstPtr(mgr.GetObjectById(it->second))) { x354_boundsTrigger = it->second; + } + } } } } void CScriptPlatform::AddRider(std::vector& riders, TUniqueId riderId, const CPhysicsActor* ridee, CStateManager& mgr) { - auto search = std::find_if(riders.begin(), riders.end(), [riderId](const SRiders& r) { return r.x0_uid == riderId; }); + const auto search = + std::find_if(riders.begin(), riders.end(), [riderId](const SRiders& r) { return r.x0_uid == riderId; }); if (search == riders.end()) { zeus::CTransform xf; - if (TCastToPtr act = mgr.ObjectById(riderId)) { + if (const TCastToPtr act = mgr.ObjectById(riderId)) { xf.origin = ridee->GetTransform().transposeRotate(act->GetTranslation() - ridee->GetTranslation()); mgr.SendScriptMsg(act.GetPtr(), ridee->GetUniqueId(), EScriptObjectMessage::AddPlatformRider); } @@ -382,33 +420,38 @@ void CScriptPlatform::AddRider(std::vector& riders, TUniqueId riderId, } void CScriptPlatform::AddSlave(TUniqueId id, CStateManager& mgr) { - auto search = std::find_if(x338_slavesDynamic.begin(), x338_slavesDynamic.end(), - [id](const SRiders& r) { return r.x0_uid == id; }); - if (search == x338_slavesDynamic.end()) { - if (TCastToPtr act = mgr.ObjectById(id)) { - act->AddMaterial(EMaterialTypes::PlatformSlave, mgr); - zeus::CTransform localXf = x34_transform.inverse() * act->GetTransform(); - x338_slavesDynamic.emplace_back(id, 0.166667f, localXf); - } + const auto search = std::find_if(x338_slavesDynamic.begin(), x338_slavesDynamic.end(), + [id](const SRiders& r) { return r.x0_uid == id; }); + if (search != x338_slavesDynamic.end()) { + return; + } + + if (const TCastToPtr act = mgr.ObjectById(id)) { + act->AddMaterial(EMaterialTypes::PlatformSlave, mgr); + const zeus::CTransform localXf = x34_transform.inverse() * act->GetTransform(); + x338_slavesDynamic.emplace_back(id, 0.166667f, localXf); } } TUniqueId CScriptPlatform::GetNext(TUniqueId uid, CStateManager& mgr) { - TCastToConstPtr nextWp = mgr.GetObjectById(uid); - if (!nextWp) + const TCastToConstPtr nextWp = mgr.GetObjectById(uid); + if (!nextWp) { return GetWaypoint(mgr); + } - TUniqueId next = nextWp->NextWaypoint(mgr); - if (TCastToConstPtr wp = mgr.GetObjectById(next)) + const TUniqueId next = nextWp->NextWaypoint(mgr); + if (const TCastToConstPtr wp = mgr.GetObjectById(next)) { x25c_currentSpeed = wp->GetSpeed(); + } return next; } TUniqueId CScriptPlatform::GetWaypoint(CStateManager& mgr) { for (const SConnection& conn : x20_conns) { - if (conn.x4_msg == EScriptObjectMessage::Follow) + if (conn.x4_msg == EScriptObjectMessage::Follow) { return mgr.GetIdForScript(conn.x8_objId); + } } return kInvalidUniqueId; @@ -420,27 +463,32 @@ void CScriptPlatform::SplashThink(const zeus::CAABox&, const CFluidPlane&, float zeus::CQuaternion CScriptPlatform::Move(float dt, CStateManager& mgr) { TUniqueId nextWaypoint = x25a_targetWaypoint; - if (x25a_targetWaypoint == kInvalidUniqueId) + if (x25a_targetWaypoint == kInvalidUniqueId) { nextWaypoint = GetNext(x258_currentWaypoint, mgr); - - TCastToPtr wp = mgr.ObjectById(nextWaypoint); - if (x258_currentWaypoint != kInvalidUniqueId && wp && !wp->GetActive()) { - nextWaypoint = GetNext(x258_currentWaypoint, mgr); - if (nextWaypoint == kInvalidUniqueId) - if (TCastToPtr wp = mgr.ObjectById(x258_currentWaypoint)) - if (wp->GetActive()) - nextWaypoint = x258_currentWaypoint; } - if (nextWaypoint == kInvalidUniqueId) + const TCastToConstPtr wp = mgr.ObjectById(nextWaypoint); + if (x258_currentWaypoint != kInvalidUniqueId && wp && !wp->GetActive()) { + nextWaypoint = GetNext(x258_currentWaypoint, mgr); + if (nextWaypoint == kInvalidUniqueId) { + if (const TCastToConstPtr wp2 = mgr.ObjectById(x258_currentWaypoint)) { + if (wp2->GetActive()) { + nextWaypoint = x258_currentWaypoint; + } + } + } + } + + if (nextWaypoint == kInvalidUniqueId) { return zeus::CQuaternion(); + } while (nextWaypoint != kInvalidUniqueId) { - if (TCastToPtr wp = mgr.ObjectById(nextWaypoint)) { - zeus::CVector3f platToWp = wp->GetTranslation() - GetTranslation(); + if (const TCastToPtr wp2 = mgr.ObjectById(nextWaypoint)) { + const zeus::CVector3f platToWp = wp2->GetTranslation() - GetTranslation(); if (zeus::close_enough(platToWp, zeus::skZero3f)) { x258_currentWaypoint = nextWaypoint; - mgr.SendScriptMsg(wp.GetPtr(), GetUniqueId(), EScriptObjectMessage::Arrived); + mgr.SendScriptMsg(wp2.GetPtr(), GetUniqueId(), EScriptObjectMessage::Arrived); if (zeus::close_enough(x25c_currentSpeed, 0.f, 0.02)) { nextWaypoint = GetNext(x258_currentWaypoint, mgr); x25c_currentSpeed = 0.f; @@ -449,19 +497,19 @@ zeus::CQuaternion CScriptPlatform::Move(float dt, CStateManager& mgr) { nextWaypoint = GetNext(x258_currentWaypoint, mgr); } - if (nextWaypoint != kInvalidUniqueId) + if (nextWaypoint != kInvalidUniqueId) { continue; + } mgr.SendScriptMsg(this, GetUniqueId(), EScriptObjectMessage::Stop); } if (zeus::close_enough(platToWp, zeus::skZero3f)) { - x270_dragDelta = wp->GetTranslation() - GetTranslation(); + x270_dragDelta = wp2->GetTranslation() - GetTranslation(); MoveToWR(GetTranslation(), dt); - } else if ((platToWp.normalized() * x25c_currentSpeed * dt).magSquared() > platToWp.magSquared()) { - x270_dragDelta = wp->GetTranslation() - GetTranslation(); - MoveToWR(wp->GetTranslation(), dt); + x270_dragDelta = wp2->GetTranslation() - GetTranslation(); + MoveToWR(wp2->GetTranslation(), dt); } else { x270_dragDelta = platToWp.normalized() * x25c_currentSpeed * dt; MoveToWR(GetTranslation() + x270_dragDelta, dt); @@ -470,15 +518,17 @@ zeus::CQuaternion CScriptPlatform::Move(float dt, CStateManager& mgr) { rstl::reserved_vector nearList; mgr.BuildColliderList(nearList, *this, GetMotionVolume(dt)); rstl::reserved_vector nonRiders; - for (TUniqueId id : nearList) - if (!IsRider(id) && !IsSlave(id)) + for (TUniqueId id : nearList) { + if (!IsRider(id) && !IsSlave(id)) { nonRiders.push_back(id); + } + } if (x356_26_detectCollision) { - CMotionState mState = PredictMotion(dt); + const CMotionState mState = PredictMotion(dt); MoveCollisionPrimitive(mState.x0_translation); - bool collision = CGameCollision::DetectDynamicCollisionBoolean(*GetCollisionPrimitive(), - GetPrimitiveTransform(), nonRiders, mgr); + const bool collision = CGameCollision::DetectDynamicCollisionBoolean(*GetCollisionPrimitive(), + GetPrimitiveTransform(), nonRiders, mgr); MoveCollisionPrimitive(zeus::skZero3f); if (collision || x356_27_squishedRider) { if (x356_26_detectCollision) { @@ -487,7 +537,7 @@ zeus::CQuaternion CScriptPlatform::Move(float dt, CStateManager& mgr) { break; } else { x356_27_squishedRider = false; - TUniqueId prevWaypoint = nextWaypoint; + const TUniqueId prevWaypoint = nextWaypoint; nextWaypoint = GetNext(nextWaypoint, mgr); if (x25a_targetWaypoint == nextWaypoint || x25a_targetWaypoint == prevWaypoint) { x260_moveDelay = 0.035f;