Let activities know when they are being run for the first or last time

This commit is contained in:
Oliver Brakmann
2016-11-28 19:41:27 +01:00
parent 85053d8d59
commit 0dfbd46b1f
3 changed files with 38 additions and 1 deletions

View File

@@ -9,14 +9,19 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Activities namespace OpenRA.Activities
{ {
public enum ActivityState { Queued, Active, Done }
public abstract class Activity public abstract class Activity
{ {
public ActivityState State { get; private set; }
public Activity NextActivity { get; set; } public Activity NextActivity { get; set; }
public bool IsInterruptible { get; protected set; } public bool IsInterruptible { get; protected set; }
protected bool IsCanceled { get; private set; } protected bool IsCanceled { get; private set; }
@@ -26,8 +31,39 @@ namespace OpenRA.Activities
IsInterruptible = true; IsInterruptible = true;
} }
public Activity TickOuter(Actor self)
{
if (State == ActivityState.Done && Game.Settings.Debug.StrictActivityChecking)
throw new InvalidOperationException("Actor {0} attempted to tick activity {1} after it had already completed.".F(self, this.GetType()));
if (State == ActivityState.Queued)
{
OnFirstRun(self);
State = ActivityState.Active;
}
var ret = Tick(self);
if (ret != this)
{
State = ActivityState.Done;
OnLastRun(self);
}
return ret;
}
public abstract Activity Tick(Actor self); public abstract Activity Tick(Actor self);
/// <summary>
/// Runs once immediately before the first Tick() execution.
/// </summary>
protected virtual void OnFirstRun(Actor self) { }
/// <summary>
/// Runs once immediately after the last Tick() execution.
/// </summary>
protected virtual void OnLastRun(Actor self) { }
public virtual bool Cancel(Actor self) public virtual bool Cancel(Actor self)
{ {
if (!IsInterruptible) if (!IsInterruptible)

View File

@@ -92,6 +92,7 @@ namespace OpenRA
public bool SanityCheckUnsyncedCode = false; public bool SanityCheckUnsyncedCode = false;
public int Samples = 25; public int Samples = 25;
public bool IgnoreVersionMismatch = false; public bool IgnoreVersionMismatch = false;
public bool StrictActivityChecking = false;
public bool SendSystemInformation = true; public bool SendSystemInformation = true;
public int SystemInformationVersionPrompt = 0; public int SystemInformationVersionPrompt = 0;
public string UUID = System.Guid.NewGuid().ToString(); public string UUID = System.Guid.NewGuid().ToString();

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Traits
while (act != null) while (act != null)
{ {
var prev = act; var prev = act;
act = act.Tick(self); act = act.TickOuter(self);
var current = Stopwatch.GetTimestamp(); var current = Stopwatch.GetTimestamp();
if (current - start > longTickThresholdInStopwatchTicks) if (current - start > longTickThresholdInStopwatchTicks)
{ {