summaryrefslogtreecommitdiffstats
path: root/src/script
diff options
context:
space:
mode:
authorXienDev <XienDev@gmail.com>2013-04-28 16:49:48 +0300
committerXienDev <XienDev@gmail.com>2013-04-28 16:49:48 +0300
commit6a4ac9ce1614052b25fea0a0c0572d0ee1afbdff (patch)
tree97572b8133f6bafcfdf9337268304bb0b17f1e97 /src/script
parent0ce02b21f40ed8a0086dffe2c4bb01eeff942e58 (diff)
downloadcolobot-6a4ac9ce1614052b25fea0a0c0572d0ee1afbdff.tar.gz
colobot-6a4ac9ce1614052b25fea0a0c0572d0ee1afbdff.tar.bz2
colobot-6a4ac9ce1614052b25fea0a0c0572d0ee1afbdff.zip
Added functions "canbuild(category)" and "buildinfo(category)"
Also fixed issue with undefined behaviour of build(category) function
Diffstat (limited to 'src/script')
-rw-r--r--src/script/cbottoken.cpp10
-rw-r--r--src/script/script.cpp135
-rw-r--r--src/script/script.h5
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<Error>(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<ObjectType>(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<ObjectType>(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<Error>(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<ObjectType>(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);