diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index e5cbbeec93..68dd454762 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -9,6 +9,7 @@ using OpenRa.Game.GameRules; using OpenRa.Game.Graphics; using System.Drawing; using OpenRa.Game.Traits; +using OpenRa.Game.Traits.Activities; namespace OpenRa.Game { @@ -21,6 +22,7 @@ namespace OpenRa.Game public int2 Location; public Player Owner; public int Health; + IActivity currentActivity; public Actor( string name, int2 location, Player owner ) { @@ -54,6 +56,13 @@ namespace OpenRa.Game public void Tick() { + var nextActivity = currentActivity; + while( nextActivity != null ) + { + currentActivity = nextActivity; + nextActivity = nextActivity.Tick( this ); + } + foreach (var tick in traits.WithInterface()) tick.Tick(this); } @@ -124,5 +133,32 @@ namespace OpenRa.Game foreach (var ndx in traits.WithInterface()) ndx.Damaged(this, damage); } + + public void QueueActivity( IActivity nextActivity ) + { + if( currentActivity == null ) + { + currentActivity = nextActivity; + return; + } + var act = currentActivity; + while( act.NextActivity != null ) + { + act = act.NextActivity; + } + act.NextActivity = nextActivity; + } + + public void CancelActivity( Actor self ) + { + if( currentActivity != null ) + currentActivity.Cancel( self ); + } + + // For pathdebug, et al + public IActivity GetCurrentActivity() + { + return currentActivity; + } } } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index ade911e8dd..d2d2f566ce 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -381,7 +381,7 @@ namespace OpenRa.Game if( mobile != null ) { unit.Facing = 128; - mobile.QueueActivity(new Traits.Activities.Move(dest, 1)); + newActor.QueueActivity(new Traits.Activities.Move(dest, 1)); } var heli = newActor.traits.GetOrDefault(); diff --git a/OpenRa.Game/Traits/AcceptsOre.cs b/OpenRa.Game/Traits/AcceptsOre.cs index 85f77109bd..a391c58ca7 100644 --- a/OpenRa.Game/Traits/AcceptsOre.cs +++ b/OpenRa.Game/Traits/AcceptsOre.cs @@ -17,7 +17,7 @@ namespace OpenRa.Game.Traits var unit = harvester.traits.Get(); var mobile = harvester.traits.Get(); unit.Facing = 64; - mobile.QueueActivity(new Harvest()); + harvester.QueueActivity(new Harvest()); w.Add(harvester); }); } diff --git a/OpenRa.Game/Traits/Activities/Harvest.cs b/OpenRa.Game/Traits/Activities/Harvest.cs index c1c88bed62..481b8faf93 100644 --- a/OpenRa.Game/Traits/Activities/Harvest.cs +++ b/OpenRa.Game/Traits/Activities/Harvest.cs @@ -41,7 +41,7 @@ namespace OpenRa.Game.Traits.Activities } else { - mobile.QueueActivity( new Move( + self.QueueActivity( new Move( () => { var search = new PathSearch @@ -53,7 +53,7 @@ namespace OpenRa.Game.Traits.Activities search.AddInitialCell( self.Location ); return Game.PathFinder.FindPath( search ); } ) ); - mobile.QueueActivity( new Harvest() ); + self.QueueActivity( new Harvest() ); return NextActivity; } } diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index cdb9b9cec8..a8dcb4dbec 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -8,7 +8,7 @@ using OpenRa.Game.Traits.Activities; namespace OpenRa.Game.Traits { - class Mobile : ITick, IOrder + class Mobile : IOrder { public Actor self; @@ -17,7 +17,6 @@ namespace OpenRa.Game.Traits public int2 toCell { get { return self.Location; } set { Game.UnitInfluence.Remove( this ); self.Location = value; Game.UnitInfluence.Add( this ); } } public int Voice = Game.CosmeticRandom.Next(2); - IActivity currentActivity; public Mobile(Actor self) { @@ -26,37 +25,6 @@ namespace OpenRa.Game.Traits Game.UnitInfluence.Update( this ); } - public void QueueActivity( IActivity nextActivity ) - { - if( currentActivity == null ) - { - currentActivity = nextActivity; - return; - } - var act = currentActivity; - while( act.NextActivity != null ) - { - act = act.NextActivity; - } - act.NextActivity = nextActivity; - } - - public void Tick(Actor self) - { - if( currentActivity == null ) - { - fromCell = toCell; - return; - } - - var nextActivity = currentActivity; - while( nextActivity != null ) - { - currentActivity = nextActivity; - nextActivity = nextActivity.Tick( self ); - } - } - public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) { if( lmb ) return null; @@ -70,12 +38,6 @@ namespace OpenRa.Game.Traits !Game.IsCellBuildable(xy, GetMovementType()) ); } - public void Cancel(Actor self) - { - if (currentActivity != null) - currentActivity.Cancel(self); - } - public IEnumerable OccupiedCells() { return new[] { fromCell, toCell }; @@ -100,11 +62,9 @@ namespace OpenRa.Game.Traits public IEnumerable GetCurrentPath() { - var move = currentActivity as Traits.Activities.Move; + var move = self.GetCurrentActivity() as Traits.Activities.Move; if (move == null || move.path == null) return new int2[] { }; return Enumerable.Reverse(move.path); } - - public bool HasActivity { get { return currentActivity != null; } } } } diff --git a/OpenRa.Game/UnitOrders.cs b/OpenRa.Game/UnitOrders.cs index af26a56ff9..053b5a85c3 100755 --- a/OpenRa.Game/UnitOrders.cs +++ b/OpenRa.Game/UnitOrders.cs @@ -18,8 +18,8 @@ namespace OpenRa.Game var mobile = order.Subject.traits.GetOrDefault(); if (mobile != null) { - mobile.Cancel(order.Subject); - mobile.QueueActivity(new Traits.Activities.Move(order.TargetLocation, 8)); + order.Subject.CancelActivity( order.Subject ); + order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) ); } var heli = order.Subject.traits.GetOrDefault(); @@ -38,10 +38,10 @@ namespace OpenRa.Game /* todo: choose the appropriate weapon, when only one works against this target */ var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary; - mobile.Cancel(order.Subject); + order.Subject.CancelActivity(order.Subject); if (order.Subject.traits.Contains()) { - mobile.QueueActivity( + order.Subject.QueueActivity( new Traits.Activities.Follow(order.TargetActor, Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance))); @@ -49,7 +49,7 @@ namespace OpenRa.Game } else { - mobile.QueueActivity( + order.Subject.QueueActivity( new Traits.Activities.Attack(order.TargetActor, Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance))); } @@ -61,25 +61,22 @@ namespace OpenRa.Game if( !Game.CanPlaceBuilding( factBuildingInfo, order.Subject.Location - new int2( 1, 1 ), order.Subject, false ) ) break; /* throw the order on the floor */ - var mobile = order.Subject.traits.Get(); - mobile.Cancel(order.Subject); - mobile.QueueActivity( new Traits.Activities.Turn( 96 ) ); - mobile.QueueActivity( new Traits.Activities.DeployMcv() ); + order.Subject.CancelActivity( order.Subject ); + order.Subject.QueueActivity( new Traits.Activities.Turn( 96 ) ); + order.Subject.QueueActivity( new Traits.Activities.DeployMcv() ); break; } case "DeliverOre": { - var mobile = order.Subject.traits.Get(); - mobile.Cancel(order.Subject); - mobile.QueueActivity(new Traits.Activities.DeliverOre(order.TargetActor)); + order.Subject.CancelActivity( order.Subject ); + order.Subject.QueueActivity( new Traits.Activities.DeliverOre( order.TargetActor ) ); break; } case "Harvest": { - var mobile = order.Subject.traits.Get(); - mobile.Cancel(order.Subject); - mobile.QueueActivity(new Traits.Activities.Move(order.TargetLocation, 0)); - mobile.QueueActivity(new Traits.Activities.Harvest() ); + order.Subject.CancelActivity( order.Subject ); + order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 0 ) ); + order.Subject.QueueActivity( new Traits.Activities.Harvest() ); break; } case "PlaceBuilding":