Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Attack.js
Show First 20 Lines • Show All 381 Lines • ▼ Show 20 Lines | Attack.prototype.GetFullAttackRange = function() | ||||
{ | { | ||||
let range = this.GetRange(type); | let range = this.GetRange(type); | ||||
ret.min = Math.min(ret.min, range.min); | ret.min = Math.min(ret.min, range.min); | ||||
ret.max = Math.max(ret.max, range.max); | ret.max = Math.max(ret.max, range.max); | ||||
} | } | ||||
return ret; | return ret; | ||||
}; | }; | ||||
/** | |||||
* Find the best attack against a target. | |||||
* @param {number} target - The entity-ID of the target. | |||||
* @param {boolean} allowCapture - Whether capturing is allowed. | |||||
* @return {string} - The preferred attack type. | |||||
*/ | |||||
Attack.prototype.GetBestAttackAgainst = function(target, allowCapture) | Attack.prototype.GetBestAttackAgainst = function(target, allowCapture) | ||||
{ | { | ||||
let cmpFormation = Engine.QueryInterface(target, IID_Formation); | let cmpFormation = Engine.QueryInterface(target, IID_Formation); | ||||
if (cmpFormation) | if (cmpFormation) | ||||
{ | { | ||||
// TODO: Formation against formation needs review | // TODO: Formation against formation needs review | ||||
let types = this.GetAttackTypes(); | let types = this.GetAttackTypes(); | ||||
return g_AttackTypes.find(attack => types.indexOf(attack) != -1); | return g_AttackTypes.find(attack => types.indexOf(attack) != -1); | ||||
} | } | ||||
let cmpIdentity = Engine.QueryInterface(target, IID_Identity); | let cmpIdentity = Engine.QueryInterface(target, IID_Identity); | ||||
if (!cmpIdentity) | if (!cmpIdentity) | ||||
return undefined; | return undefined; | ||||
let targetClasses = cmpIdentity.GetClassesList(); | |||||
let isTargetClass = className => targetClasses.indexOf(className) != -1; | |||||
// Always slaughter domestic animals instead of using a normal attack | // Always slaughter domestic animals instead of using a normal attack | ||||
if (isTargetClass("Domestic") && this.template.Slaughter) | if (this.template.Slaughter && cmpIdentity.HasClass("Domestic")) | ||||
return "Slaughter"; | return "Slaughter"; | ||||
let types = this.GetAttackTypes().filter(type => this.CanAttack(target, [type])); | let types = this.GetAttackTypes().filter(type => this.CanAttack(target, [type])); | ||||
// Check whether the target is capturable and prefer that when it is allowed. | // Check whether the target is capturable and prefer that when it is allowed. | ||||
let captureIndex = types.indexOf("Capture"); | let captureIndex = types.indexOf("Capture"); | ||||
if (captureIndex != -1) | if (captureIndex != -1) | ||||
{ | { | ||||
if (allowCapture) | if (allowCapture) | ||||
return "Capture"; | return "Capture"; | ||||
types.splice(captureIndex, 1); | types.splice(captureIndex, 1); | ||||
} | } | ||||
let isPreferred = className => this.GetPreferredClasses(className).some(isTargetClass); | let targetClasses = cmpIdentity.GetClassesList(); | ||||
let isPreferred = attackType => MatchesClassList(targetClasses, this.GetPreferredClasses(attackType)); | |||||
return types.sort((a, b) => | return types.sort((a, b) => | ||||
(types.indexOf(a) + (isPreferred(a) ? types.length : 0)) - | (types.indexOf(a) + (isPreferred(a) ? types.length : 0)) - | ||||
(types.indexOf(b) + (isPreferred(b) ? types.length : 0))).pop(); | (types.indexOf(b) + (isPreferred(b) ? types.length : 0))).pop(); | ||||
}; | }; | ||||
Attack.prototype.CompareEntitiesByPreference = function(a, b) | Attack.prototype.CompareEntitiesByPreference = function(a, b) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 295 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator