Skip to content

Commit

Permalink
qtscript: Add new function getDroidProduction(factory) to return a vi…
Browse files Browse the repository at this point in the history
…rtual

droid representation of whatever is in production in a factory.
  • Loading branch information
perim committed Jun 25, 2012
1 parent 7bc528e commit 40fcab0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
52 changes: 21 additions & 31 deletions src/droid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,29 @@ DROID::DROID(uint32_t id, unsigned player)
order.direction = 0;
order.psObj = NULL;
order.psStats = NULL;

sMove.asPath = NULL;
sMove.Status = MOVEINACTIVE;
lastFrustratedTime = -UINT16_MAX; // make sure we do not start the game frustrated
listSize = 0;
listPendingBegin = 0;
iAudioID = NO_SOUND;
psCurAnim = NULL;
group = UBYTE_MAX;
psBaseStruct = NULL;
sDisplay.frameNumber = 0; // it was never drawn before
for (unsigned vPlayer = 0; vPlayer < MAX_PLAYERS; ++vPlayer)
{
visible[vPlayer] = hasSharedVision(vPlayer, player)? UINT8_MAX : 0;
}
memset(seenThisTick, 0, sizeof(seenThisTick));
died = 0;
burnStart = 0;
burnDamage = 0;
sDisplay.screenX = OFF_SCREEN;
sDisplay.screenY = OFF_SCREEN;
sDisplay.screenR = 0;
illumination = UBYTE_MAX;
resistance = ACTION_START_TIME; // init the resistance to indicate no EW performed on this droid
}

/* DROID::~DROID: release all resources associated with a droid -
Expand Down Expand Up @@ -1848,16 +1868,6 @@ DROID *reallyBuildDroid(DROID_TEMPLATE *pTemplate, Position pos, UDWORD player,
psGrp->add(psDroid);
}

psDroid->lastFrustratedTime = -UINT16_MAX; // make sure we do not start the game frustrated

psDroid->listSize = 0;
psDroid->listPendingBegin = 0;

psDroid->iAudioID = NO_SOUND;
psDroid->psCurAnim = NULL;
psDroid->group = UBYTE_MAX;
psDroid->psBaseStruct = NULL;

// find the highest stored experience
// Unless game time is stopped, then we're hopefully loading a game and
// don't want to use up recycled experience for the droids we just loaded.
Expand Down Expand Up @@ -1895,12 +1905,8 @@ DROID *reallyBuildDroid(DROID_TEMPLATE *pTemplate, Position pos, UDWORD player,
// Initialise the movement stuff
psDroid->baseSpeed = calcDroidBaseSpeed(pTemplate, psDroid->weight, (UBYTE)player);

psDroid->sMove.asPath = NULL;
initDroidMovement(psDroid);

// it was never drawn before
psDroid->sDisplay.frameNumber = 0;

//allocate 'easy-access' data!
objSensorCache((BASE_OBJECT *)psDroid, asSensorStats + pTemplate->asParts[COMP_SENSOR]);
objEcmCache((BASE_OBJECT *)psDroid, asECMStats + pTemplate->asParts[COMP_ECM]);
Expand All @@ -1913,23 +1919,7 @@ DROID *reallyBuildDroid(DROID_TEMPLATE *pTemplate, Position pos, UDWORD player,
cyborgDroid(psDroid) ? CYBORG_BODY_UPGRADE : DROID_BODY_UPGRADE, (WEAPON_CLASS)inc);
}

//init the resistance to indicate no EW performed on this droid
psDroid->resistance = ACTION_START_TIME;

for (unsigned vPlayer = 0; vPlayer < MAX_PLAYERS; ++vPlayer)
{
psDroid->visible[vPlayer] = hasSharedVision(vPlayer, player)? UINT8_MAX : 0;
}
memset(psDroid->seenThisTick, 0, sizeof(psDroid->seenThisTick));
psDroid->died = 0;
psDroid->burnStart = 0;
psDroid->burnDamage = 0;
psDroid->sDisplay.screenX = OFF_SCREEN;
psDroid->sDisplay.screenY = OFF_SCREEN;
psDroid->sDisplay.screenR = 0;

/* Set droid's initial illumination */
psDroid->illumination = UBYTE_MAX;
psDroid->sDisplay.imd = BODY_IMD(psDroid, psDroid->player);

//don't worry if not on homebase cos not being drawn yet
Expand Down
32 changes: 31 additions & 1 deletion src/qtscriptfuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ static QScriptValue js_enumArea(QScriptContext *context, QScriptEngine *engine)
nextparam = 1;
SCRIPT_ASSERT(context, labels.contains(label), "Label %s not found", label.toUtf8().constData());
labeltype p = labels.value(label);
SCRIPT_ASSERT(context, p.type == AREA, "Wrong label type for %s", label.toUtf8().constData());
SCRIPT_ASSERT(context, p.type == SCRIPT_AREA, "Wrong label type for %s", label.toUtf8().constData());
x1 = p.p1.x;
y1 = p.p1.y;
x2 = p.p2.x;
Expand Down Expand Up @@ -2518,6 +2518,35 @@ static QScriptValue js_hackNetOn(QScriptContext *, QScriptEngine *)
return QScriptValue();
}

//-- \subsection{getDroidProduction(factory)}
//-- Return droid in production in given factory. Note that this droid is fully
//-- virtual, and should never be passed anywhere.
static QScriptValue js_getDroidProduction(QScriptContext *context, QScriptEngine *engine)
{
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);
FACTORY *psFactory = &psStruct->pFunctionality->factory;
DROID_TEMPLATE *psTemp = psFactory->psSubject;
if (!psTemp)
{
return QScriptValue::NullValue;
}
DROID sDroid(0, player), *psDroid = &sDroid;
psDroid->pos = psStruct->pos;
psDroid->rot = psStruct->rot;
psDroid->experience = 0;
droidSetName(psDroid, psTemp->aName);
droidSetBits(psTemp, psDroid);
psDroid->weight = calcDroidWeight(psTemp);
psDroid->baseSpeed = calcDroidBaseSpeed(psTemp, psDroid->weight, player);
objSensorCache((BASE_OBJECT *)psDroid, asSensorStats + psTemp->asParts[COMP_SENSOR]);
objEcmCache((BASE_OBJECT *)psDroid, asECMStats + psTemp->asParts[COMP_ECM]);
return convDroid(psDroid, engine);
}

//-- \subsection{getDroidLimit([player[, unit type]])}
//-- Return maximum number of droids that this player can produce. This limit is usually
//-- fixed throughout a game and the same for all players. If no arguments are passed,
Expand Down Expand Up @@ -2601,6 +2630,7 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("safeDest", engine->newFunction(js_safeDest));
engine->globalObject().setProperty("activateStructure", engine->newFunction(js_activateStructure));
engine->globalObject().setProperty("chat", engine->newFunction(js_chat));
engine->globalObject().setProperty("getDroidProduction", engine->newFunction(js_getDroidProduction));
engine->globalObject().setProperty("getDroidLimit", engine->newFunction(js_getDroidLimit));

// Functions that operate on the current player only
Expand Down

0 comments on commit 40fcab0

Please sign in to comment.