1
0
forked from mirrors/0ad

Let units attacking low preference units without direction check for high preference units

-During each attack on a low preference target unless player-directed, update the range query and find a new target.
-Adjust behavior for elephants to work better versus infantry.
fixes #1488
This commit is contained in:
real_tabasco_sauce
2025-08-17 05:55:25 -07:00
committed by real_tabasco_sauce
parent 3b0bf7fe75
commit 18ef4b6a1a
3 changed files with 12 additions and 4 deletions
@@ -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);
@@ -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": {
@@ -19,7 +19,7 @@
<MaxRange>5</MaxRange>
<PrepareTime>750</PrepareTime>
<RepeatTime>1500</RepeatTime>
<PreferredClasses datatype="tokens">!Ship</PreferredClasses>
<PreferredClasses datatype="tokens">Unit+!Ship</PreferredClasses>
</Melee>
</Attack>
<Identity>