| 33 | "<optional>" + |
| 34 | "<element name='Regeneration' a:help='Controls whether this resource can regenerate its remaining amount.'>" + |
| 35 | "<interleave>" + |
| 36 | "<element name='Rate' a:help='Optional regeneration rate. Resources/second if the growth is linear or the rate of population growth if the growith is exponential.'>" + |
| 37 | "<data type='decimal'/>" + |
| 38 | "</element>" + |
| 39 | "<element name='Delay' a:help='Seconds between when the number of gatherers hit 0 and the resource starts regenerating.'>" + |
| 40 | "<data type='nonNegativeInteger'/>" + |
| 41 | "</element>" + |
| 42 | "<element name='Growth' a:help='Growth formula for regeneration. Either linear or exponential. Linear continually adds the rate, while exponential uses it as the rate in the standard population growth formula.'>" + |
| 43 | "<choice>" + |
| 44 | "<value>linear</value>" + |
| 45 | "<value>exponential</value>" + |
| 46 | "</choice>" + |
| 47 | "</element>" + |
| 48 | "</interleave>" + |
| 49 | "</element>" + |
| 50 | "</optional>" + |
| 115 | ResourceSupply.prototype.RegenerateResources = function(data, lateness) { |
| 116 | var max = this.GetMaxAmount(); |
| 117 | if (this.gatherers.length == 0 && !this.regenDelayTimer && this.amount < max) { |
| 118 | var old = this.amount; |
| 119 | if (this.regenTime == null) |
| 120 | this.regenTime == 0; |
| 121 | if (this.regenGrowth == "linear") |
| 122 | this.amount = Math.min(max, this.amount + data.rate); |
| 123 | else |
| 124 | this.amount = Math.min(max, Math.round(this.amount * Math.pow(Math.E, data.rate / 100 * this.regenTime))); |
| 125 | ++this.regenTime; |
| 126 | Engine.PostMessage(this.entity, MT_ResourceSupplyChanged, { "from": old, "to": this.amount }); |
| 127 | } |
| 128 | if (this.amount == max) |
| 129 | this.regenTime = null; |
| 130 | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); |
| 131 | var regenRate = this.GetRegenerationRate(); |
| 132 | var absRegen = Math.abs(regenRate); |
| 133 | if (Math.floor(regenRate) == regenRate || this.regenGrowth == "exponential") |
| 134 | cmpTimer.SetTimeout(this.entity, IID_ResourceSupply, "RegenerateResources", 1000, { "rate": regenRate }); |
| 135 | else |
| 136 | cmpTimer.SetTimeout(this.entity, IID_ResourceSupply, "RegenerateResources", 1000 / absRegen, |
| 137 | { "rate": absRegen == regenRate ? 1 : -1 }); |
| 138 | }; |
| 139 | |
| 140 | ResourceSupply.prototype.StartRegenerationDelayTimer = function() { |
| 141 | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); |
| 142 | this.regenDelayTimer = cmpTimer.SetTimeout(this.entity, IID_ResourceSupply, "CancelRegenerationDelayTimer", this.GetRegenerationDelay() * 1000, null); |
| 143 | }; |
| 144 | |
| 145 | ResourceSupply.prototype.CancelRegenerationDelayTimer = function() { |
| 146 | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); |
| 147 | cmpTimer.CancelTimer(this.regenDelayTimer); |
| 148 | this.regenDelayTimer = null; |
| 149 | }; |
| 150 | |
| 166 | ResourceSupply.prototype.IsRegenerative = function() { |
| 167 | return this.GetRegenerationRate() != 0; |
| 168 | }; |
| 169 | |
| 170 | ResourceSupply.prototype.GetTerritoryOwner = function () { |
| 171 | var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
| 172 | var cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager); |
| 173 | var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); |
| 174 | if (!(cmpPosition && cmpPosition.IsInWorld())) |
| 175 | return 0; // Something's wrong, just say we're in neutral territory. |
| 176 | var pos = cmpPosition.GetPosition2D(); |
| 177 | return cmpPlayerManager.GetPlayerByID(cmpTerritoryManager.GetOwner(pos.x, pos.y)); |
| 178 | }; |
| 179 | |
| 180 | ResourceSupply.prototype.GetRegenerationRate = function() { |
| 181 | return ApplyTechModificationsToPlayer("ResourceSupply/Regeneration/Rate", this.regenRate, this.GetTerritoryOwner()); |
| 182 | }; |
| 183 | |
| 184 | ResourceSupply.prototype.GetRegenerationDelay = function() { |
| 185 | return ApplyTechModificationsToPlayer("ResourcesSupply/Regeneration/Delay", this.regenDelay, this.GetTerritoryOwner()); |
| 186 | }; |
| 187 | |