Skip to content

Commit

Permalink
Add alliance events and functions for JS API.
Browse files Browse the repository at this point in the history
eventAllianceOffer(from, to) - Fires when an alliance is offered.
eventAllianceAccepted(from, to).
eventAllianceBroken(from, to).

Qtscript: sendAllianceRequest(to) - Request an alliance with a player.
It does nothing if the alliance type is not ALLIANCES.
  • Loading branch information
KJeff01 committed Dec 18, 2017
1 parent 19b767a commit 3670bbd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/multigifts.cpp
Expand Up @@ -46,6 +46,7 @@
#include "loop.h"
#include "transporter.h"
#include "mission.h" // for INVALID_XY
#include "qtscript.h"

#include "lib/netplay/netplay.h"
#include "multiplay.h"
Expand Down Expand Up @@ -439,6 +440,7 @@ void requestAlliance(uint8_t from, uint8_t to, bool prop, bool allowAudio)
CBallFrom = from;
CBallTo = to;
eventFireCallbackTrigger((TRIGGER_TYPE) CALL_ALLIANCEOFFER);
triggerEventAllianceOffer(from, to);

if (to == selectedPlayer)
{
Expand Down Expand Up @@ -481,6 +483,7 @@ void breakAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio)
}

syncDebug("Break alliance %d %d", p1, p2);
triggerEventAllianceBroken(p1, p2);
alliances[p1][p2] = ALLIANCE_BROKEN;
alliances[p2][p1] = ALLIANCE_BROKEN;
alliancebits[p1] &= ~(1 << p2);
Expand All @@ -506,6 +509,7 @@ void formAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio, bool allow
}

syncDebug("Form alliance %d %d", p1, p2);
triggerEventAllianceAccepted(p1, p2);
alliances[p1][p2] = ALLIANCE_FORMED;
alliances[p2][p1] = ALLIANCE_FORMED;
if (bMultiPlayer && alliancesSharedVision(game.alliance)) // this is for shared vision only
Expand Down
42 changes: 42 additions & 0 deletions src/qtscript.cpp
Expand Up @@ -1546,6 +1546,48 @@ bool triggerEventDesignCreated(DROID_TEMPLATE *psTemplate)
return true;
}

//__ \subsection{eventAllianceOffer(from, to)}
//__ An event that is called whenever an alliance offer is requested.
bool triggerEventAllianceOffer(uint8_t from, uint8_t to)
{
for (auto *engine : scripts)
{
QScriptValueList args;
args += QScriptValue(from);
args += QScriptValue(to);
callFunction(engine, "eventAllianceOffer", args);
}
return true;
}

//__ \subsection{eventAllianceAccepted(from, to)}
//__ An event that is called whenever an alliance is accepted.
bool triggerEventAllianceAccepted(uint8_t from, uint8_t to)
{
for (auto *engine : scripts)
{
QScriptValueList args;
args += QScriptValue(from);
args += QScriptValue(to);
callFunction(engine, "eventAllianceAccepted", args);
}
return true;
}

//__ \subsection{eventAllianceBroken(from, to)}
//__ An event that is called whenever an alliance is broken.
bool triggerEventAllianceBroken(uint8_t from, uint8_t to)
{
for (auto *engine : scripts)
{
QScriptValueList args;
args += QScriptValue(from);
args += QScriptValue(to);
callFunction(engine, "eventAllianceBroken", args);
}
return true;
}

//__ \subsection{eventSyncRequest(req_id, x, y, obj_id, obj_id2)}
//__ An event that is called from a script and synchronized with all other scripts and hosts
//__ to prevent desync from happening. Sync requests must be carefully validated to prevent
Expand Down
3 changes: 3 additions & 0 deletions src/qtscript.h
Expand Up @@ -138,6 +138,9 @@ bool triggerEventPlayerLeft(int id);
bool triggerEventDesignCreated(DROID_TEMPLATE *psTemplate);
bool triggerEventSyncRequest(int from, int req_id, int x, int y, BASE_OBJECT *psObj, BASE_OBJECT *psObj2);
bool triggerEventKeyPressed(int meta, int key);
bool triggerEventAllianceOffer(uint8_t from, uint8_t to);
bool triggerEventAllianceAccepted(uint8_t from, uint8_t to);
bool triggerEventAllianceBroken(uint8_t from, uint8_t to);

// ----------------------------------------------
// Debug functions
Expand Down
15 changes: 15 additions & 0 deletions src/qtscriptfuncs.cpp
Expand Up @@ -72,6 +72,7 @@
#include "projectile.h"
#include "component.h"
#include "seqdisp.h"
#include "ai.h"

#define FAKE_REF_LASSAT 999
#define ALL_PLAYERS -1
Expand Down Expand Up @@ -3884,6 +3885,19 @@ static QScriptValue js_setAlliance(QScriptContext *context, QScriptEngine *engin
return QScriptValue(true);
}

//-- \subsection{sendAllianceRequest(player)}
//-- Send an alliance request to a player. (3.2.4+ only)
static QScriptValue js_sendAllianceRequest(QScriptContext *context, QScriptEngine *engine)
{
if (!alliancesFixed(game.alliance))
{
int player1 = engine->globalObject().property("me").toInt32();
int player2 = context->argument(0).toInt32();
requestAlliance(player1, player2, true, true);
}
return QScriptValue();
}

//-- \subsection{setAssemblyPoint(structure, x, y)}
//-- Set the assembly point droids go to when built for the specified structure. (3.2+ only)
static QScriptValue js_setAssemblyPoint(QScriptContext *context, QScriptEngine *engine)
Expand Down Expand Up @@ -5483,6 +5497,7 @@ bool registerFunctions(QScriptEngine *engine, const QString& scriptName)
engine->globalObject().setProperty("enumTemplates", engine->newFunction(js_enumTemplates));
engine->globalObject().setProperty("makeTemplate", engine->newFunction(js_makeTemplate));
engine->globalObject().setProperty("setAlliance", engine->newFunction(js_setAlliance));
engine->globalObject().setProperty("sendAllianceRequest", engine->newFunction(js_sendAllianceRequest));
engine->globalObject().setProperty("setAssemblyPoint", engine->newFunction(js_setAssemblyPoint));
engine->globalObject().setProperty("setSunPosition", engine->newFunction(js_setSunPosition));
engine->globalObject().setProperty("setSunIntensity", engine->newFunction(js_setSunIntensity));
Expand Down

0 comments on commit 3670bbd

Please sign in to comment.