From cc356bcfee3b672ecf23d4b786b21f3e48817370 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 4 Jan 2011 14:30:24 +1300 Subject: [PATCH] Refactoring. --- OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj | 2 +- ...ineryDockAction.cs => TiberiumRefinery.cs} | 10 ++- OpenRA.Mods.RA/Harvester.cs | 10 --- OpenRA.Mods.RA/OpenRA.Mods.RA.csproj | 1 - OpenRA.Mods.RA/OreRefinery.cs | 69 ++++++++++++-- OpenRA.Mods.RA/OreRefineryDockAction.cs | 90 ------------------- mods/cnc/rules/structures.yaml | 3 +- mods/ra/rules/structures.yaml | 1 - 8 files changed, 73 insertions(+), 113 deletions(-) rename OpenRA.Mods.Cnc/{TiberiumRefineryDockAction.cs => TiberiumRefinery.cs} (69%) delete mode 100644 OpenRA.Mods.RA/OreRefineryDockAction.cs diff --git a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj index ffc278720e..7056fa91db 100644 --- a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj +++ b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj @@ -57,7 +57,6 @@ - @@ -67,6 +66,7 @@ + diff --git a/OpenRA.Mods.Cnc/TiberiumRefineryDockAction.cs b/OpenRA.Mods.Cnc/TiberiumRefinery.cs similarity index 69% rename from OpenRA.Mods.Cnc/TiberiumRefineryDockAction.cs rename to OpenRA.Mods.Cnc/TiberiumRefinery.cs index 143c5ace96..5cd0e92e98 100644 --- a/OpenRA.Mods.Cnc/TiberiumRefineryDockAction.cs +++ b/OpenRA.Mods.Cnc/TiberiumRefinery.cs @@ -18,9 +18,15 @@ using OpenRA.Mods.RA.Move; namespace OpenRA.Mods.Cnc { - class TiberiumRefineryDockActionInfo : TraitInfo {} - class TiberiumRefineryDockAction : OreRefineryDockAction + class TiberiumRefineryInfo : OreRefineryInfo { + public override object Create(ActorInitializer init) { return new TiberiumRefinery(init.self, this); } + } + class TiberiumRefinery : OreRefinery + { + public TiberiumRefinery(Actor self, TiberiumRefineryInfo info) + : base(self, info as OreRefineryInfo) {} + public override IActivity DockSequence(Actor harv, Actor self) { return new HarvesterDockSequence(harv, self); diff --git a/OpenRA.Mods.RA/Harvester.cs b/OpenRA.Mods.RA/Harvester.cs index 08e4ad7379..c6fe246b6b 100644 --- a/OpenRA.Mods.RA/Harvester.cs +++ b/OpenRA.Mods.RA/Harvester.cs @@ -111,16 +111,6 @@ namespace OpenRA.Mods.RA return contents.Count == 0; } - - public void Deliver(Actor self, Actor proc) - { - if (!proc.IsInWorld) - return; // fail to deliver if there is no proc. - - proc.Trait().GiveOre(contents.Sum(kv => kv.Key.ValuePerUnit * kv.Value)); - contents.Clear(); - } - public IEnumerable Orders { get diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 7a921a31fc..8cc58517d2 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -274,7 +274,6 @@ - diff --git a/OpenRA.Mods.RA/OreRefinery.cs b/OpenRA.Mods.RA/OreRefinery.cs index a5710c658e..0598b7df6c 100644 --- a/OpenRA.Mods.RA/OreRefinery.cs +++ b/OpenRA.Mods.RA/OreRefinery.cs @@ -14,10 +14,13 @@ using System.Linq; using OpenRA.Mods.RA.Activities; using OpenRA.Mods.RA.Buildings; using OpenRA.Traits; +using OpenRA.Mods.RA.Render; +using OpenRA.Mods.RA.Move; +using System.Drawing; namespace OpenRA.Mods.RA { - class OreRefineryInfo : ITraitInfo + public class OreRefineryInfo : ITraitInfo { public readonly bool LocalStorage = false; public readonly int PipCount = 0; @@ -28,10 +31,10 @@ namespace OpenRA.Mods.RA public readonly int ProcessAmount = 50; public readonly int LowPowerProcessTick = 50; - public object Create(ActorInitializer init) { return new OreRefinery(init.self, this); } + public virtual object Create(ActorInitializer init) { return new OreRefinery(init.self, this); } } - class OreRefinery : ITick, IAcceptOre, INotifyDamage, INotifySold, INotifyCapture, IPips, IExplodeModifier + public class OreRefinery : ITick, IAcceptOre, INotifyDamage, INotifySold, INotifyCapture, IPips, IExplodeModifier { readonly Actor self; readonly OreRefineryInfo Info; @@ -43,6 +46,17 @@ namespace OpenRA.Mods.RA [Sync] public int Ore = 0; + [Sync] + Actor dockedHarv = null; + [Sync] + bool preventDock = false; + + public int2 DeliverOffset { get { return Info.DockOffset; } } + public virtual IActivity DockSequence(Actor harv, Actor self) + { + return new RAHarvesterDockSequence(harv, self); + } + public OreRefinery (Actor self, OreRefineryInfo info) { this.self = self; @@ -77,8 +91,24 @@ namespace OpenRA.Mods.RA } } + void CancelDock(Actor self) + { + preventDock = true; + + // Cancel the dock sequence + if (dockedHarv != null && !dockedHarv.IsDead()) + dockedHarv.CancelActivity(); + } + public void Tick (Actor self) { + // Harvester was killed while unloading + if (dockedHarv != null && dockedHarv.IsDead()) + { + self.Trait().CancelCustomAnim(self); + dockedHarv = null; + } + if (!Info.LocalStorage) return; @@ -101,18 +131,45 @@ namespace OpenRA.Mods.RA public void Damaged (Actor self, AttackInfo e) { if (e.DamageState == DamageState.Dead) + { + CancelDock(self); foreach (var harv in GetLinkedHarvesters()) harv.Trait.UnlinkProc(harv.Actor, self); + } } - public int2 DeliverOffset {get{ return Info.DockOffset; }} public void OnDock (Actor harv, DeliverResources dockOrder) { - self.Trait().OnDock(self, harv, dockOrder); + var mobile = harv.Trait(); + var harvester = harv.Trait(); + + if (!preventDock) + { + harv.QueueActivity( new CallFunc( () => dockedHarv = harv, false ) ); + harv.QueueActivity( DockSequence(harv, self) ); + harv.QueueActivity( new CallFunc( () => dockedHarv = null, false ) ); + } + + // Tell the harvester to start harvesting + // TODO: This belongs on the harv idle activity + harv.QueueActivity( new CallFunc( () => + { + if (harvester.LastHarvestedCell != int2.Zero) + { + harv.QueueActivity( mobile.MoveTo(harvester.LastHarvestedCell, 5) ); + harv.SetTargetLine(Target.FromCell(harvester.LastHarvestedCell), Color.Red, false); + } + harv.QueueActivity( new Harvest() ); + })); } + public void OnCapture (Actor self, Actor captor, Player oldOwner, Player newOwner) { + // Steal any docked harv too + if (dockedHarv != null) + dockedHarv.ChangeOwner(newOwner); + // Unlink any non-docked harvs foreach (var harv in GetLinkedHarvesters()) if (harv.Actor.Owner == oldOwner) @@ -122,7 +179,7 @@ namespace OpenRA.Mods.RA PlayerPower = newOwner.PlayerActor.Trait(); } - public void Selling (Actor self) {} + public void Selling (Actor self) { CancelDock(self); } public void Sold (Actor self) { foreach (var harv in GetLinkedHarvesters()) diff --git a/OpenRA.Mods.RA/OreRefineryDockAction.cs b/OpenRA.Mods.RA/OreRefineryDockAction.cs deleted file mode 100644 index 6737232524..0000000000 --- a/OpenRA.Mods.RA/OreRefineryDockAction.cs +++ /dev/null @@ -1,90 +0,0 @@ -#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 OpenRA.Mods.RA.Activities; -using OpenRA.Mods.RA.Render; -using OpenRA.Traits; -using OpenRA.Traits.Activities; -using System.Drawing; -using OpenRA.Mods.RA.Move; - -namespace OpenRA.Mods.RA -{ - public class OreRefineryDockActionInfo : TraitInfo {} - - public class OreRefineryDockAction : IAcceptOreDockAction, INotifyCapture - { - public virtual IActivity DockSequence(Actor harv, Actor self) - { - return new RAHarvesterDockSequence(harv, self); - } - - Actor dockedHarv = null; - bool preventDock = false; - public void OnDock(Actor self, Actor harv, DeliverResources dockOrder) - { - var mobile = harv.Trait(); - var harvester = harv.Trait(); - - if (!preventDock) - { - harv.QueueActivity( new CallFunc( () => dockedHarv = harv, false ) ); - harv.QueueActivity( DockSequence(harv, self) ); - harv.QueueActivity( new CallFunc( () => dockedHarv = null, false ) ); - } - - // Tell the harvester to start harvesting - // TODO: This belongs on the harv idle activity - harv.QueueActivity( new CallFunc( () => - { - if (harvester.LastHarvestedCell != int2.Zero) - { - harv.QueueActivity( mobile.MoveTo(harvester.LastHarvestedCell, 5) ); - harv.SetTargetLine(Target.FromCell(harvester.LastHarvestedCell), Color.Red, false); - } - harv.QueueActivity( new Harvest() ); - })); - } - - public void Tick(Actor self) - { - // Harvester was killed while unloading - if (dockedHarv != null && dockedHarv.IsDead()) - { - self.Trait().CancelCustomAnim(self); - dockedHarv = null; - } - } - - void CancelDock(Actor self) - { - preventDock = true; - - // Cancel the dock sequence - if (dockedHarv != null && !dockedHarv.IsDead()) - dockedHarv.CancelActivity(); - } - - public void Selling (Actor self) { CancelDock(self); } - public void Sold (Actor self) {} - - public void Damaged (Actor self, AttackInfo e) - { - if (e.DamageState == DamageState.Dead) - CancelDock(self); - } - - public void OnCapture (Actor self, Actor captor, Player oldOwner, Player newOwner) - { - if (dockedHarv != null) - dockedHarv.ChangeOwner(newOwner); - } - } -} diff --git a/mods/cnc/rules/structures.yaml b/mods/cnc/rules/structures.yaml index 679a0b8e6b..1af547138c 100644 --- a/mods/cnc/rules/structures.yaml +++ b/mods/cnc/rules/structures.yaml @@ -78,13 +78,12 @@ PROC: RevealsShroud: Range: 6 Bib: - OreRefinery: + TiberiumRefinery: DockOffset: 0,2 StoresOre: PipColor: Green PipCount: 15 Capacity: 1500 - TiberiumRefineryDockAction: CustomSellValue: Value: 500 FreeActor: diff --git a/mods/ra/rules/structures.yaml b/mods/ra/rules/structures.yaml index aa2a446900..0119b985ce 100755 --- a/mods/ra/rules/structures.yaml +++ b/mods/ra/rules/structures.yaml @@ -627,7 +627,6 @@ PROC: Range: 6 Bib: OreRefinery: - OreRefineryDockAction: StoresOre: PipCount: 17 Capacity: 2000