From b4cecff74e9c28762be903239aaf60bc459aa79b Mon Sep 17 00:00:00 2001 From: Taryn Hill Date: Wed, 9 Jul 2014 20:38:06 -0500 Subject: [PATCH] ClonesProductionUnits: string[] CloneableTypes checks Cloneable.Types Cloneable: string[] Types checked by ClonesProductionUnits.CloneableTypes Added INotifyOtherProduction for notifying self when another actor produces a unit. --- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 + .../Buildings/ClonesProducedUnits.cs | 50 +++++++++++++++++++ OpenRA.Mods.RA/Cloneable.cs | 24 +++++++++ OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 2 + OpenRA.Mods.RA/Production.cs | 7 +++ mods/ra/rules/defaults.yaml | 2 + 6 files changed, 86 insertions(+) create mode 100644 OpenRA.Mods.RA/Buildings/ClonesProducedUnits.cs create mode 100644 OpenRA.Mods.RA/Cloneable.cs diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index b079dd26ea..563acaeece 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -74,6 +74,7 @@ namespace OpenRA.Traits public interface INotifyBuildComplete { void BuildingComplete(Actor self); } public interface INotifyBuildingPlaced { void BuildingPlaced(Actor self); } public interface INotifyProduction { void UnitProduced(Actor self, Actor other, CPos exit); } + public interface INotifyOtherProduction { void UnitProducedByOther(Actor self, Actor producer, Actor produced); } public interface INotifyDelivery { void IncomingDelivery(Actor self); void Delivered(Actor self); } public interface INotifyOwnerChanged { void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner); } public interface INotifyEffectiveOwnerChanged { void OnEffectiveOwnerChanged(Actor self, Player oldEffectiveOwner, Player newEffectiveOwner); } diff --git a/OpenRA.Mods.RA/Buildings/ClonesProducedUnits.cs b/OpenRA.Mods.RA/Buildings/ClonesProducedUnits.cs new file mode 100644 index 0000000000..f2caec1fce --- /dev/null +++ b/OpenRA.Mods.RA/Buildings/ClonesProducedUnits.cs @@ -0,0 +1,50 @@ + #region Copyright & License Information + /* + * 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, + * see COPYING. + */ + #endregion + +using System; +using System.Linq; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + [Desc("Creates a free duplicate of produced units.")] + public class ClonesProducedUnitsInfo : ITraitInfo, Requires, Requires + { + [Desc("Uses the \"Cloneable\" trait to determine whether or not we should clone a produced unit.")] + public readonly string[] CloneableTypes = { }; + + public object Create(ActorInitializer init) { return new ClonesProducedUnits(init.self, this); } + } + + public class ClonesProducedUnits : INotifyOtherProduction + { + readonly ClonesProducedUnitsInfo info; + readonly Production production; + + public ClonesProducedUnits(Actor self, ClonesProducedUnitsInfo info) + { + this.info = info; + production = self.Trait(); + } + + public void UnitProducedByOther(Actor self, Actor producer, Actor produced) + { + // No recursive cloning! + if (producer.HasTrait()) + return; + + var ci = produced.Info.Traits.GetOrDefault(); + if (ci == null || !info.CloneableTypes.Intersect(ci.Types).Any()) + return; + + production.Produce(self, produced.Info, self.Owner.Country.Race); + } + } +} diff --git a/OpenRA.Mods.RA/Cloneable.cs b/OpenRA.Mods.RA/Cloneable.cs new file mode 100644 index 0000000000..961ca76807 --- /dev/null +++ b/OpenRA.Mods.RA/Cloneable.cs @@ -0,0 +1,24 @@ + #region Copyright & License Information + /* + * 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, + * see COPYING. + */ + #endregion + +using System; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + [Desc("Actors with the \"ClonesProducedUnits\" trait will produce a free duplicate of me.")] + public class CloneableInfo : TraitInfo + { + [Desc("This unit's cloneable type is:")] + public readonly string[] Types = { }; + } + + public class Cloneable { } +} diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 847590105e..fb71ed70f5 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -528,6 +528,8 @@ + + diff --git a/OpenRA.Mods.RA/Production.cs b/OpenRA.Mods.RA/Production.cs index e4b38f1182..b8004e535a 100755 --- a/OpenRA.Mods.RA/Production.cs +++ b/OpenRA.Mods.RA/Production.cs @@ -10,6 +10,7 @@ using System; using System.Drawing; +using System.Collections.Generic; using System.Linq; using OpenRA.Mods.RA.Move; using OpenRA.Primitives; @@ -96,6 +97,12 @@ namespace OpenRA.Mods.RA if (!self.IsDead()) foreach (var t in self.TraitsImplementing()) t.UnitProduced(self, newUnit, exit); + + var notifyOthers = self.World.ActorsWithTrait() + .Where(a => a.Actor.Owner == self.Owner); + + foreach (var notify in notifyOthers) + notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit); }); } diff --git a/mods/ra/rules/defaults.yaml b/mods/ra/rules/defaults.yaml index 9b9ae2bf28..10730dac6e 100644 --- a/mods/ra/rules/defaults.yaml +++ b/mods/ra/rules/defaults.yaml @@ -180,6 +180,8 @@ KilledOnImpassableTerrain: true ParachuteSequence: parach ShadowSequence: parach-shadow + Cloneable: + Types: Infantry ^Ship: AppearsOnRadar: