Simplify GetTemplateData calls

Previously it was made so that "template data" was a sort of super type
of "entity state" and that the former could always be replaced by the
latter. Using this we can simplify some code passages by removing
GetTemplateData calls and using the entity's state directly instead.
This commit is contained in:
Vantha 2026-02-21 18:03:34 +01:00 committed by Vantha
parent 60970320e3
commit 8c36cc4879
No known key found for this signature in database
GPG key ID: 3F5D02FA4D3E8E74
6 changed files with 24 additions and 32 deletions

View file

@ -30,8 +30,7 @@ class PanelEntity
this.panelEntButton.hidden = false;
const entityState = GetEntityState(entityID);
const template = GetTemplateData(entityState.templateName);
this.nameTooltip = setStringTags(g_SpecificNamesPrimary ? template.name.specific : template.name.generic, this.NameTags) + "\n";
this.nameTooltip = setStringTags(g_SpecificNamesPrimary ? entityState.name.specific : entityState.name.generic, this.NameTags) + "\n";
Engine.GetGUIObjectByName("panelEntityHealthSection[" + buttonID + "]").hidden = !entityState.hitpoints;
@ -45,7 +44,7 @@ class PanelEntity
}
Engine.GetGUIObjectByName("panelEntityImage[" + buttonID + "]").sprite =
"stretched:" + this.PortraitDirectory + template.icon;
"stretched:" + this.PortraitDirectory + entityState.icon;
}
destroy()

View file

@ -74,7 +74,7 @@ EntityGroups.prototype.add = function(ents)
{
if (this.ents[ent])
continue;
var entState = GetEntityState(ent);
const entState = GetEntityState(ent);
// When this function is called during group rebuild, deleted
// entities will not yet have been removed, so entities might
@ -82,8 +82,7 @@ EntityGroups.prototype.add = function(ents)
if (!entState)
continue;
var templateName = entState.templateName;
var key = GetTemplateData(templateName, entState.player).selectionGroupName || templateName;
let key = entState.selectionGroupName || entState.templateName;
// Group the ents by player and template
if (entState.player !== undefined)

View file

@ -48,15 +48,13 @@ function updateGarrisonHealthBar(entState, selection)
// Fills out information that most entities have
function displaySingle(entState)
{
const template = GetTemplateData(entState.templateName);
const primaryName = g_SpecificNamesPrimary ? template.name.specific : template.name.generic;
const primaryName = g_SpecificNamesPrimary ? entState.name.specific : entState.name.generic;
let secondaryName;
if (g_ShowSecondaryNames)
secondaryName = g_SpecificNamesPrimary ? template.name.generic : template.name.specific;
secondaryName = g_SpecificNamesPrimary ? entState.name.generic : entState.name.specific;
// If packed, add that to the generic name (reduces template clutter).
if (template.pack && template.pack.state == "packed")
if (entState.pack?.state === "packed")
{
if (secondaryName && g_ShowSecondaryNames)
secondaryName = sprintf(translate("%(secondaryName)s — Packed"), { "secondaryName": secondaryName });
@ -327,8 +325,8 @@ function displaySingle(entState)
}
// TODO: we should require all entities to have icons
Engine.GetGUIObjectByName("icon").sprite = template.icon ? ("stretched:session/portraits/" + template.icon) : "BackgroundBlack";
if (template.icon)
Engine.GetGUIObjectByName("icon").sprite = entState.icon ? ("stretched:session/portraits/" + entState.icon) : "BackgroundBlack";
if (entState.icon)
{
const iconBorder = Engine.GetGUIObjectByName("iconBorder");
@ -373,7 +371,7 @@ function displaySingle(entState)
getAurasTooltip,
getEntityTooltip,
getTreasureTooltip
].map(func => func(template)));
].map(func => func(entState)));
const leftClickTooltip = hasClass(entState, "Unit") ? getFollowOnLeftClickTooltip() : getFocusOnLeftClickTooltip();
iconTooltips.push(leftClickTooltip + " " + getTemplateViewerOnRightClickTooltip());

View file

@ -343,14 +343,12 @@ g_SelectionPanels.Garrison = {
"setupButton": function(data)
{
const entState = GetEntityState(data.item.ents[0]);
const template = GetTemplateData(entState.templateName, entState.player);
if (!template)
if (!entState)
return false;
data.button.onPress = function()
{
unloadTemplate(template.selectionGroupName || entState.templateName, entState.player);
unloadTemplate(entState.selectionGroupName || entState.templateName, entState.player);
};
data.countDisplay.caption = data.item.ents.length > 1 ? data.item.ents.length : "";
@ -360,9 +358,9 @@ g_SelectionPanels.Garrison = {
data.button.enabled = canUngarrison;
data.button.tooltip = (canUngarrison ?
sprintf(translate("Unload %(name)s"), { "name": getEntityNames(template) }) + "\n" +
sprintf(translate("Unload %(name)s"), { "name": getEntityNames(entState) }) + "\n" +
translate("Single-click to unload 1. Shift-click to unload all of this type.") :
getEntityNames(template)) + "\n" +
getEntityNames(entState)) + "\n" +
sprintf(translate("Player: %(playername)s"), {
"playername": g_Players[entState.player].name
});
@ -374,7 +372,7 @@ g_SelectionPanels.Garrison = {
// also appear disabled to the owner of the structure.
data.icon.sprite =
(canUngarrison || g_IsObserver ? "" : "grayscale:") +
"stretched:session/portraits/" + template.icon;
"stretched:session/portraits/" + entState.icon;
setPanelObjectPosition(data.button, data.i, data.rowLength);
@ -1048,7 +1046,7 @@ g_SelectionPanels.Research = {
{
showTemplateDetails(
t,
GetTemplateData(baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).templateName, state.player).nativeCiv
baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).nativeCiv
);
});
@ -1114,8 +1112,7 @@ g_SelectionPanels.Selection = {
"setupButton": function(data)
{
const entState = GetEntityState(data.item.ents[0]);
const template = GetTemplateData(entState.templateName, entState.player);
if (!template)
if (!entState)
return false;
for (const ent of data.item.ents)
@ -1152,7 +1149,7 @@ g_SelectionPanels.Selection = {
}
const unitOwner = GetEntityState(data.item.ents[0]).player;
let tooltip = getEntityNames(template);
let tooltip = getEntityNames(entState);
if (data.carried)
tooltip += "\n" + Object.keys(data.carried).map(res =>
resourceIcon(res) + data.carried[res]
@ -1177,8 +1174,8 @@ g_SelectionPanels.Selection = {
};
data.button.onPressRight = function() { removeFromSelectionGroup(data.item.key); };
if (template.icon)
data.icon.sprite = "stretched:session/portraits/" + template.icon;
if (entState.icon)
data.icon.sprite = "stretched:session/portraits/" + entState.icon;
setPanelObjectPosition(data.button, data.i, data.rowLength);
return true;

View file

@ -824,8 +824,7 @@ function updateGroups()
// Determine the sum of the costs of a given template
const getCostSum = (ent) =>
{
const entState = GetEntityState(ent);
const cost = GetTemplateData(entState.templateName, entState.player).cost;
const cost = GetEntityState(ent).cost;
return cost ? Object.keys(cost).map(key => cost[key]).reduce((sum, cur) => sum + cur) : 0;
};
@ -842,12 +841,12 @@ function updateGroups()
// Choose the icon of the most common template (or the most costly if it's not unique)
if (g_Groups.groups[i].getTotalCount() > 0)
{
const icon = GetTemplateData(GetEntityState(g_Groups.groups[i].getEntsGrouped().reduce((pre, cur) =>
const icon = GetEntityState(g_Groups.groups[i].getEntsGrouped().reduce((pre, cur) =>
{
if (pre.ents.length == cur.ents.length)
return getCostSum(pre.ents[0]) > getCostSum(cur.ents[0]) ? pre : cur;
return pre.ents.length > cur.ents.length ? pre : cur;
}).ents[0]).templateName).icon;
}).ents[0]).icon;
Engine.GetGUIObjectByName("unitGroupIcon[" + i + "]").sprite =
icon ? ("stretched:session/portraits/" + icon) : "groupsIcon";

View file

@ -1250,7 +1250,7 @@ var g_UnitActions =
let trader;
if (entState.trainer?.entities?.length)
for (let i = 0; i < entState.trainer.entities.length; ++i)
if ((trader = GetTemplateData(entState.trainer.entities[i]).trader))
if ((trader = entState.trainer.entities[i].trader))
break;
const traderData = {