Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 3,206 Lines • ▼ Show 20 Lines | UnitAI.prototype.Init = function() | ||||
this.isGuardOf = undefined; | this.isGuardOf = undefined; | ||||
// For preventing increased action rate due to Stop orders or target death. | // For preventing increased action rate due to Stop orders or target death. | ||||
this.lastAttacked = undefined; | this.lastAttacked = undefined; | ||||
this.lastHealed = undefined; | this.lastHealed = undefined; | ||||
this.SetStance(this.template.DefaultStance); | this.SetStance(this.template.DefaultStance); | ||||
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | |||||
this.prefAttack = "Default"; | |||||
}; | }; | ||||
UnitAI.prototype.IsTurret = function() | UnitAI.prototype.IsTurret = function() | ||||
{ | { | ||||
if (!this.IsGarrisoned()) | if (!this.IsGarrisoned()) | ||||
return false; | return false; | ||||
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
return cmpPosition && cmpPosition.GetTurretParent() != INVALID_ENTITY; | return cmpPosition && cmpPosition.GetTurretParent() != INVALID_ENTITY; | ||||
▲ Show 20 Lines • Show All 1,333 Lines • ▼ Show 20 Lines | UnitAI.prototype.CheckTargetIsInVisionRange = function(target) | ||||
var distance = DistanceBetweenEntities(this.entity, target); | var distance = DistanceBetweenEntities(this.entity, target); | ||||
return distance < range; | return distance < range; | ||||
}; | }; | ||||
UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture) | UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture) | ||||
{ | { | ||||
var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | ||||
if (!cmpAttack) | if (!cmpAttack) | ||||
return undefined; | return undefined; | ||||
// First try to use a preferred attack type. | |||||
let prefAttack = this.GetPrefAttack(); | |||||
if (prefAttack != "Default" && cmpAttack.CanAttack(target, [prefAttack])) | |||||
return prefAttack; | |||||
return cmpAttack.GetBestAttackAgainst(target, allowCapture); | return cmpAttack.GetBestAttackAgainst(target, allowCapture); | ||||
}; | }; | ||||
/** | /** | ||||
* Try to find one of the given entities which can be attacked, | * Try to find one of the given entities which can be attacked, | ||||
* and start attacking it. | * and start attacking it. | ||||
* Returns true if it found something to attack. | * Returns true if it found something to attack. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 1,052 Lines • ▼ Show 20 Lines | UnitAI.prototype.GetSelectableStances = function() | ||||
return Object.keys(g_Stances).filter(key => g_Stances[key].selectable); | return Object.keys(g_Stances).filter(key => g_Stances[key].selectable); | ||||
}; | }; | ||||
UnitAI.prototype.GetStanceName = function() | UnitAI.prototype.GetStanceName = function() | ||||
{ | { | ||||
return this.stance; | return this.stance; | ||||
}; | }; | ||||
UnitAI.prototype.SetPrefAttack = function(attackType) | |||||
{ | |||||
if (this.GetSelectableAttacks().indexOf(attackType) != -1) | |||||
{ | |||||
this.prefAttack = attackType; | |||||
Engine.PostMessage(this.entity, MT_UnitPrefAttackChanged, { "to": this.prefAttack }); | |||||
Freagarach: I'm not entirely sure the message is needed? | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsIt is, for if we are attacking using ranged and the owner wants us to run after a fleeing target instead of missing 9/10 due to the range (s)he can set our preferred attack type to melee and we need to comply immediately. Freagarach: It is, for if we are attacking using ranged and the owner wants us to run after a fleeing… | |||||
// Reset the range queries, since the range depends on attack type. | |||||
this.SetupRangeQueries(); | |||||
} | |||||
}; | |||||
UnitAI.prototype.GetSelectableAttacks = function() | |||||
{ | |||||
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | |||||
if (cmpAttack) | |||||
return cmpAttack.GetAttackTypes().concat(["Default"]); | |||||
}; | |||||
UnitAI.prototype.GetPrefAttack = function() | |||||
{ | |||||
return this.prefAttack; | |||||
}; | |||||
/* | /* | ||||
* Make the unit walk at its normal pace. | * Make the unit walk at its normal pace. | ||||
*/ | */ | ||||
UnitAI.prototype.ResetSpeedMultiplier = function() | UnitAI.prototype.ResetSpeedMultiplier = function() | ||||
{ | { | ||||
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
if (cmpUnitMotion) | if (cmpUnitMotion) | ||||
cmpUnitMotion.SetSpeedMultiplier(1); | cmpUnitMotion.SetSpeedMultiplier(1); | ||||
▲ Show 20 Lines • Show All 393 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
I'm not entirely sure the message is needed?