From 072ad018166d09b9de5691412d40bda1153f9485 Mon Sep 17 00:00:00 2001 From: Vantha Date: Fri, 7 Aug 2026 10:47:30 +0200 Subject: [PATCH] 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. --- .../public/gui/session/selection_panels.js | 29 ++++++++++++-- .../gui/session/selection_panels_helpers.js | 39 ------------------- .../public/simulation/components/UnitAI.js | 31 ++++----------- .../components/tests/test_EntityStates.js | 3 -- .../helpers/EntityStateRetriever.js | 1 - 5 files changed, 34 insertions(+), 69 deletions(-) diff --git a/binaries/data/mods/public/gui/session/selection_panels.js b/binaries/data/mods/public/gui/session/selection_panels.js index 23a3551397..70d9c7b3e0 100644 --- a/binaries/data/mods/public/gui/session/selection_panels.js +++ b/binaries/data/mods/public/gui/session/selection_panels.js @@ -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") + } } }; diff --git a/binaries/data/mods/public/gui/session/selection_panels_helpers.js b/binaries/data/mods/public/gui/session/selection_panels_helpers.js index a2a84d87d9..0853c7c997 100644 --- a/binaries/data/mods/public/gui/session/selection_panels_helpers.js +++ b/binaries/data/mods/public/gui/session/selection_panels_helpers.js @@ -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 */ diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index 6a64a8e28a..c8deddaa3a 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -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; diff --git a/binaries/data/mods/public/simulation/components/tests/test_EntityStates.js b/binaries/data/mods/public/simulation/components/tests/test_EntityStates.js index 70a1b259e5..a790d2c1c6 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_EntityStates.js +++ b/binaries/data/mods/public/simulation/components/tests/test_EntityStates.js @@ -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 }, diff --git a/binaries/data/mods/public/simulation/helpers/EntityStateRetriever.js b/binaries/data/mods/public/simulation/helpers/EntityStateRetriever.js index be96756cca..4a49e52cbb 100644 --- a/binaries/data/mods/public/simulation/helpers/EntityStateRetriever.js +++ b/binaries/data/mods/public/simulation/helpers/EntityStateRetriever.js @@ -317,7 +317,6 @@ class EntityStateRetriever "orders": cmpUnitAI.GetOrders(), "hasWorkOrders": cmpUnitAI.HasWorkOrders(), "isGuarding": cmpUnitAI.IsGuardOf(), - "selectableStances": cmpUnitAI.GetSelectableStances(), "isIdle": cmpUnitAI.IsIdle(), "formation": cmpUnitAI.GetFormationController() };