Permalink
Browse files

qtscript: Rename getStructureCount to countStruct. Add countDroid(pla…

…yer) as well.

Backport the ALL_PLAYERS, ENEMIES and ALLIES defines from master.
  • Loading branch information...
1 parent 6b4dfcc commit d0c8eb7cd1f7fbce25d83107c772a14ca1a0956e @perim perim committed Nov 3, 2012
Showing with 61 additions and 17 deletions.
  1. +1 −1 ChangeLog
  2. +10 −11 data/mp/multiplay/skirmish/semperfi.js
  3. +50 −5 src/qtscriptfuncs.cpp
View
@@ -4,7 +4,7 @@
* Multiplayer:
* Change: AI droids shoot at blocking features again, and can move through each other when jammed (commit:a19e41c15d78106c7128e63662e8824454b8900c)
* Scripting:
- * New: Functions getStructureLimit() and getStructureCount() (commit:af757d28bd082384355272f890fe6d79fd5369a8)
+ * New: getStructureLimit(), countDroid() and countStruct() (commit:af757d28bd082384355272f890fe6d79fd5369a8)
* New: orderDroidBuild() now takes an optional direction parameter for rotation (commit:24414caa0c6a8db7459e7204a1c12546d27f7441)
2012-10-08: Version 3.1 rc3
@@ -245,7 +245,6 @@ function sortByDistToBase(obj1, obj2)
function buildFundamentals()
{
var needPwGen = false;
- var factlist = enumStruct(me, factory);
var droids = enumDroid(me, DROID_CONSTRUCT);
// Do we need power generators?
@@ -304,39 +303,39 @@ function buildFundamentals()
function buildFundamentals2()
{
// Need factories? FIXME, check real limits
- var factlist = enumStruct(me, factory);
- var reslist = enumStruct(me, resLab);
- var hqlist = enumStruct(me, playerHQ);
+ var factcount = countStruct(factory);
+ var rescount = countStruct(resLab);
+ var hqcount = countStruct(playerHQ);
// Build as many research labs as factories
- if (reslist.length < factlist.length && grabTrucksAndBuild(20, resLab, 1))
+ if (rescount < factcount && grabTrucksAndBuild(20, resLab, 1))
{
return;
}
// Build as many factories as we can afford
- if ((factlist.length < 2 || (factlist.length < 4 && playerPower(me) > factlist.length * 1000))
+ if ((factcount < 2 || (factcount < 4 && playerPower(me) > factcount * 1000))
&& grabTrucksAndBuild(20, factory, 1))
{
return; // done here
}
// Build HQ if missing
- if (hqlist.length == 0 && grabTrucksAndBuild(20, playerHQ, 1))
+ if (hqcount == 0 && grabTrucksAndBuild(20, playerHQ, 1))
{
return;
}
// Build cyborg factory if we don't have one
if (isStructureAvailable(cybFactory, me))
{
- var cyblist = enumStruct(me, cybFactory);
- if (cyblist.length == 0 && playerPower(me) > 250 && grabTrucksAndBuild(20, cybFactory, 1))
+ var cybcount = countStruct(cybFactory);
+ if (cybcount == 0 && playerPower(me) > 250 && grabTrucksAndBuild(20, cybFactory, 1))
{
return;
}
}
// Build VTOL factory if we don't have one
if (isStructureAvailable(me, vtolFactory))
{
- var vfaclist = enumStruct(me, vtolFactory);
- if (vfaclist.length == 0 && playerPower(me) > 500 && grabTrucksAndBuild(20, vtolFactory, 1))
+ var vfaccount = countStruct(vtolFactory);
+ if (vfaccount == 0 && playerPower(me) > 500 && grabTrucksAndBuild(20, vtolFactory, 1))
{
return;
}
View
@@ -53,6 +53,9 @@
#include "scriptextern.h"
#define FAKE_REF_LASSAT 999
+#define ALL_PLAYERS -1
+#define ALLIES -2
+#define ENEMIES -3
// hack, this is used from scriptfuncs.cpp -- and we don't want to include any stinkin' wzscript headers here!
// TODO, move this stuff into a script common subsystem
@@ -1964,13 +1967,15 @@ static QScriptValue js_getStructureLimit(QScriptContext *context, QScriptEngine
return QScriptValue(asStructLimits[player][index].limit);
}
-//-- \subsection{getStructureCount(structure type[, player])}
-//-- Create a structure on the given position. Returns true on success.
-static QScriptValue js_getStructureCount(QScriptContext *context, QScriptEngine *engine)
+//-- \subsection{countStruct(structure type[, player])}
+//-- Count the number of structures of a given type.
+//-- The player parameter can be a specific player, ALL_PLAYERS, ALLIES or ENEMIES.
+static QScriptValue js_countStruct(QScriptContext *context, QScriptEngine *engine)
{
QString building = context->argument(0).toString();
int index = getStructStatFromName(building.toUtf8().constData());
int player;
+ int quantity = 0;
if (context->argumentCount() > 1)
{
player = context->argument(1).toInt32();
@@ -1980,7 +1985,43 @@ static QScriptValue js_getStructureCount(QScriptContext *context, QScriptEngine
player = engine->globalObject().property("me").toInt32();
}
SCRIPT_ASSERT(context, index < numStructureStats && index >= 0, "Structure %s not found", building.toUtf8().constData());
- return QScriptValue(asStructLimits[player][index].currentQuantity);
+ for (int i = 0; i < MAX_PLAYERS; i++)
+ {
+ if (player == i || player == ALL_PLAYERS
+ || (player == ALLIES && aiCheckAlliances(i, player))
+ || (player == ENEMIES && !aiCheckAlliances(i, player)))
+ {
+ quantity += asStructLimits[i][index].currentQuantity;
+ }
+ }
+ return QScriptValue(quantity);
+}
+
+//-- \subsection{countDroid([player])}
+//-- Count the number of droids that a given player has.
+//-- The player parameter can be a specific player, ALL_PLAYERS, ALLIES or ENEMIES.
+static QScriptValue js_countDroid(QScriptContext *context, QScriptEngine *engine)
+{
+ int player;
+ int quantity = 0;
+ if (context->argumentCount() > 1)
+ {
+ player = context->argument(1).toInt32();
+ }
+ else
+ {
+ player = engine->globalObject().property("me").toInt32();
+ }
+ for (int i = 0; i < MAX_PLAYERS; i++)
+ {
+ if (player == i || player == ALL_PLAYERS
+ || (player == ALLIES && aiCheckAlliances(i, player))
+ || (player == ENEMIES && !aiCheckAlliances(i, player)))
+ {
+ quantity += getNumDroids(i);
+ }
+ }
+ return QScriptValue(quantity);
}
//-- \subsection{setNoGoArea(x1, y1, x2, y2, player)}
@@ -2189,7 +2230,8 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("setScrollParams", engine->newFunction(js_setScrollParams));
engine->globalObject().setProperty("addStructure", engine->newFunction(js_addStructure));
engine->globalObject().setProperty("getStructureLimit", engine->newFunction(js_getStructureLimit));
- engine->globalObject().setProperty("getStructureCount", engine->newFunction(js_getStructureCount));
+ engine->globalObject().setProperty("countStruct", engine->newFunction(js_countStruct));
+ engine->globalObject().setProperty("countDroid", engine->newFunction(js_countDroid));
engine->globalObject().setProperty("loadLevel", engine->newFunction(js_loadLevel));
engine->globalObject().setProperty("setDroidExperience", engine->newFunction(js_setDroidExperience));
engine->globalObject().setProperty("setNoGoArea", engine->newFunction(js_setNoGoArea));
@@ -2254,6 +2296,9 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("STRUCTURE", OBJ_STRUCTURE, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("DROID", OBJ_DROID, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("FEATURE", OBJ_FEATURE, QScriptValue::ReadOnly | QScriptValue::Undeletable);
+ engine->globalObject().setProperty("ALL_PLAYERS", ALL_PLAYERS, QScriptValue::ReadOnly | QScriptValue::Undeletable);
+ engine->globalObject().setProperty("ALLIES", ALLIES, QScriptValue::ReadOnly | QScriptValue::Undeletable);
+ engine->globalObject().setProperty("ENEMIES", ENEMIES, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("POSITION", SCRIPT_POSITION, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("AREA", SCRIPT_AREA, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("PLAYER_DATA", SCRIPT_PLAYER, QScriptValue::ReadOnly | QScriptValue::Undeletable);

0 comments on commit d0c8eb7

Please sign in to comment.