Index: binaries/data/mods/public/simulation/components/ResourceTrickle.js =================================================================== --- binaries/data/mods/public/simulation/components/ResourceTrickle.js +++ binaries/data/mods/public/simulation/components/ResourceTrickle.js @@ -11,10 +11,7 @@ ResourceTrickle.prototype.Init = function() { - this.ComputeRates(); - - let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); - cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined); + this.CheckTimer(); }; ResourceTrickle.prototype.GetTimer = function() @@ -30,8 +27,18 @@ ResourceTrickle.prototype.ComputeRates = function() { this.rates = {}; + let hasTrickle = false; for (let resource in this.template.Rates) - this.rates[resource] = ApplyValueModificationsToEntity("ResourceTrickle/Rates/"+resource, +this.template.Rates[resource], this.entity); + { + let rate = ApplyValueModificationsToEntity("ResourceTrickle/Rates/" + resource, +this.template.Rates[resource], this.entity); + if (rate == 0) + continue; + + this.rates[resource] = rate; + hasTrickle = true; + } + + return hasTrickle; }; ResourceTrickle.prototype.Trickle = function(data, lateness) @@ -49,7 +56,35 @@ if (msg.component != "ResourceTrickle") return; - this.ComputeRates(); + this.CheckTimer(); }; +/** + * Checks whether we need a timer to handle the trickling. + * If there are no resource to be trickled there is no point + * in having a timer running. + */ +ResourceTrickle.prototype.CheckTimer = function() +{ + // Check if we need a timer. + if (!hasRates) + { + // We don't need a timer, disable if one exists. + if (this.timer) + { + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + cmpTimer.CancelTimer(this.timer); + delete this.timer; + } + return; + } + + // We need a timer, enable if one doesn't exist. + if (this.timer) + return; + + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + this.timer = cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", interval, interval, undefined); +} + Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle);