From 12ac96a8853b9955a9bb769c88c8ed78a875f0f0 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 8 Dec 2009 21:12:25 +1300 Subject: [PATCH] added AutoTarget trait --- OpenRa.Game/Game.cs | 1 - OpenRa.Game/OpenRa.Game.csproj | 1 + OpenRa.Game/Traits/AutoTarget.cs | 30 ++++++++++++++++++++++++++++++ units.ini | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 OpenRa.Game/Traits/AutoTarget.cs diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index efe5d1610b..e9cbb0011a 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -239,7 +239,6 @@ namespace OpenRa.Game .FirstOrDefault(); } - public static Random SharedRandom = new Random(0); /* for things that require sync */ public static Random CosmeticRandom = new Random(); /* for things that are just fluff */ diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index d1c5f7b95b..0aa18fa011 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -157,6 +157,7 @@ + diff --git a/OpenRa.Game/Traits/AutoTarget.cs b/OpenRa.Game/Traits/AutoTarget.cs new file mode 100644 index 0000000000..9ecc2f2f39 --- /dev/null +++ b/OpenRa.Game/Traits/AutoTarget.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OpenRa.Game.Traits +{ + class AutoTarget : ITick + { + public AutoTarget(Actor self) {} + + public void Tick(Actor self) + { + var attack = self.traits.WithInterface().First(); + + if (attack.target == null) + attack.target = ChooseTarget(self, + Rules.WeaponInfo[self.Info.Primary].Range); + } + + Actor ChooseTarget(Actor self, float range) + { + var inRange = Game.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range); + + return inRange.Where(a => a.Owner != self.Owner) /* todo: one day deal with friendly players */ + .OrderBy(a => (a.Location - self.Location).LengthSquared) + .FirstOrDefault(); + } + } +} diff --git a/units.ini b/units.ini index c9a405dd94..8b2b01bbe5 100755 --- a/units.ini +++ b/units.ini @@ -278,7 +278,7 @@ Footprint=_ x SelectionPriority=3 [GUN] Description=Turret -Traits=Building, Turreted, RenderBuildingTurreted, AttackTurreted +Traits=Building, Turreted, RenderBuildingTurreted, AttackTurreted, AutoTarget Dimensions=1,1 Footprint=x SelectionPriority=3