-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoomStateMachine.asc
412 lines (367 loc) · 9.53 KB
/
DoomStateMachine.asc
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Parser state, for internal use
struct DSM_Parser {
String Input;
int PeekAt;
} DSMParser;
String PeekWord() {
String input = DSMParser.Input;
//System.Log(eLogDebug, "PeekWord: %s", input);
int word_at = -1, word_end_at = -1;
int at;
for (; at < input.Length; ++at) {
if (input.Chars[at] == '\n') {
break;
}
if (input.Chars[at] != ' ') {
word_at = at;
break;
}
}
if (word_at < 0) {
return null;
}
for (; at < input.Length; ++at) {
if (input.Chars[at] == ' ' || input.Chars[at] == '\n') {
word_end_at = at;
break;
}
}
if (word_end_at < 0) {
word_end_at = input.Length;
}
DSMParser.PeekAt = word_end_at;
//System.Log(eLogDebug, "PeekWord: << %d %d", word_at, word_end_at - word_at);
return input.Substring(word_at, word_end_at - word_at);
}
String ParseWord() {
String input = DSMParser.Input;
String word = PeekWord();
if (!String.IsNullOrEmpty(word)) {
DSMParser.Input =
input.Substring(DSMParser.PeekAt + 1, input.Length - DSMParser.PeekAt - 1);
}
return word;
}
void ParseEndLine() {
String input = DSMParser.Input;
int break_at = -1;
for (int i = 0; i < input.Length; ++i) {
if (input.Chars[i] != ' ' && input.Chars[i] != '\n') {
return; // valid data, don't skip
}
if (input.Chars[i] == '\n') {
break_at = i;
break;
}
}
if (break_at >= 0) {
DSMParser.Input = input.Substring(break_at + 1, input.Length - break_at - 1);
} else {
DSMParser.Input = "";
}
}
bool ParseLoop(DSM_StateStep *step) {
String loop = ParseWord();
String frameseq = ParseWord();
if (String.IsNullOrEmpty(loop) || String.IsNullOrEmpty(frameseq)) {
return false;
}
step.Loop = loop.AsInt;
String framenums[] = SplitText(frameseq, ',');
step.Frames = new DSM_Frame[framenums.Length];
for (int f = 0; f < framenums.Length; ++f) {
step.Frames[f] = new DSM_Frame;
step.Frames[f].Frame = framenums[f].AsInt;
step.Frames[f].Delay = 0; // ?
}
// Optional args
String delay = ParseWord();
if (String.IsNullOrEmpty(delay)) {
step.Delay = -1000; // use viewframe's delay
return true;
}
step.Delay = delay.AsInt;
String action = ParseWord();
if (String.IsNullOrEmpty(action)) {
return true;
}
step.Action = action;
String chance = ParseWord();
if (String.IsNullOrEmpty(chance)) {
step.Chance = 1.0;
return true;
}
step.Chance = chance.AsFloat;
String args = ParseWord();
step.Args = args;
return true;
}
bool ParseAction(DSM_StateStep *step) {
String chance = ParseWord();
String args = ParseWord();
if (String.IsNullOrEmpty(chance) || String.IsNullOrEmpty(args)) {
//System.Log(eLogDebug, "DSM::ParseAction: failed");
return false;
}
step.Chance = chance.AsFloat;
step.Args = args;
//System.Log(eLogDebug, "DSM::ParseAction: %s %s", chance, args);
return true;
}
bool ParseStep(DSM_State *state) {
//System.Log(eLogDebug, "DSM::ParseStep:\n%s", DSMParser.Input);
String keyword = PeekWord();
if (String.IsNullOrEmpty(keyword)) {
//System.Log(eLogDebug, "DSM::ParseStep: no keyword");
return false;
}
if (keyword.CompareTo("STATE") == 0) {
//System.Log(eLogDebug, "DSM::ParseStep: peeked STATE, cancel");
return false;
}
String input = DSMParser.Input;
DSMParser.Input = input.Substring(DSMParser.PeekAt + 1, input.Length - DSMParser.PeekAt - 1);
DSM_StateStep *step = new DSM_StateStep;
bool result;
//System.Log(eLogDebug, "DSM::ParseStep: peeked %s", keyword);
if (keyword.CompareTo("LOOP") == 0) {
result = ParseLoop(step);
} else if (keyword.CompareTo("GOTO") == 0) {
step.Action = keyword;
result = ParseAction(step);
} else if (keyword.CompareTo("STOP") == 0) {
step.Action = keyword;
result = true;
} else { // ACTION
step.Action = keyword;
result = ParseAction(step);
}
if (result) {
//System.Log(eLogDebug, "DSM::ParseStep: added");
ParseEndLine();
state.AddStep(step);
} else {
//System.Log(eLogDebug, "DSM::ParseStep: failed");
}
return result;
}
bool ParseState(DSM_StateList *list) {
//System.Log(eLogDebug, "DSM::ParseState:\n%s", DSMParser.Input);
String keyword = ParseWord();
if (String.IsNullOrEmpty(keyword)) {
//System.Log(eLogDebug, "DSM::ParseState: no keyword");
return false;
}
//System.Log(eLogDebug, "DSM::ParseState: keyword = %s", keyword);
if (keyword.CompareTo("STATE") != 0) {
return false;
}
String name = ParseWord();
if (String.IsNullOrEmpty(name)) {
return false;
}
ParseEndLine();
DSM_State *state = new DSM_State;
state.Name = name;
// Parse steps
while (ParseStep(state)) {}
list.AddState(state);
return true;
}
void DSM_State::AddStep(DSM_StateStep *step) {
int index = Array_TryAdd(Steps, step);
if (index < 0) {
int old_len = Array_SafeLength(Steps);
DSM_StateStep *new_arr[] = new DSM_StateStep[old_len + 10];
Array_Copy(new_arr, Steps, old_len);
new_arr[old_len] = step;
Steps = new_arr;
}
}
static DSM_StateList *DSM_StateList::CreateFromText(int view, String text) {
DSM_StateList *list = new DSM_StateList;
list.View = view;
// Parse whole thing
//System.Log(eLogDebug, "DSM_StateList::CreateFromText: %d\n%s", view, text);
DSMParser.Input = text;
while (ParseState(list)) {}
/*
System.Log(eLogInfo, "DSM_StateList::CreateFromText: created:");
System.Log(eLogInfo, "--- states %d", list.States.Length);
for (int s = 0; s < list.States.Length; ++s) {
DSM_State *state = list.States[s];
System.Log(eLogInfo, "--- state %d %s", s, state.Name);
System.Log(eLogInfo, "--- steps %d", state.Steps.Length);
for (int p = 0; p < state.Steps.Length; ++p) {
DSM_StateStep *step = state.Steps[p];
if (step.Frames != null) {
System.Log(eLogInfo, "--- step %d LOOP %d, %d frames", p, step.Loop, step.Frames.Length);
} else {
System.Log(eLogInfo, "--- step %d %s %s (%0.2f)", p, step.Action, step.Args, step.Chance);
}
}
}
*/
return list;
}
DSM_State *DSM_StateList::FindState(String name) {
for (int i = 0; i < States.Length; ++i) {
if (States[i].Name.CompareTo(name) == 0) {
return States[i];
}
}
return null;
}
int DSM_StateList::FindStateIndex(String name) {
for (int i = 0; i < States.Length; ++i) {
if (States[i].Name.CompareTo(name) == 0) {
return i;
}
}
return -1;
}
void DSM_StateList::AddState(DSM_State *state) {
int index = Array_TryAdd(States, state);
if (index < 0) {
int old_len = Array_SafeLength(States);
DSM_State *new_arr[] = new DSM_State[old_len + 10];
Array_Copy(new_arr, States, old_len);
new_arr[old_len] = state;
States = new_arr;
}
}
static DSM_StateRunner *DSM_StateRunner::Create(DSM_StateList *list) {
if (list.States == null) {
return null;
}
DSM_StateRunner *runner = new DSM_StateRunner;
runner.List = list;
runner.View = list.View;
runner.StartState();
return runner;
}
String DSM_StateRunner::Tick() {
if (--Timer > 0) {
return null;
}
NextFrame();
return Action;
}
bool DSM_StateRunner::Goto(String state) {
int index = List.FindStateIndex(state);
if (index < 0) {
return false;
}
State = index;
StartState();
return true;
}
protected void DSM_StateRunner::StartState() {
Step = -1;
FrameIndex = 0;
Loop = -1;
Frame = -1;
Timer = 0;
//System.Log(eLogDebug, "StartState: %d", State);
NextStep();
}
protected void DSM_StateRunner::StartStep() {
DSM_StateStep *step = List.States[State].Steps[Step];
Loop = step.Loop;
FrameIndex = -1;
Frame = -1;
Timer = 0;
Action = null;
Args = null;
//System.Log(eLogDebug, "StartStep: %d-%d", State, Step);
if (step.Frames != null && step.Frames.Length > 0) {
// Sequence of frames
NextFrame();
} else {
// A single action
if (step.Chance == 0.0 || Maths.HitChance(step.Chance)) {
DoAction();
}
}
}
protected void DSM_StateRunner::StartFrame() {
//System.Log(eLogDebug, "StartFrame: %d-%d-%d", State, Step, FrameIndex);
DSM_StateStep *step = List.States[State].Steps[Step];
Frame = step.Frames[FrameIndex].Frame;
Timer = step.Frames[FrameIndex].Delay + step.Delay;
float chance = step.Chance;
if (chance > 0.0 && Maths.HitChance(chance)) {
DoAction();
} else {
Action = null;
Args = null;
}
}
protected void DSM_StateRunner::NextState() {
//System.Log(eLogDebug, "NextState: %d->%d", State, State + 1);
if (List.States == null) {
return;
}
if (++State < List.States.Length) {
StartState();
} else {
Stop();
}
}
protected void DSM_StateRunner::NextStep() {
//System.Log(eLogDebug, "NextStep: %d->%d", Step, Step + 1);
Frame = -1;
if (List.States[State].Steps == null) {
return;
}
while (Frame < 0 && (++Step < List.States[State].Steps.Length)) {
// Next step
StartStep();
}
if (Step >= List.States[State].Steps.Length) {
// Next state (goto missing?)
NextState();
}
}
protected void DSM_StateRunner::NextFrame() {
//System.Log(eLogDebug, "NextFrame[%d,%d]: %d->%d", State, Step, FrameIndex, FrameIndex + 1);
if (List.States[State].Steps[Step].Frames == null) {
return;
}
if (++FrameIndex < List.States[State].Steps[Step].Frames.Length) {
// Next frame
StartFrame();
} else {
// Step complete, next step
NextStep();
}
}
protected void DSM_StateRunner::Stop() {
//System.Log(eLogDebug, "Stop");
// TODO: implement!
// wrap to state 0
State = 0;
StartState();
}
protected void DSM_StateRunner::DoAction() {
DSM_StateStep *step = List.States[State].Steps[Step];
Action = step.Action;
Args = step.Args;
if (String.IsNullOrEmpty(Action)) {
return;
}
//System.Log(eLogDebug, "DoAction: %s %s", Action, Args != null ? Args : "(none)");
// Internal actions
if (Action.CompareTo("goto") == 0) {
// Goto another state
int state_idx = List.FindStateIndex(Args);
if (state_idx >= 0) {
State = state_idx;
StartState();
} else {
Stop();
}
} else if (Action.CompareTo("stop") == 0) {
Stop();
}
}