forked from EricEve/adv3lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
precond.t
887 lines (750 loc) · 28.9 KB
/
precond.t
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
#charset "us-ascii"
#include "advlite.h"
/*
* ***************************************************************************
* precond.t
*
* This module forms part of the adv3Lite library (c) 2012-13 Eric Eve
*
* Based on code in the adv3 library (c) Michael J. Roberts.
*/
/*
* A PreCondition encapsulate a condition that must be fulfilled in order for
* an action to be fulfilled (e.g. a container must be open before we can put
* anything in it). A PreCondition may also try to bring about the fulfilment
* of the condition it enforces via an implicit action.
*/
class PreCondition: object
/*
* Check the condition on the given object (which may be nil, if
* this condition doesn't apply specifically to one of the objects
* in the command). If it is possible to meet the condition with an
* implicit command, and allowImplicit is true, try to execute the
* command. If the condition cannot be met, report a failure and
* return nil to terminate the command.
*
* If allowImplicit is nil, an implicit command may not be
* attempted. In this case, if the condition is not met, we must
* simply report a failure return nil to terminate the command.
*/
checkPreCondition(obj, allowImplicit) { return true; }
/*
* Verify the condition. This is called during the object
* verification step so that the pre-condition can add verifications
* of its own. This can be used, for example, to add likelihood to
* objects that already meet the condition. Note that it is
* generally not desirable to report illogical for conditions that
* checkPreCondition() enforces, because doing so will prevent
* checkPreCondition() from ever being reached and thus will prevent
* checkPreCondition() from attempting to carry out implicit actions
* to meet the condition.
*
* 'obj' is the object being checked. Note that because this is
* called during verification, the explicitly passed-in object must
* be used in the check rather than the current object in the global
* current action.
*/
verifyPreCondition(obj) { }
/*
* Precondition execution order. When we execute preconditions for a
* given action, we'll sort the list of all applicable preconditions
* in ascending execution order.
*
* For the most part, the relative order of two preconditions is
* arbitrary. In some unusual cases, though, the order is important,
* such as when applying one precondition can destroy the conditions
* that the other would try to create but not vice versa. When the
* order doesn't matter, this can be left at the default setting.
*/
preCondOrder = 100
;
/*
* A precondition to check whether a container is open. If this is defined on
* the action handling of an object, it only takes effect if that object
* behaves like a container, in which case either it, or its remapIn object,
* must be open for the action to proceed.
*/
containerOpen: PreCondition
checkPreCondition(obj, allowImplicit)
{
/*
* if we have a non-nil remapIn property, that's the container
* representing us, so we need to use it instead.
*/
if(obj.remapIn != nil)
obj = obj.remapIn;
/*
* If the object is already open, we're already done; also there's
* nothing to be done if we're not a container (in the sense of
* something objects can be put in).
*/
if (obj == nil || obj.contType != In || obj.isOpen)
return true;
/* If we're allowed to try an implicit action, then try opening obj */
if(allowImplicit)
{
/*
* Try opening obj via an implicit action and note whether tha
* action was actually attempted.
*/
local tried = tryImplicitAction(Open, obj);
/*
* If obj is now open, this precondition has been met, so return
* true.
*/
if(obj.isOpen)
return true;
/*
* Otherwise if we tried but failed to open obj, return nil to
* signal that we haven't met this precondition so the main action
* can't go ahead. The attempt to open obj will have reported the
* reason why it failed, so there's no need to display anything
* else.
*/
if(tried)
return nil;
}
/*
* If we reach here obj is closed and we weren't allowed to try to
* open it; display a message explaining the problem.
*/
gMessageParams(obj);
DMsg(container needs to be open, '{The subj obj} need{s/ed} to be open
for that. ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/* A PreCondition to check whether an object is open. */
objOpen: PreCondition
verifyPreCondition(obj)
{
/*
* If the object isn't open already, make it a slightly less logical
* choice of object for this command.
*/
if(!obj.isOpen)
logicalRank(90);
}
checkPreCondition(obj, allowImplicit)
{
/*
* if the object is already open, we're already done.
*/
if (obj == nil || obj.isOpen)
return true;
/*
* If we're allowed to attempt an implicit action, try opening obj
* implicitly and see if we succeed.
*/
if(allowImplicit)
{
/*
* Try opening obj implicitly and note if we were allowed to make
* the attempt.
*/
local tried = tryImplicitAction(Open, obj);
/*
* If obj is now open return true to signal that this precondition
* has now been met.
*/
if(obj.isOpen)
return true;
/*
* Otherwise, if we tried but failed to open obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to open obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* If we reach here obj is closed and we weren't allowed to try to
* open it; display a message explaining the problem.
*/
gMessageParams(obj);
DMsg(object needs to be open, '{The subj obj} need{s/ed} to be open for
that. ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/* A PreCondition to check whether an object is closed. */
objClosed: PreCondition
verifyPreCondition(obj)
{
/*
* If the object is open, make it a slightly less logical choice of
* object for this command.
*/
if(obj.isOpen)
logicalRank(90);
}
checkPreCondition(obj, allowImplicit)
{
/*
* if the object is already closed, we're already done.
*/
if (obj == nil || ! obj.isOpen)
return true;
/*
* If we're allowed to attempt an implicit action, try closing obj
* implicitly and see if we succeed.
*/
if(allowImplicit)
{
/*
* Try closing obj implicitly and note if we were allowed to make
* the attempt.
*/
local tried = tryImplicitAction(Close, obj);
/*
* If obj is now closed return true to signal that this
* precondition has now been met.
*/
if(!obj.isOpen)
return true;
/*
* Otherwise, if we tried but failed to open obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to close obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* If we reach here obj is open and we weren't allowed to try to
* close it; display a message explaining the problem.
*/
gMessageParams(obj);
DMsg(obj needs to be closed, '{The subj obj} need{s/ed} to be closed for
that. ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/*
* A PreCondition to check whether an object is unlocked, and to attempt to
* unlock it if it seems possible to do so.
*/
objUnlocked: PreCondition
checkPreCondition(obj, allowImplicit)
{
/*
* If the object is already unlocked, we're already done. If the
* object is neither lockableWithKey nor lockableWithoutKey there's
* nothing we can do here.
*/
if (obj == nil || ! obj.isLocked
|| obj.lockability not in (lockableWithoutKey, lockableWithKey) )
return true;
/*
* If the object can be unlocked without a Key, try unlocking it
* implicitly
*/
if(allowImplicit && obj.lockability == lockableWithoutKey)
{
/*
* Try unlocking obj implicitly and note if we were allowed to make
* the attempt.
*/
local tried = tryImplicitAction(Unlock, obj);
/*
* If obj is now unlocked return true to signal that this
* precondition has now been met.
*/
if(!obj.isLocked)
return true;
/*
* Otherwise, if we tried but failed to unlock obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to unlock obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* If the object needs a key to unlock it, attempt to unlock it with a
* plausible key if there is one, otherwise return true to let the
* OPEN action go ahead and fail.
*/
if(allowImplicit && obj.lockability == lockableWithKey)
{
/*
* Try to find a key held by the actor that may unlock this
* object; the result will be stored in the useKey_ property of
* the object.
*/
obj.findPlausibleKey(true);
/*
* If we don't find a key, return TRUE to let the OPEN action go
* ahead and report that the object is locked, since there's
* nothing else we can do here.
*/
if(obj.useKey_ == nil)
return true;
/*
* Try unlocking obj with useKey_ implicitly and note if we were
* allowed to make the attempt.
*/
local tried = tryImplicitAction(UnlockWith, obj, obj.useKey_);
/*
* If obj is now unlocked return true to signal that this
* precondition has now been met.
*/
if(!obj.isLocked)
return true;
/*
* Otherwise, if we tried but failed to unlock obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to unlock obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* Although the PreCondition hasn't been met, we'll return true at
* this point to let the OPEN action go ahead and report the problem.
*/
return true;
}
;
/* A PreCondition to check whether an object is currently held by the actor. */
objHeld: PreCondition
verifyPreCondition(obj)
{
/*
* If the object is fixed in place it can't be picked up, so there's
* no point in trying. BUT we also have to check for the case that the
* object is directly in the player (perhaps because a body part).
*/
if(obj.isFixed && !obj.isDirectlyIn(gActor))
illogical(obj.cannotTakeMsg);
/*
* If the actor isn't carrying the object it's slightly less likely to
* be the one the player means.
*/
if(!obj.isIn(gActor))
logicalRank(90);
}
checkPreCondition(obj, allowImplicit)
{
/*
* if the object is already held, we're already done.
*/
if (obj == nil || obj.isDirectlyIn(gActor))
return true;
/*
* If we're allowed to attempt an implicit action, try taking obj
* implicitly and see if we succeed.
*/
if(allowImplicit)
{
/*
* Try taking obj implicitly and note if we were allowed to make
* the attempt.
*/
local tried = tryImplicitAction(Take, obj);
/*
* If obj is now held by the actor return true to signal that this
* precondition has now been met.
*/
if(obj.isDirectlyIn(gActor))
return true;
/*
* Otherwise, if we tried but failed to take obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to take obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* If we reach here obj isn't being held by the actor and we weren't
* allowed to try to take it; display a message explaining the
* problem.
*/
gMessageParams(obj);
DMsg(need to hold, '{I} need{s/ed} to be holding {the obj} to do that. ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/* A PreCondition to check that an object isn't being worn. */
objNotWorn: PreCondition
checkPreCondition(obj, allowImplicit)
{
/*
* if the object is not being worn, we're already done.
*/
if (obj == nil || obj.wornBy != gActor)
return true;
/*
* If we're allowed to attempt an implicit action, try taking off obj
* implicitly and see if we succeed.
*/
if(allowImplicit)
{
/*
* Try taking off obj implicitly and note if we were allowed to
* make the attempt.
*/
local tried = tryImplicitAction(Doff, obj);
/*
* If obj is now not being worn return true to signal that this
* precondition has now been met.
*/
if(obj.wornBy == nil)
return true;
/*
* Otherwise, if we tried but failed to take obj off, return nil
* to signal that this precondition can't be met (so the main
* action cannot proceed). The attempt to take off obj will have
* explained why it failed, so there's no need for any further
* explanation here.
*/
if(tried)
return nil;
}
/*
* If we reach here obj is still being worn and we weren't allowed to
* try to take it off; display a message explaining the problem.
*/
gMessageParams(obj);
DMsg(cannot do that while wearing, '{I} {can\'t} do that while
{he actor}{\'m} wearing {the obj). ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/* A PreCondition to check that an object is visible. */
objVisible: PreCondition
verifyPreCondition(obj)
{
/* If it's too dark to see then we can't examine the object */
if(!(gActor.outermostVisibleParent().isIlluminated || obj.visibleInDark))
inaccessible(obj.tooDarkToSeeMsg);
/* If the actor can't see the obj then we can't examine the object. */
if(!Q.canSee(gActor, obj))
inaccessible(BMsg(cannot see obj, '{I} {can\'t} see {1}. ',
obj.theName));
}
;
/* A PreCondition to check that an object is audible. */
objAudible: PreCondition
verifyPreCondition(obj)
{
/*
* Construct a list of objects that are blocking the sound path between
* the actor and obj.
*/
local lst = Q.soundBlocker(gActor, obj);
/*
* If the actor cannot hear obj and there's at least one object
* blocking the sound path between them, construct an appropriate
* error message and block the action.
*/
if(!Q.canHear(gActor, obj) && lst.length > 0)
{
local errMsg;
gMessageParams(obj);
/*
* If the blocking object is a Room, then obj is in a remote
* location, so the reason the actor can't hear it is that it's
* too far away.
*/
if(lst[1].ofKind(Room))
errMsg = BMsg(too far away to hear obj, '{The subj obj} {is} too
far away to hear. ');
/*
* Otherwise the reason the actor can't hear obj is that the first
* blocking object is in the way.
*/
else
errMsg = BMsg(cannot hear, '{I} {can\'t} hear {1}
through {2}. ', obj.theName, lst[1].theName);
/* Declare obj to be inaccessible to hearing. */
inaccessible(errMsg);
}
}
;
/* A PreCondition to check that an object is smellable. */
objSmellable: PreCondition
verifyPreCondition(obj)
{
/*
* Construct a list of objects that are blocking the scent path between
* the actor and obj.
*/
local lst = Q.scentBlocker(gActor, obj);
/*
* If the actor cannot hear obj and there's at least one object
* blocking the sound path between them, construct an appropriate
* error message and block the action.
*/
if(!Q.canSmell(gActor, obj) && lst.length > 0)
{
local errMsg;
gMessageParams(obj);
/*
* If the blocking object is a Room, then obj is in a remote
* location, so the reason the actor can't hear it is that it's
* too far away.
*/
if(lst[1].ofKind(Room))
errMsg = BMsg(too far away to smell obj, '{The subj obj} {is}
too far away to smell. ');
/*
* Otherwise the reason the actor can't hear obj is that the first
* blocking object is in the way.
*/
else
errMsg = BMsg(cannot smell, '{I} {can\'t} smell {1}
through {2}. ', obj.theName, lst[1].theName);
/* Declare obj to be inaccessible to hearing. */
inaccessible(errMsg);
}
}
;
/*
* A PreCondition to check that an object can be touched (which is likely to
* be needed for any action that manipulated the object). This Precondition
* farms out as much of the detailed checking as possible to the Query object.
*/
touchObj: PreCondition
verifyPreCondition(obj)
{
/*
* Store any issues that the Query object finds with reaching obj from
* gActor
*/
local reachIssues = Q.reachProblemVerify(gActor, obj);
/* Run the verify method of any issues we found */
foreach(local issue in reachIssues)
issue.verify();
}
checkPreCondition(obj, allowImplicit)
{
/*
* Obtain any issues the Query object finds with reaching obj from
* gActor at the check stage.
*/
local reachIssues = Q.reachProblemCheck(gActor, obj);
/*
* Go through each stored issue in turn running its check method; if
* any of the check methods return nil, exit and return nil.
*/
foreach(local issue in reachIssues)
{
if(issue.check(allowImplicit) == nil)
return nil;
}
/*
* If we reach here we've passed all the checks, so return true to
* indicate success.
*/
return true;
}
preCondOrder = 80
;
/* Declare attachedTo as a property since the attachables module is optional. */
property attachedTo;
property attachedToList;
/* A PreCondition to check that an object isn't attached to anything. */
objDetached: PreCondition
verifyPreCondition(obj)
{
/*
* An object that is attached may be a slightly less logical choice
* for this action.
*/
if(obj.attachedTo != nil)
logicalRank(90);
}
checkPreCondition(obj, allowImplicit)
{
/* If the object isn't attached, we're done */
if(obj.attachedToList.length == 0)
return true;
/*
* If we're allowed to attempt an implicit action, try detaching obj
* implicitly and see if we succeed.
*/
if(allowImplicit)
{
/*
* Try detaching obj implicitly and note if we were allowed to
* make the attempt.
*/
local tried = nil;
foreach(local cur in obj.attachedToList)
tried = tryImplicitAction(DetachFrom, obj, cur);
/*
* If obj is now not attached to anything return true to signal
* that this precondition has now been met.
*/
if(obj.attachedToList.length == 0)
return true;
/*
* Otherwise, if we tried but failed to detach obj, return nil to
* signal that this precondition can't be met (so the main action
* cannot proceed). The attempt to detach obj will have explained
* why it failed, so there's no need for any further explanation
* here.
*/
if(tried)
return nil;
}
/*
* If we reach here obj is still attached to something and we weren't
* allowed to try to detach it; display a message explaining the
* problem.
*/
local att = obj.attachedTo;
gMessageParams(obj, att);
DMsg(cannot do that while attached, '{I} {can\'t} do that while {the subj
obj} is attached to {the att). ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/* ------------------------------------------------------------------------ */
/*
* A pre-condition that applies to a specific, pre-determined object,
* rather than the direct/indirect object of the command.
*/
class ObjectPreCondition: PreCondition
construct(obj, cond)
{
/*
* remember the specific object I act upon, and the underlying
* precondition to apply to that object
*/
obj_ = obj;
cond_ = cond;
}
/* route our check to the pre-condition using our specific object */
checkPreCondition(obj, allowImplicit)
{
/* check the precondition */
return cond_.checkPreCondition(obj_, allowImplicit);
}
/* route our verification check to the pre-condition */
verifyPreCondition(obj)
{
cond_.verifyPreCondition(obj_);
}
/* use the same order as our underlying condition */
preCondOrder = (cond_.preCondOrder)
/* the object we check with the condition */
obj_ = nil
/* the pre-condition we check */
cond_ = nil
;
/* -------------------------------------------------------------------------- */
actorInStagingLocation: PreCondition
checkPreCondition(obj, allowImplicit)
{
local loc = gActor.location;
local stagingLoc = obj.stagingLocation;
local action;
/* If the actor's location is the staging location then we're done. */
if(loc == stagingLoc)
return true;
if(stagingLoc == nil)
{
gMessageParams(obj);
DMsg(no staging loc, '{The subj obj} {can\'t} be reached. ');
return nil;
}
if(allowImplicit)
{
local tried = nil;
while(!stagingLoc.isOrIsIn(loc))
{
action = loc.contType == In ? GetOutOf : GetOff;
tried = tryImplicitAction(action, loc);
if(gActor.location == loc)
break;
loc = gActor.location;
}
if(stagingLoc == loc)
return true;
local path = [];
local step = stagingLoc;
while(step && step != loc)
{
path = [step] + path;
step = step.stagingLocation;
}
foreach(step in path)
{
action = step.contType == In ? Enter : Board;
tried = tryImplicitAction(action, step);
if(gActor.location != step)
break;
}
if(stagingLoc == gActor.location)
return true;
if(tried)
return nil;
}
gMessageParams(stagingLoc);
DMsg(not in staging location, '{I} need{s/ed} to be <<if
stagingLoc.ofKind(Room)>> directly <<end>>
{in stagingloc} to do that. ');
/* Then return nil to indicate that the precondition hasn't been met. */
return nil;
}
;
/*
* A PreCondition to make sure that the actor is out of any nested rooms
* before the action takes place.
*/
actorOutOfNested: PreCondition
checkPreCondition(obj, allowImplicit)
{
local loc = gActor.location;
local action;
/*
* If the Actor's location is a room, then there's nothing to be done.
*/
if(loc.ofKind(Room))
return true;
if(allowImplicit)
{
local tried = nil;
while(!loc.ofKind(Room))
{
action = loc.contType == In ? GetOutOf : GetOff;
tried = tryImplicitAction(action, loc);
if(gActor.location == loc)
break;
loc = gActor.location;
}
/* If the actor is now in a Room, we've succeeded. */
if(loc.ofKind(Room))
return true;
/*
* Otherwise, if we tried but failed to get out into the Room,
* return nil to tell our caller we failed.
*/
if(tried)
return nil;
}
gMessageParams(loc);
DMsg(still in nested, '{I} {can\'t} do that while {he actor} {is}
{in loc}. ');
return nil;
}
;