From e937db94c8ff804d6cbc3f945b48f55ecec3c260 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Thu, 13 Aug 2015 10:49:26 +0200 Subject: [PATCH] Moved GetPhysics() and GetMotion() to CMovableObject; moved trace drawing to CTraceDrawingObject --- src/CMakeLists.txt | 2 +- src/graphics/engine/camera.cpp | 7 +- src/graphics/engine/pyro.cpp | 10 +- src/object/auto/autobase.cpp | 4 +- src/object/auto/autofactory.cpp | 18 ++-- src/object/auto/autorepair.cpp | 4 +- src/object/auto/autotower.cpp | 4 +- .../implementation/programmable_impl.cpp | 20 ++-- src/object/implementation/programmable_impl.h | 3 +- src/object/interface/flying_object.h | 2 +- src/object/interface/jet_flying_object.h | 9 +- src/object/interface/movable_object.h | 9 ++ src/object/interface/task_executor_object.h | 3 +- .../trace_drawing_object.cpp} | 3 +- .../trace_drawing_object.h} | 28 +++++- src/object/mainmovie.cpp | 32 +++---- src/object/motion/motionvehicle.cpp | 38 -------- src/object/motion/motionvehicle.h | 13 --- src/object/object_factory.cpp | 12 +-- src/object/object_interface_type.h | 1 + src/object/object_manager.cpp | 17 +++- src/object/old_object.cpp | 81 +++++++++++++--- src/object/old_object.h | 25 ++++- src/object/old_object_interface.cpp | 10 -- src/object/old_object_interface.h | 3 - src/object/robotmain.cpp | 29 +++--- src/object/task/taskgoto.cpp | 4 +- src/object/task/taskmanager.h | 2 +- src/object/task/taskpen.h | 4 +- src/object/task/taskterraform.cpp | 4 +- src/physics/physics.cpp | 61 ++++++------ src/physics/physics.h | 6 +- src/script/scriptfunc.cpp | 96 +++++++++++++------ src/ui/displayinfo.cpp | 40 ++++---- src/ui/displaytext.cpp | 39 ++++---- src/ui/object_interface.cpp | 37 ++++--- 36 files changed, 370 insertions(+), 310 deletions(-) rename src/object/{trace_color.cpp => interface/trace_drawing_object.cpp} (97%) rename src/object/{trace_color.h => interface/trace_drawing_object.h} (62%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 28598940..b53eb517 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -150,6 +150,7 @@ set(BASE_SOURCES object/auto/autopowerstation.cpp object/auto/autotower.cpp object/drive_type.cpp + object/interface/trace_drawing_object.cpp object/implementation/power_container_impl.cpp object/implementation/programmable_impl.cpp object/implementation/task_executor_impl.cpp @@ -199,7 +200,6 @@ set(BASE_SOURCES object/task/taskturn.cpp object/task/taskwait.cpp object/tool_type.cpp - object/trace_color.cpp object/subclass/exchange_post.cpp object/subclass/static_object.cpp physics/physics.cpp diff --git a/src/graphics/engine/camera.cpp b/src/graphics/engine/camera.cpp index cb6d65a7..e27af21d 100644 --- a/src/graphics/engine/camera.cpp +++ b/src/graphics/engine/camera.cpp @@ -35,6 +35,7 @@ #include "object/robotmain.h" #include "object/interface/carrier_object.h" +#include "object/interface/movable_object.h" #include "object/interface/powered_object.h" #include "object/interface/transportable_object.h" @@ -1468,8 +1469,10 @@ bool CCamera::EventFrameBack(const Event &event) m_eyePt = RotateView(lookatPt, h, v, d); - CPhysics* physics = m_cameraObj->GetPhysics(); - if ( (physics != NULL) && physics->GetLand() ) // ground? + bool ground = true; + if (m_cameraObj->Implements(ObjectInterfaceType::Movable)) + ground = dynamic_cast(m_cameraObj)->GetPhysics()->GetLand(); + if ( ground ) // ground? { Math::Vector pos = lookatPt + (lookatPt - m_eyePt); float floor = m_terrain->GetHeightToFloor(pos) - 4.0f; diff --git a/src/graphics/engine/pyro.cpp b/src/graphics/engine/pyro.cpp index 9af79a7e..7ac54a9e 100644 --- a/src/graphics/engine/pyro.cpp +++ b/src/graphics/engine/pyro.cpp @@ -269,9 +269,11 @@ bool CPyro::Create(PyroType type, CObject* obj, float force) { m_object->SetDead(true); - CMotion* motion = m_object->GetMotion(); - if (motion != nullptr) + if ( obj->Implements(ObjectInterfaceType::Movable) ) + { + CMotion* motion = dynamic_cast(obj)->GetMotion(); motion->SetAction(MHS_DEADg, 1.0f); + } m_camera->StartCentering(m_object, Math::PI*0.5f, 99.9f, 0.0f, 1.5f); m_camera->StartOver(CAM_OVER_EFFECT_FADEOUT_WHITE, m_pos, 1.0f); @@ -282,9 +284,9 @@ bool CPyro::Create(PyroType type, CObject* obj, float force) { m_object->SetDead(true); - CMotion* motion = m_object->GetMotion(); - if ( motion != nullptr ) + if ( obj->Implements(ObjectInterfaceType::Movable) ) { + CMotion* motion = dynamic_cast(obj)->GetMotion(); motion->SetAction(MHS_DEADw, 4.0f); } m_camera->StartCentering(m_object, Math::PI*0.5f, 99.9f, 0.0f, 3.0f); diff --git a/src/object/auto/autobase.cpp b/src/object/auto/autobase.cpp index ba42650f..af97f03a 100644 --- a/src/object/auto/autobase.cpp +++ b/src/object/auto/autobase.cpp @@ -1246,9 +1246,9 @@ void CAutoBase::FreezeCargo(bool freeze) if ( dist < 32.0f ) { m_cargoObjects.insert(obj); - CPhysics* physics = obj->GetPhysics(); - if ( physics != nullptr ) + if ( obj->Implements(ObjectInterfaceType::Movable) ) { + CPhysics* physics = dynamic_cast(obj)->GetPhysics(); physics->SetFreeze(freeze); } } diff --git a/src/object/auto/autofactory.cpp b/src/object/auto/autofactory.cpp index 061c6438..1a3a0d5e 100644 --- a/src/object/auto/autofactory.cpp +++ b/src/object/auto/autofactory.cpp @@ -385,13 +385,11 @@ bool CAutoFactory::EventProcess(const Event &event) } vehicle = SearchVehicle(); - if ( vehicle != 0 ) + if ( vehicle != nullptr ) { - physics = vehicle->GetPhysics(); - if ( physics != 0 ) - { - physics->SetFreeze(false); // can move - } + assert(vehicle->Implements(ObjectInterfaceType::Movable)); + physics = dynamic_cast(vehicle)->GetPhysics(); + physics->SetFreeze(false); // can move vehicle->SetLock(false); // vehicle useable vehicle->SetRotationY(m_object->GetRotationY()+Math::PI); @@ -656,11 +654,9 @@ bool CAutoFactory::CreateVehicle() vehicle->SetLock(true); // not usable - CPhysics* physics = vehicle->GetPhysics(); - if ( physics != nullptr ) - { - physics->SetFreeze(true); // it doesn't move - } + assert(vehicle->Implements(ObjectInterfaceType::Movable)); + CPhysics* physics = dynamic_cast(vehicle)->GetPhysics(); + physics->SetFreeze(true); // it doesn't move if (vehicle->Implements(ObjectInterfaceType::Programmable)) { diff --git a/src/object/auto/autorepair.cpp b/src/object/auto/autorepair.cpp index 9164015c..726c98e9 100644 --- a/src/object/auto/autorepair.cpp +++ b/src/object/auto/autorepair.cpp @@ -268,8 +268,7 @@ CObject* CAutoRepair::SearchVehicle() type != OBJECT_MOBILEit && type != OBJECT_MOBILEdr ) continue; - CPhysics* physics = obj->GetPhysics(); - if ( physics != nullptr && !physics->GetLand() ) continue; // in flight? + if ( obj->Implements(ObjectInterfaceType::Movable) && !dynamic_cast(obj)->GetPhysics()->GetLand() ) continue; // in flight? Math::Vector oPos = obj->GetPosition(); float dist = Math::Distance(oPos, sPos); @@ -323,4 +322,3 @@ bool CAutoRepair::Read(CLevelParserLine* line) return true; } - diff --git a/src/object/auto/autotower.cpp b/src/object/auto/autotower.cpp index 535c7faf..4b510109 100644 --- a/src/object/auto/autotower.cpp +++ b/src/object/auto/autotower.cpp @@ -279,9 +279,9 @@ CObject* CAutoTower::SearchTarget(Math::Vector &impact) //? if ( g_researchDone & RESEARCH_QUICK ) if ( false ) { - CPhysics* physics = obj->GetPhysics(); - if ( physics != nullptr ) + if ( obj->Implements(ObjectInterfaceType::Movable) ) { + CPhysics* physics = dynamic_cast(obj)->GetPhysics(); float speed = fabs(physics->GetLinMotionX(MO_REASPEED)); if ( speed > 20.0f ) continue; // moving too fast? } diff --git a/src/object/implementation/programmable_impl.cpp b/src/object/implementation/programmable_impl.cpp index 33ccf54c..140a9b29 100644 --- a/src/object/implementation/programmable_impl.cpp +++ b/src/object/implementation/programmable_impl.cpp @@ -410,9 +410,8 @@ void CProgrammableObjectImpl::TraceRecordStart() TraceRecordStop(); } - assert(m_object->Implements(ObjectInterfaceType::Old)); // TODO - CMotionVehicle* motionVehicle = dynamic_cast(dynamic_cast(m_object)->GetMotion()); - assert(motionVehicle != nullptr); + assert(m_object->Implements(ObjectInterfaceType::TraceDrawing)); + CTraceDrawingObject* traceDrawing = dynamic_cast(m_object); m_traceRecord = true; @@ -421,9 +420,9 @@ void CProgrammableObjectImpl::TraceRecordStart() m_tracePos = m_object->GetPosition(); m_traceAngle = m_object->GetRotationY(); - if ( motionVehicle->GetTraceDown() ) // pencil down? + if ( traceDrawing->GetTraceDown() ) // pencil down? { - m_traceColor = motionVehicle->GetTraceColor(); + m_traceColor = traceDrawing->GetTraceColor(); } else // pen up? { @@ -442,11 +441,10 @@ void CProgrammableObjectImpl::TraceRecordFrame() Math::Vector pos; float angle, len, speed; - assert(m_object->Implements(ObjectInterfaceType::Old)); // TODO - CMotionVehicle* motionVehicle = dynamic_cast(dynamic_cast(m_object)->GetMotion()); - assert(motionVehicle != nullptr); + assert(m_object->Implements(ObjectInterfaceType::TraceDrawing)); + CTraceDrawingObject* traceDrawing = dynamic_cast(m_object); - CPhysics* physics = dynamic_cast(m_object)->GetPhysics(); + CPhysics* physics = dynamic_cast(m_object)->GetPhysics(); speed = physics->GetLinMotionX(MO_REASPEED); if ( speed > 0.0f ) oper = TO_ADVANCE; @@ -456,9 +454,9 @@ void CProgrammableObjectImpl::TraceRecordFrame() if ( speed != 0.0f ) oper = TO_TURN; TraceColor color = TraceColor::Default; - if ( motionVehicle->GetTraceDown() ) // pencil down? + if ( traceDrawing->GetTraceDown() ) // pencil down? { - color = motionVehicle->GetTraceColor(); + color = traceDrawing->GetTraceColor(); } if ( oper != m_traceOper || diff --git a/src/object/implementation/programmable_impl.h b/src/object/implementation/programmable_impl.h index f80af53a..e9700335 100644 --- a/src/object/implementation/programmable_impl.h +++ b/src/object/implementation/programmable_impl.h @@ -21,8 +21,7 @@ #include "object/interface/interactive_object.h" #include "object/interface/programmable_object.h" - -#include "object/trace_color.h" +#include "object/interface/trace_drawing_object.h" #include "math/vector.h" diff --git a/src/object/interface/flying_object.h b/src/object/interface/flying_object.h index d7caae08..b8a991a5 100644 --- a/src/object/interface/flying_object.h +++ b/src/object/interface/flying_object.h @@ -27,7 +27,7 @@ * \class CFlyingObject * \brief Interface for objects that can fly */ -class CFlyingObject : CMovableObject +class CFlyingObject : public CMovableObject { public: explicit CFlyingObject(ObjectInterfaceTypes& types) diff --git a/src/object/interface/jet_flying_object.h b/src/object/interface/jet_flying_object.h index a4896b7d..f150540a 100644 --- a/src/object/interface/jet_flying_object.h +++ b/src/object/interface/jet_flying_object.h @@ -27,7 +27,7 @@ * \class CJetFlyingObject * \brief Interface for objects that can fly using a jet engine */ -class CJetFlyingObject : CFlyingObject +class CJetFlyingObject : public CFlyingObject { public: explicit CJetFlyingObject(ObjectInterfaceTypes& types) @@ -38,8 +38,15 @@ public: virtual ~CJetFlyingObject() {} + // TODO: Refactor naming of these functions + //! Sets jet engine heating speed (bigger = slower, 0 for infinite) virtual void SetRange(float range) = 0; //! Returns jet engine heating speed (bigger = slower, 0 for infinite) virtual float GetRange() = 0; + + //! Sets current jet engine heat level (this is actually how much is left before it overheats, so smaller = more hot) + virtual void SetReactorRange(float reactorRange) = 0; + //! Returns current jet engine heat level (this is actually how much is left before it overheats, so smaller = more hot) + virtual float GetReactorRange() = 0; }; diff --git a/src/object/interface/movable_object.h b/src/object/interface/movable_object.h index 1165c051..b4ec4b1b 100644 --- a/src/object/interface/movable_object.h +++ b/src/object/interface/movable_object.h @@ -24,6 +24,10 @@ /** * \class CMovableObject * \brief Interface for objects that can move (have an engine) + * + * TODO: Currently, it just returns pointers to CPhysics and CMotion. + * These classes should be probably merged with CObject, + * and maybe even split into some more interfaces. */ class CMovableObject { @@ -34,4 +38,9 @@ public: } virtual ~CMovableObject() {} + + //! Returns CPhysics instance associated with this object. If the object implements Movable interface, and type != OBJECT_TOTO, this can be assumed to be != nullptr + virtual CPhysics* GetPhysics() = 0; + //! Returns CMotion instance associated with this object. If the object implements Movable interface, this can be assumed to be != nullptr + virtual CMotion* GetMotion() = 0; }; diff --git a/src/object/interface/task_executor_object.h b/src/object/interface/task_executor_object.h index ac3e25ea..49c92290 100644 --- a/src/object/interface/task_executor_object.h +++ b/src/object/interface/task_executor_object.h @@ -21,7 +21,8 @@ #include "object/object.h" #include "object/object_interface_type.h" -#include "object/trace_color.h" + +#include "object/interface/trace_drawing_object.h" #include "object/task/taskflag.h" #include "object/task/taskgoto.h" diff --git a/src/object/trace_color.cpp b/src/object/interface/trace_drawing_object.cpp similarity index 97% rename from src/object/trace_color.cpp rename to src/object/interface/trace_drawing_object.cpp index 8a4ca457..14c2efb1 100644 --- a/src/object/trace_color.cpp +++ b/src/object/interface/trace_drawing_object.cpp @@ -16,7 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://gnu.org/licenses */ -#include "object/trace_color.h" + +#include "object/interface/trace_drawing_object.h" std::string TraceColorName(TraceColor color) { diff --git a/src/object/trace_color.h b/src/object/interface/trace_drawing_object.h similarity index 62% rename from src/object/trace_color.h rename to src/object/interface/trace_drawing_object.h index c9804943..028ea5d1 100644 --- a/src/object/trace_color.h +++ b/src/object/interface/trace_drawing_object.h @@ -16,11 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://gnu.org/licenses */ + #pragma once -#include +#include "object/object_interface_type.h" -enum class TraceColor : int +enum class TraceColor { Default = -1, @@ -44,5 +45,26 @@ enum class TraceColor : int RedArrow = 17, Max, }; - std::string TraceColorName(TraceColor c); + +/** + * \class CTraceDrawingObject + * \brief Interface for objects that can draw wheel trace (at the moment, all movable objects) + */ +class CTraceDrawingObject +{ +public: + explicit CTraceDrawingObject(ObjectInterfaceTypes& types) + { + types[static_cast(ObjectInterfaceType::TraceDrawing)] = true; + } + virtual ~CTraceDrawingObject() + {} + + virtual bool GetTraceDown() = 0; + virtual void SetTraceDown(bool down) = 0; + virtual TraceColor GetTraceColor() = 0; + virtual void SetTraceColor(TraceColor color) = 0; + virtual float GetTraceWidth() = 0; + virtual void SetTraceWidth(float width) = 0; +}; diff --git a/src/object/mainmovie.cpp b/src/object/mainmovie.cpp index f186468e..8094f49e 100644 --- a/src/object/mainmovie.cpp +++ b/src/object/mainmovie.cpp @@ -27,6 +27,8 @@ #include "object/object.h" #include "object/robotmain.h" +#include "object/interface/movable_object.h" + #include "object/motion/motionhuman.h" @@ -67,7 +69,6 @@ bool CMainMovie::Start(MainMovieType type, float time) Math::Matrix* mat; Math::Vector pos; CObject* pObj; - CMotion* motion; m_type = type; m_speed = 1.0f/time; @@ -76,17 +77,14 @@ bool CMainMovie::Start(MainMovieType type, float time) if ( m_type == MM_SATCOMopen ) { pObj = m_main->SearchHuman(); - if ( pObj == 0 ) + if ( pObj == nullptr ) { m_type = MM_NONE; // it's over! return true; } - motion = pObj->GetMotion(); - if ( motion != 0 ) - { - motion->SetAction(MHS_SATCOM, 0.5f); // reads the SatCom - } + assert(pObj->Implements(ObjectInterfaceType::Movable)); + dynamic_cast(pObj)->GetMotion()->SetAction(MHS_SATCOM, 0.5f); // reads the SatCom m_camera->GetCamera(m_initialEye, m_initialLookat); m_camera->SetType(Gfx::CAM_TYPE_SCRIPT); @@ -105,13 +103,10 @@ bool CMainMovie::Start(MainMovieType type, float time) if ( m_type == MM_SATCOMclose ) { pObj = m_main->SearchHuman(); - if ( pObj != 0 ) + if ( pObj != nullptr ) { - motion = pObj->GetMotion(); - if ( motion != 0 ) - { - motion->SetAction(-1); // finishes reading SatCom - } + assert(pObj->Implements(ObjectInterfaceType::Movable)); + dynamic_cast(pObj)->GetMotion()->SetAction(-1); // finishes reading SatCom } m_camera->SetType(Gfx::CAM_TYPE_BACK); @@ -126,18 +121,14 @@ bool CMainMovie::Start(MainMovieType type, float time) bool CMainMovie::Stop() { CObject* pObj; - CMotion* motion; if ( m_type == MM_SATCOMopen ) { pObj = m_main->SearchHuman(); - if ( pObj != 0 ) + if ( pObj != nullptr ) { - motion = pObj->GetMotion(); - if ( motion != 0 ) - { - motion->SetAction(-1); // finishes reading SatCom - } + assert(pObj->Implements(ObjectInterfaceType::Movable)); + dynamic_cast(pObj)->GetMotion()->SetAction(-1); // finishes reading SatCom } } @@ -232,4 +223,3 @@ MainMovieType CMainMovie::GetStopType() { return m_stopType; } - diff --git a/src/object/motion/motionvehicle.cpp b/src/object/motion/motionvehicle.cpp index 7d77f9ff..c7160f0d 100644 --- a/src/object/motion/motionvehicle.cpp +++ b/src/object/motion/motionvehicle.cpp @@ -68,10 +68,6 @@ CMotionVehicle::CMotionVehicle(COldObject* object) : CMotion(object) m_wheelLastAngle = Math::Vector(0.0f, 0.0f, 0.0f); m_posKey = Math::Vector(0.0f, 0.0f, 0.0f); m_bFlyFix = false; - - m_bTraceDown = false; - m_traceColor = TraceColor::Black; // black - m_traceWidth = 0.5f; } // Object's destructor. @@ -1860,37 +1856,3 @@ void CMotionVehicle::UpdateTrackMapping(float left, float right, ObjectType type } } - - - -// State management of the pencil drawing robot. - -bool CMotionVehicle::GetTraceDown() -{ - return m_bTraceDown; -} - -void CMotionVehicle::SetTraceDown(bool bDown) -{ - m_bTraceDown = bDown; -} - -TraceColor CMotionVehicle::GetTraceColor() -{ - return m_traceColor; -} - -void CMotionVehicle::SetTraceColor(TraceColor color) -{ - m_traceColor = color; -} - -float CMotionVehicle::GetTraceWidth() -{ - return m_traceWidth; -} - -void CMotionVehicle::SetTraceWidth(float width) -{ - m_traceWidth = width; -} diff --git a/src/object/motion/motionvehicle.h b/src/object/motion/motionvehicle.h index 65bff6d8..1beff877 100644 --- a/src/object/motion/motionvehicle.h +++ b/src/object/motion/motionvehicle.h @@ -22,9 +22,6 @@ #include "object/motion/motion.h" -#include "object/trace_color.h" - - class CMotionVehicle : public CMotion { @@ -36,13 +33,6 @@ public: void Create(Math::Vector pos, float angle, ObjectType type, float power, Gfx::COldModelManager* modelManager); bool EventProcess(const Event &event); - bool GetTraceDown(); - void SetTraceDown(bool bDown); - TraceColor GetTraceColor(); - void SetTraceColor(TraceColor color); - float GetTraceWidth(); - void SetTraceWidth(float width); - protected: void CreatePhysics(ObjectType type); bool EventFrame(const Event &event); @@ -64,7 +54,4 @@ protected: Math::Vector m_wheelLastAngle; Math::Vector m_posKey; bool m_bFlyFix; - bool m_bTraceDown; - TraceColor m_traceColor; - float m_traceWidth; }; diff --git a/src/object/object_factory.cpp b/src/object/object_factory.cpp index 7e06889f..9b655ff5 100644 --- a/src/object/object_factory.cpp +++ b/src/object/object_factory.cpp @@ -2553,7 +2553,7 @@ CObjectUPtr CObjectFactory::CreateVehicle(const ObjectCreateParams& params) { auto motion = MakeUnique(obj.get()); motion->Create(pos, angle, type, 1.0f, m_oldModelManager); - obj->SetMotion(std::move(motion)); + obj->SetMovable(std::move(motion), nullptr); return std::move(obj); } @@ -2591,9 +2591,8 @@ CObjectUPtr CObjectFactory::CreateVehicle(const ObjectCreateParams& params) motion->Create(pos, angle, type, power, m_oldModelManager); - obj->SetProgrammable(true); - obj->SetMotion(std::move(motion)); - obj->SetPhysics(std::move(physics)); + obj->SetProgrammable(); + obj->SetMovable(std::move(motion), std::move(physics)); return std::move(obj); } @@ -2641,9 +2640,8 @@ CObjectUPtr CObjectFactory::CreateInsect(const ObjectCreateParams& params) motion->Create(pos, angle, type, 0.0f, m_oldModelManager); - obj->SetMotion(std::move(motion)); - obj->SetPhysics(std::move(physics)); - obj->SetProgrammable(true); + obj->SetProgrammable(); + obj->SetMovable(std::move(motion), std::move(physics)); return std::move(obj); } diff --git a/src/object/object_interface_type.h b/src/object/object_interface_type.h index 3893b331..03ed4ddc 100644 --- a/src/object/object_interface_type.h +++ b/src/object/object_interface_type.h @@ -46,6 +46,7 @@ enum class ObjectInterfaceType Controllable, //!< objects that can be selected and controlled by the player PowerContainer, //!< objects that hold power Ranged, //!< objects that have a operation range to be displayed after pressing button in the UI + TraceDrawing, //!< objects that can draw wheel trace Old, //!< old objects, TODO: remove once no longer necessary Max //!< maximum value (for getting number of items in enum) }; diff --git a/src/object/object_manager.cpp b/src/object/object_manager.cpp index 156ee4a1..dc30e994 100644 --- a/src/object/object_manager.cpp +++ b/src/object/object_manager.cpp @@ -254,7 +254,6 @@ CObject* CObjectManager::Radar(CObject* pThis, Math::Vector thisPosition, float CObject* CObjectManager::Radar(CObject* pThis, Math::Vector thisPosition, float thisAngle, std::vector type, float angle, float focus, float minDist, float maxDist, bool furthest, RadarFilter filter, bool cbotTypes) { CObject *pObj, *pBest; - CPhysics* physics; Math::Vector iPos, oPos; float best, iAngle, d, a; ObjectType oType; @@ -319,13 +318,21 @@ CObject* CObjectManager::Radar(CObject* pThis, Math::Vector thisPosition, float if ( filter_flying == FILTER_ONLYLANDING ) { - physics = pObj->GetPhysics(); - if ( physics != nullptr && !physics->GetLand() ) continue; + if ( pObj->Implements(ObjectInterfaceType::Movable) ) + { + CPhysics* physics = dynamic_cast(pObj)->GetPhysics(); + if ( physics != nullptr ) + { + if ( !physics->GetLand() ) continue; + } + } } if ( filter_flying == FILTER_ONLYFLYING ) { - physics = pObj->GetPhysics(); - if ( physics != nullptr && physics->GetLand() ) continue; + if ( !pObj->Implements(ObjectInterfaceType::Movable) ) continue; + CPhysics* physics = dynamic_cast(pObj)->GetPhysics(); + if ( physics == nullptr ) continue; + if ( physics->GetLand() ) continue; } if ( filter_team != 0 && pObj->GetTeam() != filter_team ) diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index e23cce2f..611ca869 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -86,9 +86,12 @@ COldObject::COldObject(int id) , CControllableObject(m_implementedInterfaces) , CPowerContainerObjectImpl(m_implementedInterfaces, this) , CRangedObject(m_implementedInterfaces) + , CTraceDrawingObject(m_implementedInterfaces) { // A bit of a hack since we don't have subclasses yet, set externally in SetProgrammable() m_implementedInterfaces[static_cast(ObjectInterfaceType::Programmable)] = false; + // Another hack, see SetMovable() + m_implementedInterfaces[static_cast(ObjectInterfaceType::Movable)] = false; // Another hack m_implementedInterfaces[static_cast(ObjectInterfaceType::Jostleable)] = false; @@ -171,6 +174,12 @@ COldObject::COldObject(int id) m_buttonAxe = EVENT_NULL; + m_reactorRange = 1.0f; + + m_traceDown = false; + m_traceColor = TraceColor::Black; + m_traceWidth = 0.5f; + DeleteAllCrashSpheres(); } @@ -2235,6 +2244,18 @@ float COldObject::GetRange() return m_range; } +void COldObject::SetReactorRange(float reactorRange) +{ + if (reactorRange > 1.0f) reactorRange = 1.0f; + if (reactorRange < 0.0f) reactorRange = 0.0f; + m_reactorRange = reactorRange; +} + +float COldObject::GetReactorRange() +{ + return m_reactorRange; +} + // Management of transparency of the object. @@ -2858,17 +2879,6 @@ CPhysics* COldObject::GetPhysics() return m_physics.get(); } -void COldObject::SetPhysics(std::unique_ptr physics) -{ - m_physics = std::move(physics); -} - -// TODO: Temporary hack until we'll have subclasses for objects -void COldObject::SetProgrammable(bool programmable) -{ - m_implementedInterfaces[static_cast(ObjectInterfaceType::Programmable)] = programmable; -} - // Returns the movement associated to the object. CMotion* COldObject::GetMotion() @@ -2876,9 +2886,18 @@ CMotion* COldObject::GetMotion() return m_motion.get(); } -void COldObject::SetMotion(std::unique_ptr motion) +// TODO: Temporary hack until we'll have subclasses for objects +void COldObject::SetProgrammable() +{ + m_implementedInterfaces[static_cast(ObjectInterfaceType::Programmable)] = true; +} + +// TODO: Another hack +void COldObject::SetMovable(std::unique_ptr motion, std::unique_ptr physics) { m_motion = std::move(motion); + m_physics = std::move(physics); + m_implementedInterfaces[static_cast(ObjectInterfaceType::Movable)] = true; } // Returns the controller associated to the object. @@ -3028,10 +3047,10 @@ Error COldObject::StartTaskPen(bool down, TraceColor color) assert(motionVehicle != nullptr); if (color == TraceColor::Default) - color = motionVehicle->GetTraceColor(); + color = GetTraceColor(); - motionVehicle->SetTraceDown(down); - motionVehicle->SetTraceColor(color); + SetTraceDown(down); + SetTraceColor(color); m_physics->SetMotorSpeedX(0.0f); m_physics->SetMotorSpeedY(0.0f); @@ -3190,3 +3209,35 @@ void COldObject::StopProgram() m_motion->SetAction(-1); } + +// State management of the pencil drawing robot. + +bool COldObject::GetTraceDown() +{ + return m_traceDown; +} + +void COldObject::SetTraceDown(bool down) +{ + m_traceDown = down; +} + +TraceColor COldObject::GetTraceColor() +{ + return m_traceColor; +} + +void COldObject::SetTraceColor(TraceColor color) +{ + m_traceColor = color; +} + +float COldObject::GetTraceWidth() +{ + return m_traceWidth; +} + +void COldObject::SetTraceWidth(float width) +{ + m_traceWidth = width; +} diff --git a/src/object/old_object.h b/src/object/old_object.h index fd712fc8..e0b0fc07 100644 --- a/src/object/old_object.h +++ b/src/object/old_object.h @@ -38,6 +38,7 @@ #include "object/interface/programmable_object.h" #include "object/interface/ranged_object.h" #include "object/interface/task_executor_object.h" +#include "object/interface/trace_drawing_object.h" #include "object/interface/transportable_object.h" #include "object/implementation/power_container_impl.h" @@ -82,16 +83,16 @@ class COldObject : public CObject, public CJetFlyingObject, public CControllableObject, public CPowerContainerObjectImpl, - public CRangedObject + public CRangedObject, + public CTraceDrawingObject { friend class CObjectFactory; friend class CObjectManager; protected: void DeleteObject(bool bAll=false); - void SetPhysics(std::unique_ptr physics); - void SetProgrammable(bool programmable); - void SetMotion(std::unique_ptr motion); + void SetProgrammable(); + void SetMovable(std::unique_ptr motion, std::unique_ptr physics); void SetAuto(std::unique_ptr automat); void SetOption(int option); void SetJostlingSphere(const Math::Sphere& sphere); @@ -198,6 +199,9 @@ public: void SetRange(float delay) override; float GetRange() override; + void SetReactorRange(float reactorRange) override; + float GetReactorRange() override; + void SetTransparency(float value) override; void SetFixed(bool bFixed) override; @@ -305,6 +309,13 @@ public: void StopProgram() override; + bool GetTraceDown() override; + void SetTraceDown(bool down) override; + TraceColor GetTraceColor() override; + void SetTraceColor(TraceColor color) override; + float GetTraceWidth() override; + void SetTraceWidth(float width) override; + protected: bool EventFrame(const Event &event); void VirusFrame(float rTime); @@ -394,4 +405,10 @@ protected: float m_time; float m_burnTime; + + float m_reactorRange; + + bool m_traceDown; + TraceColor m_traceColor; + float m_traceWidth; }; diff --git a/src/object/old_object_interface.cpp b/src/object/old_object_interface.cpp index e3c70eb1..5ce3d845 100644 --- a/src/object/old_object_interface.cpp +++ b/src/object/old_object_interface.cpp @@ -211,16 +211,6 @@ bool COldObjectInterface::GetActive() } -CPhysics* COldObjectInterface::GetPhysics() -{ - throw std::logic_error("GetPhysics: not implemented!"); -} - -CMotion* COldObjectInterface::GetMotion() -{ - throw std::logic_error("GetMotion: not implemented!"); -} - CAuto* COldObjectInterface::GetAuto() { throw std::logic_error("GetAuto: not implemented!"); diff --git a/src/object/old_object_interface.h b/src/object/old_object_interface.h index 69549144..b23ad4ef 100644 --- a/src/object/old_object_interface.h +++ b/src/object/old_object_interface.h @@ -126,9 +126,6 @@ public: virtual bool GetRuin(); virtual bool GetActive(); - virtual CPhysics* GetPhysics(); - virtual CMotion* GetMotion(); - // This will be eventually removed after refactoring to subclasses virtual CAuto* GetAuto(); diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 7bb1d159..8f258e43 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -1241,9 +1241,8 @@ void CRobotMain::ExecuteCmd(char *cmd) object->SetShield(1.0f); - CPhysics* physics = object->GetPhysics(); - if (physics != nullptr) - physics->SetReactorRange(1.0f); + if (object->Implements(ObjectInterfaceType::JetFlying)) + dynamic_cast(object)->SetReactorRange(1.0f); } return; } @@ -1277,9 +1276,8 @@ void CRobotMain::ExecuteCmd(char *cmd) CObject* object = GetSelect(); if (object != nullptr) { - CPhysics* physics = object->GetPhysics(); - if (physics != nullptr) - physics->SetReactorRange(1.0f); + if (object->Implements(ObjectInterfaceType::JetFlying)) + dynamic_cast(object)->SetReactorRange(1.0f); } return; } @@ -1416,8 +1414,8 @@ void CRobotMain::StartDisplayInfo(int index, bool movie) if (!m_editLock && movie && !m_movie->IsExist() && human) { - CMotion* motion = obj->GetMotion(); - if (motion != nullptr && motion->GetAction() == -1) + assert(obj->Implements(ObjectInterfaceType::Movable)); + if (dynamic_cast(obj)->GetMotion()->GetAction() == -1) { m_movieInfoIndex = index; m_movie->Start(MM_SATCOMopen, 2.5f); @@ -1784,9 +1782,9 @@ void CRobotMain::SelectOneObject(CObject* obj, bool displayError) CObject* toto = SearchToto(); if (toto != nullptr) { - CMotionToto* mt = static_cast(toto->GetMotion()); - if (mt != nullptr) - mt->SetLinkType(type); + assert(toto->Implements(ObjectInterfaceType::Movable)); + CMotionToto* mt = static_cast(dynamic_cast(toto)->GetMotion()); + mt->SetLinkType(type); } } @@ -2848,9 +2846,9 @@ void CRobotMain::ScenePerso() { obj->SetDrawFront(true); // draws the interface - CMotionHuman* mh = static_cast(obj->GetMotion()); - if (mh != nullptr) - mh->StartDisplayPerso(); + assert(obj->Implements(ObjectInterfaceType::Movable)); + CMotionHuman* mh = static_cast(dynamic_cast(obj)->GetMotion()); + mh->StartDisplayPerso(); } } @@ -3572,7 +3570,8 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) if (m_fixScene && type == OBJECT_HUMAN) { - CMotion* motion = obj->GetMotion(); + assert(obj->Implements(ObjectInterfaceType::Movable)); + CMotion* motion = dynamic_cast(obj)->GetMotion(); if (m_phase == PHASE_WIN ) motion->SetAction(MHS_WIN, 0.4f); if (m_phase == PHASE_LOST) motion->SetAction(MHS_LOST, 0.5f); } diff --git a/src/object/task/taskgoto.cpp b/src/object/task/taskgoto.cpp index d2516159..d187dd1a 100644 --- a/src/object/task/taskgoto.cpp +++ b/src/object/task/taskgoto.cpp @@ -767,7 +767,7 @@ Error CTaskGoto::IsEnded() if ( m_phase == TGP_BEAMWCOLD ) // expects cool reactor? { if ( m_altitude != 0.0f && - m_physics->GetReactorRange() < 1.0f ) return ERR_CONTINUE; + (m_object->Implements(ObjectInterfaceType::JetFlying) && dynamic_cast(m_object)->GetReactorRange() < 1.0f) ) return ERR_CONTINUE; m_phase = TGP_BEAMUP; } @@ -789,7 +789,7 @@ Error CTaskGoto::IsEnded() if ( m_phase == TGP_BEAMGOTO ) // goto dot list ? { if ( m_altitude != 0.0f && - m_physics->GetReactorRange() < 0.1f ) // overheating? + (m_object->Implements(ObjectInterfaceType::JetFlying) && dynamic_cast(m_object)->GetReactorRange() < 0.1f) ) // overheating? { m_physics->SetMotorSpeedX(0.0f); // stops the advance m_physics->SetMotorSpeedZ(0.0f); // stops the rotation diff --git a/src/object/task/taskmanager.h b/src/object/task/taskmanager.h index f30e807d..24a0ed65 100644 --- a/src/object/task/taskmanager.h +++ b/src/object/task/taskmanager.h @@ -21,7 +21,7 @@ #pragma once -#include "object/trace_color.h" +#include "object/interface/trace_drawing_object.h" #include "object/task/task.h" #include "object/task/taskflag.h" diff --git a/src/object/task/taskpen.h b/src/object/task/taskpen.h index 4354a2d6..baa2ed5f 100644 --- a/src/object/task/taskpen.h +++ b/src/object/task/taskpen.h @@ -24,9 +24,9 @@ #include "object/task/task.h" -#include "math/vector.h" +#include "object/interface/trace_drawing_object.h" -#include "object/trace_color.h" +#include "math/vector.h" enum TaskPenPhase diff --git a/src/object/task/taskterraform.cpp b/src/object/task/taskterraform.cpp index df3e7f1b..c4e2ac7a 100644 --- a/src/object/task/taskterraform.cpp +++ b/src/object/task/taskterraform.cpp @@ -370,8 +370,8 @@ bool CTaskTerraform::Terraform() } else { - motion = pObj->GetMotion(); - if ( motion == 0 ) continue; + if ( !pObj->Implements(ObjectInterfaceType::Movable) == 0 ) continue; + motion = dynamic_cast(pObj)->GetMotion(); dist = Math::Distance(m_terraPos, pObj->GetPosition()); if ( dist > ACTION_RADIUS ) continue; diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index c538d83b..9643020f 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -99,7 +99,6 @@ CPhysics::CPhysics(COldObject* object) m_bWheelParticleBrake = false; m_absorbWater = 0.0f; m_reactorTemperature = 0.0f; - m_reactorRange = 1.0f; m_timeReactorFail = 0.0f; m_lastEnergy = 0.0f; m_lastSoundWater = 0.0f; @@ -164,7 +163,7 @@ bool CPhysics::Write(CLevelParserLine* line) { if ( m_object->Implements(ObjectInterfaceType::JetFlying) ) { - line->AddParam("reactorRange", MakeUnique(GetReactorRange())); + line->AddParam("reactorRange", MakeUnique(m_object->GetReactorRange())); } line->AddParam("land", MakeUnique(GetLand())); } @@ -182,7 +181,7 @@ bool CPhysics::Read(CLevelParserLine* line) { if ( m_object->Implements(ObjectInterfaceType::JetFlying) ) { - SetReactorRange(line->GetParam("reactorRange")->AsFloat()); + m_object->SetReactorRange(line->GetParam("reactorRange")->AsFloat()); } SetLand(line->GetParam("land")->AsBool()); } @@ -292,19 +291,6 @@ bool CPhysics::GetFreeze() } -// Returns the range of the reactor. - -void CPhysics::SetReactorRange(float range) -{ - m_reactorRange = range; -} - -float CPhysics::GetReactorRange() -{ - return m_reactorRange; -} - - // Specifies the engine speed. // x = forward/backward // y = up/down @@ -862,15 +848,15 @@ void CPhysics::MotorUpdate(float aTime, float rTime) if ( m_object->Implements(ObjectInterfaceType::JetFlying) && dynamic_cast(m_object)->GetRange() > 0.0f ) // limited flight range? { + CJetFlyingObject* jetFlying = dynamic_cast(m_object); if ( m_bLand || m_bSwim || m_bObstacle ) // on the ground or in the water? { factor = 1.0f; if ( m_bObstacle ) factor = 3.0f; // in order to leave! if ( m_bSwim ) factor = 3.0f; // cools faster in water - m_reactorRange += rTime*(1.0f/5.0f)*factor; - if ( m_reactorRange > 1.0f ) + jetFlying->SetReactorRange(jetFlying->GetReactorRange() + rTime*(1.0f/5.0f)*factor); + if ( jetFlying->GetReactorRange() == 1.0f ) { - m_reactorRange = 1.0f; if ( m_bLowLevel && m_object->GetSelect() ) // beep cool? { m_sound->Play(SOUND_INFO, m_object->GetPosition(), 1.0f, 2.0f); @@ -881,12 +867,11 @@ void CPhysics::MotorUpdate(float aTime, float rTime) } else // in flight? { - m_reactorRange -= rTime*(1.0f/dynamic_cast(m_object)->GetRange()); - if ( m_reactorRange < 0.0f ) m_reactorRange = 0.0f; - if ( m_reactorRange < 0.5f ) m_bLowLevel = true; + jetFlying->SetReactorRange(jetFlying->GetReactorRange() - rTime*(1.0f/jetFlying->GetRange())); + if ( jetFlying->GetReactorRange() < 0.5f ) m_bLowLevel = true; } - if ( m_reactorRange == 0.0f ) // reactor tilt? + if ( jetFlying->GetReactorRange() == 0.0f ) // reactor tilt? { motorSpeed.y = -1.0f; // grave SetFalling(); @@ -966,7 +951,12 @@ void CPhysics::MotorUpdate(float aTime, float rTime) pos = m_object->GetPosition(); h = m_terrain->GetFlyingLimit(pos, type==OBJECT_BEE); h += m_object->GetCharacter()->height; - if ( motorSpeed.y > 0.0f && m_reactorRange > 0.1f && pos.y < h ) + bool reactorCool = true; + if ( m_object->Implements(ObjectInterfaceType::JetFlying) ) + { + reactorCool = dynamic_cast(m_object)->GetReactorRange() > 0.1f; + } + if ( motorSpeed.y > 0.0f && reactorCool && pos.y < h ) { m_bLand = false; // take off SetMotor(true); @@ -2103,7 +2093,7 @@ void CPhysics::SoundReactorFull(float rTime, ObjectType type) m_soundChannelSlide = -1; } - if ( m_reactorRange > 0.0f ) + if ( !m_object->Implements(ObjectInterfaceType::JetFlying) || dynamic_cast(m_object)->GetReactorRange() > 0.0f ) { if ( m_soundChannel == -1 ) { @@ -2384,10 +2374,13 @@ void CPhysics::FloorAdapt(float aTime, float rTime, if ( m_floorHeight == 0.0f ) // ground plate? { - CMotionVehicle* motionVehicle = dynamic_cast(m_motion); - if (motionVehicle != nullptr && motionVehicle->GetTraceDown()) + CTraceDrawingObject* traceDrawing = nullptr; + if (m_object->Implements(ObjectInterfaceType::TraceDrawing)) + traceDrawing = dynamic_cast(m_object); + + if (traceDrawing != nullptr && traceDrawing->GetTraceDown()) { - WheelParticle(motionVehicle->GetTraceColor(), motionVehicle->GetTraceWidth()*g_unit); + WheelParticle(traceDrawing->GetTraceColor(), traceDrawing->GetTraceWidth()*g_unit); } else { @@ -2461,7 +2454,6 @@ void CPhysics::FloorAngle(const Math::Vector &pos, Math::Vector &angle) int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) { - CPhysics* ph; Math::Matrix matRotate; Math::Vector iPos, oAngle, oSpeed; float distance, force, volume; @@ -2616,8 +2608,11 @@ int CPhysics::ObjectAdapt(const Math::Vector &pos, const Math::Vector &angle) m_linMotion.currentSpeed.y = 0.0f; } - ph = pObj->GetPhysics(); - if ( ph != 0 ) + + CPhysics* ph = nullptr; + if (pObj->Implements(ObjectInterfaceType::Movable)) + ph = dynamic_cast(pObj)->GetPhysics(); + if ( ph != nullptr ) { oAngle = pObj->GetRotation(); oSpeed = Normalize(oPos-iPos)*force; @@ -3281,7 +3276,7 @@ void CPhysics::MotorParticle(float aTime, float rTime) } else // in flight? { - if ( !m_bMotor || m_reactorRange == 0.0f ) return; + if ( !m_bMotor || (m_object->Implements(ObjectInterfaceType::JetFlying) && dynamic_cast(m_object)->GetReactorRange() == 0.0f) ) return; if ( m_reactorTemperature < 1.0f ) // not too hot? { @@ -3411,7 +3406,7 @@ void CPhysics::MotorParticle(float aTime, float rTime) } else // in flight? { - if ( !m_bMotor || m_reactorRange == 0.0f ) return; + if ( !m_bMotor || (m_object->Implements(ObjectInterfaceType::JetFlying) && dynamic_cast(m_object)->GetReactorRange() == 0.0f) ) return; if ( aTime-m_lastMotorParticle < m_engine->ParticleAdapt(0.02f) ) return; m_lastMotorParticle = aTime; diff --git a/src/physics/physics.h b/src/physics/physics.h index a0ca986c..fb4e2645 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -30,7 +30,8 @@ #include "math/vector.h" #include "object/object_type.h" -#include "object/trace_color.h" + +#include "object/interface/trace_drawing_object.h" class CObject; @@ -148,8 +149,6 @@ public: bool GetCollision(); void SetFreeze(bool bFreeze); bool GetFreeze(); - void SetReactorRange(float range); - float GetReactorRange(); void SetMotorSpeed(Math::Vector speed); void SetMotorSpeedX(float speed); @@ -236,7 +235,6 @@ protected: Math::Vector m_wheelParticlePos[2]; float m_absorbWater; float m_reactorTemperature; - float m_reactorRange; float m_timeReactorFail; float m_timeUnderWater; float m_lastEnergy; diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 21e388cc..81e69e52 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -38,7 +38,6 @@ #include "object/object.h" #include "object/object_manager.h" #include "object/robotmain.h" -#include "object/trace_color.h" #include "object/auto/auto.h" #include "object/auto/autobase.h" @@ -46,11 +45,10 @@ #include "object/interface/programmable_object.h" #include "object/interface/task_executor_object.h" +#include "object/interface/trace_drawing_object.h" #include "object/level/parser.h" -#include "object/motion/motionvehicle.h" - #include "object/subclass/exchange_post.h" #include "object/task/taskmanager.h" @@ -2886,8 +2884,18 @@ bool CScriptFunctions::rPenDown(CBotVar* var, CBotVar* result, int& exception, v float width; Error err; - CMotionVehicle* motionVehicle = dynamic_cast(pThis->GetMotion()); - assert(motionVehicle != nullptr); + if (!pThis->Implements(ObjectInterfaceType::TraceDrawing)) + { + result->SetValInt(ERR_WRONG_OBJ); + if ( script->m_errMode == ERM_STOP ) + { + exception = ERR_WRONG_OBJ; + return false; + } + return true; + } + + CTraceDrawingObject* traceDrawing = dynamic_cast(pThis); exception = 0; @@ -2896,7 +2904,7 @@ bool CScriptFunctions::rPenDown(CBotVar* var, CBotVar* result, int& exception, v color = var->GetValInt(); if ( color < 0 ) color = 0; if ( color > static_cast(TraceColor::Max) ) color = static_cast(TraceColor::Max); - motionVehicle->SetTraceColor(static_cast(color)); + traceDrawing->SetTraceColor(static_cast(color)); var = var->GetNext(); if ( var != 0 ) @@ -2904,16 +2912,16 @@ bool CScriptFunctions::rPenDown(CBotVar* var, CBotVar* result, int& exception, v width = var->GetValFloat(); if ( width < 0.1f ) width = 0.1f; if ( width > 1.0f ) width = 1.0f; - motionVehicle->SetTraceWidth(width); + traceDrawing->SetTraceWidth(width); } } - motionVehicle->SetTraceDown(true); + traceDrawing->SetTraceDown(true); if ( pThis->GetType() == OBJECT_MOBILEdr ) { if ( !script->m_taskExecutor->IsForegroundTask() ) // no task in progress? { - err = script->m_taskExecutor->StartTaskPen(motionVehicle->GetTraceDown(), motionVehicle->GetTraceColor()); + err = script->m_taskExecutor->StartTaskPen(traceDrawing->GetTraceDown(), traceDrawing->GetTraceColor()); if ( err != ERR_OK ) { script->m_taskExecutor->StopForegroundTask(); @@ -2942,20 +2950,27 @@ bool CScriptFunctions::rPenUp(CBotVar* var, CBotVar* result, int& exception, voi CObject* pThis = script->m_object; Error err; - CMotionVehicle* motionVehicle = dynamic_cast(pThis->GetMotion()); - assert(motionVehicle != nullptr); - exception = 0; - motionVehicle->SetTraceDown(false); + if (!pThis->Implements(ObjectInterfaceType::TraceDrawing)) + { + result->SetValInt(ERR_WRONG_OBJ); + if ( script->m_errMode == ERM_STOP ) + { + exception = ERR_WRONG_OBJ; + return false; + } + return true; + } + + CTraceDrawingObject* traceDrawing = dynamic_cast(pThis); + traceDrawing->SetTraceDown(false); if ( pThis->GetType() == OBJECT_MOBILEdr ) { if ( !script->m_taskExecutor->IsForegroundTask() ) // no task in progress? { - motionVehicle->SetTraceDown(false); - - err = script->m_taskExecutor->StartTaskPen(motionVehicle->GetTraceDown(), motionVehicle->GetTraceColor()); + err = script->m_taskExecutor->StartTaskPen(traceDrawing->GetTraceDown(), traceDrawing->GetTraceColor()); if ( err != ERR_OK ) { script->m_taskExecutor->StopForegroundTask(); @@ -2985,21 +3000,31 @@ bool CScriptFunctions::rPenColor(CBotVar* var, CBotVar* result, int& exception, int color; Error err; - CMotionVehicle* motionVehicle = dynamic_cast(pThis->GetMotion()); - assert(motionVehicle != nullptr); - exception = 0; + if (!pThis->Implements(ObjectInterfaceType::TraceDrawing)) + { + result->SetValInt(ERR_WRONG_OBJ); + if ( script->m_errMode == ERM_STOP ) + { + exception = ERR_WRONG_OBJ; + return false; + } + return true; + } + + CTraceDrawingObject* traceDrawing = dynamic_cast(pThis); + color = var->GetValInt(); if ( color < 0 ) color = 0; if ( color > static_cast(TraceColor::Max) ) color = static_cast(TraceColor::Max); - motionVehicle->SetTraceColor(static_cast(color)); + traceDrawing->SetTraceColor(static_cast(color)); if ( pThis->GetType() == OBJECT_MOBILEdr ) { if ( !script->m_taskExecutor->IsForegroundTask() ) // no task in progress? { - err = script->m_taskExecutor->StartTaskPen(motionVehicle->GetTraceDown(), motionVehicle->GetTraceColor()); + err = script->m_taskExecutor->StartTaskPen(traceDrawing->GetTraceDown(), traceDrawing->GetTraceColor()); if ( err != ERR_OK ) { script->m_taskExecutor->StopForegroundTask(); @@ -3024,16 +3049,29 @@ bool CScriptFunctions::rPenColor(CBotVar* var, CBotVar* result, int& exception, bool CScriptFunctions::rPenWidth(CBotVar* var, CBotVar* result, int& exception, void* user) { - CObject* pThis = static_cast(user)->m_object; + CScript* script = static_cast(user); + CObject* pThis = script->m_object; float width; - CMotionVehicle* motionVehicle = dynamic_cast(pThis->GetMotion()); - assert(motionVehicle != nullptr); + exception = 0; + + if (!pThis->Implements(ObjectInterfaceType::TraceDrawing)) + { + result->SetValInt(ERR_WRONG_OBJ); + if ( script->m_errMode == ERM_STOP ) + { + exception = ERR_WRONG_OBJ; + return false; + } + return true; + } + + CTraceDrawingObject* traceDrawing = dynamic_cast(pThis); width = var->GetValFloat(); if ( width < 0.1f ) width = 0.1f; if ( width > 1.0f ) width = 1.0f; - motionVehicle->SetTraceWidth(width); + traceDrawing->SetTraceWidth(width); return true; } @@ -3843,8 +3881,8 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) // Updates the temperature of the reactor. pVar = pVar->GetNext(); // "temperature" - if ( physics == 0 ) value = 0.0f; - else value = 1.0f-physics->GetReactorRange(); + if ( !obj->Implements(ObjectInterfaceType::JetFlying) ) value = 0.0f; + else value = 1.0f-dynamic_cast(object)->GetReactorRange(); pVar->SetValFloat(value); // Updates the height above the ground. @@ -3869,7 +3907,7 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) } else if (power->Implements(ObjectInterfaceType::Old)) { - pVar->SetPointer(dynamic_cast(power)->GetBotVar()); + pVar->SetPointer(power->GetBotVar()); } } @@ -3884,7 +3922,7 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) } else if (cargo->Implements(ObjectInterfaceType::Old)) { - pVar->SetPointer(dynamic_cast(cargo)->GetBotVar()); + pVar->SetPointer(cargo->GetBotVar()); } } diff --git a/src/ui/displayinfo.cpp b/src/ui/displayinfo.cpp index 77e0e84a..70cead18 100644 --- a/src/ui/displayinfo.cpp +++ b/src/ui/displayinfo.cpp @@ -37,6 +37,8 @@ #include "object/object_manager.h" #include "object/robotmain.h" +#include "object/interface/movable_object.h" + #include "object/motion/motion.h" #include "object/motion/motiontoto.h" @@ -74,7 +76,7 @@ CDisplayInfo::CDisplayInfo() m_infoFinalDim = m_infoActualPos = m_infoNormalDim = Math::Point(1.00f, 1.00f); m_lightSuppl = -1; - m_toto = 0; + m_toto = nullptr; m_bSoluce = false; m_initPause = PAUSE_NONE; m_bEditLock = false; @@ -96,7 +98,6 @@ bool CDisplayInfo::EventProcess(const Event &event) Ui::CWindow* pw; Ui::CEdit* edit; Ui::CSlider* slider; - CMotionToto* toto; if ( event.type == EVENT_FRAME ) { @@ -106,13 +107,12 @@ bool CDisplayInfo::EventProcess(const Event &event) if ( event.type == EVENT_MOUSE_MOVE ) { - if ( m_toto != 0 ) + if ( m_toto != nullptr ) { - toto = static_cast(m_toto->GetMotion()); - if ( toto != 0 ) - { - toto->SetMousePos(event.mousePos); - } + assert(m_toto->Implements(ObjectInterfaceType::Movable)); + CMotionToto* toto = static_cast(dynamic_cast(m_toto)->GetMotion()); + assert(toto != nullptr); + toto->SetMousePos(event.mousePos); } } @@ -346,7 +346,6 @@ void CDisplayInfo::StartDisplayInfo(std::string filename, int index, bool bSoluc Ui::CEdit* edit; Ui::CButton* button; Ui::CSlider* slider; - CMotionToto* toto; m_index = index; m_bSoluce = bSoluce; @@ -446,15 +445,14 @@ void CDisplayInfo::StartDisplayInfo(std::string filename, int index, bool bSoluc m_particle->SetFrameUpdate(Gfx::SH_WORLD, false); // particles break into world m_toto = SearchToto(); - if ( m_toto != 0 ) + if ( m_toto != nullptr ) { m_toto->SetDrawFront(true); - toto = static_cast(m_toto->GetMotion()); - if ( toto != 0 ) - { - toto->StartDisplayInfo(); - } + assert(m_toto->Implements(ObjectInterfaceType::Movable)); + CMotionToto* toto = static_cast(dynamic_cast(m_toto)->GetMotion()); + assert(toto != nullptr); + toto->StartDisplayInfo(); } light.type = Gfx::LIGHT_DIRECTIONAL; @@ -814,7 +812,6 @@ void CDisplayInfo::UpdateCopyButton() void CDisplayInfo::StopDisplayInfo() { Ui::CWindow* pw; - CMotionToto* toto; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW4)); if ( pw == 0 ) return; @@ -842,13 +839,12 @@ void CDisplayInfo::StopDisplayInfo() m_particle->FlushParticle(Gfx::SH_FRONT); m_particle->FlushParticle(Gfx::SH_INTERFACE); - if ( m_toto != 0 ) + if ( m_toto != nullptr ) { - toto = static_cast(m_toto->GetMotion()); - if ( toto != 0 ) - { - toto->StopDisplayInfo(); - } + assert(m_toto->Implements(ObjectInterfaceType::Movable)); + CMotionToto* toto = static_cast(dynamic_cast(m_toto)->GetMotion()); + assert(toto != nullptr); + toto->StopDisplayInfo(); } m_light->DeleteLight(m_lightSuppl); diff --git a/src/ui/displaytext.cpp b/src/ui/displaytext.cpp index 0d3ea9c6..a0e9ca7b 100644 --- a/src/ui/displaytext.cpp +++ b/src/ui/displaytext.cpp @@ -30,6 +30,8 @@ #include "object/object.h" #include "object/object_manager.h" +#include "object/interface/movable_object.h" + #include "object/motion/motion.h" #include "object/motion/motiontoto.h" @@ -250,27 +252,26 @@ void CDisplayText::DisplayText(const char *text, Math::Vector goal, float height m_textLines[nLine] = line; toto = SearchToto(); - if ( toto != 0 ) + if ( toto != nullptr ) { - motion = toto->GetMotion(); - if ( motion != 0 ) + assert(toto->Implements(ObjectInterfaceType::Movable)); + motion = dynamic_cast(toto)->GetMotion(); + + if ( type == TT_ERROR ) { - if ( type == TT_ERROR ) - { - motion->SetAction(MT_ERROR, 4.0f); - } - if ( type == TT_WARNING ) - { - motion->SetAction(MT_WARNING, 4.0f); - } - if ( type == TT_INFO ) - { - motion->SetAction(MT_INFO, 4.0f); - } - if ( type == TT_MESSAGE ) - { - motion->SetAction(MT_MESSAGE, 4.0f); - } + motion->SetAction(MT_ERROR, 4.0f); + } + if ( type == TT_WARNING ) + { + motion->SetAction(MT_WARNING, 4.0f); + } + if ( type == TT_INFO ) + { + motion->SetAction(MT_INFO, 4.0f); + } + if ( type == TT_MESSAGE ) + { + motion->SetAction(MT_MESSAGE, 4.0f); } } diff --git a/src/ui/object_interface.cpp b/src/ui/object_interface.cpp index d38a1357..411bcc44 100644 --- a/src/ui/object_interface.cpp +++ b/src/ui/object_interface.cpp @@ -887,14 +887,7 @@ bool CObjectInterface::CreateInterface(bool bSelect) } } - if ( type == OBJECT_HUMAN || - type == OBJECT_MOBILEfa || - type == OBJECT_MOBILEfc || - type == OBJECT_MOBILEfi || - type == OBJECT_MOBILEfs || - type == OBJECT_MOBILEft || - type == OBJECT_BEE || - type == OBJECT_CONTROLLER) // driving? + if ( m_object->Implements(ObjectInterfaceType::Flying) ) { pos.x = ox+sx*6.4f; pos.y = oy+sy*0; @@ -906,15 +899,16 @@ bool CObjectInterface::CreateInterface(bool bSelect) pb = pw->CreateButton(pos, dim, 28, EVENT_OBJECT_GASUP); pb->SetImmediat(true); - if ( (type != OBJECT_HUMAN && - type != OBJECT_CONTROLLER) || - m_object->GetOption() != 2 ) + if ( m_object->Implements(ObjectInterfaceType::JetFlying) ) { - pos.x = ox+sx*15.3f; - pos.y = oy+sy*0; - ddim.x = 14.0f/640.0f; - ddim.y = 66.0f/480.0f; - pw->CreateGauge(pos, ddim, 2, EVENT_OBJECT_GRANGE); + if ( type != OBJECT_HUMAN || m_object->GetOption() != 2 ) // if not Me without a jetpack, display reactor temperature + { + pos.x = ox+sx*15.3f; + pos.y = oy+sy*0; + ddim.x = 14.0f/640.0f; + ddim.y = 66.0f/480.0f; + pw->CreateGauge(pos, ddim, 2, EVENT_OBJECT_GRANGE); + } } } @@ -1544,8 +1538,9 @@ void CObjectInterface::UpdateInterface(float rTime) pg = static_cast< CGauge* >(pw->SearchControl(EVENT_OBJECT_GRANGE)); if ( pg != 0 ) { + assert(m_object->Implements(ObjectInterfaceType::JetFlying)); icon = 2; // blue/red - range = m_physics->GetReactorRange(); + range = dynamic_cast(m_object)->GetReactorRange(); if ( range < 0.2f && range != 0.0f && !m_physics->GetLand() ) { @@ -1863,8 +1858,10 @@ void CObjectInterface::UpdateInterface() CheckInterface(pw, EVENT_OBJECT_MFRONT, m_manipStyle==EVENT_OBJECT_MFRONT); } - CMotionVehicle* motionVehicle = dynamic_cast(m_motion); - if (motionVehicle != nullptr && motionVehicle->GetTraceDown()) + CTraceDrawingObject* traceDrawing = nullptr; + if (m_object->Implements(ObjectInterfaceType::TraceDrawing)) + traceDrawing = dynamic_cast(m_object); + if (traceDrawing != nullptr && traceDrawing->GetTraceDown()) { pb = static_cast< CButton* >(pw->SearchControl(EVENT_OBJECT_PEN0)); if ( pb != 0 ) @@ -1872,7 +1869,7 @@ void CObjectInterface::UpdateInterface() pb->ClearState(STATE_CHECK); } - TraceColor color = motionVehicle->GetTraceColor(); + TraceColor color = traceDrawing->GetTraceColor(); pc = static_cast< CColor* >(pw->SearchControl(EVENT_OBJECT_PEN1)); if ( pc != 0 ) {