forked from 0ad/0ad
cache quantities depending only on the selection, adresses #2179
This was SVN commit r14296.
This commit is contained in:
parent
2f3ce82dc3
commit
9678cf547a
@ -1689,7 +1689,7 @@ function addTrainingByPosition(position)
|
||||
if (!selection.length)
|
||||
return;
|
||||
|
||||
var trainableEnts = getAllTrainableEntities(selection);
|
||||
var trainableEnts = getAllTrainableEntitiesFromSelection();
|
||||
|
||||
// Check if the position is valid
|
||||
if (!trainableEnts.length || trainableEnts.length <= position)
|
||||
|
@ -217,6 +217,7 @@ EntitySelection.prototype.getTemplateNames = function()
|
||||
*/
|
||||
EntitySelection.prototype.update = function()
|
||||
{
|
||||
var changed = false;
|
||||
this.checkRenamedEntities();
|
||||
for each (var ent in this.selected)
|
||||
{
|
||||
@ -227,7 +228,7 @@ EntitySelection.prototype.update = function()
|
||||
{
|
||||
delete this.selected[ent];
|
||||
this.groups.removeEnt(ent);
|
||||
this.dirty = true;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -241,10 +242,12 @@ EntitySelection.prototype.update = function()
|
||||
|
||||
delete this.selected[ent];
|
||||
this.groups.removeEnt(ent);
|
||||
this.dirty = true;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (changed)
|
||||
this.onChange();
|
||||
};
|
||||
|
||||
/**
|
||||
@ -318,7 +321,7 @@ EntitySelection.prototype.addList = function(ents, quiet)
|
||||
}
|
||||
|
||||
this.groups.add(this.toList()); // Create Selection Groups
|
||||
this.dirty = true;
|
||||
this.onChange();
|
||||
};
|
||||
|
||||
EntitySelection.prototype.removeList = function(ents)
|
||||
@ -339,7 +342,7 @@ EntitySelection.prototype.removeList = function(ents)
|
||||
_setStatusBars(removed, false);
|
||||
_setMotionOverlay(removed, false);
|
||||
|
||||
this.dirty = true;
|
||||
this.onChange();
|
||||
};
|
||||
|
||||
EntitySelection.prototype.reset = function()
|
||||
@ -349,7 +352,7 @@ EntitySelection.prototype.reset = function()
|
||||
_setMotionOverlay(this.toList(), false);
|
||||
this.selected = {};
|
||||
this.groups.reset();
|
||||
this.dirty = true;
|
||||
this.onChange();
|
||||
};
|
||||
|
||||
EntitySelection.prototype.rebuildSelection = function(renamed)
|
||||
@ -407,8 +410,32 @@ EntitySelection.prototype.SetMotionDebugOverlay = function(enabled)
|
||||
_setMotionOverlay(this.toList(), enabled);
|
||||
};
|
||||
|
||||
EntitySelection.prototype.onChange = function()
|
||||
{
|
||||
this.dirty = true;
|
||||
if (this == g_Selection)
|
||||
onSelectionChange();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache some quantities which depends only on selection
|
||||
*/
|
||||
|
||||
var g_Selection = new EntitySelection();
|
||||
|
||||
var g_canMoveIntoFormation = {};
|
||||
var g_allBuildableEntities = undefined;
|
||||
var g_allTrainableEntities = undefined;
|
||||
|
||||
// Reset cached quantities
|
||||
function onSelectionChange()
|
||||
{
|
||||
g_canMoveIntoFormation = {};
|
||||
g_allBuildableEntities = undefined;
|
||||
g_allTrainableEntities = undefined;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* EntityGroupsContainer class for managing grouped entities
|
||||
*/
|
||||
|
@ -332,6 +332,8 @@ function onTick()
|
||||
// simulation state and the current selection).
|
||||
if (g_Selection.dirty)
|
||||
{
|
||||
g_Selection.dirty = false;
|
||||
|
||||
onSimulationUpdate();
|
||||
|
||||
// Display rally points for selected buildings
|
||||
@ -432,7 +434,6 @@ function changeGameSpeed(speed)
|
||||
*/
|
||||
function onSimulationUpdate()
|
||||
{
|
||||
g_Selection.dirty = false;
|
||||
g_EntityStates = {};
|
||||
g_TemplateData = {};
|
||||
g_TechnologyData = {};
|
||||
|
@ -572,11 +572,7 @@ function setupUnitPanel(guiName, usedPanels, unitEntState, playerState, items, c
|
||||
// Get icon image
|
||||
if (guiName == FORMATION)
|
||||
{
|
||||
var formationOk = Engine.GuiInterfaceCall("CanMoveEntsIntoFormation", {
|
||||
"ents": g_Selection.toList(),
|
||||
"formationName": item
|
||||
});
|
||||
|
||||
var formationOk = canMoveSelectionIntoFormation(item);
|
||||
var grayscale = "";
|
||||
button.enabled = formationOk;
|
||||
if (!formationOk)
|
||||
@ -1069,8 +1065,8 @@ function updateUnitCommands(entState, supplementalDetailsPanel, commandsPanel, s
|
||||
setupUnitBarterPanel(entState, playerState);
|
||||
}
|
||||
|
||||
var buildableEnts = getAllBuildableEntities(selection);
|
||||
var trainableEnts = getAllTrainableEntities(selection);
|
||||
var buildableEnts = getAllBuildableEntitiesFromSelection();
|
||||
var trainableEnts = getAllTrainableEntitiesFromSelection();
|
||||
|
||||
// Whether the GUI's right panel has been filled.
|
||||
var rightUsed = true;
|
||||
@ -1272,6 +1268,14 @@ function getAllTrainableEntities(selection)
|
||||
return trainableEnts;
|
||||
}
|
||||
|
||||
function getAllTrainableEntitiesFromSelection()
|
||||
{
|
||||
if (!g_allTrainableEntities)
|
||||
g_allTrainableEntities = getAllTrainableEntities(g_Selection.toList());
|
||||
|
||||
return g_allTrainableEntities;
|
||||
}
|
||||
|
||||
// Get all of the available entities which can be built by the selected entities
|
||||
function getAllBuildableEntities(selection)
|
||||
{
|
||||
@ -1288,3 +1292,24 @@ function getAllBuildableEntities(selection)
|
||||
removeDupes(buildableEnts);
|
||||
return buildableEnts;
|
||||
}
|
||||
|
||||
function getAllBuildableEntitiesFromSelection()
|
||||
{
|
||||
if (!g_allBuildableEntities)
|
||||
g_allBuildableEntities = getAllBuildableEntities(g_Selection.toList());
|
||||
|
||||
return g_allBuildableEntities;
|
||||
}
|
||||
|
||||
// Check if the selection can move into formation, and cache the result
|
||||
function canMoveSelectionIntoFormation(formationName)
|
||||
{
|
||||
if (!(formationName in g_canMoveIntoFormation))
|
||||
{
|
||||
g_canMoveIntoFormation[formationName] = Engine.GuiInterfaceCall("CanMoveEntsIntoFormation", {
|
||||
"ents": g_Selection.toList(),
|
||||
"formationName": formationName
|
||||
});
|
||||
}
|
||||
return g_canMoveIntoFormation[formationName];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user