Skip to content

Commit

Permalink
fix #6034
Browse files Browse the repository at this point in the history
ZK's habit of shoving over 9000 parameters into custom
terraform commands is *of course* perfectly reasonable
  • Loading branch information
rt committed Aug 16, 2018
1 parent 2db3144 commit 4a410ab
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 118 deletions.
30 changes: 15 additions & 15 deletions rts/ExternalAI/Interface/AISCommands.h
Expand Up @@ -600,7 +600,7 @@ struct SBuildUnitCommand {
int timeOut;

int toBuildUnitDefId;
float* buildPos_posF3;
const float* buildPos_posF3;
/// set it to UNIT_COMMAND_BUILD_NO_FACING, if you do not want to specify a certain facing
int facing;
}; //$ COMMAND_UNIT_BUILD Unit_build REF:toBuildUnitDefId->UnitDef
Expand Down Expand Up @@ -760,7 +760,7 @@ struct SMoveUnitCommand {
*/
int timeOut;

float* toPos_posF3;
const float* toPos_posF3;
}; //$ COMMAND_UNIT_MOVE Unit_moveTo

struct SPatrolUnitCommand {
Expand All @@ -778,7 +778,7 @@ struct SPatrolUnitCommand {
*/
int timeOut;

float* toPos_posF3;
const float* toPos_posF3;
}; //$ COMMAND_UNIT_PATROL Unit_patrolTo

struct SFightUnitCommand {
Expand All @@ -796,7 +796,7 @@ struct SFightUnitCommand {
*/
int timeOut;

float* toPos_posF3;
const float* toPos_posF3;
}; //$ COMMAND_UNIT_FIGHT Unit_fight

struct SAttackUnitCommand {
Expand Down Expand Up @@ -834,7 +834,7 @@ struct SAttackAreaUnitCommand {
*/
int timeOut;

float* toAttackPos_posF3;
const float* toAttackPos_posF3;
float radius;
}; //$ COMMAND_UNIT_ATTACK_AREA Unit_attackArea

Expand Down Expand Up @@ -994,7 +994,7 @@ struct SSetBaseUnitCommand {
*/
int timeOut;

float* basePos_posF3;
const float* basePos_posF3;
}; //$ COMMAND_UNIT_SET_BASE Unit_setBase

//struct SInternalUnitCommand {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ struct SLoadUnitsAreaUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
float radius;
}; //$ COMMAND_UNIT_LOAD_UNITS_AREA Unit_loadUnitsInArea

Expand Down Expand Up @@ -1102,7 +1102,7 @@ struct SUnloadUnitCommand {
*/
int timeOut;

float* toPos_posF3;
const float* toPos_posF3;
int toUnloadUnitId;
}; //$ COMMAND_UNIT_UNLOAD_UNIT Unit_unload REF:toUnloadUnitId->Unit

Expand All @@ -1121,7 +1121,7 @@ struct SUnloadUnitsAreaUnitCommand {
*/
int timeOut;

float* toPos_posF3;
const float* toPos_posF3;
float radius;
}; //$ COMMAND_UNIT_UNLOAD_UNITS_AREA Unit_unloadUnitsInArea

Expand Down Expand Up @@ -1194,7 +1194,7 @@ struct SReclaimAreaUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
float radius;
}; //$ COMMAND_UNIT_RECLAIM_AREA Unit_reclaimInArea

Expand Down Expand Up @@ -1265,7 +1265,7 @@ struct SDGunPosUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
}; //$ COMMAND_UNIT_D_GUN_POS Unit_dGunPosition

struct SRestoreAreaUnitCommand {
Expand All @@ -1283,7 +1283,7 @@ struct SRestoreAreaUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
float radius;
}; //$ COMMAND_UNIT_RESTORE_AREA Unit_restoreArea

Expand Down Expand Up @@ -1358,7 +1358,7 @@ struct SResurrectAreaUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
float radius;
}; //$ COMMAND_UNIT_RESURRECT_AREA Unit_resurrectInArea

Expand Down Expand Up @@ -1395,7 +1395,7 @@ struct SCaptureAreaUnitCommand {
*/
int timeOut;

float* pos_posF3;
const float* pos_posF3;
float radius;
}; //$ COMMAND_UNIT_CAPTURE_AREA Unit_captureInArea

Expand Down Expand Up @@ -1469,7 +1469,7 @@ struct SCustomUnitCommand {
int timeOut;

int cmdId;
float* params;
const float* params;
int params_size;
}; //$ COMMAND_UNIT_CUSTOM Unit_executeCustomCommand ARRAY:params

Expand Down
67 changes: 67 additions & 0 deletions rts/Sim/Units/CommandAI/Command.cpp
@@ -1,6 +1,9 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "Command.h"
#include "CommandParamsPool.hpp"

CommandParamsPool cmdParamsPool;

CR_BIND(Command, )
CR_REG_METADATA(Command, (
Expand All @@ -14,3 +17,67 @@ CR_REG_METADATA(Command, (
CR_MEMBER(params)
))


Command::~Command() {
if (!IsPooledCommand())
return;

cmdParamsPool.ReleasePage(pageIndex);
}


const float* Command::GetParams(unsigned int idx) const {
if (idx >= numParams)
return nullptr;

if (IsPooledCommand()) {
assert(numParams > MAX_COMMAND_PARAMS);
return (cmdParamsPool.GetPtr(pageIndex, idx));
}

return ((idx < MAX_COMMAND_PARAMS)? &params[idx]: nullptr);
}

float Command::GetParam(unsigned int idx) const {
const float* ptr = GetParams(idx);

if (ptr != nullptr)
return *ptr;

return 0.0f;
}


bool Command::SetParam(unsigned int idx, float param) {
float* ptr = const_cast<float*>(GetParams(idx));

if (ptr != nullptr)
return (*ptr = param, true);

return false;
}

bool Command::PushParam(float param) {
if (numParams < MAX_COMMAND_PARAMS) {
// no need to make this a pooled command just yet
params[numParams++] = param;
return true;
}

if (!IsPooledCommand()) {
// not in the pool, reserve an entry and fill it
pageIndex = cmdParamsPool.AcquirePage();

for (unsigned int i = 0; i < numParams; i++) {
cmdParamsPool.Push(pageIndex, params[i]);
}

memset(&params[0], 0, sizeof(params));
assert(IsPooledCommand());
}

// add new parameter
numParams = cmdParamsPool.Push(pageIndex, param);
return true;
}

0 comments on commit 4a410ab

Please sign in to comment.