Skip to content

Commit

Permalink
Add new qtscript function setDroidLimit(player, value) that replaces the
Browse files Browse the repository at this point in the history
previous hard-coded droid limits.
  • Loading branch information
perim committed Nov 3, 2012
1 parent 91e4e32 commit ee6da7c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 59 deletions.
1 change: 1 addition & 0 deletions data/base/script/rules.js
Expand Up @@ -7,6 +7,7 @@ function eventStartLevel()
// Disable by default
setMiniMap(false);
setDesign(false);
setDroidLimit(0, 100);

var structlist = enumStruct(me, HQ);
for (var i = 0; i < structlist.length; i++)
Expand Down
2 changes: 2 additions & 0 deletions data/mp/multiplay/skirmish/rules.js
Expand Up @@ -13,6 +13,8 @@ function eventGameInit()
hackNetOff();
for (var playnum = 0; playnum < maxPlayers; playnum++)
{
setDroidLimit(playnum, 150);

enableStructure("A0CommandCentre", playnum); // make structures available to build
enableStructure("A0LightFactory", playnum);
enableStructure("A0ResourceExtractor", playnum);
Expand Down
3 changes: 0 additions & 3 deletions src/droid.h
Expand Up @@ -38,9 +38,6 @@

#define DROID_RESISTANCE_FACTOR 30

#define MAX_MP_DROIDS 150
#define MAX_SP_DROIDS 100
#define MAX_SP_AI_DROIDS 999
// Changing this breaks campaign saves!
#define MAX_RECYCLED_DROIDS 450

Expand Down
44 changes: 18 additions & 26 deletions src/hci.cpp
Expand Up @@ -1831,22 +1831,18 @@ INT_RETVAL intRunWidgets(void)
structX2 = world_coord(structX2) + TILE_UNITS / 2;
structY2 = world_coord(structY2) + TILE_UNITS / 2;

if (!IsPlayerStructureLimitReached(selectedPlayer))
// Set the droid order
if (intNumSelectedDroids(DROID_CONSTRUCT) == 0
&& intNumSelectedDroids(DROID_CYBORG_CONSTRUCT) == 0
&& psObjSelected != NULL && isConstructionDroid(psObjSelected))
{
// Set the droid order
if (intNumSelectedDroids(DROID_CONSTRUCT) == 0
&& intNumSelectedDroids(DROID_CYBORG_CONSTRUCT) == 0
&& psObjSelected != NULL && isConstructionDroid(psObjSelected))
{
orderDroidStatsTwoLocDir((DROID *)psObjSelected, DORDER_LINEBUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, structX2, structY2, player.r.y, ModeQueue);
}
else
{
orderSelectedStatsTwoLocDir(selectedPlayer, DORDER_LINEBUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, structX2, structY2, player.r.y, ctrlShiftDown());
}
orderDroidStatsTwoLocDir((DROID *)psObjSelected, DORDER_LINEBUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, structX2, structY2, player.r.y, ModeQueue);
}
else
{
orderSelectedStatsTwoLocDir(selectedPlayer, DORDER_LINEBUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, structX2, structY2, player.r.y, ctrlShiftDown());
}
}

if (!quickQueueMode)
{
// Clear the object screen, only if we aren't immediately building something else
Expand All @@ -1872,22 +1868,18 @@ INT_RETVAL intRunWidgets(void)
AddDerrickBurningMessage();
}
}

if (CanBuild)
{
if (!IsPlayerStructureLimitReached(selectedPlayer))
// Set the droid order
if (intNumSelectedDroids(DROID_CONSTRUCT) == 0
&& intNumSelectedDroids(DROID_CYBORG_CONSTRUCT) == 0
&& psObjSelected != NULL)
{
// Set the droid order
if (intNumSelectedDroids(DROID_CONSTRUCT) == 0
&& intNumSelectedDroids(DROID_CYBORG_CONSTRUCT) == 0
&& psObjSelected != NULL)
{
orderDroidStatsLocDir((DROID *)psObjSelected, DORDER_BUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, player.r.y, ModeQueue);
}
else
{
orderSelectedStatsLocDir(selectedPlayer, DORDER_BUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, player.r.y, ctrlShiftDown());
}
orderDroidStatsLocDir((DROID *)psObjSelected, DORDER_BUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, player.r.y, ModeQueue);
}
else
{
orderSelectedStatsLocDir(selectedPlayer, DORDER_BUILD, (STRUCTURE_STATS *)psPositionStats, structX, structY, player.r.y, ctrlShiftDown());
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/qtscriptfuncs.cpp
Expand Up @@ -1760,12 +1760,7 @@ static QScriptValue js_orderDroidBuild(QScriptContext *context, QScriptEngine *)
{
direction = DEG(context->argument(5).toNumber());
}
// Don't allow scripts to order structure builds if players structure limit has been reached.
if (!IsPlayerStructureLimitReached(psDroid->player))
{
orderDroidStatsLocDir(psDroid, order, psStats, world_coord(x) + TILE_UNITS / 2, world_coord(y) + TILE_UNITS / 2, direction, ModeQueue);
return QScriptValue(false);
}
orderDroidStatsLocDir(psDroid, order, psStats, world_coord(x) + TILE_UNITS / 2, world_coord(y) + TILE_UNITS / 2, direction, ModeQueue);
return QScriptValue(true);
}

Expand Down Expand Up @@ -2658,6 +2653,16 @@ static QScriptValue js_getDroidLimit(QScriptContext *context, QScriptEngine *eng
return QScriptValue(getMaxDroids(engine->globalObject().property("me").toInt32()));
}

//-- \subsection{setDroidLimit(player, value)}
//-- Set the maximum number of droids that this player can produce.
static QScriptValue js_setDroidLimit(QScriptContext *context, QScriptEngine *)
{
int player = context->argument(0).toInt32();
int value = context->argument(1).toInt32();
setMaxDroids(player, value);
return QScriptValue();
}

// ----------------------------------------------------------------------------------------
// Register functions with scripting system

Expand Down Expand Up @@ -2716,6 +2721,7 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("chat", engine->newFunction(js_chat));
engine->globalObject().setProperty("getDroidProduction", engine->newFunction(js_getDroidProduction));
engine->globalObject().setProperty("getDroidLimit", engine->newFunction(js_getDroidLimit));
engine->globalObject().setProperty("setDroidLimit", engine->newFunction(js_setDroidLimit));

// Functions that operate on the current player only
engine->globalObject().setProperty("centreView", engine->newFunction(js_centreView));
Expand Down
6 changes: 0 additions & 6 deletions src/scriptai.cpp
Expand Up @@ -643,11 +643,6 @@ bool scrOrderDroidStatsLoc(void)
ASSERT( false, "Invalid order" );
return false;
}

// Don't allow scripts to order structure builds if players structure
// limit has been reached.
if (!IsPlayerStructureLimitReached(psDroid->player))
{
// HACK: FIXME: Looks like a script error in the player*.slo files
// buildOnExactLocation() which references previously destroyed buildings from
// _stat = rebuildStructStat[_count] causes this.
Expand All @@ -658,7 +653,6 @@ bool scrOrderDroidStatsLoc(void)
}

orderDroidStatsLocDir(psDroid, order, psStats, x, y, 0, ModeQueue);
}

return true;
}
Expand Down
24 changes: 11 additions & 13 deletions src/structure.cpp
Expand Up @@ -174,6 +174,9 @@ static int structureTotalReturn(STRUCTURE *psStruct);
// last time the maximum units message was displayed
static UDWORD lastMaxUnitMessage;

/// max number of units
static int droidLimit[MAX_PLAYERS];

#define MAX_UNIT_MESSAGE_PAUSE 20000

/*
Expand Down Expand Up @@ -286,6 +289,7 @@ void structureInitVars(void)

for (i = 0; i < MAX_PLAYERS; i++)
{
droidLimit[i] = INT16_MAX;
asStructLimits[i] = NULL;
for (j = 0; j < NUM_FLAG_TYPES; j++)
{
Expand Down Expand Up @@ -2545,27 +2549,22 @@ static bool IsFactoryCommanderGroupFull(const FACTORY* psFactory)
// doesn't mean that these numbers can't be exceeded if units are
// put down in the editor or by the scripts.

bool IsPlayerStructureLimitReached(UDWORD PlayerNumber)
void setMaxDroids(int player, int value)
{
// PC currently doesn't limit number of structures a player can build.
return false;
droidLimit[player] = value;
}


UDWORD getMaxDroids(UDWORD PlayerNumber)
int getMaxDroids(int player)
{
return bMultiPlayer? MAX_MP_DROIDS : PlayerNumber == 0? MAX_SP_DROIDS : MAX_SP_AI_DROIDS;
return droidLimit[player];
}


bool IsPlayerDroidLimitReached(UDWORD PlayerNumber)
bool IsPlayerDroidLimitReached(int player)
{
unsigned int numDroids = getNumDroids(PlayerNumber) + getNumMissionDroids(PlayerNumber) + getNumTransporterDroids(PlayerNumber);

return numDroids >= getMaxDroids(PlayerNumber);
unsigned int numDroids = getNumDroids(player) + getNumMissionDroids(player) + getNumTransporterDroids(player);
return numDroids >= getMaxDroids(player);
}


static bool maxDroidsByTypeReached(STRUCTURE *psStructure)
{
FACTORY *psFact = &psStructure->pFunctionality->factory;
Expand All @@ -2588,7 +2587,6 @@ static bool maxDroidsByTypeReached(STRUCTURE *psStructure)
return false;
}


// Check for max number of units reached and halt production.
//
bool CheckHaltOnMaxUnitsReached(STRUCTURE *psStructure)
Expand Down
9 changes: 4 additions & 5 deletions src/structure.h
Expand Up @@ -98,9 +98,10 @@ extern STRUCTSTRENGTH_MODIFIER asStructStrengthModifier[WE_NUMEFFECTS][

extern void handleAbandonedStructures(void);

extern bool IsPlayerDroidLimitReached(UDWORD PlayerNumber);
extern bool IsPlayerStructureLimitReached(UDWORD PlayerNumber);
extern bool CheckHaltOnMaxUnitsReached(STRUCTURE *psStructure);
int getMaxDroids(int player);
void setMaxDroids(int player, int value);
bool IsPlayerDroidLimitReached(int player);
bool CheckHaltOnMaxUnitsReached(STRUCTURE *psStructure);

extern bool loadStructureStats(const char *pStructData, UDWORD bufferSize);
extern bool loadStructureWeapons(const char *pWeaponData, UDWORD bufferSize);
Expand Down Expand Up @@ -393,8 +394,6 @@ extern bool checkStructureStats(void);
/*returns the power cost to build this structure*/
extern UDWORD structPowerToBuild(const STRUCTURE* psStruct);

extern UDWORD getMaxDroids(UDWORD PlayerNumber);

// check whether a factory of a certain number and type exists
extern bool checkFactoryExists(UDWORD player, UDWORD factoryType, UDWORD inc);

Expand Down

0 comments on commit ee6da7c

Please sign in to comment.