CWaypointExtra: Make use of ranged-for where applicable

Same behavior, less moving parts.
This commit is contained in:
Lioncash 2020-06-20 00:06:57 -04:00
parent 8c06309364
commit ef57260f0b
3 changed files with 33 additions and 35 deletions

View File

@ -3,6 +3,8 @@
#include "Core/Resource/Script/CLink.h" #include "Core/Resource/Script/CLink.h"
#include "Core/Scene/CScene.h" #include "Core/Scene/CScene.h"
#include <algorithm>
CSplinePathExtra::CSplinePathExtra(CScriptObject* pInstance, CScene* pScene, CScriptNode* pParent) CSplinePathExtra::CSplinePathExtra(CScriptObject* pInstance, CScene* pScene, CScriptNode* pParent)
: CScriptExtra(pInstance, pScene, pParent) : CScriptExtra(pInstance, pScene, pParent)
{ {

View File

@ -12,8 +12,8 @@ CWaypointExtra::CWaypointExtra(CScriptObject *pInstance, CScene *pScene, CScript
CWaypointExtra::~CWaypointExtra() CWaypointExtra::~CWaypointExtra()
{ {
for (auto it = mPaths.begin(); it != mPaths.end(); it++) for (auto& path : mPaths)
(*it)->RemoveWaypoint(this); path->RemoveWaypoint(this);
} }
void CWaypointExtra::CheckColor() void CWaypointExtra::CheckColor()
@ -24,16 +24,15 @@ void CWaypointExtra::CheckColor()
CSplinePathExtra *pPath = mPaths.front(); CSplinePathExtra *pPath = mPaths.front();
mColor = pPath->PathColor(); mColor = pPath->PathColor();
} }
// Fetch color from parent node's model (MP1/2/3) // Fetch color from parent node's model (MP1/2/3)
else if (mGame < EGame::DKCReturns) else if (mGame < EGame::DKCReturns)
{ {
CScriptNode *pScript = static_cast<CScriptNode*>(mpParent); auto *pScript = static_cast<CScriptNode*>(mpParent);
CModel *pModel = pScript->ActiveModel(); CModel *pModel = pScript->ActiveModel();
if (pModel && (pModel->GetMatSetCount() > 0) && (pModel->GetMatCount() > 0)) if (pModel != nullptr && (pModel->GetMatSetCount() > 0) && (pModel->GetMatCount() > 0))
{ {
CMaterial *pMat = pModel->GetMaterialByIndex(0, 0); const CMaterial *pMat = pModel->GetMaterialByIndex(0, 0);
mColor = pMat->Konst(0); mColor = pMat->Konst(0);
} }
} }
@ -47,28 +46,27 @@ void CWaypointExtra::CheckColor()
void CWaypointExtra::AddToSplinePath(CSplinePathExtra *pPath) void CWaypointExtra::AddToSplinePath(CSplinePathExtra *pPath)
{ {
for (auto it = mPaths.begin(); it != mPaths.end(); it++) const auto iter = std::find_if(mPaths.cbegin(), mPaths.cend(),
{ [pPath](const auto* entry) { return entry == pPath; });
if (*it == pPath)
if (iter != mPaths.cend())
return; return;
}
mPaths.push_back(pPath); mPaths.push_back(pPath);
if (mPaths.size() == 1) if (mPaths.size() == 1)
CheckColor(); CheckColor();
} }
void CWaypointExtra::RemoveFromSplinePath(CSplinePathExtra *pPath) void CWaypointExtra::RemoveFromSplinePath(const CSplinePathExtra *pPath)
{ {
for (auto it = mPaths.begin(); it != mPaths.end(); ++it) const auto iter = std::find_if(mPaths.cbegin(), mPaths.cend(),
{ [pPath](const auto* entry) { return entry == pPath; });
if (*it == pPath)
{ if (iter == mPaths.cend())
mPaths.erase(it); return;
mPaths.erase(iter);
CheckColor(); CheckColor();
break;
}
}
} }
void CWaypointExtra::BuildLinks() void CWaypointExtra::BuildLinks()
@ -122,7 +120,7 @@ bool CWaypointExtra::IsPathLink(const CLink *pLink) const
{ {
const CScriptNode *pNode = mpScene->NodeForInstanceID(pLink->ReceiverID()); const CScriptNode *pNode = mpScene->NodeForInstanceID(pLink->ReceiverID());
if (pNode) if (pNode != nullptr)
return pNode->Instance()->ObjectTypeID() == mpInstance->ObjectTypeID(); return pNode->Instance()->ObjectTypeID() == mpInstance->ObjectTypeID();
} }
@ -131,24 +129,22 @@ bool CWaypointExtra::IsPathLink(const CLink *pLink) const
void CWaypointExtra::GetLinkedWaypoints(std::list<CWaypointExtra*>& rOut) void CWaypointExtra::GetLinkedWaypoints(std::list<CWaypointExtra*>& rOut)
{ {
if (!mLinksBuilt) BuildLinks(); if (!mLinksBuilt)
BuildLinks();
for (uint32 iLink = 0; iLink < mLinks.size(); iLink++) for (auto& link : mLinks)
{ {
const SWaypointLink& rkLink = mLinks[iLink]; rOut.push_back(static_cast<CWaypointExtra*>(link.pWaypoint->Extra()));
CWaypointExtra *pExtra = static_cast<CWaypointExtra*>(rkLink.pWaypoint->Extra());
rOut.push_back(pExtra);
} }
} }
void CWaypointExtra::OnTransformed() void CWaypointExtra::OnTransformed()
{ {
for (uint32 iLink = 0; iLink < mLinks.size(); iLink++) for (auto& link : mLinks)
{ {
SWaypointLink& rLink = mLinks[iLink]; link.LineAABB = CAABox::Infinite();
rLink.LineAABB = CAABox::Infinite(); link.LineAABB.ExpandBounds(AbsolutePosition());
rLink.LineAABB.ExpandBounds(AbsolutePosition()); link.LineAABB.ExpandBounds(link.pWaypoint->AbsolutePosition());
rLink.LineAABB.ExpandBounds(rLink.pWaypoint->AbsolutePosition());
} }
} }
@ -163,7 +159,7 @@ void CWaypointExtra::AddToRenderer(CRenderer *pRenderer, const SViewInfo& rkView
// won't work properly because we haven't finished loading the scene yet. // won't work properly because we haven't finished loading the scene yet.
if (!mLinksBuilt) BuildLinks(); if (!mLinksBuilt) BuildLinks();
if (!rkViewInfo.GameMode && (rkViewInfo.ShowFlags & EShowFlag::ObjectGeometry) && mpParent->IsVisible() && !mpParent->IsSelected()) if (!rkViewInfo.GameMode && ((rkViewInfo.ShowFlags & EShowFlag::ObjectGeometry) != 0) && mpParent->IsVisible() && !mpParent->IsSelected())
{ {
for (uint32 iLink = 0; iLink < mLinks.size(); iLink++) for (uint32 iLink = 0; iLink < mLinks.size(); iLink++)
{ {
@ -188,5 +184,5 @@ void CWaypointExtra::Draw(FRenderOptions /*Options*/, int ComponentIndex, ERende
CColor CWaypointExtra::TevColor() CColor CWaypointExtra::TevColor()
{ {
return (mGame < EGame::DKCReturns ? CColor::White() : mColor); return mGame < EGame::DKCReturns ? CColor::White() : mColor;
} }

View File

@ -24,7 +24,7 @@ public:
~CWaypointExtra(); ~CWaypointExtra();
void CheckColor(); void CheckColor();
void AddToSplinePath(CSplinePathExtra *pPath); void AddToSplinePath(CSplinePathExtra *pPath);
void RemoveFromSplinePath(CSplinePathExtra *pPath); void RemoveFromSplinePath(const CSplinePathExtra *pPath);
void BuildLinks(); void BuildLinks();
bool IsPathLink(const CLink *pLink) const; bool IsPathLink(const CLink *pLink) const;
void GetLinkedWaypoints(std::list<CWaypointExtra*>& rOut); void GetLinkedWaypoints(std::list<CWaypointExtra*>& rOut);