Tidy AutoTarget code.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#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
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
@@ -8,10 +8,10 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Traits;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
@@ -32,62 +32,72 @@ namespace OpenRA.Mods.RA
|
||||
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
|
||||
{
|
||||
readonly AutoTargetInfo Info;
|
||||
readonly AutoTargetInfo info;
|
||||
readonly AttackBase attack;
|
||||
readonly AttackTurreted at;
|
||||
[Sync] int nextScanTime = 0;
|
||||
|
||||
[Sync] public int nextScanTime = 0;
|
||||
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 */
|
||||
public UnitStance Stance;
|
||||
[Sync] public Actor Aggressor;
|
||||
[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)
|
||||
{
|
||||
Info = info;
|
||||
this.info = info;
|
||||
attack = self.Trait<AttackBase>();
|
||||
stance = Info.InitialStance;
|
||||
predictedStance = stance;
|
||||
Stance = info.InitialStance;
|
||||
PredictedStance = Stance;
|
||||
at = self.TraitOrDefault<AttackTurreted>();
|
||||
}
|
||||
|
||||
public void ResolveOrder(Actor self, Order order)
|
||||
{
|
||||
if (order.OrderString == "SetUnitStance")
|
||||
stance = (UnitStance)order.TargetLocation.X;
|
||||
Stance = (UnitStance)order.TargetLocation.X;
|
||||
}
|
||||
|
||||
public void Damaged(Actor self, AttackInfo e)
|
||||
{
|
||||
if (!self.IsIdle) return;
|
||||
if (e.Attacker.Destroyed) return;
|
||||
if (!self.IsIdle)
|
||||
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?
|
||||
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.
|
||||
if (e.Attacker.AppearsFriendlyTo(self)) return;
|
||||
// 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.Damage < 0) return; // don't retaliate against healers
|
||||
// don't retaliate against healers
|
||||
if (e.Damage < 0)
|
||||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -99,7 +109,7 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
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 (nextScanTime <= 0)
|
||||
return ChooseTarget(self, range);
|
||||
@@ -119,12 +129,12 @@ namespace OpenRA.Mods.RA
|
||||
TargetedActor = targetActor;
|
||||
var target = Target.FromActor(targetActor);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
if (self.Owner.HasFogVisibility())
|
||||
@@ -150,5 +160,4 @@ namespace OpenRA.Mods.RA
|
||||
[Desc("Will not get automatically targeted by enemy (like walls)")]
|
||||
class AutoTargetIgnoreInfo : TraitInfo<AutoTargetIgnore> { }
|
||||
class AutoTargetIgnore { }
|
||||
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ namespace OpenRA.Mods.RA.Missions
|
||||
void SetAlliedUnitsToDefensiveStance()
|
||||
{
|
||||
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)
|
||||
|
||||
@@ -349,7 +349,7 @@ namespace OpenRA.Mods.RA.Missions
|
||||
void SetupSubStances()
|
||||
{
|
||||
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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#region Copyright & License Information
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* 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)
|
||||
{
|
||||
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>())))
|
||||
actor.AddTrait(new Invulnerable());
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace OpenRA.Mods.RA.Missions
|
||||
void SetSovietUnitsToDefensiveStance()
|
||||
{
|
||||
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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#region Copyright & License Information
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* 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>();
|
||||
if (at != null)
|
||||
at.stance = Enum<UnitStance>.Parse(stance);
|
||||
at.Stance = Enum<UnitStance>.Parse(stance);
|
||||
}
|
||||
|
||||
[LuaGlobal]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#region Copyright & License Information
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* 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 nextStance = stances.Concat(stances)
|
||||
.SkipWhile(s => s != actor.Second.predictedStance)
|
||||
.SkipWhile(s => s != actor.Second.PredictedStance)
|
||||
.Skip(1)
|
||||
.First();
|
||||
|
||||
@@ -158,7 +158,7 @@ namespace OpenRA.Mods.RA.Widgets
|
||||
{
|
||||
var at = a.TraitOrDefault<AutoTarget>();
|
||||
if (at != null)
|
||||
at.predictedStance = nextStance;
|
||||
at.PredictedStance = nextStance;
|
||||
|
||||
// FIXME: Abuse of the type system here with `CPos`
|
||||
return new Order("SetUnitStance", a, false) { TargetLocation = new CPos((int)nextStance, 0) };
|
||||
|
||||
Reference in New Issue
Block a user