0ad/binaries/data/mods/public/gui/session/PanelEntityManager.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

/**
* This class creates a new buttonhandler per entity owned by the currently viewed player,
* or any player if the observer view is selected.
* The buttons are animated when the according entities are attacked, hence they have to keep a state.
*/
class PanelEntityManager
{
constructor(playerViewControl, selection, entityOrder)
{
this.panelEntityButtons = Engine.GetGUIObjectByName("panelEntityButtons").children;
this.selection = selection;
this.entityOrder = entityOrder;
// One handler per panelEntity owned, sorted according to given order, limited to buttoncount
this.handlers = [];
const updater = this.update.bind(this);
registerSimulationUpdateHandler(updater);
playerViewControl.registerViewedPlayerChangeHandler(updater);
}
update()
{
// Obtain entity IDs to display
const entityIDs =
g_ViewedPlayer == -1 ?
g_SimState.players.reduce((ents, pState) => ents.concat(pState.panelEntities), []) :
g_SimState.players[g_ViewedPlayer].panelEntities;
let reposition = false;
// Delete handlers for entities not owned anymore
for (let i = 0; i < this.handlers.length;)
if (entityIDs.indexOf(this.handlers[i].entityID) == -1)
{
this.handlers[i].destroy();
this.handlers.splice(i, 1);
reposition = true;
}
else
++i;
// Construct new handlers
for (const entityID of entityIDs)
if (this.handlers.every(handler => entityID != handler.entityID))
if (this.insertIfRelevant(entityID))
reposition = true;
for (let i = 0; i < this.handlers.length; ++i)
this.handlers[i].update(i, reposition);
}
insertIfRelevant(entityID)
{
const entityState = GetEntityState(entityID);
const orderKey = this.entityOrder.findIndex(entClass =>
Optimise GetEntityState GetEntityState is a performance-critical function in the GUI when a large number of units are selected. The goal of this patch is to increase the efficiency of it without modifying the returned states visible to rest of the GUI in any way (it renames a few properties of entities states or moves them around, but the information they contain and the way to access it remain the complete same) As explained the comments, certain parts (the template data) of an entity state are "predictable" and can be reused between entities with the same template. What one has to account for, however, is that values of the template data can be modified. This happens on two different levels: Firstly, player-wide modifications, which apply to all entities owned by that player. This includes stuff like bonuses from researched techs, civs bonuses, team bonuses. And secondly, entity-local modifications, which apply to individual entities. This includes buffs or debuffs from status effects or auras (of other entities). So we can construct a whole entity state by first just computing the dynamic state (stuff like current hitpoints, which differs from entity to entity) and then adding the (potentially already cached) template data to it, which can be reused between entities with the same template and owning player. And then overwrite the values affected by entity-local modifications, which are usually just a few and even they can be cached and reused for entities with the same owning player, template, and entity-local modifications (identified by the "modifications ID"). This saves the effort of retrieving/computing a ton of data each turn and also saves the time it takes the engine to clone the data from the simulation to the GUI (as the return value of the GUI interface call), which previously took just as long as retrieving the entity states themselves. Since only the dynamic state is read from the components, a number of small getter methods have become unused; they are kept (for now at least) since they might be useful again in the future or for mods.
2026-02-12 06:52:21 -08:00
entityState.identityClasses.indexOf(entClass) != -1);
// Sort depending on given order
const insertPos = this.handlers.reduce(
(pos, handler) =>
{
if (handler.orderKey <= orderKey)
++pos;
return pos;
},
0);
// Don't insert past the button limit
if (insertPos >= this.panelEntityButtons.length)
return false;
// Delete last handler if the limit is reached
if (this.handlers.length == this.panelEntityButtons.length)
{
this.handlers[this.handlers.length - 1].destroy();
this.handlers.splice(this.handlers.length - 1);
}
this.handlers.splice(
insertPos,
0,
new PanelEntity(
this.selection,
entityID,
this.panelEntityButtons.findIndex(button => button.hidden),
orderKey));
return true;
}
}