-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.inc
1884 lines (1838 loc) · 73.6 KB
/
parser.inc
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//{$DEFINE DEBUG_PARSER} // -*- delphi -*-
// Context is:
//
// procedure TWorld.Perform(Command: UTF8String; Player: TPlayer);
// var
// Tokens, OriginalTokens: TTokens;
// CurrentToken: Cardinal;
function CanEnd(): Boolean;
begin
Result := (CurrentToken >= Length(Tokens)) or
(Tokens[CurrentToken] = 'then') or
(Tokens[CurrentToken] = 'and') or
(Tokens[CurrentToken] = ';') or
(Tokens[CurrentToken] = '.') or
((CurrentToken+1 < Length(Tokens)) and
(Tokens[CurrentToken] = ',') and
((Tokens[CurrentToken+1] = 'then') or
(Tokens[CurrentToken+1] = 'and')));
end;
function IsQuote(): Boolean;
begin
Result := (CurrentToken < Length(Tokens)) and
(Length(Tokens[CurrentToken]) >= 2) and
(Tokens[CurrentToken][1] = '"');
end;
procedure EnsureNoPunctuation();
begin
Assert(not CanEnd());
if (Tokens[CurrentToken] = ',') then
Fail('I don''t understand your use of commas.');
if (Tokens[CurrentToken] = ';') then
Fail('I don''t understand your use of semicolons.');
if (Tokens[CurrentToken] = ':') then
Fail('I don''t understand your use of colons.');
if (Tokens[CurrentToken] = '.') then
Fail('I don''t understand your use of periods.');
if (Tokens[CurrentToken] = '?') then
Fail('I don''t understand your use of question marks.');
if (Tokens[CurrentToken] = '!') then
Fail('I don''t understand your use of exclamation marks.');
if ((Length(Tokens[CurrentToken]) >= 2) and (Tokens[CurrentToken][1] = '"')) then
Fail('I don''t understand your use of quotation marks.');
end;
procedure SkipComma();
begin
if ((CurrentToken < Length(Tokens)) and (Tokens[CurrentToken] = ',')) then
Inc(CurrentToken);
end;
function GetContinuation(): Boolean; { returns true if there's another action to parse }
begin
Assert(CanEnd());
if (CurrentToken < Length(Tokens)) then
begin
if (Tokens[CurrentToken] = '.') then
begin
Inc(CurrentToken);
Result := CurrentToken < Length(Tokens);
end
else
begin
if (Tokens[CurrentToken] = ';') then
begin
Inc(CurrentToken);
if (CurrentToken >= Length(Tokens)) then
Fail('I don''t understand your use of semicolons.');
end
else
if (Tokens[CurrentToken] = ',') then
begin
Inc(CurrentToken);
if (CurrentToken >= Length(Tokens)) then
raise Exception.Create('Found inconsistency between CanEnd() and GetContinuation() when parsing comma in "' + Command + '".');
end;
if (Tokens[CurrentToken] = 'and') then
begin
Inc(CurrentToken);
SkipComma();
if (CurrentToken >= Length(Tokens)) then
Fail('And what?');
end;
if (Tokens[CurrentToken] = 'then') then
begin
Inc(CurrentToken);
SkipComma();
if (CurrentToken >= Length(Tokens)) then
Fail('Then what?');
end;
Result := True;
end;
end
else
Result := False;
end;
type
TDirectionOptions = set of (optAllowDefiniteArticle, optRequireCompassDirection);
function TryDirection(out Direction: TCardinalDirection; Options: TDirectionOptions): Boolean;
var
FoundDefiniteArticle: Boolean = False;
begin
Result := False;
if ((optAllowDefiniteArticle in Options) and (Tokens[CurrentToken] = 'the')) then
begin
FoundDefiniteArticle := True;
Inc(CurrentToken);
if (CanEnd()) then
begin
Dec(CurrentToken);
Exit;
end;
end;
if (Tokens[CurrentToken] = 'north') or (Tokens[CurrentToken] = 'n') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdNorth;
if (not CanEnd()) then
begin
if (Tokens[CurrentToken] = 'east') or (Tokens[CurrentToken] = 'e') then
begin
Inc(CurrentToken);
Direction := cdNorthEast;
end
else if (Tokens[CurrentToken] = 'west') or (Tokens[CurrentToken] = 'w') then
begin
Inc(CurrentToken);
Direction := cdNorthWest;
end
end;
end
else if (Tokens[CurrentToken] = 'northeast') or (Tokens[CurrentToken] = 'ne') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdNorthEast;
end
else if (Tokens[CurrentToken] = 'east') or (Tokens[CurrentToken] = 'e') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdEast;
end
else if (Tokens[CurrentToken] = 'southeast') or (Tokens[CurrentToken] = 'se') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdSouthEast;
end
else if (Tokens[CurrentToken] = 'south') or (Tokens[CurrentToken] = 's') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdSouth;
if (not CanEnd()) then
begin
if (Tokens[CurrentToken] = 'east') or (Tokens[CurrentToken] = 'e') then
begin
Inc(CurrentToken);
Direction := cdSouthEast;
end
else if (Tokens[CurrentToken] = 'west') or (Tokens[CurrentToken] = 'w') then
begin
Inc(CurrentToken);
Direction := cdSouthWest;
end
end;
end
else if (Tokens[CurrentToken] = 'southwest') or (Tokens[CurrentToken] = 'sw') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdSouthWest;
end
else if (Tokens[CurrentToken] = 'west') or (Tokens[CurrentToken] = 'w') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdWest;
end
else if (Tokens[CurrentToken] = 'northwest') or (Tokens[CurrentToken] = 'nw') then
begin
Result := True;
Inc(CurrentToken);
Direction := cdNorthWest;
end
else if ((not FoundDefiniteArticle) and (not (optRequireCompassDirection in Options))) then
begin
if ((Tokens[CurrentToken] = 'up') or (Tokens[CurrentToken] = 'u')) then
begin
Result := True;
Inc(CurrentToken);
Direction := cdUp;
end
else if ((Tokens[CurrentToken] = 'down') or (Tokens[CurrentToken] = 'd')) then
begin
Result := True;
Inc(CurrentToken);
Direction := cdDown;
end
else if ((Tokens[CurrentToken] = 'out') or (Tokens[CurrentToken] = 'outside')) then
begin
Result := True;
Inc(CurrentToken);
Direction := cdOut;
end;
end;
if (FoundDefiniteArticle and not Result) then
Dec(CurrentToken);
end;
function TryPrepositionDirection(Preposition: UTF8String; out Direction: TCardinalDirection): Boolean;
begin
Result := False;
if (Tokens[CurrentToken] = Preposition) then
begin
Inc(CurrentToken);
if ((not CanEnd()) and (TryDirection(Direction, [optAllowDefiniteArticle, optRequireCompassDirection]))) then
Result := True
else
Dec(CurrentToken);
end;
end;
{$IFDEF DEBUG}
function GetLocation(out Target: TAtom): Boolean;
var
Location: TLocation;
begin
Assert(not CanEnd());
EnsureNoPunctuation();
for Location in FLocations do
begin
if (TryMatch(CurrentToken, Tokens, TokeniseCanonically(Location.GetName(Player)))) then
begin
Target := Location;
Result := True;
Exit;
end;
end;
Result := False;
end;
function GetLocation(out Target: TLocation): Boolean;
var
Location: TAtom;
begin
Result := GetLocation(Location);
if (Result) then
Target := Location as TLocation;
end;
{$ENDIF}
{ You can assume that either CanEnd() is true or EnsureNoPunctuation() has been called just before this returns }
{ Do not call if CanEnd(). }
function GetThing(out Thing: TThing; EndingClauses: TEndingClauseKinds; VerbString: UTF8String): Boolean;
var
Things: TThingList;
Disambiguate: Boolean;
Count: Cardinal;
begin
Assert(not CanEnd());
EnsureNoPunctuation();
Result := GlobalThingCollector.Collect(Player, Tokens, OriginalTokens, CurrentToken, [], [], EndingClauses, VerbString);
if (Result) then
begin
Count := GlobalThingCollector.GetTokenCount();
Disambiguate := GlobalThingCollector.GetDisambiguate();
Things := GlobalThingCollector.GetThingList(); { resets the thing collector }
Assert(Things.Length = 1);
try
if (not CanEnd()) then
EnsureNoPunctuation();
Thing := Things.First;
Inc(CurrentToken, Count);
if (Disambiguate) then
Player.AutoDisambiguated(Thing.GetLongDefiniteName(Player)); // XXX should really let the caller do that
finally
Things.Free();
end;
end;
end;
{ You can assume that either CanEnd() is true or EnsureNoPunctuation() has been called just before this returns }
{ Do not call if CanEnd(). }
function GetThing(out Thing: TAtom; EndingClauses: TEndingClauseKinds; VerbString: UTF8String): Boolean;
var
ActualThing: TThing;
begin
Result := GetThing(ActualThing, EndingClauses, VerbString);
Thing := ActualThing;
end;
{ You can assume that either CanEnd() is true or EnsureNoPunctuation() has been called just before this returns }
{ Do not call if CanEnd(). }
function GetThings(out Things: TThingList; AllImpliedScope: TAllImpliedScope; EndingClauses: TEndingClauseKinds; VerbString: UTF8String; NothingFail: UTF8String = ''): Boolean;
var
Disambiguate: Boolean;
Count: Cardinal;
begin
Things := nil;
Assert(not CanEnd());
EnsureNoPunctuation();
Result := GlobalThingCollector.Collect(Player, Tokens, OriginalTokens, CurrentToken, [optAllowMultiples], AllImpliedScope, EndingClauses, VerbString);
if (Result) then
begin
Count := GlobalThingCollector.GetTokenCount();
Disambiguate := GlobalThingCollector.GetDisambiguate();
Things := GlobalThingCollector.GetThingList(); { resets the thing collector }
try
if (not CanEnd()) then
EnsureNoPunctuation();
if (Things.Length = 0) then
begin
if (NothingFail <> '') then
Fail(NothingFail)
else
Fail('I don''t see anything to ' + VerbString + ' here.');
end;
Inc(CurrentToken, Count);
if (Disambiguate) then
Player.AutoDisambiguated(Things.GetLongDefiniteString(Player, 'and')); // XXX should really let the caller do that
except
{ in case we fail }
Things.Free();
Things := nil;
raise;
end;
end;
end;
{ You can assume EnsureNoPunctuation() has been called just before this returns }
function GetCompoundPrepositionThing(Prepositions: array of UTF8String; EndingClauses: TEndingClauseKinds; VerbString, PrepositionString: UTF8String; out Thing: TThing): Boolean;
var
Direction: TCardinalDirection;
Index: Cardinal;
begin
Assert(Length(Prepositions) > 0);
if (CurrentToken + Length(Prepositions) <= Length(Tokens)) then
begin
for Index := Low(Prepositions) to High(Prepositions) do // $R-
begin
{$IFDEF DEBUG_PARSER} Writeln('GetCompoundPrepositionThing() comparing "', Tokens[CurrentToken+Index], '" to "', Prepositions[Index], '"'); {$ENDIF}
if (Tokens[CurrentToken+Index] <> Prepositions[Index]) then
begin
Result := False;
{$IFDEF DEBUG_PARSER} Writeln('GetCompoundPrepositionThing() failed'); {$ENDIF}
Exit;
end;
end;
{$IFDEF DEBUG_PARSER} Writeln('GetCompoundPrepositionThing() invoking the thingseeker'); {$ENDIF}
Inc(CurrentToken, Length(Prepositions));
Result := True;
if (CanEnd()) then
Fail('What do you want to ' + VerbString + ' ' + PrepositionString + '?');
if (not GetThing(Thing, EndingClauses, VerbString + ' ' + PrepositionString)) then
begin
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('That''s a direction, I don''t know how to ' + VerbString + ' ' + PrepositionString + ' it.')
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here to ' + VerbString + ' ' + PrepositionString + '.');
end;
end
else
Result := False;
end;
{ You can assume EnsureNoPunctuation() has been called just before this returns }
function GetCompoundPrepositionThing(Prepositions: array of UTF8String; EndingClauses: TEndingClauseKinds; VerbString, PrepositionString: UTF8String; out Thing: TAtom): Boolean;
var
ActualThing: TThing;
begin
Result := GetCompoundPrepositionThing(Prepositions, EndingClauses, VerbString, PrepositionString, ActualThing);
Thing := ActualThing;
end;
{ You can assume EnsureNoPunctuation() has been called just before this returns }
function GetPrepositionThing(Preposition: UTF8String; EndingClauses: TEndingClauseKinds; VerbString: UTF8String; out Thing: TAtom): Boolean;
begin
Result := GetCompoundPrepositionThing([Preposition], EndingClauses, VerbString, Preposition, Thing);
end;
{ You can assume EnsureNoPunctuation() has been called just before this returns }
function GetPrepositionThing(Preposition: UTF8String; EndingClauses: TEndingClauseKinds; VerbString: UTF8String; out Thing: TThing): Boolean;
begin
Result := GetCompoundPrepositionThing([Preposition], EndingClauses, VerbString, Preposition, Thing);
end;
function GetDirection(var Action: TAction): Boolean;
begin
if (TryDirection(Action.GoDirection, [optAllowDefiniteArticle])) then
begin
Result := True;
Action.Verb := avGo;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to go ' + CardinalDirectionToString(Action.GoDirection) + ' "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
Result := False;
end;
function GetInventory(var Action: TAction): Boolean;
begin
// lone "i" is handled in GetSelfDescriptionOrConsumeLeadingI()
if (Tokens[CurrentToken] = 'inventory') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avInventory;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to inventory "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
Result := False;
end;
function GetSelfDescriptionOrConsumeLeadingI(var Action: TAction): Boolean;
var
Pronouns: TPronouns;
function GetPronouns(Word1, Word2: String; CandidatePronouns: TPronouns): Boolean;
begin
if (Tokens[CurrentToken] = Word1) then
begin
Inc(CurrentToken);
if ((not CanEnd()) and (Tokens[CurrentToken] = Word2)) then
begin
Inc(CurrentToken);
end;
Pronouns := CandidatePronouns;
Result := True;
end
else
if (Tokens[CurrentToken] = Word1 + '/' + Word2) then
begin
Inc(CurrentToken);
Pronouns := CandidatePronouns;
Result := True;
end
else
Result := False;
end;
begin
if (Tokens[CurrentToken] = 'i') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
Result := True;
Action.Verb := avInventory;
end
else
if (Tokens[CurrentToken] = 'use') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
Fail('You use what?');
end
else
if (Tokens[CurrentToken] = 'singular') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
Fail('You use singular what?');
end
else
if (not GetPronouns('they', 'them', pSingularThey)) then
begin
EnsureNoPunctuation();
Fail('I don''t understand what it means to use singular "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
if (Tokens[CurrentToken] = 'plural') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
Fail('You use plural what?');
end
else
if (not GetPronouns('they', 'them', pPluralThey)) then
begin
EnsureNoPunctuation();
Fail('I don''t understand what it means to use plural "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
if (not GetPronouns('he', 'him', pHe) and
not GetPronouns('she', 'her', pShe) and
not GetPronouns('they', 'them', pSingularThey) and
not GetPronouns('it', 'its', pIt) and
not GetPronouns('ze', 'zem', pZe)) then
begin
EnsureNoPunctuation();
Fail('I don''t understand what it means to use "' + OriginalTokens[CurrentToken] + '".');
end;
if ((not CanEnd()) and (Tokens[CurrentToken] = 'pronouns')) then
begin
Inc(CurrentToken);
end;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand what it means to use pronouns "' + OriginalTokens[CurrentToken] + '".');
end;
Result := True;
Action.Verb := avPronouns;
Action.Pronouns := Pronouns;
end
else
begin
// leading "i" is consumed; all subsequent parsers can therefore ignore it
Result := False;
end;
end
else
Result := False;
end;
function GetLook(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
begin
if (Tokens[CurrentToken] = 'look') or (Tokens[CurrentToken] = 'l') then
begin
Inc(CurrentToken);
Result := True;
if (CanEnd()) then
begin
Action.Verb := avLook;
end
else
if (TryDirection(Action.LookDirection, []) or
TryPrepositionDirection('at', Action.LookDirection) or
TryPrepositionDirection('to', Action.LookDirection) or
TryPrepositionDirection('towards', Action.LookDirection)) then
begin
Action.Verb := avLookDirectional;
if (not CanEnd()) then
Fail('I don''t understand how to look ' + CardinalDirectionToString(Action.LookDirection) + ' "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('at', [], 'look', Action.LookAtSubject)) then
begin
Action.Verb := avLookAt;
if (not CanEnd()) then
Fail('I don''t understand how to look at things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('under', [], 'look', Action.LookUnder)) then
begin
Action.Verb := avLookUnder;
if (not CanEnd()) then
Fail('I don''t understand how to look under things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('below', [], 'look', Action.LookUnder)) then
begin
Action.Verb := avLookUnder;
if (not CanEnd()) then
Fail('I don''t understand how to look below things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('in', [], 'look', Action.LookIn)) then
begin
Action.Verb := avLookIn;
if (not CanEnd()) then
Fail('I don''t understand how to look in things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('inside', [], 'look', Action.LookIn)) then
begin
Action.Verb := avLookIn;
if (not CanEnd()) then
Fail('I don''t understand how to look inside things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (Tokens[CurrentToken] = 'for') then
begin
Inc(CurrentToken);
Action.Verb := avFind;
if (CanEnd()) then
Fail('What do you want to look for?');
if (not GetThing(Action.FindSubject, [], 'look for')) then
begin
// Action.FindSubject is invalid here but it doesn't matter because we immediately throw
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('I don''t understand how to look for a direction.')
else
Fail('I can''t find anything like ' + IndefiniteArticle(Tokens[CurrentToken]) + ' "' + OriginalTokens[CurrentToken] + '" here.');
end;
if (not CanEnd()) then
Fail('I don''t understand how to look for things "' + OriginalTokens[CurrentToken] + '".');
end
else
begin
Fail('I don''t understand how to look "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
Result := False;
end;
function GetExamine(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
begin
if (Tokens[CurrentToken] = 'examine') or (Tokens[CurrentToken] = 'x') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avExamine;
if (CanEnd()) then
Fail('What do you want to examine?');
if (not GetThing(Action.ExamineSubject, [], 'examine')) then
begin
// Action.ExamineSubject is invalid here but it doesn't matter because we immediately throw
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('That''s a direction, I don''t know how to examine it.')
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here to examine.');
end;
if (not CanEnd()) then
Fail('I don''t understand how to examine things "' + OriginalTokens[CurrentToken] + '".');
end
else
Result := False;
end;
function GetRead(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
begin
if (Tokens[CurrentToken] = 'read') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avRead;
if (CanEnd()) then
Fail('A fine notion, but what do you want to read?');
if (not GetThing(Action.ReadSubject, [], 'read')) then
begin
// Action.ReadSubject is invalid here but it doesn't matter because we immediately throw
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('That''s a direction, I don''t know how to read it.')
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here to read.');
end;
if (not CanEnd()) then
Fail('I don''t understand how to read things "' + OriginalTokens[CurrentToken] + '".');
end
else
Result := False;
end;
function GetFind(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
begin
if (Tokens[CurrentToken] = 'find') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avFind;
if (CanEnd()) then
Fail('What do you want to find?');
if (not GetThing(Action.FindSubject, [], 'find')) then
begin
// Action.FindSubject is invalid here but it doesn't matter because we immediately throw
if (TryDirection(Direction, [optAllowDefiniteArticle, optRequireCompassDirection])) then
begin
if (CanEnd()) then
Fail(Capitalise(CardinalDirectionToDefiniteString(Direction)) + ' is ' + CardinalDirectionToDirectionString(Direction) + ', obviously. I have a keen sense of direction that you can rely on.')
else
Fail('I don''t understand how to find things in a particular direction. It seems redundant.');
end
else
Fail('I can''t find anything like ' + IndefiniteArticle(Tokens[CurrentToken]) + ' "' + OriginalTokens[CurrentToken] + '" here.');
end
else
if (not CanEnd()) then
Fail('I don''t understand how to find things "' + OriginalTokens[CurrentToken] + '".');
end
else
Result := False;
end;
function GetGo(var Action: TAction): Boolean;
begin
if (Tokens[CurrentToken] = 'go') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avGo;
if (CanEnd()) then
Fail('Where do you want to go?');
EnsureNoPunctuation();
if (not TryDirection(Action.GoDirection, [])) then
Fail('I don''t understand how to go "' + OriginalTokens[CurrentToken] + '".');
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to go ' + CardinalDirectionToString(Action.GoDirection) + ' "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
Result := False;
end;
function GetEnter(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
Details: TSubjectiveInformation;
begin
if (Tokens[CurrentToken] = 'enter') then
begin
Inc(CurrentToken);
Result := True;
Action.Verb := avEnter;
if (CanEnd()) then
Fail('What do you want to enter?');
if (not GetThing(Action.EnterSubject, [], 'enter')) then
begin
// Action.EnterSubject is invalid here but it doesn't matter because we immediately throw
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('That''s a direction, I don''t know how to enter it.')
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here.');
end;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to enter things "' + OriginalTokens[CurrentToken] + '".');
end;
Details := Player.Locate(Action.EnterSubject);
Action.EnterRequiredAbilities := Details.RequiredAbilitiesToNavigate;
end
else
Result := False;
end;
function GetLeave(var Action: TAction): Boolean;
var
Verb: UTF8String;
begin
if ((Tokens[CurrentToken] = 'leave') or (Tokens[CurrentToken] = 'exit')) then
begin
Verb := Tokens[CurrentToken];
Inc(CurrentToken);
Result := True;
Action.Verb := avGo;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
if (not TryDirection(Action.GoDirection, [])) then
Fail('I don''t understand how to ' + Verb + ' "' + OriginalTokens[CurrentToken] + '".');
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to ' + Verb + ' ' + CardinalDirectionToString(Action.GoDirection) + ' "' + OriginalTokens[CurrentToken] + '".');
end;
end
else
Action.GoDirection := cdOut;
end
else
Result := False;
end;
function GetClimb(var Action: TAction): Boolean;
var
Details: TSubjectiveInformation;
begin
if (Tokens[CurrentToken] = 'climb') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
Action.Verb := avGo;
Action.GoDirection := cdUp;
Result := True;
end
else
if (GetPrepositionThing('onto', [], 'climb', Action.ClimbOnSubject) or
GetPrepositionThing('on', [], 'climb', Action.ClimbOnSubject) or
GetCompoundPrepositionThing(['on', 'to'], [], 'climb', 'onto', Action.ClimbOnSubject) or
GetThing(Action.ClimbOnSubject, [], 'climb')) then
begin
Action.Verb := avClimbOn;
if (not CanEnd()) then
Fail('I don''t understand how to climb onto things "' + OriginalTokens[CurrentToken] + '".');
Details := Player.Locate(Action.ClimbOnSubject);
Action.EnterRequiredAbilities := Details.RequiredAbilitiesToNavigate;
Result := True;
end
else
if (GetPrepositionThing('into', [], 'climb', Action.EnterSubject) or
GetPrepositionThing('in', [], 'climb', Action.EnterSubject) or
GetCompoundPrepositionThing(['in', 'to'], [], 'climb', 'into', Action.EnterSubject)) then
begin
Action.Verb := avEnter;
if (not CanEnd()) then
Fail('I don''t understand how to climb into things "' + OriginalTokens[CurrentToken] + '".');
Details := Player.Locate(Action.EnterSubject);
Action.EnterRequiredAbilities := Details.RequiredAbilitiesToNavigate;
Result := True;
end
else
begin
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here to climb.');
end;
end
else
Result := False;
end;
function GetTake(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
IsTake: Boolean = False;
Subject: TThing;
TransportationInstruction: TTransportationInstruction;
begin
if (Tokens[CurrentToken] = 'take') then
begin
Result := True;
IsTake := True;
Inc(CurrentToken);
end
else
if ((Tokens[CurrentToken] = 'get') or
(Tokens[CurrentToken] = 'grab') or
(Tokens[CurrentToken] = 'carry')) then
begin
Result := True;
Inc(CurrentToken);
end
else
if ((CurrentToken + 1 < Length(Tokens)) and (Tokens[CurrentToken] = 'pick') and (Tokens[CurrentToken+1] = 'up')) then
begin
Inc(CurrentToken, 2);
Result := True;
end
else
begin
Result := False;
end;
if (Result) then
begin
Action.Verb := avTake;
Action.TakeSubject := nil;
if (CanEnd()) then
Fail('What do you want to take?');
if (not GetThings(Action.TakeSubject, [aisSurroundings], [], 'take')) then
begin
// Action.TakeSubject is not valid here, but that doesn't matter as we immediately throw.
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail('That''s a direction, I don''t know how to take it.')
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here.');
end;
if (not CanEnd()) then
begin
EnsureNoPunctuation();
Fail('I don''t understand how to take things "' + OriginalTokens[CurrentToken] + '".');
end;
if (IsTake and (Action.TakeSubject.Length = 1)) then
begin
// check for "take taxi" or whatever
Subject := Action.TakeSubject.First;
TransportationInstruction := Subject.GetTransportationDestination(Player);
if (TransportationInstruction.TravelType <> ttNone) then
begin
Action.TakeSubject.Free();
Action.Verb := avUseTransportation;
Action.UseTransportationInstruction := TransportationInstruction;
end;
end;
end;
end;
function GetPut(var Action: TAction): Boolean;
var
Direction: TCardinalDirection;
Verb, WhatQ, DirectionQ: UTF8String;
begin
if (Tokens[CurrentToken] = 'put') then
begin
Result := True;
Action.PutCare := psCarefully;
Verb := 'put';
WhatQ := 'What do you want to put somewhere?';
DirectionQ := 'That''s a direction; it is where it is.';
end
else if (Tokens[CurrentToken] = 'place') then
begin
Result := True;
Action.PutCare := psCarefully;
Verb := 'place';
WhatQ := 'What do you want to carefully place somewhere?';
DirectionQ := 'You can''t place a direction.';
end
else if (Tokens[CurrentToken] = 'drop') then
begin
Result := True;
Action.PutCare := psRoughly;
Verb := 'drop';
WhatQ := 'What do you want to carelessly drop?';
DirectionQ := 'What do you want to drop there?';
end
else
Result := False;
if (Result) then
begin
Inc(CurrentToken);
Action.Verb := avPut;
Action.PutSubject := nil;
if (CanEnd()) then
Fail(WhatQ);
if (not GetThings(Action.PutSubject, [aisSelf], [eckIn, eckOn], Verb, 'You have nothing to ' + Verb + ' anywhere.')) then
begin
// Action.PutSubject is not valid here, but that doesn't matter as we immediately throw.
if (TryDirection(Direction, [optAllowDefiniteArticle])) then
Fail(DirectionQ)
else
Fail('I can''t see any "' + OriginalTokens[CurrentToken] + '" here.');
end;
if (CanEnd()) then
begin
if (Player.Position in tpContained) then
begin
Action.PutPosition := tpIn;
end
else
begin
Action.PutPosition := tpOn;
end;
Action.PutTarget := Player.Parent;
if (Action.PutCare = psCarefully) then
Player.AutoDisambiguated(ThingPositionToString(Action.PutPosition) + ' ' + Action.PutTarget.GetDefiniteName(Player));
end
else
if (GetPrepositionThing('onto', [], Verb + ' things', Action.PutTarget) or
GetCompoundPrepositionThing(['on', 'to'], [], Verb + ' things', 'on', Action.PutTarget) or
GetPrepositionThing('on', [], Verb + ' things', Action.PutTarget)) then
begin
Action.PutPosition := tpOn;
if (not CanEnd()) then
Fail('I don''t understand how to ' + Verb + ' things on other things "' + OriginalTokens[CurrentToken] + '".');
end
else
if (GetPrepositionThing('into', [], Verb + ' things', Action.PutTarget) or
GetCompoundPrepositionThing(['in', 'to'], [], Verb + ' things', 'in', Action.PutTarget) or
GetPrepositionThing('in', [], Verb + ' things', Action.PutTarget) or
GetPrepositionThing('inside', [], Verb + ' things', Action.PutTarget)) then
begin
Action.PutPosition := tpIn;
if (not CanEnd()) then
Fail('I don''t understand how to ' + Verb + ' things in other things "' + OriginalTokens[CurrentToken] + '".');
end
else
begin
Fail('I don''t understand how to ' + Verb + ' things "' + OriginalTokens[CurrentToken] + '".');
end;
Assert(Assigned(Action.PutTarget));
end;
end;
function GetMove(var Action: TAction): Boolean;
var
Details: TSubjectiveInformation;
begin
if (Tokens[CurrentToken] = 'move') then
begin
Inc(CurrentToken);
if (CanEnd()) then
begin
// XXX pick a random direction and imply it
Fail('Move in what direction?');
end;
if (TryDirection(Action.GoDirection, []) or
TryPrepositionDirection('to', Action.GoDirection) or
TryPrepositionDirection('towards', Action.GoDirection)) then