diff --git a/binaries/data/mods/public/simulation/components/Capturable.js b/binaries/data/mods/public/simulation/components/Capturable.js
index 55fdde0..3acae7f 100644
a
|
b
|
Capturable.prototype.Reduce = function(amount, playerID)
|
76 | 76 | if (numberOfEnemies == 0) |
77 | 77 | return 0; |
78 | 78 | |
79 | | // distribute the capture points over all enemies |
| 79 | // Distribute the capture points over all enemies |
80 | 80 | let distributedAmount = amount / numberOfEnemies; |
81 | 81 | let removedAmount = 0; |
82 | 82 | while (distributedAmount > 0.0001) |
… |
… |
Capturable.prototype.Reduce = function(amount, playerID)
|
111 | 111 | }; |
112 | 112 | |
113 | 113 | /** |
| 114 | * Transfer the players capture points to gaia. |
| 115 | */ |
| 116 | Capturable.prototype.RemovePlayer = function(playerID) |
| 117 | { |
| 118 | if (!this.cp[playerID]) |
| 119 | return; |
| 120 | |
| 121 | this.cp[0] += this.cp[playerID]; |
| 122 | this.cp[playerID] = 0; |
| 123 | this.RegisterCapturePointsChanged(); |
| 124 | this.CheckTimer(); |
| 125 | } |
| 126 | |
| 127 | /** |
114 | 128 | * Check if the source can (re)capture points from this building |
115 | 129 | */ |
116 | 130 | Capturable.prototype.CanCapture = function(playerID) |
… |
… |
Capturable.prototype.CanCapture = function(playerID)
|
130 | 144 | //// Private functions //// |
131 | 145 | |
132 | 146 | /** |
133 | | * this has to be called whenever the capture points are changed. |
| 147 | * This has to be called whenever the capture points are changed. |
134 | 148 | * It notifies other components of the change, and switches ownership when needed |
135 | 149 | */ |
136 | 150 | Capturable.prototype.RegisterCapturePointsChanged = function() |
… |
… |
Capturable.prototype.RegisterCapturePointsChanged = function()
|
145 | 159 | if (owner == -1 || this.cp[owner] > 0) |
146 | 160 | return; |
147 | 161 | |
148 | | // if all cp has been taken from the owner, convert it to the best player |
| 162 | // If all cp has been taken from the owner, convert it to the best player |
149 | 163 | var bestPlayer = 0; |
150 | 164 | for (let i in this.cp) |
151 | 165 | if (this.cp[i] >= this.cp[bestPlayer]) |
… |
… |
Capturable.prototype.TimerTick = function()
|
185 | 199 | var owner = cmpOwnership.GetOwner(); |
186 | 200 | var modifiedCp = 0; |
187 | 201 | |
188 | | // special handle for the territory decay |
189 | | // reduce cp from the owner in favour of all neighbours (also allies) |
| 202 | // Special handle for the territory decay. |
| 203 | // Reduce cp from the owner in favour of all neighbours (also allies). |
190 | 204 | var cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay); |
191 | 205 | if (cmpTerritoryDecay && cmpTerritoryDecay.IsDecaying()) |
192 | 206 | { |
… |
… |
Capturable.prototype.TimerTick = function()
|
214 | 228 | if (modifiedCp) |
215 | 229 | return; |
216 | 230 | |
217 | | // nothing changed, stop the timer |
| 231 | // Nothing changed, stop the timer |
218 | 232 | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); |
219 | 233 | cmpTimer.CancelTimer(this.timer); |
220 | 234 | this.timer = 0; |
… |
… |
Capturable.prototype.OnOwnershipChanged = function(msg)
|
290 | 304 | } |
291 | 305 | else |
292 | 306 | { |
293 | | // initialise the capture points when created |
| 307 | // Initialise the capture points when created |
294 | 308 | var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
295 | 309 | for (let i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i) |
296 | 310 | if (i == msg.to) |
diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
index 1c017bc..eb87a36 100644
a
|
b
|
Player.prototype.SetState = function(newState, resign)
|
381 | 381 | cmpRangeManager.SetLosRevealAll(this.playerID, true); |
382 | 382 | else |
383 | 383 | { |
384 | | // Reassign all player's entities to Gaia |
385 | | let entities = cmpRangeManager.GetEntitiesByPlayer(this.playerID); |
386 | 384 | |
| 385 | let ownedEntities = []; |
| 386 | let otherEntities = []; |
| 387 | let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
| 388 | for (let i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i) |
| 389 | { |
| 390 | let entities = cmpRangeManager.GetEntitiesByPlayer(i); |
| 391 | |
| 392 | if (i == this.playerID) |
| 393 | ownedEntities = ownedEntities.concat(entities); |
| 394 | else |
| 395 | otherEntities = otherEntities.concat(entities); |
| 396 | } |
| 397 | |
| 398 | // Reassign all player's entities to Gaia |
387 | 399 | // The ownership change is done in two steps so that entities don't hit idle |
388 | 400 | // (and thus possibly look for "enemies" to attack) before nearby allies get |
389 | 401 | // converted to Gaia as well. |
390 | | for (let entity of entities) |
| 402 | for (let entity of ownedEntities) |
391 | 403 | { |
392 | 404 | let cmpOwnership = Engine.QueryInterface(entity, IID_Ownership); |
393 | 405 | cmpOwnership.SetOwnerQuiet(0); |
394 | 406 | } |
395 | 407 | |
396 | 408 | // With the real ownership change complete, send OwnershipChanged messages. |
397 | | for (let entity of entities) |
| 409 | for (let entity of ownedEntities) |
398 | 410 | Engine.PostMessage(entity, MT_OwnershipChanged, { |
399 | 411 | "entity": entity, |
400 | 412 | "from": this.playerID, |
401 | 413 | "to": 0 |
402 | 414 | }); |
| 415 | |
| 416 | // Remove capture points from other entities |
| 417 | for (let entity of otherEntities) |
| 418 | { |
| 419 | let cmpCapturable = Engine.QueryInterface(entity, IID_Capturable); |
| 420 | if (cmpCapturable) |
| 421 | cmpCapturable.RemovePlayer(this.playerID); |
| 422 | } |
403 | 423 | } |
404 | 424 | |
405 | 425 | Engine.BroadcastMessage(won ? MT_PlayerWon : MT_PlayerDefeated, { "playerId": this.playerID }); |
diff --git a/binaries/data/mods/public/simulation/components/tests/test_Capturable.js b/binaries/data/mods/public/simulation/components/tests/test_Capturable.js
index f3a10ac..c0874c8 100644
a
|
b
|
testReduce(testData, 0, 3, 0);
|
191 | 191 | testReduce(testData, 1500, 3, 1500); |
192 | 192 | testReduce(testData, 2000, 3, 2000); |
193 | 193 | testReduce(testData, 3000, 3, 2000); |
| 194 | |
| 195 | |
| 196 | // Transfer capture points of defeatet players to gaia |
| 197 | testCapturable(testData, cmpCapturable => { |
| 198 | cmpCapturable.SetCapturePoints([500, 1000, 0, 250]); |
| 199 | cmpCapturable.RemovePlayer(3); |
| 200 | TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [750, 1000, 0, 0]); |
| 201 | }); |