0ad/binaries/data/mods/public/simulation/ai/common-api/technology.js
Vantha 280a19587f Define technology pairs in an array
This patch renames the "pair" property of paired technologies to
"partOfPair" for clarity and in the pair parent packs the two techs
(previously "top" and "bottom") into an array called "pair".
This makes the hierarchy more clear in the code and pairs might not be
always placed vertically in the future.
2026-07-06 05:01:12 +02:00

124 lines
2.8 KiB
JavaScript

LoadModificationTemplates();
/** Wrapper around a technology template */
export function Technology(templateName)
{
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;
}
/** returns generic, or specific if civ provided. */
Technology.prototype.name = function(civ)
{
if (civ === undefined)
return this._template.genericName;
if (this._template.specificName === undefined || this._template.specificName[civ] === undefined)
return undefined;
return this._template.specificName[civ];
};
Technology.prototype.pairDef = function()
{
return this._definesPair;
};
/** in case this defines a pair only, returns the two paired technologies. */
Technology.prototype.getPairedTechs = function()
{
if (!this._definesPair)
return undefined;
return this._template.pair.map(name => new Technology(name));
};
Technology.prototype.pair = function()
{
return this._template.partOfPair;
};
Technology.prototype.pairedWith = function()
{
if (!this._template.partOfPair)
return undefined;
return this._pairedWith;
};
Technology.prototype.cost = function(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;
};
Technology.prototype.costSum = function(researcher)
{
const cost = this.cost(researcher);
if (!cost)
return 0;
let ret = 0;
for (const type in cost)
ret += cost[type];
return ret;
};
Technology.prototype.researchTime = function()
{
return this._template.researchTime || 0;
};
Technology.prototype.requirements = function(civ)
{
return DeriveTechnologyRequirements(this._template, civ);
};
Technology.prototype.autoResearch = function()
{
if (!this._template.autoResearch)
return undefined;
return this._template.autoResearch;
};
Technology.prototype.supersedes = function()
{
if (!this._template.supersedes)
return undefined;
return this._template.supersedes;
};
Technology.prototype.modifications = function()
{
if (!this._template.modifications)
return undefined;
return this._template.modifications;
};
Technology.prototype.affects = function()
{
if (!this._template.affects)
return undefined;
return this._template.affects;
};
Technology.prototype.isAffected = function(classes)
{
return this._template.affects && this._template.affects.some(affect => MatchesClassList(classes, affect));
};