Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Trainer.js
Show First 20 Lines • Show All 473 Lines • ▼ Show 20 Lines | Trainer.prototype.CalculateEntitiesMap = function() | ||||
// Tokens can be added -> process an empty list to get them. | // Tokens can be added -> process an empty list to get them. | ||||
let addedTokens = ApplyValueModificationsToEntity("Trainer/Entities/_string", "", this.entity); | let addedTokens = ApplyValueModificationsToEntity("Trainer/Entities/_string", "", this.entity); | ||||
if (!addedTokens && !string) | if (!addedTokens && !string) | ||||
return; | return; | ||||
addedTokens = addedTokens == "" ? [] : addedTokens.split(/\s+/); | addedTokens = addedTokens == "" ? [] : addedTokens.split(/\s+/); | ||||
const cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | const cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | ||||
const cmpPlayer = QueryOwnerInterface(this.entity); | |||||
const disabledEntities = cmpPlayer ? cmpPlayer.GetDisabledTemplates() : {}; | |||||
/** | /** | ||||
* Process tokens: | * Process tokens: | ||||
* - process token modifiers (this is a bit tricky). | * - process token modifiers (this is a bit tricky). | ||||
* - replace the "{civ}" and "{native}" codes with the owner's civ ID and entity's civ ID | * - replace the "{civ}" and "{native}" codes with the owner's civ ID and entity's civ ID | ||||
* - remove disabled entities | * - remove disabled entities | ||||
* - upgrade templates where necessary | * - upgrade templates where necessary | ||||
* This also updates currently queued production (it's more convenient to do it here). | * This also updates currently queued production (it's more convenient to do it here). | ||||
Show All 14 Lines | for (const [id, item] of this.queue) | ||||
if (item.templateName === template) | if (item.templateName === template) | ||||
item.templateName = updateTo; | item.templateName = updateTo; | ||||
}; | }; | ||||
const toks = string.split(/\s+/); | const toks = string.split(/\s+/); | ||||
for (const tok of addedTokens) | for (const tok of addedTokens) | ||||
toks.push(tok); | toks.push(tok); | ||||
const disabledEntities = QueryOwnerInterface(this.entity, IID_TechnologyManager)?.GetDisabledTemplates() || new Set(); | |||||
const nativeCiv = Engine.QueryInterface(this.entity, IID_Identity)?.GetCiv(); | const nativeCiv = Engine.QueryInterface(this.entity, IID_Identity)?.GetCiv(); | ||||
const playerCiv = cmpPlayer?.GetCiv(); | const playerCiv = QueryOwnerInterface(this.entity)?.GetCiv(); | ||||
const addedDict = addedTokens.reduce((out, token) => { out[token] = true; return out; }, {}); | const addedDict = addedTokens.reduce((out, token) => { out[token] = true; return out; }, {}); | ||||
this.entitiesMap = toks.reduce((entMap, token) => { | this.entitiesMap = toks.reduce((entMap, token) => { | ||||
const rawToken = token; | const rawToken = token; | ||||
if (!(token in addedDict)) | if (!(token in addedDict)) | ||||
{ | { | ||||
// This is a bit wasteful but I can't think of a simpler/better way. | // This is a bit wasteful but I can't think of a simpler/better way. | ||||
// The list of token is unlikely to be a performance bottleneck anyways. | // The list of token is unlikely to be a performance bottleneck anyways. | ||||
token = ApplyValueModificationsToEntity("Trainer/Entities/_string", token, this.entity); | token = ApplyValueModificationsToEntity("Trainer/Entities/_string", token, this.entity); | ||||
token = token.split(/\s+/); | token = token.split(/\s+/); | ||||
if (token.every(tok => addedTokens.indexOf(tok) !== -1)) | if (token.every(tok => addedTokens.indexOf(tok) !== -1)) | ||||
{ | { | ||||
removeAllQueuedTemplate(rawToken); | removeAllQueuedTemplate(rawToken); | ||||
return entMap; | return entMap; | ||||
} | } | ||||
token = token[0]; | token = token[0]; | ||||
} | } | ||||
// Replace the "{civ}" and "{native}" codes with the owner's civ ID and entity's civ ID. | // Replace the "{civ}" and "{native}" codes with the owner's civ ID and entity's civ ID. | ||||
if (nativeCiv) | if (nativeCiv) | ||||
token = token.replace(/\{native\}/g, nativeCiv); | token = token.replace(/\{native\}/g, nativeCiv); | ||||
if (playerCiv) | if (playerCiv) | ||||
token = token.replace(/\{civ\}/g, playerCiv); | token = token.replace(/\{civ\}/g, playerCiv); | ||||
// Filter out disabled and invalid entities. | // Filter out disabled and invalid entities. | ||||
if (disabledEntities[token] || !cmpTemplateManager.TemplateExists(token)) | if (disabledEntities.has(token) || !cmpTemplateManager.TemplateExists(token)) | ||||
{ | { | ||||
removeAllQueuedTemplate(rawToken); | removeAllQueuedTemplate(rawToken); | ||||
return entMap; | return entMap; | ||||
} | } | ||||
token = this.GetUpgradedTemplate(token); | token = this.GetUpgradedTemplate(token); | ||||
entMap.set(rawToken, token); | entMap.set(rawToken, token); | ||||
updateAllQueuedTemplate(rawToken, token); | updateAllQueuedTemplate(rawToken, token); | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator