-
Notifications
You must be signed in to change notification settings - Fork 2
/
threshold.pas
1610 lines (1493 loc) · 65.7 KB
/
threshold.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 threshold;
interface
uses
locations, things, thingdim, grammarian, matcher, storable, physics, messages, textstream, properties;
type
TRelativePerspectivePosition = (rppFront, rppBack, rppHere);
TVisibleSide = (vsFront, vsBack);
TVisibleSides = set of TVisibleSide;
type
TThresholdThing = class abstract(TScenery)
protected
FFrontSideFacesDirection: TCardinalDirection;
function LocatePerspective(Perspective: TAvatar): TRelativePerspectivePosition;
class function CreateFromProperties(Properties: TTextStreamProperties): TThresholdThing; override;
public
constructor Create(Name: UTF8String; Pattern: UTF8String; Description: UTF8String; FrontFacesDirection: TCardinalDirection);
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
function CanTraverse(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar): Boolean; virtual;
property FrontSideFacesDirection: TCardinalDirection read FFrontSideFacesDirection write FFrontSideFacesDirection;
end;
TStaticThresholdThing = class(TThresholdThing) // @RegisterStorableClass
// This is for something that's always traversable, like an archway or something
// note that we inherit from a class that defines Openable, so IsOpen() might be true or false
// but that doesn't mean that when IsOpen() is false, we're not traversable
protected
FFrontSideDescription: UTF8String;
FBackSideDescription: UTF8String;
class function CreateFromProperties(Properties: TTextStreamProperties): TStaticThresholdThing; override;
public
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
function GetDescriptionSelf(Perspective: TAvatar): UTF8String; override;
property FrontSideDescription: UTF8String read FFrontSideDescription write FFrontSideDescription;
property BackSideDescription: UTF8String read FBackSideDescription write FBackSideDescription;
end;
TDoor = class;
TDoorWay = class(TThresholdThing) // @RegisterStorableClass
protected // open state is stored in inherited FOpened boolean
const tpConsiderForDoorPosition = tpIn; // if you try to use this, it'll turn into tpOfficialDoorPosition
const tpOfficialDoorPosition = tpInstalledIn; // this must have at most one TDoor that is tpOfficialDoorPosition at any one time
const tpOnGround = tpOn;
class function CreateFromProperties(Properties: TTextStreamProperties): TDoorWay; override;
function GetDoor(): TDoor; // or nil if there isn't one
function GetCouldBeDoor(Thing: TThing; ThingPosition: TThingPosition): Boolean;
procedure Removed(Thing: TThing); override;
public
constructor Create(Name: UTF8String; Pattern: UTF8String; Description: UTF8String; FrontFacesDirection: TCardinalDirection; Door: TDoor = nil);
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
function IsClear(): Boolean; virtual;
procedure EnumerateChildren(List: TThingList; const PositionFilter: TThingPositionFilter); override;
procedure ProxiedEnumerateExplicitlyReferencedThings(Tokens: TTokens; Start: Cardinal; Perspective: TAvatar; FromOutside, FromFarAway: Boolean; Directions: TCardinalDirectionSet; Reporter: TThingReporter); override;
procedure ProxiedFindMatchingThings(Perspective: TAvatar; Options: TFindMatchingThingsOptions; PositionFilter: TThingPositionFilter; PropertyFilter: TThingFeatures; List: TThingList); override;
function ProxiedFindThingTraverser(Thing: TThing; Perspective: TAvatar; Options: TFindThingOptions): Boolean; override;
function CanPut(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar; var Message: TMessage): Boolean; override;
procedure Put(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar); override;
procedure HandleAdd(Thing: TThing; Blame: TAvatar); override;
procedure HandlePassedThrough(Traveller: TThing; AFrom, ATo: TAtom; AToPosition: TThingPosition; Perspective: TAvatar); override;
function GetInside(var PositionOverride: TThingPosition): TThing; override;
function CanInsideHold(const Manifest: TThingSizeManifest; const ManifestCount: Integer): Boolean; override;
function GetDefaultDestination(var ThingPosition: TThingPosition): TThing; override;
function GetLookIn(Perspective: TAvatar): UTF8String; override;
function GetLookThrough(Perspective: TAvatar): UTF8String; virtual; // this gives the answer regardless of whether there's a door, it's open, or whatever
function GetLookUnder(Perspective: TAvatar): UTF8String; override;
function GetDescriptionRemoteBrief(Perspective: TAvatar; Mode: TGetPresenceStatementMode; Direction: TCardinalDirection): UTF8String; override;
function GetDescriptionRemoteDetailed(Perspective: TAvatar; Direction: TCardinalDirection; LeadingPhrase: UTF8String; Options: TLeadingPhraseOptions): UTF8String; override;
function GetDescriptionObstacles(Perspective: TAvatar; NoObstacleMessage: UTF8String = ''): UTF8String; virtual;
function GetDescriptionEmpty(Perspective: TAvatar): UTF8String; override;
function GetDescriptionClosed(Perspective: TAvatar): UTF8String; override; // defers to the door
function GetEntrance(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar; var PositionOverride: TThingPosition; var DisambiguationOpening: TThing; var Message: TMessage; NotificationList: TAtomList): TAtom; override;
function GetFeatures(): TThingFeatures; override;
function CanSeeIn(): Boolean; override;
function CanSeeThrough(): Boolean; virtual;
function Open(Perspective: TAvatar; var Message: TMessage): Boolean; override;
function Close(Perspective: TAvatar; var Message: TMessage): Boolean; override;
function CanTraverse(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar): Boolean; override;
function GetNavigationInstructions(Direction: TCardinalDirection; Child: TThing; Perspective: TAvatar; var Message: TMessage): TNavigationInstruction; override;
property Door: TDoor read GetDoor; // can be nil, if there's no door
end;
TDoorSide = class;
TDoor = class(TDescribedPhysicalThing) // @RegisterStorableClass
// The description (settable via the .Description property) will override
// using the sides when both sides are visible. This may be helpful if the
// sides have identical descriptions (in which case the normal behaviour
// of including both in the overall description is ugly).
protected
FFrontSide, FBackSide: TDoorSide;
class function CreateFromProperties(Properties: TTextStreamProperties): TDoor; override;
function GetDoorWay(): TDoorWay; // or nil if the door isn't in a doorway
// GetLock() could work a similar way
function IsChildTraversable(Child: TThing; Perspective: TAvatar; FromOutside: Boolean): Boolean; override;
function GetMatcherFlags(Perspective: TAvatar): TMatcherFlags; override;
function LocatePerspective(Perspective: TAvatar): TRelativePerspectivePosition;
function DetermineVisibleSides(Perspective: TAvatar): TVisibleSides;
public
const mfOpen: TMatcherFlag = 1;
const mfClosed: TMatcherFlag = 2;
constructor Create(Name: UTF8String; Pattern: UTF8String; FrontSide, BackSide: TDoorSide; AMass: TThingMass = tmHeavy; ASize: TThingSize = tsMassive);
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
function CanPut(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar; var Message: TMessage): Boolean; override;
procedure HandlePassedThrough(Traveller: TThing; AFrom, ATo: TAtom; AToPosition: TThingPosition; Perspective: TAvatar); override;
function GetDescriptionSelf(Perspective: TAvatar): UTF8String; override;
function GetLookUnder(Perspective: TAvatar): UTF8String; override;
function CanSeeUnder(Perspective: TAvatar): Boolean; virtual;
function GetCannotSeeUnder(Perspective: TAvatar): UTF8String; virtual;
function GetDescriptionClosed(Perspective: TAvatar): UTF8String; override;
function GetEntrance(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar; var PositionOverride: TThingPosition; var DisambiguationOpening: TThing; var Message: TMessage; NotificationList: TAtomList): TAtom; override;
function GetFeatures(): TThingFeatures; override;
function Open(Perspective: TAvatar; var Message: TMessage): Boolean; override;
function Close(Perspective: TAvatar; var Message: TMessage): Boolean; override;
property DoorWay: TDoorWay read GetDoorWay; // can be nil, if the door is not in a doorway
property Description: UTF8String read FDescription write FDescription;
end;
TDoorSide = class(TDescribedPhysicalThing) // @RegisterStorableClass
// description argument to constructor shouldn't have a capital first letter
// it gets concatenated to leading clauses like "On the front side, "...
protected
function GetMatcherFlags(Perspective: TAvatar): TMatcherFlags; override;
class function CreateFromProperties(Properties: TTextStreamProperties): TDoorSide; override;
public
constructor Create(Name: UTF8String; Pattern: UTF8String; Description: UTF8String; AMass: TThingMass = tmLight; ASize: TThingSize = tsSmall);
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
const mfOtherSideVisible: TMatcherFlag = 1;
function GetDescriptionSelf(Perspective: TAvatar): UTF8String; override;
function GetDescriptionSelfSentenceFragment(Perspective: TAvatar): UTF8String; virtual;
function GetRepresentative(): TAtom; override;
function GetSurface(): TThing; override;
function CanMove(Perspective: TAvatar; var Message: TMessage): Boolean; override;
function CanPut(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar; var Message: TMessage): Boolean; override;
end;
TThresholdSurface = class(TSurface) // @RegisterStorableClass
public
function CanPut(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar; var Message: TMessage): Boolean; override;
end;
TThresholdLocation = class(TSurfaceProxyLocation) // @RegisterStorableClass
protected
class function CreateFromProperties(Properties: TTextStreamProperties): TThresholdLocation; override;
public
constructor Create(PassageWay: TThing; Surface: TThing);
class procedure DescribeProperties(Describer: TPropertyDescriber); override;
function GetTitle(Perspective: TAvatar): UTF8String; override;
function GetLookTowardsDirectionDefault(Perspective: TAvatar; Direction: TCardinalDirection): UTF8String; override;
function GetDescriptionHere(Perspective: TAvatar; Mode: TGetPresenceStatementMode; Directions: TCardinalDirectionSet = cdAllDirections; Context: TAtom = nil): UTF8String; override;
function GetDescriptionRemoteBrief(Perspective: TAvatar; Mode: TGetPresenceStatementMode; Direction: TCardinalDirection): UTF8String; override;
function GetDescriptionRemoteDetailed(Perspective: TAvatar; Direction: TCardinalDirection; LeadingPhrase: UTF8String; Options: TLeadingPhraseOptions): UTF8String; override;
function GetContextFragment(Perspective: TAvatar; PertinentPosition: TThingPosition; Context: TAtom = nil): UTF8String; override;
procedure GetNearbyThingsByClass(List: TThingList; FromOutside: Boolean; Filter: TThingClass); override;
procedure EnumerateExplicitlyReferencedThings(Tokens: TTokens; Start: Cardinal; Perspective: TAvatar; FromOutside, FromFarAway: Boolean; Directions: TCardinalDirectionSet; Reporter: TThingReporter); override;
function GetEntrance(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar; var PositionOverride: TThingPosition; var DisambiguationOpening: TThing; var Message: TMessage; NotificationList: TAtomList): TAtom; override;
procedure ProxiedFindMatchingThings(Perspective: TAvatar; Options: TFindMatchingThingsOptions; PositionFilter: TThingPositionFilter; PropertyFilter: TThingFeatures; List: TThingList); override;
function ProxiedFindThingTraverser(Thing: TThing; Perspective: TAvatar; Options: TFindThingOptions): Boolean; override;
end;
// XXX wall with a hole in it... wall without a hole in it... wall that can be hit to make a hole in it...
// Same as ConnectLocations but puts a threshold between them. Direction is determined from the Threshold thing.
//
// Return value must be added to the World. (If you see a memory leak on exit, you probably forgot to do that.)
// If you omit loAutoDescribe from the last argument, then the threshold won't be mentioned in descriptions of rooms that contain it.
// Flags will always contain loPermissibleNavigationTarget and loThreshold regardless of the provided argument.
function ConnectThreshold(FrontLocation, BackLocation: TLocation; Threshold: TThresholdThing; Surface: TThing = nil; Flags: TLandmarkOptions = [loAutoDescribe]): TThresholdLocation;
implementation
uses
lists, exceptions, broadcast, typinfo;
function ConnectThreshold(FrontLocation, BackLocation: TLocation; Threshold: TThresholdThing; Surface: TThing; Flags: TLandmarkOptions = [loAutoDescribe]): TThresholdLocation;
begin
if (not Assigned(Surface)) then
Surface := TThresholdSurface.Create('floor', 'flat? (ground/grounds floor/floors)@', 'The floor is flat.');
Result := TThresholdLocation.Create(Threshold, Surface);
Flags := Flags + [loPermissibleNavigationTarget, loThreshold];
FrontLocation.AddLandmark(cdReverse[Threshold.FrontSideFacesDirection], Result, Flags);
Result.AddLandmark(Threshold.FrontSideFacesDirection, FrontLocation, [loAutoDescribe, loPermissibleNavigationTarget, loNotVisibleFromBehind]);
BackLocation.AddLandmark(Threshold.FrontSideFacesDirection, Result, Flags);
Result.AddLandmark(cdReverse[Threshold.FrontSideFacesDirection], BackLocation, [loAutoDescribe, loPermissibleNavigationTarget, loNotVisibleFromBehind]);
end;
constructor TThresholdThing.Create(Name: UTF8String; Pattern: UTF8String; Description: UTF8String; FrontFacesDirection: TCardinalDirection);
begin
inherited Create(Name, Pattern, Description, tmHeavy, tsMassive);
FFrontSideFacesDirection := FrontFacesDirection;
end;
constructor TThresholdThing.Read(Stream: TReadStream);
begin
inherited;
FFrontSideFacesDirection := TCardinalDirection(Stream.ReadCardinal());
end;
procedure TThresholdThing.Write(Stream: TWriteStream);
begin
inherited;
Stream.WriteCardinal(Cardinal(FFrontSideFacesDirection));
end;
class function TThresholdThing.CreateFromProperties(Properties: TTextStreamProperties): TThresholdThing;
var
Name: UTF8String;
Pattern: UTF8String;
Description: UTF8String;
FrontDirection: TCardinalDirection;
StreamedChildren: TStreamedChildren;
begin
while (not Properties.Done) do
begin
if (Properties.HandleUniqueStringProperty(pnName, Name) and
Properties.HandleUniqueStringProperty(pnPattern, Pattern) and
Properties.HandleUniqueStringProperty(pnDescription, Description) and
Properties.specialize HandleUniqueEnumProperty<TCardinalDirection>(pnFrontDirection, FrontDirection) and {BOGUS Hint: Local variable "FrontDirection" does not seem to be initialized}
HandleChildProperties(Properties, StreamedChildren)) then
Properties.FailUnknownProperty();
end;
Properties.EnsureSeen([pnName, pnPattern, pnDescription, pnFrontDirection]);
Result := Create(Name, Pattern, Description, FrontDirection);
StreamedChildren.Apply(Result);
end;
class procedure TThresholdThing.DescribeProperties(Describer: TPropertyDescriber);
begin
Describer.AddProperty(pnName, ptString);
Describer.AddProperty(pnPattern, ptPattern);
Describer.AddProperty(pnDescription, ptString);
Describer.AddProperty(pnFrontDirection, ptDirection);
Describer.AddProperty(pnChild, ptChild);
end;
function TThresholdThing.LocatePerspective(Perspective: TAvatar): TRelativePerspectivePosition;
var
Ancestor: TAtom;
SubjectiveInformation: TSubjectiveInformation;
Direction, CandidateDirection: TCardinalDirection;
begin
Ancestor := Self;
Assert(Ancestor is TThing); // because we are a TThing
repeat
Ancestor := (Ancestor as TThing).Parent;
until (not (Ancestor is TThing)) or (Ancestor = Perspective);
if (Ancestor = Perspective) then
begin
Result := rppHere;
end
else
begin
SubjectiveInformation := Perspective.Locate(Self);
if (PopCnt(Cardinal(SubjectiveInformation.Directions)) <> 1) then
begin
// e.g. if you're right there at the archway or whatever
// or if there's a trapdoor on some object instead of it being a directional landmark
// we assume that if we can't find Perspective at all, that we're here somehow
Result := rppHere;
end
else
begin
for CandidateDirection in SubjectiveInformation.Directions do
Direction := CandidateDirection; // there can only be one at this point, so this should be enough
Assert(Direction in [FFrontSideFacesDirection, cdReverse[FFrontSideFacesDirection]]);
if (Direction = FFrontSideFacesDirection) then
Result := rppBack
else
Result := rppFront;
end;
end;
end;
function TDoorWay.GetNavigationInstructions(Direction: TCardinalDirection; Child: TThing; Perspective: TAvatar; var Message: TMessage): TNavigationInstruction;
begin
Assert(Message.IsValid);
if (Direction in [cdOut, cdDown]) then
begin
Result.TravelType := ttByPosition;
Result.RequiredAbilities := [naWalk];
Result.PositionTarget := FParent.GetSurface();
Assert(Assigned(Result.PositionTarget)); // XXX handle a doorway being in an area with no surface? but what would that mean?
Result.Position := tpOn;
end
else
begin
Result := inherited;
end;
end;
function TThresholdThing.CanTraverse(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar): Boolean;
begin
Result := True;
end;
constructor TStaticThresholdThing.Read(Stream: TReadStream);
begin
inherited;
FFrontSideDescription := Stream.ReadString();
FBackSideDescription := Stream.ReadString();
end;
procedure TStaticThresholdThing.Write(Stream: TWriteStream);
begin
inherited;
Stream.WriteString(FFrontSideDescription);
Stream.WriteString(FBackSideDescription);
end;
class function TStaticThresholdThing.CreateFromProperties(Properties: TTextStreamProperties): TStaticThresholdThing;
var
Name: UTF8String;
Pattern: UTF8String;
Description, FrontDescription, BackDescription: UTF8String;
FrontDirection: TCardinalDirection;
StreamedChildren: TStreamedChildren;
begin
while (not Properties.Done) do
begin
if (Properties.HandleUniqueStringProperty(pnName, Name) and
Properties.HandleUniqueStringProperty(pnPattern, Pattern) and
Properties.HandleUniqueStringProperty(pnDescription, Description) and
Properties.HandleUniqueStringProperty(pnFrontDescription, FrontDescription) and
Properties.HandleUniqueStringProperty(pnBackDescription, BackDescription) and
Properties.specialize HandleUniqueEnumProperty<TCardinalDirection>(pnFrontDirection, FrontDirection) and {BOGUS Hint: Local variable "FrontDirection" does not seem to be initialized}
HandleChildProperties(Properties, StreamedChildren)) then
Properties.FailUnknownProperty();
end;
Properties.EnsureSeen([pnName, pnPattern, pnDescription, pnFrontDirection]);
Result := Create(Name, Pattern, Description, FrontDirection);
if (Properties.Seen(pnFrontDescription)) then
Result.FrontSideDescription := FrontDescription;
if (Properties.Seen(pnBackDescription)) then
Result.BackSideDescription := BackDescription;
StreamedChildren.Apply(Result);
end;
class procedure TStaticThresholdThing.DescribeProperties(Describer: TPropertyDescriber);
begin
Describer.AddProperty(pnName, ptString);
Describer.AddProperty(pnPattern, ptPattern);
Describer.AddProperty(pnDescription, ptString);
Describer.AddProperty(pnFrontDescription, ptString);
Describer.AddProperty(pnBackDescription, ptString);
Describer.AddProperty(pnFrontDescription, ptDirection);
Describer.AddProperty(pnChild, ptChild);
end;
function TStaticThresholdThing.GetDescriptionSelf(Perspective: TAvatar): UTF8String;
begin
case (LocatePerspective(Perspective)) of
rppFront:
begin
if (FFrontSideDescription <> '') then
Result := FFrontSideDescription
else
Result := inherited;
end;
rppBack:
begin
if (FBackSideDescription <> '') then
Result := FBackSideDescription
else
Result := inherited;
end;
rppHere:
Result := inherited;
end;
end;
constructor TDoorWay.Create(Name: UTF8String; Pattern: UTF8String; Description: UTF8String; FrontFacesDirection: TCardinalDirection; Door: TDoor = nil);
begin
inherited Create(Name, Pattern, Description, FrontFacesDirection);
if (Assigned(Door)) then
begin
Add(Door, tpOfficialDoorPosition);
Assert(Door.Position in tpArguablyInside);
end;
end;
class function TDoorWay.CreateFromProperties(Properties: TTextStreamProperties): TDoorWay;
var
Name: UTF8String;
Pattern: UTF8String;
Description: UTF8String;
FrontDirection: TCardinalDirection;
DoorValue: TThing = nil;
StreamedChildren: TStreamedChildren;
begin
while (not Properties.Done) do
begin
if (Properties.HandleUniqueStringProperty(pnName, Name) and
Properties.HandleUniqueStringProperty(pnPattern, Pattern) and
Properties.HandleUniqueStringProperty(pnDescription, Description) and
Properties.specialize HandleUniqueEnumProperty<TCardinalDirection>(pnFrontDirection, FrontDirection) and {BOGUS Hint: Local variable "FrontDirection" does not seem to be initialized}
TThing.HandleUniqueThingProperty(Properties, pnDoor, DoorValue, TDoor) and
HandleChildProperties(Properties, StreamedChildren)) then
Properties.FailUnknownProperty();
end;
Properties.EnsureSeen([pnName, pnPattern, pnDescription, pnFrontDirection]);
Result := Create(Name, Pattern, Description, FrontDirection, DoorValue as TDoor);
StreamedChildren.Apply(Result);
end;
class procedure TDoorWay.DescribeProperties(Describer: TPropertyDescriber);
begin
Describer.AddProperty(pnName, ptString);
Describer.AddProperty(pnPattern, ptPattern);
Describer.AddProperty(pnDescription, ptString);
Describer.AddProperty(pnFrontDirection, ptDirection);
Describer.AddProperty(pnDoor, ptDoor);
Describer.AddProperty(pnChild, ptChild);
end;
function TDoorWay.GetDoor(): TDoor;
var
Child: TThing;
begin
Result := nil;
for Child in FChildren do
if (GetCouldBeDoor(Child, Child.Position)) then
begin
Assert(Child.Position in tpArguablyInside);
Assert(not Assigned(Result));
Result := Child as TDoor;
{$IFOPT C-} exit; {$ENDIF}
end;
end;
function TDoorWay.GetCouldBeDoor(Thing: TThing; ThingPosition: TThingPosition): Boolean;
begin
Result := (Thing is TDoor) and (ThingPosition in [tpOfficialDoorPosition, tpConsiderForDoorPosition]) and ((Thing as TDoor).Size = FSize);
end;
function TDoorWay.IsClear(): Boolean;
var
List: TThingList;
begin
List := GetChildren(tpObtrusive);
Result := List.Length = 0;
List.Free();
end;
procedure TDoorWay.EnumerateChildren(List: TThingList; const PositionFilter: TThingPositionFilter);
begin
inherited;
if (FParent is TThresholdLocation) then
FParent.EnumerateChildren(List, PositionFilter);
end;
procedure TDoorWay.ProxiedEnumerateExplicitlyReferencedThings(Tokens: TTokens; Start: Cardinal; Perspective: TAvatar; FromOutside, FromFarAway: Boolean; Directions: TCardinalDirectionSet; Reporter: TThingReporter);
var
Obstacles: TThingList;
Obstacle: TThing;
begin
inherited;
Obstacles := GetChildren(tpObtrusive);
try
for Obstacle in Obstacles do // should we check IsChildTraversable() ?
Obstacle.ProxiedEnumerateExplicitlyReferencedThings(Tokens, Start, Perspective, FromOutside, FromFarAway, Directions, Reporter);
finally
Obstacles.Free();
end;
end;
procedure TDoorWay.ProxiedFindMatchingThings(Perspective: TAvatar; Options: TFindMatchingThingsOptions; PositionFilter: TThingPositionFilter; PropertyFilter: TThingFeatures; List: TThingList);
var
Obstacles: TThingList;
Obstacle: TThing;
begin
inherited;
Obstacles := GetChildren(tpObtrusive);
try
for Obstacle in Obstacles do // should we check IsChildTraversable() ?
Obstacle.ProxiedFindMatchingThings(Perspective, Options, PositionFilter, PropertyFilter, List);
finally
Obstacles.Free();
end;
end;
function TDoorWay.ProxiedFindThingTraverser(Thing: TThing; Perspective: TAvatar; Options: TFindThingOptions): Boolean;
var
Obstacles: TThingList;
Obstacle: TThing;
begin
Result := inherited;
if (Result) then
exit;
Obstacles := GetChildren(tpObtrusive);
try
for Obstacle in Obstacles do // should we check IsChildTraversable() ?
if (Obstacle.ProxiedFindThingTraverser(Thing, Perspective, Options)) then
begin
Result := True;
exit;
end;
finally
Obstacles.Free();
end;
end;
function TDoorWay.CanPut(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar; var Message: TMessage): Boolean;
var
OldDoor: TDoor;
CouldBeDoor: Boolean;
DoorObstacles: TThingList;
begin
Assert(Message.IsValid);
OldDoor := GetDoor();
if (ThingPosition = tpOn) then
begin
if (not Assigned(OldDoor)) then
Message := TMessage.Create(mkClosed, '_ can''t put something on _.',
[Capitalise(Perspective.GetDefiniteName(Perspective)), GetIndefiniteName(Perspective)])
else
Message := TMessage.Create(mkClosed, '_ can''t put something on _. Did you mean on _?',
[Capitalise(Perspective.GetDefiniteName(Perspective)),
GetIndefiniteName(Perspective),
OldDoor.GetDefiniteName(Perspective)]);
Result := False;
end
else
if (ThingPosition = tpIn) then
begin
CouldBeDoor := GetCouldBeDoor(Thing, ThingPosition) and (Care = psCarefully);
if (CouldBeDoor) then
begin
if (Assigned(OldDoor)) then
begin
// can't install a door when there's already a door
Message := TMessage.Create(mkDuplicate, '_ already _ _.', [Capitalise(GetDefiniteName(Perspective)),
TernaryConditional('has', 'have', IsPlural(Perspective)),
OldDoor.GetIndefiniteName(Perspective)]);
Result := False;
end
else
begin
DoorObstacles := Thing.GetChildren(tpObtrusive);
try
if (DoorObstacles.Length > 0) then
begin
Message := TMessage.Create(mkBlocked, '_ cannot install _ _ _ while _ _ _ _.',
[Capitalise(Perspective.GetDefiniteName(Perspective)),
Thing.GetIndefiniteName(Perspective),
ThingPositionToString(tpConsiderForDoorPosition),
GetIndefiniteName(Perspective),
DoorObstacles.GetIndefiniteString(Perspective, 'or'),
IsAre(DoorObstacles.IsPlural(Perspective)),
ThingPositionToString(DoorObstacles.First.Position),
Thing.GetObjectPronoun(Perspective)]);
Result := False;
end
else
begin
Message := TMessage.Create(mkSuccess, '_ _ _ _ _.',
[Capitalise(Perspective.GetDefiniteName(Perspective)),
TernaryConditional('installs', 'install', Perspective.IsPlural(Perspective)),
Thing.GetIndefiniteName(Perspective),
ThingPositionToString(tpConsiderForDoorPosition),
GetDefiniteName(Perspective)]);
Result := True;
end;
finally
DoorObstacles.Free();
end;
end;
end
else
if (FParent is TThresholdLocation) then
begin
// just dump the junk in the threshold location
Assert(Assigned(FParent.GetSurface()));
Result := FParent.GetSurface().CanPut(Thing, tpOn, Care, Perspective, Message);
end
else
if (Assigned(OldDoor) and not IsOpen()) then
begin
// can't put something inside a closed doorway, whether it could itself be a door or not
Message := TMessage.Create(mkClosed, GetDescriptionClosed(Perspective));
Result := False;
end
else
begin
// just dump the stuff in us
Result := inherited;
end;
end
else
Assert(False); // CanPut only supports tpOn and tpIn
end;
procedure TDoorWay.Put(Thing: TThing; ThingPosition: TThingPosition; Care: TPlacementStyle; Perspective: TAvatar);
var
Ground: TAtom;
begin
if (((not GetCouldBeDoor(Thing, ThingPosition)) or (Care <> psCarefully) or (Assigned(GetDoor()))) and (FParent is TThresholdLocation)) then
begin
Ground := FParent.GetSurface();
Assert(Assigned(Ground));
DoBroadcast([Self, Ground], Perspective,
[C(M(@Perspective.GetDefiniteName)), SP, // You
MP(Perspective, M('drops'), M('drop')), SP, // drop
M(@Thing.GetDefiniteName), SP, // the door
M(ThingPositionToString(ThingPosition)), SP, // in
M(@GetDefiniteName), // the door way
M(', '),
M(ThingPositionToString(tpOnGround)), SP, // to
M(@Ground.GetDefiniteName), // the ground
M('.')]);
Ground.Put(Thing, tpOnGround, Care, Perspective);
end
else
begin
Assert(Care = psCarefully);
Assert(ThingPosition in [tpConsiderForDoorPosition, tpOfficialDoorPosition]);
inherited Put(Thing, tpOfficialDoorPosition, psCarefully, Perspective);
end;
end;
procedure TDoorWay.HandleAdd(Thing: TThing; Blame: TAvatar);
begin
if (Thing = GetDoor()) then
begin
FOpened := not IsClear();
DoBroadcast([Self, Blame], Blame,
[C(M(@Blame.GetDefiniteName)), SP, // You
MP(Blame, M('installs'), M('install')), SP, // install
M(@Thing.GetIndefiniteName), SP, // a thing
M(ThingPositionToString(tpConsiderForDoorPosition)), SP, // in
M(@GetDefiniteName), // the door way
M('. '),
C(M(@Thing.GetSubjectPronoun)), SP, // It
MP(Thing, M('is'), M('are')), SP, // is
M(TernaryConditional('closed', 'open', IsOpen())),
M('.')]);
end;
inherited;
end;
procedure TDoorWay.HandlePassedThrough(Traveller: TThing; AFrom, ATo: TAtom; AToPosition: TThingPosition; Perspective: TAvatar);
var
TheDoor: TDoor;
begin
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
TheDoor.HandlePassedThrough(Traveller, AFrom, ATo, AToPosition, Perspective);
end;
procedure TDoorWay.Removed(Thing: TThing);
begin
if (not Assigned(GetDoor())) then
FOpened := True;
inherited;
end;
function TDoorWay.GetInside(var PositionOverride: TThingPosition): TThing;
begin
Result := Self;
end;
function TDoorWay.CanInsideHold(const Manifest: TThingSizeManifest; const ManifestCount: Integer): Boolean;
begin
Result := (GetInsideSizeManifest() + Manifest) < FSize;
end;
function TDoorWay.GetDefaultDestination(var ThingPosition: TThingPosition): TThing;
begin
if ((ThingPosition = tpOn) and (FParent is TThresholdLocation)) then
begin
Result := FParent.GetSurface();
end
else
begin
Assert(ThingPosition in [tpAt, tpIn]);
ThingPosition := tpIn;
Result := Self;
end;
Assert(Assigned(Result));
end;
function TDoorWay.GetLookIn(Perspective: TAvatar): UTF8String;
begin
if (CanSeeThrough()) then
begin
Result := GetLookThrough(Perspective);
if (Result = '') then
Result := inherited;
end
else
Result := inherited;
end;
function TDoorWay.GetLookUnder(Perspective: TAvatar): UTF8String;
var
TheDoor: TDoor;
Directions: TCardinalDirectionSet;
begin
Result := '';
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
begin
Directions := cdAllDirections;
case (LocatePerspective(Perspective)) of
rppFront: Exclude(Directions, cdReverse[FFrontSideFacesDirection]);
rppBack: Exclude(Directions, FFrontSideFacesDirection);
rppHere: ; // nothing to change
end;
Result := Capitalise(GetDefiniteName(Perspective)) + ' ' +
TernaryConditional('contains', 'contain', IsPlural(Perspective)) + ' ' +
TheDoor.GetIndefiniteName(Perspective) + '.' +
WithSpaceIfNotEmpty(TheDoor.GetBasicDescription(Perspective, psThereIsAThingHere, Directions)) +
WithNewlineIfNotEmpty(GetDescriptionObstacles(Perspective,
'Other than ' + TheDoor.GetDefiniteName(Perspective) + ', there is nothing in ' + GetDefiniteName(Perspective) + '.'));
end
else
begin
Result := GetDescriptionObstacles(Perspective, 'There is nothing in ' + GetDefiniteName(Perspective) + '.');
end;
end;
function TDoorWay.GetLookThrough(Perspective: TAvatar): UTF8String;
begin
Assert((not Assigned(GetDoor())) or
IsOpen() or
CanSeeThrough() or
GetDoor().CanSeeUnder(Perspective));
case (LocatePerspective(Perspective)) of
rppFront: Result := GetLookTowardsDirection(Perspective, cdReverse[FFrontSideFacesDirection]);
rppBack: Result := GetLookTowardsDirection(Perspective, FFrontSideFacesDirection);
else
Result := FParent.GetBasicDescription(Perspective, psThereIsAThingHere, cdAllDirections, Self); // we're probably at the threshold itself somehow
end;
end;
function TDoorWay.GetDescriptionRemoteBrief(Perspective: TAvatar; Mode: TGetPresenceStatementMode; Direction: TCardinalDirection): UTF8String;
var
TheDoor: TDoor;
begin
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
begin
Result := TheDoor.GetDescriptionRemoteBrief(Perspective, Mode, Direction);
end
else
begin
Result := inherited;
end;
end;
function TDoorWay.GetDescriptionRemoteDetailed(Perspective: TAvatar; Direction: TCardinalDirection; LeadingPhrase: UTF8String; Options: TLeadingPhraseOptions): UTF8String;
var
TheDoor: TDoor;
begin
// this whole function is a weird mess of cases
// we should rethink this all through
// we should probably not bother using GetLookThrough either, since we know the direction we're looking
TheDoor := GetDoor();
if (not Assigned(TheDoor)) then
begin
if (CanSeeThrough()) then
begin
Perspective.AutoDisambiguated('looking through ' + GetDefiniteName(Perspective));
Result := GetLookThrough(Perspective);
end
else
begin
// Defer to the default behavior but
if ((lpMandatory in Options) or not (lpNamesTarget in Options)) then
LeadingPhrase := LeadingPhrase + ','
else
LeadingPhrase := 'Looking';
LeadingPhrase := LeadingPhrase + ' through ' + GetDefiniteName(Perspective);
Include(Options, lpMandatory);
Result := inherited;
end;
end
else
begin
Exclude(Options, lpNamesTarget);
if (IsOpen()) then
begin
if (CanSeeThrough()) then
begin
if (Direction <> cdOut) then
Perspective.AutoDisambiguated('looking through the open ' + TheDoor.GetName(Perspective));
Result := GetLookThrough(Perspective);
end
else
begin
Result := TheDoor.GetDescriptionRemoteDetailed(Perspective, Direction, LeadingPhrase, Options);
end;
end
else
Result := TheDoor.GetDescriptionRemoteDetailed(Perspective, Direction, LeadingPhrase, Options);
end;
/// XXX if we go through the no-door case above, and there's something blocking the door frame, this is ugly:
Result := Result + WithSpaceIfNotEmpty(GetDescriptionObstacles(Perspective));
end;
function TDoorWay.GetDescriptionObstacles(Perspective: TAvatar; NoObstacleMessage: UTF8String = ''): UTF8String;
var
Obstacles: TThingList;
begin
Result := '';
Obstacles := GetChildren(tpObtrusive);
try
if (Obstacles.Length > 0) then
Result := 'Blocking ' +
GetDefiniteName(Perspective) + ' ' +
IsAre(Obstacles.IsPlural(Perspective)) + ' ' +
Obstacles.GetIndefiniteString(Perspective, 'and') + '.'
else
Result := NoObstacleMessage;
finally
Obstacles.Free();
end;
end;
function TDoorWay.GetDescriptionEmpty(Perspective: TAvatar): UTF8String;
var
TheDoor: TDoor;
begin
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
Result := 'Other than ' + TheDoor.GetDefiniteName(Perspective) + ', there is nothing in ' + GetDefiniteName(Perspective) + '.'
else
Result := inherited;
end;
function TDoorWay.GetDescriptionClosed(Perspective: TAvatar): UTF8String;
var
TheDoor: TDoor;
begin
TheDoor := GetDoor();
Assert(Assigned(TheDoor) and not IsOpen());
if (Assigned(TheDoor)) then
Result := TheDoor.GetDescriptionClosed(Perspective)
else
Result := inherited;
end;
function TDoorWay.GetEntrance(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar; var PositionOverride: TThingPosition; var DisambiguationOpening: TThing; var Message: TMessage; NotificationList: TAtomList): TAtom;
begin
Assert(Message.IsValid);
if (IsOpen() and (FParent is TThresholdLocation)) then
begin
if (Direction = cdIn) then
begin
case (LocatePerspective(Perspective)) of
rppFront: Direction := cdReverse[FFrontSideFacesDirection];
rppBack: Direction := FFrontSideFacesDirection;
rppHere: ; // cdIn is fine
end;
end;
PositionOverride := tpOn;
Result := FParent.GetEntrance(Traveller, Direction, Perspective, PositionOverride, DisambiguationOpening, Message, NotificationList)
end
else
Result := inherited;
end;
function TDoorWay.CanSeeIn(): Boolean;
begin
Result := True;
end;
function TDoorWay.CanSeeThrough(): Boolean;
begin
Result := (not Assigned(GetDoor())) or (IsOpen());
end;
function TDoorWay.Open(Perspective: TAvatar; var Message: TMessage): Boolean;
var
TheDoor: TDoor;
begin
Assert(Message.IsValid);
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
begin
if (IsOpen()) then
begin
Message := TMessage.Create(mkRedundant, '_ in _ _ already open.',
[Capitalise(TheDoor.GetDefiniteName(Perspective)),
GetDefiniteName(Perspective),
IsAre(IsPlural(Perspective))]);
Result := False;
end
else
begin
Assert(IsClear(), 'Something is mysteriously blocking the (closed!) door.');
DoBroadcast([TheDoor, Perspective], Perspective, [C(M(@Perspective.GetDefiniteName)), SP,
MP(Perspective, M('opens'), M('open')), SP,
M(@TheDoor.GetDefiniteName), M('.')]);
FOpened := True;
Result := True;
end;
end
else
begin
Assert(IsOpen());
Message := TMessage.Create(mkNoDoor, '_ _ wide open.', [Capitalise(GetDefiniteName(Perspective)), IsAre(IsPlural(Perspective))]);
Result := False;
end;
end;
function TDoorWay.Close(Perspective: TAvatar; var Message: TMessage): Boolean;
var
TheDoor: TDoor;
Obstacles, MoreObstacles: TThingList;
begin
Assert(Message.IsValid);
TheDoor := GetDoor();
if (Assigned(TheDoor)) then
begin
if (not IsOpen()) then
begin
Message := TMessage.Create(mkRedundant, '_ _ _ _ already closed.',
[Capitalise(TheDoor.GetDefiniteName(Perspective)),
ThingPositionToString(TheDoor.Position),
GetDefiniteName(Perspective),
IsAre(IsPlural(Perspective))]);
Result := False;
end
else
begin
Obstacles := GetChildren(tpObtrusive);
try
MoreObstacles := TheDoor.GetChildren(tpObtrusive);
try
Obstacles.AdoptList(MoreObstacles);
finally
MoreObstacles.Free();
end;
if (Obstacles.Length > 0) then
begin
Message := TMessage.Create(mkBlocked, '_ cannot be closed; _ _ in the way.',
[Capitalise(TheDoor.GetDefiniteName(Perspective)),
Obstacles.GetDefiniteString(Perspective, 'and'),
IsAre(Obstacles.IsPlural(Perspective))]);
Result := False;
exit;
end;
finally
Obstacles.Free();
end;
DoBroadcast([TheDoor, Perspective], Perspective, [C(M(@Perspective.GetDefiniteName)), SP,
MP(Perspective, M('closes'), M('close')), SP,
M(@TheDoor.GetDefiniteName), M('.')]);
Result := True;
FOpened := False;
end;
end
else
begin
Assert(IsOpen());
Message := TMessage.Create(mkNoDoor, 'There is nothing in _ with which to close _.', [GetDefiniteName(Perspective), GetObjectPronoun(Perspective)]);
Result := False;
end;
end;
function TDoorWay.GetFeatures(): TThingFeatures;
begin
Result := inherited;
Result := Result + [tfCanHaveThingsPushedIn];
end;
function TDoorWay.CanTraverse(Traveller: TThing; Direction: TCardinalDirection; Perspective: TAvatar): Boolean;
begin
Result := (not Assigned(GetDoor())) or (IsOpen());
end;
constructor TDoor.Create(Name: UTF8String; Pattern: UTF8String; FrontSide, BackSide: TDoorSide; AMass: TThingMass = tmHeavy; ASize: TThingSize = tsMassive);
begin
inherited Create(Name, Pattern, '' { description }, AMass, ASize);
Assert(Assigned(FrontSide));
FFrontSide := FrontSide;
Add(FrontSide, tpAmbiguousPartOfImplicit);
Assert(Assigned(BackSide));
FBackSide := BackSide;
Add(BackSide, tpAmbiguousPartOfImplicit);
end;
constructor TDoor.Read(Stream: TReadStream);
begin
inherited;