1
0
forked from 0ad/0ad

Fix Trainer/Researcher without entities/techs.

It was explicitly allowed in 0c4f59d0a7, but not all references to
`this.template` were properly checked.

Noticed by: @nwtour
Differential revision: https://code.wildfiregames.com/D4357
Tested by: @nwtour
Comments by: @Stan
This was SVN commit r26043.
This commit is contained in:
Freagarach 2021-12-09 16:22:52 +00:00
parent a53405f697
commit 695ce382ec
4 changed files with 22 additions and 11 deletions

View File

@ -218,11 +218,7 @@ Researcher.prototype.Deserialize = function(data)
*/
Researcher.prototype.GetTechnologiesList = function()
{
if (!this.template.Technologies)
return [];
const string = ApplyValueModificationsToEntity("Researcher/Technologies/_string", this.template.Technologies._string, this.entity);
const string = ApplyValueModificationsToEntity("Researcher/Technologies/_string", this.template?.Technologies?._string || "", this.entity);
if (!string)
return [];
@ -309,7 +305,7 @@ Researcher.prototype.GetTechnologiesList = function()
Researcher.prototype.GetTechCostMultiplier = function()
{
const techCostMultiplier = {};
for (const res in this.template.TechCostMultiplier)
for (const res in this.template?.TechCostMultiplier)
techCostMultiplier[res] = ApplyValueModificationsToEntity(
"Researcher/TechCostMultiplier/" + res,
+this.template.TechCostMultiplier[res],

View File

@ -461,10 +461,8 @@ Trainer.prototype.CalculateEntitiesMap = function()
// Don't reset the map, it's used below to update entities.
if (!this.entitiesMap)
this.entitiesMap = new Map();
if (!this.template.Entities)
return;
const string = this.template.Entities._string;
const string = this.template?.Entities?._string || "";
// Tokens can be added -> process an empty list to get them.
let addedTokens = ApplyValueModificationsToEntity("Trainer/Entities/_string", "", this.entity);
if (!addedTokens && !string)
@ -577,7 +575,7 @@ Trainer.prototype.GetUpgradedTemplate = function(templateName)
Trainer.prototype.GetTrainCostMultiplier = function()
{
const trainCostMultiplier = {};
for (const res in this.template.TrainCostMultiplier)
for (const res in this.template?.TrainCostMultiplier)
trainCostMultiplier[res] = ApplyValueModificationsToEntity(
"Trainer/TrainCostMultiplier/" + res,
+this.template.TrainCostMultiplier[res],
@ -594,7 +592,7 @@ Trainer.prototype.GetBatchTime = function(batchSize)
// TODO: work out what equation we should use here.
return Math.pow(batchSize, ApplyValueModificationsToEntity(
"Trainer/BatchTimeModifier",
+(this.template.BatchTimeModifier || 1),
+(this.template?.BatchTimeModifier || 1),
this.entity));
};

View File

@ -151,3 +151,11 @@ spyTechManager = new Spy(techManager, "ResearchTechnology");
TS_ASSERT_EQUALS(cmpResearcher.Progress(id, 1000), 500);
TS_ASSERT_EQUALS(spyTechManager._called, 1);
TS_ASSERT_EQUALS(cmpResearcher.queue.size, 0);
// Test that we can affect an empty researcher.
Engine.RegisterGlobal("ApplyValueModificationsToEntity", (_, value) => value + "some_test");
TS_ASSERT_UNEVAL_EQUALS(
ConstructComponent(entityID, "Researcher", null).GetTechnologiesList(),
["some_test"]
);

View File

@ -299,3 +299,12 @@ TS_ASSERT_UNEVAL_EQUALS(
TS_ASSERT_EQUALS(cmpTrainer.queue.size, 1);
TS_ASSERT_EQUALS(cmpTrainer.GetBatch(id1), undefined);
TS_ASSERT_EQUALS(cmpTrainer.GetBatch(id2).unitTemplate, "units/iber/c");
// Test that we can affect an empty trainer.
const emptyTrainer = ConstructComponent(entityID, "Trainer", null);
emptyTrainer.OnValueModification({ "component": "Trainer", "entities": [entityID], "valueNames": ["Trainer/Entities/"] });
TS_ASSERT_UNEVAL_EQUALS(
emptyTrainer.GetEntitiesList(),
["units/iber/d"]
);