From 28925106e4dedacdfe1cb615299eacbfafe5b853 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Tue, 18 Aug 2015 12:29:41 +0200 Subject: [PATCH] Cleaned up some long lists of ifs --- src/level/robotmain.cpp | 16 ++++++++++--- src/object/auto/autoconvert.cpp | 41 +-------------------------------- src/object/object.cpp | 29 +++++++++++++++++++++++ src/object/object.h | 2 ++ src/object/old_object.cpp | 20 +++++----------- src/object/task/taskmanip.cpp | 6 ++--- src/object/task/tasktake.cpp | 37 +++++------------------------ src/physics/physics.cpp | 40 +++++--------------------------- src/ui/controls/target.cpp | 25 ++++++++------------ 9 files changed, 75 insertions(+), 141 deletions(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 855d1818..28fb183a 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -1891,11 +1891,21 @@ CObject* CRobotMain::DetectObject(Math::Point pos) if (obj->GetProxyActivate()) continue; CObject* target = obj; - if (obj->GetType() == OBJECT_POWER || obj->GetType() == OBJECT_ATOMIC) + if (obj->Implements(ObjectInterfaceType::PowerContainer) && obj->Implements(ObjectInterfaceType::Transportable)) { - assert(obj->Implements(ObjectInterfaceType::Transportable)); target = dynamic_cast(obj)->GetTransporter(); // battery connected - if (target == nullptr) target = obj; // standalone battery + if (target == nullptr) + { + target = obj; // standalone battery + } + else + { + if (!target->Implements(ObjectInterfaceType::Powered) || dynamic_cast(target)->GetPower() != obj) + { + // transported, but not in the power slot + target = obj; + } + } } if (!obj->Implements(ObjectInterfaceType::Old)) continue; diff --git a/src/object/auto/autoconvert.cpp b/src/object/auto/autoconvert.cpp index 5dd69d5d..5d5dd88c 100644 --- a/src/object/auto/autoconvert.cpp +++ b/src/object/auto/autoconvert.cpp @@ -423,46 +423,7 @@ bool CAutoConvert::SearchVehicle() for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { ObjectType type = obj->GetType(); - if ( type != OBJECT_HUMAN && - type != OBJECT_MOBILEfa && - type != OBJECT_MOBILEta && - type != OBJECT_MOBILEwa && - type != OBJECT_MOBILEia && - type != OBJECT_MOBILEfc && - type != OBJECT_MOBILEtc && - type != OBJECT_MOBILEwc && - type != OBJECT_MOBILEic && - type != OBJECT_MOBILEfi && - type != OBJECT_MOBILEti && - type != OBJECT_MOBILEwi && - type != OBJECT_MOBILEii && - type != OBJECT_MOBILEfs && - type != OBJECT_MOBILEts && - type != OBJECT_MOBILEws && - type != OBJECT_MOBILEis && - type != OBJECT_MOBILErt && - type != OBJECT_MOBILErc && - type != OBJECT_MOBILErr && - type != OBJECT_MOBILErs && - type != OBJECT_MOBILEsa && - type != OBJECT_MOBILEtg && - type != OBJECT_MOBILEft && - type != OBJECT_MOBILEtt && - type != OBJECT_MOBILEwt && - type != OBJECT_MOBILEit && - type != OBJECT_MOBILEdr && - type != OBJECT_METAL && - type != OBJECT_URANIUM && - type != OBJECT_POWER && - type != OBJECT_ATOMIC && - type != OBJECT_BULLET && - type != OBJECT_BBOX && - type != OBJECT_TNT && - type != OBJECT_MOTHER && - type != OBJECT_ANT && - type != OBJECT_SPIDER && - type != OBJECT_BEE && - type != OBJECT_WORM ) continue; + if ( type == OBJECT_STONE ) continue; if (obj->GetCrashSphereCount() == 0) continue; diff --git a/src/object/object.cpp b/src/object/object.cpp index b15f469e..a929daac 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -111,6 +111,35 @@ std::vector CObject::GetAllCrashSpheres() return allCrashSpheres; } +bool CObject::CanCollideWith(CObject* other) +{ + ObjectType otherType = other->GetType(); + if (m_type == OBJECT_WORM) return otherType == OBJECT_WORM; + if (m_type == OBJECT_MOTHER) + { + if (otherType == OBJECT_ANT) return false; + if (otherType == OBJECT_SPIDER) return false; + if (otherType == OBJECT_EGG) return false; + } + if (otherType == OBJECT_MOTHER) + { + if (m_type == OBJECT_ANT) return false; + if (m_type == OBJECT_SPIDER) return false; + if (m_type == OBJECT_EGG) return false; + } + if ( m_type == OBJECT_MOTHER || + m_type == OBJECT_ANT || + m_type == OBJECT_SPIDER || + m_type == OBJECT_WORM || + m_type == OBJECT_BEE ) + { + if (other->Implements(ObjectInterfaceType::Transportable)) return false; + if (otherType >= OBJECT_PLANT0 && otherType <= OBJECT_PLANT19) return false; + if (otherType >= OBJECT_MUSHROOM1 && otherType <= OBJECT_MUSHROOM2) return false; + } + return true; +} + Math::Vector CObject::GetPosition() const { return m_position; diff --git a/src/object/object.h b/src/object/object.h index 18d2296b..a3a5a199 100644 --- a/src/object/object.h +++ b/src/object/object.h @@ -149,6 +149,8 @@ public: std::vector GetAllCrashSpheres(); //! Removes all crash spheres void DeleteAllCrashSpheres(); + //! Returns true if this object can collide with the other one + bool CanCollideWith(CObject* other); //! Returns sphere used to test for camera collisions Math::Sphere GetCameraCollisionSphere(); diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index a64bf2c2..b3af869b 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -537,14 +537,7 @@ void COldObject::DestroyObject(DestructionType type) if ( m_botVar != nullptr ) { - if ( m_type == OBJECT_STONE || - m_type == OBJECT_URANIUM || - m_type == OBJECT_METAL || - m_type == OBJECT_POWER || - m_type == OBJECT_ATOMIC || - m_type == OBJECT_BULLET || - m_type == OBJECT_BBOX || - m_type == OBJECT_TNT ) // (*) + if ( Implements(ObjectInterfaceType::Transportable) ) // (*) { CScriptFunctions::DestroyObjectVar(m_botVar, false); } @@ -880,8 +873,10 @@ void COldObject::SetType(ObjectType type) } // TODO: Another one? :/ - if ( m_type == OBJECT_POWER || // PowerCell - m_type == OBJECT_ATOMIC ) // NuclearCell + if ( m_type == OBJECT_POWER || // PowerCell + m_type == OBJECT_ATOMIC || // NuclearCell + m_type == OBJECT_STATION || // PowerStation + m_type == OBJECT_ENERGY ) // PowerPlant { m_implementedInterfaces[static_cast(ObjectInterfaceType::PowerContainer)] = true; } @@ -2171,10 +2166,7 @@ bool COldObject::EventFrame(const Event &event) void COldObject::UpdateMapping() { - if ( m_type == OBJECT_POWER || - m_type == OBJECT_ATOMIC || - m_type == OBJECT_STATION || - m_type == OBJECT_ENERGY ) + if ( Implements(ObjectInterfaceType::PowerContainer) ) { UpdateEnergyMapping(); } diff --git a/src/object/task/taskmanip.cpp b/src/object/task/taskmanip.cpp index c50806f3..bc21154f 100644 --- a/src/object/task/taskmanip.cpp +++ b/src/object/task/taskmanip.cpp @@ -633,8 +633,7 @@ Error CTaskManip::IsEnded() { if ( (m_arm == TMA_OTHER || m_arm == TMA_POWER ) && - (m_cargoType == OBJECT_POWER || - m_cargoType == OBJECT_ATOMIC ) ) + m_object->GetCargo()->Implements(ObjectInterfaceType::PowerContainer) ) { m_sound->Play(SOUND_POWEROFF, m_object->GetPosition()); } @@ -656,8 +655,7 @@ Error CTaskManip::IsEnded() { if ( (m_arm == TMA_OTHER || m_arm == TMA_POWER ) && - (m_cargoType == OBJECT_POWER || - m_cargoType == OBJECT_ATOMIC ) ) + cargo->Implements(ObjectInterfaceType::PowerContainer) ) { m_sound->Play(SOUND_POWERON, m_object->GetPosition()); } diff --git a/src/object/task/tasktake.cpp b/src/object/task/tasktake.cpp index 511d0f4b..d6e5bd79 100644 --- a/src/object/task/tasktake.cpp +++ b/src/object/task/tasktake.cpp @@ -131,19 +131,10 @@ Error CTaskTake::Start() if (other != nullptr && dynamic_cast(other)->GetPower() != nullptr) { - type = dynamic_cast(other)->GetPower()->GetType(); + CObject* power = dynamic_cast(other)->GetPower(); + type = power->GetType(); if ( type == OBJECT_URANIUM ) return ERR_MANIP_RADIO; - if ( type != OBJECT_STONE && - type != OBJECT_BULLET && - type != OBJECT_METAL && - type != OBJECT_POWER && - type != OBJECT_ATOMIC && - type != OBJECT_BBOX && - type != OBJECT_KEYa && - type != OBJECT_KEYb && - type != OBJECT_KEYc && - type != OBJECT_KEYd && - type != OBJECT_TNT ) return ERR_MANIP_FRIEND; + if ( !power->Implements(ObjectInterfaceType::Transportable) ) return ERR_MANIP_FRIEND; // TODO: This makes no sense, probably redundant //? m_camera->StartCentering(m_object, Math::PI*0.3f, -Math::PI*0.1f, 0.0f, 0.8f); m_arm = TTA_FRIEND; } @@ -240,8 +231,7 @@ Error CTaskTake::IsEnded() if ( TransporterTakeObject() ) { if ( m_arm == TTA_FRIEND && - (m_cargoType == OBJECT_POWER || - m_cargoType == OBJECT_ATOMIC ) ) + m_object->GetCargo()->Implements(ObjectInterfaceType::PowerContainer) ) { m_sound->Play(SOUND_POWEROFF, m_object->GetPosition()); } @@ -261,8 +251,7 @@ Error CTaskTake::IsEnded() CObject* cargo = m_object->GetCargo(); TransporterDeposeObject(); if ( m_arm == TTA_FRIEND && - (m_cargoType == OBJECT_POWER || - m_cargoType == OBJECT_ATOMIC ) ) + cargo->Implements(ObjectInterfaceType::PowerContainer) ) { m_sound->Play(SOUND_POWERON, m_object->GetPosition()); } @@ -300,7 +289,6 @@ CObject* CTaskTake::SearchTakeObject(float &angle, { CObject *pBest; Math::Vector iPos, oPos; - ObjectType type; float min, iAngle, bAngle, a, distance; iPos = m_object->GetPosition(); @@ -312,20 +300,7 @@ CObject* CTaskTake::SearchTakeObject(float &angle, bAngle = 0.0f; for (CObject* pObj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - type = pObj->GetType(); - - if ( type != OBJECT_STONE && - type != OBJECT_URANIUM && - type != OBJECT_BULLET && - type != OBJECT_METAL && - type != OBJECT_POWER && - type != OBJECT_ATOMIC && - type != OBJECT_BBOX && - type != OBJECT_KEYa && - type != OBJECT_KEYb && - type != OBJECT_KEYc && - type != OBJECT_KEYd && - type != OBJECT_TNT ) continue; + if ( !pObj->Implements(ObjectInterfaceType::Transportable) ) continue; if (IsObjectBeingTransported(pObj)) continue; if ( pObj->GetLock() ) continue; diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 3f7c173f..dc4b8ee5 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -2496,45 +2496,14 @@ int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) if ( pObj->Implements(ObjectInterfaceType::Destroyable) && dynamic_cast(pObj)->IsDying() ) continue; // is burning or exploding? oType = pObj->GetType(); - if ( oType == OBJECT_NULL ) continue; - if ( oType == OBJECT_TOTO ) continue; -//? if ( iType == OBJECT_BEE && oType == OBJECT_BEE ) continue; - if ( iType == OBJECT_WORM && oType != OBJECT_WORM ) continue; - if ( iType != OBJECT_WORM && oType == OBJECT_WORM ) continue; - if ( iType == OBJECT_MOTHER && oType == OBJECT_ANT ) continue; - if ( iType == OBJECT_ANT && oType == OBJECT_MOTHER ) continue; - if ( iType == OBJECT_MOTHER && oType == OBJECT_SPIDER ) continue; - if ( iType == OBJECT_SPIDER && oType == OBJECT_MOTHER ) continue; - if ( iType == OBJECT_MOTHER && oType == OBJECT_EGG ) continue; - if ( iType == OBJECT_EGG && oType == OBJECT_MOTHER ) continue; + if ( oType == OBJECT_TOTO ) continue; + if ( !m_object->CanCollideWith(pObj) ) continue; if (pObj->Implements(ObjectInterfaceType::Jostleable)) { JostleObject(dynamic_cast(pObj), iPos, iRad); } - if ( iType == OBJECT_MOTHER || - iType == OBJECT_ANT || - iType == OBJECT_SPIDER || - iType == OBJECT_WORM || - iType == OBJECT_BEE ) // insect? - { - if ( oType == OBJECT_STONE || - oType == OBJECT_URANIUM || - oType == OBJECT_METAL || - oType == OBJECT_POWER || - oType == OBJECT_ATOMIC || - oType == OBJECT_BULLET || - oType == OBJECT_BBOX || - oType == OBJECT_KEYa || - oType == OBJECT_KEYb || - oType == OBJECT_KEYc || - oType == OBJECT_KEYd || - oType == OBJECT_TNT || - (oType >= OBJECT_PLANT0 && oType <= OBJECT_PLANT19 ) || - (oType >= OBJECT_MUSHROOM1 && oType <= OBJECT_MUSHROOM2) ) continue; - } - if ( oType == OBJECT_WAYPOINT && !pObj->GetLock() && m_object->GetTrainer() ) // driving vehicle? @@ -2564,6 +2533,8 @@ int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) Math::Vector oPos = crashSphere.sphere.pos; float oRad = crashSphere.sphere.radius; + // Aliens ignore small objects + // TODO: But why? :/ if ( iType == OBJECT_MOTHER && oRad <= 1.2f ) continue; if ( iType == OBJECT_ANT && oRad <= 1.2f ) continue; if ( iType == OBJECT_SPIDER && oRad <= 1.2f ) continue; @@ -2966,7 +2937,8 @@ void CPhysics::PowerParticle(float factor, bool bBreak) if (m_object->Implements(ObjectInterfaceType::Carrier)) { CObject* cargo = dynamic_cast(m_object)->GetCargo(); - if ( cargo != nullptr && cargo->GetType() == OBJECT_POWER && + if ( cargo != nullptr && cargo->Implements(ObjectInterfaceType::PowerContainer) && + dynamic_cast(cargo)->IsRechargeable() && m_object->GetPartRotationZ(1) == ARM_STOCK_ANGLE1 ) { bCarryPower = true; // carries a battery diff --git a/src/ui/controls/target.cpp b/src/ui/controls/target.cpp index e3a5831a..22b95eb4 100644 --- a/src/ui/controls/target.cpp +++ b/src/ui/controls/target.cpp @@ -140,12 +140,17 @@ CObject* CTarget::DetectFriendObject(Math::Point pos) for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects()) { - if ( !obj->GetDetectable() ) continue; - if ( obj->GetProxyActivate() ) continue; - if ( obj->Implements(ObjectInterfaceType::Controllable) && dynamic_cast(obj)->GetSelect() ) continue; + CObject* target = obj; + if ( obj->Implements(ObjectInterfaceType::PowerContainer) && IsObjectBeingTransported(obj) ) + { + target = dynamic_cast(obj)->GetTransporter(); + } - CObject* target = nullptr; - ObjectType type = obj->GetType(); + if ( !target->GetDetectable() ) continue; + if ( target->GetProxyActivate() ) continue; + if ( target->Implements(ObjectInterfaceType::Controllable) && dynamic_cast(target)->GetSelect() ) continue; + + ObjectType type = target->GetType(); if ( type == OBJECT_DERRICK || type == OBJECT_FACTORY || type == OBJECT_REPAIR || @@ -194,16 +199,6 @@ CObject* CTarget::DetectFriendObject(Math::Point pos) { target = obj; } - else if ( (type == OBJECT_POWER || - type == OBJECT_ATOMIC ) && - IsObjectBeingTransported(obj) ) // battery used? - { - target = dynamic_cast(obj)->GetTransporter(); - if ( target->GetType() == OBJECT_MOBILEtg ) - { - target = nullptr; - } - } for (int j=0 ; j