Skip to content

Commit

Permalink
qtscript: addDroid() and addStructure() now return the created game o…
Browse files Browse the repository at this point in the history
…bjects on success.

Added new global isMultiplayer which is true iff the current game is a online game.
  • Loading branch information
perim committed Jan 16, 2013
1 parent d1627c4 commit 958993e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/qtscript.cpp
Expand Up @@ -42,6 +42,7 @@
#include "multiplay.h"
#include "map.h"
#include "difficulty.h"
#include "lib/netplay/netplay.h"

#include "qtscriptfuncs.h"

Expand Down Expand Up @@ -436,6 +437,8 @@ bool loadPlayerScript(QString path, int player, int difficulty)
engine->globalObject().setProperty("mapHeight", mapHeight, QScriptValue::ReadOnly | QScriptValue::Undeletable);
//== \item[scavengerPlayer] Index of scavenger player. (3.2+ only)
engine->globalObject().setProperty("scavengerPlayer", scavengerPlayer(), QScriptValue::ReadOnly | QScriptValue::Undeletable);
//== \item[isMultiplayer] If the current game is a online multiplayer game or not. (3.2+ only)
engine->globalObject().setProperty("isMultiplayer", NetPlay.bComms, QScriptValue::ReadOnly | QScriptValue::Undeletable);

// Regular functions
QFileInfo basename(path);
Expand Down
20 changes: 11 additions & 9 deletions src/qtscriptfuncs.cpp
Expand Up @@ -1186,7 +1186,7 @@ static QScriptValue js_addFeature(QScriptContext *context, QScriptEngine *engine
//-- \subsection{addDroid(player, x, y, name, body, propulsion, reserved, droid type, turrets...)}
//-- Create and place a droid at the given x, y position as belonging to the given player, built with
//-- the given components. Currently does not support placing droids in multiplayer, doing so will
//-- cause a desync. Returns a boolean that is true on success.
//-- cause a desync. Returns the created droid on success, otherwise returns null.
static QScriptValue js_addDroid(QScriptContext *context, QScriptEngine *engine)
{
int player = context->argument(0).toInt32();
Expand All @@ -1198,20 +1198,20 @@ static QScriptValue js_addDroid(QScriptContext *context, QScriptEngine *engine)
DROID_TYPE droidType = (DROID_TYPE)context->argument(7).toInt32();
int numTurrets = context->argumentCount() - 8; // anything beyond first eight parameters, are turrets
COMPONENT_TYPE compType = COMP_NUMCOMPONENTS;

memset(psTemplate->asParts, 0, sizeof(psTemplate->asParts)); // reset to defaults
memset(psTemplate->asWeaps, 0, sizeof(psTemplate->asWeaps));
int body = getCompFromName(COMP_BODY, context->argument(4).toString().toUtf8().constData());
int propulsion = getCompFromName(COMP_PROPULSION, context->argument(5).toString().toUtf8().constData());
if (body < 0)
{
debug(LOG_ERROR, "Wanted to build %s, but body type unavailable", templName.toUtf8().constData());
return QScriptValue(false); // no component available
return QScriptValue::NullValue; // no component available
}
if (propulsion < 0)
{
debug(LOG_ERROR, "Wanted to build %s, but propulsion type unavailable", templName.toUtf8().constData());
return QScriptValue(false); // no component available
return QScriptValue::NullValue; // no component available
}
psTemplate->asParts[COMP_BODY] = body;
psTemplate->asParts[COMP_PROPULSION] = propulsion;
Expand Down Expand Up @@ -1263,7 +1263,7 @@ static QScriptValue js_addDroid(QScriptContext *context, QScriptEngine *engine)
if (j < 0)
{
debug(LOG_SCRIPT, "Wanted to build %s, but turret unavailable", templName.toUtf8().constData());
return QScriptValue(false);
return QScriptValue::NullValue;
}
psTemplate->asParts[compType] = j;
}
Expand All @@ -1284,12 +1284,13 @@ static QScriptValue js_addDroid(QScriptContext *context, QScriptEngine *engine)
debug(LOG_ERROR, "Invalid droid %s", templName.toUtf8().constData());
}
bMultiMessages = oldMulti; // ugh
return psDroid ? QScriptValue(convDroid(psDroid, engine)) : QScriptValue::NullValue;
}
else
{
debug(LOG_ERROR, "Invalid template %s", templName.toUtf8().constData());
}
return QScriptValue(valid);
return QScriptValue::NullValue;
}

static int get_first_available_component(STRUCTURE *psFactory, const QScriptValue &list, COMPONENT_TYPE type)
Expand Down Expand Up @@ -2730,8 +2731,8 @@ static QScriptValue js_safeDest(QScriptContext *context, QScriptEngine *engine)
}

//-- \subsection{addStructure(structure type, player, x, y)}
//-- Create a structure on the given position. Returns true on success.
static QScriptValue js_addStructure(QScriptContext *context, QScriptEngine *)
//-- Create a structure on the given position. Returns the structure on success, null otherwise.
static QScriptValue js_addStructure(QScriptContext *context, QScriptEngine *engine)
{
QString building = context->argument(0).toString();
int index = getStructStatFromName(building.toUtf8().constData());
Expand All @@ -2745,8 +2746,9 @@ static QScriptValue js_addStructure(QScriptContext *context, QScriptEngine *)
{
psStruct->status = SS_BUILT;
buildingComplete(psStruct);
return QScriptValue(convStructure(psStruct, engine));
}
return QScriptValue(psStruct != NULL);
return QScriptValue::NullValue;
}

//-- \subsection{getStructureLimit(structure type[, player])}
Expand Down

0 comments on commit 958993e

Please sign in to comment.