Ticket #2156: 2156_unitAI_range_queries_testpass.patch

File 2156_unitAI_range_queries_testpass.patch, 7.6 KB (added by dumbo, 10 years ago)

now passes a test also. thanks for sanderd

  • binaries/data/mods/public/simulation/components/tests/test_UnitAI.js

     
    9999    unitAI.SetupRangeQuery(1);
    100100
    101101
    102     if (mode == 1)
     102    if (mode == 1) {
    103103        AddMock(enemy, IID_Health, {
    104104            GetHitpoints: function() { return 10; },
    105105        });
     106        AddMock(enemy, IID_UnitAI, {
     107            IsAnimal: function() { return false; }
     108        });
     109    }           
    106110    else if (mode == 2)
    107111        AddMock(enemy, IID_Health, {
    108112            GetHitpoints: function() { return 0; },
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    146146        // ignore newly-seen units by default
    147147    },
    148148
    149     "LosGaiaRangeUpdate": function(msg) {
    150         // ignore newly-seen Gaia units by default
    151     },
    152 
    153149    "LosHealRangeUpdate": function(msg) {
    154150        // ignore newly-seen injured units by default
    155151    },
     
    13821378                }
    13831379            },
    13841380
    1385             "LosGaiaRangeUpdate": function(msg) {
    1386                 if (this.GetStance().targetVisibleEnemies)
    1387                 {
    1388                     // Start attacking one of the newly-seen enemy (if any)
    1389                     this.AttackGaiaEntitiesByPreference(msg.data.added);
    1390                 }
    1391             },
    1392 
    13931381            "LosHealRangeUpdate": function(msg) {
    13941382                this.RespondToHealableEntities(msg.data.added);
    13951383            },
     
    15041492                        this.AttackEntitiesByPreference(msg.data.added);
    15051493                },
    15061494
    1507                 "LosGaiaRangeUpdate": function(msg) {
    1508                     // Start attacking one of the newly-seen enemy (if any)
    1509                     if (this.GetStance().targetVisibleEnemies)
    1510                         this.AttackGaiaEntitiesByPreference(msg.data.added);
    1511                 },
    1512 
    15131495                "Timer": function(msg) {
    15141496                    // Check the target is alive
    15151497                    if (!this.TargetIsAlive(this.isGuardOf))
     
    30263008    return (this.orderQueue.length > 0 && this.orderQueue[0].type == "WalkAndFight");
    30273009};
    30283010
    3029 UnitAI.prototype.CanAttackGaia = function()
    3030 {
    3031     var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    3032     if (!cmpAttack)
    3033         return false;
    3034 
    3035     // Rejects Gaia (0) and INVALID_PLAYER (-1)
    3036     var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    3037     if (!cmpOwnership || cmpOwnership.GetOwner() <= 0)
    3038         return false;
    3039 
    3040     return true;
    3041 };
    3042 
    30433011UnitAI.prototype.OnCreate = function()
    30443012{
    30453013    if (this.IsAnimal())
     
    30813049        rangeMan.DestroyActiveQuery(this.losRangeQuery);
    30823050    if (this.losHealRangeQuery)
    30833051        rangeMan.DestroyActiveQuery(this.losHealRangeQuery);
    3084     if (this.losGaiaRangeQuery)
    3085         rangeMan.DestroyActiveQuery(this.losGaiaRangeQuery);
    30863052};
    30873053
    30883054UnitAI.prototype.OnVisionRangeChanged = function(msg)
     
    31253091    }
    31263092};
    31273093
    3128 // Wrapper function that sets up the normal, healer, and Gaia range queries.
     3094// Wrapper function that sets up the normal and healer range queries.
    31293095UnitAI.prototype.SetupRangeQueries = function()
    31303096{
    31313097    this.SetupRangeQuery();
     
    31333099    if (this.IsHealer())
    31343100        this.SetupHealRangeQuery();
    31353101
    3136     if (this.CanAttackGaia() || this.losGaiaRangeQuery)
    3137         this.SetupGaiaRangeQuery();
    31383102}
    31393103
    3140 // Set up a range query for all enemy units within LOS range
     3104// Set up a range query for all enemy and gaia units within LOS range
    31413105// which can be attacked.
    31423106// This should be called whenever our ownership changes.
    31433107UnitAI.prototype.SetupRangeQuery = function()
     
    31613125        // If unit not just killed, get enemy players via diplomacy
    31623126        var cmpPlayer = Engine.QueryInterface(playerMan.GetPlayerByID(owner), IID_Player);
    31633127        var numPlayers = playerMan.GetNumPlayers();
    3164         for (var i = 1; i < numPlayers; ++i)
     3128        for (var i = 0; i < numPlayers; ++i)
    31653129        {
    3166             // Exclude gaia, allies, and self
     3130            // Exclude allies, and self
    31673131            // TODO: How to handle neutral players - Special query to attack military only?
    31683132            if (cmpPlayer.IsEnemy(i))
    31693133                players.push(i);
     
    31733137    var range = this.GetQueryRange(IID_Attack);
    31743138
    31753139    this.losRangeQuery = rangeMan.CreateActiveQuery(this.entity, range.min, range.max, players, IID_DamageReceiver, rangeMan.GetEntityFlagMask("normal"));
     3140   
    31763141    rangeMan.EnableActiveQuery(this.losRangeQuery);
    31773142};
    31783143
     
    32113176    rangeMan.EnableActiveQuery(this.losHealRangeQuery);
    32123177};
    32133178
    3214 // Set up a range query for Gaia units within LOS range which can be attacked.
    3215 // This should be called whenever our ownership changes.
    3216 UnitAI.prototype.SetupGaiaRangeQuery = function()
    3217 {
    3218     var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    3219     var owner = cmpOwnership.GetOwner();
    32203179
    3221     var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    3222     var playerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    32233180
    3224     if (this.losGaiaRangeQuery)
    3225     {
    3226         rangeMan.DestroyActiveQuery(this.losGaiaRangeQuery);
    3227         this.losGaiaRangeQuery = undefined;
    3228     }
    3229 
    3230     // Only create the query if Gaia is our enemy and we can attack.
    3231     if (this.CanAttackGaia())
    3232     {
    3233         var range = this.GetQueryRange(IID_Attack);
    3234    
    3235         // This query is only interested in Gaia entities that can attack.
    3236         this.losGaiaRangeQuery = rangeMan.CreateActiveQuery(this.entity, range.min, range.max, [0], IID_Attack, rangeMan.GetEntityFlagMask("normal"));
    3237         rangeMan.EnableActiveQuery(this.losGaiaRangeQuery);
    3238     }
    3239 };
    3240 
    32413181//// FSM linkage functions ////
    32423182
    32433183UnitAI.prototype.SetNextState = function(state)
     
    36693609{
    36703610    if (msg.tag == this.losRangeQuery)
    36713611        UnitFsm.ProcessMessage(this, {"type": "LosRangeUpdate", "data": msg});
    3672     else if (msg.tag == this.losGaiaRangeQuery)
    3673         UnitFsm.ProcessMessage(this, {"type": "LosGaiaRangeUpdate", "data": msg});
    36743612    else if (msg.tag == this.losHealRangeQuery)
    36753613        UnitFsm.ProcessMessage(this, {"type": "LosHealRangeUpdate", "data": msg});
    36763614};
     
    50004938    var rangeMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    50014939    if (this.AttackEntitiesByPreference( rangeMan.ResetActiveQuery(this.losRangeQuery) ))
    50024940        return true;
    5003     // If no regular enemies were found, attempt to attack a hostile Gaia entity.
    5004     else if (this.losGaiaRangeQuery)
    5005         return this.AttackGaiaEntitiesByPreference( rangeMan.ResetActiveQuery(this.losGaiaRangeQuery) );
    50064941
    50074942    return false;
    50084943};
     
    50624997    if (targets.length)
    50634998        return targets;
    50644999
    5065     // if nothing found, look for gaia targets
    5066     var entities = rangeMan.ResetActiveQuery(this.losGaiaRangeQuery);
    5067     var targets = entities.filter(function (v, i, a) { return cmpAttack.CanAttack(v); })
    5068         .sort(function (a, b) { return cmpAttack.CompareEntitiesByPreference(a, b); });
    5069 
    50705000    return targets;
    50715001};
    50725002
     
    54605390UnitAI.prototype.AttackEntitiesByPreference = function(ents)
    54615391{
    54625392    var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    5463     if (!cmpAttack)
    5464         return false;
    54655393
    5466     return this.RespondToTargetedEntities(
    5467         ents.filter(function (v, i, a) { return cmpAttack.CanAttack(v); })
    5468         .sort(function (a, b) { return cmpAttack.CompareEntitiesByPreference(a, b); })
    5469     );
    5470 };
    5471 
    5472 UnitAI.prototype.AttackGaiaEntitiesByPreference = function(ents)
    5473 {
    5474     var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    54755394    if (!cmpAttack)
    54765395        return false;
    54775396
    5478     const filter = function(e) {
     5397    const animalfilter = function(e) {
    54795398        var cmpUnitAI = Engine.QueryInterface(e, IID_UnitAI);
    54805399        return (cmpUnitAI && (!cmpUnitAI.IsAnimal() || cmpUnitAI.IsDangerousAnimal()));
    54815400    };
    54825401
    54835402    return this.RespondToTargetedEntities(
    5484         ents.filter(function (v, i, a) { return cmpAttack.CanAttack(v) && filter(v); })
     5403        ents.filter(function (v, i, a) { return cmpAttack.CanAttack(v) && animalfilter(v); })
    54855404        .sort(function (a, b) { return cmpAttack.CompareEntitiesByPreference(a, b); })
    54865405    );
    54875406};