Index: binaries/data/mods/public/simulation/components/ProductionQueue.js =================================================================== --- binaries/data/mods/public/simulation/components/ProductionQueue.js +++ binaries/data/mods/public/simulation/components/ProductionQueue.js @@ -110,7 +110,11 @@ let template = cmpTemplateManager.GetTemplate(templateName); while (template && template.Promotion !== undefined) { - let requiredXp = ApplyValueModificationsToTemplate("Promotion/RequiredXp", +template.Promotion.RequiredXp, cmpPlayer.GetPlayerID(), template); + let requiredXp = ApplyValueModificationsToTemplate( + "Promotion/RequiredXp", + +template.Promotion.RequiredXp, + cmpPlayer.GetPlayerID(), + template); if (requiredXp > 0) break; templateName = template.Promotion.Entity; @@ -135,20 +139,20 @@ if (!this.template.Technologies) return []; - var string = this.template.Technologies._string; + let string = this.template.Technologies._string; if (!string) return []; - var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); + let cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); if (!cmpTechnologyManager) return []; - var cmpPlayer = QueryOwnerInterface(this.entity); - var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); + let cmpPlayer = QueryOwnerInterface(this.entity); + let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); if (!cmpPlayer || !cmpIdentity) return []; - var techs = string.split(/\s+/); + let techs = string.split(/\s+/); // Replace the civ specific technologies for (let i = 0; i < techs.length; ++i) @@ -166,16 +170,16 @@ DeriveTechnologyRequirements(TechnologyTemplates.Get(tech), cmpPlayer.GetCiv()), true)); - var techList = []; - var superseded = {}; // Stores the tech which supersedes the key + let techList = []; + let superseded = {}; // Stores the tech which supersedes the key - var disabledTechnologies = cmpPlayer.GetDisabledTechnologies(); + let disabledTechnologies = cmpPlayer.GetDisabledTechnologies(); // Add any top level technologies to an array which corresponds to the displayed icons // Also store what a technology is superceded by in the superceded object {"tech1":"techWhichSupercedesTech1", ...} - for (var i in techs) + for (let i in techs) { - var tech = techs[i]; + let tech = techs[i]; if (disabledTechnologies && disabledTechnologies[tech]) continue; @@ -187,21 +191,21 @@ } // Now make researched/in progress techs invisible - for (var i in techList) + for (let i in techList) { - var tech = techList[i]; + let tech = techList[i]; while (this.IsTechnologyResearchedOrInProgress(tech)) tech = superseded[tech]; techList[i] = tech; } - var ret = []; + let ret = []; // This inserts the techs into the correct positions to line up the technology pairs - for (var i = 0; i < techList.length; i++) + for (let i = 0; i < techList.length; ++i) { - var tech = techList[i]; + let tech = techList[i]; if (!tech) { ret[i] = undefined; @@ -222,7 +226,11 @@ { let techCostMultiplier = {}; for (let res in this.template.TechCostMultiplier) - techCostMultiplier[res] = ApplyValueModificationsToEntity("ProductionQueue/TechCostMultiplier/"+res, +this.template.TechCostMultiplier[res], this.entity); + techCostMultiplier[res] = ApplyValueModificationsToEntity( + "ProductionQueue/TechCostMultiplier/" + res, + +this.template.TechCostMultiplier[res], + this.entity); + return techCostMultiplier; }; @@ -231,12 +239,14 @@ if (!tech) return false; - var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); + let cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); let template = TechnologyTemplates.Get(tech); if (template.top) - return cmpTechnologyManager.IsTechnologyResearched(template.top) || cmpTechnologyManager.IsInProgress(template.top) || - cmpTechnologyManager.IsTechnologyResearched(template.bottom) || cmpTechnologyManager.IsInProgress(template.bottom); + return cmpTechnologyManager.IsTechnologyResearched(template.top) || + cmpTechnologyManager.IsInProgress(template.top) || + cmpTechnologyManager.IsTechnologyResearched(template.bottom) || + cmpTechnologyManager.IsInProgress(template.bottom); return cmpTechnologyManager.IsTechnologyResearched(tech) || cmpTechnologyManager.IsInProgress(tech); }; @@ -267,7 +277,13 @@ let template = cmpTemplateManager.GetTemplate(templateName); if (!template) return; - if (template.Promotion && ApplyValueModificationsToTemplate("Promotion/RequiredXp", +template.Promotion.RequiredXp, cmpPlayer.GetPlayerID(), template) == 0) + if (template.Promotion && + !ApplyValueModificationsToTemplate( + "Promotion/RequiredXp", + +template.Promotion.RequiredXp, + cmpPlayer.GetPlayerID(), + template) + ) { this.AddBatch(template.Promotion.Entity, type, count, metadata); return; @@ -280,12 +296,22 @@ // Obviously we don't have the entities yet, so we must use template data let costs = {}; let totalCosts = {}; - let buildTime = ApplyValueModificationsToTemplate("Cost/BuildTime", +template.Cost.BuildTime, cmpPlayer.GetPlayerID(), template); + let buildTime = ApplyValueModificationsToTemplate( + "Cost/BuildTime", + +template.Cost.BuildTime, + cmpPlayer.GetPlayerID(), + template); + let time = timeMult * buildTime; for (let res in template.Cost.Resources) { - costs[res] = ApplyValueModificationsToTemplate("Cost/Resources/"+res, +template.Cost.Resources[res], cmpPlayer.GetPlayerID(), template); + costs[res] = ApplyValueModificationsToTemplate( + "Cost/Resources/" + res, + +template.Cost.Resources[res], + cmpPlayer.GetPlayerID(), + template); + totalCosts[res] = Math.floor(count * costs[res]); } @@ -308,15 +334,25 @@ "count": count, "metadata": metadata, "resources": costs, - "population": ApplyValueModificationsToTemplate("Cost/Population", +template.Cost.Population, cmpPlayer.GetPlayerID(), template), + "population": ApplyValueModificationsToTemplate( + "Cost/Population", + +template.Cost.Population, + cmpPlayer.GetPlayerID(), + template), "productionStarted": false, - "timeTotal": time*1000, - "timeRemaining": time*1000, + "timeTotal": time * 1000, + "timeRemaining": time * 1000, }); // Call the related trigger event let cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); - cmpTrigger.CallEvent("TrainingQueued", { "playerid": cmpPlayer.GetPlayerID(), "unitTemplate": templateName, "count": count, "metadata": metadata, "trainerEntity": this.entity }); + cmpTrigger.CallEvent("TrainingQueued", { + "playerid": cmpPlayer.GetPlayerID(), + "unitTemplate": templateName, + "count": count, + "metadata": metadata, + "trainerEntity": this.entity, + }); } else if (type == "technology") { @@ -349,26 +385,30 @@ // thing twice. let cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); cmpTechnologyManager.QueuedResearch(templateName, this.entity); - if (this.queue.length == 0) + if (!this.queue.length) { cmpTechnologyManager.StartedResearch(templateName, false); this.SetAnimation("researching"); } this.queue.push({ - "id": this.nextID++, - "player": cmpPlayer.GetPlayerID(), - "count": 1, - "technologyTemplate": templateName, - "resources": cost, - "productionStarted": false, - "timeTotal": time*1000, - "timeRemaining": time*1000, + "id": this.nextID++, + "player": cmpPlayer.GetPlayerID(), + "count": 1, + "technologyTemplate": templateName, + "resources": cost, + "productionStarted": false, + "timeTotal": time * 1000, + "timeRemaining": time * 1000, }); // Call the related trigger event let cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); - cmpTrigger.CallEvent("ResearchQueued", { "playerid": cmpPlayer.GetPlayerID(), "technologyTemplate": templateName, "researcherEntity": this.entity }); + cmpTrigger.CallEvent("ResearchQueued", { + "playerid": cmpPlayer.GetPlayerID(), + "technologyTemplate": templateName, + "researcherEntity": this.entity, + }); } else { @@ -376,7 +416,7 @@ return; } - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); // If this is the first item in the queue, start the timer if (!this.timer) @@ -387,9 +427,12 @@ } else { - let notification = { "players": [cmpPlayer.GetPlayerID()], "message": markForTranslation("The production queue is full."), "translateMessage": true }; let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); - cmpGUIInterface.PushNotification(notification); + cmpGUIInterface.PushNotification({ + "players": [cmpPlayer.GetPlayerID()], + "message": markForTranslation("The production queue is full."), + "translateMessage": true, + }); } }; @@ -405,32 +448,32 @@ this.entityCache = []; - for (var i = 0; i < this.queue.length; ++i) + for (let i = 0; i < this.queue.length; ++i) { - var item = this.queue[i]; + let item = this.queue[i]; if (item.id != id) continue; // Now we've found the item to remove - var cmpPlayer = QueryPlayerIDInterface(item.player); + let cmpPlayer = QueryPlayerIDInterface(item.player); // Update entity count in the EntityLimits component if (item.unitTemplate) { - var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); - var template = cmpTemplateManager.GetTemplate(item.unitTemplate); + let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); + let template = cmpTemplateManager.GetTemplate(item.unitTemplate); if (template.TrainingRestrictions) { - var unitCategory = template.TrainingRestrictions.Category; - var cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits); + let unitCategory = template.TrainingRestrictions.Category; + let cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits); cmpPlayerEntityLimits.ChangeCount(unitCategory, -item.count); } } // Refund the resource cost for this batch - var totalCosts = {}; - var cmpStatisticsTracker = QueryPlayerIDInterface(item.player, IID_StatisticsTracker); + let totalCosts = {}; + let cmpStatisticsTracker = QueryPlayerIDInterface(item.player, IID_StatisticsTracker); for (let r in item.resources) { totalCosts[r] = Math.floor(item.count * item.resources[r]); @@ -448,7 +491,7 @@ if (item.technologyTemplate) { // item.player is used as this.entity's owner may be invalid (deletion, etc.) - var cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager); + let cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager); cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true); this.SetAnimation("idle"); } @@ -456,7 +499,7 @@ // 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); - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); return; } @@ -467,28 +510,23 @@ let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); if (cmpVisual) cmpVisual.SelectAnimation(name, false, 1); -} +}; /* * Returns basic data from all batches in the production queue. */ ProductionQueue.prototype.GetQueue = function() { - var out = []; - for (var item of this.queue) - { - out.push({ - "id": item.id, - "unitTemplate": item.unitTemplate, - "technologyTemplate": item.technologyTemplate, - "count": item.count, - "neededSlots": item.neededSlots, - "progress": 1 - (item.timeRemaining / (item.timeTotal || 1)), - "timeRemaining": item.timeRemaining, - "metadata": item.metadata, - }); - } - return out; + return this.queue.map(item => ({ + "id": item.id, + "unitTemplate": item.unitTemplate, + "technologyTemplate": item.technologyTemplate, + "count": item.count, + "neededSlots": item.neededSlots, + "progress": 1 - (item.timeRemaining / (item.timeTotal || 1)), + "timeRemaining": item.timeRemaining, + "metadata": item.metadata, + })); }; /* @@ -510,7 +548,10 @@ ProductionQueue.prototype.GetBatchTime = function(batchSize) { // TODO: work out what equation we should use here. - return Math.pow(batchSize, ApplyValueModificationsToEntity("ProductionQueue/BatchTimeModifier", +this.template.BatchTimeModifier, this.entity)); + return Math.pow(batchSize, ApplyValueModificationsToEntity( + "ProductionQueue/BatchTimeModifier", + +this.template.BatchTimeModifier, + this.entity)); }; ProductionQueue.prototype.OnOwnershipChanged = function(msg) @@ -518,8 +559,8 @@ if (msg.from != INVALID_PLAYER) { // Unset flag that previous owner's training may be blocked - var cmpPlayer = QueryPlayerIDInterface(msg.from); - if (cmpPlayer && this.queue.length > 0) + let cmpPlayer = QueryPlayerIDInterface(msg.from); + if (cmpPlayer && this.queue.length) cmpPlayer.UnBlockTraining(); } if (msg.to != INVALID_PLAYER) @@ -545,7 +586,7 @@ if (this.timer) { - var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); cmpTimer.CancelTimer(this.timer); } }; @@ -560,13 +601,16 @@ let cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); let cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint); + let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); + let cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); + let cmpPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); let createdEnts = []; let spawnedEnts = []; // We need entities to test spawning, but we don't want to waste resources, // so only create them once and use as needed - if (this.entityCache.length == 0) + if (!this.entityCache.length) for (let i = 0; i < count; ++i) this.entityCache.push(Engine.AddEntity(templateName)); @@ -607,7 +651,6 @@ let cmpNewPosition = Engine.QueryInterface(ent, IID_Position); cmpNewPosition.JumpTo(pos.x, pos.z); - let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (cmpPosition) cmpNewPosition.SetYRotation(cmpPosition.GetPosition().horizAngleTo(pos)); @@ -622,25 +665,23 @@ if (cmpTrainingRestrictions) { let unitCategory = cmpTrainingRestrictions.GetCategory(); - let cmpPlayerEntityLimits = QueryOwnerInterface(this.entity, IID_EntityLimits); cmpPlayerEntityLimits.ChangeCount(unitCategory, -1); } cmpNewOwnership.SetOwner(cmpOwnership.GetOwner()); - let cmpPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); if (cmpPlayerStatisticsTracker) cmpPlayerStatisticsTracker.IncreaseTrainedUnitsCounter(ent); // Play a sound, but only for the first in the batch (to avoid nasty phasing effects) - if (createdEnts.length == 0) + if (!createdEnts.length) PlaySound("trained", ent); this.entityCache.shift(); createdEnts.push(ent); } - if (spawnedEnts.length > 0 && !cmpAutoGarrison) + if (spawnedEnts.length && !cmpAutoGarrison) { // If a rally point is set, walk towards it (in formation) using a suitable command based on where the // rally point is placed. @@ -656,11 +697,11 @@ } } - if (createdEnts.length > 0) + if (createdEnts.length) Engine.PostMessage(this.entity, MT_TrainingFinished, { - "entities": createdEnts, - "owner": cmpOwnership.GetOwner(), - "metadata": metadata, + "entities": createdEnts, + "owner": cmpOwnership.GetOwner(), + "metadata": metadata, }); return createdEnts.length; @@ -678,20 +719,25 @@ // Allocate the 1000msecs to as many queue items as it takes // until we've used up all the time (so that we work accurately // with items that take fractions of a second) - var time = g_ProgressInterval; - var cmpPlayer = QueryOwnerInterface(this.entity); + let time = g_ProgressInterval; + let cmpPlayer = QueryOwnerInterface(this.entity); + let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); while (time > 0 && this.queue.length) { - var item = this.queue[0]; + let item = this.queue[0]; if (!item.productionStarted) { // If the item is a unit then do population checks if (item.unitTemplate) { // 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); + let template = cmpTemplateManager.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 @@ -713,7 +759,7 @@ if (item.technologyTemplate) { // Mark the research as started. - var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); + let cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); cmpTechnologyManager.StartedResearch(item.technologyTemplate, true); this.SetAnimation("researching"); } @@ -728,13 +774,13 @@ { item.timeRemaining -= time; // send a message for the AIs. - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); break; } if (item.unitTemplate) { - var numSpawned = this.SpawnUnits(item.unitTemplate, item.count, item.metadata); + let numSpawned = this.SpawnUnits(item.unitTemplate, item.count, item.metadata); if (numSpawned == item.count) { // All entities spawned, this batch finished @@ -744,7 +790,7 @@ // Unset flag that training is blocked cmpPlayer.UnBlockTraining(); this.spawnNotified = false; - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); } else { @@ -753,7 +799,7 @@ // Only partially finished cmpPlayer.UnReservePopulationSlots(item.population * numSpawned); item.count -= numSpawned; - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); } // Some entities failed to spawn @@ -762,10 +808,12 @@ if (!this.spawnNotified) { - var cmpPlayer = QueryOwnerInterface(this.entity); - var notification = { "players": [cmpPlayer.GetPlayerID()], "message": markForTranslation("Can't find free space to spawn trained units"), "translateMessage": true }; - var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); - cmpGUIInterface.PushNotification(notification); + let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); + cmpGUIInterface.PushNotification({ + "players": [cmpPlayer.GetPlayerID()], + "message": markForTranslation("Can't find free space to spawn trained units"), + "translateMessage": true, + }); this.spawnNotified = true; } break; @@ -773,13 +821,13 @@ } else if (item.technologyTemplate) { - var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); + let cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager); cmpTechnologyManager.ResearchTechnology(item.technologyTemplate); this.SetAnimation("idle"); let template = TechnologyTemplates.Get(item.technologyTemplate); if (template && template.soundComplete) { - var cmpSoundManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager); + let cmpSoundManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager); if (cmpSoundManager) cmpSoundManager.PlaySoundGroup(template.soundComplete, this.entity); @@ -788,12 +836,12 @@ time -= item.timeRemaining; this.queue.shift(); - Engine.PostMessage(this.entity, MT_ProductionQueueChanged, { }); + Engine.PostMessage(this.entity, MT_ProductionQueueChanged, {}); } } // If the queue's empty, delete the timer, else repeat it - if (this.queue.length == 0) + if (!this.queue.length) { this.timer = undefined; @@ -803,7 +851,7 @@ } else { - var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); this.timer = cmpTimer.SetTimeout(this.entity, IID_ProductionQueue, "ProgressTimeout", g_ProgressInterval, data); } }; @@ -817,7 +865,7 @@ ProductionQueue.prototype.UnpauseProduction = function() { this.paused = false; - var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); this.timer = cmpTimer.SetTimeout(this.entity, IID_ProductionQueue, "ProgressTimeout", g_ProgressInterval, {}); };