Changeset 25276

Timestamp:
Apr 16, 2021, 9:02:58 AM (3 years ago)
Author:
Freagarach
Message:

Use a map for the weighted list.

Less object creations at addition time, more intuitive handling.

Differential revision: https://code.wildfiregames.com/D3854
Comments by: @Stan, @wraitii

Location:
ps/trunk/binaries/data/mods/public/simulation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/binaries/data/mods/public/simulation/components/BuildingAI.js

    r25123 r25276  
    342342    while (firedArrows < arrowsToFire && targets.length())
    343343    {
    344         let selectedIndex = targets.randomIndex();
    345         let selectedTarget = targets.itemAt(selectedIndex);
     344        let selectedTarget = targets.randomItem();
    346345
    347346        let targetCmpPosition = Engine.QueryInterface(selectedTarget, IID_Position);
     
    365364
    366365        // Could not attack target, try a different target.
    367         targets.removeAt(selectedIndex);
     366        targets.remove);
    368367    }
    369368
  • ps/trunk/binaries/data/mods/public/simulation/helpers/WeightedList.js

    r19270 r25276  
    1 var WeightedList = function()
     1()
    22{
    3     this.elements = [ ];
    4     this.totalWeight = 0;
     3;
     4this.totalWeight = 0;
    55};
    66
    77WeightedList.prototype.length = function()
    88{
    9     return this.elements.length;
     9    return this.elements.;
    1010};
    1111
    12 WeightedList.prototype.push = function(item, weight)
     12WeightedList.prototype.push = function(item, weight)
    1313{
    14     if (weight === undefined)
    15         weight = 1;
     14    this.elements.set(item, weight);
    1615    this.totalWeight += weight;
    17     this.elements.push({ "item": item, "weight": weight });
    1816};
    1917
    20 WeightedList.prototype.removeAt = function(index)
     18WeightedList.prototype.remove)
    2119{
    22     var element = this.elements.splice(index, 1)[0];
    23     if (element)
    24         this.totalWeight -= element.weight;
     20    const weight = this.elements.get(item);
     21    if (weight)
     22        this.totalWeight -= weight;
     23    this.elements.delete(item);
    2524};
    2625
    27 WeightedList.prototype.itemAt = function(index)
     26WeightedList.prototype.)
    2827{
    29     var element = this.elements[index];
    30     return element ? element.item : null;
    31 };
    32 
    33 WeightedList.prototype.randomIndex = function() {
    34     var element;
    35     var targetWeight = randFloat(0, this.totalWeight);
    36     var cumulativeWeight = 0;
    37     for (var index = 0; index < this.elements.length; index++)
     28    const targetWeight = randFloat(0, this.totalWeight);
     29    let cumulativeWeight = 0;
     30    for (let [item, weight] of this.elements)
    3831    {
    39         element = this.elements[index];
    40         cumulativeWeight += element.weight;
     32        cumulativeWeight += weight;
    4133        if (cumulativeWeight >= targetWeight)
    42             return index;
     34            return i;
    4335    }
    44     return -1;
     36    return ;
    4537};
    4638
Note: See TracChangeset for help on using the changeset viewer.