diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index 66af95b5..f069b12d 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -51,6 +51,7 @@ #include "physics/physics.h" #include "script/cbottoken.h" +#include "script/scriptfunc.h" #include @@ -76,133 +77,6 @@ static float debug_arm3 = 0.0f; #endif - - -// Updates the class Object. - -void uObject(CBotVar* botThis, void* user) -{ - CPhysics* physics; - CBotVar *pVar, *pSub; - ObjectType type; - Math::Vector pos; - float value; - - if ( user == nullptr ) return; - - assert(static_cast(user)->Implements(ObjectInterfaceType::Old)); - COldObject* object = static_cast(user); - - physics = object->GetPhysics(); - - // Updates the object's type. - pVar = botThis->GetItemList(); // "category" - type = object->GetType(); - pVar->SetValInt(type, object->GetName()); - - // Updates the position of the object. - pVar = pVar->GetNext(); // "position" - if (IsObjectBeingTransported(object)) - { - pSub = pVar->GetItemList(); // "x" - pSub->SetInit(CBotVar::InitType::IS_NAN); - pSub = pSub->GetNext(); // "y" - pSub->SetInit(CBotVar::InitType::IS_NAN); - pSub = pSub->GetNext(); // "z" - pSub->SetInit(CBotVar::InitType::IS_NAN); - } - else - { - pos = object->GetPosition(); - float waterLevel = Gfx::CEngine::GetInstancePointer()->GetWater()->GetLevel(); - pos.y -= waterLevel; // relative to sea level! - pSub = pVar->GetItemList(); // "x" - pSub->SetValFloat(pos.x/g_unit); - pSub = pSub->GetNext(); // "y" - pSub->SetValFloat(pos.z/g_unit); - pSub = pSub->GetNext(); // "z" - pSub->SetValFloat(pos.y/g_unit); - } - - // Updates the angle. - pos = object->GetRotation(); - pos += object->GetTilt(); - pVar = pVar->GetNext(); // "orientation" - pVar->SetValFloat(360.0f-Math::Mod(pos.y*180.0f/Math::PI, 360.0f)); - pVar = pVar->GetNext(); // "pitch" - pVar->SetValFloat(pos.z*180.0f/Math::PI); - pVar = pVar->GetNext(); // "roll" - pVar->SetValFloat(pos.x*180.0f/Math::PI); - - // Updates the energy level of the object. - pVar = pVar->GetNext(); // "energyLevel" - value = object->GetEnergy(); - pVar->SetValFloat(value); - - // Updates the shield level of the object. - pVar = pVar->GetNext(); // "shieldLevel" - value = object->GetShield(); - pVar->SetValFloat(value); - - // Updates the temperature of the reactor. - pVar = pVar->GetNext(); // "temperature" - if ( physics == 0 ) value = 0.0f; - else value = 1.0f-physics->GetReactorRange(); - pVar->SetValFloat(value); - - // Updates the height above the ground. - pVar = pVar->GetNext(); // "altitude" - if ( physics == 0 ) value = 0.0f; - else value = physics->GetFloorHeight(); - pVar->SetValFloat(value/g_unit); - - // Updates the lifetime of the object. - pVar = pVar->GetNext(); // "lifeTime" - value = object->GetAbsTime(); - pVar->SetValFloat(value); - - // Updates the type of battery. - pVar = pVar->GetNext(); // "energyCell" - if (object->Implements(ObjectInterfaceType::Powered)) - { - CObject* power = dynamic_cast(object)->GetPower(); - if (power == nullptr) - { - pVar->SetPointer(nullptr); - } - else if (power->Implements(ObjectInterfaceType::Old)) - { - pVar->SetPointer(dynamic_cast(power)->GetBotVar()); - } - } - - // Updates the transported object's type. - pVar = pVar->GetNext(); // "load" - if (object->Implements(ObjectInterfaceType::Carrier)) - { - CObject* cargo = dynamic_cast(object)->GetCargo(); - if (cargo == nullptr) - { - pVar->SetPointer(nullptr); - } - else if (cargo->Implements(ObjectInterfaceType::Old)) - { - pVar->SetPointer(dynamic_cast(cargo)->GetBotVar()); - } - } - - pVar = pVar->GetNext(); // "id" - value = object->GetID(); - pVar->SetValInt(value); - - pVar = pVar->GetNext(); // "team" - value = object->GetTeam(); - pVar->SetValInt(value); -} - - - - // Object's constructor. COldObject::COldObject(int id) @@ -309,27 +183,15 @@ COldObject::COldObject(int id) DeleteAllCrashSpheres(); - CBotClass* bc = CBotClass::Find("object"); - if ( bc != 0 ) - { - bc->AddUpdateFunc(uObject); - } - - m_botVar = CBotVar::Create("", CBotTypResult(CBotTypClass, "object")); - m_botVar->SetUserPtr(this); - m_botVar->SetIdent(m_id); + m_botVar = CScriptFunctions::CreateObjectVar(this); } // Object's destructor. COldObject::~COldObject() { - if ( m_botVar != nullptr ) - { - m_botVar->SetUserPtr(OBJECTDELETED); - delete m_botVar; - m_botVar = nullptr; - } + CScriptFunctions::DestroyObjectVar(m_botVar, true); + m_botVar = nullptr; } @@ -339,10 +201,7 @@ COldObject::~COldObject() void COldObject::DeleteObject(bool bAll) { - if ( m_botVar != 0 ) - { - m_botVar->SetUserPtr(OBJECTDELETED); - } + CScriptFunctions::DestroyObjectVar(m_botVar, false); if ( m_camera->GetControllingObject() == this ) { @@ -729,7 +588,7 @@ bool COldObject::ExplodeObject(ExplosionType type, float force, float decay) m_type == OBJECT_SCRAP4 || m_type == OBJECT_SCRAP5 ) // (*) { - m_botVar->SetUserPtr(OBJECTDELETED); + CScriptFunctions::DestroyObjectVar(m_botVar, false); } } diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 12512ff6..8fa51494 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -3611,6 +3611,7 @@ void CScriptFunctions::Init() bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), PR_READ); bc->AddItem("id", CBotTypResult(CBotTypInt), PR_READ); bc->AddItem("team", CBotTypResult(CBotTypInt), PR_READ); + bc->AddItem("velocity", CBotTypResult(CBotTypClass, "point"), PR_READ); bc->AddFunction("busy", CScriptFunctions::rBusy, CScriptFunctions::cBusy); bc->AddFunction("factory", CScriptFunctions::rFactory, CScriptFunctions::cFactory); bc->AddFunction("research", CScriptFunctions::rResearch, CScriptFunctions::cClassOneFloat); @@ -3724,3 +3725,149 @@ void CScriptFunctions::Init() CBotProgram::AddFunction("canbuild", rCanBuild, CScriptFunctions::cCanBuild); CBotProgram::AddFunction("build", rBuild, CScriptFunctions::cOneFloat); } + + +// Updates the class Object. + +void CScriptFunctions::uObject(CBotVar* botThis, void* user) +{ + CPhysics* physics; + CBotVar *pVar, *pSub; + ObjectType type; + Math::Vector pos; + float value; + + if ( user == nullptr ) return; + + assert(static_cast(user)->Implements(ObjectInterfaceType::Old)); + COldObject* object = static_cast(user); + + physics = object->GetPhysics(); + + // Updates the object's type. + pVar = botThis->GetItemList(); // "category" + type = object->GetType(); + pVar->SetValInt(type, object->GetName()); + + // Updates the position of the object. + pVar = pVar->GetNext(); // "position" + if (IsObjectBeingTransported(object)) + { + pSub = pVar->GetItemList(); // "x" + pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub = pSub->GetNext(); // "y" + pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub = pSub->GetNext(); // "z" + pSub->SetInit(CBotVar::InitType::IS_NAN); + } + else + { + pos = object->GetPosition(); + float waterLevel = Gfx::CEngine::GetInstancePointer()->GetWater()->GetLevel(); + pos.y -= waterLevel; // relative to sea level! + pSub = pVar->GetItemList(); // "x" + pSub->SetValFloat(pos.x/g_unit); + pSub = pSub->GetNext(); // "y" + pSub->SetValFloat(pos.z/g_unit); + pSub = pSub->GetNext(); // "z" + pSub->SetValFloat(pos.y/g_unit); + } + + // Updates the angle. + pos = object->GetRotation(); + pos += object->GetTilt(); + pVar = pVar->GetNext(); // "orientation" + pVar->SetValFloat(360.0f-Math::Mod(pos.y*180.0f/Math::PI, 360.0f)); + pVar = pVar->GetNext(); // "pitch" + pVar->SetValFloat(pos.z*180.0f/Math::PI); + pVar = pVar->GetNext(); // "roll" + pVar->SetValFloat(pos.x*180.0f/Math::PI); + + // Updates the energy level of the object. + pVar = pVar->GetNext(); // "energyLevel" + value = object->GetEnergy(); + pVar->SetValFloat(value); + + // Updates the shield level of the object. + pVar = pVar->GetNext(); // "shieldLevel" + value = object->GetShield(); + pVar->SetValFloat(value); + + // Updates the temperature of the reactor. + pVar = pVar->GetNext(); // "temperature" + if ( physics == 0 ) value = 0.0f; + else value = 1.0f-physics->GetReactorRange(); + pVar->SetValFloat(value); + + // Updates the height above the ground. + pVar = pVar->GetNext(); // "altitude" + if ( physics == 0 ) value = 0.0f; + else value = physics->GetFloorHeight(); + pVar->SetValFloat(value/g_unit); + + // Updates the lifetime of the object. + pVar = pVar->GetNext(); // "lifeTime" + value = object->GetAbsTime(); + pVar->SetValFloat(value); + + // Updates the type of battery. + pVar = pVar->GetNext(); // "energyCell" + if (object->Implements(ObjectInterfaceType::Powered)) + { + CObject* power = dynamic_cast(object)->GetPower(); + if (power == nullptr) + { + pVar->SetPointer(nullptr); + } + else if (power->Implements(ObjectInterfaceType::Old)) + { + pVar->SetPointer(dynamic_cast(power)->GetBotVar()); + } + } + + // Updates the transported object's type. + pVar = pVar->GetNext(); // "load" + if (object->Implements(ObjectInterfaceType::Carrier)) + { + CObject* cargo = dynamic_cast(object)->GetCargo(); + if (cargo == nullptr) + { + pVar->SetPointer(nullptr); + } + else if (cargo->Implements(ObjectInterfaceType::Old)) + { + pVar->SetPointer(dynamic_cast(cargo)->GetBotVar()); + } + } + + pVar = pVar->GetNext(); // "id" + value = object->GetID(); + pVar->SetValInt(value); + + pVar = pVar->GetNext(); // "team" + value = object->GetTeam(); + pVar->SetValInt(value); +} + +CBotVar* CScriptFunctions::CreateObjectVar(CObject* obj) +{ + CBotClass* bc = CBotClass::Find("object"); + if ( bc != 0 ) + { + bc->AddUpdateFunc(CScriptFunctions::uObject); + } + + CBotVar* botVar = CBotVar::Create("", CBotTypResult(CBotTypClass, "object")); + botVar->SetUserPtr(obj); + botVar->SetIdent(obj->GetID()); + return botVar; +} + +void CScriptFunctions::DestroyObjectVar(CBotVar* botVar, bool permanent) +{ + if ( botVar == nullptr ) return; + + botVar->SetUserPtr(OBJECTDELETED); + if(permanent) + delete botVar; +} diff --git a/src/script/scriptfunc.h b/src/script/scriptfunc.h index 33bfe8df..97856eb1 100644 --- a/src/script/scriptfunc.h +++ b/src/script/scriptfunc.h @@ -41,6 +41,8 @@ class CScriptFunctions { public: static void Init(); + static CBotVar* CreateObjectVar(CObject* obj); + static void DestroyObjectVar(CBotVar* botVar, bool permanent); private: static CBotTypResult cNull(CBotVar* &var, void* user); @@ -177,6 +179,8 @@ private: static CBotTypResult cPointConstructor(CBotVar* pThis, CBotVar* &var); static bool rPointConstructor(CBotVar* pThis, CBotVar* var, CBotVar* pResult, int& Exception, void* user); + static void uObject(CBotVar* botThis, void* user); + public: static int m_numberOfOpenFiles;