Skip to content

Commit

Permalink
qtscript: donateObject() works on structures. findResearch() works on…
Browse files Browse the repository at this point in the history
… other players.
  • Loading branch information
perim committed Mar 25, 2017
1 parent 2acaae6 commit 839e0b0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
37 changes: 32 additions & 5 deletions src/multigifts.cpp
Expand Up @@ -58,10 +58,12 @@
///////////////////////////////////////////////////////////////////////////////
// prototypes

static void recvGiftDroids(uint8_t from, uint8_t to, uint32_t droidID);
static void sendGiftDroids(uint8_t from, uint8_t to);
static void giftResearch(uint8_t from, uint8_t to, bool send);
static void giftAutoGame(uint8_t from, uint8_t to, bool send);
static void recvGiftStruct(uint8_t from, uint8_t to, uint32_t structID);
static void recvGiftDroids(uint8_t from, uint8_t to, uint32_t droidID);
static void sendGiftDroids(uint8_t from, uint8_t to);
static void giftResearch(uint8_t from, uint8_t to, bool send);
static void giftAutoGame(uint8_t from, uint8_t to, bool send);

///////////////////////////////////////////////////////////////////////////////
// gifts..

Expand Down Expand Up @@ -96,6 +98,10 @@ bool recvGift(NETQUEUE queue)
audioTrack = ID_UNITS_TRANSFER;
recvGiftDroids(from, to, droidID);
break;
case STRUCTURE_GIFT:
audioTrack = ID_GIFT;
recvGiftStruct(from, to, droidID);
break;
case RESEARCH_GIFT:
audioTrack = ID_TECHNOLOGY_TRANSFER;
giftResearch(from, to, false);
Expand Down Expand Up @@ -148,9 +154,10 @@ bool sendGift(uint8_t type, uint8_t to)
giftAutoGame(selectedPlayer, to, true);
return true;
break;
case STRUCTURE_GIFT:
// not implemented
default:
debug(LOG_ERROR, "Unknown Gift sent");

return false;
break;
}
Expand Down Expand Up @@ -218,6 +225,26 @@ void giftRadar(uint8_t from, uint8_t to, bool send)
}
}

// NOTICE: the packet is already set-up for decoding via recvGift()
static void recvGiftStruct(uint8_t from, uint8_t to, uint32_t structID)
{
STRUCTURE *psStruct = IdToStruct(structID, from);
if (psStruct)
{
syncDebugStructure(psStruct, '<');
giftSingleStructure(psStruct, to, false);
syncDebugStructure(psStruct, '>');
if (to == selectedPlayer)
{
CONPRINTF(ConsoleString, (ConsoleString, _("%s Gives you a %s"), getPlayerName(from), objInfo(psStruct)));
}
}
else
{
debug(LOG_ERROR, "Bad structure id %u, from %u to %u", structID, from, to);
}
}

// recvGiftDroid()
// We received a droid gift from another player.
// NOTICE: the packet is already set-up for decoding via recvGift()
Expand Down
2 changes: 1 addition & 1 deletion src/multigifts.h
Expand Up @@ -46,7 +46,7 @@ extern void giftRadar(uint8_t from, uint8_t to, bool send);
#define DROID_GIFT 2
#define RESEARCH_GIFT 3
#define POWER_GIFT 4
#define STRUCTURE_GIFT 5 // Unused
#define STRUCTURE_GIFT 5
#define AUTOGAME_GIFT 6 // Notify others that we are now being controled by the AI

#endif // __INCLUDED_SRC_MULTIGIFTS_H__
39 changes: 30 additions & 9 deletions src/qtscriptfuncs.cpp
Expand Up @@ -1407,13 +1407,18 @@ static QScriptValue js_activateStructure(QScriptContext *context, QScriptEngine
return QScriptValue(true);
}

//-- \subsection{findResearch(research)}
//-- \subsection{findResearch(research, [player])}
//-- Return list of research items remaining to be researched for the given research item. (3.2+ only)
//-- (Optional second argument 3.2.3+ only)
static QScriptValue js_findResearch(QScriptContext *context, QScriptEngine *engine)
{
QList<RESEARCH *> list;
QString resName = context->argument(0).toString();
int player = engine->globalObject().property("me").toInt32();
if (context->argumentCount() == 2)
{
player = context->argument(1).toInt32();
}
RESEARCH *psTarget = getResearch(resName.toUtf8().constData());
SCRIPT_ASSERT(context, psTarget, "No such research: %s", resName.toUtf8().constData());
PLAYER_RESEARCH *plrRes = &asPlayerResList[player][psTarget->index];
Expand Down Expand Up @@ -3293,7 +3298,7 @@ static QScriptValue js_setDroidExperience(QScriptContext *context, QScriptEngine
}

//-- \subsection{donateObject(object, to)}
//-- Donate a game object (currently restricted to droids) to another player. Returns true if
//-- Donate a game object (restricted to droids before 3.2.3) to another player. Returns true if
//-- donation was successful. May return false if this donation would push the receiving player
//-- over unit limits. (3.2+ only)
static QScriptValue js_donateObject(QScriptContext *context, QScriptEngine *engine)
Expand All @@ -3303,6 +3308,7 @@ static QScriptValue js_donateObject(QScriptContext *context, QScriptEngine *engi
uint8_t player = val.property("player").toInt32();
OBJECT_TYPE type = (OBJECT_TYPE)val.property("type").toInt32();
uint8_t to = context->argument(1).toInt32();
uint8_t giftType = 0;
if (type == OBJ_DROID)
{
// Check unit limits.
Expand All @@ -3314,14 +3320,29 @@ static QScriptValue js_donateObject(QScriptContext *context, QScriptEngine *engi
{
return QScriptValue(false);
}
uint8_t giftType = DROID_GIFT;
NETbeginEncode(NETgameQueue(selectedPlayer), GAME_GIFT);
NETuint8_t(&giftType);
NETuint8_t(&player);
NETuint8_t(&to);
NETuint32_t(&id);
NETend();
giftType = DROID_GIFT;
}
else if (type == OBJ_STRUCTURE)
{
STRUCTURE *psStruct = IdToStruct(id, player);
SCRIPT_ASSERT(context, psStruct, "No such struct id %u belonging to player %u", id, player);
int max = psStruct->pStructureType - asStructureStats;
if (asStructLimits[player][max].currentQuantity + 1 > asStructLimits[player][max].limit)
{
return QScriptValue(false);
}
giftType = STRUCTURE_GIFT;
}
else
{
return QScriptValue(false);
}
NETbeginEncode(NETgameQueue(selectedPlayer), GAME_GIFT);
NETuint8_t(&giftType);
NETuint8_t(&player);
NETuint8_t(&to);
NETuint32_t(&id);
NETend();
return QScriptValue(true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/scriptfuncs.cpp
Expand Up @@ -5647,7 +5647,7 @@ bool scrTakeOverSingleStructure(void)
}
else
{
psNewStruct = giftSingleStructure(psStructToTake, (UBYTE)playerToGain, true);
psNewStruct = giftSingleStructure(psStructToTake, (UBYTE)playerToGain);
if (psNewStruct)
{
//check the structure limits aren't compromised
Expand Down Expand Up @@ -5736,7 +5736,7 @@ bool scrTakeOverStructsInArea(void)
else
{
//give the structure away
psNewStruct = giftSingleStructure(psStruct, (UBYTE)toPlayer, true);
psNewStruct = giftSingleStructure(psStruct, (UBYTE)toPlayer);
if (psNewStruct)
{
numChanged++;
Expand Down
18 changes: 3 additions & 15 deletions src/structure.cpp
Expand Up @@ -5687,7 +5687,7 @@ bool electronicDamage(BASE_OBJECT *psTarget, UDWORD damage, UBYTE attackPlayer)
}
bCompleted = true;
//give the structure to the attacking player
(void)giftSingleStructure(psStructure, attackPlayer, false);
(void)giftSingleStructure(psStructure, attackPlayer);
}
}
}
Expand Down Expand Up @@ -6832,7 +6832,7 @@ bool structIsDamaged(STRUCTURE *psStruct)

// give a structure from one player to another - used in Electronic Warfare
//returns pointer to the new structure
STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool bFromScript)
STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool electronic_warfare)
{
STRUCTURE *psNewStruct, *psStruct;
DROID *psCurr;
Expand All @@ -6846,23 +6846,12 @@ STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool
CHECK_STRUCTURE(psStructure);
visRemoveVisibility(psStructure);

//this is not the case for EW in multiPlayer mode
if (!bMultiPlayer)
{
//added 'selectedPlayer != 0' to be able to test the game by changing player...
//in this version of Warzone, the attack Player can NEVER be the selectedPlayer (unless from the script)
ASSERT_OR_RETURN(NULL, bFromScript || selectedPlayer != 0 || attackPlayer != selectedPlayer, "EW attack by selectedPlayer on a structure");
}

int prevState = intGetResearchState();

//don't want the hassle in multiplayer either
//and now we do! - AB 13/05/99

if (bMultiPlayer)
{
//certain structures give specific results - the rest swap sides!
if (!electronicReward(psStructure, attackPlayer))
if (!electronic_warfare || !electronicReward(psStructure, attackPlayer))
{
//tell the system the structure no longer exists
(void)removeStruct(psStructure, false);
Expand Down Expand Up @@ -6927,7 +6916,6 @@ STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool
return NULL;
}


//save info about the structure
psType = psStructure->pStructureType;
x = psStructure->pos.x;
Expand Down
2 changes: 1 addition & 1 deletion src/structure.h
Expand Up @@ -344,7 +344,7 @@ bool vtolOnRearmPad(STRUCTURE *psStruct, DROID *psDroid);
extern bool structIsDamaged(STRUCTURE *psStruct);

// give a structure from one player to another - used in Electronic Warfare
extern STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool bFromScript);
STRUCTURE *giftSingleStructure(STRUCTURE *psStructure, UBYTE attackPlayer, bool electronic_warfare = true);

/*Initialise the production list and set up the production player*/
extern void changeProductionPlayer(UBYTE player);
Expand Down

0 comments on commit 839e0b0

Please sign in to comment.