Ticket #4161: 0005-Add-GetPlayersByDiplomacy-to-cmpPlayer.patch

File 0005-Add-GetPlayersByDiplomacy-to-cmpPlayer.patch, 9.2 KB (added by leper, 8 years ago)
  • binaries/data/mods/public/simulation/components/BuildingAI.js

    From 10e8c732ffa0ce62611018a60736c43a69de17da Mon Sep 17 00:00:00 2001
    From: leper <leper@wildfiregames.com>
    Date: Fri, 26 Aug 2016 18:59:00 +0200
    Subject: [PATCH 5/5] Add GetPlayersByDiplomacy to cmpPlayer.
    
    This is in turn used by GetEnemies and the new GetAllies and GetMutualAllies.
    Use these in a few places as those are common queries.
    ---
     .../public/simulation/components/BuildingAI.js     |  8 ++--
     .../data/mods/public/simulation/components/Gate.js |  6 +--
     .../mods/public/simulation/components/Player.js    | 45 +++++++++++++---------
     .../mods/public/simulation/components/UnitAI.js    | 32 +++------------
     .../simulation/components/tests/test_Player.js     |  2 +
     .../simulation/components/tests/test_UnitAI.js     | 10 +++--
     6 files changed, 43 insertions(+), 60 deletions(-)
    
    diff --git a/binaries/data/mods/public/simulation/components/BuildingAI.js b/binaries/data/mods/public/simulation/components/BuildingAI.js
    index 0bb06f5..efacf79 100644
    a b BuildingAI.prototype.SetupRangeQuery = function()  
    121121    if (!cmpPlayer)
    122122        return;
    123123
    124     var enemies = [];
    125     var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
    126     for (let i = 1; i < numPlayers; ++i)
    127         if (cmpPlayer.IsEnemy(i))
    128             enemies.push(i);
     124    var enemies = cmpPlayer.GetEnemies();
     125    if (enemies.length && enemies[0] == 0)
     126        enemies.shift(); // remove gaia
    129127
    130128    if (!enemies.length)
    131129        return;
  • binaries/data/mods/public/simulation/components/Gate.js

    diff --git a/binaries/data/mods/public/simulation/components/Gate.js b/binaries/data/mods/public/simulation/components/Gate.js
    index c322c17..faa5c11 100644
    a b Gate.prototype.OnDestroy = function()  
    6565Gate.prototype.SetupRangeQuery = function(owner)
    6666{
    6767    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    68     var cmpPlayer = QueryPlayerIDInterface(owner);
    6968
    7069    if (this.unitsQuery)
    7170        cmpRangeManager.DestroyActiveQuery(this.unitsQuery);
    7271
    7372    // Only allied units can make the gate open.
    74     var players = [];
    75     for (var i = 0; i < cmpPlayer.GetDiplomacy().length; ++i)
    76         if (cmpPlayer.IsAlly(i))
    77             players.push(i);
     73    var players = QueryPlayerIDInterface(owner).GetAllies();
    7874
    7975    var range = this.GetPassRange();
    8076    if (range > 0)
  • 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 7959dd3..89986af 100644
    a b Player.prototype.SetDiplomacyIndex = function(idx, value)  
    497497Player.prototype.UpdateSharedLos = function()
    498498{
    499499    let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    500     let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    501500    let cmpTechnologyManager = Engine.QueryInterface(this.entity, IID_TechnologyManager);
    502     if (!cmpRangeManager || !cmpPlayerManager || !cmpTechnologyManager)
     501    if (!cmpRangeManager || !cmpTechnologyManager)
    503502        return;
    504503
    505     let sharedLos = [];
    506504    if (!cmpTechnologyManager.IsTechnologyResearched(this.template.SharedLosTech))
    507505    {
    508506        cmpRangeManager.SetSharedLos(this.playerID, [this.playerID]);
    509507        return;
    510508    }
    511     for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); ++i)
    512         if (this.IsMutualAlly(i))
    513             sharedLos.push(i);
    514509
    515     cmpRangeManager.SetSharedLos(this.playerID, sharedLos);
     510    cmpRangeManager.SetSharedLos(this.playerID, this.GetMutualAllies());
    516511};
    517512
    518513Player.prototype.GetFormations = function()
    Player.prototype.IsAI = function()  
    575570    return this.isAI;
    576571};
    577572
     573Player.prototype.GetPlayersByDiplomacy = function(func)
     574{
     575    var players = [];
     576    for (var i = 0; i < this.diplomacy.length; ++i)
     577        if (this[func](i))
     578            players.push(i);
     579    return players;
     580};
     581
    578582Player.prototype.SetAlly = function(id)
    579583{
    580584    this.SetDiplomacyIndex(id, 1);
    Player.prototype.IsAlly = function(id)  
    588592    return this.diplomacy[id] > 0;
    589593};
    590594
     595Player.prototype.GetAllies = function()
     596{
     597    return this.GetPlayersByDiplomacy("IsAlly");
     598};
     599
    591600/**
    592601 * Check if given player is our ally excluding ourself
    593602 */
    Player.prototype.IsMutualAlly = function(id)  
    605614    return this.IsAlly(id) && cmpPlayer && cmpPlayer.IsAlly(this.playerID);
    606615};
    607616
     617Player.prototype.GetMutualAllies = function()
     618{
     619    return this.GetPlayersByDiplomacy("IsMutualAlly");
     620};
     621
    608622/**
    609623 * Check if given player is our ally, and we are its ally, excluding ourself
    610624 */
    Player.prototype.SetEnemy = function(id)  
    619633};
    620634
    621635/**
    622  * Get all enemies of a given player.
    623  */
    624 Player.prototype.GetEnemies = function()
    625 {
    626     var enemies = [];
    627     for (var i = 0; i < this.diplomacy.length; ++i)
    628         if (this.diplomacy[i] < 0)
    629             enemies.push(i);
    630     return enemies;
    631 };
    632 
    633 /**
    634636 * Check if given player is our enemy
    635637 */
    636638Player.prototype.IsEnemy = function(id)
    Player.prototype.IsEnemy = function(id)  
    638640    return this.diplomacy[id] < 0;
    639641};
    640642
     643Player.prototype.GetEnemies = function()
     644{
     645    return this.GetPlayersByDiplomacy("IsEnemy");
     646};
     647
    641648Player.prototype.SetNeutral = function(id)
    642649{
    643650    this.SetDiplomacyIndex(id, 0);
  • binaries/data/mods/public/simulation/components/UnitAI.js

    diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
    index 0276d21..b0591fa 100644
    a b UnitAI.prototype.SetupRangeQuery = function(enable = true)  
    34733473    if (!cmpPlayer)
    34743474        return;
    34753475
    3476     var players = [];
    3477 
    3478     var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
    3479     for (var i = 0; i < numPlayers; ++i)
    3480     {
    3481         // Exclude allies, and self
    3482         // TODO: How to handle neutral players - Special query to attack military only?
    3483         if (cmpPlayer.IsEnemy(i))
    3484             players.push(i);
    3485     }
    3486 
     3476    // Exclude allies, and self
     3477    // TODO: How to handle neutral players - Special query to attack military only?
     3478    var players = cmpPlayer.GetEnemies();
    34873479    var range = this.GetQueryRange(IID_Attack);
    34883480
    34893481    this.losRangeQuery = cmpRangeManager.CreateActiveQuery(this.entity, range.min, range.max, players, IID_DamageReceiver, cmpRangeManager.GetEntityFlagMask("normal"));
    UnitAI.prototype.SetupHealRangeQuery = function(enable = true)  
    35103502    if (!cmpPlayer)
    35113503        return;
    35123504
    3513     var players = [];
    3514 
    3515     var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
    3516     for (var i = 0; i < numPlayers; ++i)
    3517     {
    3518         // Exclude gaia and enemies
    3519         if (cmpPlayer.IsAlly(i))
    3520             players.push(i);
    3521     }
    3522 
     3505    var players = cmpPlayer.GetAllies();
    35233506    var range = this.GetQueryRange(IID_Heal);
    35243507
    35253508    this.losHealRangeQuery = cmpRangeManager.CreateActiveQuery(this.entity, range.min, range.max, players, IID_Health, cmpRangeManager.GetEntityFlagMask("injured"));
    UnitAI.prototype.FindNearestDropsite = function(genericType)  
    40804063    var players = [owner];
    40814064    var cmpPlayer = QueryOwnerInterface(this.entity);
    40824065    if (cmpPlayer && cmpPlayer.HasSharedDropsites())
    4083     {
    4084         let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    4085         for (let i = 1; i < cmpPlayerManager.GetNumPlayers(); ++i)
    4086             if (i != owner && cmpPlayer.IsMutualAlly(i))
    4087                 players.push(i);
    4088     }
     4066        players = cmpPlayer.GetMutualAllies();
    40894067
    40904068    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    40914069    var nearby = cmpRangeManager.ExecuteQuery(this.entity, 0, -1, players, IID_ResourceDropsite);
  • binaries/data/mods/public/simulation/components/tests/test_Player.js

    diff --git a/binaries/data/mods/public/simulation/components/tests/test_Player.js b/binaries/data/mods/public/simulation/components/tests/test_Player.js
    index f8650c9..abb2d73 100644
    a b TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0);  
    99TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0);
    1010
    1111cmpPlayer.SetDiplomacy([-1, 1, 0, 1, -1]);
     12TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetAllies(), [1, 3]);
     13TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetEnemies(), [0, 4]);
    1214
    1315var diplo = cmpPlayer.GetDiplomacy();
    1416diplo[0] = 1;
  • binaries/data/mods/public/simulation/components/tests/test_UnitAI.js

    diff --git a/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js b/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js
    index add4cc8..e899f0e 100644
    a b function TestFormationExiting(mode)  
    5858    });
    5959
    6060    AddMock(playerEntity, IID_Player, {
    61         IsAlly: function() { return []; },
    62         IsEnemy: function() { return []; },
     61        IsAlly: function() { return false; },
     62        IsEnemy: function() { return true; },
     63        GetEnemies: function() { return []; },
    6364    });
    6465
    6566
    function TestMoveIntoFormationWhileAttacking()  
    204205    });
    205206
    206207    AddMock(playerEntity, IID_Player, {
    207         IsAlly: function() { return []; },
    208         IsEnemy: function() { return []; },
     208        IsAlly: function() { return false; },
     209        IsEnemy: function() { return true; },
     210        GetEnemies: function() { return []; },
    209211    });
    210212
    211213    // create units