From 304542afe22b3d5b94beede933a959347d1cef85 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 21 Jun 2015 16:22:09 +0200 Subject: [PATCH] Some syntax sugar for iterating through all objects --- src/graphics/engine/camera.cpp | 12 ++-- src/graphics/engine/lightning.cpp | 3 +- src/graphics/engine/particle.cpp | 9 +-- src/graphics/engine/pyro.cpp | 39 ++++++----- src/object/auto/autobase.cpp | 72 ++++++++------------ src/object/auto/autoconvert.cpp | 42 +++++------- src/object/auto/autoderrick.cpp | 36 ++++------ src/object/auto/autodestroyer.cpp | 19 ++---- src/object/auto/autoegg.cpp | 28 +++----- src/object/auto/autoenergy.cpp | 39 ++++------- src/object/auto/autofactory.cpp | 77 ++++++++------------- src/object/auto/automush.cpp | 19 ++---- src/object/auto/autonest.cpp | 38 ++++------- src/object/auto/autonuclear.cpp | 17 ++--- src/object/auto/autopara.cpp | 33 ++++----- src/object/auto/autoradar.cpp | 31 ++++----- src/object/auto/autorepair.cpp | 26 +++---- src/object/auto/autosafe.cpp | 108 +++++++++++------------------- src/object/auto/autostation.cpp | 19 ++---- src/object/auto/autotower.cpp | 40 +++++------ src/object/object.cpp | 22 +++--- src/object/object_manager.cpp | 24 +++---- src/object/object_manager.h | 61 +++++++++++++++-- src/object/robotmain.cpp | 96 +++++++++----------------- src/object/task/taskbuild.cpp | 15 ++--- src/object/task/taskflag.cpp | 14 ++-- src/object/task/taskgoto.cpp | 77 ++++++++------------- src/object/task/taskmanip.cpp | 48 +++++-------- src/object/task/taskreset.cpp | 19 ++---- src/object/task/taskshield.cpp | 19 ++---- src/object/task/tasktake.cpp | 19 ++---- src/object/task/taskterraform.cpp | 7 +- src/physics/physics.cpp | 5 +- src/ui/mainshort.cpp | 7 +- src/ui/target.cpp | 38 +++++------ 35 files changed, 468 insertions(+), 710 deletions(-) diff --git a/src/graphics/engine/camera.cpp b/src/graphics/engine/camera.cpp index 035874db..cb7b7fca 100644 --- a/src/graphics/engine/camera.cpp +++ b/src/graphics/engine/camera.cpp @@ -243,9 +243,8 @@ void CCamera::SetType(CameraType type) if ( (m_type == CAM_TYPE_BACK) && m_transparency ) { - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck()) continue; // battery or cargo? @@ -888,11 +887,9 @@ bool CCamera::IsCollisionBack(Math::Vector &eye, Math::Vector lookat) max.z = Math::Max(m_actualEye.z, m_actualLookat.z); m_transparency = false; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if (obj->GetTruck()) continue; // battery or cargo? SetTransparency(obj, 0.0f); // opaque object @@ -965,9 +962,8 @@ bool CCamera::IsCollisionBack(Math::Vector &eye, Math::Vector lookat) bool CCamera::IsCollisionFix(Math::Vector &eye, Math::Vector lookat) { - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj == m_cameraObj) continue; ObjectType type = obj->GetType(); diff --git a/src/graphics/engine/lightning.cpp b/src/graphics/engine/lightning.cpp index 36a5f4a5..25709842 100644 --- a/src/graphics/engine/lightning.cpp +++ b/src/graphics/engine/lightning.cpp @@ -317,9 +317,8 @@ CObject* CLightning::SearchObject(Math::Vector pos) // Seeking the object closest to the point of impact of lightning. CObject* bestObj = 0; float min = 100000.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); if (!obj->GetActif()) continue; // inactive object? if (obj->GetTruck() != nullptr) continue; // object transported? diff --git a/src/graphics/engine/particle.cpp b/src/graphics/engine/particle.cpp index 75cc37b4..0410bdb9 100644 --- a/src/graphics/engine/particle.cpp +++ b/src/graphics/engine/particle.cpp @@ -3658,9 +3658,8 @@ CObject* CParticle::SearchObjectGun(Math::Vector old, Math::Vector pos, CObject* best = 0; bool shield = false; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); if (!obj->GetActif()) continue; // inactive? if (obj == father) continue; @@ -3781,11 +3780,9 @@ CObject* CParticle::SearchObjectRay(Math::Vector pos, Math::Vector goal, box2.x += min; box2.y += min; box2.z += min; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if (!obj->GetActif()) continue; // inactive? if (obj == father) continue; diff --git a/src/graphics/engine/pyro.cpp b/src/graphics/engine/pyro.cpp index 5597eac1..19ddd31b 100644 --- a/src/graphics/engine/pyro.cpp +++ b/src/graphics/engine/pyro.cpp @@ -2211,14 +2211,12 @@ void CPyro::FallStart() CObject* CPyro::FallSearchBeeExplo() { Math::Vector iPos; - float iRadius; + float iRadius = 0.0f; m_object->GetCrashSphere(0, iPos, iRadius); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - CObject* pObj = it.second.get(); - ObjectType oType = pObj->GetType(); + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { + ObjectType oType = obj->GetType(); if ( oType != OBJECT_HUMAN && oType != OBJECT_MOBILEfa && oType != OBJECT_MOBILEta && @@ -2268,40 +2266,41 @@ CObject* CPyro::FallSearchBeeExplo() oType != OBJECT_POWER && oType != OBJECT_ATOMIC ) continue; - if ( pObj->GetTruck() != 0 ) continue; // object transported? + if ( obj->GetTruck() != nullptr ) continue; // object transported? - Math::Vector oPos = pObj->GetPosition(0); + Math::Vector oPos = obj->GetPosition(0); - float distance; - - float shieldRadius = pObj->GetShieldRadius(); + float shieldRadius = obj->GetShieldRadius(); if ( shieldRadius > 0.0f ) { - distance = Math::Distance(oPos, iPos); - if (distance <= shieldRadius) return pObj; + float distance = Math::Distance(oPos, iPos); + if (distance <= shieldRadius) + return obj; } if ( oType == OBJECT_BASE ) { - distance = Math::Distance(oPos, iPos); - if (distance < 25.0f) return pObj; + float distance = Math::Distance(oPos, iPos); + if (distance < 25.0f) + return obj; } // Test the center of the object, which is necessary for objects // that have no sphere in the center (station). - distance = Math::Distance(oPos, iPos)-4.0f; - if (distance < 5.0f) return pObj; + float distance = Math::Distance(oPos, iPos)-4.0f; + if (distance < 5.0f) + return obj; // Test with all spheres of the object. Math::Vector ooPos; - float ooRadius; + float ooRadius = 0.0f; int j = 0; - while (pObj->GetCrashSphere(j++, ooPos, ooRadius)) + while (obj->GetCrashSphere(j++, ooPos, ooRadius)) { distance = Math::Distance(ooPos, iPos); if (distance <= iRadius+ooRadius) { - return pObj; + return obj; } } } diff --git a/src/object/auto/autobase.cpp b/src/object/auto/autobase.cpp index 032b1aae..5acc4de6 100644 --- a/src/object/auto/autobase.cpp +++ b/src/object/auto/autobase.cpp @@ -1237,35 +1237,28 @@ void CAutoBase::UpdateInterface() // Freeze or frees all cargo. -void CAutoBase::FreezeCargo(bool bFreeze) +void CAutoBase::FreezeCargo(bool freeze) { - CObject* pObj; - CPhysics* physics; - Math::Vector oPos; - float dist; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + obj->SetCargo(false); - pObj->SetCargo(false); + if ( obj == m_object ) continue; // yourself? + if ( obj->GetTruck() != nullptr ) continue; // transport object? - if ( pObj == m_object ) continue; // yourself? - if ( pObj->GetTruck() != 0 ) continue; // transport object? - - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(m_pos, oPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(m_pos, oPos); if ( dist < 32.0f ) { - if ( bFreeze ) + if ( freeze ) { - pObj->SetCargo(true); + obj->SetCargo(true); } - physics = pObj->GetPhysics(); - if ( physics != 0 ) + CPhysics* physics = obj->GetPhysics(); + if ( physics != nullptr ) { - physics->SetFreeze(bFreeze); + physics->SetFreeze(freeze); } } } @@ -1275,23 +1268,18 @@ void CAutoBase::FreezeCargo(bool bFreeze) void CAutoBase::MoveCargo() { - CObject* pObj; - Math::Vector oPos, sPos; + Math::Vector sPos = m_object->GetPosition(0); - sPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetCargo() ) continue; - if ( !pObj->GetCargo() ) continue; - - oPos = pObj->GetPosition(0); + Math::Vector oPos = obj->GetPosition(0); oPos.y = sPos.y+30.0f; - oPos.y += pObj->GetCharacter()->height; + oPos.y += obj->GetCharacter()->height; oPos.x += sPos.x-m_lastPos.x; oPos.z += sPos.z-m_lastPos.z; - pObj->SetPosition(0, oPos); + obj->SetPosition(0, oPos); } m_lastPos = sPos; @@ -1302,26 +1290,20 @@ void CAutoBase::MoveCargo() Error CAutoBase::CheckCloseDoor() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - float oRad, dist; - int j; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj == m_object ) continue; // yourself? + if ( !obj->GetActif() ) continue; // inactive? - if ( pObj == m_object ) continue; // yourself? - if ( !pObj->GetActif() ) continue; // inactive? - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type == OBJECT_PORTICO ) continue; - j = 0; - while ( pObj->GetCrashSphere(j++, oPos, oRad) ) + int j = 0; + Math::Vector oPos; + float oRad = 0.0f; + while ( obj->GetCrashSphere(j++, oPos, oRad) ) { - dist = Math::DistanceProjected(m_pos, oPos); + float dist = Math::DistanceProjected(m_pos, oPos); if ( dist+oRad > 32.0f && dist-oRad < 72.0f ) { diff --git a/src/object/auto/autoconvert.cpp b/src/object/auto/autoconvert.cpp index 9cd7e461..40a1d648 100644 --- a/src/object/auto/autoconvert.cpp +++ b/src/object/auto/autoconvert.cpp @@ -395,46 +395,32 @@ bool CAutoConvert::Read(CLevelParserLine* line) CObject* CAutoConvert::SearchStone(ObjectType type) { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType oType; - float dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( oType != type ) continue; - if ( pObj->GetTruck() != 0 ) continue; + if ( obj->GetTruck() != nullptr ) continue; - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, cPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, cPos); - if ( dist <= 5.0f ) return pObj; + if ( dist <= 5.0f ) return obj; } - return 0; + return nullptr; } // Search if a vehicle is too close. bool CAutoConvert::SearchVehicle() { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType type; - float oRadius, dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && @@ -476,8 +462,10 @@ bool CAutoConvert::SearchVehicle() type != OBJECT_BEE && type != OBJECT_WORM ) continue; - if ( !pObj->GetCrashSphere(0, oPos, oRadius) ) continue; - dist = Math::Distance(oPos, cPos)-oRadius; + Math::Vector oPos; + float oRadius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, oRadius) ) continue; + float dist = Math::Distance(oPos, cPos)-oRadius; if ( dist < 8.0f ) return true; } diff --git a/src/object/auto/autoderrick.cpp b/src/object/auto/autoderrick.cpp index b95d32c3..76eabf9c 100644 --- a/src/object/auto/autoderrick.cpp +++ b/src/object/auto/autoderrick.cpp @@ -461,47 +461,35 @@ bool CAutoDerrick::Read(CLevelParserLine* line) CObject* CAutoDerrick::SearchFret() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type == OBJECT_DERRICK ) continue; - oPos = pObj->GetPosition(0); + Math::Vector oPos = obj->GetPosition(0); if ( oPos.x == m_fretPos.x && - oPos.z == m_fretPos.z ) return pObj; + oPos.z == m_fretPos.z ) return obj; } - return 0; + return nullptr; } // Seeks if a site is free. bool CAutoDerrick::SearchFree(Math::Vector pos) { - CObject* pObj; - Math::Vector sPos; - ObjectType type; - float sRadius, distance; - int j; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type == OBJECT_DERRICK ) continue; - j = 0; - while ( pObj->GetCrashSphere(j++, sPos, sRadius) ) + int j = 0; + Math::Vector sPos; + float sRadius = 0.0f; + while ( obj->GetCrashSphere(j++, sPos, sRadius) ) { - distance = Math::Distance(sPos, pos); + float distance = Math::Distance(sPos, pos); distance -= sRadius; if ( distance < 2.0f ) return false; // location occupied } diff --git a/src/object/auto/autodestroyer.cpp b/src/object/auto/autodestroyer.cpp index bb958210..27356a7a 100644 --- a/src/object/auto/autodestroyer.cpp +++ b/src/object/auto/autodestroyer.cpp @@ -272,18 +272,11 @@ bool CAutoDestroyer::CreateInterface(bool bSelect) CObject* CAutoDestroyer::SearchPlastic() { - CObject* pObj; - Math::Vector sPos, oPos; - ObjectType type; - float dist; + Math::Vector sPos = m_object->GetPosition(0); - sPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); //if ( type != OBJECT_SCRAP4 && // type != OBJECT_SCRAP5 ) continue; if ( type != OBJECT_FRET && @@ -334,9 +327,9 @@ CObject* CAutoDestroyer::SearchPlastic() type != OBJECT_WORM ) continue; - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, sPos); - if ( dist <= 5.0f ) return pObj; + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, sPos); + if ( dist <= 5.0f ) return obj; } return nullptr; diff --git a/src/object/auto/autoegg.cpp b/src/object/auto/autoegg.cpp index 13703203..a3ce8d88 100644 --- a/src/object/auto/autoegg.cpp +++ b/src/object/auto/autoegg.cpp @@ -271,36 +271,28 @@ Error CAutoEgg::GetError() CObject* CAutoEgg::SearchAlien() { - CObject* pObj; - CObject* pBest; - Math::Vector cPos, oPos; - ObjectType type; - float dist, min; - - cPos = m_object->GetPosition(0); - min = 100000.0f; - pBest = 0; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + Math::Vector cPos = m_object->GetPosition(0); + float min = 100000.0f; + CObject* best = nullptr; + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj->GetTruck() != nullptr ) continue; - if ( pObj->GetTruck() != 0 ) continue; - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_ANT && type != OBJECT_BEE && type != OBJECT_SPIDER && type != OBJECT_WORM ) continue; - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(oPos, cPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(oPos, cPos); if ( dist < 8.0f && dist < min ) { min = dist; - pBest = pObj; + best = obj; } } - return pBest; + return best; } diff --git a/src/object/auto/autoenergy.cpp b/src/object/auto/autoenergy.cpp index 449b553c..634c414f 100644 --- a/src/object/auto/autoenergy.cpp +++ b/src/object/auto/autoenergy.cpp @@ -394,18 +394,11 @@ CObject* CAutoEnergy::SearchMetal() bool CAutoEnergy::SearchVehicle() { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType type; - float oRadius, dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && @@ -440,8 +433,10 @@ bool CAutoEnergy::SearchVehicle() type != OBJECT_BEE && type != OBJECT_WORM ) continue; - if ( !pObj->GetCrashSphere(0, oPos, oRadius) ) continue; - dist = Math::Distance(oPos, cPos)-oRadius; + Math::Vector oPos; + float oRadius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, oRadius) ) continue; + float dist = Math::Distance(oPos, cPos)-oRadius; if ( dist < 10.0f ) return true; } @@ -468,30 +463,24 @@ void CAutoEnergy::CreatePower() CObject* CAutoEnergy::SearchPower() { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType type; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetLock() ) continue; - if ( !pObj->GetLock() ) continue; - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_POWER ) continue; - oPos = pObj->GetPosition(0); + Math::Vector oPos = obj->GetPosition(0); if ( oPos.x == cPos.x && oPos.z == cPos.z ) { - return pObj; + return obj; } } - return 0; + return nullptr; } diff --git a/src/object/auto/autofactory.cpp b/src/object/auto/autofactory.cpp index dcaa9764..04e023ec 100644 --- a/src/object/auto/autofactory.cpp +++ b/src/object/auto/autofactory.cpp @@ -547,25 +547,18 @@ bool CAutoFactory::Read(CLevelParserLine* line) CObject* CAutoFactory::SearchFret() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - float dist; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_METAL ) continue; - if ( pObj->GetTruck() != 0 ) continue; - - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, m_fretPos); - - if ( dist < 8.0f ) return pObj; + if ( obj->GetTruck() != nullptr ) continue; + + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, m_fretPos); + + if ( dist < 8.0f ) return obj; } - + return 0; } @@ -573,18 +566,11 @@ CObject* CAutoFactory::SearchFret() bool CAutoFactory::NearestVehicle() { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType type; - float oRadius, dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && @@ -619,8 +605,10 @@ bool CAutoFactory::NearestVehicle() type != OBJECT_BEE && type != OBJECT_WORM ) continue; - if ( !pObj->GetCrashSphere(0, oPos, oRadius) ) continue; - dist = Math::Distance(oPos, cPos)-oRadius; + Math::Vector oPos; + float oRadius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, oRadius) ) continue; + float dist = Math::Distance(oPos, cPos)-oRadius; if ( dist < 10.0f ) return true; } @@ -680,29 +668,22 @@ bool CAutoFactory::CreateVehicle() // Seeking the vehicle during manufacture. CObject* CAutoFactory::SearchVehicle() -{ - CObject* pObj; - Math::Vector oPos; - ObjectType type; - float dist; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) +{ + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - if ( !pObj->GetLock() ) continue; - - type = pObj->GetType(); + if ( !obj->GetLock() ) continue; + + ObjectType type = obj->GetType(); if ( type != m_type ) continue; - if ( pObj->GetTruck() != 0 ) continue; - - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, m_fretPos); - - if ( dist < 8.0f ) return pObj; + if ( obj->GetTruck() != nullptr ) continue; + + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, m_fretPos); + + if ( dist < 8.0f ) return obj; } - - return 0; + + return nullptr; } // Creates all the interface when the object is selected. diff --git a/src/object/auto/automush.cpp b/src/object/auto/automush.cpp index fb5f02d0..cb73e704 100644 --- a/src/object/auto/automush.cpp +++ b/src/object/auto/automush.cpp @@ -225,20 +225,13 @@ bool CAutoMush::EventProcess(const Event &event) bool CAutoMush::SearchTarget() { - CObject* pObj; - Math::Vector iPos, oPos; - ObjectType type; - float dist; + Math::Vector iPos = m_object->GetPosition(0); - iPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj->GetLock() ) continue; - if ( pObj->GetLock() ) continue; - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && type != OBJECT_MOBILEwa && @@ -282,8 +275,8 @@ bool CAutoMush::SearchTarget() type != OBJECT_PARA && type != OBJECT_HUMAN ) continue; - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, iPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, iPos); if ( dist < 50.0f ) return true; } diff --git a/src/object/auto/autonest.cpp b/src/object/auto/autonest.cpp index 17b2fc4a..f95e0fe8 100644 --- a/src/object/auto/autonest.cpp +++ b/src/object/auto/autonest.cpp @@ -148,23 +148,17 @@ bool CAutoNest::EventProcess(const Event &event) bool CAutoNest::SearchFree(Math::Vector pos) { - CObject* pObj; - Math::Vector sPos; - ObjectType type; - float sRadius, distance; - int j; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type == OBJECT_NEST ) continue; - j = 0; - while ( pObj->GetCrashSphere(j++, sPos, sRadius) ) + int j = 0; + Math::Vector sPos; + float sRadius = 0.0f; + while ( obj->GetCrashSphere(j++, sPos, sRadius) ) { - distance = Math::Distance(sPos, pos); + float distance = Math::Distance(sPos, pos); distance -= sRadius; if ( distance < 2.0f ) return false; // location occupied } @@ -186,28 +180,22 @@ void CAutoNest::CreateFret(Math::Vector pos, float angle, ObjectType type) CObject* CAutoNest::SearchFret() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetLock() ) continue; - if ( !pObj->GetLock() ) continue; - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_BULLET ) continue; - oPos = pObj->GetPosition(0); + Math::Vector oPos = obj->GetPosition(0); if ( oPos.x == m_fretPos.x && oPos.z == m_fretPos.z ) { - return pObj; + return obj; } } - return 0; + return nullptr; } diff --git a/src/object/auto/autonuclear.cpp b/src/object/auto/autonuclear.cpp index 5961a120..c1444b97 100644 --- a/src/object/auto/autonuclear.cpp +++ b/src/object/auto/autonuclear.cpp @@ -331,16 +331,9 @@ CObject* CAutoNuclear::SearchUranium() bool CAutoNuclear::SearchVehicle() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - float oRadius, dist; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && @@ -375,8 +368,10 @@ bool CAutoNuclear::SearchVehicle() type != OBJECT_BEE && type != OBJECT_WORM ) continue; - if ( !pObj->GetCrashSphere(0, oPos, oRadius) ) continue; - dist = Math::Distance(oPos, m_pos)-oRadius; + Math::Vector oPos; + float oRadius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, oRadius) ) continue; + float dist = Math::Distance(oPos, m_pos)-oRadius; if ( dist < 10.0f ) return true; } diff --git a/src/object/auto/autopara.cpp b/src/object/auto/autopara.cpp index d95552c0..61b324c6 100644 --- a/src/object/auto/autopara.cpp +++ b/src/object/auto/autopara.cpp @@ -243,42 +243,35 @@ Error CAutoPara::GetError() void CAutoPara::ChargeObject(float rTime) { - CObject* pObj; - CObject* power; - Math::Vector sPos, oPos; - float dist, energy; + Math::Vector sPos = m_object->GetPosition(0); - sPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, sPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, sPos); if ( dist > 20.0f ) continue; - if ( pObj->GetTruck() == 0 && pObj->GetType() == OBJECT_POWER ) + if ( obj->GetTruck() == nullptr && obj->GetType() == OBJECT_POWER ) { - energy = pObj->GetEnergy(); + float energy = obj->GetEnergy(); energy += rTime/2.0f; if ( energy > 1.0f ) energy = 1.0f; - pObj->SetEnergy(energy); + obj->SetEnergy(energy); } - power = pObj->GetPower(); - if ( power != 0 && power->GetType() == OBJECT_POWER ) + CObject* power = obj->GetPower(); + if ( power != nullptr && power->GetType() == OBJECT_POWER ) { - energy = power->GetEnergy(); + float energy = power->GetEnergy(); energy += rTime/2.0f; if ( energy > 1.0f ) energy = 1.0f; power->SetEnergy(energy); } - power = pObj->GetFret(); - if ( power != 0 && power->GetType() == OBJECT_POWER ) + power = obj->GetFret(); + if ( power != nullptr && power->GetType() == OBJECT_POWER ) { - energy = power->GetEnergy(); + float energy = power->GetEnergy(); energy += rTime/2.0f; if ( energy > 1.0f ) energy = 1.0f; power->SetEnergy(energy); diff --git a/src/object/auto/autoradar.cpp b/src/object/auto/autoradar.cpp index c7850d9b..66294540 100644 --- a/src/object/auto/autoradar.cpp +++ b/src/object/auto/autoradar.cpp @@ -264,23 +264,16 @@ void CAutoRadar::UpdateInterface() bool CAutoRadar::SearchEnemy(Math::Vector &pos) { - CObject* pObj; - CObject* pBest = 0; - Math::Vector iPos, oPos; - ObjectType oType; - float distance, min; - - iPos = m_object->GetPosition(0); - min = 1000000.0f; + Math::Vector iPos = m_object->GetPosition(0); + float min = 1000000.0f; m_totalDetect = 0; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + + CObject* best = nullptr; + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetActif() ) continue; - if ( !pObj->GetActif() ) continue; - - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( oType != OBJECT_ANT && oType != OBJECT_SPIDER && oType != OBJECT_BEE && @@ -289,19 +282,19 @@ bool CAutoRadar::SearchEnemy(Math::Vector &pos) m_totalDetect ++; - oPos = pObj->GetPosition(0); - distance = Math::Distance(oPos, iPos); + Math::Vector oPos = obj->GetPosition(0); + float distance = Math::Distance(oPos, iPos); if ( distance < min ) { min = distance; - pBest = pObj; + best = obj; } } UpdateInterface(); - if ( pBest == 0 ) return false; - pos = pBest->GetPosition(0); + if ( best == nullptr ) return false; + pos = best->GetPosition(0); return true; } diff --git a/src/object/auto/autorepair.cpp b/src/object/auto/autorepair.cpp index ceb4940d..7002aec7 100644 --- a/src/object/auto/autorepair.cpp +++ b/src/object/auto/autorepair.cpp @@ -234,19 +234,11 @@ bool CAutoRepair::CreateInterface(bool bSelect) CObject* CAutoRepair::SearchVehicle() { - CObject* pObj; - CPhysics* physics; - Math::Vector sPos, oPos; - ObjectType type; - float dist; + Math::Vector sPos = m_object->GetPosition(0); - sPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && type != OBJECT_MOBILEwa && @@ -275,15 +267,15 @@ CObject* CAutoRepair::SearchVehicle() type != OBJECT_MOBILEit && type != OBJECT_MOBILEdr ) continue; - physics = pObj->GetPhysics(); - if ( physics != 0 && !physics->GetLand() ) continue; // in flight? + CPhysics* physics = obj->GetPhysics(); + if ( physics != nullptr && !physics->GetLand() ) continue; // in flight? - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, sPos); - if ( dist <= 5.0f ) return pObj; + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, sPos); + if ( dist <= 5.0f ) return obj; } - return 0; + return nullptr; } diff --git a/src/object/auto/autosafe.cpp b/src/object/auto/autosafe.cpp index 514c7d63..503ce767 100644 --- a/src/object/auto/autosafe.cpp +++ b/src/object/auto/autosafe.cpp @@ -385,38 +385,32 @@ bool CAutoSafe::Read(CLevelParserLine* line) int CAutoSafe::CountKeys() { - CObject* pObj; - Math::Vector cPos, oPos; - Math::Point rot; - ObjectType oType; - float dist, angle, limit = 0.0f, cAngle, oAngle = 0.0f; - int i, index; + Math::Vector cPos = m_object->GetPosition(0); + float cAngle = m_object->GetAngleY(0); - cPos = m_object->GetPosition(0); - cAngle = m_object->GetAngleY(0); - - for ( index=0 ; index<4 ; index++ ) + for (int index = 0; index < 4; index++) { m_bKey[index] = false; m_keyPos[index] = cPos; } - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj->GetTruck() != nullptr ) continue; - if ( pObj->GetTruck() != 0 ) continue; - - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( oType != OBJECT_KEYa && oType != OBJECT_KEYb && oType != OBJECT_KEYc && oType != OBJECT_KEYd ) continue; - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(oPos, cPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(oPos, cPos); if ( dist > 20.0f ) continue; + float limit = 0.0f; + float oAngle = 0.0f; + int index = 0; if ( oType == OBJECT_KEYa ) { limit = Math::PI*1.0f; @@ -442,23 +436,23 @@ int CAutoSafe::CountKeys() index = 3; } - angle = Math::RotateAngle(oPos.x-cPos.x, oPos.z-cPos.z)+cAngle; + float angle = Math::RotateAngle(oPos.x-cPos.x, oPos.z-cPos.z)+cAngle; if ( !Math::TestAngle(angle, limit-8.0f*Math::PI/180.0f, limit+8.0f*Math::PI/180.0f) ) continue; // Key changes the shape of the base. - rot = Math::RotatePoint(Math::Point(cPos.x, cPos.z), limit-cAngle, Math::Point(cPos.x+16.0f, cPos.z)); + Math::Point rot = Math::RotatePoint(Math::Point(cPos.x, cPos.z), limit-cAngle, Math::Point(cPos.x+16.0f, cPos.z)); oPos.x = rot.x; oPos.z = rot.y; oPos.y = cPos.y+1.0f; - pObj->SetPosition(0, oPos); - pObj->SetAngleY(0, oAngle+cAngle); + obj->SetPosition(0, oPos); + obj->SetAngleY(0, oAngle+cAngle); m_keyPos[index] = oPos; m_bKey[index] = true; } - i = 0; - for ( index=0 ; index<4 ; index++ ) + int i = 0; + for (int index = 0; index < 4; index++) { if ( m_bKey[index] ) i++; } @@ -469,30 +463,23 @@ int CAutoSafe::CountKeys() void CAutoSafe::LockKeys() { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType oType; - float dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oType = pObj->GetType(); - if ( pObj->GetTruck() != 0 ) continue; + ObjectType oType = obj->GetType(); + if ( obj->GetTruck() != nullptr ) continue; if ( oType != OBJECT_KEYa && oType != OBJECT_KEYb && oType != OBJECT_KEYc && oType != OBJECT_KEYd ) continue; - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(oPos, cPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(oPos, cPos); if ( dist > 20.0f ) continue; - pObj->SetLock(true); + obj->SetLock(true); } } @@ -500,31 +487,24 @@ void CAutoSafe::LockKeys() void CAutoSafe::DownKeys(float progress) { - CObject* pObj; - Math::Vector cPos, oPos; - ObjectType oType; - float dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oType = pObj->GetType(); - if ( pObj->GetTruck() != 0 ) continue; + ObjectType oType = obj->GetType(); + if ( obj->GetTruck() != nullptr ) continue; if ( oType != OBJECT_KEYa && oType != OBJECT_KEYb && oType != OBJECT_KEYc && oType != OBJECT_KEYd ) continue; - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(oPos, cPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(oPos, cPos); if ( dist > 20.0f ) continue; oPos.y = cPos.y+1.0f-progress*2.2f; - pObj->SetPosition(0, oPos); + obj->SetPosition(0, oPos); } } @@ -538,10 +518,8 @@ void CAutoSafe::DeleteKeys() do { haveDeleted = false; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); - ObjectType oType = obj->GetType(); if ( obj->GetTruck() != nullptr ) continue; @@ -566,23 +544,17 @@ void CAutoSafe::DeleteKeys() CObject* CAutoSafe::SearchVehicle() { - CObject* pObj; - Math::Vector cPos, oPos; - float dist; + Math::Vector cPos = m_object->GetPosition(0); - cPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj == m_object ) continue; + if ( obj->GetTruck() != nullptr ) continue; - if ( pObj == m_object ) continue; - if ( pObj->GetTruck() != 0 ) continue; - - oPos = pObj->GetPosition(0); - dist = Math::DistanceProjected(oPos, cPos); - if ( dist <= 4.0f ) return pObj; + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::DistanceProjected(oPos, cPos); + if ( dist <= 4.0f ) return obj; } - return 0; + return nullptr; } diff --git a/src/object/auto/autostation.cpp b/src/object/auto/autostation.cpp index c427d525..09d688d9 100644 --- a/src/object/auto/autostation.cpp +++ b/src/object/auto/autostation.cpp @@ -241,18 +241,11 @@ bool CAutoStation::EventProcess(const Event &event) CObject* CAutoStation::SearchVehicle() { - CObject* pObj; - Math::Vector sPos, oPos; - ObjectType type; - float dist; + Math::Vector sPos = m_object->GetPosition(0); - sPos = m_object->GetPosition(0); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_MOBILEfa && type != OBJECT_MOBILEta && @@ -281,9 +274,9 @@ CObject* CAutoStation::SearchVehicle() type != OBJECT_MOBILEit && type != OBJECT_MOBILEdr ) continue; - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, sPos); - if ( dist <= 5.0f ) return pObj; + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, sPos); + if ( dist <= 5.0f ) return obj; } return 0; diff --git a/src/object/auto/autotower.cpp b/src/object/auto/autotower.cpp index 2006d7ab..6b8a47e9 100644 --- a/src/object/auto/autotower.cpp +++ b/src/object/auto/autotower.cpp @@ -267,53 +267,47 @@ bool CAutoTower::EventProcess(const Event &event) CObject* CAutoTower::SearchTarget(Math::Vector &impact) { - CObject* pObj; - CObject* pBest = 0; - CPhysics* physics; - Math::Vector iPos, oPos; - ObjectType oType; - float distance, min, radius, speed; + Math::Vector iPos = m_object->GetPosition(0); + float min = 1000000.0f; - iPos = m_object->GetPosition(0); - min = 1000000.0f; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + CObject* best = nullptr; + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( oType != OBJECT_MOTHER && oType != OBJECT_ANT && oType != OBJECT_SPIDER && oType != OBJECT_BEE && oType != OBJECT_WORM ) continue; - if ( !pObj->GetActif() ) continue; // inactive? + if ( !obj->GetActif() ) continue; // inactive? //? if ( g_researchDone & RESEARCH_QUICK ) if ( false ) { - physics = pObj->GetPhysics(); - if ( physics != 0 ) + CPhysics* physics = obj->GetPhysics(); + if ( physics != nullptr ) { - speed = fabs(physics->GetLinMotionX(MO_REASPEED)); + float speed = fabs(physics->GetLinMotionX(MO_REASPEED)); if ( speed > 20.0f ) continue; // moving too fast? } } - if ( !pObj->GetCrashSphere(0, oPos, radius) ) continue; - distance = Math::Distance(oPos, iPos); + Math::Vector oPos; + float radius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, radius) ) continue; + float distance = Math::Distance(oPos, iPos); if ( distance > TOWER_SCOPE ) continue; // too far if ( distance < min ) { min = distance; - pBest = pObj; + best = obj; } } - if ( pBest == 0 ) return 0; + if ( best == nullptr ) return nullptr; - impact = pBest->GetPosition(0); - return pBest; + impact = best->GetPosition(0); + return best; } diff --git a/src/object/object.cpp b/src/object/object.cpp index 373ecd04..ca913a1c 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -347,8 +347,6 @@ CObject::~CObject() void CObject::DeleteObject(bool bAll) { - CObject* pObj; - Gfx::CPyro* pPyro; if ( m_botVar != 0 ) { m_botVar->SetUserPtr(OBJECTDELETED); @@ -361,12 +359,10 @@ void CObject::DeleteObject(bool bAll) CInstanceManager* iMan = CInstanceManager::GetInstancePointer(); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); - pObj->DeleteDeselList(this); + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { + obj->DeleteDeselList(this); } if ( !bAll ) @@ -379,25 +375,25 @@ void CObject::DeleteObject(bool bAll) type == Gfx::CAM_TYPE_ONBOARD) && m_camera->GetControllingObject() == this ) { - pObj = m_main->SearchNearest(GetPosition(0), this); - if ( pObj == 0 ) + obj = m_main->SearchNearest(GetPosition(0), this); + if ( obj == 0 ) { m_camera->SetControllingObject(0); m_camera->SetType(Gfx::CAM_TYPE_FREE); } else { - m_camera->SetControllingObject(pObj); + m_camera->SetControllingObject(obj); m_camera->SetType(Gfx::CAM_TYPE_BACK); } } #endif for (int i=0 ; i<1000000 ; i++ ) { - pPyro = static_cast(iMan->SearchInstance(CLASS_PYRO, i)); - if ( pPyro == 0 ) break; + Gfx::CPyro* pyro = static_cast(iMan->SearchInstance(CLASS_PYRO, i)); + if ( pyro == nullptr ) break; - pPyro->CutObjectLink(this); // the object no longer exists + pyro->CutObjectLink(this); // the object no longer exists } if ( m_bSelect ) diff --git a/src/object/object_manager.cpp b/src/object/object_manager.cpp index 498718b7..92bcf233 100644 --- a/src/object/object_manager.cpp +++ b/src/object/object_manager.cpp @@ -54,10 +54,10 @@ bool CObjectManager::DeleteObject(CObject* instance) instance->DeleteObject(); - auto it = m_table.find(instance->GetID()); - if (it != m_table.end()) + auto it = m_objects.find(instance->GetID()); + if (it != m_objects.end()) { - m_table.erase(it); + m_objects.erase(it); return true; } @@ -66,27 +66,27 @@ bool CObjectManager::DeleteObject(CObject* instance) void CObjectManager::DeleteAllObjects() { - for (auto& it : m_table) + for (auto& it : m_objects) { bool all = true; it.second->DeleteObject(all); } - m_table.clear(); + m_objects.clear(); m_nextId = 0; } CObject* CObjectManager::GetObjectById(unsigned int id) { - if(m_table.count(id) == 0) return nullptr; - return m_table[id].get(); + if(m_objects.count(id) == 0) return nullptr; + return m_objects[id].get(); } CObject* CObjectManager::GetObjectByRank(unsigned int id) { - if(id >= m_table.size()) return nullptr; - auto it = m_table.begin(); + if(id >= m_objects.size()) return nullptr; + auto it = m_objects.begin(); for(unsigned int i = 0; i < id; i++, ++it); return it->second.get(); } @@ -108,7 +108,7 @@ CObject* CObjectManager::CreateObject(Math::Vector pos, m_nextId++; } - assert(m_table.find(id) == m_table.end()); + assert(m_objects.find(id) == m_objects.end()); ObjectCreateParams params; params.pos = pos; @@ -125,7 +125,7 @@ CObject* CObjectManager::CreateObject(Math::Vector pos, auto objectUPtr = m_objectFactory->CreateObject(params); CObject* objectPtr = objectUPtr.get(); - m_table[id] = std::move(objectUPtr); + m_objects[id] = std::move(objectUPtr); return objectPtr; } @@ -180,7 +180,7 @@ CObject* CObjectManager::Radar(CObject* pThis, Math::Vector thisPosition, float if ( !furthest ) best = 100000.0f; else best = 0.0f; pBest = nullptr; - for ( auto it = m_table.begin() ; it != m_table.end() ; ++it ) + for ( auto it = m_objects.begin() ; it != m_objects.end() ; ++it ) { pObj = it->second.get(); if ( pObj == pThis ) continue; // pThis may be nullptr but it doesn't matter diff --git a/src/object/object_manager.h b/src/object/object_manager.h index e56568e0..90e4b26b 100644 --- a/src/object/object_manager.h +++ b/src/object/object_manager.h @@ -54,6 +54,59 @@ enum RadarFilter }; using CObjectMap = std::map>; +using CObjectMapCIt = std::map>::const_iterator; + +class CObjectIteratorProxy +{ +public: + inline CObject* operator*() + { + return m_it->second.get(); + } + + inline void operator++() + { + ++m_it; + } + + inline bool operator!=(const CObjectIteratorProxy& other) + { + return m_it != other.m_it; + } + +private: + friend class CObjectContainerProxy; + + CObjectIteratorProxy(CObjectMapCIt it) + : m_it(it) + {} + +private: + CObjectMapCIt m_it; +}; + +class CObjectContainerProxy +{ +public: + inline CObjectIteratorProxy begin() const + { + return CObjectIteratorProxy(m_map.begin()); + } + inline CObjectIteratorProxy end() const + { + return CObjectIteratorProxy(m_map.end()); + } + +private: + friend class CObjectManager; + + inline CObjectContainerProxy(const CObjectMap& map) + : m_map(map) + {} + +private: + const CObjectMap& m_map; +}; /** * \class ObjectManager @@ -89,13 +142,13 @@ public: //! Finds object by id (CObject::GetID()) CObject* GetObjectById(unsigned int id); - //! Gets object by id in range <0; number of objects - 1) + //! Gets object by id in range <0; number of objects - 1> CObject* GetObjectByRank(unsigned int id); //! Returns all objects - inline const CObjectMap& GetAllObjects() + inline CObjectContainerProxy GetAllObjects() { - return m_table; + return CObjectContainerProxy(m_objects); } //! Finds an object, like radar() in CBot @@ -164,7 +217,7 @@ public: //@} protected: - CObjectMap m_table; + CObjectMap m_objects; std::unique_ptr m_objectFactory; int m_nextId; }; diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index a2674e5f..879f623d 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -1806,9 +1806,8 @@ CObject* CRobotMain::GetSelectObject() CObject* CRobotMain::DeselectAll() { CObject* prev = nullptr; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetSelect()) prev = obj; obj->SetSelect(false); } @@ -1962,9 +1961,8 @@ CObject* CRobotMain::SearchNearest(Math::Vector pos, CObject* exclu) { float min = 100000.0f; CObject* best = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj == exclu) continue; if (!IsSelectable(obj)) continue; @@ -1985,9 +1983,8 @@ CObject* CRobotMain::SearchNearest(Math::Vector pos, CObject* exclu) //! Returns the selected object CObject* CRobotMain::GetSelect() { - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetSelect()) return obj; } @@ -2003,11 +2000,9 @@ CObject* CRobotMain::SearchObject(ObjectType type) CObject* CRobotMain::DetectObject(Math::Point pos) { int objRank = m_engine->DetectObject(pos); - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { if (!obj->GetActif()) continue; CObject* truck = obj->GetTruck(); if (truck != nullptr) if (!truck->GetActif()) continue; @@ -2243,11 +2238,9 @@ void CRobotMain::HiliteClear() int rank = -1; m_engine->SetHighlightRank(&rank); // nothing more selected - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { obj->SetHilite(false); m_map->SetHighlight(0); m_short->SetHighlight(0); @@ -2404,9 +2397,8 @@ void CRobotMain::HelpObject() //! Change the mode of the camera void CRobotMain::ChangeCamera() { - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetSelect()) { if (obj->GetCameraLock()) return; @@ -2548,9 +2540,8 @@ void CRobotMain::RemoteCamera(float pan, float zoom, float rTime) //! Cancels the current movie void CRobotMain::AbortMovie() { - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); CAuto* automat = obj->GetAuto(); if (automat != 0) automat->Abort(); @@ -2635,9 +2626,8 @@ bool CRobotMain::EventFrame(const Event &event) if (!m_freePhoto) { // Advances all the robots, but not toto. - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (pm != nullptr) pm->UpdateObject(obj); if (obj->GetTruck() != nullptr) continue; ObjectType type = obj->GetType(); @@ -2647,9 +2637,8 @@ bool CRobotMain::EventFrame(const Event &event) obj->EventProcess(event); } // Advances all objects transported by robots. - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() == nullptr) continue; obj->EventProcess(event); } @@ -2806,11 +2795,9 @@ bool CRobotMain::EventObject(const Event &event) if (m_freePhoto) return true; m_resetCreate = false; - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { obj->EventProcess(event); } @@ -4203,9 +4190,8 @@ bool CRobotMain::TestGadgetQuantity(int rank) float CRobotMain::SearchNearestObject(Math::Vector center, CObject *exclu) { float min = 100000.0f; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (!obj->GetActif()) continue; // inactive? if (obj->GetTruck() != nullptr) continue; // object carries? if (obj == exclu) continue; @@ -4352,9 +4338,8 @@ void CRobotMain::ShowDropZone(CObject* metal, CObject* truck) // Calculates the maximum radius possible depending on other items. float oMax = 30.0f; // radius to build the biggest building float tMax; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (!obj->GetActif()) continue; // inactive? if (obj->GetTruck() != nullptr) continue; // object carried? if (obj == metal) continue; @@ -4557,9 +4542,8 @@ void CRobotMain::CompileScript(bool soluce) { lastError = nbError; nbError = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() != nullptr) continue; CBrain* brain = obj->GetBrain(); @@ -4585,9 +4569,8 @@ void CRobotMain::CompileScript(bool soluce) // Load all solutions. if (soluce) { - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() != 0) continue; CBrain* brain = obj->GetBrain(); @@ -4602,9 +4585,8 @@ void CRobotMain::CompileScript(bool soluce) } // Start all programs according to the command "run". - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() != nullptr) continue; CBrain* brain = obj->GetBrain(); @@ -4683,9 +4665,8 @@ void CRobotMain::LoadFileScript(CObject *obj, const char* filename, int objRank, //! Saves all programs of all the robots void CRobotMain::SaveAllScript() { - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); SaveOneScript(obj); } } @@ -4834,11 +4815,9 @@ char* CRobotMain::GetNewScriptName(ObjectType type, int rank) bool CRobotMain::IsBusy() { if (CScriptFunctions::m_CompteurFileOpen > 0) return true; - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { CBrain* brain = obj->GetBrain(); if (brain != nullptr) { @@ -4969,9 +4948,8 @@ bool CRobotMain::IOWriteScene(const char *filename, const char *filecbot, char * int objRank = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetType() == OBJECT_TOTO) continue; if (obj->GetType() == OBJECT_FIX) continue; if (obj->GetTruck() != nullptr) continue; @@ -5020,9 +4998,8 @@ bool CRobotMain::IOWriteScene(const char *filename, const char *filecbot, char * fWrite(&version, sizeof(long), 1, file); // version of CBOT objRank = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetType() == OBJECT_TOTO) continue; if (obj->GetType() == OBJECT_FIX) continue; if (obj->GetTruck() != nullptr) continue; @@ -5186,9 +5163,8 @@ CObject* CRobotMain::IOReadScene(const char *filename, const char *filecbot) { lastError = nbError; nbError = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() != nullptr) continue; objRank = obj->GetDefRank(); @@ -5200,9 +5176,8 @@ CObject* CRobotMain::IOReadScene(const char *filename, const char *filecbot) while (nbError > 0 && nbError != lastError); // Starts scripts - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetTruck() != nullptr) continue; if (obj->GetDefRank() == -1) continue; @@ -5228,9 +5203,8 @@ CObject* CRobotMain::IOReadScene(const char *filename, const char *filecbot) if (version == CBotProgram::GetVersion()) { objRank = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); if (obj->GetType() == OBJECT_TOTO) continue; if (obj->GetType() == OBJECT_FIX) continue; if (obj->GetTruck() != nullptr) continue; @@ -5423,11 +5397,9 @@ void CRobotMain::ResetCreate() CreateScene(m_dialog->GetSceneSoluce(), false, true); if (!GetNiceReset()) return; - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { ResetCap cap = obj->GetResetCap(); if (cap == RESET_NONE) continue; @@ -5456,9 +5428,8 @@ void CRobotMain::UpdateAudio(bool frame) Math::Vector oPos; int nb = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); // Do not use GetActif () because an invisible worm (underground) // should be regarded as existing here! if (obj->GetLock()) continue; @@ -5580,9 +5551,8 @@ Error CRobotMain::CheckEndMission(bool frame) Math::Vector oPos; int nb = 0; - for(auto& it : m_objMan->GetAllObjects()) + for (CObject* obj : m_objMan->GetAllObjects()) { - CObject* obj = it.second.get(); // Do not use GetActif () because an invisible worm (underground) // should be regarded as existing here! if (obj->GetLock()) continue; @@ -5858,11 +5828,9 @@ bool CRobotMain::GetRadar() { if (m_cheatRadar) return true; - - for(auto& it : m_objMan->GetAllObjects()) - { - CObject* obj = it.second.get(); + for (CObject* obj : m_objMan->GetAllObjects()) + { ObjectType type = obj->GetType(); if (type == OBJECT_RADAR && !obj->GetLock()) return true; diff --git a/src/object/task/taskbuild.cpp b/src/object/task/taskbuild.cpp index 8285bd68..57a2f2c0 100644 --- a/src/object/task/taskbuild.cpp +++ b/src/object/task/taskbuild.cpp @@ -548,7 +548,6 @@ bool CTaskBuild::Abort() Error CTaskBuild::FlatFloor() { - CObject *pObj; ObjectType type; Math::Vector center, pos, oPos, bPos; Math::Point c, p; @@ -589,9 +588,8 @@ Error CTaskBuild::FlatFloor() max = 100000.0f; bBase = false; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); if ( !pObj->GetActif() ) continue; // inactive? if ( pObj->GetTruck() != 0 ) continue; // object transported? if ( pObj == m_metal ) continue; @@ -635,9 +633,8 @@ Error CTaskBuild::FlatFloor() } max = 100000.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); if ( !pObj->GetActif() ) continue; // inactive? if ( pObj->GetTruck() != 0 ) continue; // object transported? if ( pObj == m_metal ) continue; @@ -691,7 +688,7 @@ Error CTaskBuild::FlatFloor() CObject* CTaskBuild::SearchMetalObject(float &angle, float dMin, float dMax, float aLimit, Error &err) { - CObject *pObj, *pBest; + CObject *pBest; Math::Vector iPos, oPos; ObjectType type; float min, iAngle, a, aa, aBest, distance, magic; @@ -704,9 +701,8 @@ CObject* CTaskBuild::SearchMetalObject(float &angle, float dMin, float dMax, min = 1000000.0f; pBest = 0; bMetal = false; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); if ( !pObj->GetActif() ) continue; // objet inactive? if ( pObj->GetTruck() != 0 ) continue; // object transported? @@ -759,9 +755,8 @@ void CTaskBuild::DeleteMark(Math::Vector pos, float radius) { std::vector objectsToDelete; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - CObject* obj = it.second.get(); ObjectType type = obj->GetType(); if ( type != OBJECT_MARKSTONE && type != OBJECT_MARKURANIUM && diff --git a/src/object/task/taskflag.cpp b/src/object/task/taskflag.cpp index 5786fc50..10d60244 100644 --- a/src/object/task/taskflag.cpp +++ b/src/object/task/taskflag.cpp @@ -150,18 +150,12 @@ CObject* CTaskFlag::SearchNearest(Math::Vector pos, ObjectType type) int CTaskFlag::CountObject(ObjectType type) { - ObjectType oType; - CObject *pObj; - Math::Vector oPos; - int count; - - count = 0; - for (auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + int count = 0; + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - if ( !pObj->GetEnable() ) continue; + if ( !obj->GetEnable() ) continue; - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( type == OBJECT_NULL ) { if ( oType != OBJECT_FLAGb && diff --git a/src/object/task/taskgoto.cpp b/src/object/task/taskgoto.cpp index 2249a17e..8c90912e 100644 --- a/src/object/task/taskgoto.cpp +++ b/src/object/task/taskgoto.cpp @@ -499,20 +499,13 @@ bool CTaskGoto::EventProcess(const Event &event) CObject* CTaskGoto::WormSearch(Math::Vector &impact) { - CObject* pObj; - CObject* pBest = 0; - Math::Vector iPos, oPos; - ObjectType oType; - float distance, min, radius; + Math::Vector iPos = m_object->GetPosition(0); + float min = 1000000.0f; - iPos = m_object->GetPosition(0); - min = 1000000.0f; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + CObject* best = nullptr; + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - oType = pObj->GetType(); + ObjectType oType = obj->GetType(); if ( oType != OBJECT_MOBILEfa && oType != OBJECT_MOBILEta && oType != OBJECT_MOBILEwa && @@ -557,20 +550,22 @@ CObject* CTaskGoto::WormSearch(Math::Vector &impact) oType != OBJECT_SAFE && oType != OBJECT_HUSTON ) continue; - if ( pObj->GetVirusMode() ) continue; // object infected? + if ( obj->GetVirusMode() ) continue; // object infected? - if ( !pObj->GetCrashSphere(0, oPos, radius) ) continue; - distance = Math::DistanceProjected(oPos, iPos); + Math::Vector oPos; + float radius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, radius) ) continue; + float distance = Math::DistanceProjected(oPos, iPos); if ( distance < min ) { min = distance; - pBest = pObj; + best = obj; } } - if ( pBest == 0 ) return 0; + if ( best == nullptr ) return nullptr; - impact = pBest->GetPosition(0); - return pBest; + impact = best->GetPosition(0); + return best; } // Contaminate objects near the worm. @@ -1150,22 +1145,18 @@ bool CTaskGoto::AdjustTarget(CObject* pObj, Math::Vector &pos, float &distance) bool CTaskGoto::AdjustBuilding(Math::Vector &pos, float margin, float &distance) { - CObject* pObj; - Math::Vector oPos; - float dist, suppl; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetActif() ) continue; + if ( obj->GetTruck() != nullptr ) continue; // object transported? - if ( !pObj->GetActif() ) continue; - if ( pObj->GetTruck() != 0 ) continue; // object transported? - - if ( !GetHotPoint(pObj, oPos, false, 0.0f, suppl) ) continue; - dist = Math::DistanceProjected(pos, oPos); + Math::Vector oPos; + float suppl = 0.0f; + if ( !GetHotPoint(obj, oPos, false, 0.0f, suppl) ) continue; + float dist = Math::DistanceProjected(pos, oPos); if ( dist <= margin ) { - GetHotPoint(pObj, pos, true, distance, suppl); + GetHotPoint(obj, pos, true, distance, suppl); distance += suppl; return true; } @@ -1314,9 +1305,8 @@ bool CTaskGoto::LeakSearch(Math::Vector &pos, float &delay) min = 100000.0f; bRadius = 0.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); if ( pObj == m_object ) continue; if ( !pObj->GetActif() ) continue; if ( pObj->GetTruck() != 0 ) continue; // object transported? @@ -1406,7 +1396,6 @@ void CTaskGoto::ComputeRepulse(Math::Point &dir) ObjectType iType, oType; Math::Vector iPos, oPos; Math::Point repulse; - CObject *pObj; float gDist, add, addi, fac, dist, iRadius, oRadius; int j; bool bAlien; @@ -1492,11 +1481,9 @@ void CTaskGoto::ComputeRepulse(Math::Point &dir) { bAlien = true; } - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( pObj == m_object ) continue; if ( pObj->GetTruck() != 0 ) continue; @@ -1574,7 +1561,6 @@ void CTaskGoto::ComputeFlyingRepulse(float &dir) { ObjectType oType; Math::Vector iPos, oPos; - CObject *pObj; float add, fac, dist, iRadius, oRadius, repulse; int j; @@ -1583,11 +1569,9 @@ void CTaskGoto::ComputeFlyingRepulse(float &dir) add = 0.0f; fac = 1.5f; dir = 0.0f; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( pObj == m_object ) continue; if ( pObj->GetTruck() != 0 ) continue; @@ -1889,18 +1873,15 @@ bool CTaskGoto::BitmapTestLine(const Math::Vector &start, const Math::Vector &go void CTaskGoto::BitmapObject() { - CObject *pObj; ObjectType type; Math::Vector iPos, oPos; float iRadius, oRadius, h; int j; m_object->GetCrashSphere(0, iPos, iRadius); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { type = pObj->GetType(); if ( pObj == m_object ) continue; diff --git a/src/object/task/taskmanip.cpp b/src/object/task/taskmanip.cpp index 84de57ae..65f6e683 100644 --- a/src/object/task/taskmanip.cpp +++ b/src/object/task/taskmanip.cpp @@ -724,7 +724,7 @@ bool CTaskManip::Abort() CObject* CTaskManip::SearchTakeUnderObject(Math::Vector &pos, float dLimit) { - CObject *pObj, *pBest; + CObject *pBest; Math::Vector iPos, oPos; ObjectType type; float min, distance; @@ -733,9 +733,8 @@ CObject* CTaskManip::SearchTakeUnderObject(Math::Vector &pos, float dLimit) min = 1000000.0f; pBest = 0; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); type = pObj->GetType(); if ( type != OBJECT_FRET && @@ -777,7 +776,7 @@ CObject* CTaskManip::SearchTakeUnderObject(Math::Vector &pos, float dLimit) CObject* CTaskManip::SearchTakeFrontObject(bool bAdvance, Math::Vector &pos, float &distance, float &angle) { - CObject *pObj, *pBest; + CObject *pBest; Math::Vector iPos, oPos; ObjectType type; float min, iAngle, bAngle, aLimit, dLimit, f; @@ -801,9 +800,8 @@ CObject* CTaskManip::SearchTakeFrontObject(bool bAdvance, Math::Vector &pos, min = 1000000.0f; pBest = 0; bAngle = 0.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); type = pObj->GetType(); if ( type != OBJECT_FRET && @@ -866,7 +864,7 @@ CObject* CTaskManip::SearchTakeFrontObject(bool bAdvance, Math::Vector &pos, CObject* CTaskManip::SearchTakeBackObject(bool bAdvance, Math::Vector &pos, float &distance, float &angle) { - CObject *pObj, *pBest; + CObject *pBest; Math::Vector iPos, oPos; ObjectType type; float min, iAngle, bAngle, aLimit, dLimit, f; @@ -889,9 +887,8 @@ CObject* CTaskManip::SearchTakeBackObject(bool bAdvance, Math::Vector &pos, min = 1000000.0f; pBest = 0; bAngle = 0.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); type = pObj->GetType(); if ( type != OBJECT_FRET && @@ -956,7 +953,6 @@ CObject* CTaskManip::SearchOtherObject(bool bAdvance, Math::Vector &pos, float &height) { Character* character; - CObject* pObj; CObject* pPower; Math::Matrix* mat; Math::Vector iPos, oPos; @@ -982,11 +978,9 @@ CObject* CTaskManip::SearchOtherObject(bool bAdvance, Math::Vector &pos, aLimit = 7.0f*Math::PI/180.0f; dLimit = MARGIN_FRIEND; } - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( pObj == m_object ) continue; // yourself? type = pObj->GetType(); @@ -1329,25 +1323,19 @@ bool CTaskManip::TruckDeposeObject() bool CTaskManip::IsFreeDeposeObject(Math::Vector pos) { - CObject* pObj; - Math::Matrix* mat; - Math::Vector iPos, oPos; - float oRadius; - int j; + Math::Matrix* mat = m_object->GetWorldMatrix(0); + Math::Vector iPos = Transform(*mat, pos); - mat = m_object->GetWorldMatrix(0); - iPos = Transform(*mat, pos); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj == m_object ) continue; + if ( !obj->GetActif() ) continue; // inactive? + if ( obj->GetTruck() != nullptr ) continue; // object transported? - if ( pObj == m_object ) continue; - if ( !pObj->GetActif() ) continue; // inactive? - if ( pObj->GetTruck() != 0 ) continue; // object transported? - - j = 0; - while ( pObj->GetCrashSphere(j++, oPos, oRadius) ) + Math::Vector oPos; + float oRadius = 0.0f; + int j = 0; + while ( obj->GetCrashSphere(j++, oPos, oRadius) ) { if ( Math::Distance(iPos, oPos)-(oRadius+1.0f) < 2.0f ) { diff --git a/src/object/task/taskreset.cpp b/src/object/task/taskreset.cpp index 71f2c754..f3ff51e3 100644 --- a/src/object/task/taskreset.cpp +++ b/src/object/task/taskreset.cpp @@ -278,18 +278,11 @@ Error CTaskReset::IsEnded() bool CTaskReset::SearchVehicle() { - CObject* pObj; - Math::Vector oPos; - ObjectType type; - float oRadius, dist; - - for (auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( obj == m_object ) continue; - if ( pObj == m_object ) continue; - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type != OBJECT_HUMAN && type != OBJECT_TECH && type != OBJECT_MOBILEfa && @@ -325,8 +318,10 @@ bool CTaskReset::SearchVehicle() type != OBJECT_BEE && type != OBJECT_WORM ) continue; - if ( !pObj->GetCrashSphere(0, oPos, oRadius) ) continue; - dist = Math::Distance(oPos, m_goal)-oRadius; + Math::Vector oPos; + float oRadius = 0.0f; + if ( !obj->GetCrashSphere(0, oPos, oRadius) ) continue; + float dist = Math::Distance(oPos, m_goal)-oRadius; if ( dist < 5.0f ) return true; } diff --git a/src/object/task/taskshield.cpp b/src/object/task/taskshield.cpp index 186d8b96..3d0ab2c7 100644 --- a/src/object/task/taskshield.cpp +++ b/src/object/task/taskshield.cpp @@ -551,30 +551,23 @@ bool CTaskShield::CreateLight(Math::Vector pos) void CTaskShield::IncreaseShield() { - ObjectType type; - CObject* pObj; - Math::Vector oPos; - float dist, shield; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); - - type = pObj->GetType(); + ObjectType type = obj->GetType(); if ( type == OBJECT_MOTHER || type == OBJECT_ANT || type == OBJECT_SPIDER || type == OBJECT_BEE || type == OBJECT_WORM ) continue; - oPos = pObj->GetPosition(0); - dist = Math::Distance(oPos, m_shieldPos); + Math::Vector oPos = obj->GetPosition(0); + float dist = Math::Distance(oPos, m_shieldPos); if ( dist <= GetRadius()+10.0f ) { - shield = pObj->GetShield(); + float shield = obj->GetShield(); shield += 0.1f; if ( shield > 1.0f ) shield = 1.0f; - pObj->SetShield(shield); + obj->SetShield(shield); } } } diff --git a/src/object/task/tasktake.cpp b/src/object/task/tasktake.cpp index 10361c80..cb3c93c8 100644 --- a/src/object/task/tasktake.cpp +++ b/src/object/task/tasktake.cpp @@ -299,7 +299,7 @@ bool CTaskTake::Abort() CObject* CTaskTake::SearchTakeObject(float &angle, float dLimit, float aLimit) { - CObject *pObj, *pBest; + CObject *pBest; Math::Vector iPos, oPos; ObjectType type; float min, iAngle, bAngle, a, distance; @@ -311,9 +311,8 @@ CObject* CTaskTake::SearchTakeObject(float &angle, min = 1000000.0f; pBest = 0; bAngle = 0.0f; - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); type = pObj->GetType(); if ( type != OBJECT_FRET && @@ -363,7 +362,6 @@ CObject* CTaskTake::SearchFriendObject(float &angle, float dLimit, float aLimit) { Character* character; - CObject* pObj; CObject* pPower; Math::Matrix* mat; Math::Vector iPos, oPos; @@ -373,11 +371,9 @@ CObject* CTaskTake::SearchFriendObject(float &angle, if ( !m_object->GetCrashSphere(0, iPos, iRad) ) return 0; iAngle = m_object->GetAngleY(0); iAngle = Math::NormAngle(iAngle); // 0..2*Math::PI - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( pObj == m_object ) continue; // yourself? type = pObj->GetType(); @@ -559,7 +555,6 @@ bool CTaskTake::TruckDeposeObject() bool CTaskTake::IsFreeDeposeObject(Math::Vector pos) { - CObject* pObj; Math::Matrix* mat; Math::Vector iPos, oPos; float oRadius; @@ -567,11 +562,9 @@ bool CTaskTake::IsFreeDeposeObject(Math::Vector pos) mat = m_object->GetWorldMatrix(0); iPos = Transform(*mat, pos); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( pObj == m_object ) continue; if ( !pObj->GetActif() ) continue; // inactive? if ( pObj->GetTruck() != 0 ) continue; // object transported? diff --git a/src/object/task/taskterraform.cpp b/src/object/task/taskterraform.cpp index 1e2f8a12..2a225cea 100644 --- a/src/object/task/taskterraform.cpp +++ b/src/object/task/taskterraform.cpp @@ -339,7 +339,6 @@ bool CTaskTerraform::Abort() bool CTaskTerraform::Terraform() { - CObject* pObj; CBrain* brain; CMotion* motion; Gfx::CPyro* pyro; @@ -349,11 +348,9 @@ bool CTaskTerraform::Terraform() m_camera->StartEffect(Gfx::CAM_EFFECT_TERRAFORM, m_terraPos, 1.0f); m_sound->Play(SOUND_THUMP, m_terraPos); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { type = pObj->GetType(); if ( type == OBJECT_NULL ) continue; diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 50de0843..5822b6c3 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -2507,7 +2507,6 @@ void CPhysics::FloorAngle(const Math::Vector &pos, Math::Vector &angle) int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) { - CObject* pObj; Gfx::CPyro* pyro; CPhysics* ph; Math::Matrix matRotate; @@ -2526,10 +2525,8 @@ int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) iPos = iiPos + (pos - m_object->GetPosition(0)); iType = m_object->GetType(); - - for(auto &it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); if ( pObj == m_object ) continue; // yourself? if ( pObj->GetTruck() != 0 ) continue; // object transported? if ( !pObj->GetEnable() ) continue; // inactive? diff --git a/src/ui/mainshort.cpp b/src/ui/mainshort.cpp index 338eefc9..059b03d6 100644 --- a/src/ui/mainshort.cpp +++ b/src/ui/mainshort.cpp @@ -94,7 +94,6 @@ static EventType table_sc_em[20] = bool CMainShort::CreateShortcuts() { - CObject* pObj; CControl* pc; ObjectType type; Math::Point pos, dim; @@ -137,11 +136,9 @@ bool CMainShort::CreateShortcuts() pos.x += dim.x*1.2f; m_shortcuts[rank] = 0; rank ++; - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) - { - pObj = it.second.get(); + for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) + { if ( !pObj->GetActif() ) continue; if ( !pObj->GetSelectable() ) continue; if ( pObj->GetProxyActivate() ) continue; diff --git a/src/ui/target.cpp b/src/ui/target.cpp index 2836b44e..e541c695 100644 --- a/src/ui/target.cpp +++ b/src/ui/target.cpp @@ -185,22 +185,16 @@ bool CTarget::GetTooltip(Math::Point pos, std::string &name) CObject* CTarget::DetectFriendObject(Math::Point pos) { - ObjectType type; - CObject *pObj, *pTarget; - int objRank, j, rank; + int objRank = m_engine->DetectObject(pos); - objRank = m_engine->DetectObject(pos); - - for(auto& it : CObjectManager::GetInstancePointer()->GetAllObjects()) + for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - pObj = it.second.get(); + if ( !obj->GetActif() ) continue; + if ( obj->GetProxyActivate() ) continue; + if ( obj->GetSelect() ) continue; - if ( !pObj->GetActif() ) continue; - if ( pObj->GetProxyActivate() ) continue; - if ( pObj->GetSelect() ) continue; - - pTarget = 0; - type = pObj->GetType(); + CObject* target = nullptr; + ObjectType type = obj->GetType(); if ( type == OBJECT_DERRICK || type == OBJECT_FACTORY || type == OBJECT_REPAIR || @@ -247,29 +241,29 @@ CObject* CTarget::DetectFriendObject(Math::Point pos) type == OBJECT_MOBILEit || type == OBJECT_MOBILEdr ) { - pTarget = pObj; + target = obj; } else if ( (type == OBJECT_POWER || type == OBJECT_ATOMIC ) && - pObj->GetTruck() != 0 ) // battery used? + obj->GetTruck() != nullptr ) // battery used? { - pTarget = pObj->GetTruck(); - if ( pTarget->GetType() == OBJECT_MOBILEtg ) + target = obj->GetTruck(); + if ( target->GetType() == OBJECT_MOBILEtg ) { - pTarget = 0; + target = nullptr; } } - for ( j=0 ; jGetObjectRank(j); + int rank = obj->GetObjectRank(j); if ( rank == -1 ) continue; if ( rank != objRank ) continue; - return pTarget; + return target; } } - return 0; + return nullptr; } }