-
Notifications
You must be signed in to change notification settings - Fork 10
/
storyparser.pas
634 lines (601 loc) · 15.3 KB
/
storyparser.pas
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
unit StoryParser;
interface
uses
SysUtils, Types, outputcolor, endprogram;
const
TokenEnd = 'end';
TokenText = 'text';
TokenTexts = 'texts';
TokenCmd = 'cmd';
TokenToLocation = 'toLocation';
TokenToEvent = 'toEvent';
TokenEffect = 'effect';
TokenEffects = 'effects';
TokenValue = 'value';
TokenCommand = 'command';
TokenCommands = 'commands';
TokenEvent = 'event';
TokenEvents = 'events';
TokenTransition = 'transition';
TokenTransitions = 'transitions';
TokenCondition = 'condition';
TokenConditions = 'conditions';
TokenAttribute = 'attribute';
TokenLocation = 'location';
TokenLocations = 'locations';
TokenStory = 'story';
TokenStories = 'stories';
function ReadToken(var text: TextFile; var token: String): Boolean;
function ReadToken(var text: TextFile; var token: TColorString): Boolean;
function ReadTexts(var text: TextFile; var texts: TMultiLineColorText; defaultColor: String): Boolean;
function ReadLocation(var text: TextFile; var location: TLocation): Boolean;
function ReadLocations(var text: TextFile; var locations: TLocations): Boolean;
function ReadStory(var text: TextFile; var story: TStory): Boolean;
function ReadStories(var text: TextFile; var stories: TStories): Boolean;
function ReadEvent(var text: TextFile; var event: TEvent): Boolean;
function ReadEvents(var text: TextFile; var events: TEvents): Boolean;
function ReadCommand(var text: TextFile; var command: TCommand): Boolean;
function ReadCommands(var text: TextFile; var commands: TCommands): Boolean;
function ReadTransition(var text: TextFile; var transition: TTransition): Boolean;
function ReadTransitions(var text: TextFile; var transitions: TTransitions): Boolean;
function ReadCondition(var text: TextFile; var condition: TCondition): Boolean;
function ReadConditions(var text: TextFile; var conditions: TConditions): Boolean;
function ReadEffect(var text: TextFile; var effect: TEffect): Boolean;
function ReadEffects(var text: TextFile; var effects: TEffects): Boolean;
function LoadStory(fileName: String; var locations: TLocations; folderPath: String): Boolean;
function LoadStories(fileName: String; var locations: TLocations; var currentPosition: TPosition; folderPath: String): Boolean;
implementation
function ReadEffect(var text: TextFile; var effect: TEffect): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, effect.name) then
Exit(False);
while ReadToken(text, token) do
begin
if token = TokenText then
ReadToken(text, effect.text);
if token = TokenValue then
begin
ReadToken(text, token);
effect.value := StrToInt(token);
end;
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenEffect then
Exit(True);
end;
end;
Exit(False);
end;
function ReadEffects(var text: TextFile; var effects: TEffects): Boolean;
var
token: String;
effectsCount, I: Integer;
begin
ReadToken(text, token);
effectsCount := StrToInt(token);
SetLength(effects, effectsCount);
for I:= 0 to effectsCount - 1 do
begin
if ReadToken(text, token) then
begin
if (token = TokenEffect) then
ReadEffect(text, effects[I]);
end;
end;
ReadToken(text, token);
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenEffect then
Exit(True);
end;
Exit(False);
end;
function ReadCondition(var text: TextFile; var condition: TCondition): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, condition.name) then
Exit(False);
while ReadToken(text, token) do
begin
if token = TokenAttribute then
ReadToken(text, condition.attribute);
if token = TokenValue then
begin
ReadToken(text, token);
condition.value := StrToInt(token);
end;
if token = TokenText then
begin
condition.isMultiLine := false;
ReadToken(text, condition.text);
if (condition.text.color = '') then
condition.text.color := ColorConditionText;
end;
if token = TokenTexts then
begin
condition.isMultiLine := true;
ReadTexts(text, condition.texts, ColorConditionText);
end;
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenCondition then
Exit(True);
end
end;
Exit(False);
end;
function ReadConditions(var text: TextFile; var conditions: TConditions): Boolean;
var
token: String;
conditionsCount, I: Integer;
begin
ReadToken(text, token);
conditionsCount := StrToInt(token);
SetLength(conditions, conditionsCount);
for I:= 0 to conditionsCount - 1 do
begin
if (ReadToken(text, token)) then
begin
if (token = TokenCondition) then
begin
ReadCondition(text, conditions[I]);
end;
end;
end;
ReadToken(text, token);
if token = TokenEnd then
begin
ReadToken(text, token);
end;
Exit(True);
end;
function ReadTransition(var text: TextFile; var transition: TTransition): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, transition.name) then
Exit(False);
while ReadToken(text, token) do
begin
if token = TokenConditions then
ReadConditions(text, transition.conditions);
if token = TokenText then
begin
transition.isMultiLine := false;
ReadToken(text, transition.text);
if (transition.text.color = '') then
transition.text.color := ColorTransitionText;
end;
if token = TokenTexts then
begin
transition.isMultiLine := true;
ReadTexts(text, transition.texts, ColorTransitionText);
end;
if token = TokenEffects then
ReadEffects(text, transition.effects);
if token = TokenToLocation then
begin
ReadToken(text, transition.toLocation);
end;
if token = TokenToEvent then
begin
ReadToken(text, transition.toEvent);
end;
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenTransition then
Exit(True);
end
end;
Exit(False);
end;
function ReadTransitions(var text: TextFile; var transitions: TTransitions): Boolean;
var
token: String;
transitionsCount, I: Integer;
begin
ReadToken(text, token);
transitionsCount := StrToInt(token);
SetLength(transitions, transitionsCount);
for I:= 0 to transitionsCount - 1 do
begin
if (ReadToken(text, token)) then
if (token = TokenTransition) then
ReadTransition(text, transitions[I]);
end;
ReadToken(text, token);
if token = TokenEnd then
ReadToken(text, token);
Exit(True);
end;
function ReadCommand(var text: TextFile; var command: TCommand): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, command.name) then
Exit(False);
while ReadToken(text, token) do
begin
if token = TokenText then
begin
command.isMultiLine := false;
ReadToken(text, command.text);
if command.text.color = '' then
command.text.color := ColorCommandText;
end;
if token = TokenTexts then
begin
command.isMultiLine := true;
ReadTexts(text, command.texts, ColorCommandText);
end;
if token = TokenConditions then
ReadConditions(text, command.conditions);
if token = TokenCmd then
ReadToken(text, command.cmd);
if token = TokenTransitions then
ReadTransitions(text, command.transitions);
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenCommand then
begin
Exit(True);
end;
end
end;
Exit(False);
end;
function ReadCommands(var text: TextFile; var commands: TCommands): Boolean;
var
token: String;
commandsCount, I: Integer;
begin
ReadToken(text, token);
commandsCount := StrToInt(token);
SetLength(commands, commandsCount);
for I:= 0 to commandsCount - 1 do
begin
if (ReadToken(text, token)) then
begin
if (token = TokenCommand) then
begin
ReadCommand(text, commands[I]);
end;
end;
end;
ReadToken(text, token);
if token = TokenEnd then
begin
ReadToken(text, token);
end;
Exit(True);
end;
function ReadEvent(var text: TextFile; var event: TEvent): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, event.name) then
Exit(False);
if (event.name.color = '') then
event.name.color := ColorEventName;
//Write('Event: ');
//ColorWrite(event.name.text, event.name.color, 1);
while ReadToken(text, token) do
begin
if token = TokenText then
begin
event.isMultiLine := false;
ReadToken(text, event.text);
if (event.text.color = '') then
event.text.color := ColorEventText;
end;
if token = TokenTexts then
begin
event.isMultiLine := true;
ReadTexts(text, event.texts, ColorEventText);
end;
if token = TokenCommands then
ReadCommands(text, event.commands);
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenEvent then
Exit(True);
end;
end;
Exit(True);
end;
function ReadEvents(var text: TextFile; var events: TEvents): Boolean;
var
token: String;
eventsCount, I: Integer;
begin
ReadToken(text, token);
eventsCount := StrToInt(token);
SetLength(events, eventsCount);
for I := 0 to eventsCount - 1 do
begin
ReadToken(text, token);
if (token = TokenEvent) then
begin
ReadEvent(text, events[I]);
end;
end;
ReadToken(text, token);
if token = TokenEnd then
begin
//Writeln('events end');
ReadToken(text, token);
end;
Exit(True);
end;
function ReadLocation(var text: TextFile; var location: TLocation): Boolean;
var
token: String;
begin
token := '';
if not ReadToken(text, location.name) then
Exit(False);
while ReadToken(text, token) do
begin
if token = TokenEvents then
ReadEvents(text, location.events);
if token = TokenEnd then
begin
ReadToken(text, token);
if token = TokenLocation then
Exit(True);
end;
end;
end;
function ReadLocations(var text: TextFile; var locations: TLocations): Boolean;
var
token: String;
locationsCount, I: Integer;
preLength: Integer;
begin
ReadToken(text, token);
locationsCount := StrToInt(token);
preLength := Length(locations);
SetLength(locations, locationsCount + Length(locations));
for I := (preLength) to (locationsCount + preLength - 1) do
begin
ReadToken(text, token);
if (token = TokenLocation) then
begin
ReadLocation(text, locations[I]);
end;
end;
ReadToken(text, token);
if token = TokenEnd then
begin
ReadToken(text, token);
end;
Exit(True);
end;
function ReadStory(var text: TextFile; var story: TStory): Boolean;
begin
ReadToken(text, story);
Exit(true);
end;
function ReadStories(var text: TextFile; var stories: TStories): Boolean;
var
I: Integer;
StoryCount: Integer;
token: String;
begin
ReadToken(text, token);
StoryCount := StrToInt(token);
SetLength(stories, StoryCount);
for I := 0 to StoryCount - 1 do
begin
ReadToken(text, token);
if token = TokenStory then
ReadStory(text, stories[I]);
end;
ReadToken(text, token);
if token = TokenEnd then
begin
ReadToken(text, token);
Exit(true);
end;
end;
function ReadTexts(var text: TextFile; var texts: TMultiLineColorText; defaultColor: String): Boolean;
var
I: Integer;
LinesCount: Integer;
token: String;
begin
ReadToken(text, token);
LinesCount := StrToInt(token);
SetLength(texts, LinesCount);
for I := 0 to LinesCount - 1 do
begin
ReadToken(text, token);
if token = tokenText then
begin
ReadToken(text, texts[I]);
if (texts[I].color = '') then
begin
texts[I].color := defaultColor;
end;
end;
end;
ReadToken(text, token);
if (token = TokenEnd) then
begin
ReadToken(text, token);
end;
Exit(True);
end;
function ReadToken(var text: TextFile; var token: String): Boolean;
var
isStarted, isSpecial: Boolean;
ch: Char;
begin
isSpecial := False;
isStarted := False;
token := '';
ch := Chr(0);
while not EOF(text) do
begin
Read(text, ch);
if ch in ['A'..'Z', 'a'..'z', '0'..'9', ',', ''''] then
begin
if ch = '''' then
begin
isSpecial := True;
if not isStarted then
begin
isStarted := True;
continue;
end
else
begin
break;
end;
end
else
isStarted := True;
end
else
begin
if (not isSpecial) and isStarted then
begin
break;
end;
end;
if isStarted then
begin
token := token + ch;
end;
end;
ReadToken := not EOF(text);
if (token = '') and EOF(text) then
begin
Writeln('danger, ', Ord(ch));
ReadLn();
end;
end;
function ReadToken(var text: TextFile; var token: TColorString): Boolean;
var
isStarted, isSpecial, isColor: Boolean;
ch: Char;
begin
isSpecial := False;
isStarted := False;
isColor := False;
token.text := '';
token.color := '';
ch := Chr(0);
while not EOF(text) do
begin
Read(text, ch);
if ch in ['A'..'Z', 'a'..'z', '0'..'9', '''', '#'] then
begin
if ch = '''' then
begin
isSpecial := True;
if not isStarted then
begin
isStarted := True;
continue;
end
else
begin
break;
end;
end
else
isStarted := True;
if ch = '#' then
begin
isColor := true;
continue;
end;
end
else
begin
if (not isSpecial) and isStarted then
begin
break;
end;
end;
if isStarted then
begin
if not isColor then
token.text := token.text + ch
else
token.color := token.color + ch;
end;
end;
ReadToken := not EOF(text);
end;
function LoadStory(fileName: String; var locations: TLocations; folderPath: String): Boolean;
var
text: TextFile;
token: String;
filePath: String;
begin
fileName := fileName + '.spt';
filePath := folderPath + '/' + fileName;
Assign(text, filePath);
{$I-}
Reset(text);
{$I+}
if (IoResult <> 0) then
begin
ExitProgram(1, 'Could not read ' + filePath);
end;
ReadToken(text, token);
if token = TokenLocations then
ReadLocations(text, locations);
Close(text);
//ColorWrite('[R+] ', ColorDebug);
//ColorWrite(fileName, ColorDebug, 1);
Exit(True);
end;
function LoadStories(fileName: String; var locations: TLocations; var currentPosition: TPosition; folderPath: String): Boolean;
var
I: Integer;
text: TextFile;
token: String;
stories: TStories;
filePath: String;
begin
filePath := folderPath + '/' + fileName;
Assign(text, filePath);
{$I-}
Reset(text);
{$I+}
if (IoResult <> 0) then
begin
ExitProgram(1, 'Could not read ' + filePath);
end;
ReadToken(text, token);
if token = TokenStories then
ReadStories(text, stories);
Close(text);
//ColorWrite('[+] Stories', ColorDebug, 1);
for I := 0 to Length(stories) - 1 do
begin
LoadStory(stories[I], locations, folderPath);
end;
currentPosition.location := locations[0];
currentPosition.event := currentPosition.location.events[0];
//ColorWrite('[+] Locations', ColorDebug, 1);
Exit(True);
end;
end.