From 692e2364996f4cfe2c0242ec07b2af14fe8275e2 Mon Sep 17 00:00:00 2001 From: wraitii Date: Sun, 17 Jan 2021 18:29:11 +0000 Subject: [PATCH] Speed up carry capacity tech research / resource value modification. Carry capacity changes triggered 4x recomputation of gather rates (because modifiersManager isn't as efficient as it can be), and this was slow enough to matter. This reduces the work sufficiently to not matter much. Reviewed By: Freagarach Differential Revision: https://code.wildfiregames.com/D3373 This was SVN commit r24670. --- .../simulation/components/ResourceGatherer.js | 35 +++++++++++++++---- .../components/tests/test_ResourceGatherer.js | 3 +- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/ResourceGatherer.js b/binaries/data/mods/public/simulation/components/ResourceGatherer.js index f9176617c8..6a06e64508 100644 --- a/binaries/data/mods/public/simulation/components/ResourceGatherer.js +++ b/binaries/data/mods/public/simulation/components/ResourceGatherer.js @@ -104,7 +104,7 @@ ResourceGatherer.prototype.SetLastCarriedType = function(lastCarriedType) }; // Since this code is very performancecritical and applying technologies quite slow, cache it. -ResourceGatherer.prototype.RecalculateGatherRatesAndCapacities = function() +ResourceGatherer.prototype.RecalculateGatherRates = function() { this.baseSpeed = ApplyValueModificationsToEntity("ResourceGatherer/BaseSpeed", +this.template.BaseSpeed, this.entity); @@ -122,12 +122,21 @@ ResourceGatherer.prototype.RecalculateGatherRatesAndCapacities = function() let rate = ApplyValueModificationsToEntity("ResourceGatherer/Rates/" + r, +this.template.Rates[r], this.entity); this.rates[r] = rate * this.baseSpeed; } +}; +ResourceGatherer.prototype.RecalculateCapacities = function() +{ this.capacities = {}; for (let r in this.template.Capacities) this.capacities[r] = ApplyValueModificationsToEntity("ResourceGatherer/Capacities/" + r, +this.template.Capacities[r], this.entity); }; +ResourceGatherer.prototype.RecalculateCapacity = function(type) +{ + if (type in this.capacities) + this.capacities[type] = ApplyValueModificationsToEntity("ResourceGatherer/Capacities/" + type, +this.template.Capacities[type], this.entity); +}; + ResourceGatherer.prototype.GetGatherRates = function() { return this.rates; @@ -305,7 +314,7 @@ ResourceGatherer.prototype.CommitResources = function(target) if (!cmpResourceDropsite) return; - let change = cmpResourceDropsite.ReceiveResources(this.carrying, this.entity) + let change = cmpResourceDropsite.ReceiveResources(this.carrying, this.entity); let changed = false; for (let type in change) { @@ -373,7 +382,19 @@ ResourceGatherer.prototype.OnValueModification = function(msg) if (msg.component != "ResourceGatherer") return; - this.RecalculateGatherRatesAndCapacities(); + // NB: at the moment, 0 A.D. always uses the fast path, the other is mod support. + if (msg.valueNames.length === 1) + { + if (msg.valueNames[0].indexOf("Capacities") !== -1) + this.RecalculateCapacity(msg.valueNames[0].substr(28)); + else + this.RecalculateGatherRates(); + } + else + { + this.RecalculateGatherRates(); + this.RecalculateCapacities(); + } }; ResourceGatherer.prototype.OnOwnershipChanged = function(msg) @@ -384,19 +405,21 @@ ResourceGatherer.prototype.OnOwnershipChanged = function(msg) return; } - this.RecalculateGatherRatesAndCapacities(); + this.RecalculateGatherRates(); + this.RecalculateCapacities(); }; ResourceGatherer.prototype.OnGlobalInitGame = function(msg) { - this.RecalculateGatherRatesAndCapacities(); + this.RecalculateGatherRates(); + this.RecalculateCapacities(); }; ResourceGatherer.prototype.OnMultiplierChanged = function(msg) { let cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); if (cmpPlayer && msg.player == cmpPlayer.GetPlayerID()) - this.RecalculateGatherRatesAndCapacities(); + this.RecalculateGatherRates(); }; Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer); diff --git a/binaries/data/mods/public/simulation/components/tests/test_ResourceGatherer.js b/binaries/data/mods/public/simulation/components/tests/test_ResourceGatherer.js index 96dfa01eb2..26f9b7ad5a 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_ResourceGatherer.js +++ b/binaries/data/mods/public/simulation/components/tests/test_ResourceGatherer.js @@ -43,7 +43,8 @@ let template = { }; let cmpResourceGatherer = ConstructComponent(entity, "ResourceGatherer", template); -cmpResourceGatherer.RecalculateGatherRatesAndCapacities(); +cmpResourceGatherer.RecalculateGatherRates(); +cmpResourceGatherer.RecalculateCapacities(); TS_ASSERT_UNEVAL_EQUALS(cmpResourceGatherer.GetCarryingStatus(), []); cmpResourceGatherer.GiveResources([{ "type": "food", "amount": 11 }]);