Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/BuildingAI.js
Show First 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | BuildingAI.prototype.SetupRangeQuery = function() | ||||
if (enemies.length && enemies[0] == 0) | if (enemies.length && enemies[0] == 0) | ||||
enemies.shift(); // remove gaia | enemies.shift(); // remove gaia | ||||
if (!enemies.length) | if (!enemies.length) | ||||
return; | return; | ||||
var range = cmpAttack.GetRange(attackType); | var range = cmpAttack.GetRange(attackType); | ||||
this.enemyUnitsQuery = cmpRangeManager.CreateActiveParabolicQuery( | this.enemyUnitsQuery = cmpRangeManager.CreateActiveParabolicQuery( | ||||
this.entity, range.min, range.max, range.elevationBonus, | this.entity, range.min, range.max, range.elevationBonus, | ||||
Lint: ESLintBear (indent): `Expected indentation of 2 tabs but found 3.` | |||||
enemies, IID_Resistance, cmpRangeManager.GetEntityFlagMask("normal")); | enemies, IID_Resistance, cmpRangeManager.GetEntityFlagMask("normal")); | ||||
Lint: ESLintBear (indent) Expected indentation of 2 tabs but found 3. Lint: ESLintBear (indent): `Expected indentation of 2 tabs but found 3.` | |||||
cmpRangeManager.EnableActiveQuery(this.enemyUnitsQuery); | cmpRangeManager.EnableActiveQuery(this.enemyUnitsQuery); | ||||
}; | }; | ||||
// Set up a range query for Gaia units within LOS range which can be attacked. | // Set up a range query for Gaia units within LOS range which can be attacked. | ||||
// This should be called whenever our ownership changes. | // This should be called whenever our ownership changes. | ||||
BuildingAI.prototype.SetupGaiaRangeQuery = function() | BuildingAI.prototype.SetupGaiaRangeQuery = function() | ||||
{ | { | ||||
Show All 11 Lines | BuildingAI.prototype.SetupGaiaRangeQuery = function() | ||||
var cmpPlayer = QueryOwnerInterface(this.entity); | var cmpPlayer = QueryOwnerInterface(this.entity); | ||||
if (!cmpPlayer || !cmpPlayer.IsEnemy(0)) | if (!cmpPlayer || !cmpPlayer.IsEnemy(0)) | ||||
return; | return; | ||||
var range = cmpAttack.GetRange(attackType); | var range = cmpAttack.GetRange(attackType); | ||||
// This query is only interested in Gaia entities that can attack. | // This query is only interested in Gaia entities that can attack. | ||||
this.gaiaUnitsQuery = cmpRangeManager.CreateActiveParabolicQuery( | this.gaiaUnitsQuery = cmpRangeManager.CreateActiveParabolicQuery( | ||||
this.entity, range.min, range.max, range.elevationBonus, | this.entity, range.min, range.max, range.elevationBonus, | ||||
Lint: ESLintBear (indent) Expected indentation of 2 tabs but found 3. Lint: ESLintBear (indent): `Expected indentation of 2 tabs but found 3.` | |||||
[0], IID_Attack, cmpRangeManager.GetEntityFlagMask("normal")); | [0], IID_Attack, cmpRangeManager.GetEntityFlagMask("normal")); | ||||
Lint: ESLintBear (indent) Expected indentation of 2 tabs but found 3. Lint: ESLintBear (indent): `Expected indentation of 2 tabs but found 3.` | |||||
cmpRangeManager.EnableActiveQuery(this.gaiaUnitsQuery); | cmpRangeManager.EnableActiveQuery(this.gaiaUnitsQuery); | ||||
}; | }; | ||||
/** | /** | ||||
* Called when units enter or leave range | * Called when units enter or leave range | ||||
*/ | */ | ||||
BuildingAI.prototype.OnRangeUpdate = function(msg) | BuildingAI.prototype.OnRangeUpdate = function(msg) | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | else | ||||
); | ); | ||||
if (arrowsToFire <= 0) | if (arrowsToFire <= 0) | ||||
{ | { | ||||
++this.currentRound; | ++this.currentRound; | ||||
return; | return; | ||||
} | } | ||||
// Add targets to a weighted list, to allow preferences | let thisCmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
if (!thisCmpPosition || !thisCmpPosition.IsInWorld()) | |||||
return false; | |||||
Lint: ESLintBear (consistent-return) Function expected no return value. Lint: ESLintBear (consistent-return): `Function expected no return value.` | |||||
// Add targets to a weighted list, to allow preferences. | |||||
let targets = new WeightedList(); | let targets = new WeightedList(); | ||||
let maxPreference = this.MAX_PREFERENCE_BONUS; | let maxPreference = this.MAX_PREFERENCE_BONUS; | ||||
let range = cmpAttack.GetRange(attackType); | |||||
let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); | |||||
let addTarget = function(target) | let addTarget = function(target) | ||||
{ | { | ||||
let preference = cmpAttack.GetPreference(target); | let preference = cmpAttack.GetPreference(target); | ||||
let weight = 1; | let weight = 1; | ||||
if (preference !== null && preference !== undefined) | if (preference !== null && preference !== undefined) | ||||
weight += maxPreference / (1 + preference); | weight += maxPreference / (1 + preference); | ||||
targets.push(target, weight); | targets.push(target, weight); | ||||
}; | }; | ||||
// Add the UnitAI target separately, as the UnitMotion and RangeManager implementations differ | // Add the UnitAI target separately, as the UnitMotion and RangeManager implementations differ. | ||||
if (this.unitAITarget && this.targetUnits.indexOf(this.unitAITarget) == -1) | if (this.unitAITarget && this.targetUnits.indexOf(this.unitAITarget) == -1) | ||||
addTarget(this.unitAITarget); | addTarget(this.unitAITarget); | ||||
for (let target of this.targetUnits) | for (let target of this.targetUnits) | ||||
addTarget(target); | addTarget(target); | ||||
Not Done Inline ActionsYou might want to add the filter here to avoid looping twice? wraitii: You might want to add the filter here to avoid looping twice? | |||||
for (let i = 0; i < arrowsToFire; ++i) | for (let firedArrows = 0; firedArrows < arrowsToFire; ++firedArrows) | ||||
{ | { | ||||
let selectedIndex = targets.randomIndex(); | let selectedIndex = targets.randomIndex(); | ||||
let selectedTarget = targets.itemAt(selectedIndex); | let selectedTarget = targets.itemAt(selectedIndex); | ||||
if (selectedTarget && this.CheckTargetVisible(selectedTarget)) | // Filter the entities on whether they are really in range. The rangeManager can return too many entities and entities outside vision. | ||||
if (selectedTarget) | |||||
Not Done Inline ActionsThis is wrong. Silier: This is wrong.
cmpAttack returns raw range data and ranged attacks use parabolic attack ranges. | |||||
{ | |||||
let targetCmpPosition = Engine.QueryInterface(selectedTarget, IID_Position); | |||||
if (targetCmpPosition && targetCmpPosition.IsInWorld() && cmpObstructionManager.IsInTargetRange( | |||||
Not Done Inline ActionsI assume we have no way to check whether it's in SquareRange to avoid the squareroot below? Also, already checked for selectedtarget. Stan: I assume we have no way to check whether it's in SquareRange to avoid the squareroot below? | |||||
Done Inline ActionsselectedTarget: fruits of rererewritting code The way to avoid sqrts would be to learn the squared checks to cpp, but iirc I ran into overflow issues when I tried it ~ 3 years ago bb: `selectedTarget`: fruits of rererewritting code
The way to avoid sqrts would be to learn the… | |||||
this.entity, | |||||
selectedTarget, | |||||
range.min, | |||||
Math.sqrt(Math.square(range.max) + 2 * range.max * (thisCmpPosition.GetPosition().y - targetCmpPosition.GetPosition().y + range.elevationBonus)), | |||||
false) && this.CheckTargetVisible(selectedTarget)) | |||||
{ | { | ||||
cmpAttack.PerformAttack(attackType, selectedTarget); | cmpAttack.PerformAttack(attackType, selectedTarget); | ||||
PlaySound("attack_" + attackType.toLowerCase(), this.entity); | PlaySound("attack_" + attackType.toLowerCase(), this.entity); | ||||
continue; | continue; | ||||
} | } | ||||
} | |||||
// Could not attack target, retry | // Could not attack target, retry. | ||||
targets.removeAt(selectedIndex); | targets.removeAt(selectedIndex); | ||||
--i; | |||||
if (!targets.length()) | if (!targets.length()) | ||||
{ | { | ||||
this.arrowsLeft += arrowsToFire; | this.arrowsLeft -= firedArrows; | ||||
break; | ++this.currentRound; | ||||
return; | |||||
} | } | ||||
// Discount an arrow since we didn't fire this one. | |||||
--firedArrows; | |||||
} | } | ||||
this.arrowsLeft -= arrowsToFire; | this.arrowsLeft -= arrowsToFire; | ||||
this.currentRound++; | ++this.currentRound; | ||||
}; | }; | ||||
/** | /** | ||||
* Returns true if the target entity is visible through the FoW/SoD. | * Returns true if the target entity is visible through the FoW/SoD. | ||||
*/ | */ | ||||
BuildingAI.prototype.CheckTargetVisible = function(target) | BuildingAI.prototype.CheckTargetVisible = function(target) | ||||
{ | { | ||||
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
Show All 14 Lines |
Wildfire Games · Phabricator
Expected indentation of 2 tabs but found 3.