Tidy AutoTarget code.

This commit is contained in:
Paul Chote
2014-01-19 10:25:46 +13:00
parent 7a77f3a755
commit 85500c0ec7
7 changed files with 46 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -8,10 +8,10 @@
*/ */
#endregion #endregion
using OpenRA.FileFormats;
using OpenRA.Traits;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -32,62 +32,72 @@ namespace OpenRA.Mods.RA
public object Create(ActorInitializer init) { return new AutoTarget(init.self, this); } public object Create(ActorInitializer init) { return new AutoTarget(init.self, this); }
} }
public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything }; public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything }
public class AutoTarget : INotifyIdle, INotifyDamage, ITick, IResolveOrder, ISync public class AutoTarget : INotifyIdle, INotifyDamage, ITick, IResolveOrder, ISync
{ {
readonly AutoTargetInfo Info; readonly AutoTargetInfo info;
readonly AttackBase attack; readonly AttackBase attack;
readonly AttackTurreted at; readonly AttackTurreted at;
[Sync] int nextScanTime = 0;
[Sync] public int nextScanTime = 0; public UnitStance Stance;
public UnitStance stance;
[Sync] public int stanceNumber { get { return (int)stance; } }
public UnitStance predictedStance; /* NOT SYNCED: do not refer to this anywhere other than UI code */
[Sync] public Actor Aggressor; [Sync] public Actor Aggressor;
[Sync] public Actor TargetedActor; [Sync] public Actor TargetedActor;
// NOT SYNCED: do not refer to this anywhere other than UI code
public UnitStance PredictedStance;
public AutoTarget(Actor self, AutoTargetInfo info) public AutoTarget(Actor self, AutoTargetInfo info)
{ {
Info = info; this.info = info;
attack = self.Trait<AttackBase>(); attack = self.Trait<AttackBase>();
stance = Info.InitialStance; Stance = info.InitialStance;
predictedStance = stance; PredictedStance = Stance;
at = self.TraitOrDefault<AttackTurreted>(); at = self.TraitOrDefault<AttackTurreted>();
} }
public void ResolveOrder(Actor self, Order order) public void ResolveOrder(Actor self, Order order)
{ {
if (order.OrderString == "SetUnitStance") if (order.OrderString == "SetUnitStance")
stance = (UnitStance)order.TargetLocation.X; Stance = (UnitStance)order.TargetLocation.X;
} }
public void Damaged(Actor self, AttackInfo e) public void Damaged(Actor self, AttackInfo e)
{ {
if (!self.IsIdle) return; if (!self.IsIdle)
if (e.Attacker.Destroyed) return; return;
if (stance < UnitStance.ReturnFire) return; if (e.Attacker.Destroyed)
return;
if (Stance < UnitStance.ReturnFire) return;
// not a lot we can do about things we can't hurt... although maybe we should automatically run away? // not a lot we can do about things we can't hurt... although maybe we should automatically run away?
if (!attack.HasAnyValidWeapons(Target.FromActor(e.Attacker))) return; if (!attack.HasAnyValidWeapons(Target.FromActor(e.Attacker)))
return;
// don't retaliate against own units force-firing on us. it's usually not what the player wanted. // don't retaliate against own units force-firing on us. It's usually not what the player wanted.
if (e.Attacker.AppearsFriendlyTo(self)) return; if (e.Attacker.AppearsFriendlyTo(self))
return;
if (e.Damage < 0) return; // don't retaliate against healers // don't retaliate against healers
if (e.Damage < 0)
return;
Aggressor = e.Attacker; Aggressor = e.Attacker;
if (at == null || !at.IsReachableTarget(at.Target, Info.AllowMovement && stance != UnitStance.Defend)) if (at == null || !at.IsReachableTarget(at.Target, info.AllowMovement && Stance != UnitStance.Defend))
Attack(self, e.Attacker); Attack(self, e.Attacker);
} }
public void TickIdle(Actor self) public void TickIdle(Actor self)
{ {
if (stance < UnitStance.Defend) return; if (Stance < UnitStance.Defend)
return;
if (at == null || !at.IsReachableTarget(at.Target, Info.AllowMovement && stance != UnitStance.Defend)) var allowMovement = info.AllowMovement && Stance != UnitStance.Defend;
if (at == null || !at.IsReachableTarget(at.Target, allowMovement))
ScanAndAttack(self); ScanAndAttack(self);
} }
@@ -99,7 +109,7 @@ namespace OpenRA.Mods.RA
public Actor ScanForTarget(Actor self, Actor currentTarget) public Actor ScanForTarget(Actor self, Actor currentTarget)
{ {
var range = Info.ScanRadius > 0 ? WRange.FromCells(Info.ScanRadius) : attack.GetMaximumRange(); var range = info.ScanRadius > 0 ? WRange.FromCells(info.ScanRadius) : attack.GetMaximumRange();
if (self.IsIdle || currentTarget == null || !Target.FromActor(currentTarget).IsInRange(self.CenterPosition, range)) if (self.IsIdle || currentTarget == null || !Target.FromActor(currentTarget).IsInRange(self.CenterPosition, range))
if (nextScanTime <= 0) if (nextScanTime <= 0)
return ChooseTarget(self, range); return ChooseTarget(self, range);
@@ -119,12 +129,12 @@ namespace OpenRA.Mods.RA
TargetedActor = targetActor; TargetedActor = targetActor;
var target = Target.FromActor(targetActor); var target = Target.FromActor(targetActor);
self.SetTargetLine(target, Color.Red, false); self.SetTargetLine(target, Color.Red, false);
attack.AttackTarget(target, false, Info.AllowMovement && stance != UnitStance.Defend); attack.AttackTarget(target, false, info.AllowMovement && Stance != UnitStance.Defend);
} }
Actor ChooseTarget(Actor self, WRange range) Actor ChooseTarget(Actor self, WRange range)
{ {
nextScanTime = self.World.SharedRandom.Next(Info.MinimumScanTimeInterval, Info.MaximumScanTimeInterval); nextScanTime = self.World.SharedRandom.Next(info.MinimumScanTimeInterval, info.MaximumScanTimeInterval);
var inRange = self.World.FindActorsInCircle(self.CenterPosition, range); var inRange = self.World.FindActorsInCircle(self.CenterPosition, range);
if (self.Owner.HasFogVisibility()) if (self.Owner.HasFogVisibility())
@@ -150,5 +160,4 @@ namespace OpenRA.Mods.RA
[Desc("Will not get automatically targeted by enemy (like walls)")] [Desc("Will not get automatically targeted by enemy (like walls)")]
class AutoTargetIgnoreInfo : TraitInfo<AutoTargetIgnore> { } class AutoTargetIgnoreInfo : TraitInfo<AutoTargetIgnore> { }
class AutoTargetIgnore { } class AutoTargetIgnore { }
} }

View File

@@ -272,7 +272,7 @@ namespace OpenRA.Mods.RA.Missions
void SetAlliedUnitsToDefensiveStance() void SetAlliedUnitsToDefensiveStance()
{ {
foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == allies && !a.IsDead() && a.HasTrait<AutoTarget>())) foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == allies && !a.IsDead() && a.HasTrait<AutoTarget>()))
actor.Trait<AutoTarget>().stance = UnitStance.Defend; actor.Trait<AutoTarget>().Stance = UnitStance.Defend;
} }
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)

View File

@@ -349,7 +349,7 @@ namespace OpenRA.Mods.RA.Missions
void SetupSubStances() void SetupSubStances()
{ {
foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait<TargetableSubmarine>())) foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait<TargetableSubmarine>()))
actor.Trait<AutoTarget>().stance = UnitStance.Defend; actor.Trait<AutoTarget>().Stance = UnitStance.Defend;
} }
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
@@ -290,7 +290,7 @@ namespace OpenRA.Mods.RA.Missions
foreach (var actor in actors.Values) foreach (var actor in actors.Values)
{ {
if (actor.Owner == allies && actor.HasTrait<AutoTarget>()) if (actor.Owner == allies && actor.HasTrait<AutoTarget>())
actor.Trait<AutoTarget>().stance = UnitStance.Defend; actor.Trait<AutoTarget>().Stance = UnitStance.Defend;
if (actor.IsInWorld && (actor.HasTrait<Bridge>() || actor.Owner == allies || (actor.Owner == soviets && actor.HasTrait<Building>()))) if (actor.IsInWorld && (actor.HasTrait<Bridge>() || actor.Owner == allies || (actor.Owner == soviets && actor.HasTrait<Building>())))
actor.AddTrait(new Invulnerable()); actor.AddTrait(new Invulnerable());

View File

@@ -123,7 +123,7 @@ namespace OpenRA.Mods.RA.Missions
void SetSovietUnitsToDefensiveStance() void SetSovietUnitsToDefensiveStance()
{ {
foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait<AutoTarget>())) foreach (var actor in world.Actors.Where(a => a.IsInWorld && a.Owner == soviets && !a.IsDead() && a.HasTrait<AutoTarget>()))
actor.Trait<AutoTarget>().stance = UnitStance.Defend; actor.Trait<AutoTarget>().Stance = UnitStance.Defend;
} }
Actor FirstUnshroudedOrDefault(IEnumerable<Actor> actors, World world, int shroudRange) Actor FirstUnshroudedOrDefault(IEnumerable<Actor> actors, World world, int shroudRange)

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
@@ -283,7 +283,7 @@ namespace OpenRA.Mods.RA.Scripting
{ {
var at = actor.TraitOrDefault<AutoTarget>(); var at = actor.TraitOrDefault<AutoTarget>();
if (at != null) if (at != null)
at.stance = Enum<UnitStance>.Parse(stance); at.Stance = Enum<UnitStance>.Parse(stance);
} }
[LuaGlobal] [LuaGlobal]

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
@@ -150,7 +150,7 @@ namespace OpenRA.Mods.RA.Widgets
var stances = Enum<UnitStance>.GetValues(); var stances = Enum<UnitStance>.GetValues();
var nextStance = stances.Concat(stances) var nextStance = stances.Concat(stances)
.SkipWhile(s => s != actor.Second.predictedStance) .SkipWhile(s => s != actor.Second.PredictedStance)
.Skip(1) .Skip(1)
.First(); .First();
@@ -158,7 +158,7 @@ namespace OpenRA.Mods.RA.Widgets
{ {
var at = a.TraitOrDefault<AutoTarget>(); var at = a.TraitOrDefault<AutoTarget>();
if (at != null) if (at != null)
at.predictedStance = nextStance; at.PredictedStance = nextStance;
// FIXME: Abuse of the type system here with `CPos` // FIXME: Abuse of the type system here with `CPos`
return new Order("SetUnitStance", a, false) { TargetLocation = new CPos((int)nextStance, 0) }; return new Order("SetUnitStance", a, false) { TargetLocation = new CPos((int)nextStance, 0) };