diff --git a/binaries/data/mods/public/simulation/components/Attack.js b/binaries/data/mods/public/simulation/components/Attack.js
index 1159ebc0d9..86e363a551 100644
--- a/binaries/data/mods/public/simulation/components/Attack.js
+++ b/binaries/data/mods/public/simulation/components/Attack.js
@@ -475,7 +475,7 @@ Attack.prototype.RepeatRangeCheck = function(type) {
*
* @return {boolean} - Whether we started attacking.
*/
-Attack.prototype.StartAttacking = function(target, type, callerIID)
+Attack.prototype.StartAttacking = function(target, type, callerIID, force)
{
if (this.target)
this.StopAttacking();
@@ -525,6 +525,7 @@ Attack.prototype.StartAttacking = function(target, type, callerIID)
this.resyncAnimation = prepare != timings.prepare;
this.target = target;
this.callerIID = callerIID;
+ this.force = force;
this.timer = cmpTimer.SetInterval(this.entity, IID_Attack, "Attack", prepare, timings.repeat, type);
this.checkTimer = cmpTimer.SetInterval(this.entity, IID_Attack, "RepeatRangeCheck", checkStart, repeatPerCheck, type);
return true;
@@ -600,6 +601,13 @@ Attack.prototype.Attack = function(type, lateness)
return;
}
+ // If a low preference unit is attacked without player direction, check for higher preference.
+ if (this.GetPreference(this.target) === undefined && !this.force)
+ {
+ this.StopAttacking("TargetInvalidated");
+ return;
+ }
+
if (this.resyncAnimation)
{
const cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
index c0c61a75cc..d6bd180f9d 100644
--- a/binaries/data/mods/public/simulation/components/UnitAI.js
+++ b/binaries/data/mods/public/simulation/components/UnitAI.js
@@ -2198,7 +2198,7 @@ UnitAI.prototype.UnitFsmSpec = {
if (this.order.data.hunting && this.orderQueue.length > 1 && this.orderQueue[1].type === "Gather")
this.RememberTargetPosition(this.orderQueue[1].data);
- if (!cmpAttack.StartAttacking(this.order.data.target, this.order.data.attackType, IID_UnitAI))
+ if (!cmpAttack.StartAttacking(this.order.data.target, this.order.data.attackType, IID_UnitAI, this.order.data.force))
{
this.ProcessMessage("TargetInvalidated");
return true;
@@ -2250,7 +2250,7 @@ UnitAI.prototype.UnitFsmSpec = {
if (this.order.data.attackType == "Capture" && (this.GetStance().targetAttackersAlways || !this.order.data.force) &&
this.order.data.target != msg.data.attacker && this.GetBestAttackAgainst(msg.data.attacker, true) != "Capture")
this.RespondToTargetedEntities([msg.data.attacker]);
- },
+ }
},
"FINDINGNEWTARGET": {
diff --git a/binaries/data/mods/public/simulation/templates/template_unit_champion_elephant_melee.xml b/binaries/data/mods/public/simulation/templates/template_unit_champion_elephant_melee.xml
index 119594143a..2081599e49 100644
--- a/binaries/data/mods/public/simulation/templates/template_unit_champion_elephant_melee.xml
+++ b/binaries/data/mods/public/simulation/templates/template_unit_champion_elephant_melee.xml
@@ -19,7 +19,7 @@
5
750
1500
- !Ship
+ Unit+!Ship