New scripts interface, removed script limit

This is a big change in game structure and requires more testing!
master
krzys-h 2015-03-29 14:21:02 +02:00
parent 72196b252c
commit a3ae7802a2
22 changed files with 514 additions and 277 deletions

View File

@ -851,6 +851,18 @@ msgstr ""
msgid "Edit the selected program" msgid "Edit the selected program"
msgstr "" msgstr ""
msgid "Add new program"
msgstr ""
msgid "Remove selected program"
msgstr ""
msgid "Move selected program up"
msgstr ""
msgid "Move selected program down"
msgstr ""
msgid "\\SatCom on standby" msgid "\\SatCom on standby"
msgstr "" msgstr ""

View File

@ -115,6 +115,9 @@ msgid "Access to solutions\\Show program \"4: Solution\" in the exercises"
msgstr "" msgstr ""
"Lösung zugänglich\\Die Lösung ist im Programmslot \"4: Lösung\" zugänglich" "Lösung zugänglich\\Die Lösung ist im Programmslot \"4: Lösung\" zugänglich"
msgid "Add new program"
msgstr ""
msgid "Alien Queen" msgid "Alien Queen"
msgstr "Insektenkönigin" msgstr "Insektenkönigin"
@ -851,6 +854,14 @@ msgstr "Umkehr Y\\Umkehr der Kameradrehung Y-Achse"
msgid "Mouse shadow\\Gives the mouse a shadow" msgid "Mouse shadow\\Gives the mouse a shadow"
msgstr "Schatten unter der Maus\\Ein Schatten erscheint unter der Maus" msgstr "Schatten unter der Maus\\Ein Schatten erscheint unter der Maus"
#, fuzzy
msgid "Move selected program down"
msgstr "Gewähltes Programm bearbeiten"
#, fuzzy
msgid "Move selected program up"
msgstr "Gewähltes Programm bearbeiten"
msgid "Mute\\No sound" msgid "Mute\\No sound"
msgstr "Kein Ton\\Keine Geräusche und Geräuschkulisse" msgstr "Kein Ton\\Keine Geräusche und Geräuschkulisse"
@ -1190,6 +1201,10 @@ msgstr "Überreste einer Apollo-Mission"
msgid "Remove a flag" msgid "Remove a flag"
msgstr "Sammelt die Fahne ein" msgstr "Sammelt die Fahne ein"
#, fuzzy
msgid "Remove selected program"
msgstr "Gewähltes Programm bearbeiten"
#, fuzzy #, fuzzy
msgid "Render distance\\Maximum visibility" msgid "Render distance\\Maximum visibility"
msgstr "Sichtweite\\Maximale Sichtweite" msgstr "Sichtweite\\Maximale Sichtweite"

View File

@ -110,6 +110,9 @@ msgstr "Accès à la solution\\Donne la solution"
msgid "Access to solutions\\Show program \"4: Solution\" in the exercises" msgid "Access to solutions\\Show program \"4: Solution\" in the exercises"
msgstr "Accès aux solutions\\Programme \"4: Solution\" dans les exercices" msgstr "Accès aux solutions\\Programme \"4: Solution\" dans les exercices"
msgid "Add new program"
msgstr ""
msgid "Alien Queen" msgid "Alien Queen"
msgstr "Pondeuse" msgstr "Pondeuse"
@ -847,6 +850,14 @@ msgstr ""
msgid "Mouse shadow\\Gives the mouse a shadow" msgid "Mouse shadow\\Gives the mouse a shadow"
msgstr "Souris ombrée\\Jolie souris avec une ombre" msgstr "Souris ombrée\\Jolie souris avec une ombre"
#, fuzzy
msgid "Move selected program down"
msgstr "Édite le programme sélectionné"
#, fuzzy
msgid "Move selected program up"
msgstr "Édite le programme sélectionné"
msgid "Mute\\No sound" msgid "Mute\\No sound"
msgstr "Silencieux\\Totalement silencieux" msgstr "Silencieux\\Totalement silencieux"
@ -1186,6 +1197,10 @@ msgstr "Vestige d'une mission Apollo"
msgid "Remove a flag" msgid "Remove a flag"
msgstr "Enlève un drapeau" msgstr "Enlève un drapeau"
#, fuzzy
msgid "Remove selected program"
msgstr "Édite le programme sélectionné"
#, fuzzy #, fuzzy
msgid "Render distance\\Maximum visibility" msgid "Render distance\\Maximum visibility"
msgstr "Profondeur de champ\\Distance de vue maximale" msgstr "Profondeur de champ\\Distance de vue maximale"

View File

@ -115,6 +115,9 @@ msgstr ""
msgid "Access to solutions\\Show program \"4: Solution\" in the exercises" msgid "Access to solutions\\Show program \"4: Solution\" in the exercises"
msgstr "Accčs aux solutions\\Programme \"4: Solution\" dans les exercices" msgstr "Accčs aux solutions\\Programme \"4: Solution\" dans les exercices"
msgid "Add new program"
msgstr "Dodaj nowy program"
msgid "Alien Queen" msgid "Alien Queen"
msgstr "Królowa Obcych" msgstr "Królowa Obcych"
@ -854,6 +857,12 @@ msgstr "Odwrócenie myszy Y\\Odwrócenie kierunków przewijania w pionie"
msgid "Mouse shadow\\Gives the mouse a shadow" msgid "Mouse shadow\\Gives the mouse a shadow"
msgstr "Cień kursora myszy\\Dodaje cień kursorowi myszy" msgstr "Cień kursora myszy\\Dodaje cień kursorowi myszy"
msgid "Move selected program down"
msgstr "Przenieś zaznaczony program w dół"
msgid "Move selected program up"
msgstr "Przenieś zaznaczony program w górę"
msgid "Mute\\No sound" msgid "Mute\\No sound"
msgstr "Cisza\\Brak dźwięków" msgstr "Cisza\\Brak dźwięków"
@ -1195,6 +1204,9 @@ msgstr "Pozostałości z misji Apollo"
msgid "Remove a flag" msgid "Remove a flag"
msgstr "Usuń flagę" msgstr "Usuń flagę"
msgid "Remove selected program"
msgstr "Usuń zaznaczony program"
msgid "Render distance\\Maximum visibility" msgid "Render distance\\Maximum visibility"
msgstr "Zasięg renderingu\\Maksymalna widoczność" msgstr "Zasięg renderingu\\Maksymalna widoczność"

View File

@ -110,6 +110,9 @@ msgstr "Доступ к решению\\Показывает решение (п
msgid "Access to solutions\\Show program \"4: Solution\" in the exercises" msgid "Access to solutions\\Show program \"4: Solution\" in the exercises"
msgstr "Доступ к решению\\Показывает решение \"4: Решение\" в упражнениях" msgstr "Доступ к решению\\Показывает решение \"4: Решение\" в упражнениях"
msgid "Add new program"
msgstr ""
msgid "Alien Queen" msgid "Alien Queen"
msgstr "Королева чужих" msgstr "Королева чужих"
@ -844,6 +847,14 @@ msgstr "Инверсия мыши по оси Y\\Инверсия прокрут
msgid "Mouse shadow\\Gives the mouse a shadow" msgid "Mouse shadow\\Gives the mouse a shadow"
msgstr "Тень мыши\\Мышь отбрасывает тень" msgstr "Тень мыши\\Мышь отбрасывает тень"
#, fuzzy
msgid "Move selected program down"
msgstr "Изменить выбранную программу"
#, fuzzy
msgid "Move selected program up"
msgstr "Изменить выбранную программу"
msgid "Mute\\No sound" msgid "Mute\\No sound"
msgstr "Без звука\\Без звука" msgstr "Без звука\\Без звука"
@ -1186,6 +1197,10 @@ msgstr "Остатки миссии Аполлон"
msgid "Remove a flag" msgid "Remove a flag"
msgstr "Удалить флаг" msgstr "Удалить флаг"
#, fuzzy
msgid "Remove selected program"
msgstr "Изменить выбранную программу"
#, fuzzy #, fuzzy
msgid "Render distance\\Maximum visibility" msgid "Render distance\\Maximum visibility"
msgstr "Дальность прорисовки\\Максимальная видимость" msgstr "Дальность прорисовки\\Максимальная видимость"

View File

@ -427,6 +427,10 @@ void InitializeEventTypeTexts()
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGEDIT] = "EVENT_OBJECT_PROGEDIT"; EVENT_TYPE_TEXT[EVENT_OBJECT_PROGEDIT] = "EVENT_OBJECT_PROGEDIT";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGSTART] = "EVENT_OBJECT_PROGSTART"; EVENT_TYPE_TEXT[EVENT_OBJECT_PROGSTART] = "EVENT_OBJECT_PROGSTART";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGSTOP] = "EVENT_OBJECT_PROGSTOP"; EVENT_TYPE_TEXT[EVENT_OBJECT_PROGSTOP] = "EVENT_OBJECT_PROGSTOP";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGADD] = "EVENT_OBJECT_PROGADD";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGREMOVE] = "EVENT_OBJECT_PROGREMOVE";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGMOVEUP] = "EVENT_OBJECT_PROGMOVEUP";
EVENT_TYPE_TEXT[EVENT_OBJECT_PROGMOVEDOWN] = "EVENT_OBJECT_PROGMOVEDOWN";
EVENT_TYPE_TEXT[EVENT_OBJECT_INFOOK] = "EVENT_OBJECT_INFOOK"; EVENT_TYPE_TEXT[EVENT_OBJECT_INFOOK] = "EVENT_OBJECT_INFOOK";
EVENT_TYPE_TEXT[EVENT_OBJECT_DELETE] = "EVENT_OBJECT_DELETE"; EVENT_TYPE_TEXT[EVENT_OBJECT_DELETE] = "EVENT_OBJECT_DELETE";
EVENT_TYPE_TEXT[EVENT_OBJECT_GENERGY] = "EVENT_OBJECT_GENERGY"; EVENT_TYPE_TEXT[EVENT_OBJECT_GENERGY] = "EVENT_OBJECT_GENERGY";

View File

@ -402,7 +402,6 @@ enum EventType
EVENT_OBJECT_BPARA = 1061, EVENT_OBJECT_BPARA = 1061,
EVENT_OBJECT_BINFO = 1062, EVENT_OBJECT_BINFO = 1062,
EVENT_OBJECT_BDESTROYER = 1063, EVENT_OBJECT_BDESTROYER = 1063,
//EVENT_OBJECT_BXXXX = 1063,
EVENT_OBJECT_GFLAT = 1070, EVENT_OBJECT_GFLAT = 1070,
EVENT_OBJECT_FCREATE = 1071, EVENT_OBJECT_FCREATE = 1071,
EVENT_OBJECT_FDELETE = 1072, EVENT_OBJECT_FDELETE = 1072,
@ -459,6 +458,10 @@ enum EventType
EVENT_OBJECT_PROGEDIT = 1312, EVENT_OBJECT_PROGEDIT = 1312,
EVENT_OBJECT_PROGSTART = 1313, EVENT_OBJECT_PROGSTART = 1313,
EVENT_OBJECT_PROGSTOP = 1314, EVENT_OBJECT_PROGSTOP = 1314,
EVENT_OBJECT_PROGADD = 1315,
EVENT_OBJECT_PROGREMOVE = 1316,
EVENT_OBJECT_PROGMOVEUP = 1317,
EVENT_OBJECT_PROGMOVEDOWN = 1318,
EVENT_OBJECT_INFOOK = 1340, EVENT_OBJECT_INFOOK = 1340,
EVENT_OBJECT_DELETE = 1350, EVENT_OBJECT_DELETE = 1350,
EVENT_OBJECT_GENERGY = 1360, EVENT_OBJECT_GENERGY = 1360,

View File

@ -293,6 +293,15 @@ bool CResourceManager::Move(const std::string& from, const std::string& to)
return false; return false;
} }
bool CResourceManager::Remove(const std::string& filename)
{
if(PHYSFS_isInit())
{
return PHYSFS_delete(filename.c_str()) != 0;
}
return false;
}
int CResourceManager::SDLClose(SDL_RWops *context) int CResourceManager::SDLClose(SDL_RWops *context)
{ {
if (CheckSDLContext(context)) if (CheckSDLContext(context))

View File

@ -65,6 +65,8 @@ public:
//! Move file/directory //! Move file/directory
static bool Move(const std::string &from, const std::string &to); static bool Move(const std::string &from, const std::string &to);
//! Remove file
static bool Remove(const std::string& filename);
private: private:
static int SDLSeek(SDL_RWops *context, int offset, int whence); static int SDLSeek(SDL_RWops *context, int offset, int whence);

View File

@ -372,6 +372,10 @@ void InitializeRestext()
stringsEvent[EVENT_OBJECT_DELSEARCH] = TR("Delete mark"); stringsEvent[EVENT_OBJECT_DELSEARCH] = TR("Delete mark");
stringsEvent[EVENT_OBJECT_PROGRUN] = TR("Execute the selected program"); stringsEvent[EVENT_OBJECT_PROGRUN] = TR("Execute the selected program");
stringsEvent[EVENT_OBJECT_PROGEDIT] = TR("Edit the selected program"); stringsEvent[EVENT_OBJECT_PROGEDIT] = TR("Edit the selected program");
stringsEvent[EVENT_OBJECT_PROGADD] = TR("Add new program");
stringsEvent[EVENT_OBJECT_PROGREMOVE] = TR("Remove selected program");
stringsEvent[EVENT_OBJECT_PROGMOVEUP] = TR("Move selected program up");
stringsEvent[EVENT_OBJECT_PROGMOVEDOWN] = TR("Move selected program down");
stringsEvent[EVENT_OBJECT_INFOOK] = TR("\\SatCom on standby"); stringsEvent[EVENT_OBJECT_INFOOK] = TR("\\SatCom on standby");
stringsEvent[EVENT_OBJECT_DELETE] = TR("Destroy the building"); stringsEvent[EVENT_OBJECT_DELETE] = TR("Destroy the building");
stringsEvent[EVENT_OBJECT_GENERGY] = TR("Energy level"); stringsEvent[EVENT_OBJECT_GENERGY] = TR("Energy level");

View File

@ -22,6 +22,7 @@
#include "math/geometry.h" #include "math/geometry.h"
#include "object/brain.h"
#include "object/objman.h" #include "object/objman.h"
#include "object/level/parserline.h" #include "object/level/parserline.h"
#include "object/level/parserparam.h" #include "object/level/parserparam.h"
@ -189,8 +190,13 @@ bool CAutoEgg::EventProcess(const Event &event)
return true; return true;
} }
alien->SetActivity(false); alien->SetActivity(false);
alien->ReadProgram(0, m_string); CBrain* brain = alien->GetBrain();
alien->RunProgram(0); if(brain != nullptr)
{
Program* program = brain->AddProgram();
brain->ReadProgram(program, m_string);
brain->RunProgram(program);
}
Init(); Init();
} }

View File

@ -33,6 +33,7 @@
#include "physics/physics.h" #include "physics/physics.h"
#include "script/cmdtoken.h" #include "script/cmdtoken.h"
#include "script/script.h"
#include "ui/interface.h" #include "ui/interface.h"
#include "ui/window.h" #include "ui/window.h"
@ -496,9 +497,9 @@ bool CAutoFactory::EventProcess(const Event &event)
CBrain* brain = m_vehicle->GetBrain(); CBrain* brain = m_vehicle->GetBrain();
if ( brain != nullptr ) if ( brain != nullptr )
{ {
brain->SendProgram(0, const_cast<const char*>(m_program)); Program* program = brain->AddProgram();
brain->SetScriptRun(0); program->script->SendScript(const_cast<const char*>(m_program));
brain->RunProgram(0); brain->RunProgram(program);
} }
} }
@ -643,8 +644,6 @@ bool CAutoFactory::CreateVehicle()
CPhysics* physics; CPhysics* physics;
Math::Vector pos; Math::Vector pos;
float angle; float angle;
char* name;
int i;
angle = m_object->GetAngleY(0); angle = m_object->GetAngleY(0);
@ -679,12 +678,16 @@ bool CAutoFactory::CreateVehicle()
physics->SetFreeze(true); // it doesn't move physics->SetFreeze(true); // it doesn't move
} }
/* ???
char* name;
int i;
for ( i=0 ; i<10 ; i++ ) for ( i=0 ; i<10 ; i++ )
{ {
name = m_main->GetNewScriptName(m_type, i); name = m_main->GetNewScriptName(m_type, i);
if ( name == 0 ) break; if ( name == 0 ) break;
vehicle->ReadProgram(i, name); vehicle->ReadProgram(i, name);
} }
*/
return true; return true;
} }

View File

@ -72,7 +72,8 @@ CBrain::CBrain(CObject* object)
m_secondaryTask = nullptr; m_secondaryTask = nullptr;
m_studio = nullptr; m_studio = nullptr;
m_program = -1; m_program.clear();
m_currentProgram = nullptr;
m_bActivity = true; m_bActivity = true;
m_bBurn = false; m_bBurn = false;
m_bActiveVirus = false; m_bActiveVirus = false;
@ -91,12 +92,7 @@ CBrain::CBrain(CObject* object)
m_defaultEnter = EVENT_NULL; m_defaultEnter = EVENT_NULL;
m_manipStyle = EVENT_OBJECT_MFRONT; m_manipStyle = EVENT_OBJECT_MFRONT;
for (int i=0 ; i<BRAINMAXSCRIPT ; i++ ) m_scriptRun = nullptr;
{
m_script[i] = 0;
m_scriptName[i][0] = 0;
}
m_scriptRun = -1;
m_soluceName[0] = 0; m_soluceName[0] = 0;
m_selScript = 0; m_selScript = 0;
@ -108,11 +104,13 @@ CBrain::CBrain(CObject* object)
CBrain::~CBrain() CBrain::~CBrain()
{ {
for (int i=0 ; i<BRAINMAXSCRIPT ; i++ ) for(auto x : m_program)
{ {
delete m_script[i]; delete x->script;
m_script[i] = nullptr; x->script = nullptr;
delete x;
} }
m_program.clear();
delete m_primaryTask; delete m_primaryTask;
m_primaryTask = nullptr; m_primaryTask = nullptr;
@ -314,9 +312,12 @@ bool CBrain::EventProcess(const Event &event)
if ( action == EVENT_OBJECT_PROGRUN ) if ( action == EVENT_OBJECT_PROGRUN )
{ {
if ( m_program == -1 ) if ( m_currentProgram == nullptr )
{ {
RunProgram(m_selScript); if(m_selScript < m_program.size())
{
RunProgram(m_program[m_selScript]);
}
} }
else else
{ {
@ -326,7 +327,10 @@ bool CBrain::EventProcess(const Event &event)
if ( action == EVENT_OBJECT_PROGSTART ) if ( action == EVENT_OBJECT_PROGSTART )
{ {
m_main->SaveOneScript(m_object); m_main->SaveOneScript(m_object);
RunProgram(m_selScript); if(m_selScript < m_program.size())
{
RunProgram(m_program[m_selScript]);
}
} }
if ( action == EVENT_OBJECT_PROGSTOP ) if ( action == EVENT_OBJECT_PROGSTOP )
{ {
@ -342,11 +346,12 @@ bool CBrain::EventProcess(const Event &event)
StopEditScript(true); StopEditScript(true);
m_main->SaveOneScript(m_object); m_main->SaveOneScript(m_object);
} }
return true; return true;
} }
if ( !m_object->GetSelect() && // robot pas sélectionné ? if ( !m_object->GetSelect() && // robot pas sélectionné ?
m_program == -1 && m_currentProgram == nullptr &&
m_primaryTask == 0 ) m_primaryTask == 0 )
{ {
axeX = 0.0f; axeX = 0.0f;
@ -384,7 +389,7 @@ bool CBrain::EventProcess(const Event &event)
return true; return true;
} }
if ( m_program != -1 && if ( m_currentProgram != nullptr &&
m_object->GetRuin() ) m_object->GetRuin() )
{ {
StopProgram(); StopProgram();
@ -404,7 +409,7 @@ bool CBrain::EventProcess(const Event &event)
} }
} }
if ( m_primaryTask != 0 || // current task? if ( m_primaryTask != 0 || // current task?
m_program != -1 ) m_currentProgram != nullptr )
{ {
if ( action == EVENT_OBJECT_PROGRUN ) if ( action == EVENT_OBJECT_PROGRUN )
{ {
@ -412,11 +417,82 @@ bool CBrain::EventProcess(const Event &event)
} }
if ( action == EVENT_OBJECT_PROGEDIT ) if ( action == EVENT_OBJECT_PROGEDIT )
{ {
StartEditScript(m_selScript, m_main->GetScriptName()); if(m_selScript < m_program.size())
{
StartEditScript(m_program[m_selScript], m_main->GetScriptName());
}
} }
if ( m_primaryTask == 0 || !m_primaryTask->IsPilot() ) return true; if ( m_primaryTask == 0 || !m_primaryTask->IsPilot() ) return true;
} }
if ( m_currentProgram == nullptr )
{
if( action == EVENT_OBJECT_PROGADD )
{
AddProgram();
m_selScript = m_program.size()-1;
m_main->SaveOneScript(m_object);
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
SetSelScript(m_selScript);
}
if( action == EVENT_OBJECT_PROGREMOVE )
{
if(m_selScript < m_program.size())
{
RemoveProgram(m_program[m_selScript]);
if(m_selScript >= m_program.size())
m_selScript = m_program.size()-1;
m_main->SaveOneScript(m_object);
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
SetSelScript(m_selScript);
}
}
if( action == EVENT_OBJECT_PROGMOVEUP )
{
std::iter_swap(m_program.begin() + m_selScript, m_program.begin() + m_selScript - 1);
m_selScript--;
m_main->SaveOneScript(m_object);
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
SetSelScript(m_selScript);
}
if( action == EVENT_OBJECT_PROGMOVEDOWN )
{
std::iter_swap(m_program.begin() + m_selScript, m_program.begin() + m_selScript + 1);
m_selScript++;
m_main->SaveOneScript(m_object);
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
SetSelScript(m_selScript);
}
}
if ( action == EVENT_OBJECT_LEFT || if ( action == EVENT_OBJECT_LEFT ||
action == EVENT_OBJECT_RIGHT || action == EVENT_OBJECT_RIGHT ||
action == EVENT_OBJECT_UP || action == EVENT_OBJECT_UP ||
@ -478,19 +554,25 @@ bool CBrain::EventProcess(const Event &event)
if ( action == EVENT_OBJECT_PROGEDIT ) if ( action == EVENT_OBJECT_PROGEDIT )
{ {
StartEditScript(m_selScript, m_main->GetScriptName()); if(m_selScript < m_program.size())
{
StartEditScript(m_program[m_selScript], m_main->GetScriptName());
}
} }
if ( action == EVENT_OBJECT_PROGRUN ) if ( action == EVENT_OBJECT_PROGRUN )
{ {
StopProgram(); // stops the current program StopProgram(); // stops the current program
RunProgram(m_selScript); if(m_selScript < m_program.size())
{
RunProgram(m_program[m_selScript]);
}
UpdateInterface(); UpdateInterface();
} }
err = ERR_OK; err = ERR_OK;
if ( m_program == -1 ) if ( m_currentProgram == nullptr )
{ {
if ( action == EVENT_OBJECT_HTAKE ) if ( action == EVENT_OBJECT_HTAKE )
{ {
@ -826,9 +908,9 @@ bool CBrain::EventFrame(const Event &event)
if ( !m_bActivity ) return true; // expected if idle if ( !m_bActivity ) return true; // expected if idle
if ( EndedTask() == ERR_CONTINUE ) return true; // expected if not finished ... if ( EndedTask() == ERR_CONTINUE ) return true; // expected if not finished ...
if ( m_program != -1 ) // current program? if ( m_currentProgram != nullptr ) // current program?
{ {
if ( m_script[m_program]->Continue(event) ) if ( m_currentProgram->script->Continue(event) )
{ {
StopProgram(); StopProgram();
} }
@ -852,15 +934,14 @@ void CBrain::StopProgram()
if ( m_object->GetType() == OBJECT_HUMAN || if ( m_object->GetType() == OBJECT_HUMAN ||
m_object->GetType() == OBJECT_TECH ) return; m_object->GetType() == OBJECT_TECH ) return;
if ( m_program != -1 && if ( m_currentProgram != nullptr )
m_script[m_program] != 0 )
{ {
m_script[m_program]->Stop(); m_currentProgram->script->Stop();
} }
BlinkScript(false); // stops flashing BlinkScript(false); // stops flashing
m_program = -1; m_currentProgram = nullptr;
m_physics->SetMotorSpeedX(0.0f); m_physics->SetMotorSpeedX(0.0f);
m_physics->SetMotorSpeedY(0.0f); m_physics->SetMotorSpeedY(0.0f);
@ -907,14 +988,11 @@ bool CBrain::IntroduceVirus()
for ( i=0 ; i<50 ; i++ ) for ( i=0 ; i<50 ; i++ )
{ {
j = rand()%BRAINMAXSCRIPT; j = rand()%m_program.size();
if ( m_script[j] != 0 ) if ( m_program[j]->script->IntroduceVirus() ) // tries to introduce
{ {
if ( m_script[j]->IntroduceVirus() ) // tries to introduce m_bActiveVirus = true; // active virus
{ return true;
m_bActiveVirus = true; // active virus
return true;
}
} }
} }
return false; return false;
@ -944,17 +1022,12 @@ bool CBrain::GetActiveVirus()
// Start editing a program. // Start editing a program.
void CBrain::StartEditScript(int rank, char* name) void CBrain::StartEditScript(Program* program, char* name)
{ {
CreateInterface(false); // removes the control buttons CreateInterface(false); // removes the control buttons
if ( m_script[rank] == 0 )
{
m_script[rank] = new CScript(m_object, &m_secondaryTask);
}
m_studio = new Ui::CStudio(); m_studio = new Ui::CStudio();
m_studio->StartEditScript(m_script[rank], name, rank); m_studio->StartEditScript(program->script, name, program);
} }
// End of editing a program. // End of editing a program.
@ -1317,13 +1390,28 @@ bool CBrain::CreateInterface(bool bSelect)
if (!(m_main->GetRetroMode())) if (!(m_main->GetRetroMode()))
{ {
ddim.x = dim.x*5.1f; ddim.x = dim.x*5.1f;
ddim.y = dim.y*2.0f; // default => 2 ddim.y = dim.y*1.5f;
pos.x = ox+sx*0.0f; pos.x = ox+sx*0.0f;
pos.y = oy+sy*0.0f; pos.y = oy+sy*0.5f;
pw->CreateList(pos, ddim, -1, EVENT_OBJECT_PROGLIST, -1.10f); pw->CreateList(pos, ddim, -1, EVENT_OBJECT_PROGLIST, -1.10f);
UpdateScript(pw); UpdateScript(pw);
ddim.y = dim.y*0.5f;
pos.y = oy+sy*0.0f;
ddim.x = dim.x*2.0f;
pos.x = ox+sx*0.0f;
pw->CreateButton(pos, ddim, 24, EVENT_OBJECT_PROGADD);
ddim.x = dim.x*1.0f;
pos.x = ox+sx*2.0f;
pw->CreateButton(pos, ddim, 25, EVENT_OBJECT_PROGREMOVE);
ddim.x = dim.x*1.05f;
pos.x = ox+sx*3.0f;
pw->CreateButton(pos, ddim, 49, EVENT_OBJECT_PROGMOVEUP);
ddim.x = dim.x*1.05f;
pos.x = ox+sx*4.05f;
pw->CreateButton(pos, ddim, 50, EVENT_OBJECT_PROGMOVEDOWN);
pos.x = ox+sx*5.2f; pos.x = ox+sx*5.2f;
pos.y = oy+sy*1.0f; pos.y = oy+sy*1.0f;
pw->CreateButton(pos, dim, 8, EVENT_OBJECT_PROGRUN); pw->CreateButton(pos, dim, 8, EVENT_OBJECT_PROGRUN);
@ -2175,12 +2263,16 @@ void CBrain::UpdateInterface()
type = m_object->GetType(); type = m_object->GetType();
bEnable = ( m_secondaryTask == 0 && m_program == -1 ); bEnable = ( m_secondaryTask == 0 && m_currentProgram == nullptr );
bEnable = ( m_primaryTask == 0 && m_program == -1 ); bEnable = ( m_primaryTask == 0 && m_currentProgram == nullptr );
EnableInterface(pw, EVENT_OBJECT_PROGEDIT, (m_primaryTask == 0 && !m_bTraceRecord)); EnableInterface(pw, EVENT_OBJECT_PROGEDIT, (m_primaryTask == 0 && !m_bTraceRecord) && m_selScript < m_program.size());
EnableInterface(pw, EVENT_OBJECT_PROGLIST, bEnable && !m_bTraceRecord); EnableInterface(pw, EVENT_OBJECT_PROGLIST, bEnable && !m_bTraceRecord);
EnableInterface(pw, EVENT_OBJECT_PROGADD, m_currentProgram == nullptr);
EnableInterface(pw, EVENT_OBJECT_PROGREMOVE, m_currentProgram == nullptr);
EnableInterface(pw, EVENT_OBJECT_PROGMOVEUP, m_currentProgram == nullptr && m_program.size() >= 2 && m_selScript > 0);
EnableInterface(pw, EVENT_OBJECT_PROGMOVEDOWN,m_currentProgram == nullptr && m_program.size() >= 2 && m_selScript < m_program.size()-1);
EnableInterface(pw, EVENT_OBJECT_LEFT, bEnable); EnableInterface(pw, EVENT_OBJECT_LEFT, bEnable);
EnableInterface(pw, EVENT_OBJECT_RIGHT, bEnable); EnableInterface(pw, EVENT_OBJECT_RIGHT, bEnable);
EnableInterface(pw, EVENT_OBJECT_UP, bEnable); EnableInterface(pw, EVENT_OBJECT_UP, bEnable);
@ -2248,7 +2340,7 @@ void CBrain::UpdateInterface()
if ( type == OBJECT_MOBILErs ) // shield? if ( type == OBJECT_MOBILErs ) // shield?
{ {
if ( (m_secondaryTask == 0 || !m_secondaryTask->IsBusy()) && m_program == -1 ) if ( (m_secondaryTask == 0 || !m_secondaryTask->IsBusy()) && m_currentProgram == nullptr )
{ {
EnableInterface(pw, EVENT_OBJECT_BEGSHIELD, (m_secondaryTask == 0)); EnableInterface(pw, EVENT_OBJECT_BEGSHIELD, (m_secondaryTask == 0));
EnableInterface(pw, EVENT_OBJECT_ENDSHIELD, (m_secondaryTask != 0)); EnableInterface(pw, EVENT_OBJECT_ENDSHIELD, (m_secondaryTask != 0));
@ -2325,31 +2417,31 @@ void CBrain::UpdateInterface()
type == OBJECT_CONTROLLER) // vehicle? type == OBJECT_CONTROLLER) // vehicle?
{ {
bRun = false; bRun = false;
if ( m_script[m_selScript] != 0 ) if ( m_selScript < m_program.size() )
{ {
m_script[m_selScript]->GetTitle(title); m_program[m_selScript]->script->GetTitle(title);
if ( title[0] != 0 ) if ( title[0] != 0 )
{ {
bRun = true; bRun = true;
} }
} }
if ( !bEnable && m_program == -1 ) bRun = false; if ( !bEnable && m_currentProgram == nullptr ) bRun = false;
if ( m_bTraceRecord ) bRun = false; if ( m_bTraceRecord ) bRun = false;
EnableInterface(pw, EVENT_OBJECT_PROGRUN, bRun); EnableInterface(pw, EVENT_OBJECT_PROGRUN, bRun);
pb = static_cast< Ui::CButton* >(pw->SearchControl(EVENT_OBJECT_PROGRUN)); pb = static_cast< Ui::CButton* >(pw->SearchControl(EVENT_OBJECT_PROGRUN));
if ( pb != 0 ) if ( pb != 0 )
{ {
pb->SetIcon(m_program==-1?21:8); // run/stop pb->SetIcon(m_currentProgram==nullptr?21:8); // run/stop
} }
//? pb = (CButton*)pw->SearchControl(EVENT_OBJECT_PROGEDIT); //? pb = (CButton*)pw->SearchControl(EVENT_OBJECT_PROGEDIT);
//? if ( pb != 0 ) //? if ( pb != 0 )
//? { //? {
//? pb->SetIcon(m_program==-1?22:40); // edit/debug //? pb->SetIcon(m_currentProgram==nullptr?22:40); // edit/debug
//? } //? }
BlinkScript(m_program != -1); // blinks if script execution BlinkScript(m_currentProgram != nullptr); // blinks if script execution
} }
if ( type == OBJECT_MOBILEfa || if ( type == OBJECT_MOBILEfa ||
@ -2470,22 +2562,19 @@ void CBrain::UpdateScript(Ui::CWindow *pw)
Ui::CList* pl; Ui::CList* pl;
char name[100]; char name[100];
char title[100]; char title[100];
int i;
pl = static_cast< Ui::CList* >(pw->SearchControl(EVENT_OBJECT_PROGLIST)); pl = static_cast< Ui::CList* >(pw->SearchControl(EVENT_OBJECT_PROGLIST));
if ( pl == 0 ) return; if ( pl == 0 ) return;
for ( i=0 ; i<BRAINMAXSCRIPT ; i++ ) pl->Flush();
for ( unsigned int i = 0 ; i < m_program.size() ; i++ )
{ {
sprintf(name, "%d", i+1); sprintf(name, "%d", i+1);
if ( m_script[i] != 0 ) m_program[i]->script->GetTitle(title);
if ( title[0] != 0 )
{ {
m_script[i]->GetTitle(title); sprintf(name, "%d: %s", i+1, title);
if ( title[0] != 0 )
{
sprintf(name, "%d: %s", i+1, title);
}
} }
pl->SetItemName(i, name); pl->SetItemName(i, name);
@ -2626,26 +2715,16 @@ bool CBrain::GetActivity()
bool CBrain::IsProgram() bool CBrain::IsProgram()
{ {
return ( m_program != -1 ); return m_currentProgram != nullptr;
}
// Indicates whether a program exists.
bool CBrain::ProgramExist(int rank)
{
return ( m_script[rank] != 0 );
} }
// Starts a program. // Starts a program.
void CBrain::RunProgram(int rank) void CBrain::RunProgram(Program* program)
{ {
if ( rank < 0 ) return; if ( program->script->Run() )
if ( m_script[rank] != 0 &&
m_script[rank]->Run() )
{ {
m_program = rank; // start new program m_currentProgram = program; // start new program
BlinkScript(true); // blink BlinkScript(true); // blink
m_object->CreateSelectParticle(); m_object->CreateSelectParticle();
m_main->UpdateShortcuts(); m_main->UpdateShortcuts();
@ -2654,50 +2733,37 @@ void CBrain::RunProgram(int rank)
} }
} }
// Returns the first free program.
int CBrain::FreeProgram()
{
int i;
for ( i=0 ; i<BRAINMAXSCRIPT ; i++ )
{
if ( m_script[i] == 0 ) return i;
}
return -1;
}
// Returns the current program. // Returns the current program.
int CBrain::GetProgram() int CBrain::GetProgram()
{ {
return m_program; if(m_currentProgram == nullptr)
return -1;
for(unsigned int i = 0; i < m_program.size(); i++)
{
if(m_program[i] == m_currentProgram)
{
return i;
}
}
return -1;
} }
// Name management scripts to load. // Name management scripts to load.
void CBrain::SetScriptRun(int rank) void CBrain::SetScriptRun(Program* program)
{ {
m_scriptRun = rank; m_scriptRun = program;
} }
int CBrain::GetScriptRun() Program* CBrain::GetScriptRun()
{ {
return m_scriptRun; return m_scriptRun;
} }
void CBrain::SetScriptName(int rank, char *name)
{
strcpy(m_scriptName[rank], name);
}
char* CBrain::GetScriptName(int rank)
{
return m_scriptName[rank];
}
void CBrain::SetSoluceName(char *name) void CBrain::SetSoluceName(char *name)
{ {
strcpy(m_soluceName, name); strcpy(m_soluceName, name);
@ -2714,21 +2780,15 @@ char* CBrain::GetSoluceName()
bool CBrain::ReadSoluce(char* filename) bool CBrain::ReadSoluce(char* filename)
{ {
int rank, i; Program* prog = AddProgram();
rank = FreeProgram(); if ( !ReadProgram(prog, filename) ) return false; // load solution
if ( rank == -1 ) return false;
if ( !ReadProgram(rank, filename) ) return false; // load solution for(unsigned int i = 0; i < m_program.size(); i++)
for ( i=0 ; i<BRAINMAXSCRIPT ; i++ )
{ {
if ( i == rank || m_script[i] == 0 ) continue; if ( m_program[i]->script->Compare(prog->script) ) // the same already?
if ( m_script[i]->Compare(m_script[rank]) ) // the same already?
{ {
delete m_script[rank]; RemoveProgram(prog);
m_script[rank] = 0;
return false; return false;
} }
} }
@ -2736,61 +2796,27 @@ bool CBrain::ReadSoluce(char* filename)
return true; 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. // Load a script with a text file.
bool CBrain::ReadProgram(int rank, const char* filename) bool CBrain::ReadProgram(Program* program, const char* filename)
{ {
if ( m_script[rank] == 0 ) if ( program->script->ReadScript(filename) ) return true;
{
m_script[rank] = new CScript(m_object, &m_secondaryTask);
}
if ( m_script[rank]->ReadScript(filename) ) return true;
delete m_script[rank];
m_script[rank] = 0;
return false; return false;
} }
// Indicates whether a program is compiled correctly. // Indicates whether a program is compiled correctly.
bool CBrain::GetCompile(int rank) bool CBrain::GetCompile(Program* program)
{ {
if ( m_script[rank] == 0 ) return false; return program->script->GetCompile();
return m_script[rank]->GetCompile();
} }
// Saves a script in a text file. // Saves a script in a text file.
bool CBrain::WriteProgram(int rank, char* filename) bool CBrain::WriteProgram(Program* program, char* filename)
{ {
if ( m_script[rank] == 0 ) if ( program->script->WriteScript(filename) ) return true;
{
m_script[rank] = new CScript(m_object, &m_secondaryTask);
}
if ( m_script[rank]->WriteScript(filename) ) return true;
delete m_script[rank];
m_script[rank] = 0;
return false; return false;
} }
@ -2806,39 +2832,36 @@ bool CBrain::ReadStack(FILE *file)
if ( op == 1 ) // run ? if ( op == 1 ) // run ?
{ {
fRead(&op, sizeof(short), 1, file); // program rank fRead(&op, sizeof(short), 1, file); // program rank
if ( op >= 0 && op < BRAINMAXSCRIPT ) if ( op >= 0 )
{ {
m_program = op; // program restarts assert(op < static_cast<int>(m_program.size())); //TODO: is it good?
m_selScript = op; m_selScript = op;
BlinkScript(true); // blink BlinkScript(true); // blink
if ( m_script[op] == 0 ) if ( !m_program[op]->script->ReadStack(file) ) return false;
{
m_script[op] = new CScript(m_object, &m_secondaryTask);
}
if ( !m_script[op]->ReadStack(file) ) return false;
} }
} }
return true; return true;
} }
// ave the script implementation stack of a file. // Save the script implementation stack of a file.
bool CBrain::WriteStack(FILE *file) bool CBrain::WriteStack(FILE *file)
{ {
short op; short op;
if ( m_program != -1 && // current program? if ( m_currentProgram != nullptr && // current program?
m_script[m_program]->IsRunning() ) m_currentProgram->script->IsRunning() )
{ {
op = 1; // run op = 1; // run
fWrite(&op, sizeof(short), 1, file); fWrite(&op, sizeof(short), 1, file);
op = m_program; op = GetProgram();
fWrite(&op, sizeof(short), 1, file); fWrite(&op, sizeof(short), 1, file);
return m_script[m_program]->WriteStack(file); return m_currentProgram->script->WriteStack(file);
} }
op = 0; // stop op = 0; // stop
@ -2847,6 +2870,12 @@ bool CBrain::WriteStack(FILE *file)
} }
const std::vector<Program*>& CBrain::GetPrograms()
{
return m_program;
}
// Start of registration of the design. // Start of registration of the design.
@ -2973,12 +3002,8 @@ void CBrain::TraceRecordStop()
strncat(buffer, "}\n", max-1); strncat(buffer, "}\n", max-1);
buffer[max-1] = 0; buffer[max-1] = 0;
i = m_selScript; Program* prog = AddProgram();
if ( m_script[i] == 0 ) prog->script->SendScript(buffer);
{
m_script[i] = new CScript(m_object, &m_secondaryTask);
}
m_script[i]->SendScript(buffer);
delete[] buffer; delete[] buffer;
} }
@ -3044,3 +3069,80 @@ bool CBrain::TraceRecordPut(char *buffer, int max, TraceOper oper, float param)
return true; return true;
} }
Program* CBrain::AddProgram()
{
Program* program = new Program();
program->script = new CScript(m_object, &m_secondaryTask);
AddProgram(program);
return program;
}
bool CBrain::AddProgram(Program* program)
{
if(std::find(m_program.begin(), m_program.end(), program) != m_program.end())
return false;
m_program.push_back(program);
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
return true;
}
void CBrain::RemoveProgram(Program* program)
{
if(m_currentProgram == program)
{
StopProgram();
}
m_program.erase(std::remove(m_program.begin(), m_program.end(), program), m_program.end());
delete program->script;
program->script = nullptr;
delete program;
UpdateInterface();
Ui::CWindow* pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
if ( pw != 0 )
{
UpdateScript(pw);
}
}
int CBrain::GetProgramIndex(Program* program)
{
for(unsigned int i = 0; i < m_program.size(); i++)
{
if(m_program[i] == program)
{
return i;
}
}
return -1;
}
Program* CBrain::GetProgram(int index)
{
if(index < 0 || index >= static_cast<int>(m_program.size()))
return nullptr;
return m_program[index];
}
Program* CBrain::GetOrAddProgram(int index)
{
if(index < 0)
return nullptr;
if(index < static_cast<int>(m_program.size()))
return m_program[index];
for(int i = m_program.size(); i < index; i++)
{
AddProgram();
}
return AddProgram();
}

View File

@ -58,9 +58,6 @@ class CParticle;
} /* Gfx */ } /* Gfx */
const int BRAINMAXSCRIPT = 10;
enum TraceOper enum TraceOper
{ {
@ -77,6 +74,12 @@ struct TraceRecord
float param; float param;
}; };
struct Program
{
CScript* script;
std::string filename;
};
class CBrain class CBrain
@ -100,9 +103,7 @@ public:
void SetActivity(bool bMode); void SetActivity(bool bMode);
bool GetActivity(); bool GetActivity();
bool IsProgram(); bool IsProgram();
bool ProgramExist(int rank); void RunProgram(Program* program);
void RunProgram(int rank);
int FreeProgram();
int GetProgram(); int GetProgram();
void StopProgram(); void StopProgram();
void StopTask(); void StopTask();
@ -112,20 +113,18 @@ public:
void SetActiveVirus(bool bActive); void SetActiveVirus(bool bActive);
bool GetActiveVirus(); bool GetActiveVirus();
void SetScriptRun(int rank); void SetScriptRun(Program* rank);
int GetScriptRun(); Program* GetScriptRun();
void SetScriptName(int rank, char *name);
char* GetScriptName(int rank);
void SetSoluceName(char *name); void SetSoluceName(char *name);
char* GetSoluceName(); char* GetSoluceName();
bool SendProgram(int rank, const char* buffer);
bool ReadSoluce(char* filename); bool ReadSoluce(char* filename);
bool ReadProgram(int rank, const char* filename); bool ReadProgram(Program* program, const char* filename);
bool GetCompile(int rank); bool GetCompile(Program* program);
bool WriteProgram(int rank, char* filename); bool WriteProgram(Program* program, char* filename);
bool ReadStack(FILE *file); bool ReadStack(FILE *file);
bool WriteStack(FILE *file); bool WriteStack(FILE *file);
const std::vector<Program*>& GetPrograms();
Error StartTaskTake(); Error StartTaskTake();
Error StartTaskManip(TaskManipOrder order, TaskManipArm arm); Error StartTaskManip(TaskManipOrder order, TaskManipArm arm);
@ -146,10 +145,18 @@ public:
void UpdateInterface(float rTime); void UpdateInterface(float rTime);
void UpdateInterface(); void UpdateInterface();
Program* AddProgram();
bool AddProgram(Program* program);
void RemoveProgram(Program* program);
Program* GetProgram(int index);
Program* GetOrAddProgram(int index);
int GetProgramIndex(Program* program);
protected: protected:
bool EventFrame(const Event &event); bool EventFrame(const Event &event);
void StartEditScript(int rank, char* name); void StartEditScript(Program* program, char* name);
void StopEditScript(bool bCancel); void StopEditScript(bool bCancel);
Error EndedTask(); Error EndedTask();
@ -190,15 +197,16 @@ protected:
CTaskManager* m_primaryTask; CTaskManager* m_primaryTask;
CTaskManager* m_secondaryTask; CTaskManager* m_secondaryTask;
CScript* m_script[BRAINMAXSCRIPT]; std::vector<Program*> m_program;
int m_selScript; // rank of the selected script Program* m_currentProgram;
int m_program; // rank of the executed program / -1
unsigned int m_selScript; // rank of the selected script
bool m_bActivity; bool m_bActivity;
bool m_bBurn; bool m_bBurn;
bool m_bActiveVirus; bool m_bActiveVirus;
int m_scriptRun; Program* m_scriptRun;
char m_scriptName[BRAINMAXSCRIPT][50];
char m_soluceName[50]; char m_soluceName[50];
EventType m_buttonAxe; EventType m_buttonAxe;

View File

@ -306,7 +306,7 @@ CObject::CObject()
m_bResetBusy = false; m_bResetBusy = false;
m_resetPosition = Math::Vector(0.0f, 0.0f, 0.0f); m_resetPosition = Math::Vector(0.0f, 0.0f, 0.0f);
m_resetAngle = Math::Vector(0.0f, 0.0f, 0.0f); m_resetAngle = Math::Vector(0.0f, 0.0f, 0.0f);
m_resetRun = -1; m_resetRun = nullptr;
m_cameraType = Gfx::CAM_TYPE_BACK; m_cameraType = Gfx::CAM_TYPE_BACK;
m_cameraDist = 50.0f; m_cameraDist = 50.0f;
@ -1058,7 +1058,7 @@ bool CObject::Write(CLevelParserLine* line)
line->AddParam("resetCap", new CLevelParserParam(static_cast<int>(GetResetCap()))); line->AddParam("resetCap", new CLevelParserParam(static_cast<int>(GetResetCap())));
line->AddParam("resetPos", new CLevelParserParam(GetResetPosition()/g_unit)); line->AddParam("resetPos", new CLevelParserParam(GetResetPosition()/g_unit));
line->AddParam("resetAngle", new CLevelParserParam(GetResetAngle()/(Math::PI/180.0f))); line->AddParam("resetAngle", new CLevelParserParam(GetResetAngle()/(Math::PI/180.0f)));
line->AddParam("resetRun", new CLevelParserParam(GetResetRun())); line->AddParam("resetRun", new CLevelParserParam(m_brain->GetProgramIndex(GetResetRun())));
} }
if ( m_bVirusMode ) if ( m_bVirusMode )
@ -1145,7 +1145,7 @@ bool CObject::Read(CLevelParserLine* line)
SetResetCap(static_cast<ResetCap>(line->GetParam("resetCap")->AsInt(0))); SetResetCap(static_cast<ResetCap>(line->GetParam("resetCap")->AsInt(0)));
SetResetPosition(line->GetParam("resetPos")->AsPoint(Math::Vector())*g_unit); SetResetPosition(line->GetParam("resetPos")->AsPoint(Math::Vector())*g_unit);
SetResetAngle(line->GetParam("resetAngle")->AsPoint(Math::Vector())*(Math::PI/180.0f)); SetResetAngle(line->GetParam("resetAngle")->AsPoint(Math::Vector())*(Math::PI/180.0f));
SetResetRun(line->GetParam("resetRun")->AsInt(0)); SetResetRun(m_brain->GetProgram(line->GetParam("resetRun")->AsInt(-1)));
m_bBurn = line->GetParam("burnMode")->AsBool(false); m_bBurn = line->GetParam("burnMode")->AsBool(false);
m_bVirusMode = line->GetParam("virusMode")->AsBool(false); m_bVirusMode = line->GetParam("virusMode")->AsBool(false);
m_virusTime = line->GetParam("virusTime")->AsFloat(0.0f); m_virusTime = line->GetParam("virusTime")->AsFloat(0.0f);
@ -1822,12 +1822,12 @@ Math::Vector CObject::GetResetAngle()
return m_resetAngle; return m_resetAngle;
} }
int CObject::GetResetRun() Program* CObject::GetResetRun()
{ {
return m_resetRun; return m_resetRun;
} }
void CObject::SetResetRun(int run) void CObject::SetResetRun(Program* run)
{ {
m_resetRun = run; m_resetRun = run;
} }
@ -5525,44 +5525,26 @@ void CObject::CreateOtherObject(ObjectType type)
// Reads a program. // Reads a program.
bool CObject::ReadProgram(int rank, const char* filename) bool CObject::ReadProgram(Program* program, const char* filename)
{ {
if ( m_brain != 0 ) if ( m_brain != 0 )
{ {
return m_brain->ReadProgram(rank, filename); return m_brain->ReadProgram(program, filename);
} }
return false; return false;
} }
// Writes a program. // Writes a program.
bool CObject::WriteProgram(int rank, char* filename) bool CObject::WriteProgram(Program* program, char* filename)
{ {
if ( m_brain != 0 ) if ( m_brain != 0 )
{ {
return m_brain->WriteProgram(rank, filename); return m_brain->WriteProgram(program, filename);
} }
return false; return false;
} }
// Starts a program.
bool CObject::RunProgram(int rank)
{
if ( m_brain != 0 )
{
m_brain->RunProgram(rank);
return true;
}
if ( m_auto != 0 )
{
m_auto->Start(rank);
return true;
}
return false;
}
// Calculates the matrix for transforming the object. // Calculates the matrix for transforming the object.

View File

@ -41,6 +41,7 @@ class CRobotMain;
class CBotVar; class CBotVar;
class CScript; class CScript;
class CLevelParserLine; class CLevelParserLine;
struct Program;
/** /**
@ -401,9 +402,8 @@ public:
bool CreateRuin(Math::Vector pos, float angle, float height, ObjectType type); bool CreateRuin(Math::Vector pos, float angle, float height, ObjectType type);
bool CreateApollo(Math::Vector pos, float angle, ObjectType type); bool CreateApollo(Math::Vector pos, float angle, ObjectType type);
bool ReadProgram(int rank, const char* filename); bool ReadProgram(Program* program, const char* filename);
bool WriteProgram(int rank, char* filename); bool WriteProgram(Program* program, char* filename);
bool RunProgram(int rank);
int GetShadowLight(); int GetShadowLight();
int GetEffectLight(); int GetEffectLight();
@ -471,8 +471,8 @@ public:
Math::Vector GetResetPosition(); Math::Vector GetResetPosition();
void SetResetAngle(const Math::Vector &angle); void SetResetAngle(const Math::Vector &angle);
Math::Vector GetResetAngle(); Math::Vector GetResetAngle();
void SetResetRun(int run); void SetResetRun(Program* run);
int GetResetRun(); Program* GetResetRun();
void SetMasterParticle(int part, int parti); void SetMasterParticle(int part, int parti);
int GetMasterParticle(int part); int GetMasterParticle(int part);
@ -765,7 +765,7 @@ protected:
bool m_bResetBusy; bool m_bResetBusy;
Math::Vector m_resetPosition; Math::Vector m_resetPosition;
Math::Vector m_resetAngle; Math::Vector m_resetAngle;
int m_resetRun; Program* m_resetRun;
int m_infoTotal; int m_infoTotal;
Info m_info[OBJECTMAXINFO]; Info m_info[OBJECTMAXINFO];

View File

@ -3471,12 +3471,11 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
m_controller->SetMagnifyDamage(100.0f); m_controller->SetMagnifyDamage(100.0f);
m_controller->SetIgnoreBuildCheck(true); m_controller->SetIgnoreBuildCheck(true);
CBrain* brain = m_controller->GetBrain(); CBrain* brain = m_controller->GetBrain();
if (brain != nullptr) if (brain != nullptr && line->GetParam("script")->IsDefined())
{ {
std::string name = "../"+line->GetParam("script")->AsPath("ai"); Program* program = brain->AddProgram();
if (!name.empty()) program->filename = "../"+line->GetParam("script")->AsPath("ai");
brain->SetScriptName(0, const_cast<char*>(name.c_str())); brain->SetScriptRun(program);
brain->SetScriptRun(0);
} }
continue; continue;
} }
@ -3628,6 +3627,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
} }
int run = -1; int run = -1;
std::map<int, Program*> loadedPrograms;
CBrain* brain = obj->GetBrain(); CBrain* brain = obj->GetBrain();
if (brain != nullptr) if (brain != nullptr)
{ {
@ -3635,7 +3635,9 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
{ {
std::string op = "script"+boost::lexical_cast<std::string>(i+1); // script1..script10 std::string op = "script"+boost::lexical_cast<std::string>(i+1); // script1..script10
if(line->GetParam(op)->IsDefined()) { if(line->GetParam(op)->IsDefined()) {
brain->SetScriptName(i, const_cast<char*>(("../"+line->GetParam(op)->AsPath("ai")).c_str())); Program* program = brain->AddProgram();
program->filename = "../"+line->GetParam(op)->AsPath("ai");
loadedPrograms[i] = program;
} }
} }
@ -3644,7 +3646,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (i != 0) if (i != 0)
{ {
run = i-1; run = i-1;
brain->SetScriptRun(run); brain->SetScriptRun(loadedPrograms[run]);
} }
} }
CAuto* automat = obj->GetAuto(); CAuto* automat = obj->GetAuto();
@ -3673,7 +3675,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
obj->SetResetPosition(obj->GetPosition(0)); obj->SetResetPosition(obj->GetPosition(0));
obj->SetResetAngle(obj->GetAngle(0)); obj->SetResetAngle(obj->GetAngle(0));
obj->SetResetRun(run); obj->SetResetRun(loadedPrograms[run]);
if (line->GetParam("reset")->AsBool(false)) if (line->GetParam("reset")->AsBool(false))
obj->SetResetCap(RESET_MOVE); obj->SetResetCap(RESET_MOVE);
@ -4569,19 +4571,16 @@ void CRobotMain::CompileScript(bool soluce)
CBrain* brain = obj->GetBrain(); CBrain* brain = obj->GetBrain();
if (brain == nullptr) continue; if (brain == nullptr) continue;
for (int j = 0; j < 10; j++) for(Program* program : brain->GetPrograms())
{ {
if (brain->GetCompile(j)) continue; //? if (brain->GetCompile(j)) continue;
if(program->filename.empty()) continue;
std::string name = brain->GetScriptName(j); std::string name = "ai/"+program->filename;
if (name[0] != 0) if(! brain->ReadProgram(program, const_cast<char*>(name.c_str()))) {
{ CLogger::GetInstancePointer()->Error("Unable to read script from file \"%s\"\n", name.c_str());
name = "ai/"+name;
if(! brain->ReadProgram(j, const_cast<char*>(name.c_str()))) {
CLogger::GetInstancePointer()->Error("Unable to read script from file \"%s\"\n", name.c_str());
}
if (!brain->GetCompile(j)) nbError++;
} }
if (!brain->GetCompile(program)) nbError++;
} }
LoadOneScript(obj, nbError); LoadOneScript(obj, nbError);
@ -4617,10 +4616,10 @@ void CRobotMain::CompileScript(bool soluce)
CBrain* brain = obj->GetBrain(); CBrain* brain = obj->GetBrain();
if (brain == nullptr) continue; if (brain == nullptr) continue;
int run = brain->GetScriptRun(); Program* program = brain->GetScriptRun();
if (run != -1) if (program != nullptr)
{ {
brain->RunProgram(run); // starts the program brain->RunProgram(program); // starts the program
} }
} }
} }
@ -4642,15 +4641,20 @@ void CRobotMain::LoadOneScript(CObject *obj, int &nbError)
char* name = m_dialog->GetSceneName(); char* name = m_dialog->GetSceneName();
int rank = m_dialog->GetSceneRank(); int rank = m_dialog->GetSceneRank();
for (int i = 0; i < BRAINMAXSCRIPT; i++) for(unsigned int i = 0; i < 999; i++)
{ {
if (brain->GetCompile(i)) continue; //? if (brain->GetCompile(i)) continue;
char filename[MAX_FNAME]; char filename[MAX_FNAME];
sprintf(filename, "%s/%s/%c%.3d%.3d%.1d.txt", sprintf(filename, "%s/%s/%c%.3d%.3d%.3d.txt",
GetSavegameDir(), m_gamerName.c_str(), name[0], rank, objRank, i); GetSavegameDir(), m_gamerName.c_str(), name[0], rank, objRank, i);
brain->ReadProgram(i, filename);
if (!brain->GetCompile(i)) nbError++; if(CResourceManager::Exists(filename))
{
Program* program = brain->GetOrAddProgram(i);
brain->ReadProgram(program, filename);
if (!brain->GetCompile(program)) nbError++;
}
} }
} }
@ -4670,11 +4674,15 @@ void CRobotMain::LoadFileScript(CObject *obj, const char* filename, int objRank,
dirname = dirname.substr(0, dirname.find_last_of("/")); dirname = dirname.substr(0, dirname.find_last_of("/"));
char fn[MAX_FNAME]; //TODO: Refactor to std::string char fn[MAX_FNAME]; //TODO: Refactor to std::string
for (int i = 0; i < BRAINMAXSCRIPT; i++) for(unsigned int i = 0; i < 999; i++)
{ {
sprintf(fn, "%s/prog%.3d%.1d.txt", dirname.c_str(), objRank, i); sprintf(fn, "%s/prog%.3d%.3d.txt", dirname.c_str(), objRank, i);
brain->ReadProgram(i, fn); if(CResourceManager::Exists(fn))
if (!brain->GetCompile(i)) nbError++; {
Program* program = brain->GetOrAddProgram(i);
brain->ReadProgram(program, fn);
if (!brain->GetCompile(program)) nbError++;
}
} }
} }
@ -4707,12 +4715,21 @@ void CRobotMain::SaveOneScript(CObject *obj)
char* name = m_dialog->GetSceneName(); char* name = m_dialog->GetSceneName();
int rank = m_dialog->GetSceneRank(); int rank = m_dialog->GetSceneRank();
for (int i = 0; i < BRAINMAXSCRIPT; i++) auto programs = brain->GetPrograms();
// TODO: Find a better way to do that
for(unsigned int i = 0; i < 999; i++)
{ {
char filename[MAX_FNAME]; char filename[MAX_FNAME];
sprintf(filename, "%s/%s/%c%.3d%.3d%.1d.txt", sprintf(filename, "%s/%s/%c%.3d%.3d%.3d.txt",
GetSavegameDir(), m_gamerName.c_str(), name[0], rank, objRank, i); GetSavegameDir(), m_gamerName.c_str(), name[0], rank, objRank, i);
brain->WriteProgram(i, filename); if(i < programs.size())
{
brain->WriteProgram(programs[i], filename);
}
else
{
CResourceManager::Remove(filename);
}
} }
} }
@ -4732,10 +4749,19 @@ void CRobotMain::SaveFileScript(CObject *obj, const char* filename, int objRank)
dirname = dirname.substr(0, dirname.find_last_of("/")); dirname = dirname.substr(0, dirname.find_last_of("/"));
char fn[MAX_FNAME]; //TODO: Refactor to std::string char fn[MAX_FNAME]; //TODO: Refactor to std::string
for (int i = 0; i < BRAINMAXSCRIPT; i++) auto programs = brain->GetPrograms();
// TODO: Find a better way to do that
for(unsigned int i = 0; i < 999; i++)
{ {
sprintf(fn, "%s/prog%.3d%.1d.txt", dirname.c_str(), objRank, i); sprintf(fn, "%s/prog%.3d%.3d.txt", dirname.c_str(), objRank, i);
brain->WriteProgram(i, fn); if(i < programs.size())
{
brain->WriteProgram(programs[i], fn);
}
else
{
CResourceManager::Remove(fn);
}
} }
} }

View File

@ -177,7 +177,12 @@ Error CTaskReset::Start(Math::Vector goal, Math::Vector angle)
{ {
m_object->SetPosition(0, goal); m_object->SetPosition(0, goal);
m_object->SetAngle(0, angle); m_object->SetAngle(0, angle);
m_brain->RunProgram(m_object->GetResetRun()); Program* program = m_object->GetResetRun();
if(program != nullptr)
{
m_brain->AddProgram(program);
m_brain->RunProgram(program);
}
m_bError = false; m_bError = false;
return ERR_OK; return ERR_OK;
@ -258,7 +263,12 @@ Error CTaskReset::IsEnded()
power->SetEnergy(power->GetCapacity()); // refueling power->SetEnergy(power->GetCapacity()); // refueling
} }
m_brain->RunProgram(m_object->GetResetRun()); Program* program = m_object->GetResetRun();
if(program != nullptr)
{
m_brain->AddProgram(program);
m_brain->RunProgram(program);
}
m_object->SetResetBusy(false); m_object->SetResetBusy(false);
return ERR_STOP; return ERR_STOP;
} }

View File

@ -1028,7 +1028,7 @@ bool CScript::WriteScript(const char* filename)
if ( m_script == nullptr ) if ( m_script == nullptr )
{ {
remove(filename); CResourceManager::Remove(filename);
return false; return false;
} }

View File

@ -32,10 +32,11 @@
#include "math/all.h" #include "math/all.h"
#include "object/brain.h"
#include "object/object.h" #include "object/object.h"
#include "object/objman.h"
#include "object/robotmain.h" #include "object/robotmain.h"
#include "object/task/taskmanager.h" #include "object/task/taskmanager.h"
#include "object/objman.h"
#include "object/auto/auto.h" #include "object/auto/auto.h"
#include "object/auto/autofactory.h" #include "object/auto/autofactory.h"
@ -1709,8 +1710,13 @@ bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, v
if (name[0] != 0) if (name[0] != 0)
{ {
std::string name2 = CPathManager::InjectLevelDir(name, "ai"); std::string name2 = CPathManager::InjectLevelDir(name, "ai");
object->ReadProgram(0, name2.c_str()); CBrain* brain = object->GetBrain();
object->RunProgram(0); if(brain != nullptr)
{
Program* program = brain->AddProgram();
brain->ReadProgram(program, name2.c_str());
brain->RunProgram(program);
}
} }
result->SetValInt(0); // no error result->SetValInt(0); // no error

View File

@ -32,6 +32,7 @@
#include "graphics/engine/camera.h" #include "graphics/engine/camera.h"
#include "graphics/engine/engine.h" #include "graphics/engine/engine.h"
#include "object/brain.h"
#include "object/object.h" #include "object/object.h"
#include "script/cbottoken.h" #include "script/cbottoken.h"
@ -542,7 +543,7 @@ void CStudio::ColorizeScript(CEdit* edit)
// Starts editing a program. // Starts editing a program.
void CStudio::StartEditScript(CScript *script, std::string name, int rank) void CStudio::StartEditScript(CScript *script, std::string name, Program* program)
{ {
Math::Point pos, dim; Math::Point pos, dim;
CWindow* pw; CWindow* pw;
@ -552,7 +553,7 @@ void CStudio::StartEditScript(CScript *script, std::string name, int rank)
CList* list; CList* list;
m_script = script; m_script = script;
m_rank = rank; m_program = program;
m_main->SetEditLock(true, true); m_main->SetEditLock(true, true);
m_main->SetEditFull(false); m_main->SetEditFull(false);

View File

@ -42,6 +42,8 @@ class CEngine;
class CCamera; class CCamera;
}; };
struct Program;
namespace Ui { namespace Ui {
class CEdit; class CEdit;
@ -66,7 +68,7 @@ public:
bool EventProcess(const Event &event); bool EventProcess(const Event &event);
void StartEditScript(CScript *script, std::string name, int rank); void StartEditScript(CScript *script, std::string name, Program* program);
bool StopEditScript(bool bCancel); bool StopEditScript(bool bCancel);
protected: protected:
@ -104,7 +106,7 @@ protected:
CApplication* m_app; CApplication* m_app;
CPauseManager* m_pause; CPauseManager* m_pause;
int m_rank; Program* m_program;
CScript* m_script; CScript* m_script;
Gfx::CameraType m_editCamera; Gfx::CameraType m_editCamera;