From f4e4f987be9735b1d1de09d5045662a6c5289537 Mon Sep 17 00:00:00 2001 From: alzeih Date: Sun, 13 Mar 2011 15:37:24 +1300 Subject: [PATCH] RA - AppearsFriendlyTo and AppearsHostileTo actor extensions --- OpenRA.Mods.RA/ActorExts.cs | 51 ++++++++++++++++++++++++++++ OpenRA.Mods.RA/AttackBase.cs | 2 +- OpenRA.Mods.RA/AutoHeal.cs | 2 +- OpenRA.Mods.RA/AutoTarget.cs | 2 +- OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 1 + OpenRA.Mods.RA/Spy.cs | 2 +- 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 OpenRA.Mods.RA/ActorExts.cs diff --git a/OpenRA.Mods.RA/ActorExts.cs b/OpenRA.Mods.RA/ActorExts.cs new file mode 100644 index 0000000000..f214a89539 --- /dev/null +++ b/OpenRA.Mods.RA/ActorExts.cs @@ -0,0 +1,51 @@ +using System; +using OpenRA.Traits; +namespace OpenRA.Mods.RA +{ + public static class ActorExts + { + public static bool AppearsFriendlyTo(this Actor self, Actor toActor) + { + if (self.HasTrait()) + { + if (self.Trait().Disguised) + { + //TODO: check if we can see through disguise + if ( toActor.Owner.Stances[self.Trait().disguisedAsPlayer] == Stance.Ally) + return true; + } + else + { + if (toActor.Owner.Stances[self.Owner] == Stance.Ally) + return true; + } + return false; + } + return toActor.Owner.Stances[self.Owner] == Stance.Ally; + } + + public static bool AppearsHostileTo(this Actor self, Actor toActor) + { + if (self.HasTrait()) + { + if (toActor.Owner.Stances[self.Owner] == Stance.Ally) + return false; + + if (self.Trait().Disguised) + { + //TODO: check if we can see through disguise + if ( toActor.Owner.Stances[self.Trait().disguisedAsPlayer] == Stance.Enemy) + return true; + } + else + { + if (toActor.Owner.Stances[self.Owner] == Stance.Enemy) + return true; + } + return false; + } + return toActor.Owner.Stances[self.Owner] == Stance.Enemy; + } + } +} + diff --git a/OpenRA.Mods.RA/AttackBase.cs b/OpenRA.Mods.RA/AttackBase.cs index 8aa7d8c8df..7d4825a670 100644 --- a/OpenRA.Mods.RA/AttackBase.cs +++ b/OpenRA.Mods.RA/AttackBase.cs @@ -222,7 +222,7 @@ namespace OpenRA.Mods.RA var inRange = self.World.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range); return inRange - .Where(a => a.Owner != null && self.Owner.Stances[a.Owner] == Stance.Enemy) + .Where(a => a.Owner != null && a.AppearsHostileTo(self)) .Where(a => !a.HasTrait()) .Where(a => HasAnyValidWeapons(Target.FromActor(a))) .OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared) diff --git a/OpenRA.Mods.RA/AutoHeal.cs b/OpenRA.Mods.RA/AutoHeal.cs index 02288d8d31..dc4d6de743 100644 --- a/OpenRA.Mods.RA/AutoHeal.cs +++ b/OpenRA.Mods.RA/AutoHeal.cs @@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA var inRange = self.World.FindUnitsInCircle(self.CenterLocation, Game.CellSize * attack.GetMaximumRange()); var target = inRange - .Where(a => a != self && self.Owner.Stances[a.Owner] == Stance.Ally) + .Where(a => a != self && a.AppearsFriendlyTo(self)) .Where(a => a.IsInWorld && !a.IsDead()) .Where(a => a.HasTrait() && a.GetDamageState() > DamageState.Undamaged) .Where(a => attack.HasAnyValidWeapons(Target.FromActor(a))) diff --git a/OpenRA.Mods.RA/AutoTarget.cs b/OpenRA.Mods.RA/AutoTarget.cs index b95e307b41..0be9bf17ed 100644 --- a/OpenRA.Mods.RA/AutoTarget.cs +++ b/OpenRA.Mods.RA/AutoTarget.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA 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 (self.Owner.Stances[e.Attacker.Owner] == Stance.Ally) return; + if (e.Attacker.AppearsFriendlyTo(self)) return; if (e.Damage < 0) return; // don't retaliate against healers diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 8eab689ffd..ad3bceaab4 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -330,6 +330,7 @@ + diff --git a/OpenRA.Mods.RA/Spy.cs b/OpenRA.Mods.RA/Spy.cs index ef6d09a711..8efd3c59ca 100644 --- a/OpenRA.Mods.RA/Spy.cs +++ b/OpenRA.Mods.RA/Spy.cs @@ -115,7 +115,7 @@ namespace OpenRA.Mods.RA if (target != null && target.IsInWorld) { - var tooltip = target.Trait(); + var tooltip = target.TraitsImplementing().FirstOrDefault(); disguisedAsName = tooltip.Name(); disguisedAsPlayer = tooltip.Owner(); disguisedAsSprite = target.Trait().GetImage(target);