forked from Whiplash141/SpaceEngineersScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.cs
More file actions
236 lines (210 loc) · 7.2 KB
/
Copy pathScheduler.cs
File metadata and controls
236 lines (210 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#region Scheduler
/// <summary>
/// Class for scheduling actions to occur at specific frequencies. Actions can be updated in parallel or in sequence (queued).
/// </summary>
public class Scheduler
{
public double CurrentTimeSinceLastRun { get; private set; } = 0;
public long CurrentTicksSinceLastRun { get; private set; } = 0;
QueuedAction _currentlyQueuedAction = null;
bool _firstRun = true;
bool _inUpdate = false;
readonly bool _ignoreFirstRun;
readonly List<ScheduledAction> _actionsToAdd = new List<ScheduledAction>();
readonly List<ScheduledAction> _scheduledActions = new List<ScheduledAction>();
readonly List<ScheduledAction> _actionsToDispose = new List<ScheduledAction>();
readonly Queue<QueuedAction> _queuedActions = new Queue<QueuedAction>();
readonly Program _program;
public const long TicksPerSecond = 60;
public const double TickDurationSeconds = 1.0 / TicksPerSecond;
const long ClockTicksPerGameTick = 166666L;
/// <summary>
/// Constructs a scheduler object with timing based on the runtime of the input program.
/// </summary>
public Scheduler(Program program, bool ignoreFirstRun = false)
{
_program = program;
_ignoreFirstRun = ignoreFirstRun;
}
/// <summary>
/// Updates all ScheduledAcions in the schedule and the queue.
/// </summary>
public void Update()
{
_inUpdate = true;
long deltaTicks = Math.Max(0, _program.Runtime.TimeSinceLastRun.Ticks / ClockTicksPerGameTick);
if (_firstRun)
{
if (_ignoreFirstRun)
{
deltaTicks = 0;
}
_firstRun = false;
}
_actionsToDispose.Clear();
foreach (ScheduledAction action in _scheduledActions)
{
CurrentTicksSinceLastRun = action.TicksSinceLastRun + deltaTicks;
CurrentTimeSinceLastRun = action.TimeSinceLastRun + deltaTicks * TickDurationSeconds;
action.Update(deltaTicks);
if (action.JustRan && action.DisposeAfterRun)
{
_actionsToDispose.Add(action);
}
}
if (_actionsToDispose.Count > 0)
{
_scheduledActions.RemoveAll((x) => _actionsToDispose.Contains(x));
}
if (_currentlyQueuedAction == null)
{
// If queue is not empty, populate current queued action
if (_queuedActions.Count != 0)
_currentlyQueuedAction = _queuedActions.Dequeue();
}
// If queued action is populated
if (_currentlyQueuedAction != null)
{
_currentlyQueuedAction.Update(deltaTicks);
if (_currentlyQueuedAction.JustRan)
{
if (!_currentlyQueuedAction.DisposeAfterRun)
{
_queuedActions.Enqueue(_currentlyQueuedAction);
}
// Set the queued action to null for the next cycle
_currentlyQueuedAction = null;
}
}
_inUpdate = false;
if (_actionsToAdd.Count > 0)
{
_scheduledActions.AddRange(_actionsToAdd);
_actionsToAdd.Clear();
}
}
/// <summary>
/// Adds an Action to the schedule. All actions are updated each update call.
/// </summary>
public void AddScheduledAction(Action action, double updateFrequency, bool disposeAfterRun = false, double timeOffset = 0)
{
ScheduledAction scheduledAction = new ScheduledAction(action, updateFrequency, disposeAfterRun, timeOffset);
if (!_inUpdate)
_scheduledActions.Add(scheduledAction);
else
_actionsToAdd.Add(scheduledAction);
}
/// <summary>
/// Adds a ScheduledAction to the schedule. All actions are updated each update call.
/// </summary>
public void AddScheduledAction(ScheduledAction scheduledAction)
{
if (!_inUpdate)
_scheduledActions.Add(scheduledAction);
else
_actionsToAdd.Add(scheduledAction);
}
/// <summary>
/// Adds an Action to the queue. Queue is FIFO.
/// </summary>
public void AddQueuedAction(Action action, double updateInterval, bool removeAfterRun = false)
{
if (updateInterval <= 0)
{
updateInterval = 0.001; // avoids divide by zero
}
QueuedAction scheduledAction = new QueuedAction(action, updateInterval, removeAfterRun);
_queuedActions.Enqueue(scheduledAction);
}
/// <summary>
/// Adds a ScheduledAction to the queue. Queue is FIFO.
/// </summary>
public void AddQueuedAction(QueuedAction scheduledAction)
{
_queuedActions.Enqueue(scheduledAction);
}
}
public class QueuedAction : ScheduledAction
{
public QueuedAction(Action action, double runInterval, bool removeAfterRun = false)
: base(action, 1.0 / runInterval, removeAfterRun: removeAfterRun, timeOffset: 0)
{ }
}
public class ScheduledAction
{
public bool JustRan { get; private set; } = false;
public bool DisposeAfterRun { get; private set; } = false;
public double TimeSinceLastRun { get { return TicksSinceLastRun * Scheduler.TickDurationSeconds; } }
public long TicksSinceLastRun { get; private set; } = 0;
public double RunInterval
{
get
{
return RunIntervalTicks * Scheduler.TickDurationSeconds;
}
set
{
RunIntervalTicks = (long)Math.Round(value * Scheduler.TicksPerSecond);
}
}
public long RunIntervalTicks
{
get
{
return _runIntervalTicks;
}
set
{
if (value == _runIntervalTicks)
return;
_runIntervalTicks = value < 0 ? 0 : value;
_runFrequency = value == 0 ? double.MaxValue : Scheduler.TicksPerSecond / _runIntervalTicks;
}
}
public double RunFrequency
{
get
{
return _runFrequency;
}
set
{
if (value == _runFrequency)
return;
if (value == 0)
RunIntervalTicks = long.MaxValue;
else
RunIntervalTicks = (long)Math.Round(Scheduler.TicksPerSecond / value);
}
}
long _runIntervalTicks;
double _runFrequency;
readonly Action _action;
/// <summary>
/// Class for scheduling an action to occur at a specified frequency (in Hz).
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="runFrequency">How often to run in Hz</param>
public ScheduledAction(Action action, double runFrequency, bool removeAfterRun = false, double timeOffset = 0)
{
_action = action;
RunFrequency = runFrequency; // Implicitly sets RunInterval
DisposeAfterRun = removeAfterRun;
TicksSinceLastRun = (long)Math.Round(timeOffset * Scheduler.TicksPerSecond);
}
public void Update(long deltaTicks)
{
TicksSinceLastRun += deltaTicks;
if (TicksSinceLastRun >= RunIntervalTicks)
{
_action.Invoke();
TicksSinceLastRun = 0;
JustRan = true;
}
else
{
JustRan = false;
}
}
}
#endregion