static QScriptValue js_enumDroid(QScriptContext *context, QScriptEngine *engine) { QList<DROID *> matches; int player = -1, looking = -1; DROID_TYPE droidType = DROID_ANY; switch (context->argumentCount()) { default: case 3: looking = context->argument(2).toInt32(); // fall-through case 2: droidType = (DROID_TYPE)context->argument(1).toInt32(); // 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 (DROID *psDroid = apsDroidLists[player]; psDroid; psDroid = psDroid->psNext) { if ((looking == -1 || psDroid->visible[looking]) && (droidType == DROID_ANY || droidType == psDroid->droidType)) { matches.push_back(psDroid); } } QScriptValue result = engine->newArray(matches.size()); for (int i = 0; i < matches.size(); i++) { DROID *psDroid = matches.at(i); result.setProperty(i, convDroid(psDroid, engine)); } return result; }
static QScriptValue js_enumStruct(QScriptContext *context, QScriptEngine *engine) { QList<STRUCTURE *> matches; int player = -1, looking = -1; QString statsName; switch (context->argumentCount()) { default: case 3: looking = context->argument(2).toInt32(); // fall-through case 2: statsName = context->argument(1).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 = apsStructLists[player]; psStruct; psStruct = psStruct->psNext) { if ((looking == -1 || psStruct->visible[looking]) && (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; }
static QScriptValue js_completeResearch(QScriptContext *context, QScriptEngine *engine) { QString researchName = context->argument(0).toString(); int player; if (context->argumentCount() > 1) { player = context->argument(0).toInt32(); } else { player = engine->globalObject().property("me").toInt32(); } RESEARCH *psResearch = getResearch(researchName.toUtf8().constData()); SCRIPT_ASSERT(context, psResearch, "No such research %s for player %d", researchName.toUtf8().constData(), player); int researchIndex = psResearch - asResearch; SCRIPT_ASSERT(context, researchIndex < numResearch, "Research index out of bounds"); if (bMultiMessages && (gameTime > 2)) { SendResearch(player, researchIndex, false); // Wait for our message before doing anything. } else { researchResult(researchIndex, player, false, NULL, false); } return QScriptValue(); }
static QScriptValue js_allianceExistsBetween(QScriptContext *context, QScriptEngine *engine) { int player1 = context->argument(0).toInt32(); int player2 = context->argument(1).toInt32(); SCRIPT_ASSERT(context, player1 < MAX_PLAYERS && player1 >= 0, "Invalid player"); SCRIPT_ASSERT(context, player2 < MAX_PLAYERS && player2 >= 0, "Invalid player"); return QScriptValue(alliances[player1][player2] == ALLIANCE_FORMED); }
static QScriptValue js_groupAddDroid(QScriptContext *context, QScriptEngine *engine) { int groupId = context->argument(0).toInt32(); DROID_GROUP *psGroup = grpFind(groupId); QScriptValue droidVal = context->argument(1); int droidId = droidVal.property("id").toInt32(); int droidPlayer = droidVal.property("player").toInt32(); DROID *psDroid = IdToDroid(droidId, droidPlayer); SCRIPT_ASSERT(context, psGroup, "Invalid group index %d", groupId); SCRIPT_ASSERT(context, psDroid, "Invalid droid index %d", droidId); psGroup->add(psDroid); return QScriptValue(); }
static QScriptValue js_orderDroidLoc(QScriptContext *context, QScriptEngine *) { QScriptValue droidVal = context->argument(0); int id = droidVal.property("id").toInt32(); int player = droidVal.property("player").toInt32(); QScriptValue orderVal = context->argument(1); int x = context->argument(2).toInt32(); int y = context->argument(3).toInt32(); DROID_ORDER order = (DROID_ORDER)orderVal.toInt32(); DROID *psDroid = IdToDroid(id, player); SCRIPT_ASSERT(context, psDroid, "Droid id %d not found belonging to player %d", id, player); SCRIPT_ASSERT(context, worldOnMap(x, y), "Outside map bounds (%d, %d)", x, y); orderDroidLoc(psDroid, order, x, y, ModeQueue); return QScriptValue(); }
static QScriptValue js_setReinforcementTime(QScriptContext *context, QScriptEngine *) { int value = context->argument(0).toInt32() * 100; SCRIPT_ASSERT(context, value == LZ_COMPROMISED_TIME || value < 60 * 60 * GAME_TICKS_PER_SEC, "The transport timer cannot be set to more than 1 hour!"); mission.ETA = value; if (missionCanReEnforce()) { addTransporterTimerInterface(); } if (value < 0) { DROID *psDroid; intRemoveTransporterTimer(); /* Only remove the launch if haven't got a transporter droid since the scripts set the * time to -1 at the between stage if there are not going to be reinforcements on the submap */ for (psDroid = apsDroidLists[selectedPlayer]; psDroid != NULL; psDroid = psDroid->psNext) { if (psDroid->droidType == DROID_TRANSPORTER) { break; } } // if not found a transporter, can remove the launch button if (psDroid == NULL) { intRemoveTransporterLaunch(); } } return QScriptValue(); }
//-- \subsection{bind(function, object[, player])} //-- Bind a function call to an object. The function is called before the //-- object is destroyed. The function to run is the first parameter, and it //-- \underline{must be quoted}, otherwise the function will be inlined. The //-- second argument is the object to bind to. A third, optional player parameter //-- may be passed, which may be used for filtering, depending on the object type. //-- \emph{NOTE: This function is under construction and is subject to total change!} static QScriptValue js_bind(QScriptContext *context, QScriptEngine *engine) { SCRIPT_ASSERT(context, context->argument(0).isString(), "Bound functions must be quoted"); bindNode node; QScriptValue objv = context->argument(1); node.type = objv.property("type").toInt32(); node.player = -1; node.funcName = context->argument(0).toString(); node.engine = engine; node.player = objv.property("player").toInt32(); node.id = objv.property("id").toInt32(); if (node.type == OBJ_DROID || node.type == OBJ_STRUCTURE || node.type == OBJ_FEATURE) { BASE_OBJECT *psObj = IdToPointer(node.id, node.player); if (psObj && !psObj->died) { bindings.insert(node.id, node); } } else { debug(LOG_ERROR, "Trying to bind to illegal object type"); } return QScriptValue(); }
static QScriptValue js_addReticuleButton(QScriptContext *context, QScriptEngine *engine) { int button = context->argument(0).toInt32(); SCRIPT_ASSERT(context, button != IDRET_OPTIONS, "Invalid button"); widgReveal(psWScreen, button); return QScriptValue(); }
static QScriptValue js_groupSize(QScriptContext *context, QScriptEngine *) { int groupId = context->argument(0).toInt32(); DROID_GROUP *psGroup = grpFind(groupId); SCRIPT_ASSERT(context, psGroup, "Group %d not found", groupId); return QScriptValue(psGroup->refCount); }
static QScriptValue js_makeComponentAvailable(QScriptContext *context, QScriptEngine *engine) { QString componentName = context->argument(0).toString(); int player = context->argument(1).toInt32(); SCRIPT_ASSERT(context, player < MAX_PLAYERS && player >= 0, "Invalid player"); setComponent(componentName, player, AVAILABLE); return QScriptValue(); }
static QScriptValue js_structureIdle(QScriptContext *context, QScriptEngine *) { QScriptValue structVal = context->argument(0); int id = structVal.property("id").toInt32(); int player = structVal.property("player").toInt32(); STRUCTURE *psStruct = IdToStruct(id, player); SCRIPT_ASSERT(context, psStruct, "No such structure id %d belonging to player %d", id, player); return QScriptValue(structureIdle(psStruct)); }
/* Build a droid template in the specified factory */ static QScriptValue js_buildDroid(QScriptContext *context, QScriptEngine *) { QScriptValue structVal = context->argument(1); int id = structVal.property("id").toInt32(); int player = structVal.property("player").toInt32(); QScriptValue templName = context->argument(0); DROID_TEMPLATE *psTemplate = getTemplateFromTranslatedNameNoPlayer(templName.toString().toUtf8().constData()); STRUCTURE *psStruct = IdToStruct(id, player); SCRIPT_ASSERT(context, psStruct != NULL, "No factory object found for id %d, player %d", id, player); SCRIPT_ASSERT(context, psTemplate != NULL, "No template object found for %s sent to %s", templName.toString().toUtf8().constData(), objInfo(psStruct)); SCRIPT_ASSERT(context, (psStruct->pStructureType->type == REF_FACTORY || psStruct->pStructureType->type == REF_CYBORG_FACTORY || psStruct->pStructureType->type == REF_VTOL_FACTORY), "Structure %s is not a factory", objInfo(psStruct)); SCRIPT_ASSERT(context, validTemplateForFactory(psTemplate, psStruct), "Invalid template - %s for factory - %s", psTemplate->aName, psStruct->pStructureType->pName); return QScriptValue(structSetManufacture(psStruct, psTemplate, ModeQueue)); }
static QScriptValue js_playSound(QScriptContext *context, QScriptEngine *engine) { ARG_COUNT_VAR(1, 4); ARG_STRING(0); if (context->argumentCount() != 1) { SCRIPT_ASSERT(context, context->argumentCount() == 4, "Arguments must be either 1 or 4"); ARG_NUMBER(1); ARG_NUMBER(2); ARG_NUMBER(3); } return QScriptValue(); }
static QScriptValue js_setStructureLimits(QScriptContext *context, QScriptEngine *engine) { QString building = context->argument(0).toString(); int limit = context->argument(1).toInt32(); int player; int structInc = getStructStatFromName(building.toUtf8().constData()); if (context->argumentCount() > 2) { player = context->argument(2).toInt32(); } else { player = engine->globalObject().property("me").toInt32(); } SCRIPT_ASSERT(context, player < MAX_PLAYERS && player >= 0, "Invalid player number"); SCRIPT_ASSERT(context, limit < LOTS_OF && limit >= 0, "Invalid limit"); SCRIPT_ASSERT(context, structInc < numStructureStats && structInc >= 0, "Invalid structure"); STRUCTURE_LIMITS *psStructLimits = asStructLimits[player]; psStructLimits[structInc].limit = limit; psStructLimits[structInc].globalLimit = limit; return QScriptValue(); }
//-- \subsection{setTimer(function, milliseconds[, object])} //-- Set a function to run repeated at some given time interval. The function to run //-- is the first parameter, and it \underline{must be quoted}, otherwise the function will //-- be inlined. The second parameter is the interval, in milliseconds. A third, optional //-- parameter can be a \emph{game object} to pass to the timer function. If the \emph{game object} //-- dies, the timer stops running. The minimum number of milliseconds is 100, but such //-- fast timers are strongly discouraged as they may deteriorate the game performance. //-- //-- \begin{lstlisting} //-- function conDroids() //-- { //-- ... do stuff ... //-- } //-- // call conDroids every 4 seconds //-- setTimer("conDroids", 4000); //-- \end{lstlisting} static QScriptValue js_setTimer(QScriptContext *context, QScriptEngine *engine) { SCRIPT_ASSERT(context, context->argument(0).isString(), "Timer functions must be quoted"); QString funcName = context->argument(0).toString(); QScriptValue ms = context->argument(1); int player = engine->globalObject().property("me").toInt32(); timerNode node(engine, funcName, player, ms.toInt32()); if (context->argumentCount() == 3) { QScriptValue obj = context->argument(2); node.baseobj = obj.property("id").toInt32(); } node.type = TIMER_REPEAT; timers.push_back(node); return QScriptValue(); }
static QScriptValue js_enableStructure(QScriptContext *context, QScriptEngine *engine) { QString building = context->argument(0).toString(); int index = getStructStatFromName(building.toUtf8().constData()); int player; if (context->argumentCount() > 1) { player = context->argument(0).toInt32(); } else { player = engine->globalObject().property("me").toInt32(); } SCRIPT_ASSERT(context, index >= 0 && index < numStructureStats, "Invalid structure stat"); // enable the appropriate structure apStructTypeLists[player][index] = AVAILABLE; return QScriptValue(); }
static QScriptValue js_removeTimer(QScriptContext *context, QScriptEngine *) { ARG_COUNT_EXACT(1); ARG_STRING(0); QString function = context->argument(0).toString(); int i, size = timers.size(); for (i = 0; i < size; ++i) { timerNode node = timers.at(i); if (node.function == function) { return QScriptValue(); } } QString warnName = function.left(15) + "..."; SCRIPT_ASSERT(context, false, "Did not find timer %s to remove", warnName.toUtf8().constData()); return QScriptValue(); }
static QScriptValue js_enumGroup(QScriptContext *context, QScriptEngine *engine) { int groupId = context->argument(0).toInt32(); QList<DROID *> matches; DROID_GROUP *psGroup = grpFind(groupId); DROID *psCurr; SCRIPT_ASSERT(context, psGroup, "Invalid group index %d", groupId); for (psCurr = psGroup->psList; psCurr != NULL; psCurr = psCurr->psGrpNext) { matches.push_back(psCurr); } QScriptValue result = engine->newArray(matches.size()); for (int i = 0; i < matches.size(); i++) { DROID *psDroid = matches.at(i); result.setProperty(i, convDroid(psDroid, engine)); } return result; }
static QScriptValue js_enableResearch(QScriptContext *context, QScriptEngine *engine) { QString researchName = context->argument(0).toString(); int player; if (context->argumentCount() > 1) { player = context->argument(0).toInt32(); } else { player = engine->globalObject().property("me").toInt32(); } RESEARCH *psResearch = getResearch(researchName.toUtf8().constData()); SCRIPT_ASSERT(context, psResearch, "No such research %s for player %d", researchName.toUtf8().constData(), player); if (!enableResearch(psResearch, player)) { debug(LOG_ERROR, "Unable to enable research %s for player %d", researchName.toUtf8().constData(), player); } return QScriptValue(); }
static QScriptValue js_groupAddArea(QScriptContext *context, QScriptEngine *engine) { int groupId = context->argument(0).toInt32(); int player = engine->globalObject().property("me").toInt32(); int x1 = context->argument(1).toInt32(); int y1 = context->argument(2).toInt32(); int x2 = context->argument(3).toInt32(); int y2 = context->argument(4).toInt32(); DROID_GROUP *psGroup = grpFind(groupId); SCRIPT_ASSERT(context, psGroup, "Invalid group index %d", groupId); for (DROID *psDroid = apsDroidLists[player]; psGroup && psDroid; psDroid = psDroid->psNext) { if (psDroid->pos.x >= x1 && psDroid->pos.x <= x2 && psDroid->pos.y >= y1 && psDroid->pos.y <= y2 && psDroid->droidType != DROID_COMMAND && psDroid->droidType != DROID_TRANSPORTER) { psGroup->add(psDroid); } } return QScriptValue(); }
//-- \subsection{removeTimer(function)} //-- Removes an existing timer. The first parameter is the function timer to remove, //-- and its name \underline{must be quoted}. static QScriptValue js_removeTimer(QScriptContext *context, QScriptEngine *engine) { SCRIPT_ASSERT(context, context->argument(0).isString(), "Timer functions must be quoted"); QString function = context->argument(0).toString(); int i, size = timers.size(); for (i = 0; i < size; ++i) { timerNode node = timers.at(i); if (node.function == function) { timers.removeAt(i); break; } } if (i == size) { // Friendly warning QString warnName = function.left(15) + "..."; debug(LOG_ERROR, "Did not find timer %s to remove", warnName.toAscii().constData()); } return QScriptValue(); }
const char *api_intl_PL_(const char *singular, const char *plural, int n) { SCRIPT_ASSERT(NULL != singular, NULL); SCRIPT_ASSERT(NULL != plural, NULL); return PL_(singular, plural, n); }
const char *api_intl_Q_(const char *untranslated) { SCRIPT_ASSERT(NULL != untranslated, NULL); return Q_(untranslated); }