Add flag() and deflag() CBOT functions

pyro-refactor
Fiftytwo 2020-08-15 03:31:17 +02:00
parent 56c99e741e
commit 4c1d3eecd1
5 changed files with 140 additions and 30 deletions

View File

@ -27,21 +27,22 @@ std::string TraceColorName(TraceColor color)
{ {
switch(color) switch(color)
{ {
case TraceColor::Blue: return "Blue";
case TraceColor::Red: return "Red";
case TraceColor::Green: return "Green";
case TraceColor::Yellow: return "Yellow";
case TraceColor::Violet: return "Violet";
case TraceColor::White: return "White"; case TraceColor::White: return "White";
case TraceColor::Black: return "Black"; case TraceColor::Black: return "Black";
case TraceColor::Gray: return "Gray"; case TraceColor::Gray: return "Gray";
case TraceColor::LightGray: return "LightGray"; case TraceColor::LightGray: return "LightGray";
case TraceColor::Red: return "Red";
case TraceColor::Pink: return "Pink"; case TraceColor::Pink: return "Pink";
case TraceColor::Purple: return "Purple"; case TraceColor::Purple: return "Purple";
case TraceColor::Orange: return "Orange"; case TraceColor::Orange: return "Orange";
case TraceColor::Yellow: return "Yellow";
case TraceColor::Beige: return "Beige"; case TraceColor::Beige: return "Beige";
case TraceColor::Brown: return "Brown"; case TraceColor::Brown: return "Brown";
case TraceColor::Skin: return "Skin"; case TraceColor::Skin: return "Skin";
case TraceColor::Green: return "Green";
case TraceColor::LightGreen: return "LightGreen"; case TraceColor::LightGreen: return "LightGreen";
case TraceColor::Blue: return "Blue";
case TraceColor::LightBlue: return "LightBlue"; case TraceColor::LightBlue: return "LightBlue";
case TraceColor::RedArrow: return "RedArrow"; case TraceColor::RedArrow: return "RedArrow";
case TraceColor::BlackArrow: return "BlackArrow"; case TraceColor::BlackArrow: return "BlackArrow";
@ -55,21 +56,22 @@ Gfx::Color TraceColorColor(TraceColor color)
{ {
switch(color) switch(color)
{ {
case TraceColor::Blue: return Gfx::Color(0.000f, 0.000f, 0.753f, 1.0f);
case TraceColor::Red: return Gfx::Color(1.000f, 0.000f, 0.000f, 1.0f);
case TraceColor::Green: return Gfx::Color(0.000f, 0.627f, 0.000f, 1.0f);
case TraceColor::Yellow: return Gfx::Color(1.000f, 1.000f, 0.000f, 1.0f);
case TraceColor::Violet: return Gfx::Color(0.820f, 0.000f, 0.997f, 1.0f);
case TraceColor::White: return Gfx::Color(1.000f, 1.000f, 1.000f, 1.0f); case TraceColor::White: return Gfx::Color(1.000f, 1.000f, 1.000f, 1.0f);
case TraceColor::Black: return Gfx::Color(0.000f, 0.000f, 0.000f, 1.0f); case TraceColor::Black: return Gfx::Color(0.000f, 0.000f, 0.000f, 1.0f);
case TraceColor::Gray: return Gfx::Color(0.549f, 0.549f, 0.549f, 1.0f); case TraceColor::Gray: return Gfx::Color(0.549f, 0.549f, 0.549f, 1.0f);
case TraceColor::LightGray: return Gfx::Color(0.753f, 0.753f, 0.753f, 1.0f); case TraceColor::LightGray: return Gfx::Color(0.753f, 0.753f, 0.753f, 1.0f);
case TraceColor::Red: return Gfx::Color(1.000f, 0.000f, 0.000f, 1.0f);
case TraceColor::Pink: return Gfx::Color(1.000f, 0.627f, 0.753f, 1.0f); case TraceColor::Pink: return Gfx::Color(1.000f, 0.627f, 0.753f, 1.0f);
case TraceColor::Purple: return Gfx::Color(0.878f, 0.000f, 0.753f, 1.0f); case TraceColor::Purple: return Gfx::Color(0.878f, 0.000f, 0.753f, 1.0f);
case TraceColor::Orange: return Gfx::Color(1.000f, 0.627f, 0.000f, 1.0f); case TraceColor::Orange: return Gfx::Color(1.000f, 0.627f, 0.000f, 1.0f);
case TraceColor::Yellow: return Gfx::Color(1.000f, 1.000f, 0.000f, 1.0f);
case TraceColor::Beige: return Gfx::Color(0.878f, 0.753f, 0.000f, 1.0f); case TraceColor::Beige: return Gfx::Color(0.878f, 0.753f, 0.000f, 1.0f);
case TraceColor::Brown: return Gfx::Color(0.627f, 0.361f, 0.000f, 1.0f); case TraceColor::Brown: return Gfx::Color(0.627f, 0.361f, 0.000f, 1.0f);
case TraceColor::Skin: return Gfx::Color(0.961f, 0.839f, 0.714f, 1.0f); case TraceColor::Skin: return Gfx::Color(0.961f, 0.839f, 0.714f, 1.0f);
case TraceColor::Green: return Gfx::Color(0.000f, 0.627f, 0.000f, 1.0f);
case TraceColor::LightGreen: return Gfx::Color(0.000f, 1.000f, 0.000f, 1.0f); case TraceColor::LightGreen: return Gfx::Color(0.000f, 1.000f, 0.000f, 1.0f);
case TraceColor::Blue: return Gfx::Color(0.000f, 0.000f, 0.753f, 1.0f);
case TraceColor::LightBlue: return Gfx::Color(0.000f, 0.871f, 1.000f, 1.0f); case TraceColor::LightBlue: return Gfx::Color(0.000f, 0.871f, 1.000f, 1.0f);
case TraceColor::BlackArrow: return TraceColorColor(TraceColor::Black); case TraceColor::BlackArrow: return TraceColorColor(TraceColor::Black);
case TraceColor::RedArrow: return TraceColorColor(TraceColor::Red); //TODO: We could probably have all the colors available as arrows now case TraceColor::RedArrow: return TraceColorColor(TraceColor::Red); //TODO: We could probably have all the colors available as arrows now

View File

@ -32,24 +32,25 @@ enum class TraceColor
{ {
Default = -1, Default = -1,
White = 0, Blue = 0,
Black = 1, Red = 1,
Gray = 2, Green = 2,
LightGray = 3, Yellow = 3,
Red = 4, Violet = 4,
Pink = 5, White = 5,
Purple = 6, Black = 6,
Orange = 7, Gray = 7,
Yellow = 8, LightGray = 8,
Beige = 9, Pink = 9,
Brown = 10, Purple = 10,
Skin = 11, Orange = 11,
Green = 12, Beige = 12,
LightGreen = 13, Brown = 13,
Blue = 14, Skin = 14,
LightBlue = 15, LightGreen = 15,
BlackArrow = 16, LightBlue = 16,
RedArrow = 17, BlackArrow = 17,
RedArrow = 18,
Max, Max,
}; };
//! Convert TraceColor to a std::string //! Convert TraceColor to a std::string

View File

@ -322,6 +322,8 @@ std::string GetHelpFilename(const char *token)
if ( strcmp(token, "researched" ) == 0 ) helpfile = "cbot/researched"; if ( strcmp(token, "researched" ) == 0 ) helpfile = "cbot/researched";
if ( strcmp(token, "buildingenabled") == 0 ) helpfile = "cbot/buildingenabled"; if ( strcmp(token, "buildingenabled") == 0 ) helpfile = "cbot/buildingenabled";
if ( strcmp(token, "build" ) == 0 ) helpfile = "cbot/build"; if ( strcmp(token, "build" ) == 0 ) helpfile = "cbot/build";
if ( strcmp(token, "flag" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "deflag" ) == 0 ) helpfile = "cbot/deflag";
if ( strcmp(token, "wait" ) == 0 ) helpfile = "cbot/wait"; if ( strcmp(token, "wait" ) == 0 ) helpfile = "cbot/wait";
if ( strcmp(token, "move" ) == 0 ) helpfile = "cbot/move"; if ( strcmp(token, "move" ) == 0 ) helpfile = "cbot/move";
if ( strcmp(token, "turn" ) == 0 ) helpfile = "cbot/turn"; if ( strcmp(token, "turn" ) == 0 ) helpfile = "cbot/turn";
@ -344,23 +346,24 @@ std::string GetHelpFilename(const char *token)
if ( strcmp(token, "topo" ) == 0 ) helpfile = "cbot/topo"; if ( strcmp(token, "topo" ) == 0 ) helpfile = "cbot/topo";
if ( strcmp(token, "message" ) == 0 ) helpfile = "cbot/message"; if ( strcmp(token, "message" ) == 0 ) helpfile = "cbot/message";
if ( strcmp(token, "abstime" ) == 0 ) helpfile = "cbot/abstime"; if ( strcmp(token, "abstime" ) == 0 ) helpfile = "cbot/abstime";
if ( strcmp(token, "Blue" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "Red" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "Green" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "Yellow" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "Violet" ) == 0 ) helpfile = "cbot/flag";
if ( strcmp(token, "BlackArrow" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "BlackArrow" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "RedArrow" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "RedArrow" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "White" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "White" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Black" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Black" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Gray" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Gray" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "LightGray" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "LightGray" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Red" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Pink" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Pink" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Purple" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Purple" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Orange" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Orange" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Yellow" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Beige" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Beige" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Brown" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Brown" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Skin" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "Skin" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Green" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "LightGreen" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "LightGreen" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "Blue" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "LightBlue" ) == 0 ) helpfile = "cbot/pendown"; if ( strcmp(token, "LightBlue" ) == 0 ) helpfile = "cbot/pendown";
if ( strcmp(token, "InFront" ) == 0 ) helpfile = "cbot/grab"; if ( strcmp(token, "InFront" ) == 0 ) helpfile = "cbot/grab";
if ( strcmp(token, "Behind" ) == 0 ) helpfile = "cbot/grab"; if ( strcmp(token, "Behind" ) == 0 ) helpfile = "cbot/grab";
@ -482,6 +485,8 @@ bool IsFunction(const char *token)
if ( strcmp(token, "researched" ) == 0 ) return true; if ( strcmp(token, "researched" ) == 0 ) return true;
if ( strcmp(token, "buildingenabled") == 0 ) return true; if ( strcmp(token, "buildingenabled") == 0 ) return true;
if ( strcmp(token, "build" ) == 0 ) return true; if ( strcmp(token, "build" ) == 0 ) return true;
if ( strcmp(token, "flag" ) == 0 ) return true;
if ( strcmp(token, "deflag" ) == 0 ) return true;
if ( strcmp(token, "wait" ) == 0 ) return true; if ( strcmp(token, "wait" ) == 0 ) return true;
if ( strcmp(token, "move" ) == 0 ) return true; if ( strcmp(token, "move" ) == 0 ) return true;
if ( strcmp(token, "turn" ) == 0 ) return true; if ( strcmp(token, "turn" ) == 0 ) return true;
@ -588,6 +593,8 @@ const char* GetHelpText(const char *token)
if ( strcmp(token, "researched" ) == 0 ) return "researched ( research );"; if ( strcmp(token, "researched" ) == 0 ) return "researched ( research );";
if ( strcmp(token, "buildingenabled") == 0 ) return "buildingenabled ( category );"; if ( strcmp(token, "buildingenabled") == 0 ) return "buildingenabled ( category );";
if ( strcmp(token, "build" ) == 0 ) return "build ( category );"; if ( strcmp(token, "build" ) == 0 ) return "build ( category );";
if ( strcmp(token, "flag" ) == 0 ) return "flag ( color );";
if ( strcmp(token, "deflag" ) == 0 ) return "deflag ( );";
if ( strcmp(token, "wait" ) == 0 ) return "wait ( time );"; if ( strcmp(token, "wait" ) == 0 ) return "wait ( time );";
if ( strcmp(token, "move" ) == 0 ) return "move ( distance );"; if ( strcmp(token, "move" ) == 0 ) return "move ( distance );";
if ( strcmp(token, "turn" ) == 0 ) return "turn ( angle );"; if ( strcmp(token, "turn" ) == 0 ) return "turn ( angle );";

View File

@ -1439,6 +1439,102 @@ bool CScriptFunctions::rBuild(CBotVar* var, CBotVar* result, int& exception, voi
} }
// Instruction "flag(color)"
bool CScriptFunctions::rFlag(CBotVar* var, CBotVar* result, int& exception, void* user)
{
CScript* script = static_cast<CScript*>(user);
CObject* pThis = script->m_object;
ObjectType oType;
int color;
Error err;
exception = 0;
if ( !script->m_taskExecutor->IsForegroundTask() )
{
oType = pThis->GetType();
if ( oType != OBJECT_MOBILEfs && // allowed only for sniffer bots && humans
oType != OBJECT_MOBILEts &&
oType != OBJECT_MOBILEws &&
oType != OBJECT_MOBILEis &&
oType != OBJECT_HUMAN &&
oType != OBJECT_TECH )
{
err = ERR_WRONG_BOT; // Wrong object
}
else
{
if ( var == nullptr )
{
color = 0;
}
else
{
color = var->GetValInt();
if ( color < 0 || color > static_cast<int>(TraceColor::Violet) ) color = 0;
}
err = script->m_taskExecutor->StartTaskFlag(TFL_CREATE, color);
}
if ( err != ERR_OK )
{
script->m_taskExecutor->StopForegroundTask();
result->SetValInt(err); // shows the error
if ( script->m_errMode == ERM_STOP )
{
exception = err;
return false;
}
return true;
}
}
return WaitForForegroundTask(script, result, exception);
}
// Instruction "deflag()"
bool CScriptFunctions::rDeflag(CBotVar* var, CBotVar* result, int& exception, void* user)
{
CScript* script = static_cast<CScript*>(user);
CObject* pThis = script->m_object;
ObjectType oType;
Error err;
exception = 0;
if ( !script->m_taskExecutor->IsForegroundTask() )
{
oType = pThis->GetType();
if ( oType != OBJECT_MOBILEfs && // allowed only for sniffer bots && humans
oType != OBJECT_MOBILEts &&
oType != OBJECT_MOBILEws &&
oType != OBJECT_MOBILEis &&
oType != OBJECT_HUMAN &&
oType != OBJECT_TECH )
{
err = ERR_WRONG_BOT; // Wrong object
}
else
{
err = script->m_taskExecutor->StartTaskFlag(TFL_DELETE, 0);
}
if ( err != ERR_OK )
{
script->m_taskExecutor->StopForegroundTask();
result->SetValInt(err); // shows the error
if ( script->m_errMode == ERM_STOP )
{
exception = err;
return false;
}
return true;
}
}
return WaitForForegroundTask(script, result, exception);
}
// Compilation of the instruction "produce(pos, angle, type[, scriptName[, power]])" // Compilation of the instruction "produce(pos, angle, type[, scriptName[, power]])"
// or "produce(type[, power])". // or "produce(type[, power])".
@ -3437,6 +3533,8 @@ void CScriptFunctions::Init()
CBotProgram::AddFunction("buildingenabled", rBuildingEnabled, cOneIntReturnBool); CBotProgram::AddFunction("buildingenabled", rBuildingEnabled, cOneIntReturnBool);
CBotProgram::AddFunction("build", rBuild, cOneInt); CBotProgram::AddFunction("build", rBuild, cOneInt);
CBotProgram::AddFunction("flag", rFlag, cGrabDrop);
CBotProgram::AddFunction("deflag", rDeflag, cNull);
CBotProgram::AddFunction("retobject", rGetObject, cGetObject); CBotProgram::AddFunction("retobject", rGetObject, cGetObject);
CBotProgram::AddFunction("retobjectbyid", rGetObjectById, cGetObject); CBotProgram::AddFunction("retobjectbyid", rGetObjectById, cGetObject);

View File

@ -110,6 +110,8 @@ private:
static bool rResearched(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rResearched(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rBuildingEnabled(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rBuildingEnabled(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rBuild(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rBuild(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rFlag(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rDeflag(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rProduce(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rProduce(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rDistance(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rDistance(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);
static bool rDistance2d(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user); static bool rDistance2d(CBot::CBotVar* var, CBot::CBotVar* result, int& exception, void* user);