Support decrementing training batches, show batch size on icons.

This was SVN commit r13208.
This commit is contained in:
Deiz 2013-02-28 00:20:16 +00:00
parent 3c50352134
commit c16b93794a
4 changed files with 57 additions and 18 deletions

View File

@ -1588,6 +1588,10 @@ function addTrainingToQueue(selection, trainEntType, playerState)
// Batch training possible if we can train at least 2 units
var batchTrainingPossible = canBeTrainedCount == undefined || canBeTrainedCount > 1;
var decrement = Engine.HotkeyIsPressed("selection.remove");
if (!decrement)
var template = GetTemplateData(trainEntType);
if (Engine.HotkeyIsPressed("session.batchtrain") && batchTrainingPossible)
{
if (inputState == INPUT_BATCHTRAINING)
@ -1608,19 +1612,37 @@ function addTrainingToQueue(selection, trainEntType, playerState)
// (if training limits allow)
if (sameEnts && batchTrainingType == trainEntType)
{
if (canBeTrainedCount == undefined ||
if (decrement)
{
batchTrainingCount -= batchIncrementSize;
if (batchTrainingCount <= 0)
inputState = INPUT_NORMAL;
}
else if (canBeTrainedCount == undefined ||
canBeTrainedCount > batchTrainingCount * appropriateBuildings.length)
{
if (Engine.GuiInterfaceCall("GetNeededResources", multiplyEntityCosts(
template, batchTrainingCount + batchIncrementSize)))
return;
batchTrainingCount += batchIncrementSize;
}
batchTrainingEntityAllowedCount = canBeTrainedCount;
return;
}
// Otherwise start a new one
else
else if (!decrement)
{
flushTrainingBatch();
// fall through to create the new batch
}
}
// Don't start a new batch if decrementing or unable to afford it.
if (decrement || Engine.GuiInterfaceCall("GetNeededResources",
multiplyEntityCosts(template, batchIncrementSize)))
return;
inputState = INPUT_BATCHTRAINING;
batchTrainingEntities = selection;
batchTrainingType = trainEntType;
@ -1653,10 +1675,13 @@ function getTrainingBatchStatus(playerState, entity, trainEntType, selection)
if (selection && selection.indexOf(entity) != -1)
appropriateBuildings = getBuildingsWhichCanTrainEntity(selection, trainEntType);
var nextBatchTrainingCount = 0;
var currentBatchTrainingCount = 0;
if (inputState == INPUT_BATCHTRAINING && batchTrainingEntities.indexOf(entity) != -1 &&
batchTrainingType == trainEntType)
{
nextBatchTrainingCount = batchTrainingCount;
currentBatchTrainingCount = batchTrainingCount;
var canBeTrainedCount = batchTrainingEntityAllowedCount;
}
else
@ -1679,7 +1704,7 @@ function getTrainingBatchStatus(playerState, entity, trainEntType, selection)
buildingsCountToTrainFullBatch = Math.floor(canBeTrainedCount / nextBatchTrainingCount);
remainderToTrain = canBeTrainedCount % nextBatchTrainingCount;
}
return [buildingsCountToTrainFullBatch, nextBatchTrainingCount, remainderToTrain];
return [buildingsCountToTrainFullBatch, nextBatchTrainingCount, remainderToTrain, currentBatchTrainingCount];
}
// Called by GUI when user clicks production queue item

View File

@ -1028,6 +1028,7 @@
<object name="unitTrainingButton[n]" hidden="true" style="iconButton" type="button" size="0 0 46 46" tooltip_style="sessionToolTipBottom">
<object name="unitTrainingIcon[n]" type="image" ghost="true" size="3 3 43 43"/>
<object name="unitTrainingUnaffordable[n]" hidden="true" type="image" ghost="true" size="3 3 43 43" sprite="colour: 255 0 0 127"/>
<object name="unitTrainingCount[n]" ghost="true" style="groupIconsText" type="text" z="20"/>
</object>
</repeat>
</object>

View File

@ -732,6 +732,10 @@ function setupUnitPanel(guiName, usedPanels, unitEntState, playerState, items, c
}
else if (guiName == CONSTRUCTION || guiName == TRAINING)
{
affordableMask.hidden = true;
var totalCosts = {};
var trainNum = 1;
var button_disableable = true;
if (guiName == TRAINING)
{
var trainingCategory = null;
@ -741,22 +745,20 @@ function setupUnitPanel(guiName, usedPanels, unitEntState, playerState, items, c
playerState.entityCounts[trainingCategory] >= playerState.entityLimits[trainingCategory])
grayscale = "grayscale:";
icon.sprite = "stretched:" + grayscale + "session/portraits/" + template.icon;
}
affordableMask.hidden = true;
var totalCosts = {};
var trainNum = 1;
if (Engine.HotkeyIsPressed("session.batchtrain") && guiName == TRAINING)
{
var [buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch] =
getTrainingBatchStatus(playerState, unitEntState.id, entType, selection);
trainNum = buildingsCountToTrainFullBatch * fullBatchSize + remainderBatch;
if (Engine.HotkeyIsPressed("session.batchtrain"))
{
var [buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch, batchTrainingCount] =
getTrainingBatchStatus(playerState, unitEntState.id, entType, selection);
trainNum = buildingsCountToTrainFullBatch * fullBatchSize + remainderBatch;
button_disableable = !Engine.HotkeyIsPressed("selection.remove");
}
getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (batchTrainingCount > 0) ? batchTrainingCount : "";
}
// Walls have no cost defined.
if (template.cost !== undefined)
for (var r in template.cost)
totalCosts[r] = Math.floor(template.cost[r] * trainNum);
totalCosts = multiplyEntityCosts(template, trainNum);
var neededResources = Engine.GuiInterfaceCall("GetNeededResources", totalCosts);
if (neededResources)
@ -767,7 +769,7 @@ function setupUnitPanel(guiName, usedPanels, unitEntState, playerState, items, c
for each (var resource in neededResources)
totalCost += resource;
button.enabled = false;
button.enabled = (button_disableable ? false : true);
affordableMask.hidden = false;
var alpha = 75 + totalCost/6;
alpha = alpha > 150 ? 150 : alpha;

View File

@ -259,16 +259,27 @@ function getCostComponentDisplayName(costComponentName)
return COST_DISPLAY_NAMES[costComponentName];
}
/**
* Multiplies the costs for a template by a given batch size.
*/
function multiplyEntityCosts(template, trainNum)
{
var totalCosts = {};
for (var r in template.cost)
totalCosts[r] = Math.floor(template.cost[r] * trainNum);
return totalCosts;
}
/**
* Helper function for getEntityCostTooltip.
*/
function getEntityCostComponentsTooltipString(template, trainNum, entity)
{
var totalCosts = {};
if (!trainNum)
trainNum = 1;
for (var r in template.cost)
totalCosts[r] = Math.floor(template.cost[r] * trainNum);
var totalCosts = multiplyEntityCosts(template, trainNum);
totalCosts.time = Math.ceil(template.cost.time * (entity ? Engine.GuiInterfaceCall("GetBatchTime", {"entity": entity, "batchSize": trainNum}) : 1));
var costs = [];