From 7f191887ecb845073e1381e433c277548e706e57 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 15 Aug 2010 04:53:23 +1200 Subject: [PATCH] Revamp cloak model --- OpenRA.Game/Traits/Targetable.cs | 14 +++++++-- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 + OpenRA.Mods.RA/Cloak.cs | 9 +++++- OpenRA.Mods.RA/Combat.cs | 2 +- OpenRA.Mods.RA/DetectCloaked.cs | 27 +---------------- OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 1 + OpenRA.Mods.RA/TargetableCloaked.cs | 40 ++++++++++++++++++++++++++ mods/ra/defaults.yaml | 2 ++ mods/ra/vehicles.yaml | 12 +++++--- 9 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 OpenRA.Mods.RA/TargetableCloaked.cs diff --git a/OpenRA.Game/Traits/Targetable.cs b/OpenRA.Game/Traits/Targetable.cs index c70d08949e..b788e4c98d 100644 --- a/OpenRA.Game/Traits/Targetable.cs +++ b/OpenRA.Game/Traits/Targetable.cs @@ -14,13 +14,23 @@ using System.Linq; namespace OpenRA.Traits { - public class TargetableInfo : TraitInfo + public class TargetableInfo : ITraitInfo { public readonly string[] TargetTypes = {}; + public object Create( ActorInitializer init ) { return new Targetable(this); } } - public class Targetable + public class Targetable : ITargetable { + TargetableInfo Info; + public Targetable(TargetableInfo info) + { + Info = info; + } + public string[] TargetTypes + { + get { return Info.TargetTypes;} + } } } diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 79e5882d7a..ca19fc01d7 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -37,6 +37,7 @@ namespace OpenRA.Traits public interface IResolveOrder { void ResolveOrder(Actor self, Order order); } public interface IOrderCursor { string CursorForOrder(Actor self, Order order); } public interface IOrderVoice { string VoicePhraseForOrder(Actor self, Order order); } + public interface ITargetable { string[] TargetTypes { get; } } public interface INotifySold { void Selling( Actor self ); void Sold( Actor self ); } public interface INotifyDamage { void Damaged(Actor self, AttackInfo e); } diff --git a/OpenRA.Mods.RA/Cloak.cs b/OpenRA.Mods.RA/Cloak.cs index 160caea9a4..b7162f9d5f 100644 --- a/OpenRA.Mods.RA/Cloak.cs +++ b/OpenRA.Mods.RA/Cloak.cs @@ -69,7 +69,7 @@ namespace OpenRA.Mods.RA if (remainingTime > 0) return rs; - if (self.Owner == self.World.LocalPlayer) + if (Cloaked && IsVisible(self, self.World.LocalPlayer)) return rs.Select(a => a.WithPalette("shadow")); else return new Renderable[] { }; @@ -94,6 +94,13 @@ namespace OpenRA.Mods.RA public bool Cloaked { get { return remainingTime == 0; } } + public bool IsVisible(Actor self, Player byPlayer) + { + if (!Cloaked || self.Owner.Stances[byPlayer] == Stance.Ally) + return true; + + return self.World.Queries.WithTrait().Any(a => a.Actor.Owner == byPlayer && (self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get().Range); + } public bool IsVisible(Actor self) { diff --git a/OpenRA.Mods.RA/Combat.cs b/OpenRA.Mods.RA/Combat.cs index a816cf1c24..8cca574c05 100755 --- a/OpenRA.Mods.RA/Combat.cs +++ b/OpenRA.Mods.RA/Combat.cs @@ -169,7 +169,7 @@ namespace OpenRA.Mods.RA || (weapon.ValidTargets.Contains("Water") && Game.world.GetTerrainType(Util.CellContaining(target.CenterLocation)) == "Water"); // even bigger hack! - var targetable = target.Actor.Info.Traits.GetOrDefault(); + var targetable = target.Actor.TraitOrDefault(); if (targetable == null || !weapon.ValidTargets.Intersect(targetable.TargetTypes).Any()) return false; diff --git a/OpenRA.Mods.RA/DetectCloaked.cs b/OpenRA.Mods.RA/DetectCloaked.cs index 44d7a66f41..f247638be8 100644 --- a/OpenRA.Mods.RA/DetectCloaked.cs +++ b/OpenRA.Mods.RA/DetectCloaked.cs @@ -15,33 +15,8 @@ namespace OpenRA.Mods.RA { class DetectCloakedInfo : TraitInfo { - public readonly int Interval = 12; // ~.5s - public readonly float DecloakTime = 2f; // 2s public readonly int Range = 5; - public readonly bool AffectOwnUnits = true; } - class DetectCloaked : ITick - { - [Sync] - int ticks; - - public void Tick(Actor self) - { - if (--ticks <= 0) - { - var info = self.Info.Traits.Get(); - ticks = info.Interval; - - var toDecloak = self.World.FindUnitsInCircle(self.CenterLocation, info.Range * Game.CellSize) - .Where(a => a.HasTrait()); - - if (!info.AffectOwnUnits) - toDecloak = toDecloak.Where(a => self.Owner.Stances[a.Owner] != Stance.Ally); - - foreach (var a in toDecloak) - a.Trait().Decloak((int)(25 * info.DecloakTime)); - } - } - } + class DetectCloaked {} } diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 475dd1b1e8..6540c965b9 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -234,6 +234,7 @@ + diff --git a/OpenRA.Mods.RA/TargetableCloaked.cs b/OpenRA.Mods.RA/TargetableCloaked.cs new file mode 100644 index 0000000000..3ba4bcb0dd --- /dev/null +++ b/OpenRA.Mods.RA/TargetableCloaked.cs @@ -0,0 +1,40 @@ +#region Copyright & License Information +/* + * Copyright 2007-2010 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, + * see LICENSE. + */ +#endregion + +using System.Drawing; +using OpenRA.Graphics; +using System.Linq; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + public class TargetableCloakedInfo : ITraitInfo, ITraitPrerequisite + { + public readonly string[] TargetTypes = {}; + public readonly string[] CloakedTargetTypes = {}; + public object Create( ActorInitializer init ) { return new TargetableCloaked(init.self, this); } + } + + public class TargetableCloaked : ITargetable + { + TargetableCloakedInfo Info; + Cloak Cloak; + public TargetableCloaked(Actor self, TargetableCloakedInfo info) + { + Info = info; + Cloak = self.Trait(); + } + + public string[] TargetTypes + { + get { return (Cloak.Cloaked) ? Info.CloakedTargetTypes : Info.TargetTypes;} + } + } +} diff --git a/mods/ra/defaults.yaml b/mods/ra/defaults.yaml index cb5a311030..1bbc006d7b 100644 --- a/mods/ra/defaults.yaml +++ b/mods/ra/defaults.yaml @@ -84,6 +84,8 @@ Voice: ShipVoice Targetable: TargetTypes: Ground, Water + DetectCloaked: + Range: 3 HiddenUnderFog: GainsExperience: GivesExperience: diff --git a/mods/ra/vehicles.yaml b/mods/ra/vehicles.yaml index 76b5c95fce..e244930412 100644 --- a/mods/ra/vehicles.yaml +++ b/mods/ra/vehicles.yaml @@ -391,8 +391,10 @@ SS: Speed: 5 RevealsShroud: Range: 6 - Targetable: - TargetTypes: Ground, Water, Underwater + -Targetable: + TargetableCloaked: + TargetTypes: Ground, Water + CloakedTargetTypes: Underwater RenderUnit: Cloak: InitialDelay: 0 @@ -426,8 +428,10 @@ MSUB: RevealsShroud: Range: 6 RenderUnit: - Targetable: - TargetTypes: Ground, Water, Underwater + -Targetable: + TargetableCloaked: + TargetTypes: Ground, Water + CloakedTargetTypes: Underwater Cloak: InitialDelay: 0 CloakDelay: 2.0