Ticket #3284: 0001-GetBestAttack-replacement-stand-ground-response.patch

File 0001-GetBestAttack-replacement-stand-ground-response.patch, 4.8 KB (added by Karamel, 9 years ago)

Attack patch with code style review

  • binaries/data/mods/public/simulation/components/Attack.js

    From cd6eb8a5728eee506c9677980236fefbf78d609e Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?C=C3=A9dric=20Houbart?= <cedric@scil.coop>
    Date: Fri, 5 Jun 2015 11:09:54 +0200
    Subject: [PATCH] GetBestAttack replacement (stand ground response)
    
    ---
     .../mods/public/simulation/components/Attack.js    | 35 +++++++++++++++++-----
     .../mods/public/simulation/components/UnitAI.js    | 12 ++------
     .../simulation/components/tests/test_UnitAI.js     |  2 --
     3 files changed, 30 insertions(+), 19 deletions(-)
    
    diff --git a/binaries/data/mods/public/simulation/components/Attack.js b/binaries/data/mods/public/simulation/components/Attack.js
    index 1d78bfa..4c512a4 100644
    a b Attack.prototype.GetPreference = function(target)  
    309309};
    310310
    311311/**
    312  * Return the type of the best attack.
    313  * TODO: this should probably depend on range, target, etc,
    314  * so we can automatically switch between ranged and melee
     312 * Get the full range of attack regarding available attack types.
    315313 */
    316 Attack.prototype.GetBestAttack = function()
     314Attack.prototype.GetFullAttackRange = function()
    317315{
    318     return this.GetAttackTypes().pop();
    319 };
     316    let ret = {"min": Infinity, "max": 0};
     317    let types = this.GetAttackTypes();
     318    for (let key in types)
     319    {
     320        let type = types[key];
     321        if (type != "Slaughter")
     322        {
     323            // Ignore the special attack "slaughter" dedicated to domestic animals
     324            let range = this.GetRange(type);
     325            if (range.min < ret.min)
     326                ret.min = range.min;
     327            if (range.max > ret.max)
     328                ret.max = range.max;
     329        }
     330    }
     331    return ret;
     332}
    320333
    321334Attack.prototype.GetBestAttackAgainst = function(target, allowCapture)
    322335{
    323336    var cmpFormation = Engine.QueryInterface(target, IID_Formation);
    324337    if (cmpFormation)
    325         return this.GetBestAttack();
     338    {
     339        // TODO: Formation against formation needs review
     340        let best = ["Ranged", "Melee", "Capture"];
     341        let types = this.GetAttackTypes();
     342        for (let key in best)
     343            if (types.indexOf(best[key]) != -1)
     344                return best[key];
     345        return undefined;
     346    }
    326347
    327348    var cmpIdentity = Engine.QueryInterface(target, IID_Identity);
    328349    if (!cmpIdentity)
  • 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 5fc38b6..7ebdad8 100644
    a b UnitAI.prototype.CheckTargetIsInVisionRange = function(target)  
    44664466    return distance < range;
    44674467};
    44684468
    4469 UnitAI.prototype.GetBestAttack = function()
    4470 {
    4471     var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    4472     if (!cmpAttack)
    4473         return undefined;
    4474     return cmpAttack.GetBestAttack();
    4475 };
    4476 
    44774469UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture)
    44784470{
    44794471    var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    UnitAI.prototype.GetQueryRange = function(iid)  
    54665458        var cmpRanged = Engine.QueryInterface(this.entity, iid);
    54675459        if (!cmpRanged)
    54685460            return ret;
    5469         var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack());
     5461        var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
    54705462        ret.min = range.min;
    54715463        ret.max = range.max;
    54725464    }
    UnitAI.prototype.GetQueryRange = function(iid)  
    54835475        var cmpRanged = Engine.QueryInterface(this.entity, iid);
    54845476        if (!cmpRanged)
    54855477            return ret;
    5486         var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack());
     5478        var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
    54875479        var cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
    54885480        if (!cmpVision)
    54895481            return ret;
  • 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 7e7df8d..90923e8 100644
    a b function TestFormationExiting(mode)  
    9797
    9898    AddMock(unit, IID_Attack, {
    9999        GetRange: function() { return { "max": 10, "min": 0}; },
    100         GetBestAttack: function() { return "melee"; },
    101100        GetBestAttackAgainst: function(t) { return "melee"; },
    102101        GetTimers: function() { return { "prepare": 500, "repeat": 1000 }; },
    103102        CanAttack: function(v) { return true; },
    function TestMoveIntoFormationWhileAttacking()  
    248247   
    249248        AddMock(unit + i, IID_Attack, {
    250249            GetRange: function() { return {"max":10, "min": 0}; },
    251             GetBestAttack: function() { return "melee"; },
    252250            GetBestAttackAgainst: function(t) { return "melee"; },
    253251            GetTimers: function() { return { "prepare": 500, "repeat": 1000 }; },
    254252            CanAttack: function(v) { return true; },