1
0
forked from 0ad/0ad

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
This was SVN commit r25276.
This commit is contained in:
Freagarach 2021-04-16 07:02:58 +00:00
parent 4329dd90ce
commit b3e3d1f715
2 changed files with 20 additions and 29 deletions

View File

@ -341,8 +341,7 @@ BuildingAI.prototype.FireArrows = function()
let firedArrows = 0;
while (firedArrows < arrowsToFire && targets.length())
{
let selectedIndex = targets.randomIndex();
let selectedTarget = targets.itemAt(selectedIndex);
let selectedTarget = targets.randomItem();
let targetCmpPosition = Engine.QueryInterface(selectedTarget, IID_Position);
if (targetCmpPosition && targetCmpPosition.IsInWorld() && this.CheckTargetVisible(selectedTarget))
@ -364,7 +363,7 @@ BuildingAI.prototype.FireArrows = function()
}
// Could not attack target, try a different target.
targets.removeAt(selectedIndex);
targets.remove(selectedTarget);
}
this.arrowsLeft -= firedArrows;

View File

@ -1,47 +1,39 @@
var WeightedList = function()
function WeightedList()
{
this.elements = [ ];
this.totalWeight = 0;
this.elements = new Map();
this.totalWeight = 0;
};
WeightedList.prototype.length = function()
{
return this.elements.length;
return this.elements.size;
};
WeightedList.prototype.push = function(item, weight)
WeightedList.prototype.push = function(item, weight = 1)
{
if (weight === undefined)
weight = 1;
this.elements.set(item, weight);
this.totalWeight += weight;
this.elements.push({ "item": item, "weight": weight });
};
WeightedList.prototype.removeAt = function(index)
WeightedList.prototype.remove = function(item)
{
var element = this.elements.splice(index, 1)[0];
if (element)
this.totalWeight -= element.weight;
const weight = this.elements.get(item);
if (weight)
this.totalWeight -= weight;
this.elements.delete(item);
};
WeightedList.prototype.itemAt = function(index)
WeightedList.prototype.randomItem = function()
{
var element = this.elements[index];
return element ? element.item : null;
};
WeightedList.prototype.randomIndex = function() {
var element;
var targetWeight = randFloat(0, this.totalWeight);
var cumulativeWeight = 0;
for (var index = 0; index < this.elements.length; index++)
const targetWeight = randFloat(0, this.totalWeight);
let cumulativeWeight = 0;
for (let [item, weight] of this.elements)
{
element = this.elements[index];
cumulativeWeight += element.weight;
cumulativeWeight += weight;
if (cumulativeWeight >= targetWeight)
return index;
return item;
}
return -1;
return undefined;
};
Engine.RegisterGlobal("WeightedList", WeightedList);