Running program in robots created using object.factory()

dev-ui
krzys-h 2013-05-19 16:25:53 +02:00
parent 7662f312b3
commit b9d0ee034e
9 changed files with 103 additions and 18 deletions

View File

@ -540,6 +540,7 @@ void InitializeRestext()
stringsErr[ERR_GENERIC] = "Internal error - tell the developers";
stringsErr[ERR_CMD] = "Unknown command";
stringsErr[ERR_MANIP_VEH] = "Inappropriate bot";
stringsErr[ERR_MANIP_FLY] = "Impossible when flying";

View File

@ -24,6 +24,7 @@
#include "math/geometry.h"
#include "object/robotmain.h"
#include "object/brain.h"
#include "physics/physics.h"
@ -103,6 +104,8 @@ void CAutoFactory::Init()
m_fretPos = m_object->GetPosition(0);
m_program = nullptr;
CAuto::Init();
}
@ -149,6 +152,15 @@ Error CAutoFactory::StartAction(int param)
}
// Sets program for created robot
void CAutoFactory::SetProgram(const char* program)
{
m_program = new char[strlen(program)+1];
strcpy(m_program, program);
}
// Management of an event.
bool CAutoFactory::EventProcess(const Event &event)
@ -377,7 +389,7 @@ bool CAutoFactory::EventProcess(const Event &event)
delete fret;
}
vehicle = SearchVehicle();
m_vehicle = vehicle = SearchVehicle();
if ( vehicle != 0 )
{
physics = vehicle->GetPhysics();
@ -476,6 +488,17 @@ bool CAutoFactory::EventProcess(const Event &event)
m_object->SetZoomZ(10+i, 0.30f);
}
if ( m_program != nullptr )
{
CBrain* brain = m_vehicle->GetBrain();
if ( brain != nullptr )
{
brain->SendProgram(0, const_cast<const char*>(m_program));
brain->SetScriptRun(0);
brain->RunProgram(0);
}
}
SetBusy(false);
UpdateInterface();

View File

@ -49,6 +49,7 @@ public:
bool EventProcess(const Event &event);
Error StartAction(int param);
void SetProgram(const char* program);
bool CreateInterface(bool bSelect);
@ -71,7 +72,10 @@ protected:
float m_progress;
float m_speed;
float m_lastParticle;
Math::Vector m_fretPos;
Math::Vector m_fretPos;
int m_channelSound;
CObject* m_vehicle;
char* m_program;
};

View File

@ -2720,6 +2720,23 @@ bool CBrain::ReadSoluce(char* filename)
return true;
}
// Load a script from text buffer.
bool CBrain::SendProgram(int rank, const char* buffer)
{
if ( m_script[rank] == 0 )
{
m_script[rank] = new CScript(m_object, &m_secondaryTask);
}
if ( m_script[rank]->SendScript(buffer) ) return true;
delete m_script[rank];
m_script[rank] = 0;
return false;
}
// Load a script with a text file.
bool CBrain::ReadProgram(int rank, const char* filename)

View File

@ -114,6 +114,7 @@ public:
char* GetScriptName(int rank);
void SetSoluceName(char *name);
char* GetSoluceName();
bool SendProgram(int rank, const char* buffer);
bool ReadSoluce(char* filename);
bool ReadProgram(int rank, const char* filename);

View File

@ -874,7 +874,7 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile)
bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), PR_READ);
bc->AddItem("id", CBotTypResult(CBotTypInt), PR_READ);
bc->AddFunction("busy", CScript::rBusy, CScript::cBusy);
bc->AddFunction("factory", CScript::rFactory, CScript::cClassOneFloat);
bc->AddFunction("factory", CScript::rFactory, CScript::cFactory);
bc->AddFunction("destroy", CScript::rDestroy, CScript::cClassNull);
// Initializes the class FILE.

View File

@ -470,7 +470,7 @@ const char* GetHelpText(const char *token)
if ( strcmp(token, "retobject" ) == 0 ) return "retobject ( rank );";
if ( strcmp(token, "retobjectbyid") == 0 ) return "retobjectbyid ( rank );";
if ( strcmp(token, "busy" ) == 0 ) return "object.busy ( );";
if ( strcmp(token, "factory" ) == 0 ) return "object.factory ( cat );";
if ( strcmp(token, "factory" ) == 0 ) return "object.factory ( cat, program );";
if ( strcmp(token, "destroy" ) == 0 ) return "object.destroy ( );";
if ( strcmp(token, "search" ) == 0 ) return "search ( );";
if ( strcmp(token, "radar" ) == 0 ) return "radar ( cat, angle, focus, min, max, sens );";

View File

@ -86,11 +86,6 @@ CBotTypResult CScript::cOneFloat(CBotVar* &var, void* user)
return CBotTypResult(CBotTypFloat);
}
CBotTypResult CScript::cClassOneFloat(CBotVar* thisclass, CBotVar* &var)
{
return CScript::cOneFloat(var, nullptr);
}
// Compiling a procedure with two real numbers.
CBotTypResult CScript::cTwoFloat(CBotVar* &var, void* user)
@ -588,6 +583,23 @@ bool CScript::rDestroy(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& e
return true;
}
// Compilation of instruction "object.factory(cat)"
CBotTypResult CScript::cFactory(CBotVar* thisclass, CBotVar* &var)
{
if ( var == 0 ) return CBotTypResult(CBotErrLowParam);
if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
var = var->GetNext();
if ( var != 0 )
{
if ( var->GetType() != CBotTypString ) return CBotTypResult(CBotErrBadNum);
var = var->GetNext();
if ( var != 0 ) return CBotTypResult(CBotErrOverParam);
}
return CBotTypResult(CBotTypFloat);
}
// Instruction "object.factory(cat)"
bool CScript::rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception)
@ -596,6 +608,18 @@ bool CScript::rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& e
exception = 0;
ObjectType type = static_cast<ObjectType>(var->GetValInt());
var = var->GetNext();
CBotString cbs;
const char* program;
if ( var != 0 )
{
cbs = var->GetValString();
program = cbs;
}
else
program = "";
CBotVar* classVars = thisclass->GetItemList(); // "category"
ObjectType thisType = static_cast<ObjectType>(classVars->GetValInt());
classVars = classVars->GetNext(); // "position"
@ -613,11 +637,10 @@ bool CScript::rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& e
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* factory = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CAuto* automat = factory->GetAuto();
CAutoFactory* automat = static_cast<CAutoFactory*>(factory->GetAuto());
if ( thisType == OBJECT_FACTORY )
{
ObjectType type = static_cast<ObjectType>(var->GetValInt());
bool bEnable = false;
if ( type == OBJECT_MOBILEwa )
@ -724,10 +747,19 @@ bool CScript::rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& e
}
if ( bEnable )
err = automat->StartAction(type);
{
if ( automat != nullptr )
{
err = automat->StartAction(type);
if ( err == ERR_OK ) automat->SetProgram(program);
}
else
err = ERR_GENERIC;
}
else
err = ERR_BUILD_DISABLED;
} else
}
else
err = ERR_WRONG_OBJ;
if ( err != ERR_OK )
@ -4307,13 +4339,20 @@ void CScript::New(Ui::CEdit* edit, const char* name)
// Provided a script for all parts.
bool CScript::SendScript(char* text)
bool CScript::SendScript(const char* text)
{
m_len = strlen(text);
/*m_len = strlen(text);
m_script = new char[m_len+1];
strcpy(m_script, text);
if ( !CheckToken() ) return false;
if ( !Compile() ) return false;
if ( !Compile() ) return false;*/
Ui::CEdit* edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9);
edit->SetMaxChar(Ui::EDITSTUDIOMAX);
edit->SetAutoIndent(m_engine->GetEditIndentMode());
edit->SetText(text, true);
GetScript(edit);
m_interface->DeleteControl(EVENT_EDIT9);
return true;
}

View File

@ -78,7 +78,7 @@ public:
void GetError(char* buffer);
void New(Ui::CEdit* edit, const char* name);
bool SendScript(char* text);
bool SendScript(const char* text);
bool ReadScript(const char* filename);
bool WriteScript(const char* filename);
bool ReadStack(FILE *file);
@ -192,7 +192,7 @@ private:
public:
static CBotTypResult cBusy(CBotVar* thisclass, CBotVar* &var);
static CBotTypResult cClassOneFloat(CBotVar* thisclass, CBotVar* &var);
static CBotTypResult cFactory(CBotVar* thisclass, CBotVar* &var);
static CBotTypResult cClassNull(CBotVar* thisclass, CBotVar* &var);
static bool rBusy(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);