Remove selectableStance from entity states

The array was wastefully created and passed to the GUI every single
turn for every single selected unit.
Since the GUI defines the (selectable) stances' names and tooltips, it
already knew them anyway, so they don't even need to be passed from the
simulation to it in the first place.
This commit is contained in:
Vantha 2026-08-07 10:47:30 +02:00
parent 0a3bd16cb5
commit 072ad01816
No known key found for this signature in database
GPG key ID: 3F5D02FA4D3E8E74
5 changed files with 34 additions and 69 deletions

View file

@ -1192,17 +1192,18 @@ g_SelectionPanels.Stance = {
},
"getItems": function(unitEntStates)
{
if (unitEntStates.some(state => !state.unitAI || !hasClass(state, "Unit") || hasClass(state, "Animal")))
if (unitEntStates.some(state => !state.unitAI || !hasClass(state, "Unit") || hasClass(state, "Animal")) ||
unitEntStates.every(state => state.turretable && state.turretable.holder !== INVALID_ENTITY))
return [];
return unitEntStates[0].unitAI.selectableStances;
return Object.keys(this.stancesData);
},
"setupButton": function(data)
{
const unitIds = data.unitEntStates.map(state => state.id);
data.button.onPress = function() { performStance(unitIds, data.item); };
data.button.tooltip = getStanceDisplayName(data.item) + "\n" + bodyFont(getStanceTooltip(data.item));
data.button.tooltip = this.stancesData[data.item].Name + "\n" + bodyFont(this.stancesData[data.item].Tooltip);
data.guiSelection.hidden = !Engine.GuiInterfaceCall("IsStanceSelected", {
"ents": unitIds,
@ -1213,6 +1214,28 @@ g_SelectionPanels.Stance = {
setPanelObjectPosition(data.button, data.i, data.rowLength);
return true;
},
"stancesData": {
"violent": {
"Name": translateWithContext("stance", "Violent"),
"Tooltip": translateWithContext("stance", "Attack nearby opponents, focus on attackers and chase while visible")
},
"aggressive": {
"Name": translateWithContext("stance", "Aggressive"),
"Tooltip": translateWithContext("stance", "Attack nearby opponents")
},
"defensive": {
"Name": translateWithContext("stance", "Defensive"),
"Tooltip": translateWithContext("stance", "Attack nearby opponents, chase a short distance and return to the original location")
},
"passive": {
"Name": translateWithContext("stance", "Passive"),
"Tooltip": translateWithContext("stance", "Flee if attacked")
},
"standground": {
"Name": translateWithContext("stance", "Standground"),
"Tooltip": translateWithContext("stance", "Attack opponents in range, but don't move")
}
}
};

View file

@ -45,45 +45,6 @@ function resourcesToAlphaMask(neededResources)
return "color:255 0 0 " + Math.min(125, Math.round(+totalCost / 10) + 50);
}
function getStanceDisplayName(name)
{
switch (name)
{
case "violent":
return translateWithContext("stance", "Violent");
case "aggressive":
return translateWithContext("stance", "Aggressive");
case "defensive":
return translateWithContext("stance", "Defensive");
case "passive":
return translateWithContext("stance", "Passive");
case "standground":
return translateWithContext("stance", "Standground");
default:
warn("Internationalization: Unexpected stance found: " + name);
return name;
}
}
function getStanceTooltip(name)
{
switch (name)
{
case "violent":
return translateWithContext("stance", "Attack nearby opponents, focus on attackers and chase while visible");
case "aggressive":
return translateWithContext("stance", "Attack nearby opponents");
case "defensive":
return translateWithContext("stance", "Attack nearby opponents, chase a short distance and return to the original location");
case "passive":
return translateWithContext("stance", "Flee if attacked");
case "standground":
return translateWithContext("stance", "Attack opponents in range, but don't move");
default:
return "";
}
}
/**
* Format entity count/limit message for the tooltip
*/

View file

@ -86,8 +86,7 @@ var g_Stances = {
"respondChase": true,
"respondChaseBeyondVision": true,
"respondStandGround": false,
"respondHoldGround": false,
"selectable": true
"respondHoldGround": false
},
"aggressive": {
"targetVisibleEnemies": true,
@ -97,8 +96,7 @@ var g_Stances = {
"respondChase": true,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": false,
"selectable": true
"respondHoldGround": false
},
"defensive": {
"targetVisibleEnemies": true,
@ -108,8 +106,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": true,
"selectable": true
"respondHoldGround": true
},
"passive": {
"targetVisibleEnemies": false,
@ -119,8 +116,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": false,
"selectable": true
"respondHoldGround": false
},
"standground": {
"targetVisibleEnemies": true,
@ -130,8 +126,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": true,
"respondHoldGround": false,
"selectable": true
"respondHoldGround": false
},
"skittish": {
"targetVisibleEnemies": false,
@ -141,8 +136,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": false,
"selectable": false
"respondHoldGround": false
},
"passive-defensive": {
"targetVisibleEnemies": false,
@ -152,8 +146,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": true,
"selectable": false
"respondHoldGround": true
},
"none": {
// Only to be used by AI or trigger scripts
@ -164,8 +157,7 @@ var g_Stances = {
"respondChase": false,
"respondChaseBeyondVision": false,
"respondStandGround": false,
"respondHoldGround": false,
"selectable": false
"respondHoldGround": false
}
};
@ -6665,13 +6657,6 @@ UnitAI.prototype.GetStance = function()
return g_Stances[this.stance];
};
UnitAI.prototype.GetSelectableStances = function()
{
if (this.IsTurret())
return [];
return Object.keys(g_Stances).filter(key => g_Stances[key].selectable);
};
UnitAI.prototype.GetStanceName = function()
{
return this.stance;

View file

@ -549,7 +549,6 @@ AddMockToEnts([TEST_ENTITY_UNIT_1, TEST_ENTITY_UNIT_2], IID_UnitAI, {
"GetOrders": () => [{ "type": "Walk", "data": { "x": 11, "z": 22, "force": true, "relaxed": true } }],
"HasWorkOrders": () => true,
"IsGuardOf": () => false,
"GetSelectableStances": () => ["stance1", "stance2", "stance3", "stance4", "stance5"],
"IsIdle": () => true,
"GetFormationController": () => FORMATION_CONTROLLER_ENT_ID
});
@ -599,7 +598,6 @@ const expectedDynamicStates = {
"orders": [{ "type": "Walk", "data": { "x": 11, "z": 22, "force": true, "relaxed": true } }],
"hasWorkOrders": true,
"isGuarding": false,
"selectableStances": ["stance1", "stance2", "stance3", "stance4", "stance5"],
"isIdle": true,
"formation": FORMATION_CONTROLLER_ENT_ID
},
@ -623,7 +621,6 @@ const expectedDynamicStates = {
"orders": [{ "type": "Walk", "data": { "x": 11, "z": 22, "force": true, "relaxed": true } }],
"hasWorkOrders": true,
"isGuarding": false,
"selectableStances": ["stance1", "stance2", "stance3", "stance4", "stance5"],
"isIdle": true,
"formation": FORMATION_CONTROLLER_ENT_ID
},

View file

@ -317,7 +317,6 @@ class EntityStateRetriever
"orders": cmpUnitAI.GetOrders(),
"hasWorkOrders": cmpUnitAI.HasWorkOrders(),
"isGuarding": cmpUnitAI.IsGuardOf(),
"selectableStances": cmpUnitAI.GetSelectableStances(),
"isIdle": cmpUnitAI.IsIdle(),
"formation": cmpUnitAI.GetFormationController()
};