diff --git a/OpenRA.Mods.RA/Activities/DeliverOre.cs b/OpenRA.Mods.RA/Activities/DeliverResources.cs similarity index 94% rename from OpenRA.Mods.RA/Activities/DeliverOre.cs rename to OpenRA.Mods.RA/Activities/DeliverResources.cs index 3b46d7cd6c..72cc15b2ef 100755 --- a/OpenRA.Mods.RA/Activities/DeliverOre.cs +++ b/OpenRA.Mods.RA/Activities/DeliverResources.cs @@ -9,6 +9,7 @@ #endregion using System.Collections.Generic; +using System.Drawing; using OpenRA.Traits; using OpenRA.Traits.Activities; using OpenRA.Mods.RA.Move; @@ -23,7 +24,6 @@ namespace OpenRA.Mods.RA.Activities public override Activity Tick( Actor self ) { - // TODO: wtf is this? if( NextActivity != null ) return NextActivity; @@ -39,6 +39,7 @@ namespace OpenRA.Mods.RA.Activities var proc = harv.LinkedProc; var iao = proc.Trait(); + self.SetTargetLine(Target.FromActor(proc), Color.Green, false); if( self.Location != proc.Location + iao.DeliverOffset ) return Util.SequenceActivities( mobile.MoveTo(proc.Location + iao.DeliverOffset, 0), this ); diff --git a/OpenRA.Mods.RA/Activities/Harvest.cs b/OpenRA.Mods.RA/Activities/FindResources.cs similarity index 52% rename from OpenRA.Mods.RA/Activities/Harvest.cs rename to OpenRA.Mods.RA/Activities/FindResources.cs index f21ea30603..037256cb2d 100755 --- a/OpenRA.Mods.RA/Activities/Harvest.cs +++ b/OpenRA.Mods.RA/Activities/FindResources.cs @@ -8,12 +8,13 @@ */ #endregion +using System; +using System.Collections.Generic; +using System.Drawing; using System.Linq; -using OpenRA.Mods.RA.Render; using OpenRA.Traits; using OpenRA.Traits.Activities; using OpenRA.Mods.RA.Move; -using System.Collections.Generic; namespace OpenRA.Mods.RA.Activities { @@ -21,23 +22,29 @@ namespace OpenRA.Mods.RA.Activities { public override Activity Tick( Actor self ) { - if( IsCanceled ) return NextActivity; - if( NextActivity != null ) return NextActivity; + if( IsCanceled || NextActivity != null) return NextActivity; + var harv = self.Trait(); if( harv.IsFull ) return Util.SequenceActivities( new DeliverResources(), NextActivity ); - var harvInfo = self.Info.Traits.Get(); - var mobile = self.Trait(); var mobileInfo = self.Info.Traits.Get(); + var harvInfo = self.Info.Traits.Get(); var res = self.World.WorldActor.Trait(); + + Func canHarvest = loc => loc != self.Location && + res.GetResource(loc) != null && + harvInfo.Resources.Contains( res.GetResource(loc).info.Name ); + var path = self.World.WorldActor.Trait().FindPath(PathSearch.Search(self.World, mobileInfo, self.Owner, true) - .WithHeuristic(loc => (res.GetResource(loc) != null && harvInfo.Resources.Contains( res.GetResource(loc).info.Name )) ? 0 : 1) + .WithHeuristic(loc => canHarvest(loc) ? 0 : 1) .FromPoint(self.Location)); if (path.Count == 0) return NextActivity; - return Util.SequenceActivities( mobile.MoveTo(path[0], 1), new HarvestResource(), this ); + + self.SetTargetLine(Target.FromCell(path[0]), Color.Red, false); + return Util.SequenceActivities( new MoveAdjacentTo(Target.FromCell(path[0])), new HarvestResource(self, path[0]), this ); } public override IEnumerable GetTargets( Actor self ) @@ -45,33 +52,4 @@ namespace OpenRA.Mods.RA.Activities yield return Target.FromPos(self.Location); } } - - public class HarvestResource : Activity - { - bool isHarvesting = false; - - public override Activity Tick( Actor self ) - { - if( isHarvesting ) return this; - if( IsCanceled ) return NextActivity; - var harv = self.Trait(); - harv.LastHarvestedCell = self.Location; - - if( harv.IsFull ) - return NextActivity; - - var renderUnit = self.Trait(); /* better have one of these! */ - var resource = self.World.WorldActor.Trait().Harvest(self.Location); - if (resource == null) - return NextActivity; - - if (renderUnit.anim.CurrentSequence.Name != "harvest") - { - isHarvesting = true; - renderUnit.PlayCustomAnimation(self, "harvest", () => isHarvesting = false); - } - harv.AcceptResource(resource); - return this; - } - } } diff --git a/OpenRA.Mods.RA/Activities/HarvestResource.cs b/OpenRA.Mods.RA/Activities/HarvestResource.cs new file mode 100755 index 0000000000..3a1984838f --- /dev/null +++ b/OpenRA.Mods.RA/Activities/HarvestResource.cs @@ -0,0 +1,63 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 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 OpenRA.Traits; +using OpenRA.Traits.Activities; +using OpenRA.Mods.RA.Render; + +namespace OpenRA.Mods.RA.Activities +{ + public class HarvestResource : Activity + { + Harvester harv; + IFacing facing; + RenderUnit renderUnit; + ResourceLayer resourceLayer; + bool isHarvesting = false; + int2 harvestCell; + + public HarvestResource(Actor self, int2 cell) + { + harv = self.Trait(); + facing = self.Trait(); + renderUnit = self.Trait(); + resourceLayer = self.World.WorldActor.Trait(); + harvestCell = cell; + } + + public override Activity Tick( Actor self ) + { + if( isHarvesting ) return this; + if( IsCanceled ) return NextActivity; + harv.LastHarvestedCell = harvestCell; + + if( harv.IsFull ) + return NextActivity; + + int2 dir = harvestCell - self.Location; + var f = Util.GetFacing( dir, facing.Facing ); + if( f != facing.Facing ) + return Util.SequenceActivities( new Turn(f), this ); + + var resource = resourceLayer.Harvest(harvestCell); + if (resource == null) + return NextActivity; + + if (renderUnit.anim.CurrentSequence.Name != "harvest") + { + isHarvesting = true; + renderUnit.PlayCustomAnimation(self, "harvest", () => isHarvesting = false); + } + + harv.AcceptResource(resource); + return this; + } + } +} diff --git a/OpenRA.Mods.RA/Activities/MoveAdjacentTo.cs b/OpenRA.Mods.RA/Activities/MoveAdjacentTo.cs index 0294408ba0..6a75e6e041 100755 --- a/OpenRA.Mods.RA/Activities/MoveAdjacentTo.cs +++ b/OpenRA.Mods.RA/Activities/MoveAdjacentTo.cs @@ -18,23 +18,30 @@ namespace OpenRA.Mods.RA.Activities { public class MoveAdjacentTo : Activity { - readonly Actor target; + readonly Target target; public MoveAdjacentTo( Actor target ) + { + this.target = Target.FromActor(target); + } + + public MoveAdjacentTo( Target target ) { this.target = target; } public override Activity Tick( Actor self ) { - if( IsCanceled || target.Destroyed || !target.IsInWorld) return NextActivity; + if( IsCanceled || !target.IsValid) return NextActivity; var mobile = self.Trait(); + Pair[] cells = new Pair[] {}; + if (target.IsActor) + cells = target.Actor.Trait().OccupiedCells().ToArray(); - var cells = target.Trait().OccupiedCells().ToArray(); if (cells.Length == 0) cells = new OpenRA.FileFormats.Pair[] { - Pair.New(target.Location, SubCell.FullCell) }; + Pair.New(Util.CellContaining(target.CenterLocation), SubCell.FullCell) }; var ps1 = new PathSearch( self.World, mobile.Info, self.Owner ) { @@ -51,7 +58,7 @@ namespace OpenRA.Mods.RA.Activities } ps1.heuristic = PathSearch.DefaultEstimator( mobile.toCell ); - var ps2 = PathSearch.FromPoint( self.World, mobile.Info, self.Owner, mobile.toCell, target.Location, true ); + var ps2 = PathSearch.FromPoint( self.World, mobile.Info, self.Owner, mobile.toCell, Util.CellContaining(target.CenterLocation), true ); var ret = self.World.WorldActor.Trait().FindBidiPath( ps1, ps2 ); if( ret.Count > 0 ) ret.RemoveAt( 0 ); diff --git a/OpenRA.Mods.RA/Harvester.cs b/OpenRA.Mods.RA/Harvester.cs index d86e2428f5..1be14d386a 100644 --- a/OpenRA.Mods.RA/Harvester.cs +++ b/OpenRA.Mods.RA/Harvester.cs @@ -61,9 +61,9 @@ namespace OpenRA.Mods.RA { if (LastHarvestedCell.HasValue) { - var mobile = self.Trait(); - self.QueueActivity( mobile.MoveTo(LastHarvestedCell.Value, 5) ); - self.SetTargetLine(Target.FromCell(LastHarvestedCell.Value), Color.Red, false); + var target = Target.FromCell(LastHarvestedCell.Value); + self.QueueActivity( new MoveAdjacentTo(target) ); + self.SetTargetLine(target, Color.Red, false); } self.QueueActivity( new FindResources() ); } @@ -152,12 +152,12 @@ namespace OpenRA.Mods.RA { if (order.OrderString == "Harvest") { - self.SetTargetLine(Target.FromOrder(order), Color.Red); - - var mobile = self.Trait(); + var target = Target.FromOrder(order); self.CancelActivity(); - self.QueueActivity(mobile.MoveTo(order.TargetLocation, 0)); + self.QueueActivity(new MoveAdjacentTo(target)); + self.QueueActivity(new HarvestResource(self, order.TargetLocation)); self.QueueActivity(new FindResources()); + self.SetTargetLine(target, Color.Red); } else if (order.OrderString == "Deliver") { diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index e8ebf9f1c6..d642704576 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -60,7 +60,6 @@ - @@ -190,7 +189,6 @@ - @@ -356,6 +354,9 @@ + + +