1
0
forked from 0ad/0ad

Remove silencing of resource-not-found errors (that were introduced with an enabled property in the JSON resource files that got removed later). Refs #3934.

This was SVN commit r18977.
This commit is contained in:
elexis 2016-11-21 17:32:17 +00:00
parent 552f7043b0
commit 1b20ac7671
9 changed files with 42 additions and 67 deletions

View File

@ -194,9 +194,8 @@ function GetTemplateDataHelper(template, player, auraTemplates, resources)
if (template.Cost)
{
ret.cost = {};
for (let resCode of resources.GetCodes())
if (template.Cost.Resources[resCode])
ret.cost[resCode] = getEntityValue("Cost/Resources/" + resCode);
for (let resCode in template.Cost.Resources)
ret.cost[resCode] = getEntityValue("Cost/Resources/" + resCode);
if (template.Cost.Population)
ret.cost.population = getEntityValue("Cost/Population");

View File

@ -100,8 +100,7 @@ m.ResearchManager.prototype.researchWantedTechs = function(gameState, techs)
let cost = template.cost;
let costMax = 0;
for (let res in cost)
if (gameState.sharedScript.resourceInfo.codes.indexOf(res) != -1)
costMax = Math.max(costMax, Math.max(cost[res]-available[res], 0));
costMax = Math.max(costMax, Math.max(cost[res]-available[res], 0));
if (10*numWorkers < costMax)
continue;
}

View File

@ -65,14 +65,8 @@ Cost.prototype.GetResourceCosts = function(owner)
let entityTemplate = cmpTemplateManager.GetTemplate(entityTemplateName);
let costs = {};
let resCodes = Resources.GetCodes();
for (let res in this.template.Resources)
{
if (resCodes.indexOf(res) == -1)
continue;
costs[res] = ApplyValueModificationsToTemplate("Cost/Resources/"+res, +this.template.Resources[res], owner, entityTemplate);
}
return costs;
};

View File

@ -198,8 +198,7 @@ Player.prototype.UnBlockTraining = function()
Player.prototype.SetResourceCounts = function(resources)
{
for (let res in resources)
if (this.resourceCount[res])
this.resourceCount[res] = resources[res];
this.resourceCount[res] = resources[res];
};
Player.prototype.GetResourceCounts = function()
@ -288,10 +287,8 @@ Player.prototype.SubtractResourcesOrNotify = function(amounts)
return false;
}
// Subtract the resources
for (var type in amounts)
if (this.resourceCount[type])
this.resourceCount[type] -= amounts[type];
this.resourceCount[type] -= amounts[type];
return true;
};

View File

@ -62,13 +62,13 @@ ProductionQueue.prototype.Init = function()
// "timeTotal": 15000, // msecs
// "timeRemaining": 10000, // msecs
// }
this.timer = undefined; // g_ProgressInterval msec timer, active while the queue is non-empty
this.paused = false;
this.entityCache = [];
this.spawnNotified = false;
this.alertRaiser = undefined;
};
@ -152,7 +152,7 @@ ProductionQueue.prototype.GetTechnologiesList = function()
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (!cmpTechnologyManager)
return [];
var cmpPlayer = QueryOwnerInterface(this.entity);
var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
if (!cmpPlayer || !cmpIdentity)
@ -193,7 +193,7 @@ ProductionQueue.prototype.GetTechnologiesList = function()
{
tech = superseded[tech];
}
techList[i] = tech;
}
@ -231,9 +231,9 @@ ProductionQueue.prototype.IsTechnologyResearchedOrInProgress = function(tech)
{
if (!tech)
return false;
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
var template = cmpTechnologyManager.GetTechnologyTemplate(tech);
if (template.top)
{
@ -259,7 +259,7 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
if (this.queue.length < MAX_QUEUE_SIZE)
{
if (type == "unit")
{
// Find the template data so we can determine the build costs
@ -280,7 +280,7 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
// Apply a time discount to larger batches.
var timeMult = this.GetBatchTime(count);
// We need the costs after tech modifications
// Obviously we don't have the entities yet, so we must use template data
var costs = {};
@ -290,8 +290,6 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
for (let res in template.Cost.Resources)
{
if (resCodes.indexOf(res) == -1)
continue;
costs[res] = ApplyValueModificationsToTemplate("Cost/Resources/"+res, +template.Cost.Resources[res], cmpPlayer.GetPlayerID(), template);
totalCosts[res] = Math.floor(count * costs[res]);
}
@ -322,7 +320,7 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
"timeTotal": time*1000,
"timeRemaining": time*1000,
});
// Call the related trigger event
var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
cmpTrigger.CallEvent("TrainingQueued", {"playerid": cmpPlayer.GetPlayerID(), "unitTemplate": templateName, "count": count, "metadata": metadata, "trainerEntity": this.entity});
@ -350,16 +348,12 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
let cost = {};
for (let res in template.cost)
{
if (resCodes.indexOf(res) == -1)
continue;
cost[res] = Math.floor((techCostMultiplier[res] || 1) * template.cost[res]);
}
// TrySubtractResources should report error to player (they ran out of resources)
if (!cmpPlayer.TrySubtractResources(cost))
return;
// Tell the technology manager that we have started researching this so that people can't research the same
// thing twice.
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
@ -377,7 +371,7 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
"timeTotal": time*1000,
"timeRemaining": time*1000,
});
// Call the related trigger event
var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
cmpTrigger.CallEvent("ResearchQueued", {"playerid": cmpPlayer.GetPlayerID(), "technologyTemplate": templateName, "researcherEntity": this.entity});
@ -387,7 +381,7 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
warn("Tried to add invalid item of type \"" + type + "\" and template \"" + templateName + "\" to a production queue");
return;
}
Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { });
// If this is the first item in the queue, start the timer
@ -417,7 +411,7 @@ ProductionQueue.prototype.RemoveBatch = function(id)
Engine.DestroyEntity(this.entityCache[i]);
}
this.entityCache = [];
for (var i = 0; i < this.queue.length; ++i)
{
var item = this.queue[i];
@ -425,7 +419,7 @@ ProductionQueue.prototype.RemoveBatch = function(id)
continue;
// Now we've found the item to remove
var cmpPlayer = QueryPlayerIDInterface(item.player);
// Update entity count in the EntityLimits component
@ -444,21 +438,19 @@ ProductionQueue.prototype.RemoveBatch = function(id)
// Refund the resource cost for this batch
var totalCosts = {};
var cmpStatisticsTracker = QueryPlayerIDInterface(item.player, IID_StatisticsTracker);
for (let r of Resources.GetCodes())
for (let r in item.resources)
{
if (!item.resources[r])
continue;
totalCosts[r] = Math.floor(item.count * item.resources[r]);
if (cmpStatisticsTracker)
cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -totalCosts[r]);
}
cmpPlayer.AddResources(totalCosts);
// Remove reserved population slots if necessary
if (item.productionStarted && item.unitTemplate)
cmpPlayer.UnReservePopulationSlots(item.population * item.count);
// Mark the research as stopped if we cancel it
if (item.technologyTemplate)
{
@ -466,7 +458,7 @@ ProductionQueue.prototype.RemoveBatch = function(id)
var cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager);
cmpTechnologyManager.StoppedResearch(item.technologyTemplate);
}
// Remove from the queue
// (We don't need to remove the timer - it'll expire if it discovers the queue is empty)
this.queue.splice(i, 1);
@ -571,10 +563,10 @@ ProductionQueue.prototype.SpawnUnits = function(templateName, count, metadata)
var cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
var createdEnts = [];
var spawnedEnts = [];
if (this.entityCache.length == 0)
{
// We need entities to test spawning, but we don't want to waste resources,
@ -642,7 +634,7 @@ ProductionQueue.prototype.SpawnUnits = function(templateName, count, metadata)
// Play a sound, but only for the first in the batch (to avoid nasty phasing effects)
if (createdEnts.length == 0)
PlaySound("trained", ent);
this.entityCache.shift();
createdEnts.push(ent);
}
@ -670,7 +662,7 @@ ProductionQueue.prototype.SpawnUnits = function(templateName, count, metadata)
"owner": cmpOwnership.GetOwner(),
"metadata": metadata,
});
if(this.alertRaiser && spawnedEnts.length > 0)
{
var cmpAlertRaiser = Engine.QueryInterface(this.alertRaiser, IID_AlertRaiser);
@ -678,7 +670,7 @@ ProductionQueue.prototype.SpawnUnits = function(templateName, count, metadata)
cmpAlertRaiser.UpdateUnits(spawnedEnts);
}
}
return createdEnts.length;
};
@ -708,7 +700,7 @@ ProductionQueue.prototype.ProgressTimeout = function(data)
// If something change population cost
var template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate(item.unitTemplate);
item.population = ApplyValueModificationsToTemplate("Cost/Population", +template.Cost.Population, item.player, template);
// Batch's training hasn't started yet.
// Try to reserve the necessary population slots
item.neededSlots = cmpPlayer.TryReservePopulationSlots(item.population * item.count);
@ -721,7 +713,7 @@ ProductionQueue.prototype.ProgressTimeout = function(data)
cmpPlayer.BlockTraining();
break;
}
// Unset flag that training is blocked
cmpPlayer.UnBlockTraining();
}
@ -774,7 +766,7 @@ ProductionQueue.prototype.ProgressTimeout = function(data)
// Some entities failed to spawn
// Set flag that training is blocked
cmpPlayer.BlockTraining();
if (!this.spawnNotified)
{
var cmpPlayer = QueryOwnerInterface(this.entity);
@ -790,19 +782,19 @@ ProductionQueue.prototype.ProgressTimeout = function(data)
{
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
cmpTechnologyManager.ResearchTechnology(item.technologyTemplate);
var template = cmpTechnologyManager.GetTechnologyTemplate(item.technologyTemplate);
if (template && template.soundComplete)
{
var cmpSoundManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager);
if (cmpSoundManager)
cmpSoundManager.PlaySoundGroup(template.soundComplete, this.entity);
}
time -= item.timeRemaining;
this.queue.shift();
Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { });
}

View File

@ -25,8 +25,7 @@ ResourceDropsite.prototype.Init = function()
ResourceDropsite.prototype.GetTypes = function()
{
let types = ApplyValueModificationsToEntity("ResourceDropsite/Types", this.template.Types, this.entity);
let resources = Resources.GetCodes();
return types.split(/\s+/).filter(type => resources.indexOf(type) != -1);
return types.split(/\s+/);
};
/**

View File

@ -42,9 +42,11 @@ ResourceSupply.prototype.Init = function()
{ "subtypes": Resources.GetNames() } :
Resources.GetResource(type);
// Remove entity from gameworld if the resource supplied by this entity is disabled or not valid.
if (!resData || !resData.subtypes[subtype])
{
error("ResourceSupply with invalid resource: " + uneval(resData));
Engine.DestroyEntity(this.entity);
}
this.cachedType = { "generic": type, "specific": subtype };
};

View File

@ -30,14 +30,8 @@ ResourceTrickle.prototype.GetRates = function()
ResourceTrickle.prototype.ComputeRates = function()
{
this.rates = {};
let resCodes = Resources.GetCodes();
for (let resource in this.template.Rates)
{
if (resCodes.indexOf(resource) == -1)
continue;
this.rates[resource] = ApplyValueModificationsToEntity("ResourceTrickle/Rates/"+resource, +this.template.Rates[resource], this.entity);
}
};
ResourceTrickle.prototype.Trickle = function(data, lateness)

View File

@ -373,8 +373,7 @@ StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amo
*/
StatisticsTracker.prototype.IncreaseResourceUsedCounter = function(type, amount)
{
if (typeof this.resourcesUsed[type] === "number")
this.resourcesUsed[type] += amount;
this.resourcesUsed[type] += amount;
};
StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()