Moved "object" class management to CScriptFunctions

master
krzys-h 2015-07-22 13:24:16 +02:00
parent cba3863d75
commit abdb4e9782
3 changed files with 157 additions and 147 deletions

View File

@ -51,6 +51,7 @@
#include "physics/physics.h" #include "physics/physics.h"
#include "script/cbottoken.h" #include "script/cbottoken.h"
#include "script/scriptfunc.h"
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
@ -76,133 +77,6 @@ static float debug_arm3 = 0.0f;
#endif #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<CObject*>(user)->Implements(ObjectInterfaceType::Old));
COldObject* object = static_cast<COldObject*>(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<CPoweredObject*>(object)->GetPower();
if (power == nullptr)
{
pVar->SetPointer(nullptr);
}
else if (power->Implements(ObjectInterfaceType::Old))
{
pVar->SetPointer(dynamic_cast<COldObject*>(power)->GetBotVar());
}
}
// Updates the transported object's type.
pVar = pVar->GetNext(); // "load"
if (object->Implements(ObjectInterfaceType::Carrier))
{
CObject* cargo = dynamic_cast<CCarrierObject*>(object)->GetCargo();
if (cargo == nullptr)
{
pVar->SetPointer(nullptr);
}
else if (cargo->Implements(ObjectInterfaceType::Old))
{
pVar->SetPointer(dynamic_cast<COldObject*>(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. // Object's constructor.
COldObject::COldObject(int id) COldObject::COldObject(int id)
@ -309,27 +183,15 @@ COldObject::COldObject(int id)
DeleteAllCrashSpheres(); DeleteAllCrashSpheres();
CBotClass* bc = CBotClass::Find("object"); m_botVar = CScriptFunctions::CreateObjectVar(this);
if ( bc != 0 )
{
bc->AddUpdateFunc(uObject);
}
m_botVar = CBotVar::Create("", CBotTypResult(CBotTypClass, "object"));
m_botVar->SetUserPtr(this);
m_botVar->SetIdent(m_id);
} }
// Object's destructor. // Object's destructor.
COldObject::~COldObject() COldObject::~COldObject()
{ {
if ( m_botVar != nullptr ) CScriptFunctions::DestroyObjectVar(m_botVar, true);
{ m_botVar = nullptr;
m_botVar->SetUserPtr(OBJECTDELETED);
delete m_botVar;
m_botVar = nullptr;
}
} }
@ -339,10 +201,7 @@ COldObject::~COldObject()
void COldObject::DeleteObject(bool bAll) void COldObject::DeleteObject(bool bAll)
{ {
if ( m_botVar != 0 ) CScriptFunctions::DestroyObjectVar(m_botVar, false);
{
m_botVar->SetUserPtr(OBJECTDELETED);
}
if ( m_camera->GetControllingObject() == this ) 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_SCRAP4 ||
m_type == OBJECT_SCRAP5 ) // (*) m_type == OBJECT_SCRAP5 ) // (*)
{ {
m_botVar->SetUserPtr(OBJECTDELETED); CScriptFunctions::DestroyObjectVar(m_botVar, false);
} }
} }

View File

@ -3611,6 +3611,7 @@ void CScriptFunctions::Init()
bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), PR_READ); bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), PR_READ);
bc->AddItem("id", CBotTypResult(CBotTypInt), PR_READ); bc->AddItem("id", CBotTypResult(CBotTypInt), PR_READ);
bc->AddItem("team", 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("busy", CScriptFunctions::rBusy, CScriptFunctions::cBusy);
bc->AddFunction("factory", CScriptFunctions::rFactory, CScriptFunctions::cFactory); bc->AddFunction("factory", CScriptFunctions::rFactory, CScriptFunctions::cFactory);
bc->AddFunction("research", CScriptFunctions::rResearch, CScriptFunctions::cClassOneFloat); bc->AddFunction("research", CScriptFunctions::rResearch, CScriptFunctions::cClassOneFloat);
@ -3724,3 +3725,149 @@ void CScriptFunctions::Init()
CBotProgram::AddFunction("canbuild", rCanBuild, CScriptFunctions::cCanBuild); CBotProgram::AddFunction("canbuild", rCanBuild, CScriptFunctions::cCanBuild);
CBotProgram::AddFunction("build", rBuild, CScriptFunctions::cOneFloat); 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<CObject*>(user)->Implements(ObjectInterfaceType::Old));
COldObject* object = static_cast<COldObject*>(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<CPoweredObject*>(object)->GetPower();
if (power == nullptr)
{
pVar->SetPointer(nullptr);
}
else if (power->Implements(ObjectInterfaceType::Old))
{
pVar->SetPointer(dynamic_cast<COldObject*>(power)->GetBotVar());
}
}
// Updates the transported object's type.
pVar = pVar->GetNext(); // "load"
if (object->Implements(ObjectInterfaceType::Carrier))
{
CObject* cargo = dynamic_cast<CCarrierObject*>(object)->GetCargo();
if (cargo == nullptr)
{
pVar->SetPointer(nullptr);
}
else if (cargo->Implements(ObjectInterfaceType::Old))
{
pVar->SetPointer(dynamic_cast<COldObject*>(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;
}

View File

@ -41,6 +41,8 @@ class CScriptFunctions
{ {
public: public:
static void Init(); static void Init();
static CBotVar* CreateObjectVar(CObject* obj);
static void DestroyObjectVar(CBotVar* botVar, bool permanent);
private: private:
static CBotTypResult cNull(CBotVar* &var, void* user); static CBotTypResult cNull(CBotVar* &var, void* user);
@ -177,6 +179,8 @@ private:
static CBotTypResult cPointConstructor(CBotVar* pThis, CBotVar* &var); static CBotTypResult cPointConstructor(CBotVar* pThis, CBotVar* &var);
static bool rPointConstructor(CBotVar* pThis, CBotVar* var, CBotVar* pResult, int& Exception, void* user); static bool rPointConstructor(CBotVar* pThis, CBotVar* var, CBotVar* pResult, int& Exception, void* user);
static void uObject(CBotVar* botThis, void* user);
public: public:
static int m_numberOfOpenFiles; static int m_numberOfOpenFiles;