Ticket #1985: restrict-and-prefer-building-attacks-1985_using_splice.diff

File restrict-and-prefer-building-attacks-1985_using_splice.diff, 3.4 KB (added by James Scott, 11 years ago)

Full diff including splice code review feedback

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

    diff --git a/binaries/data/mods/public/simulation/components/BuildingAI.js b/binaries/data/mods/public/simulation/components/BuildingAI.js
    index 8405880..dd50f9b 100644
    a b BuildingAI.prototype.OnRangeUpdate = function(msg)  
    169169    else if (msg.tag != this.enemyUnitsQuery)
    170170        return;
    171171
    172     const restrictedClasses = cmpAttack.GetRestrictedClasses(attackType);
    173 
    174172    if (msg.added.length > 0)
    175173    {
    176174        for each (var entity in msg.added)
    177175        {
    178             var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
    179             var targetClasses = cmpIdentity.GetClassesList();
    180 
    181             if (!targetClasses.some(function(c){return restrictedClasses.indexOf(c) > -1;}))
     176            if (cmpAttack.CanAttack(entity))
    182177                this.targetUnits.push(entity);
    183178        }
    184179    }
    BuildingAI.prototype.FireArrows = function()  
    285280            this.arrowsLeft
    286281        );
    287282    }
    288     var clonedTargets = this.targetUnits.slice();
     283    if (arrowsToFire <= 0)
     284    {
     285        this.currentRound++;
     286        return;
     287    }
     288    var targets = new WeightedList();
     289    for (var i = 0; i < this.targetUnits.length; i++)
     290    {
     291        var target = this.targetUnits[i],
     292            weight = (cmpAttack.GetPreference(target) || 0) + 1
     293        targets.add(target, weight);
     294    }
    289295    for (var i = 0;i < arrowsToFire;i++)
    290296    {
    291         var target = clonedTargets[Math.floor(Math.random() * this.targetUnits.length)];
    292         if (target && this.CheckTargetVisible(target))
     297        var selectedIndex = targets.randomIndex(),
     298            selectedTarget = targets.itemAt(selectedIndex);
     299        if (selectedTarget && this.CheckTargetVisible(selectedTarget))
    293300        {
    294             cmpAttack.PerformAttack(attackType, target);
     301            cmpAttack.PerformAttack(attackType, selectedTarget);
    295302            PlaySound("attack", this.entity);
    296303        }
    297304        else
    298305        {
    299             clonedTargets.splice(clonedTargets.indexOf(target),1);
     306            targets.removeAt(selectedIndex);
    300307            i--; // one extra arrow left to fire
    301             if(clonedTargets.length < 1)
     308            if(targets.length() < 1)
    302309            {
    303310                this.arrowsLeft += arrowsToFire;
    304311                // no targets found in this round, save arrows and go to next round
  • new file inaries/data/mods/public/simulation/helpers/WeightedList.js

    diff --git a/binaries/data/mods/public/simulation/helpers/WeightedList.js b/binaries/data/mods/public/simulation/helpers/WeightedList.js
    new file mode 100644
    index 0000000..13d3e5f
    - +  
     1var WeightedList = function()
     2{
     3    this._elements = [ ];
     4    this._totalWeight = 0;
     5};
     6
     7WeightedList.prototype.length = function()
     8{
     9    return this._elements.length;
     10};
     11
     12WeightedList.prototype.add = function(item, weight)
     13{
     14    if (weight === undefined)
     15        weight = 1;
     16    this._totalWeight += weight;
     17    this._elements.push({ "item": item, "weight": weight });
     18};
     19
     20WeightedList.prototype.removeAt = function(index)
     21{
     22    var element = this._elements.splice(index, 1);
     23    if (element.length)
     24        this._totalWeight -= element[0].weight;
     25};
     26
     27WeightedList.prototype.itemAt = function(index)
     28{
     29    var element = this._elements[index];
     30    return element ? element.item : null;
     31};
     32
     33WeightedList.prototype.randomIndex = function() {
     34    var element,
     35    targetWeight = Math.random() * this._totalWeight,
     36    cumulativeWeight = 0;
     37    for (var index = 0; index < this._elements.length; index++)
     38    {
     39        element = this._elements[index];
     40        cumulativeWeight += element.weight;
     41        if (cumulativeWeight >= targetWeight)
     42            return index;
     43    }
     44    return -1;
     45};
     46
     47Engine.RegisterGlobal("WeightedList", WeightedList);