forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandTemporal.cs
More file actions
209 lines (173 loc) · 5.55 KB
/
CommandTemporal.cs
File metadata and controls
209 lines (173 loc) · 5.55 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace kOS
{
[CommandAttribute("WAIT[UNTIL]? *")]
public class CommandWait : Command
{
public CommandWait(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
private float waitTime = 0;
private Expression waitExpression = null;
public override void Evaluate()
{
Expression e = new Expression(RegexMatch.Groups[2].Value, ParentContext);
bool untilClause = (RegexMatch.Groups[1].Value.Trim().ToUpper() == "UNTIL");
if (!untilClause)
{
waitTime = e.Float();
}
else
{
waitExpression = e;
}
State = ExecutionState.WAIT;
}
public override bool SpecialKey(kOSKeys key)
{
if (key == kOSKeys.BREAK)
{
StdOut("Break.");
State = ExecutionState.DONE;
}
return base.SpecialKey(key);
}
public override bool Type(char c)
{
if (State == ExecutionState.WAIT)
{
return true;
}
else
{
return base.Type(c);
}
}
public override void Update(float time)
{
if (waitExpression != null)
{
if (waitExpression.IsTrue())
{
State = ExecutionState.DONE;
}
}
else if (waitTime > 0)
{
waitTime -= time;
if (waitTime <= 0) State = ExecutionState.DONE;
}
}
}
[CommandAttribute("ON % *")]
public class CommandOnEvent : Command
{
private Variable targetVariable;
private Command targetCommand;
private bool originalValue;
public CommandOnEvent(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
targetVariable = ParentContext.FindOrCreateVariable(RegexMatch.Groups[1].Value);
targetCommand = Command.Get(RegexMatch.Groups[2].Value, ParentContext);
if (!objToBool(targetVariable.Value, out originalValue))
{
throw new Exception("Value type error");
}
ParentContext.Lock(this);
State = ExecutionState.DONE;
}
public override void Update(float time)
{
bool newValue;
if (!objToBool(targetVariable.Value, out newValue))
{
ParentContext.Unlock(this);
throw new Exception("Value type error");
}
if (originalValue != newValue)
{
ParentContext.Unlock(this);
targetCommand.Evaluate();
ParentContext.Push(targetCommand);
}
}
public bool objToBool(object obj, out bool result)
{
if (bool.TryParse(targetVariable.Value.ToString(), out result))
{
return true;
}
else
{
if (obj is float)
{
result = ((float)obj) > 0;
return true;
}
}
return false;
}
}
[CommandAttribute("LOCK % TO *")]
public class CommandLock : Command
{
public CommandLock(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
string varname = RegexMatch.Groups[1].Value;
Expression expression = new Expression(RegexMatch.Groups[2].Value, ParentContext);
ParentContext.Unlock(varname);
ParentContext.Lock(varname, expression);
State = ExecutionState.DONE;
}
}
[CommandAttribute("UNLOCK %")]
public class CommandUnlock : Command
{
public CommandUnlock(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String varname = RegexMatch.Groups[1].Value;
if (varname.ToUpper() == "ALL")
{
ParentContext.UnlockAll();
}
else
{
ParentContext.Unlock(varname);
}
State = ExecutionState.DONE;
}
}
[CommandAttribute("WHEN ~ THEN *")]
public class CommandWhen : Command
{
private Command targetCommand;
private Expression expression;
private bool triggered = false;
public CommandWhen(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
expression = new Expression(RegexMatch.Groups[1].Value, ParentContext);
targetCommand = Command.Get(RegexMatch.Groups[2].Value, ParentContext);
ParentContext.Lock(this);
State = ExecutionState.DONE;
}
public override void Update(float time)
{
if (triggered)
{
targetCommand.Update(time);
if (targetCommand.State == ExecutionState.DONE) ParentContext.Unlock(this);
}
else if (expression.IsTrue())
{
triggered = true;
targetCommand.Evaluate();
}
}
}
}