0ad/binaries/data/mods/public/simulation/ai/common-api/technology.js

128 lines
2.4 KiB
JavaScript
Raw Normal View History

LoadModificationTemplates();
2016-06-09 12:25:40 -07:00
/** Wrapper around a technology template */
2026-07-13 10:47:21 -07:00
export class Technology
{
2026-07-13 10:47:21 -07:00
constructor(templateName)
{
2026-07-13 10:47:21 -07:00
this._templateName = templateName;
const template = TechnologyTemplates.Get(templateName);
// check if this is one of two paired technologies.
if (template.partOfPair)
{
const parentTech = TechnologyTemplates.Get(template.partOfPair);
this._pairedWith = parentTech.pair[0] == templateName ? parentTech.pair[1] : parentTech.pair[0];
}
// check if it only defines a pair:
this._definesPair = !!template.pair;
this._template = template;
}
2026-07-13 10:47:21 -07:00
/** returns generic, or specific if civ provided. */
name(civ)
{
if (civ === undefined)
return this._template.genericName;
2026-07-13 10:47:21 -07:00
if (this._template.specificName === undefined || this._template.specificName[civ] === undefined)
return undefined;
return this._template.specificName[civ];
}
2026-07-13 10:47:21 -07:00
pairDef()
{
return this._definesPair;
}
2026-07-13 10:47:21 -07:00
/** in case this defines a pair only, returns the two paired technologies. */
getPairedTechs()
{
if (!this._definesPair)
return undefined;
2026-07-13 10:47:21 -07:00
return this._template.pair.map(name => new Technology(name));
}
2026-07-13 10:47:21 -07:00
pair()
{
return this._template.partOfPair;
}
2026-07-13 10:47:21 -07:00
pairedWith()
{
if (!this._template.partOfPair)
return undefined;
return this._pairedWith;
}
2026-07-13 10:47:21 -07:00
cost(researcher)
{
if (!this._template.cost)
return undefined;
const cost = {};
for (const type in this._template.cost)
{
cost[type] = +this._template.cost[type];
if (researcher)
cost[type] *= researcher.techCostMultiplier(type);
}
return cost;
}
2026-07-13 10:47:21 -07:00
costSum(researcher)
{
2026-07-13 10:47:21 -07:00
const cost = this.cost(researcher);
if (!cost)
return 0;
let ret = 0;
for (const type in cost)
ret += cost[type];
return ret;
}
2026-07-13 10:47:21 -07:00
researchTime()
{
return this._template.researchTime || 0;
}
2026-07-13 10:47:21 -07:00
requirements(civ)
{
return DeriveTechnologyRequirements(this._template, civ);
}
2026-07-13 10:47:21 -07:00
autoResearch()
{
if (!this._template.autoResearch)
return undefined;
return this._template.autoResearch;
}
2026-07-13 10:47:21 -07:00
supersedes()
{
if (!this._template.supersedes)
return undefined;
return this._template.supersedes;
}
2026-07-13 10:47:21 -07:00
modifications()
{
if (!this._template.modifications)
return undefined;
return this._template.modifications;
}
2026-07-13 10:47:21 -07:00
affects()
{
if (!this._template.affects)
return undefined;
return this._template.affects;
}
2026-07-13 10:47:21 -07:00
isAffected(classes)
{
return this._template.affects && this._template.affects.some(affect => MatchesClassList(classes, affect));
}
}