Activity.Cancel returns void instead of bool.

This commit is contained in:
tovl
2019-03-09 15:49:12 +01:00
committed by Paul Chote
parent a17cd0fa06
commit 705795abde
13 changed files with 31 additions and 63 deletions

View File

@@ -25,11 +25,6 @@ namespace OpenRA.Activities
* optionally child activities, and usually a next activity. An actor's CurrentActivity is a pointer into that graph * optionally child activities, and usually a next activity. An actor's CurrentActivity is a pointer into that graph
* and moves through it as activities run. * and moves through it as activities run.
* *
* There are two kinds of activities, the base activity and composite activities. They differ in the way their children
* are run: while a base activity is responsible for running its children itself, a composite activity relies on the actor's
* activity-running code. Therefore, the actor's CurrentActivity stays on the base activity while it runs its children. With
* composite activities however, the CurrentActivity moves through the list of children as they run.
*
* *
* Things to be aware of when writing activities: * Things to be aware of when writing activities:
* *
@@ -38,24 +33,13 @@ namespace OpenRA.Activities
* - Do not "reuse" (with "SequenceActivities", for example) activity objects that have already finished running. * - Do not "reuse" (with "SequenceActivities", for example) activity objects that have already finished running.
* Queue a new instance instead. * Queue a new instance instead.
* - Avoid calling actor.CancelActivity(). It is almost always a bug. Call activity.Cancel() instead. * - Avoid calling actor.CancelActivity(). It is almost always a bug. Call activity.Cancel() instead.
* - A composite activity will run at least twice. The first time when it returns its children,
* the second time when its last child returns its Parent.
* - Do not return the Parent explicitly unless you have an extremly good reason. "return NextActivity" * - Do not return the Parent explicitly unless you have an extremly good reason. "return NextActivity"
* will do the right thing in all circumstances. * will do the right thing in all circumstances.
* - You do not need to care about the ChildActivity pointer advancing through the list of children, * - You do not need to care about the ChildActivity pointer advancing through the list of children,
* the activity code already takes care of that. * the activity code already takes care of that.
* - If you want to check whether there are any follow-up activities queued, check against "NextInQueue" * - If you want to check whether there are any follow-up activities queued, check against "NextInQueue"
* in favour of "NextActivity" to avoid checking against the Parent activity. * in favour of "NextActivity" to avoid checking against the Parent activity.
* */
*
* Guide when to use which kind of activity:
*
* - The activity does not have any children -> base activity
* - The activity needs to run preparatory steps during each tick before its children can be run -> base activity
* - The activity or the actor is left in a bogus state when one of the child activities is canceled -> base activity
* - The activity's children are self-contained and can run independently of the parent -> composite activity
* - The activity does not have any or little logic of its own, but is just composed of sub-steps -> composite activity
*/
public abstract class Activity public abstract class Activity
{ {
public ActivityState State { get; private set; } public ActivityState State { get; private set; }
@@ -217,20 +201,18 @@ namespace OpenRA.Activities
OnActorDispose(self); OnActorDispose(self);
} }
public virtual bool Cancel(Actor self, bool keepQueue = false) public virtual void Cancel(Actor self, bool keepQueue = false)
{ {
if (!keepQueue) if (!keepQueue)
NextInQueue = null; NextInQueue = null;
if (!IsInterruptible) if (!IsInterruptible)
return false; return;
if (ChildActivity != null) if (ChildActivity != null)
ChildActivity.Cancel(self); ChildActivity.Cancel(self);
State = ActivityState.Canceling; State = ActivityState.Canceling;
return true;
} }
public virtual void Queue(Actor self, Activity activity, bool pretick = false) public virtual void Queue(Actor self, Activity activity, bool pretick = false)

View File

@@ -225,12 +225,10 @@ namespace OpenRA
CurrentActivity.RootActivity.Queue(this, nextActivity); CurrentActivity.RootActivity.Queue(this, nextActivity);
} }
public bool CancelActivity() public void CancelActivity()
{ {
if (CurrentActivity != null) if (CurrentActivity != null)
return CurrentActivity.RootActivity.Cancel(this); CurrentActivity.RootActivity.Cancel(this);
return true;
} }
public override int GetHashCode() public override int GetHashCode()

View File

@@ -206,12 +206,12 @@ namespace OpenRA.Mods.Common.Activities
}); });
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && innerActivity != null) if (!IsCanceling && innerActivity != null)
innerActivity.Cancel(self); innerActivity.Cancel(self);
return base.Cancel(self); base.Cancel(self);
} }
} }
} }

View File

@@ -160,14 +160,14 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
OnCancel(self); OnCancel(self);
if (!IsCanceling && moveActivity != null) if (!IsCanceling && moveActivity != null)
moveActivity.Cancel(self); moveActivity.Cancel(self);
return base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -133,12 +133,12 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && enterTransport != null) if (!IsCanceling && enterTransport != null)
enterTransport.Cancel(self); enterTransport.Cancel(self);
return base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -81,10 +81,10 @@ namespace OpenRA.Mods.Common.Activities
throw new InvalidOperationException("Invalid harvester dock state"); throw new InvalidOperationException("Invalid harvester dock state");
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
dockingState = DockingState.Undock; dockingState = DockingState.Undock;
return base.Cancel(self); base.Cancel(self);
} }
public override IEnumerable<Target> GetTargets(Actor self) public override IEnumerable<Target> GetTargets(Actor self)

View File

@@ -46,12 +46,12 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && inner != null) if (!IsCanceling && inner != null)
inner.Cancel(self); inner.Cancel(self);
return base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
} }
public override IEnumerable<Target> GetTargets(Actor self) public override IEnumerable<Target> GetTargets(Actor self)

View File

@@ -167,12 +167,12 @@ namespace OpenRA.Mods.Common.Activities
return Target.None; return Target.None;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && inner != null) if (!IsCanceling && inner != null)
inner.Cancel(self); inner.Cancel(self);
return base.Cancel(self); base.Cancel(self);
} }
} }
} }

View File

@@ -159,12 +159,12 @@ namespace OpenRA.Mods.Common.Activities
}); });
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && innerActivity != null) if (!IsCanceling && innerActivity != null)
innerActivity.Cancel(self); innerActivity.Cancel(self);
return base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -27,17 +27,11 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceling)
return NextActivity;
return (remainingTicks-- == 0) ? NextActivity : this; return (remainingTicks-- == 0) ? NextActivity : this;
} }
public override bool Cancel(Actor self, bool keepQueue = false)
{
if (!base.Cancel(self, keepQueue))
return false;
remainingTicks = 0;
return true;
}
} }
public class WaitFor : Activity public class WaitFor : Activity
@@ -53,16 +47,10 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceling)
return NextActivity;
return (f == null || f()) ? NextActivity : this; return (f == null || f()) ? NextActivity : this;
} }
public override bool Cancel(Actor self, bool keepQueue = false)
{
if (!base.Cancel(self, keepQueue))
return false;
f = null;
return true;
}
} }
} }

View File

@@ -41,12 +41,12 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceling && inner != null) if (!IsCanceling && inner != null)
inner.Cancel(self); inner.Cancel(self);
return base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -43,11 +43,11 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
public override bool Cancel(Actor self, bool keepQueue = false) public override void Cancel(Actor self, bool keepQueue = false)
{ {
base.Cancel(self, keepQueue); base.Cancel(self, keepQueue);
Dispose(); Dispose();
return true; return;
} }
public void Dispose() public void Dispose()

View File

@@ -51,8 +51,8 @@ namespace OpenRA.Mods.Common.Traits
transform.Facing = facing.Facing; transform.Facing = facing.Facing;
transform.SkipMakeAnims = info.SkipMakeAnims; transform.SkipMakeAnims = info.SkipMakeAnims;
if (crusher.CancelActivity()) crusher.CancelActivity();
crusher.QueueActivity(transform); crusher.QueueActivity(transform);
} }
} }
} }