Merge pull request #1458 from lolbot-iichan/produce_teams

Add optional team parameter to produce() function
fix-squashed-planets
tomangelo 2022-02-07 16:34:19 +01:00 committed by GitHub
commit 6d8a52eb15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 28 deletions

View File

@ -1543,7 +1543,7 @@ bool CScriptFunctions::rDeflag(CBotVar* var, CBotVar* result, int& exception, vo
return WaitForForegroundTask(script, result, exception); 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[, team]]])"
// or "produce(type[, power])". // or "produce(type[, power])".
CBotTypResult CScriptFunctions::cProduce(CBotVar* &var, void* user) CBotTypResult CScriptFunctions::cProduce(CBotVar* &var, void* user)
@ -1583,6 +1583,12 @@ CBotTypResult CScriptFunctions::cProduce(CBotVar* &var, void* user)
{ {
if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum); if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
var = var->GetNext(); var = var->GetNext();
if ( var != nullptr )
{
if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
var = var->GetNext();
}
} }
} }
} }
@ -1592,7 +1598,7 @@ CBotTypResult CScriptFunctions::cProduce(CBotVar* &var, void* user)
return CBotTypResult(CBotTypFloat); return CBotTypResult(CBotTypFloat);
} }
// Instruction "produce(pos, angle, type[, scriptName[, power]])" // Instruction "produce(pos, angle, type[, scriptName[, power[, team]]])"
// or "produce(type[, power])". // or "produce(type[, power])".
bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, void* user) bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, void* user)
@ -1600,34 +1606,36 @@ bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, v
CScript* script = static_cast<CScript*>(user); CScript* script = static_cast<CScript*>(user);
CObject* me = script->m_object; CObject* me = script->m_object;
std::string name = ""; std::string name = "";
Math::Vector pos;
float angle = 0.0f; ObjectCreateParams params;
ObjectType type = OBJECT_NULL; params.angle = 0.0f;
float power = 0.0f; params.type = OBJECT_NULL;
params.power = 0.0f;
params.team = 0;
if ( var->GetType() <= CBotTypDouble ) if ( var->GetType() <= CBotTypDouble )
{ {
type = static_cast<ObjectType>(var->GetValInt()); params.type = static_cast<ObjectType>(var->GetValInt());
var = var->GetNext(); var = var->GetNext();
pos = me->GetPosition(); params.pos = me->GetPosition();
Math::Vector rotation = me->GetRotation() + me->GetTilt(); Math::Vector rotation = me->GetRotation() + me->GetTilt();
angle = rotation.y; params.angle = rotation.y;
if ( var != nullptr ) if ( var != nullptr )
power = var->GetValFloat(); params.power = var->GetValFloat();
else else
power = -1.0f; params.power = -1.0f;
} }
else else
{ {
if ( !GetPoint(var, exception, pos) ) return true; if ( !GetPoint(var, exception, params.pos) ) return true;
angle = var->GetValFloat()*Math::PI/180.0f; params.angle = var->GetValFloat()*Math::PI/180.0f;
var = var->GetNext(); var = var->GetNext();
type = static_cast<ObjectType>(var->GetValInt()); params.type = static_cast<ObjectType>(var->GetValInt());
var = var->GetNext(); var = var->GetNext();
if ( var != nullptr ) if ( var != nullptr )
@ -1636,28 +1644,34 @@ bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, v
var = var->GetNext(); var = var->GetNext();
if ( var != nullptr ) if ( var != nullptr )
{ {
power = var->GetValFloat(); params.power = var->GetValFloat();
var = var->GetNext();
if ( var != nullptr )
{
params.team = var->GetValInt();
}
} }
else else
{ {
power = -1.0f; params.power = -1.0f;
} }
} }
else else
{ {
power = -1.0f; params.power = -1.0f;
} }
} }
CObject* object = nullptr; CObject* object = nullptr;
if ( type == OBJECT_ANT || if ( params.type == OBJECT_ANT ||
type == OBJECT_SPIDER || params.type == OBJECT_SPIDER ||
type == OBJECT_BEE || params.type == OBJECT_BEE ||
type == OBJECT_WORM ) params.type == OBJECT_WORM )
{ {
object = CObjectManager::GetInstancePointer()->CreateObject(pos, angle, type); object = CObjectManager::GetInstancePointer()->CreateObject(params);
CObjectManager::GetInstancePointer()->CreateObject(pos, angle, OBJECT_EGG); params.type = OBJECT_EGG;
CObjectManager::GetInstancePointer()->CreateObject(params);
if (object->Implements(ObjectInterfaceType::Programmable)) if (object->Implements(ObjectInterfaceType::Programmable))
{ {
dynamic_cast<CProgrammableObject&>(*object).SetActivity(false); dynamic_cast<CProgrammableObject&>(*object).SetActivity(false);
@ -1665,21 +1679,21 @@ bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, v
} }
else else
{ {
if ((type == OBJECT_POWER || type == OBJECT_ATOMIC) && power == -1.0f) if ((params.type == OBJECT_POWER || params.type == OBJECT_ATOMIC) && params.power == -1.0f)
{ {
power = 1.0f; params.power = 1.0f;
} }
bool exists = IsValidObjectTypeId(type) && type != OBJECT_NULL && type != OBJECT_MAX && type != OBJECT_MOBILEpr; bool exists = IsValidObjectTypeId(params.type) && params.type != OBJECT_NULL && params.type != OBJECT_MAX && params.type != OBJECT_MOBILEpr;
if (exists) if (exists)
{ {
object = CObjectManager::GetInstancePointer()->CreateObject(pos, angle, type, power); object = CObjectManager::GetInstancePointer()->CreateObject(params);
} }
if (object == nullptr) if (object == nullptr)
{ {
result->SetValInt(1); // error result->SetValInt(1); // error
return true; return true;
} }
if (type == OBJECT_MOBILEdr) if (params.type == OBJECT_MOBILEdr)
{ {
assert(object->Implements(ObjectInterfaceType::Old)); // TODO: temporary hack assert(object->Implements(ObjectInterfaceType::Old)); // TODO: temporary hack
dynamic_cast<COldObject&>(*object).SetManual(true); dynamic_cast<COldObject&>(*object).SetManual(true);