From 7b40eec19c27cd1457e17764c3da62be5d085a8e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 12 Jun 2020 16:55:32 -0400 Subject: [PATCH] CScriptObject: Collapse for loops into ranged for loops While we're at it, eliminate sign conversion warnings. --- src/Core/Resource/Script/CScriptObject.cpp | 36 ++++++++++++---------- src/Core/Resource/Script/CScriptObject.h | 4 +-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Core/Resource/Script/CScriptObject.cpp b/src/Core/Resource/Script/CScriptObject.cpp index b8b78118..a7d5e47c 100644 --- a/src/Core/Resource/Script/CScriptObject.cpp +++ b/src/Core/Resource/Script/CScriptObject.cpp @@ -108,7 +108,8 @@ void CScriptObject::SetLayer(CScriptLayer *pLayer, uint32 NewLayerIndex) uint32 CScriptObject::LayerIndex() const { - if (!mpLayer) return -1; + if (!mpLayer) + return UINT32_MAX; for (uint32 iInst = 0; iInst < mpLayer->NumInstances(); iInst++) { @@ -116,7 +117,7 @@ uint32 CScriptObject::LayerIndex() const return iInst; } - return -1; + return UINT32_MAX; } bool CScriptObject::HasNearVisibleActivation() const @@ -127,20 +128,20 @@ bool CScriptObject::HasNearVisibleActivation() const * instance has a "Near Visible" activation, which is typically done via a trigger that activates the object on * InternalState04/05/06 (usually through a relay). */ std::list Relays; - bool IsRelay = (ObjectTypeID() == 0x53524C59); + const bool IsRelay = ObjectTypeID() == 0x53524C59; + + if (mIsCheckingNearVisibleActivation) + return false; - if (mIsCheckingNearVisibleActivation) return false; mIsCheckingNearVisibleActivation = true; - for (uint32 iLink = 0; iLink < mInLinks.size(); iLink++) + for (const auto* pLink : mInLinks) { - CLink *pLink = mInLinks[iLink]; - // Check for trigger activation if (pLink->State() == FOURCC('IS04') || pLink->State() == FOURCC('IS05') || pLink->State() == FOURCC('IS06')) { - if ( (!IsRelay && pLink->Message() == FOURCC('ACTV')) || - (IsRelay && pLink->Message() == FOURCC('ACTN')) ) + if ((!IsRelay && pLink->Message() == FOURCC('ACTV')) || + (IsRelay && pLink->Message() == FOURCC('ACTN'))) { CScriptObject *pObj = pLink->Sender(); @@ -167,25 +168,26 @@ bool CScriptObject::HasNearVisibleActivation() const } // Check whether any of the relays have a near visible activation - for (auto it = Relays.begin(); it != Relays.end(); it++) + const bool nearVisible = std::any_of(Relays.cbegin(), Relays.cend(), + [](const auto* relay) { return relay->HasNearVisibleActivation(); }); + if (nearVisible) { - if ((*it)->HasNearVisibleActivation()) - { - mIsCheckingNearVisibleActivation = false; - return true; - } + mIsCheckingNearVisibleActivation = false; + return true; } mIsCheckingNearVisibleActivation = false; return false; } -void CScriptObject::AddLink(ELinkType Type, CLink *pLink, uint32 Index /*= -1*/) +void CScriptObject::AddLink(ELinkType Type, CLink *pLink, uint32 Index) { std::vector *pLinkVec = (Type == ELinkType::Incoming ? &mInLinks : &mOutLinks); - if (Index == -1 || Index == pLinkVec->size()) + if (Index == UINT32_MAX || Index == pLinkVec->size()) + { pLinkVec->push_back(pLink); + } else { auto it = pLinkVec->begin(); diff --git a/src/Core/Resource/Script/CScriptObject.h b/src/Core/Resource/Script/CScriptObject.h index d9b30433..da3f72f1 100644 --- a/src/Core/Resource/Script/CScriptObject.h +++ b/src/Core/Resource/Script/CScriptObject.h @@ -73,11 +73,11 @@ public: void EvaluateCollisionModel(); void EvaluateVolume(); bool IsEditorProperty(IProperty *pProp); - void SetLayer(CScriptLayer *pLayer, uint32 NewLayerIndex = -1); + void SetLayer(CScriptLayer *pLayer, uint32 NewLayerIndex = UINT32_MAX); uint32 LayerIndex() const; bool HasNearVisibleActivation() const; - void AddLink(ELinkType Type, CLink *pLink, uint32 Index = -1); + void AddLink(ELinkType Type, CLink *pLink, uint32 Index = UINT32_MAX); void RemoveLink(ELinkType Type, CLink *pLink); void BreakAllLinks();