Ticket #4384: remove_caputre_points_of_defeated_players_v1.patch

File remove_caputre_points_of_defeated_players_v1.patch, 5.7 KB (added by elexis, 7 years ago)
  • binaries/data/mods/public/simulation/components/Capturable.js

    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)  
    7676    if (numberOfEnemies == 0)
    7777        return 0;
    7878
    79     // distribute the capture points over all enemies
     79    // Distribute the capture points over all enemies
    8080    let distributedAmount = amount / numberOfEnemies;
    8181    let removedAmount = 0;
    8282    while (distributedAmount > 0.0001)
    Capturable.prototype.Reduce = function(amount, playerID)  
    111111};
    112112
    113113/**
     114 * Transfer the players capture points to gaia.
     115 */
     116Capturable.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/**
    114128 * Check if the source can (re)capture points from this building
    115129 */
    116130Capturable.prototype.CanCapture = function(playerID)
    Capturable.prototype.CanCapture = function(playerID)  
    130144//// Private functions ////
    131145
    132146/**
    133  * this has to be called whenever the capture points are changed.
     147 * This has to be called whenever the capture points are changed.
    134148 * It notifies other components of the change, and switches ownership when needed
    135149 */
    136150Capturable.prototype.RegisterCapturePointsChanged = function()
    Capturable.prototype.RegisterCapturePointsChanged = function()  
    145159    if (owner == -1 || this.cp[owner] > 0)
    146160        return;
    147161
    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
    149163    var bestPlayer = 0;
    150164    for (let i in this.cp)
    151165        if (this.cp[i] >= this.cp[bestPlayer])
    Capturable.prototype.TimerTick = function()  
    185199    var owner = cmpOwnership.GetOwner();
    186200    var modifiedCp = 0;
    187201
    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).
    190204    var cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay);
    191205    if (cmpTerritoryDecay && cmpTerritoryDecay.IsDecaying())
    192206    {
    Capturable.prototype.TimerTick = function()  
    214228    if (modifiedCp)
    215229        return;
    216230
    217     // nothing changed, stop the timer
     231    // Nothing changed, stop the timer
    218232    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    219233    cmpTimer.CancelTimer(this.timer);
    220234    this.timer = 0;
    Capturable.prototype.OnOwnershipChanged = function(msg)  
    290304    }
    291305    else
    292306    {
    293         // initialise the capture points when created
     307        // Initialise the capture points when created
    294308        var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    295309        for (let i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i)
    296310            if (i == msg.to)
  • binaries/data/mods/public/simulation/components/Player.js

    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)  
    381381        cmpRangeManager.SetLosRevealAll(this.playerID, true);
    382382    else
    383383    {
    384         // Reassign all player's entities to Gaia
    385         let entities = cmpRangeManager.GetEntitiesByPlayer(this.playerID);
    386384
     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
    387399        // The ownership change is done in two steps so that entities don't hit idle
    388400        // (and thus possibly look for "enemies" to attack) before nearby allies get
    389401        // converted to Gaia as well.
    390         for (let entity of entities)
     402        for (let entity of ownedEntities)
    391403        {
    392404            let cmpOwnership = Engine.QueryInterface(entity, IID_Ownership);
    393405            cmpOwnership.SetOwnerQuiet(0);
    394406        }
    395407
    396408        // With the real ownership change complete, send OwnershipChanged messages.
    397         for (let entity of entities)
     409        for (let entity of ownedEntities)
    398410            Engine.PostMessage(entity, MT_OwnershipChanged, {
    399411                "entity": entity,
    400412                "from": this.playerID,
    401413                "to": 0
    402414            });
     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        }
    403423    }
    404424
    405425    Engine.BroadcastMessage(won ? MT_PlayerWon : MT_PlayerDefeated, { "playerId": this.playerID });
  • binaries/data/mods/public/simulation/components/tests/test_Capturable.js

    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);  
    191191testReduce(testData, 1500, 3, 1500);
    192192testReduce(testData, 2000, 3, 2000);
    193193testReduce(testData, 3000, 3, 2000);
     194
     195
     196// Transfer capture points of defeatet players to gaia
     197testCapturable(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});