Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Attack.js
Show First 20 Lines • Show All 309 Lines • ▼ Show 20 Lines | Attack.prototype.CanAttack = function(target, wantedTypes) | ||||
let targetOwner = cmpTargetPlayer.GetPlayerID(); | let targetOwner = cmpTargetPlayer.GetPlayerID(); | ||||
let cmpCapturable = QueryMiragedInterface(target, IID_Capturable); | let cmpCapturable = QueryMiragedInterface(target, IID_Capturable); | ||||
// Check if the relative height difference is larger than the attack range | // Check if the relative height difference is larger than the attack range | ||||
// If the relative height is bigger, it means they will never be able to | // If the relative height is bigger, it means they will never be able to | ||||
// reach each other, no matter how close they come. | // reach each other, no matter how close they come. | ||||
let heightDiff = Math.abs(cmpThisPosition.GetHeightOffset() - cmpTargetPosition.GetHeightOffset()); | let heightDiff = Math.abs(cmpThisPosition.GetHeightOffset() - cmpTargetPosition.GetHeightOffset()); | ||||
let armourStrengths; | |||||
let cmpTargetArmour = QueryMiragedInterface(target, IID_DamageReceiver); | |||||
if (cmpTargetArmour) | |||||
armourStrengths = cmpTargetArmour.GetArmourStrengths(); | |||||
for (let type of types) | for (let type of types) | ||||
{ | { | ||||
if (type != "Capture" && (!cmpEntityPlayer.IsEnemy(targetOwner) || !cmpHealth || !cmpHealth.GetHitpoints())) | if (type != "Capture" && (!cmpEntityPlayer.IsEnemy(targetOwner) || !cmpHealth || !cmpHealth.GetHitpoints())) | ||||
continue; | continue; | ||||
if (type == "Capture" && (!cmpCapturable || !cmpCapturable.CanCapture(entityOwner))) | if (type == "Capture" && (!cmpCapturable || !cmpCapturable.CanCapture(entityOwner))) | ||||
continue; | continue; | ||||
if (heightDiff > this.GetRange(type).max) | if (heightDiff > this.GetRange(type).max) | ||||
continue; | continue; | ||||
// Check if the target is immune to some damage types. | |||||
if (type != "Capture") | |||||
{ | |||||
let attackStrengths = this.GetAttackStrengths(type); | |||||
let canDamage = false; | |||||
for (let damageType in attackStrengths) | |||||
if (attackStrengths[damageType] != 0 && armourStrengths[damageType] != "Infinity") | |||||
{ | |||||
canDamage = true; | |||||
break; | |||||
} | |||||
if (!canDamage) | |||||
continue; | |||||
} | |||||
let restrictedClasses = this.GetRestrictedClasses(type); | let restrictedClasses = this.GetRestrictedClasses(type); | ||||
if (!restrictedClasses.length) | if (!restrictedClasses.length) | ||||
return true; | return true; | ||||
if (!MatchesClassList(targetClasses, restrictedClasses)) | if (!MatchesClassList(targetClasses, restrictedClasses)) | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | Attack.prototype.GetBestAttackAgainst = function(target, allowCapture) | ||||
let targetClasses = cmpIdentity.GetClassesList(); | let targetClasses = cmpIdentity.GetClassesList(); | ||||
let isTargetClass = className => targetClasses.indexOf(className) != -1; | 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 (isTargetClass("Domestic") && this.template.Slaughter) | ||||
return "Slaughter"; | return "Slaughter"; | ||||
let types = this.GetAttackTypes().filter(type => !this.GetRestrictedClasses(type).some(isTargetClass)); | let types = this.GetAttackTypes().filter(type => this.CanAttack(target, [type])); | ||||
// check if the target is capturable | // check if the target is capturable | ||||
let captureIndex = types.indexOf("Capture"); | let captureIndex = types.indexOf("Capture"); | ||||
if (captureIndex != -1) | |||||
{ | |||||
let cmpCapturable = QueryMiragedInterface(target, IID_Capturable); | |||||
let cmpPlayer = QueryOwnerInterface(this.entity); | let cmpPlayer = QueryOwnerInterface(this.entity); | ||||
if (allowCapture && cmpPlayer && cmpCapturable && cmpCapturable.CanCapture(cmpPlayer.GetPlayerID())) | if (captureIndex != -1 && allowCapture && cmpPlayer) | ||||
Silier: cmpPlayer is not needed here | |||||
return "Capture"; | return "Capture"; | ||||
// not capturable, so remove this attack | |||||
types.splice(captureIndex, 1); | |||||
} | |||||
let isPreferred = className => this.GetPreferredClasses(className).some(isTargetClass); | let isPreferred = className => this.GetPreferredClasses(className).some(isTargetClass); | ||||
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(); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 297 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
cmpPlayer is not needed here