LCOV - code coverage report
Current view: top level - simulation/components - ResourceTrickle.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 49 54 90.7 %
Date: 2023-04-02 12:52:40 Functions: 8 9 88.9 %

          Line data    Source code
       1             : function ResourceTrickle() {}
       2             : 
       3           1 : ResourceTrickle.prototype.Schema =
       4             :         "<a:help>Controls the resource trickle ability of the unit.</a:help>" +
       5             :         "<element name='Rates' a:help='Trickle Rates'>" +
       6             :                 Resources.BuildSchema("nonNegativeDecimal") +
       7             :         "</element>" +
       8             :         "<element name='Interval' a:help='Number of miliseconds must pass for the player to gain the next trickle.'>" +
       9             :                 "<ref name='nonNegativeDecimal'/>" +
      10             :         "</element>";
      11             : 
      12           1 : ResourceTrickle.prototype.Init = function()
      13             : {
      14           1 :         this.trickleInterval = +this.template.Interval;
      15           1 :         this.CheckTimer();
      16             : };
      17             : 
      18           1 : ResourceTrickle.prototype.GetInterval = function()
      19             : {
      20           6 :         return this.trickleInterval;
      21             : };
      22             : 
      23           1 : ResourceTrickle.prototype.GetRates = function()
      24             : {
      25           3 :         return this.rates;
      26             : };
      27             : 
      28             : /**
      29             :  * @return {boolean} - Whether this entity has at least one non-zero trickle rate.
      30             :  */
      31           1 : ResourceTrickle.prototype.ComputeRates = function()
      32             : {
      33          11 :         this.rates = {};
      34          11 :         let hasTrickle = false;
      35          11 :         for (let resource in this.template.Rates)
      36             :         {
      37          22 :                 let rate = ApplyValueModificationsToEntity("ResourceTrickle/Rates/" + resource, +this.template.Rates[resource], this.entity);
      38          22 :                 if (rate)
      39             :                 {
      40           7 :                         this.rates[resource] = rate;
      41           7 :                         hasTrickle = true;
      42             :                 }
      43             :         }
      44             : 
      45          11 :         return hasTrickle;
      46             : };
      47             : 
      48           1 : ResourceTrickle.prototype.Trickle = function(data, lateness)
      49             : {
      50             :         // The player entity may also have a ResourceTrickle component
      51          12 :         let cmpPlayer = QueryOwnerInterface(this.entity) || Engine.QueryInterface(this.entity, IID_Player);
      52          12 :         if (!cmpPlayer)
      53           0 :                 return;
      54             : 
      55          12 :         cmpPlayer.AddResources(this.rates);
      56             : };
      57             : 
      58           1 : ResourceTrickle.prototype.OnValueModification = function(msg)
      59             : {
      60           7 :         if (msg.component != "ResourceTrickle")
      61           0 :                 return;
      62             : 
      63           7 :         this.CheckTimer();
      64             : };
      65             : 
      66           1 : ResourceTrickle.prototype.OnOwnershipChanged = function(msg)
      67             : {
      68           0 :         if (msg.to != INVALID_PLAYER)
      69           0 :                 this.CheckTimer();
      70             : };
      71             : 
      72           1 : ResourceTrickle.prototype.CheckTimer = function()
      73             : {
      74           8 :         if (!this.ComputeRates())
      75             :         {
      76           2 :                 if (!this.timer)
      77           1 :                         return;
      78             : 
      79           1 :                 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
      80           1 :                 cmpTimer.CancelTimer(this.timer);
      81           1 :                 delete this.timer;
      82           1 :                 return;
      83             :         }
      84             : 
      85           6 :         let oldTrickleInterval = this.trickleInterval;
      86           6 :         this.trickleInterval = ApplyValueModificationsToEntity("ResourceTrickle/Interval", +this.template.Interval, this.entity);
      87           6 :         if (this.trickleInterval < 0)
      88             :         {
      89           1 :                 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
      90           1 :                 cmpTimer.CancelTimer(this.timer);
      91           1 :                 delete this.timer;
      92           1 :                 return;
      93             :         }
      94             : 
      95           5 :         if (this.timer)
      96             :         {
      97           2 :                 if (this.trickleInterval == oldTrickleInterval)
      98           0 :                         return;
      99             : 
     100             :                 // If the timer wasn't invalidated before (interval <= 0), just update it.
     101           2 :                 if (oldTrickleInterval > 0)
     102             :                 {
     103           1 :                         let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     104           1 :                         cmpTimer.UpdateRepeatTime(this.timer, this.trickleInterval);
     105           1 :                         return;
     106             :                 }
     107             :         }
     108             : 
     109           4 :         let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     110           4 :         this.timer = cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.trickleInterval, this.trickleInterval, undefined);
     111             : };
     112             : 
     113           1 : Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle);

Generated by: LCOV version 1.14