autotarget retal; most units are aggressive now

This commit is contained in:
Chris Forbes
2009-12-28 10:49:20 +13:00
parent ba16424bc1
commit 579a46d63f
2 changed files with 65 additions and 39 deletions

View File

@@ -5,15 +5,24 @@ using System.Text;
namespace OpenRa.Game.Traits
{
class AutoTarget : ITick
class AutoTarget : ITick, INotifyDamage
{
public AutoTarget(Actor self) {}
public void Tick(Actor self)
void AttackTarget(Actor self, Actor target)
{
var attack = self.traits.WithInterface<AttackBase>().First();
if (target != null)
attack.ResolveOrder(self, new Order("Attack", self, target, int2.Zero, null));
}
public void Tick(Actor self)
{
if (!self.IsIdle) return;
var attack = self.traits.WithInterface<AttackBase>().First();
var range = Rules.WeaponInfo[self.Info.Primary].Range;
if (attack.target == null ||
(attack.target.Location - self.Location).LengthSquared > range * range + 2)
attack.target = ChooseTarget(self, range);
@@ -27,5 +36,20 @@ namespace OpenRa.Game.Traits
.OrderBy(a => (a.Location - self.Location).LengthSquared)
.FirstOrDefault();
}
public void Damaged(Actor self, AttackInfo e)
{
// not a lot we can do about things we can't hurt... although maybe we should automatically run away?
if (!Combat.HasAnyValidWeapons(self, e.Attacker))
return;
if (e.Attacker.Owner == self.Owner)
return; // don't retaliate against own units force-firing on us. it's usually not what the player wanted.
var attack = self.traits.WithInterface<AttackBase>().First();
if (attack.target != null) return;
AttackTarget(self, e.Attacker);
}
}
}