qtscript: Add function to enumerate base structures in away missions.

master
cybersphinx 2012-02-10 06:07:24 +01:00
parent 1266a795e6
commit 911dda38c6
1 changed files with 53 additions and 0 deletions

View File

@ -996,6 +996,58 @@ static QScriptValue js_enumStruct(QScriptContext *context, QScriptEngine *engine
return result;
}
//-- \subsection{enumStructOffWorld([player[, structure type[, looking player]]])}
//-- Returns an array of structure objects in your base when on an off-world mission, NULL otherwise.
//-- If no parameters given, it will return all of the structures for the current player.
//-- The second parameter can be either a string with the name of the structure type as defined
//-- in "structures.txt", or a stattype as defined in \ref{objects:structure}.
//-- The third parameter can be used to filter by visibility, the default is not
//-- to filter.
static QScriptValue js_enumStructOffWorld(QScriptContext *context, QScriptEngine *engine)
{
QList<STRUCTURE *> matches;
int player = -1, looking = -1;
QString statsName;
QScriptValue val;
STRUCTURE_TYPE type = NUM_DIFF_BUILDINGS;
switch (context->argumentCount())
{
default:
case 3: looking = context->argument(2).toInt32(); // fall-through
case 2: val = context->argument(1);
if (val.isNumber())
{
type = (STRUCTURE_TYPE)val.toInt32();
}
else
{
statsName = val.toString();
} // fall-through
case 1: player = context->argument(0).toInt32(); break;
case 0: player = engine->globalObject().property("me").toInt32();
}
SCRIPT_ASSERT(context, player < MAX_PLAYERS && player >= 0, "Target player index out of range: %d", player);
SCRIPT_ASSERT(context, looking < MAX_PLAYERS && looking >= -1, "Looking player index out of range: %d", looking);
for (STRUCTURE *psStruct = mission.apsStructLists[player]; psStruct; psStruct = psStruct->psNext)
{
if ((looking == -1 || psStruct->visible[looking])
&& (type == NUM_DIFF_BUILDINGS || type == psStruct->pStructureType->type)
&& (statsName.isEmpty() || statsName.compare(psStruct->pStructureType->pName) == 0))
{
matches.push_back(psStruct);
}
}
QScriptValue result = engine->newArray(matches.size());
for (int i = 0; i < matches.size(); i++)
{
STRUCTURE *psStruct = matches.at(i);
result.setProperty(i, convStructure(psStruct, engine));
}
return result;
}
//-- \subsection{enumFeature(player, name)}
//-- Returns an array of all features seen by player of given name, as defined in "features.txt".
//-- If player is -1, it will return all features irrespective of visibility to any player. If
@ -1971,6 +2023,7 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("console", engine->newFunction(js_console));
engine->globalObject().setProperty("structureIdle", engine->newFunction(js_structureIdle));
engine->globalObject().setProperty("enumStruct", engine->newFunction(js_enumStruct));
engine->globalObject().setProperty("enumStructOffWorld", engine->newFunction(js_enumStructOffWorld));
engine->globalObject().setProperty("enumDroid", engine->newFunction(js_enumDroid));
engine->globalObject().setProperty("enumGroup", engine->newFunction(js_enumGroup));
engine->globalObject().setProperty("enumFeature", engine->newFunction(js_enumFeature));