mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-07-04 05:55:47 -07:00
Add preferred classes to BuildingAI. Patch by jammus. Fixes #1985
This was SVN commit r13823.
This commit is contained in:
parent
43df87e56f
commit
508cc0d4c7
2 changed files with 66 additions and 12 deletions
|
|
@ -169,16 +169,11 @@ BuildingAI.prototype.OnRangeUpdate = function(msg)
|
||||||
else if (msg.tag != this.enemyUnitsQuery)
|
else if (msg.tag != this.enemyUnitsQuery)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const restrictedClasses = cmpAttack.GetRestrictedClasses(attackType);
|
|
||||||
|
|
||||||
if (msg.added.length > 0)
|
if (msg.added.length > 0)
|
||||||
{
|
{
|
||||||
for each (var entity in msg.added)
|
for each (var entity in msg.added)
|
||||||
{
|
{
|
||||||
var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
|
if (cmpAttack.CanAttack(entity))
|
||||||
var targetClasses = cmpIdentity.GetClassesList();
|
|
||||||
|
|
||||||
if (!targetClasses.some(function(c){return restrictedClasses.indexOf(c) > -1;}))
|
|
||||||
this.targetUnits.push(entity);
|
this.targetUnits.push(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -285,20 +280,32 @@ BuildingAI.prototype.FireArrows = function()
|
||||||
this.arrowsLeft
|
this.arrowsLeft
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
var clonedTargets = this.targetUnits.slice();
|
if (arrowsToFire <= 0)
|
||||||
|
{
|
||||||
|
this.currentRound++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var targets = new WeightedList();
|
||||||
|
for (var i = 0; i < this.targetUnits.length; i++)
|
||||||
|
{
|
||||||
|
var target = this.targetUnits[i],
|
||||||
|
weight = (cmpAttack.GetPreference(target) || 0) + 1
|
||||||
|
targets.add(target, weight);
|
||||||
|
}
|
||||||
for (var i = 0;i < arrowsToFire;i++)
|
for (var i = 0;i < arrowsToFire;i++)
|
||||||
{
|
{
|
||||||
var target = clonedTargets[Math.floor(Math.random() * this.targetUnits.length)];
|
var selectedIndex = targets.randomIndex(),
|
||||||
if (target && this.CheckTargetVisible(target))
|
selectedTarget = targets.itemAt(selectedIndex);
|
||||||
|
if (selectedTarget && this.CheckTargetVisible(selectedTarget))
|
||||||
{
|
{
|
||||||
cmpAttack.PerformAttack(attackType, target);
|
cmpAttack.PerformAttack(attackType, selectedTarget);
|
||||||
PlaySound("attack", this.entity);
|
PlaySound("attack", this.entity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
clonedTargets.splice(clonedTargets.indexOf(target),1);
|
targets.removeAt(selectedIndex);
|
||||||
i--; // one extra arrow left to fire
|
i--; // one extra arrow left to fire
|
||||||
if(clonedTargets.length < 1)
|
if(targets.length() < 1)
|
||||||
{
|
{
|
||||||
this.arrowsLeft += arrowsToFire;
|
this.arrowsLeft += arrowsToFire;
|
||||||
// no targets found in this round, save arrows and go to next round
|
// no targets found in this round, save arrows and go to next round
|
||||||
|
|
|
||||||
47
binaries/data/mods/public/simulation/helpers/WeightedList.js
Normal file
47
binaries/data/mods/public/simulation/helpers/WeightedList.js
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
var WeightedList = function()
|
||||||
|
{
|
||||||
|
this.elements = [ ];
|
||||||
|
this.totalWeight = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
WeightedList.prototype.length = function()
|
||||||
|
{
|
||||||
|
return this.elements.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
WeightedList.prototype.push = function(item, weight)
|
||||||
|
{
|
||||||
|
if (weight === undefined)
|
||||||
|
weight = 1;
|
||||||
|
this.totalWeight += weight;
|
||||||
|
this.elements.push({ "item": item, "weight": weight });
|
||||||
|
};
|
||||||
|
|
||||||
|
WeightedList.prototype.removeAt = function(index)
|
||||||
|
{
|
||||||
|
var element = this.elements.splice(index, 1)[0];
|
||||||
|
if (element)
|
||||||
|
this.totalWeight -= element.weight;
|
||||||
|
};
|
||||||
|
|
||||||
|
WeightedList.prototype.itemAt = function(index)
|
||||||
|
{
|
||||||
|
var element = this.elements[index];
|
||||||
|
return element ? element.item : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
WeightedList.prototype.randomIndex = function() {
|
||||||
|
var element,
|
||||||
|
targetWeight = Math.random() * this.totalWeight,
|
||||||
|
cumulativeWeight = 0;
|
||||||
|
for (var index = 0; index < this.elements.length; index++)
|
||||||
|
{
|
||||||
|
element = this.elements[index];
|
||||||
|
cumulativeWeight += element.weight;
|
||||||
|
if (cumulativeWeight >= targetWeight)
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
Engine.RegisterGlobal("WeightedList", WeightedList);
|
||||||
Loading…
Reference in a new issue