From a9ffe0a779a6865d7491203bd6bfe8899d789b60 Mon Sep 17 00:00:00 2001 From: Bynnar18 Date: Mon, 4 Jan 2016 03:01:09 -0600 Subject: [PATCH] Paradrop Production --- .../Traits/Buildings/ProductionAirdrop.cs | 4 +- OpenRA.Mods.Common/Activities/Parachute.cs | 6 +- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + OpenRA.Mods.Common/Traits/Crates/Crate.cs | 2 +- OpenRA.Mods.Common/Traits/Parachutable.cs | 6 +- OpenRA.Mods.Common/Traits/Production.cs | 2 +- .../Traits/ProductionParadrop.cs | 171 ++++++++++++++++++ .../Traits/Render/WithCrateBody.cs | 2 +- OpenRA.Mods.Common/TraitsInterfaces.cs | 2 +- 9 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 OpenRA.Mods.Common/Traits/ProductionParadrop.cs diff --git a/OpenRA.Mods.Cnc/Traits/Buildings/ProductionAirdrop.cs b/OpenRA.Mods.Cnc/Traits/Buildings/ProductionAirdrop.cs index 7c7277e940..b8a2e9d427 100644 --- a/OpenRA.Mods.Cnc/Traits/Buildings/ProductionAirdrop.cs +++ b/OpenRA.Mods.Cnc/Traits/Buildings/ProductionAirdrop.cs @@ -22,8 +22,8 @@ namespace OpenRA.Mods.Cnc.Traits public class ProductionAirdropInfo : ProductionInfo { public readonly string ReadyAudio = "Reinforce"; - [Desc("Cargo aircraft used.")] - [ActorReference] public readonly string ActorType = "c17"; + [Desc("Cargo aircraft used for delivery. Must have the `Aircraft` trait.")] + [ActorReference(typeof(AircraftInfo))] public readonly string ActorType = "c17"; public override object Create(ActorInitializer init) { return new ProductionAirdrop(init, this); } } diff --git a/OpenRA.Mods.Common/Activities/Parachute.cs b/OpenRA.Mods.Common/Activities/Parachute.cs index a9f2b40e01..0b084f1fd5 100644 --- a/OpenRA.Mods.Common/Activities/Parachute.cs +++ b/OpenRA.Mods.Common/Activities/Parachute.cs @@ -21,15 +21,17 @@ namespace OpenRA.Mods.Common.Activities readonly IPositionable pos; readonly ParachutableInfo para; readonly WVec fallVector; + readonly Actor ignore; WPos dropPosition; WPos currentPosition; bool triggered = false; - public Parachute(Actor self, WPos dropPosition) + public Parachute(Actor self, WPos dropPosition, Actor ignoreActor = null) { um = self.TraitOrDefault(); pos = self.TraitOrDefault(); + ignore = ignoreActor; // Parachutable trait is a prerequisite for running this activity para = self.Info.TraitInfo(); @@ -61,7 +63,7 @@ namespace OpenRA.Mods.Common.Activities um.RevokeUpgrade(self, u, this); foreach (var npl in self.TraitsImplementing()) - npl.OnLanded(); + npl.OnLanded(ignore); return NextActivity; } diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 7f0ace144d..684f573e74 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -400,6 +400,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/Crates/Crate.cs b/OpenRA.Mods.Common/Traits/Crates/Crate.cs index d8ec47e7ce..86a1ae0836 100644 --- a/OpenRA.Mods.Common/Traits/Crates/Crate.cs +++ b/OpenRA.Mods.Common/Traits/Crates/Crate.cs @@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits OnCrushInner(crusher); } - public void OnLanded() + void INotifyParachuteLanded.OnLanded(Actor ignore) { // Check whether the crate landed on anything var landedOn = self.World.ActorMap.GetActorsAt(self.Location) diff --git a/OpenRA.Mods.Common/Traits/Parachutable.cs b/OpenRA.Mods.Common/Traits/Parachutable.cs index 3b754b3201..05e29fab5b 100644 --- a/OpenRA.Mods.Common/Traits/Parachutable.cs +++ b/OpenRA.Mods.Common/Traits/Parachutable.cs @@ -9,6 +9,7 @@ */ #endregion +using System.Linq; using OpenRA.Mods.Common.Effects; using OpenRA.Traits; @@ -54,7 +55,7 @@ namespace OpenRA.Mods.Common.Traits positionable = self.TraitOrDefault(); } - public void OnLanded() + void INotifyParachuteLanded.OnLanded(Actor ignore) { if (!info.KilledOnImpassableTerrain) return; @@ -62,6 +63,9 @@ namespace OpenRA.Mods.Common.Traits if (positionable.CanEnterCell(self.Location, self)) return; + if (ignore != null && self.World.ActorMap.GetActorsAt(self.Location).Any(a => a != ignore)) + return; + var terrain = self.World.Map.GetTerrainInfo(self.Location); var sound = terrain.IsWater ? info.WaterImpactSound : info.GroundImpactSound; diff --git a/OpenRA.Mods.Common/Traits/Production.cs b/OpenRA.Mods.Common/Traits/Production.cs index 3eb811baea..459e9c6cb0 100644 --- a/OpenRA.Mods.Common/Traits/Production.cs +++ b/OpenRA.Mods.Common/Traits/Production.cs @@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Traits Faction = init.Contains() ? init.Get() : init.Self.Owner.Faction.InternalName; } - public void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string factionVariant) + public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string factionVariant) { var exit = CPos.Zero; var exitLocation = CPos.Zero; diff --git a/OpenRA.Mods.Common/Traits/ProductionParadrop.cs b/OpenRA.Mods.Common/Traits/ProductionParadrop.cs new file mode 100644 index 0000000000..73a93a38c1 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/ProductionParadrop.cs @@ -0,0 +1,171 @@ +#region Copyright & License Information +/* + * Copyright 2007-2016 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, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Drawing; +using System.Linq; +using OpenRA.Activities; +using OpenRA.Mods.Common.Activities; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Deliver the unit in production via paradrop.")] + public class ProductionParadropInfo : ProductionInfo, Requires + { + [Desc("Cargo aircraft used. Must have Aircraft trait.")] + [ActorReference(typeof(AircraftInfo))] public readonly string ActorType = "badr"; + + [Desc("Sound to play when dropping the unit.")] + public readonly string ChuteSound = "chute1.aud"; + + [Desc("Notification to play when dropping the unit.")] + public readonly string ReadyAudio = null; + + public override object Create(ActorInitializer init) { return new ProductionParadrop(init, this); } + } + + class ProductionParadrop : Production + { + readonly Lazy rp; + + public ProductionParadrop(ActorInitializer init, ProductionParadropInfo info) + : base(init, info) + { + rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault()); + } + + public override bool Produce(Actor self, ActorInfo producee, string factionVariant) + { + var owner = self.Owner; + + // Assume a single exit point for simplicity + var exit = self.Info.TraitInfos().First(); + + // Start a fixed distance away: the width of the map. + // This makes the production timing independent of spawnpoint + var dropPos = self.Location + exit.ExitCell; + var startPos = dropPos + new CVec(owner.World.Map.Bounds.Width, 0); + var endPos = new CPos(owner.World.Map.Bounds.Left - 5, dropPos.Y); + + foreach (var notify in self.TraitsImplementing()) + notify.IncomingDelivery(self); + + var info = (ProductionParadropInfo)Info; + var actorType = info.ActorType; + + owner.World.AddFrameEndTask(w => + { + if (!self.IsInWorld || self.IsDead) + return; + + var altitude = self.World.Map.Rules.Actors[actorType].TraitInfo().CruiseAltitude; + var actor = w.CreateActor(actorType, new TypeDictionary + { + new CenterPositionInit(w.Map.CenterOfCell(startPos) + new WVec(WDist.Zero, WDist.Zero, altitude)), + new OwnerInit(owner), + new FacingInit(64) + }); + + actor.QueueActivity(new Fly(actor, Target.FromCell(w, dropPos))); + actor.QueueActivity(new CallFunc(() => + { + if (!self.IsInWorld || self.IsDead) + return; + + foreach (var cargo in self.TraitsImplementing()) + cargo.Delivered(self); + + self.World.AddFrameEndTask(ww => DoProduction(self, producee, exit, factionVariant)); + Game.Sound.Play(info.ChuteSound, self.CenterPosition); + Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.ReadyAudio, self.Owner.Faction.InternalName); + })); + + actor.QueueActivity(new Fly(actor, Target.FromCell(w, endPos))); + actor.QueueActivity(new RemoveSelf()); + }); + + return true; + } + + public override void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string factionVariant) + { + var exit = CPos.Zero; + var exitLocation = CPos.Zero; + var target = Target.Invalid; + + var info = (ProductionParadropInfo)Info; + var actorType = info.ActorType; + + var bi = producee.TraitInfoOrDefault(); + if (bi != null && bi.ForceFaction != null) + factionVariant = bi.ForceFaction; + + var altitude = self.World.Map.Rules.Actors[actorType].TraitInfo().CruiseAltitude; + var td = new TypeDictionary + { + new OwnerInit(self.Owner), + }; + + if (self.OccupiesSpace != null) + { + exit = self.Location + exitinfo.ExitCell; + var spawn = self.World.Map.CenterOfCell(exit) + new WVec(WDist.Zero, WDist.Zero, altitude); + var to = self.World.Map.CenterOfCell(exit); + + var initialFacing = exitinfo.Facing < 0 ? (to - spawn).Yaw.Facing : exitinfo.Facing; + + exitLocation = rp.Value != null ? rp.Value.Location : exit; + target = Target.FromCell(self.World, exitLocation); + + td.Add(new LocationInit(exit)); + td.Add(new CenterPositionInit(spawn)); + td.Add(new FacingInit(initialFacing)); + } + + self.World.AddFrameEndTask(w => + { + if (factionVariant != null) + td.Add(new FactionInit(factionVariant)); + + var newUnit = self.World.CreateActor(producee.Name, td); + + newUnit.QueueActivity(new Parachute(newUnit, newUnit.CenterPosition, self)); + var move = newUnit.TraitOrDefault(); + if (move != null) + { + if (exitinfo.MoveIntoWorld) + { + if (exitinfo.ExitDelay > 0) + newUnit.QueueActivity(new Wait(exitinfo.ExitDelay, false)); + + newUnit.QueueActivity(move.MoveIntoWorld(newUnit, exit)); + newUnit.QueueActivity(new AttackMoveActivity(newUnit, move.MoveTo(exitLocation, 1))); + } + } + + newUnit.SetTargetLine(target, rp.Value != null ? Color.Red : Color.Green, false); + + if (!self.IsDead) + foreach (var t in self.TraitsImplementing()) + t.UnitProduced(self, newUnit, exit); + + var notifyOthers = self.World.ActorsWithTrait(); + foreach (var notify in notifyOthers) + notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit); + + foreach (var t in newUnit.TraitsImplementing()) + t.BuildingComplete(newUnit); + }); + } + } +} \ No newline at end of file diff --git a/OpenRA.Mods.Common/Traits/Render/WithCrateBody.cs b/OpenRA.Mods.Common/Traits/Render/WithCrateBody.cs index 63b4a7baec..9a689b4023 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithCrateBody.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithCrateBody.cs @@ -67,7 +67,7 @@ namespace OpenRA.Mods.Common.Traits PlaySequence(); } - void INotifyParachuteLanded.OnLanded() + void INotifyParachuteLanded.OnLanded(Actor ignore) { PlaySequence(); } diff --git a/OpenRA.Mods.Common/TraitsInterfaces.cs b/OpenRA.Mods.Common/TraitsInterfaces.cs index 8f33c63572..7b355fb53c 100644 --- a/OpenRA.Mods.Common/TraitsInterfaces.cs +++ b/OpenRA.Mods.Common/TraitsInterfaces.cs @@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits public interface INotifyAttack { void Attacking(Actor self, Target target, Armament a, Barrel barrel); } public interface INotifyCharging { void Charging(Actor self, Target target); } public interface INotifyChat { bool OnChat(string from, string message); } - public interface INotifyParachuteLanded { void OnLanded(); } + public interface INotifyParachuteLanded { void OnLanded(Actor ignore); } public interface IRenderActorPreviewInfo : ITraitInfo { IEnumerable RenderPreview(ActorPreviewInitializer init); } public interface ICruiseAltitudeInfo : ITraitInfo { WDist GetCruiseAltitude(); }