Ticket #3284: attack_patch.2.diff

File attack_patch.2.diff, 4.0 KB (added by Karamel, 9 years ago)

GetBestAttack replacement patch

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

     
    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    var ret = {"min": Infinity, "max": 0};
     317    var types = this.GetAttackTypes();
     318    for (var key in types)
     319    {
     320        var type = types[key];
     321        if (type != "Slaughter")
     322        {
     323            // Ignore the special attack "slaughter"
     324            var 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        var best = ["Ranged", "Melee", "Capture"];
     341        var types = this.GetAttackTypes();
     342        for (var key in best)
     343        {
     344            if (types.indexOf(best[key]) != -1)
     345                return best[key];
     346        }
     347        return undefined;
     348    }
    326349
    327350    var cmpIdentity = Engine.QueryInterface(target, IID_Identity);
    328351    if (!cmpIdentity)
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    44584458    return distance < range;
    44594459};
    44604460
    4461 UnitAI.prototype.GetBestAttack = function()
    4462 {
    4463     var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    4464     if (!cmpAttack)
    4465         return undefined;
    4466     return cmpAttack.GetBestAttack();
    4467 };
    4468 
    44694461UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture)
    44704462{
    44714463    var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
     
    54585450        var cmpRanged = Engine.QueryInterface(this.entity, iid);
    54595451        if (!cmpRanged)
    54605452            return ret;
    5461         var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack());
     5453        var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
    54625454        ret.min = range.min;
    54635455        ret.max = range.max;
    54645456    }
     
    54755467        var cmpRanged = Engine.QueryInterface(this.entity, iid);
    54765468        if (!cmpRanged)
    54775469            return ret;
    5478         var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack());
     5470        var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange();
    54795471        var cmpVision = Engine.QueryInterface(this.entity, IID_Vision);
    54805472        if (!cmpVision)
    54815473            return ret;
  • binaries/data/mods/public/simulation/components/tests/test_UnitAI.js

     
    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; },
     
    249248   
    250249        AddMock(unit + i, IID_Attack, {
    251250            GetRange: function() { return {"max":10, "min": 0}; },
    252             GetBestAttack: function() { return "melee"; },
    253251            GetBestAttackAgainst: function(t) { return "melee"; },
    254252            GetTimers: function() { return { "prepare": 500, "repeat": 1000 }; },
    255253            CanAttack: function(v) { return true; },