Add player to GetTemplateData calls where necessary

If no player is provided then the function falls back to the viewed
player, which is sometimes not the right one (like for observers), which
resulted in inconsistent/incorrect data being returned. This patch fixes
that.
This commit is contained in:
Vantha 2026-02-21 17:35:27 +01:00 committed by Vantha
parent 705207d1bb
commit 6deca026b3
No known key found for this signature in database
GPG key ID: 3F5D02FA4D3E8E74
6 changed files with 16 additions and 13 deletions

View file

@ -38,7 +38,7 @@ class DeveloperOverlayEntityState
const entState = GetEntityState(selection[0]); const entState = GetEntityState(selection[0]);
if (entState) if (entState)
{ {
const template = GetTemplateData(entState.template); const template = GetTemplateData(entState.template, entState.player);
text += "\n\nentity: {\n"; text += "\n\nentity: {\n";
for (const k in entState) for (const k in entState)
text += " " + k + ":" + uneval(entState[k]) + "\n"; text += " " + k + ":" + uneval(entState[k]) + "\n";

View file

@ -1555,7 +1555,7 @@ function getEntityLimitAndCount(playerState, entType)
}; };
if (!playerState.entityLimits) if (!playerState.entityLimits)
return ret; return ret;
const template = GetTemplateData(entType); const template = GetTemplateData(entType, playerState.id);
let entCategory; let entCategory;
let matchLimit; let matchLimit;
if (template.trainingRestrictions) if (template.trainingRestrictions)

View file

@ -83,7 +83,7 @@ EntityGroups.prototype.add = function(ents)
continue; continue;
var templateName = entState.template; var templateName = entState.template;
var key = GetTemplateData(templateName).selectionGroupName || templateName; var key = GetTemplateData(templateName, entState.player).selectionGroupName || templateName;
// Group the ents by player and template // Group the ents by player and template
if (entState.player !== undefined) if (entState.player !== undefined)

View file

@ -344,7 +344,7 @@ g_SelectionPanels.Garrison = {
{ {
const entState = GetEntityState(data.item.ents[0]); const entState = GetEntityState(data.item.ents[0]);
const template = GetTemplateData(entState.template); const template = GetTemplateData(entState.template, entState.player);
if (!template) if (!template)
return false; return false;
@ -566,7 +566,7 @@ g_SelectionPanels.Queue = {
// Differentiate between units and techs // Differentiate between units and techs
let template; let template;
if (queuedItem.unitTemplate) if (queuedItem.unitTemplate)
template = GetTemplateData(queuedItem.unitTemplate); template = GetTemplateData(queuedItem.unitTemplate, data.player);
else if (queuedItem.technologyTemplate) else if (queuedItem.technologyTemplate)
template = GetTechnologyData(queuedItem.technologyTemplate, GetSimState().players[data.player].civ); template = GetTechnologyData(queuedItem.technologyTemplate, GetSimState().players[data.player].civ);
else else
@ -1048,7 +1048,8 @@ g_SelectionPanels.Research = {
{ {
showTemplateDetails( showTemplateDetails(
t, t,
GetTemplateData(baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).template).nativeCiv); GetTemplateData(baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).template, state.player).nativeCiv
);
}); });
button.onPressRight = showTemplateFunc(techName); button.onPressRight = showTemplateFunc(techName);
@ -1113,7 +1114,7 @@ g_SelectionPanels.Selection = {
"setupButton": function(data) "setupButton": function(data)
{ {
const entState = GetEntityState(data.item.ents[0]); const entState = GetEntityState(data.item.ents[0]);
const template = GetTemplateData(entState.template); const template = GetTemplateData(entState.template, entState.player);
if (!template) if (!template)
return false; return false;
@ -1340,7 +1341,7 @@ g_SelectionPanels.Upgrade = {
}, },
"setupButton": function(data) "setupButton": function(data)
{ {
const template = GetTemplateData(data.item.entity); const template = GetTemplateData(data.item.entity, data.player);
if (!template) if (!template)
return false; return false;
@ -1421,7 +1422,7 @@ g_SelectionPanels.Upgrade = {
}; };
if (!requirementsMet || limits.canBeAddedCount == 0 && if (!requirementsMet || limits.canBeAddedCount == 0 &&
!upgradableEntStates.some(state => hasSameRestrictionCategory(data.item.entity, state.template))) !upgradableEntStates.some(state => hasSameRestrictionCategory(data.item.entity, state.template, state.player)))
{ {
data.button.enabled = false; data.button.enabled = false;
modifier = "color:0 0 0 127:grayscale:"; modifier = "color:0 0 0 127:grayscale:";

View file

@ -19,10 +19,10 @@ function canMoveSelectionIntoFormation(formationTemplate)
return g_canMoveIntoFormation[formationTemplate]; return g_canMoveIntoFormation[formationTemplate];
} }
function hasSameRestrictionCategory(templateName1, templateName2) function hasSameRestrictionCategory(templateName1, templateName2, player)
{ {
const template1 = GetTemplateData(templateName1); const template1 = GetTemplateData(templateName1, player);
const template2 = GetTemplateData(templateName2); const template2 = GetTemplateData(templateName2, player);
if (template1.trainingRestrictions && template2.trainingRestrictions) if (template1.trainingRestrictions && template2.trainingRestrictions)
return template1.trainingRestrictions.category == template2.trainingRestrictions.category; return template1.trainingRestrictions.category == template2.trainingRestrictions.category;

View file

@ -443,6 +443,7 @@ function updatePlayerData()
playerData.push({ playerData.push({
"name": playerState.name, "name": playerState.name,
"id": i,
"civ": playerState.civ, "civ": playerState.civ,
"color": { "color": {
"r": playerState.color.r * 255, "r": playerState.color.r * 255,
@ -727,7 +728,8 @@ function updateGroups()
// Determine the sum of the costs of a given template // Determine the sum of the costs of a given template
const getCostSum = (ent) => const getCostSum = (ent) =>
{ {
const cost = GetTemplateData(GetEntityState(ent).template).cost; const entState = GetEntityState(ent);
const cost = GetTemplateData(entState.template, entState.player).cost;
return cost ? Object.keys(cost).map(key => cost[key]).reduce((sum, cur) => sum + cur) : 0; return cost ? Object.keys(cost).map(key => cost[key]).reduce((sum, cur) => sum + cur) : 0;
}; };