-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.pas
2069 lines (1960 loc) · 75 KB
/
player.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
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit player;
interface
uses
storable, physics, grammarian, messages, lists, thingdim, thingseeker, typinfo;
const
MaxCarrySingleMass = tmPonderous; { inclusive }
MaxCarryMass: TThingMassManifest = (0, 0, 2, 0); { exclusive }
MaxCarrySize = tsGigantic; { exclusive }
MaxPushMass = tmLudicrous; { exclusive }
MaxPushSize = tsLudicrous; { exclusive }
MaxShakeMass: TThingMassManifest = (0, 2, 0, 0); { exclusive }
MaxShakeSize = tsGigantic; { exclusive }
MaxCarryCount = 10; { inclusive }
MaxBalanceCount = 1; { inclusive }
type
TPronouns = (pHe, pShe, pSingularThey, pPluralThey, pIt, pZe);
TTalkVolume = (tvWhispering, tvSpeaking, tvShouting);
TActionVerb = (avNone,
avLook, avLookDirectional, avLookAt, avExamine, avRead, avLookUnder, avLookIn, avInventory, avFind,
avGo, avEnter, avClimbOn, avUseTransportation,
avTake, avPut, avRelocate, avMove, avPush, avRemove, avPress, avShake,
avDig, avDigDirection,
avOpen, avClose,
avTalk, avDance,
{$IFDEF DEBUG} avDebugStatus, avDebugLocation, avDebugLocations, avDebugThings, avDebugThing, avDebugTeleport,
avDebugMake, avDebugConnect, avDebugListClasses, avDebugDescribeClass, avDebugDescribeEnum, {$ENDIF}
avPronouns, avHelp, avQuit);
// When adding allocated objects to this record, remember to free them in TWorld.Perform (world.pas).
TAction = record
case Verb: TActionVerb of
avNone: ();
avLook: ();
avLookDirectional: (LookDirection: TCardinalDirection);
avLookAt: (LookAtSubject: TThing);
avExamine: (ExamineSubject: TThing);
avRead: (ReadSubject: TThing);
avLookUnder: (LookUnder: TThing);
avLookIn: (LookIn: TThing);
avInventory: ();
avFind: (FindSubject: TThing);
avGo: (GoDirection: TCardinalDirection);
avEnter: (EnterSubject: TThing; EnterRequiredAbilities: TNavigationAbilities);
avClimbOn: (ClimbOnSubject: TThing; ClimbOnRequiredAbilities: TNavigationAbilities);
avUseTransportation: (UseTransportationInstruction: TTransportationInstruction);
avTake: (TakeSubject: TThingList);
avPut: (PutSubject: TThingList; PutTarget: TAtom; PutPosition: TThingPosition; PutCare: TPlacementStyle);
avRelocate: (RelocateSubject: TThingList);
avMove: (MoveSubject: TThingList; MoveTarget: TThing; MovePosition: TThingPosition); // MovePosition should be tpOn, tpIn, or, if ambiguous, tpAt
avPush: (PushSubject: TThingList; PushDirection: TCardinalDirection);
avRemove: (RemoveSubject: TThingList; RemoveFromPosition: TThingPosition; RemoveFromObject: TThing);
avPress: (PressSubject: TThingList);
avShake: (ShakeSubject: TThingList);
avDig {and avDigDirection}: (DigSpade: TThing; case TActionVerb of avDig: (DigTarget: TThing); avDigDirection: (DigDirection: TCardinalDirection));
avOpen: (OpenTarget: TThing);
avClose: (CloseTarget: TThing);
avTalk: (TalkTarget: TThing; TalkMessage: PUTF8String; TalkVolume: TTalkVolume);
avDance: ();
avPronouns: (Pronouns: TPronouns);
{$IFDEF DEBUG}
avDebugStatus: ();
avDebugLocations: ();
avDebugLocation: ();
avDebugThings: (DebugThings: TThingList);
avDebugThing: (DebugThing: TThing);
avDebugTeleport: (DebugTarget: TAtom);
avDebugMake: (DebugMakeData: PUTF8String);
avDebugConnect: (DebugConnectDirection: TCardinalDirection; DebugConnectSource: TLocation; DebugConnectTarget: TAtom; DebugConnectOptions: TLandmarkOptions; DebugConnectBidirectional: Boolean);
avDebugListClasses: (DebugSuperclass: TClass);
avDebugDescribeClass: (DebugDescribeClass: TAtomClass);
avDebugDescribeEnum: (DebugDescribeEnumTypeInfo: PTypeInfo);
{$ENDIF}
avHelp: ();
avQuit: ();
end;
TMessageEvent = procedure (Message: UTF8String) of object;
TForceDisconnectEvent = procedure () of object;
TPlayer = class(TAvatar) // @RegisterStorableClass
protected
FName, FPassword: UTF8String;
FPronouns: TPronouns;
FOnMessage: TMessageEvent; { transient }
FOnForceDisconnect: TForceDisconnectEvent; { transient }
FContext: UTF8String; { transient }
function CanCarryThing(Thing: TThing; var Message: TMessage): Boolean;
function CanPushThing(Thing: TThing; var Message: TMessage): Boolean;
function CanShakeThing(Thing: TThing; var Message: TMessage): Boolean;
function CanMoveWithoutLoops(Subject: TThing; Target: TAtom; ThingPosition: TThingPosition; var Message: TMessage): Boolean;
function IsNotUnderUs(Subject: TThing; var Message: TMessage): Boolean;
procedure DoPutInternal(CurrentSubject: TThing; Target: TAtom; ThingPosition: TThingPosition; Care: TPlacementStyle);
procedure DoPushInternal(CurrentSubject: TThing; Destination: TAtom; ThingPosition: TThingPosition; Care: TPlacementStyle; DestinationMustBeReachable: Boolean; NotificationList: TAtomList);
procedure SetContext(Context: UTF8String);
procedure ResetContext();
public
constructor Create(AName: UTF8String; APassword: UTF8String; APronouns: TPronouns);
destructor Destroy(); override;
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
procedure DoLook(); override;
procedure DoInventory();
procedure AvatarMessage(Message: TMessage); override;
procedure SendRawMessage(Message: UTF8String);
procedure SendMessage(Message: UTF8String);
procedure AutoDisambiguated(Message: UTF8String); override;
function GetImpliedThing(Scope: TAllImpliedScope; FeatureFilter: TThingFeatures): TThing;
function HasAbilityToTravelTo(Destination: TAtom; RequiredAbilities: TNavigationAbilities; Perspective: TAvatar; var Message: TMessage): Boolean; override;
function GetIntrinsicMass(): TThingMass; override;
function GetIntrinsicSize(): TThingSize; override;
function GetName(Perspective: TAvatar): UTF8String; override;
function GetLongName(Perspective: TAvatar): UTF8String; override;
function GetIndefiniteName(Perspective: TAvatar): UTF8String; override;
function GetDefiniteName(Perspective: TAvatar): UTF8String; override;
function GetLongDefiniteName(Perspective: TAvatar): UTF8String; override;
function GetSubjectPronoun(Perspective: TAvatar): UTF8String; override; // I
function GetObjectPronoun(Perspective: TAvatar): UTF8String; override; // me
function GetReflexivePronoun(Perspective: TAvatar): UTF8String; override; // myself
function GetPossessivePronoun(Perspective: TAvatar): UTF8String; override; // mine
function GetPossessiveAdjective(Perspective: TAvatar): UTF8String; override; // my
function IsPlural(Perspective: TAvatar): Boolean; override;
function GetPresenceStatement(Perspective: TAvatar; Mode: TGetPresenceStatementMode): UTF8String; override;
function GetDescriptionSelf(Perspective: TAvatar): UTF8String; override;
procedure AnnounceAppearance(); override;
procedure AnnounceDisappearance(); override;
procedure AnnounceDeparture(Destination: TAtom; Direction: TCardinalDirection); override;
procedure AnnounceDeparture(Destination: TAtom); override; {BOGUS Hint: Value parameter "Destination" is assigned but never used}
procedure AnnounceArrival(Source: TAtom; Direction: TCardinalDirection); override; {BOGUS Hint: Value parameter "Source" is assigned but never used}
procedure AnnounceArrival(Source: TAtom); override; {BOGUS Hint: Value parameter "Source" is assigned but never used}
function CanSurfaceHold(const Manifest: TThingSizeManifest; const ManifestCount: Integer): Boolean; override;
procedure HandleAdd(Thing: TThing; Blame: TAvatar); override;
function HasConnectedPlayer(): Boolean; override;
function IsReadyForRemoval(): Boolean; override;
procedure RemoveFromWorld(); override;
function IsExplicitlyReferencedThing(Tokens: TTokens; Start: Cardinal; Perspective: TAvatar; out Count: Cardinal; out GrammaticalNumber: TGrammaticalNumber): Boolean; override;
function GetUsername(): UTF8String;
function GetPassword(): UTF8String;
procedure DoFind(Subject: TThing);
procedure DoLookUnder(Subject: TThing);
procedure DoNavigation(Target: TAtom; ThingPosition: TThingPosition; RequiredAbilities: TNavigationAbilities);
procedure DoNavigation(Direction: TCardinalDirection);
procedure DoNavigation(Instruction: TTransportationInstruction);
procedure DoTake(Subject: TThingList);
procedure DoPut(Subject: TThingList; Target: TAtom; ThingPosition: TThingPosition; Care: TPlacementStyle);
procedure DoRelocate(Subject: TThingList);
procedure DoMove(Subject: TThingList; Target: TThing; ThingPosition: TThingPosition);
procedure DoPushDirectional(Subject: TThingList; Direction: TCardinalDirection);
procedure DoRemove(Subject: TThingList; RequiredPosition: TThingPosition; RequiredParent: TThing);
procedure DoPress(Subject: TThingList);
procedure DoShake(Subject: TThingList);
procedure DoDig(Target: TThing; Spade: TThing);
procedure DoDig(Direction: TCardinalDirection; Spade: TThing);
procedure DoOpen(Subject: TThing);
procedure DoClose(Subject: TThing);
procedure DoTalk(Target: TThing; Message: UTF8String; Volume: TTalkVolume);
procedure DoDance();
procedure DoSetPronouns(Pronouns: TPronouns);
{$IFDEF DEBUG}
procedure DoDebugStatus();
procedure DoDebugLocation();
procedure DoDebugThings(Things: TThingList);
procedure DoDebugThing(Thing: TThing);
procedure DoDebugTeleport(Target: TAtom);
procedure DoDebugListClasses(Superclass: TClass);
function DebugGetCurrentLocation(): TLocation; // used by parser.inc
{$ENDIF}
procedure Adopt(AOnMessage: TMessageEvent; AOnForceDisconnect: TForceDisconnectEvent);
procedure Abandon();
property Name: UTF8String read FName;
property Pronouns: TPronouns read FPronouns write FPronouns;
end;
TPlayerList = specialize TStorableList<TPlayer>; // @RegisterStorableClass
{$IFDEF DEBUG}
type
TStatusReportProc = procedure (Perspective: TPlayer) of object;
var
StatusReport: TStatusReportProc = nil;
{$ENDIF}
implementation
uses
sysutils, exceptions, broadcast, things;
constructor TPlayer.Create(AName: UTF8String; APassword: UTF8String; APronouns: TPronouns);
var
Bag: TBag;
begin
inherited Create();
FName := AName;
FPassword := APassword;
FPronouns := APronouns;
Bag := TBag.Create('bag of holding', '(embroidered (bag/bags (of holding)?) (labeled ' + Capitalise(AName) + '))&', 'The bag has the name "' + Capitalise(AName) + '" embroidered around its rim.', tsLudicrous);
Bag.Add(TFeature.Create('rim', '(rim/rims (bag? rim/rims))@', 'Around the bag''s rim is embroidered the name "' + Capitalise(AName) + '".'), tpAmbiguousPartOfImplicit); { the weird pattern is to avoid putting "bag" in the canonical description, as in, "the bag rim of the bag of holding" }
Add(Bag, tpCarried);
end;
destructor TPlayer.Destroy();
begin
if (Assigned(FOnForceDisconnect)) then
FOnForceDisconnect();
inherited;
end;
constructor TPlayer.Read(Stream: TReadStream);
begin
inherited;
FName := Stream.ReadString();
FPassword := Stream.ReadString();
FPronouns := TPronouns(Stream.ReadCardinal());
end;
procedure TPlayer.Write(Stream: TWriteStream);
begin
inherited;
Stream.WriteString(FName);
Stream.WriteString(FPassword);
Stream.WriteCardinal(Cardinal(FPronouns));
end;
procedure TPlayer.DoLook();
begin
Assert((not (FPosition in tpContained)) or (FParent.GetRepresentative() = FParent)); // otherwise we'd look outside our parent
Assert(FPosition in tpPlayerPositions);
SendMessage(FParent.GetRepresentative().GetLook(Self));
end;
procedure TPlayer.DoInventory();
var
Contents: UTF8String;
begin
Contents := GetInventory(Self);
if (Length(Contents) = 0) then
Contents := 'You are not carrying anything.';
SendMessage(Contents);
end;
{$IFDEF DEBUG}
procedure TPlayer.DoDebugStatus();
begin
SendMessage('Debug build.');
if (Assigned(StatusReport)) then
StatusReport(Self);
end;
function TPlayer.DebugGetCurrentLocation(): TLocation;
var
Location: TAtom;
begin
Location := Self;
while (Assigned(Location) and (Location is TThing)) do
Location := (Location as TThing).Parent;
if (not Assigned(Location)) then
Fail('Player is in an orphan TThing tree!');
if (not (Location is TLocation)) then
Fail('Player is in a TThing tree that is not rooted by a TLocation!');
Result := TLocation(Location);
end;
procedure TPlayer.DoDebugLocation();
begin
SendMessage(DebugGetCurrentLocation().Debug(Self));
end;
procedure TPlayer.DoDebugThings(Things: TThingList);
var
Thing: TThing;
Collect, FromOutside: Boolean;
Root: TAtom;
FindMatchingThingsOptions: TFindMatchingThingsOptions;
begin
Collect := not Assigned(Things);
try
if (Collect) then
begin
Root := GetSurroundingsRoot(FromOutside);
Things := TThingList.Create();
FindMatchingThingsOptions := [fomIncludePerspectiveChildren, fomIncludeNonImplicits];
if (FromOutside) then
Include(FindMatchingThingsOptions, fomFromOutside);
Root.FindMatchingThings(Self, FindMatchingThingsOptions, tpEverything, [], Things);
end;
Assert(Things.Length > 0); { there's always at least one thing: us }
SendMessage('Things:');
for Thing in Things do
SendMessage(' - ' + Thing.GetName(Self) + ': ' + Thing.GetLongDefiniteName(Self) + WithSpaceIfNotEmpty(ParentheticallyIfNotEmpty(Thing.GetPresenceStatement(Self, psOnThatSpecialThing))));
finally
if (Collect) then
Things.Free();
end;
end;
procedure TPlayer.DoDebugThing(Thing: TThing);
begin
SendMessage(Thing.Debug(Self));
end;
procedure TPlayer.DoDebugTeleport(Target: TAtom);
var
Ancestor: TAtom;
begin
Ancestor := Target;
while ((Ancestor <> Self) and (Ancestor is TThing)) do
Ancestor := (Ancestor as TThing).Parent;
if (Ancestor = Self) then
begin
SendMessage('Can''t teleport an actor onto the actor itself or any of its descendants.');
end
else
begin
AnnounceDisappearance();
Target.Add(Self, tpOn);
AnnounceAppearance();
DoLook();
end;
end;
procedure TPlayer.DoDebugListClasses(Superclass: TClass);
var
RegisteredClassName: RawByteString;
begin
SendMessage('The following classes are known:');
for RegisteredClassName in GetRegisteredClasses(Superclass) do // $R-
SendMessage(' - ' + RegisteredClassName);
end;
{$ENDIF}
procedure TPlayer.SetContext(Context: UTF8String);
begin
FContext := Context;
end;
procedure TPlayer.ResetContext();
begin
FContext := '';
end;
procedure TPlayer.AvatarMessage(Message: TMessage);
begin
SendMessage(Message.AsText);
end;
procedure TPlayer.SendRawMessage(Message: UTF8String);
begin
if (Assigned(FOnMessage)) then
FOnMessage(Message);
end;
procedure TPlayer.SendMessage(Message: UTF8String);
begin
if (Assigned(FOnMessage)) then
begin
if (FContext <> '') then
begin
if (Pos(#10, Message) > 0) then
Message := FContext + ':' + #10 + Message
else
Message := FContext + ': ' + Message;
end;
FOnMessage(Message);
end;
end;
procedure TPlayer.AutoDisambiguated(Message: UTF8String);
begin
SendMessage('(' + Message + ')');
end;
function TPlayer.HasAbilityToTravelTo(Destination: TAtom; RequiredAbilities: TNavigationAbilities; Perspective: TAvatar; var Message: TMessage): Boolean;
begin
Assert(Message.IsValid);
if ((RequiredAbilities - [naWalk, naJump]) <> []) then
begin
Assert((RequiredAbilities - [naFly, naDebugTeleport]) = []);
if (naFly in RequiredAbilities) then
begin
Result := False;
Message := TMessage.Create(mkCannotFly, '_ cannot fly.', [Capitalise(GetDefiniteName(Perspective))]);
end
else
if (RequiredAbilities - [naDebugTeleport] = []) then
begin
Result := False;
Message := TMessage.Create(mkNotReachable, '_ cannot reach _.', [Capitalise(GetDefiniteName(Perspective)), Destination.GetDefiniteName(Perspective)]);
end
else
begin
Result := inherited;
end;
end
else
begin
Result := True;
end;
end;
function TPlayer.GetIntrinsicMass(): TThingMass;
begin
Result := tmPonderous;
end;
function TPlayer.GetIntrinsicSize(): TThingSize;
begin
Result := tsBig;
end;
function TPlayer.GetName(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'you'
else
Result := Capitalise(FName);
end;
function TPlayer.GetLongName(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'you'
else
Result := 'other player named ' + GetName(Perspective);
end;
function TPlayer.GetIndefiniteName(Perspective: TAvatar): UTF8String;
begin
Result := GetName(Perspective);
end;
function TPlayer.GetDefiniteName(Perspective: TAvatar): UTF8String;
begin
Result := GetName(Perspective);
end;
function TPlayer.GetLongDefiniteName(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'you'
else
Result := 'the other player named ' + GetDefiniteName(Perspective);
end;
function TPlayer.GetSubjectPronoun(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'you'
else
case FPronouns of
pHe: Result := 'he';
pShe: Result := 'she';
pSingularThey: Result := 'they';
pPluralThey: Result := 'they';
pIt: Result := 'it';
pZe: Result := 'ze';
end;
end;
function TPlayer.GetObjectPronoun(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'you'
else
case FPronouns of
pHe: Result := 'him';
pShe: Result := 'her';
pSingularThey: Result := 'them';
pPluralThey: Result := 'them';
pIt: Result := 'it';
pZe: Result := 'zer';
end;
end;
function TPlayer.GetReflexivePronoun(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'yourself'
else
case FPronouns of
pHe: Result := 'himself';
pShe: Result := 'herself';
pSingularThey: Result := 'themself';
pPluralThey: Result := 'themselves';
pIt: Result := 'itself';
pZe: Result := 'zerself';
end;
end;
function TPlayer.GetPossessivePronoun(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'yours'
else
case FPronouns of
pHe: Result := 'his';
pShe: Result := 'hers';
pSingularThey: Result := 'theirs';
pPluralThey: Result := 'theirs';
pIt: Result := 'its';
pZe: Result := 'zirs';
end;
end;
function TPlayer.GetPossessiveAdjective(Perspective: TAvatar): UTF8String;
begin
if (Perspective = Self) then
Result := 'your'
else
case FPronouns of
pHe: Result := 'his';
pShe: Result := 'her';
pSingularThey: Result := 'their';
pPluralThey: Result := 'their';
pIt: Result := 'its';
pZe: Result := 'zer';
end;
end;
function TPlayer.IsPlural(Perspective: TAvatar): Boolean;
begin
if (Perspective = Self) then
Result := True
else
Result := FPronouns in [pPluralThey];
end;
function TPlayer.GetPresenceStatement(Perspective: TAvatar; Mode: TGetPresenceStatementMode): UTF8String;
begin
Assert(FPosition in tpPlayerPositions);
if (Mode = psThereIsAThingHere) then
Result := Capitalise(GetDefiniteName(Perspective)) + ' ' + IsAre(IsPlural(Perspective)) + ' here.'
else
if (Mode = psThereIsAThingThere) then
Result := Capitalise(GetDefiniteName(Perspective)) + ' ' + IsAre(IsPlural(Perspective)) + ' there.'
else
if ((Mode = psOnThatThingIsAThing) or (Mode = psTheThingIsOnThatThing)) then
Result := Capitalise(GetDefiniteName(Perspective)) + ' ' + IsAre(IsPlural(Perspective)) + ' ' +
ThingPositionToString(FPosition) + ' ' + FParent.GetLongDefiniteName(Perspective) + '.'
else
Result := inherited;
end;
function TPlayer.GetDescriptionSelf(Perspective: TAvatar): UTF8String;
begin
Result := Capitalise(GetDefiniteName(Perspective)) + ' ' + TernaryConditional('is', 'are', IsPlural(Perspective)) + ' a player.';
if (not HasConnectedPlayer) then
begin
Result := Result + ' ' + Capitalise(GetPossessiveAdjective(Perspective)) + ' eyes look into the distance, as if ' + GetSubjectPronoun(Perspective) + ' ' + TernaryConditional('isn''t', 'aren''t', IsPlural(Perspective)) + ' really here.';
end;
end;
procedure TPlayer.AnnounceAppearance();
begin
DoBroadcast([Self], Self, [C(M(@GetIndefiniteName)), SP, MP(Self, M('appears.'), M('appear.'))]);
end;
procedure TPlayer.AnnounceDisappearance();
begin
DoBroadcast([Self], Self, [C(M(@GetIndefiniteName)), SP, MP(Self, M('disappears.'), M('disappear.'))]);
end;
procedure TPlayer.AnnounceDeparture(Destination: TAtom; Direction: TCardinalDirection);
begin
if (Destination is TLocation) then
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP, MP(Self, M('goes'), M('go')), SP, M(CardinalDirectionToString(Direction) + '.')])
else
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP, MP(Self, M('enters'), M('enter')), SP, M(@Destination.GetDefiniteName), M('.')]);
end;
procedure TPlayer.AnnounceDeparture(Destination: TAtom);
begin
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP, MP(Self, M('enters'), M('enter')), SP, M(@Destination.GetDefiniteName), M('.')]);
end;
procedure TPlayer.AnnounceArrival(Source: TAtom; Direction: TCardinalDirection);
begin
// XXX this relies on the rooms being symmetric
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP, MP(Self, M('arrives'), M('arrive')), M(' from '), M(@Source.GetDefiniteName), SP, M(CardinalDirectionToDirectionString(Direction)), M('.')]);
end;
procedure TPlayer.AnnounceArrival(Source: TAtom);
begin
// XXX could be more intelligent by querying the current location
// e.g. "enters from" when the current location has an exit and "arrives from" when it doesn't
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP, MP(Self, M('arrives'), M('arrive')), M(' from '), M(@Source.GetDefiniteName), M('.')]);
end;
procedure TPlayer.DoFind(Subject: TThing);
var
SubjectiveInformation: TSubjectiveInformation;
Message, ExtraMessage: UTF8String;
UseCommas: Boolean;
Count, Index: Cardinal;
Direction: TCardinalDirection;
begin
SubjectiveInformation := Locate(Subject as TThing);
if (SubjectiveInformation.Directions <> []) then
begin
Message := Capitalise(Subject.GetDefiniteName(Self)) + ' ' + IsAre(Subject.IsPlural(Self)) + ' ';
Count := 0;
for Direction := Low(SubjectiveInformation.Directions) to High(SubjectiveInformation.Directions) do
if (Direction in SubjectiveInformation.Directions) then
Inc(Count);
Assert(Count > 0);
UseCommas := Count > 2;
Index := 0;
for Direction := Low(SubjectiveInformation.Directions) to High(SubjectiveInformation.Directions) do
begin
if (Direction in SubjectiveInformation.Directions) then
begin
Inc(Index);
if (Index > 1) then
begin
if (UseCommas) then
Message := Message + ',';
if (Count = Index) then
Message := Message + ' and';
Message := Message + ' ';
end;
Message := Message + CardinalDirectionToDirectionString(Direction);
end;
end;
ExtraMessage := Subject.GetPresenceStatement(Self, psOnThatSpecialThing);
if (ExtraMessage <> '') then
Message := Message + ', ' + ExtraMessage;
Message := Message + '.';
SendMessage(Message);
end
else
SendMessage(Subject.GetPresenceStatement(Self, psTheThingIsOnThatThing));
end;
procedure TPlayer.DoLookUnder(Subject: TThing);
var
SubjectiveInformation: TSubjectiveInformation;
begin
SubjectiveInformation := Locate(Subject);
if (SubjectiveInformation.Directions = [cdUp]) then
begin
SendMessage('You are under ' + Subject.GetDefiniteName(Self) + '.');
end
else
SendMessage(Subject.GetLookUnder(Self));
end;
procedure TPlayer.DoNavigation(Target: TAtom; ThingPosition: TThingPosition; RequiredAbilities: TNavigationAbilities);
var
Message: TMessage;
begin
Assert(ThingPosition in [tpIn, tpOn], 'DoNavigation called with a ThingPosition that isn''t supported by ForceTravel');
Message := TMessage.Create();
if (not CanReach(Target, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
Assert(Message.AsText <> '');
AvatarMessage(Message);
end
else
begin
Assert(Message.AsKind = mkSuccess);
Assert(Message.AsText = '');
if (not HasAbilityToTravelTo(Target, RequiredAbilities, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
Assert(Message.AsText <> '');
AvatarMessage(Message);
end
else
begin
Assert(Message.AsKind = mkSuccess);
Assert(Message.AsText = '');
ForceTravel(Self, Target, ThingPosition, Self);
end;
end;
end;
procedure TPlayer.DoNavigation(Direction: TCardinalDirection);
var
Message: TMessage;
Instructions: TNavigationInstruction;
begin
Message := TMessage.Create();
Instructions := FParent.GetNavigationInstructions(Direction, Self, Self, Message);
if (Instructions.TravelType = ttNone) then
begin
Assert(Message.AsKind <> mkSuccess);
Assert(Message.AsText <> '');
AvatarMessage(Message);
end
else
begin
Assert(Message.AsKind = mkSuccess);
Assert(Message.AsText = '');
Assert(Instructions.DirectionTarget = Instructions.PositionTarget);
if (not HasAbilityToTravelTo(Instructions.DirectionTarget, Instructions.RequiredAbilities, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
Assert(Message.AsText <> '');
AvatarMessage(Message);
end
else
begin
Assert(Message.AsKind = mkSuccess);
Assert(Message.AsText = '');
case Instructions.TravelType of
ttByPosition: ForceTravel(Self, Instructions.PositionTarget, Instructions.Position, Self);
ttByDirection: ForceTravel(Self, Instructions.DirectionTarget, Instructions.Direction, Self);
else
Assert(False);
end;
end;
end;
end;
procedure TPlayer.DoNavigation(Instruction: TTransportationInstruction);
begin
case Instruction.TravelType of
ttNone: Assert(False);
ttByPosition: DoNavigation(Instruction.TargetThing, Instruction.Position, Instruction.RequiredAbilities);
ttByDirection: DoNavigation(Instruction.Direction);
end;
end;
procedure TPlayer.DoTake(Subject: TThingList);
var
Multiple: Boolean;
Success: Boolean;
Message: TMessage;
TargetSurface: TAtom;
CurrentSubject: TThing;
begin
Assert(Assigned(Subject));
Assert(Subject.Length > 0);
Multiple := Subject.Length > 1;
try
for CurrentSubject in Subject do
begin
if (Multiple) then
SetContext(Capitalise(CurrentSubject.GetName(Self)));
Message := TMessage.Create();
if (not CanReach(CurrentSubject, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
SendMessage(Message.AsText);
end
else
if (CurrentSubject.Parent = Self) then
begin
SendMessage('You already have that.');
end
else
if (CurrentSubject = Self) then
begin
TargetSurface := FParent.GetSurface();
if (Assigned(TargetSurface)) then
begin
DoBroadcast([Self], Self, [C(M(@GetDefiniteName)), SP,
MP(Self, M('tries to'), M('try to')), SP,
M('pick'), SP,
M(@GetReflexivePronoun), SP,
M('up, but'), SP,
MP(Self, M('ends up'), M('end up')), SP,
M(ThingPositionToString(Position)), SP,
M(@TargetSurface.GetDefiniteName), M('.')]);
SendMessage('You try to pick yourself up but end up ' + ThingPositionToString(Position) + ' ' + TargetSurface.GetDefiniteName(Self) + '.');
end
else
SendMessage('How can one pick oneself up?');
end
else
begin
Message := TMessage.Create(mkSuccess, 'Taken.');
Success := IsNotUnderUs(CurrentSubject, Message) and
CanCarryThing(CurrentSubject, Message);
Assert((Message.AsKind = mkSuccess) = Success);
SendMessage(Message.AsText);
if (Success) then
Self.Put(CurrentSubject, tpCarried, psCarefully, Self);
end;
end;
finally
if (Multiple) then
ResetContext();
end;
end;
procedure TPlayer.DoPut(Subject: TThingList; Target: TAtom; ThingPosition: TThingPosition; Care: TPlacementStyle);
var
Multiple: Boolean;
CurrentSubject: TThing;
begin
Assert(Assigned(Subject));
Assert(Subject.Length > 0);
Assert(Assigned(Target));
Assert((ThingPosition = tpIn) or (ThingPosition = tpOn));
Multiple := Subject.Length > 1;
try
for CurrentSubject in Subject do
begin
if (Multiple) then
SetContext(Capitalise(CurrentSubject.GetName(Self)));
DoPutInternal(CurrentSubject, Target, ThingPosition, Care);
end;
finally
if (Multiple) then
ResetContext();
end;
end;
procedure TPlayer.DoPutInternal(CurrentSubject: TThing; Target: TAtom; ThingPosition: TThingPosition; Care: TPlacementStyle);
var
Message: TMessage;
SurrogateTarget: TAtom;
SingleThingList: TThingList;
Success: Boolean;
MessageText: UTF8String;
begin
Message := TMessage.Create();
SurrogateTarget := Target.GetDefaultDestination(ThingPosition);
if (not CanMoveWithoutLoops(CurrentSubject, SurrogateTarget, ThingPosition, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
SendMessage(Message.AsText);
end
else
begin
if (CurrentSubject.Parent <> Self) then
begin
AutoDisambiguated('first taking ' + CurrentSubject.GetDefiniteName(Self));
SingleThingList := TThingList.Create();
try
SingleThingList.AppendItem(CurrentSubject);
DoTake(SingleThingList);
finally
SingleThingList.Free();
end;
end;
if (CurrentSubject.Parent = Self) then
begin
if (not CanReach(CurrentSubject, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
SendMessage(Message.AsText);
end
else
if (not CanReach(Target, Self, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
SendMessage(Message.AsText);
end
else
if ((Target = CurrentSubject) or (SurrogateTarget = CurrentSubject)) then
begin
SendMessage('You can''t move something ' + ThingPositionToDirectionString(ThingPosition) + ' itself, however hard you try.');
end
else
begin
Assert(CurrentSubject.Parent = Self);
Assert(Assigned(SurrogateTarget));
case (Care) of
psCarefully: MessageText := 'Placed';
psRoughly: MessageText := 'Dropped';
end;
if (SurrogateTarget <> FParent.GetSurface()) then
MessageText := MessageText + ' ' + ThingPositionToString(ThingPosition) + ' ' + SurrogateTarget.GetDefiniteName(Self);
MessageText := MessageText + '.';
Message := TMessage.Create(mkSuccess, MessageText);
Success := Target.CanPut(CurrentSubject, ThingPosition, Care, Self, Message) and
SurrogateTarget.CanPut(CurrentSubject, ThingPosition, Care, Self, Message);
Assert((Message.AsKind = mkSuccess) = Success);
SendMessage(Message.AsText);
if (Success) then
begin
SurrogateTarget.Put(CurrentSubject, ThingPosition, Care, Self);
end;
end;
end;
end;
end;
procedure TPlayer.DoRelocate(Subject: TThingList);
var
Multiple: Boolean;
SingleThingList: TThingList;
Ancestor, LocationSurface: TAtom;
CurrentSubject: TThing;
{$IFOPT C+} PreviousParent: TAtom; {$ENDIF}
Message: TMessage;
begin
Assert(Assigned(Subject));
Assert(Subject.Length > 0);
Multiple := Subject.Length > 1;
try
for CurrentSubject in Subject do
begin
if (Multiple) then
SetContext(Capitalise(CurrentSubject.GetName(Self)));
Message := TMessage.Create();
if (CurrentSubject = Self) then
begin
{$IFOPT C+} PreviousParent := FParent; {$ENDIF}
DoDance();
{$IFOPT C+} Assert(FParent = PreviousParent); {$ENDIF}
end
else
if (not CanReach(CurrentSubject, Self, Message) or
not IsNotUnderUs(CurrentSubject, Message)) then
begin
Assert(Message.AsKind <> mkSuccess);
SendMessage(Message.AsText);
end
else
if (CurrentSubject.Parent = Self) then
begin
SingleThingList := TThingList.Create();
try
SingleThingList.AppendItem(CurrentSubject);
DoShake(SingleThingList);
finally
SingleThingList.Free();
end;
end
else
begin
// Find the subject's location.
Ancestor := CurrentSubject.Parent;
while (Ancestor is TThing) do
Ancestor := (Ancestor as TThing).Parent;
Assert(Assigned(Ancestor));
// Find the location's surface.
LocationSurface := Ancestor.GetSurface(); // might be nil
// If the subject is directly in the location or at the surface, then just shake it.
Assert(Assigned(CurrentSubject.Parent));
if ((CurrentSubject.Parent = Ancestor) or
(CurrentSubject.Parent = LocationSurface) or
(not (CurrentSubject.Position in [tpOn, tpIn]))) then
begin
SingleThingList := TThingList.Create();
try
SingleThingList.AppendItem(CurrentSubject);
DoShake(SingleThingList);
finally
SingleThingList.Free();
end;
end
else
begin
SingleThingList := TThingList.Create();
try
SingleThingList.AppendItem(CurrentSubject);
/// xxxx replace with push (tpOn) or take (tpIn)
DoRemove(SingleThingList, CurrentSubject.Position, nil);
finally
SingleThingList.Free();
end;
end;
end;
end;
finally
if (Multiple) then
ResetContext();
end;
end;
procedure TPlayer.DoMove(Subject: TThingList; Target: TThing; ThingPosition: TThingPosition);
function CanBePushedTo(Thing: TThing; Surface: TAtom): Boolean; { Thing is being pushed onto Surface }
var
Ancestor: TAtom;
begin
if (Thing.Position <> tpOn) then
begin
{ There is no way you can push something onto something else if it's not already on something }
Result := False;
exit;
end;
{ can always slide onto something we're holding }
if ((Surface is TThing) and ((Surface as TThing).Parent = Self) and ((Surface as TThing).Position = tpCarried)) then
begin
Result := True;
exit;
end;
{ next see if we're trying to move the thing off something else onto it (i.e. pushing onto an ancestor) }
{ note we have to stop if we get to something that is not on something, e.g. you can't push from on a plate that is in a bag onto the table the bag is on }
{ nor can you push it from on the plate in the bag onto the bag itself }
Ancestor := Thing;
repeat
Ancestor := (Ancestor as TThing).Parent;
if ((Ancestor = Surface) or
((Surface is TThing) and ((Surface as TThing).Parent = Ancestor) and (tfCanHaveThingsPushedOn in (Surface as TThing).GetFeatures()))) then
begin
Result := True;
exit;