From 6a4ac9ce1614052b25fea0a0c0572d0ee1afbdff Mon Sep 17 00:00:00 2001 From: XienDev Date: Sun, 28 Apr 2013 16:49:48 +0300 Subject: Added functions "canbuild(category)" and "buildinfo(category)" Also fixed issue with undefined behaviour of build(category) function --- src/script/cbottoken.cpp | 10 +++- src/script/script.cpp | 135 +++++++++++++++++++++++++++++++++-------------- src/script/script.h | 5 +- 3 files changed, 107 insertions(+), 43 deletions(-) diff --git a/src/script/cbottoken.cpp b/src/script/cbottoken.cpp index 0bd52da..6eb6592 100644 --- a/src/script/cbottoken.cpp +++ b/src/script/cbottoken.cpp @@ -262,6 +262,8 @@ std::string GetHelpFilename(const char *token) if ( strcmp(token, "turn" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/turn.txt"); if ( strcmp(token, "goto" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/goto.txt"); if ( strcmp(token, "grab" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/grab.txt"); + if ( strcmp(token, "buildinfo" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/buildinfo.txt"); + if ( strcmp(token, "canbuild" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/canbuild.txt"); if ( strcmp(token, "build" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/build.txt"); if ( strcmp(token, "drop" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/drop.txt"); if ( strcmp(token, "sniff" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/sniff.txt"); @@ -380,7 +382,9 @@ bool IsFunction(const char *token) if ( strcmp(token, "turn" ) == 0 ) return true; if ( strcmp(token, "goto" ) == 0 ) return true; if ( strcmp(token, "grab" ) == 0 ) return true; - if ( strcmp(token, "build" ) == 0 ) return true; + if ( strcmp(token, "buildinfo" ) == 0 ) return true; + if ( strcmp(token, "canbuild" ) == 0 ) return true; + if ( strcmp(token, "build" ) == 0 ) return true; if ( strcmp(token, "drop" ) == 0 ) return true; if ( strcmp(token, "sniff" ) == 0 ) return true; if ( strcmp(token, "receive" ) == 0 ) return true; @@ -463,7 +467,9 @@ const char* GetHelpText(const char *token) if ( strcmp(token, "turn" ) == 0 ) return "turn ( angle );"; if ( strcmp(token, "goto" ) == 0 ) return "goto ( position, altitude );"; if ( strcmp(token, "grab" ) == 0 ) return "grab ( order );"; - if ( strcmp(token, "build" ) == 0 ) return "build ( category );"; + if ( strcmp(token, "buildinfo" ) == 0 ) return "buildinfo ( category );"; + if ( strcmp(token, "canbuild" ) == 0 ) return "canbuild ( category );"; + if ( strcmp(token, "build" ) == 0 ) return "build ( category );"; if ( strcmp(token, "drop" ) == 0 ) return "drop ( order );"; if ( strcmp(token, "sniff" ) == 0 ) return "sniff ( );"; if ( strcmp(token, "receive" ) == 0 ) return "receive ( name, power );"; diff --git a/src/script/script.cpp b/src/script/script.cpp index c95cd4b..c9e3c66 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -1003,8 +1003,83 @@ bool CScript::rDirection(CBotVar* var, CBotVar* result, int& exception, void* us return true; } +// compilation of instruction "canbuild ( category );" + +CBotTypResult CScript::cCanBuild(CBotVar* &var, void* user) +{ + if ( var == 0 ) return CBotTypResult(CBotErrLowParam); + if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum); + var = var->GetNext(); + if ( var != 0 ) return CBotTypResult(CBotErrOverParam); + return CBotTypResult(CBotTypBoolean); +} + +// Instruction "canbuid ( category );" +// returns true if this building can be built + +bool CScript::rCanBuild(CBotVar* var, CBotVar* result, int& exception, void* user) +{ + exception = 0; + + CScript::rBuildInfo(var, result, exception, user); + Error err = static_cast(result->GetValInt()); + + if (err == ERR_OK) + result->SetValInt(true); + else + result->SetValInt(false); + + return true; +} + +// Instruction "buildinfo ( category );" +// This function indicates if this building can be built, and returns a specific value +// +// returns 0(ERR_OK) if this building can be built +// returns 132(ERR_BUILD_DISABLED) if can not build in a current mission +// returns 133(ERR_BUILD_RESEARCH) if this building needs to be researched + +bool CScript::rBuildInfo(CBotVar* var, CBotVar* result, int& exception, void* user) +{ + ObjectType category = static_cast(var->GetValInt()); //get category parameter + exception = 0; + int value = ERR_BUILD_DISABLED; + + if ( (category == OBJECT_DERRICK && (g_build & BUILD_DERRICK)) || + (category == OBJECT_FACTORY && (g_build & BUILD_FACTORY)) || + (category == OBJECT_STATION && (g_build & BUILD_STATION)) || + (category == OBJECT_CONVERT && (g_build & BUILD_CONVERT)) || + (category == OBJECT_REPAIR && (g_build & BUILD_REPAIR)) || + (category == OBJECT_TOWER && (g_build & BUILD_TOWER)) || + (category == OBJECT_RESEARCH && (g_build & BUILD_RESEARCH)) || + (category == OBJECT_RADAR && (g_build & BUILD_RADAR)) || + (category == OBJECT_ENERGY && (g_build & BUILD_ENERGY)) || + (category == OBJECT_LABO && (g_build & BUILD_LABO)) || + (category == OBJECT_NUCLEAR && (g_build & BUILD_NUCLEAR)) || + (category == OBJECT_INFO && (g_build & BUILD_INFO )) || + (category == OBJECT_PARA && (g_build & BUILD_PARA ))) + { + + //if we want to build not researched one + if ( (category == OBJECT_TOWER && !(g_researchDone & RESEARCH_TOWER)) || + (category == OBJECT_NUCLEAR && !(g_researchDone & RESEARCH_ATOMIC)) + ) + { + value = ERR_BUILD_RESEARCH; + } + else + { + value = ERR_OK; + } + + } + + result->SetValInt(value); + return true; +} + // Instruction "build(type)" -// draws error if can not build (wher errormode stop), otherwise 0 <- 1 +// draws error if can not build (wher errormode stop), otherwise 0 <- error bool CScript::rBuild(CBotVar* var, CBotVar* result, int& exception, void* user) { @@ -1027,48 +1102,26 @@ bool CScript::rBuild(CBotVar* var, CBotVar* result, int& exception, void* user) } else { - category = static_cast(var->GetValInt()); //get category parameter - - //if we want to produce one of these buildings - if ( (category == OBJECT_DERRICK && (g_build & BUILD_DERRICK)) || - (category == OBJECT_FACTORY && (g_build & BUILD_FACTORY)) || - (category == OBJECT_STATION && (g_build & BUILD_STATION)) || - (category == OBJECT_CONVERT && (g_build & BUILD_CONVERT)) || - (category == OBJECT_REPAIR && (g_build & BUILD_REPAIR)) || - (category == OBJECT_TOWER && (g_build & BUILD_TOWER)) || - (category == OBJECT_RESEARCH && (g_build & BUILD_RESEARCH)) || - (category == OBJECT_RADAR && (g_build & BUILD_RADAR)) || - (category == OBJECT_ENERGY && (g_build & BUILD_ENERGY)) || - (category == OBJECT_LABO && (g_build & BUILD_LABO)) || - (category == OBJECT_NUCLEAR && (g_build & BUILD_NUCLEAR)) || - (category == OBJECT_INFO && (g_build & BUILD_INFO )) || - (category == OBJECT_PARA && (g_build & BUILD_PARA ))) - { - - //if we want to build not researched one - if ( (category == OBJECT_TOWER && !(g_researchDone & RESEARCH_TOWER)) || - (category == OBJECT_NUCLEAR && !(g_researchDone & RESEARCH_ATOMIC)) - ) - { - err = ERR_BUILD_RESEARCH; - } - else if (script->m_primaryTask == 0) //if we have no other tasks - { - script->m_primaryTask = new CTaskManager(script->m_object); - err = script->m_primaryTask->StartTaskBuild(category); + //Let's check is building this object is possible + CScript::rBuildInfo(var, result, exception, user); + err = static_cast(result->GetValInt()); - if (err != ERR_OK) - { - delete script->m_primaryTask; - script->m_primaryTask = 0; - } - } - - } - else //if we can't build this object + if (err == ERR_OK && script->m_primaryTask == 0) //if we can build and no task is present { - err = ERR_BUILD_DISABLED; + category = static_cast(var->GetValInt()); //get category parameter + + script->m_primaryTask = new CTaskManager(script->m_object); + err = script->m_primaryTask->StartTaskBuild(category); + + if (err != ERR_OK) + { + delete script->m_primaryTask; + script->m_primaryTask = 0; + } } + //When script is waiting for finishing this task, it sets ERR_OK, and continues executing Process + //without creating new task. I think, there was a problem with previous version in release configuration + //It did not init error variable in this situation, and code tried to use variable with trash inside } if ( err != ERR_OK ) @@ -2939,6 +2992,8 @@ void CScript::InitFonctions() CBotProgram::AddFunction("pencolor", rPenColor, CScript::cOneFloat); CBotProgram::AddFunction("penwidth", rPenWidth, CScript::cOneFloat); + CBotProgram::AddFunction("buildinfo", rBuildInfo, CScript::cOneFloat); + CBotProgram::AddFunction("canbuild", rCanBuild, CScript::cCanBuild); CBotProgram::AddFunction("build", rBuild, CScript::cOneFloat); } diff --git a/src/script/script.h b/src/script/script.h index 597486b..702138b 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -104,7 +104,8 @@ private: static CBotTypResult cSearch(CBotVar* &var, void* user); static CBotTypResult cRadar(CBotVar* &var, void* user); static CBotTypResult cDetect(CBotVar* &var, void* user); - static CBotTypResult cDirection(CBotVar* &var, void* user); + static CBotTypResult cDirection(CBotVar* &var, void* user); + static CBotTypResult cCanBuild(CBotVar* &var, void* user); static CBotTypResult cProduce(CBotVar* &var, void* user); static CBotTypResult cDistance(CBotVar* &var, void* user); static CBotTypResult cSpace(CBotVar* &var, void* user); @@ -139,6 +140,8 @@ private: static bool rRadar(CBotVar* var, CBotVar* result, int& exception, void* user); static bool rDetect(CBotVar* var, CBotVar* result, int& exception, void* user); static bool rDirection(CBotVar* var, CBotVar* result, int& exception, void* user); + static bool rBuildInfo(CBotVar* var, CBotVar* result, int& exception, void* user); + static bool rCanBuild(CBotVar* var, CBotVar* result, int& exception, void* user); static bool rBuild(CBotVar* var, CBotVar* result, int& exception, void* user); static bool rProduce(CBotVar* var, CBotVar* result, int& exception, void* user); static bool rDistance(CBotVar* var, CBotVar* result, int& exception, void* user); -- cgit v1.2.3-1-g7c22 From cde8653e60daab8303aa45507ce0aa2fca0f2708 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 28 Apr 2013 18:01:30 +0200 Subject: Updated data submodule --- data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data b/data index 01900b3..fcb194a 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 01900b30c0bdb51e800971e67e6e863fe4ac2a3c +Subproject commit fcb194a2d35f72658138d237b27f2cdda7cd6b9a -- cgit v1.2.3-1-g7c22 From fc28a8e8406e4b5fd810a09ed3b9524af8d69482 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 28 Apr 2013 18:33:59 +0200 Subject: Implemented EndMissionNever (#181) --- src/object/robotmain.cpp | 8 +++++++- src/object/robotmain.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 6ba09a5..7be1177 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -3830,6 +3830,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) m_audioChangeTotal = 0; m_endTakeTotal = 0; m_endTakeResearch = 0; + m_endTakeNever = false; m_endTakeWinDelay = 2.0f; m_endTakeLostDelay = 2.0f; m_obligatoryTotal = 0; @@ -4703,6 +4704,10 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) { m_endTakeResearch |= OpResearch(line, "type"); } + if (Cmd(line, "EndMissionNever") && !resetObject && m_version >= 2) + { + m_endTakeNever = true; + } if (Cmd(line, "ObligatoryToken") && !resetObject) { @@ -6763,7 +6768,8 @@ Error CRobotMain::CheckEndMission(bool frame) } } if (nb < m_endTake[t].min || - nb > m_endTake[t].max) + nb > m_endTake[t].max || + m_endTakeNever) { m_displayText->SetEnable(true); return ERR_MISSION_NOTERM; diff --git a/src/object/robotmain.h b/src/object/robotmain.h index 7ed84c5..73315af 100644 --- a/src/object/robotmain.h +++ b/src/object/robotmain.h @@ -537,6 +537,7 @@ protected: int m_endTakeTotal; EndTake m_endTake[10]; long m_endTakeResearch; + bool m_endTakeNever; float m_endTakeWinDelay; float m_endTakeLostDelay; -- cgit v1.2.3-1-g7c22 From c7d289c00bc60d366f8c5c1016fb30717a228495 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 28 Apr 2013 20:05:19 +0200 Subject: Beggining of MissionController Controling mission using CBot --- src/object/object.cpp | 6 ++++-- src/object/object.h | 1 + src/object/robotmain.cpp | 33 +++++++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/object/object.cpp b/src/object/object.cpp index b621ef2..ce79e34 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -2145,8 +2145,10 @@ bool CObject::CreateVehicle(Math::Vector pos, float angle, ObjectType type, { m_motion = new CMotionHuman(this); } - else - { + else if ( type == OBJECT_CONTROLLER ) { + m_motion = new CMotion(this); //dummy object + } + else { m_motion = new CMotionVehicle(this); } if ( m_motion == 0 ) return false; diff --git a/src/object/object.h b/src/object/object.h index 4d8cc02..25ea708 100644 --- a/src/object/object.h +++ b/src/object/object.h @@ -156,6 +156,7 @@ enum ObjectType OBJECT_MOBILEsa = 210, //! < submarine OBJECT_MOBILEtg = 211, //! < training target OBJECT_MOBILEdr = 212, //! < robot drawing + OBJECT_CONTROLLER = 213, //! < mission controller OBJECT_WAYPOINT = 250, //! < waypoint OBJECT_FLAGb = 260, //! < blue flag OBJECT_FLAGr = 261, //! < red flag diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 7be1177..ddd6545 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -4351,6 +4351,24 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) m_beginObject = true; } + if (Cmd(line, "MissionController") && read[0] == 0 && m_version == 3) + { + if (!m_beginObject) { + GetLogger()->Error("Syntax error in file '%s' (line %d): MissionController before BeginObject\n", filename, lineNum); + continue; + } + + CObject* obj = CreateObject(Math::Vector(0.0f, 0.0f, 0.0f), 0.0f, 1.0f, 0.0f, OBJECT_CONTROLLER, 100.0f, false, false, 0); + CBrain* brain = obj->GetBrain(); + if (brain != nullptr) + { + OpString(line, "script", name); + if (name[0] != 0) + brain->SetScriptName(1, name); + brain->SetScriptRun(1); + } + } + if (Cmd(line, "CreateObject") && read[0] == 0) { if (!m_beginObject) { @@ -4672,7 +4690,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) m_camera->SetFixDirection(OpFloat(line, "fixDirection", 0.25f)*Math::PI); } - if (Cmd(line, "EndMissionTake") && !resetObject) + if (Cmd(line, "EndMissionTake") && !resetObject && m_version<3) { int i = m_endTakeTotal; if (i < 10) @@ -4695,16 +4713,16 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) m_endTakeTotal ++; } } - if (Cmd(line, "EndMissionDelay") && !resetObject) + if (Cmd(line, "EndMissionDelay") && !resetObject && m_version<3) { m_endTakeWinDelay = OpFloat(line, "win", 2.0f); m_endTakeLostDelay = OpFloat(line, "lost", 2.0f); } - if (Cmd(line, "EndMissionResearch") && !resetObject) + if (Cmd(line, "EndMissionResearch") && !resetObject && m_version<3) { m_endTakeResearch |= OpResearch(line, "type"); } - if (Cmd(line, "EndMissionNever") && !resetObject && m_version >= 2) + if (Cmd(line, "EndMissionNever") && !resetObject && m_version == 2) { m_endTakeNever = true; } @@ -5134,7 +5152,8 @@ CObject* CRobotMain::CreateObject(Math::Vector pos, float angle, float zoom, flo type == OBJECT_MOBILEwt || type == OBJECT_MOBILEit || type == OBJECT_MOBILEdr || - type == OBJECT_APOLLO2 ) + type == OBJECT_APOLLO2 || + type == OBJECT_CONTROLLER ) { object = new CObject(); object->SetOption(option); @@ -6690,6 +6709,8 @@ void CRobotMain::UpdateAudio(bool frame) //! Checks if the mission is over Error CRobotMain::CheckEndMission(bool frame) { + if (m_version >= 3) return ERR_MISSION_NOTERM; //disabled. TODO: Control from program + CInstanceManager* iMan = CInstanceManager::GetInstancePointer(); for (int t = 0; t < m_endTakeTotal; t++) @@ -6769,7 +6790,7 @@ Error CRobotMain::CheckEndMission(bool frame) } if (nb < m_endTake[t].min || nb > m_endTake[t].max || - m_endTakeNever) + m_endTakeNever ) { m_displayText->SetEnable(true); return ERR_MISSION_NOTERM; -- cgit v1.2.3-1-g7c22