Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/reference/common/Lister.js
- This file was added.
/** | |||||
* This class compiles and stores lists of which templates can be built/trained/researched by other templates. | |||||
*/ | |||||
class Lister | |||||
{ | |||||
constructor(Loader) | |||||
{ | |||||
this.Loader = Loader; | |||||
this.templateLists = new Map(); | |||||
} | |||||
/** | |||||
* Compile lists of templates buildable/trainable/researchable of a given civ. | |||||
* | |||||
* @param {object} civCode | |||||
* @param {object} civData - Data defining every civ in the game. | |||||
*/ | |||||
compileTemplateLists(civCode, civData) | |||||
{ | |||||
if (this.hasTemplateLists(civCode)) | |||||
return this.templateLists.get(civCode); | |||||
let templatesToParse = []; | |||||
for (let entity of civData[civCode].StartEntities) | |||||
templatesToParse.push(entity.Template); | |||||
let templateLists = { | |||||
"units": new Map(), | |||||
"structures": new Map(), | |||||
"techs": new Map(), | |||||
"wallsetPieces": new Map() | |||||
}; | |||||
do { | |||||
const templatesThisIteration = templatesToParse; | |||||
templatesToParse = []; | |||||
for (let templateBeingParsed of templatesThisIteration) | |||||
{ | |||||
let list = this.deriveTemplateListsFromTemplate(templateBeingParsed, civCode); | |||||
for (let type in list) | |||||
for (let templateName of list[type]) | |||||
if (!templateLists[type].has(templateName)) | |||||
{ | |||||
templateLists[type].set(templateName, [templateBeingParsed]); | |||||
if (type != "techs") | |||||
templatesToParse.push(templateName); | |||||
} | |||||
else if (templateLists[type].get(templateName).indexOf(templateBeingParsed) == -1) | |||||
templateLists[type].get(templateName).push(templateBeingParsed); | |||||
} | |||||
} while (templatesToParse.length); | |||||
// Expand/filter tech pairs | |||||
for (let [techCode, researcherList] of templateLists.techs) | |||||
{ | |||||
if (!this.Loader.isPairTech(techCode)) | |||||
continue; | |||||
for (let subTech of this.Loader.loadTechnologyPairTemplate(techCode, civCode).techs) | |||||
if (!templateLists.techs.has(subTech)) | |||||
templateLists.techs.set(subTech, researcherList); | |||||
else | |||||
for (let researcher of researcherList) | |||||
if (templateLists.techs.get(subTech).indexOf(researcher) == -1) | |||||
templateLists.techs.get(subTech).push(researcher); | |||||
templateLists.techs.delete(techCode); | |||||
} | |||||
// Remove wallset pieces, as they've served their purpose. | |||||
delete templateLists.wallsetPieces; | |||||
this.templateLists.set(civCode, templateLists); | |||||
return this.templateLists.get(civCode); | |||||
} | |||||
/** | |||||
* Returns a civ's template list. | |||||
* | |||||
* Note: this civ must have gone through the compilation process above! | |||||
* | |||||
* @param {string} civCode | |||||
* @return {object} containing lists of template names, grouped by type. | |||||
*/ | |||||
getTemplateLists(civCode) | |||||
{ | |||||
if (this.hasTemplateLists(civCode)) | |||||
return this.templateLists.get(civCode); | |||||
error("Template lists of \"" + civCode + "\" requested, but this civ has not been loaded."); | |||||
return {}; | |||||
} | |||||
/** | |||||
* Returns whether the civ of the given civCode has been loaded into cache. | |||||
* | |||||
* @param {string} civCode | |||||
* @return {boolean} | |||||
*/ | |||||
hasTemplateLists(civCode) | |||||
{ | |||||
return this.templateLists.has(civCode); | |||||
} | |||||
/** | |||||
* Compiles lists of buildable, trainable, or researchable entities from | |||||
* a named template. | |||||
*/ | |||||
deriveTemplateListsFromTemplate(templateName, civCode) | |||||
{ | |||||
if (!templateName || !Engine.TemplateExists(templateName)) | |||||
return {}; | |||||
// If this is a non-promotion variant (ie. {civ}_support_female_citizen_house) | |||||
// then it is functionally equivalent to another unit being processed, so skip it. | |||||
if (this.Loader.getBaseTemplateName(templateName, civCode) != templateName) | |||||
return {}; | |||||
let template = this.Loader.loadEntityTemplate(templateName, civCode); | |||||
let templateLists = this.Loader.deriveProductionQueue(template, civCode); | |||||
templateLists.structures = this.Loader.deriveBuildQueue(template, civCode); | |||||
if (template.WallSet) | |||||
{ | |||||
templateLists.wallsetPieces = []; | |||||
for (let segment in template.WallSet.Templates) | |||||
{ | |||||
segment = template.WallSet.Templates[segment].replace(/\{(civ|native)\}/g, civCode); | |||||
if (Engine.TemplateExists(segment)) | |||||
templateLists.wallsetPieces.push(segment); | |||||
} | |||||
} | |||||
return templateLists; | |||||
} | |||||
} |
Wildfire Games · Phabricator