Ticket #3605: attackByPreference.patch

File attackByPreference.patch, 1.4 KB (added by mimo, 9 years ago)
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    58305830
    58315831UnitAI.prototype.AttackEntitiesByPreference = function(ents)
    58325832{
     5833    if (!ents.length)
     5834        return false;
     5835
    58335836    var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
    5834 
    58355837    if (!cmpAttack)
    58365838        return false;
    58375839
     
    58435845        return cmpUnitAI && (!cmpUnitAI.IsAnimal() || cmpUnitAI.IsDangerousAnimal());
    58445846    };
    58455847
    5846     return this.RespondToTargetedEntities(
    5847         ents.filter(function (v) { return cmpAttack.CanAttack(v) && attackfilter(v); })
    5848         .sort(function (a, b) { return cmpAttack.CompareEntitiesByPreference(a, b); })
    5849     );
     5848    let entsByPreferences = {};
     5849    let preferences = [];
     5850    for (let ent of ents)
     5851    {
     5852        if (!attackfilter(ent))
     5853            continue;
     5854        let pref = cmpAttack.GetPreference(ent);
     5855        if (pref === null || pref === undefined)
     5856            pref = 999;
     5857        if (!entsByPreferences[pref])
     5858        {
     5859            preferences.push(pref);
     5860            entsByPreferences[pref] = [ent];
     5861        }
     5862        else
     5863            entsByPreferences[pref].push(ent);
     5864    }
     5865    preferences.sort((a, b) => a - b);
     5866    for (let pref of preferences)
     5867        if (this.RespondToTargetedEntities(entsByPreferences[pref]))
     5868            return true;
     5869
     5870    return false;
    58505871};
    58515872
    58525873/**