Removed the need for progfunc() in object.factory()

Changes to object.factory():
* The second argument will be automatically interpreted as public function name, file name or program code
* The program starts earlier (when the robot is ready, not when the doors open)
* Refactored to std::string
master
krzys-h 2015-07-22 16:16:16 +02:00
parent 2e606f4dcb
commit d5d8c0b29f
5 changed files with 35 additions and 51 deletions

View File

@ -43,6 +43,9 @@
#include "ui/window.h"
#include <regex>
// Object's constructor.
@ -104,7 +107,7 @@ void CAutoFactory::Init()
m_cargoPos = m_object->GetPosition();
m_program = nullptr;
m_program = "";
CAuto::Init();
}
@ -154,10 +157,9 @@ Error CAutoFactory::StartAction(int param)
// Sets program for created robot
void CAutoFactory::SetProgram(const char* program)
void CAutoFactory::SetProgram(const std::string& program)
{
m_program = new char[strlen(program)+1];
strcpy(m_program, program);
m_program = program;
}
ObjectType ObjectTypeFromFactoryButton(EventType eventType)
@ -393,7 +395,7 @@ bool CAutoFactory::EventProcess(const Event &event)
CObjectManager::GetInstancePointer()->DeleteObject(cargo);
}
m_vehicle = vehicle = SearchVehicle();
vehicle = SearchVehicle();
if ( vehicle != 0 )
{
physics = vehicle->GetPhysics();
@ -406,6 +408,31 @@ bool CAutoFactory::EventProcess(const Event &event)
//? vehicle->GetPhysics()->GetBrain()->StartTaskAdvance(16.0f);
vehicle->SetRotationY(m_object->GetRotationY()+Math::PI);
vehicle->SetScale(1.0f);
if ( !m_program.empty() )
{
if (vehicle->Implements(ObjectInterfaceType::Programmable))
{
CBrain* brain = dynamic_cast<CProgrammableObject*>(vehicle)->GetBrain();
Program* program = brain->AddProgram();
if (std::regex_search(m_program, std::regex("^[A-Za-z0-9_]+$"))) // Public function name?
{
std::string code = "extern void object::Start_"+m_program+"()\n{\n\t\n\t//Automatically generated by object.factory()\n\t"+m_program+"();\n\t\n}\n";
program->script->SendScript(code.c_str());
}
else if (std::regex_search(m_program, std::regex(".txt$"))) // File name (with .txt extension)?
{
program->script->ReadScript(m_program.c_str());
}
else // Program code?
{
program->script->SendScript(m_program.c_str());
}
brain->RunProgram(program);
}
}
}
m_main->CreateShortcuts();
@ -492,18 +519,6 @@ bool CAutoFactory::EventProcess(const Event &event)
m_object->SetPartScaleZ(10+i, 0.30f);
}
if ( m_program != nullptr )
{
if (m_vehicle->Implements(ObjectInterfaceType::Programmable))
{
CBrain* brain = dynamic_cast<CProgrammableObject*>(m_vehicle)->GetBrain();
Program* program = brain->AddProgram();
program->script->SendScript(const_cast<const char*>(m_program));
brain->RunProgram(program);
}
}
SetBusy(false);
UpdateInterface();

View File

@ -51,7 +51,7 @@ public:
bool EventProcess(const Event &event);
Error StartAction(int param);
void SetProgram(const char* program);
void SetProgram(const std::string& program);
bool CreateInterface(bool bSelect);
@ -77,7 +77,5 @@ protected:
Math::Vector m_cargoPos;
int m_channelSound;
CObject* m_vehicle;
char* m_program;
std::string m_program;
};

View File

@ -268,7 +268,6 @@ std::string GetHelpFilename(const char *token)
if ( strcmp(token, "getresearchdone" ) == 0 ) helpfile = "cbot/getresdo";
if ( strcmp(token, "retobject" ) == 0 ) helpfile = "cbot/retobj";
if ( strcmp(token, "errmode" ) == 0 ) helpfile = "cbot/errmode";
if ( strcmp(token, "progfunc" ) == 0 ) helpfile = "cbot/factory";
if ( strcmp(token, "busy" ) == 0 ) helpfile = "cbot/busy";
if ( strcmp(token, "takeoff" ) == 0 ) helpfile = "cbot/takeoff";
if ( strcmp(token, "research" ) == 0 ) helpfile = "cbot/research";
@ -406,7 +405,6 @@ bool IsFunction(const char *token)
if ( strcmp(token, "getresearchdone" ) == 0 ) return true;
if ( strcmp(token, "retobjectbyid") == 0 ) return true;
if ( strcmp(token, "retobject" ) == 0 ) return true;
if ( strcmp(token, "progfunc" ) == 0 ) return true;
if ( strcmp(token, "busy" ) == 0 ) return true;
if ( strcmp(token, "factory" ) == 0 ) return true;
if ( strcmp(token, "research" ) == 0 ) return true;
@ -568,4 +566,3 @@ const char* GetHelpText(const char *token)
if ( strcmp(token, "camerafocus") == 0 ) return "camerafocus ( object );";
return "";
}

View File

@ -577,30 +577,6 @@ bool CScriptFunctions::rGetObject(CBotVar* var, CBotVar* result, int& exception,
return true;
}
// Instruction "progfunc(funcname)".
bool CScriptFunctions::rProgFunc(CBotVar* var, CBotVar* result, int& exception, void* user)
{
CBotString cbs;
const char* funcname;
std::string program;
cbs = var->GetValString();
funcname = cbs;
//TODO: Translation :)
program = "extern void object::Auto()\n{\n\t\n\t//Automatically generated by progfunc(\"";
program += funcname;
program += "\");\n\t";
program += funcname;
program += "();\n\t\n}\n";
result->SetValString(program.c_str());
return true;
}
// Compilation of instruction "object.busy()"
CBotTypResult CScriptFunctions::cBusy(CBotVar* thisclass, CBotVar* &var)
{
@ -3676,7 +3652,6 @@ void CScriptFunctions::Init()
CBotProgram::AddFunction("setresearchenable", rSetResearchEnable, CScriptFunctions::cOneFloat);
CBotProgram::AddFunction("setresearchdone", rSetResearchDone, CScriptFunctions::cOneFloat);
CBotProgram::AddFunction("progfunc", rProgFunc, CScriptFunctions::cStringString);
CBotProgram::AddFunction("retobject", rGetObject, CScriptFunctions::cGetObject);
CBotProgram::AddFunction("retobjectbyid", rGetObjectById, CScriptFunctions::cGetObject);
CBotProgram::AddFunction("delete", rDelete, CScriptFunctions::cDelete);
@ -3850,7 +3825,7 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user)
// Updates the velocity of the object.
pVar = pVar->GetNext(); // "velocity"
if (IsObjectBeingTransported(object))
if (IsObjectBeingTransported(object) || physics == nullptr)
{
pSub = pVar->GetItemList(); // "x"
pSub->SetInit(CBotVar::InitType::IS_NAN);

View File

@ -103,7 +103,6 @@ private:
static bool rSetResearchDone(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rGetObjectById(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rGetObject(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rProgFunc(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rDelete(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rSearch(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rRadar(CBotVar* var, CBotVar* result, int& exception, void* user);