-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgame.py
1713 lines (1490 loc) · 58.9 KB
/
game.py
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
"""How we keep track of the state of the game."""
# Numeric comments scattered through this file refer to FORTRAN line
# numbers, for those comparing this file and `advent.for`; so "#2012"
# refers to FORTRAN line number 2012 (which you can find easily in the
# FORTRAN using Emacs with an interactive search for newline-2012-tab,
# that is typed C-s C-q C-j 2 0 1 2 C-i).
import os
import pickle
import random
import zlib
from operator import attrgetter
from data import Data
from model import Room, Message, Dwarf, Pirate
YESNO_ANSWERS = {'y': True, 'yes': True, 'n': False, 'no': False}
class Game(Data):
look_complaints = 3 # how many times to "SORRY, BUT I AM NOT ALLOWED..."
full_description_period = 5 # how often we use a room's full description
full_wests = 0 # how many times they have typed "west" instead of "w"
dwarf_stage = 0 # DFLAG how active the dwarves are
dwarves_killed = 0 # DKILL
knife_location = None # KNFLOC
foobar = -1 # FOOBAR turn number of most recent still-valid "fee"
gave_up = False
treasures_not_found = 0 # TALLY how many treasures have not yet been seen
impossible_treasures = 0 # TALLY2 how many treasures can never be retrieved
lamp_turns = 330
warned_about_dim_lamp = False
bonus = 0 # how they exited the final bonus round
is_dead = False # whether we are currently dead
deaths = 0 # how many times the player has died
max_deaths = 3 # how many times the player can die
turns = 0
def __init__(self, seed=None):
Data.__init__(self)
self.output = ''
self.yesno_callback = False
self.yesno_casual = False # whether to insist they answer
self.clock1 = 30 # counts down from finding last treasure
self.clock2 = 50 # counts down until cave closes
self.is_closing = False # is the cave closing?
self.panic = False # they tried to leave during closing?
self.is_closed = False # is the cave closed?
self.is_done = False # caller can check for "game over"
self.could_fall_in_pit = False # could the player fall into a pit?
self.random_generator = random.Random()
if seed is not None:
self.random_generator.seed(seed)
def random(self):
return self.random_generator.random()
def choice(self, seq):
return self.random_generator.choice(seq)
def write(self, more):
"""Append the Unicode representation of `s` to our output."""
if more:
self.output += str(more).upper()
self.output += '\n'
def write_message(self, n):
self.write(self.messages[n])
def yesno(self, s, yesno_callback, casual=False):
"""Ask a question and prepare to receive a yes-or-no answer."""
self.write(s)
self.yesno_callback = yesno_callback
self.yesno_casual = casual
# Properties of the cave.
@property
def is_dark(self):
lamp = self.objects['lamp']
if self.is_here(lamp) and lamp.prop:
return False
return self.loc.is_dark
@property
def inventory(self):
return [ obj for obj in self.object_list if obj.is_toting ]
@property
def treasures(self):
return [ obj for obj in self.object_list if obj.is_treasure ]
@property
def objects_here(self):
return self.objects_at(self.loc)
def objects_at(self, room):
return [ obj for obj in self.object_list if room in obj.rooms ]
def is_here(self, obj):
if isinstance(obj, Dwarf):
return self.loc is obj.room
else:
return obj.is_toting or (self.loc in obj.rooms)
@property
def is_finished(self):
return (self.is_dead or self.is_done) and not self.yesno_callback
# Game startup
def start(self):
"""Start the game."""
# For old-fashioned players, accept five-letter truncations like
# "inven" instead of insisting on full words like "inventory".
for key, value in list(self.vocabulary.items()):
if isinstance(key, str) and len(key) > 5:
self.vocabulary[key[:5]] = value
# Set things going.
self.chest_room = self.rooms[114]
self.bottle.contents = self.water
self.yesno(self.messages[65], self.start2) # want instructions?
def start2(self, yes):
"""Display instructions if the user wants them."""
if yes:
self.write_message(1)
self.hints[3].used = True
self.lamp_turns = 1000
self.oldloc2 = self.oldloc = self.loc = self.rooms[1]
self.dwarves = [ Dwarf(self.rooms[n]) for n in (19, 27, 33, 44, 64) ]
self.pirate = Pirate(self.chest_room)
treasures = self.treasures
self.treasures_not_found = len(treasures)
for treasure in treasures:
treasure.prop = -1
self.describe_location()
# Routines that handle the aftermath of "big" actions like movement.
# Although these are called at the end of each `do_command()` cycle,
# we place here at the top of `game.py` to mirror the order in the
# advent.for file.
def move_to(self, newloc=None): #2
loc = self.loc
if newloc is None:
newloc = loc
if self.is_closing and newloc.is_aboveground:
self.write_message(130)
newloc = loc # cancel move and put him back underground
if not self.panic:
self.clock2 = 15
self.panic = True
must_allow_move = ((newloc is loc) or (loc.is_forced)
or (loc.is_forbidden_to_pirate))
dwarf_blocking_the_way = any(
dwarf.old_room is newloc and dwarf.has_seen_adventurer
for dwarf in self.dwarves
)
if not must_allow_move and dwarf_blocking_the_way:
newloc = loc # cancel move they were going to make
self.write_message(2) # dwarf is blocking the way
self.loc = loc = newloc #74
# IF LOC.EQ.0 ?
is_dwarf_area = not (loc.is_forced or loc.is_forbidden_to_pirate)
if is_dwarf_area and self.dwarf_stage > 0:
self.move_dwarves()
else:
if is_dwarf_area and loc.is_after_hall_of_mists:
self.dwarf_stage = 1
self.describe_location()
def move_dwarves(self):
#6000
if self.dwarf_stage == 1:
# 5% chance per turn of meeting first dwarf
if self.loc.is_before_hall_of_mists or self.random() < .95:
self.describe_location()
return
self.dwarf_stage = 2
for i in range(2): # randomly remove 0, 1, or 2 dwarves
if self.random() < .5:
self.dwarves.remove(self.choice(self.dwarves))
for dwarf in self.dwarves:
if dwarf.room is self.loc: # move dwarf away from our loc
dwarf.start_at(self.rooms[18])
self.write_message(3) # dwarf throws axe and curses
self.axe.drop(self.loc)
self.describe_location()
return
#6010
dwarf_count = dwarf_attacks = knife_wounds = 0
for dwarf in self.dwarves + [ self.pirate ]:
locations = { move.action for move in dwarf.room.travel_table
if dwarf.can_move(move)
and move.action is not dwarf.old_room
and move.action is not dwarf.room }
# Without stabilizing the order with a sort, the room chosen
# would depend on how the Room addresses in memory happen to
# order the rooms in the set() - and make it impossible to
# test the game by setting the random number generator seed
# and then playing through the game.
locations = sorted(locations, key=attrgetter('n'))
if locations:
new_room = self.choice(locations)
else:
new_room = dwarf.old_room
dwarf.old_room, dwarf.room = dwarf.room, new_room
if self.loc in (dwarf.room, dwarf.old_room):
dwarf.has_seen_adventurer = True
elif self.loc.is_before_hall_of_mists:
dwarf.has_seen_adventurer = False
if not dwarf.has_seen_adventurer:
continue
dwarf.room = self.loc
if dwarf.is_dwarf:
dwarf_count += 1
# A dwarf cannot walk and attack at the same time.
if dwarf.room is dwarf.old_room:
dwarf_attacks += 1
self.knife_location = self.loc
if self.random() < .095 * (self.dwarf_stage - 2):
knife_wounds += 1
else: # the pirate
pirate = dwarf
if self.loc is self.chest_room or self.chest.prop >= 0:
continue # decide that the pirate is not really here
treasures = [ t for t in self.treasures if t.is_toting ]
if (self.platinum in treasures and self.loc.n in (100, 101)):
treasures.remove(self.platinum)
if not treasures:
h = any( t for t in self.treasures if self.is_here(t) )
one_treasure_left = (self.treasures_not_found ==
self.impossible_treasures + 1)
shiver_me_timbers = (
one_treasure_left and not h and not(self.chest.rooms)
and self.is_here(self.lamp) and self.lamp.prop == 1
)
if not shiver_me_timbers:
if (pirate.old_room != pirate.room
and self.random() < .2):
self.write_message(127)
continue # pragma: no cover
self.write_message(186)
self.chest.drop(self.chest_room)
self.message.drop(self.rooms[140])
else:
#6022 I'll just take all this booty
self.write_message(128)
if not self.message.rooms:
self.chest.drop(self.chest_room)
self.message.drop(self.rooms[140])
for treasure in treasures:
treasure.drop(self.chest_room)
#6024
pirate.old_room = pirate.room = self.chest_room
pirate.has_seen_adventurer = False # free to move
# Report what has happened.
if dwarf_count == 1:
self.write_message(4)
elif dwarf_count:
self.write('There are {} threatening little dwarves in the'
' room with you.\n'.format(dwarf_count))
if dwarf_attacks and self.dwarf_stage == 2:
self.dwarf_stage = 3
if dwarf_attacks == 1:
self.write_message(5)
k = 52
elif dwarf_attacks:
self.write('{} of them throw knives at you!\n'.format(dwarf_attacks))
k = 6
if not dwarf_attacks:
pass
elif not knife_wounds:
self.write_message(k)
else:
if knife_wounds == 1:
self.write_message(k + 1)
else:
self.write('{} of them get you!\n'.format(knife_wounds))
self.oldloc2 = self.loc
self.die()
return
self.describe_location()
def describe_location(self): #2000
loc = self.loc
could_fall = self.is_dark and self.could_fall_in_pit
if could_fall and not loc.is_forced and self.random() < .35:
self.die_here()
return
if self.bear.is_toting:
self.write_message(141)
if self.is_dark and not loc.is_forced:
self.write_message(16)
else:
do_short = loc.times_described % self.full_description_period
loc.times_described += 1
if do_short and loc.short_description:
self.write(loc.short_description)
else:
self.write(loc.long_description)
if loc.is_forced:
self.do_motion(self.vocabulary[2]) # dummy motion verb
return
if loc.n == 33 and self.random() < .25 and not self.is_closing:
self.write_message(8)
if not self.is_dark:
for obj in self.objects_here:
if obj is self.steps and self.gold.is_toting:
continue
if obj.prop < 0: # finding a treasure the first time
if self.is_closed:
continue
obj.prop = 1 if obj in (self.rug, self.chain) else 0
self.treasures_not_found -= 1
if (self.treasures_not_found > 0 and
self.treasures_not_found == self.impossible_treasures):
self.lamp_turns = min(35, self.lamp_turns)
if obj is self.steps and self.loc is self.steps.rooms[1]:
prop = 1
else:
prop = obj.prop
self.write(obj.messages[prop])
self.finish_turn()
def say_okay_and_finish(self, *ignored): #2009
self.write_message(54)
self.finish_turn()
#2009 sets SPK="OK" then...
#2010 sets SPK to K
#2011 speaks SPK then...
#2012 blanks VERB and OBJ and calls:
def finish_turn(self, obj=None): #2600
# Advance random number generator so each input affects future.
self.random()
# Check whether we should offer a hint.
for hint in self.hints.values():
if hint.turns_needed == 9999 or hint.used:
continue
if self.loc in hint.rooms:
hint.turn_counter += 1
if hint.turn_counter >= hint.turns_needed:
if hint.n != 5: # hint 5 counter does not get reset
hint.turn_counter = 0
if self.should_offer_hint(hint, obj):
hint.turn_counter = 0
def callback(yes):
if yes:
self.write(hint.message)
hint.used = True
else:
self.write_message(54)
self.yesno(hint.question, callback)
return
else:
hint.turn_counter = 0
if self.is_closed:
if self.oyster.prop < 0 and self.oyster.is_toting:
self.write(self.oyster.messages[1])
for obj in self.inventory:
if obj.prop < 0:
obj.prop = - 1 - obj.prop
self.could_fall_in_pit = self.is_dark #2605
if self.knife_location and self.knife_location is not self.loc:
self.knife_location = None
# The central do_command() method, that should be called over and
# over again with words supplied by the user.
def do_command(self, words):
"""Parse and act upon the command in the list of strings `words`."""
self.output = ''
self._do_command(words)
return self.output
def _do_command(self, words):
if self.yesno_callback is not None:
answer = YESNO_ANSWERS.get(words[0], None)
if answer is None:
if self.yesno_casual:
self.yesno_callback = None
else:
self.write('Please answer the question.')
return
else:
callback = self.yesno_callback
self.yesno_callback = None
callback(answer)
return
if self.is_dead:
self.write('You have gotten yourself killed.')
return
#2608
self.turns += 1
if (self.treasures_not_found == 0
and self.loc.n >= 15 and self.loc.n != 33):
self.clock1 -= 1
if self.clock1 == 0:
self.start_closing_cave() # no "return", to do their command
if self.clock1 < 0:
self.clock2 -= 1
if self.clock2 == 0:
return self.close_cave() # "return", to cancel their command
if self.lamp.prop == 1:
self.lamp_turns -= 1
if self.lamp_turns <= 30 and self.is_here(self.batteries) \
and self.batteries.prop == 0 and self.is_here(self.lamp):
#12000
self.write_message(188)
self.batteries.prop = 1
if self.batteries.is_toting:
self.batteries.drop(self.loc)
self.lamp_turns += 2500
self.warned_about_dim_lamp = False
elif self.lamp_turns == 0:
#12400
self.lamp_turns = -1
self.lamp.prop = 0
if self.is_here(self.lamp):
self.write_message(184)
elif self.lamp_turns < 0 and self.loc.is_aboveground:
#12600
self.write_message(185)
self.gave_up = True
self.score_and_exit()
return
elif self.lamp_turns <= 30 and not self.warned_about_dim_lamp \
and self.is_here(self.lamp):
#12200
self.warned_about_dim_lamp = True
if self.batteries.prop == 1:
self.write_message(189)
elif not self.batteries.rooms:
self.write_message(183)
else:
self.write_message(187)
self.dispatch_command(words)
def dispatch_command(self, words): #19999
if not 1 <= len(words) <= 2:
return self.dont_understand()
if words[0] == 'save' and len(words) > 1:
# Handle suspend separately, since filename can be anything,
# and is not restricted to being a vocabulary word (and, in
# fact, it can be an open file).
return self.t_suspend(words[0], words[1])
words = [ self.vocabulary.get(word) for word in words ]
if None in words:
return self.dont_understand()
word1 = words[0]
word2 = words[1] if len(words) == 2 else None
if word1 == 'enter' and (word2 == 'stream' or word2 == 'water'):
if self.loc.liquid is self.water:
self.write_message(70)
else:
self.write_message(43)
return self.finish_turn()
if (word1 == 'enter' or word1 == 'walk') and word2:
#2800 'enter house' becomes simply 'house' and so forth
word1, word2 = word2, None
if ((word1 == 'water' or word1 == 'oil') and
(word2 == 'plant' or word2 == 'door') and
self.is_here(self.referent(word2))):
word1, word2 = self.vocabulary['pour'], word1
if word1 == 'say':
return self.t_say(word1, word2) if word2 else self.i_say(word1)
kinds = (word1.kind, word2.kind if word2 else None)
#2630
if kinds == ('travel', None):
if word1.text == 'west': #2610
self.full_wests += 1
if self.full_wests == 10:
self.write_message(17)
return self.do_motion(word1)
if kinds == ('snappy_comeback', None):
self.write_message(word1.n % 1000)
return self.finish_turn()
if kinds == ('noun', None):
verb, noun = None, word1
elif kinds == ('verb', None):
verb, noun = word1, None
elif kinds == ('verb', 'noun'):
verb, noun = word1, word2
elif kinds == ('noun', 'verb'):
noun, verb = word1, word2
else:
return self.dont_understand()
if not noun:
obj = None
else:
obj = self.referent(noun)
obj_here = self.is_here(obj)
if not obj_here:
if obj is self.grate:
if self.loc.n in (1, 4, 7):
return self.dispatch_command([ 'depression' ])
elif 9 < self.loc.n < 15:
return self.dispatch_command([ 'entrance' ])
elif noun == 'dwarf':
obj_here = any( d.room is self.loc for d in self.dwarves )
elif obj is self.bottle.contents and self.is_here(self.bottle):
obj_here = True
elif obj is self.loc.liquid:
obj_here = True
elif (obj is self.plant and self.is_here(self.plant2)
and self.plant2.prop != 0):
obj = self.plant2
obj_here = True
elif obj is self.knife and self.knife_location is self.loc:
self.knife_location = None
self.write_message(116)
return self.finish_turn()
elif obj is self.rod and self.is_here(self.rod2):
obj = self.rod2
obj_here = True
elif verb and (verb == 'find' or verb == 'inventory'):
obj_here = True # lie; these verbs work for absent objects
if not obj_here:
return self.i_see_no(noun)
if not verb:
self.write('What do you want to do with the {}?\n'.format(
noun.text))
return self.finish_turn()
verb_name = verb.synonyms[0].text
if obj:
method_name = 't_' + verb_name
args = (verb, obj)
else:
method_name = 'i_' + verb_name
args = (verb,)
method = getattr(self, method_name)
method(*args)
def dont_understand(self):
#3000 (a bit earlier than in the Fortran code)
n = self.random()
if n < 0.20: # 20% of the entire 1.0 range of random()
self.write_message(61)
elif n < 0.36: # 20% of the remaining 0.8 left
self.write_message(13)
else:
self.write_message(60)
self.finish_turn()
def i_see_no(self, thing):
self.write('I see no {} here.\n'.format(getattr(thing, 'text', thing)))
self.finish_turn()
# Motion.
def do_motion(self, word): #8
if word == 'null': #2
self.move_to()
return
elif word == 'back': #20
dest = self.oldloc2 if self.oldloc.is_forced else self.oldloc
self.oldloc2, self.oldloc = self.oldloc, self.loc
if dest is self.loc:
self.write_message(91)
self.move_to()
return
alt = None
for move in self.loc.travel_table:
if move.action is dest:
word = move.verbs[0] # arbitrary verb going to `dest`
break # Fall through, to attempt the move.
elif (isinstance(move.action, Room)
and move.action.is_forced
and move.action.travel_table[0].action is dest):
alt = move.verbs[0]
else: # no direct route is available
if alt is not None:
word = alt # take a forced move if it's the only option
else:
self.write_message(140)
self.move_to()
return
elif word == 'look': #30
if self.look_complaints > 0:
self.write_message(15)
self.look_complaints -= 1
self.loc.times_described = 0
self.move_to()
self.could_fall_in_pit = False
return
elif word == 'cave': #40
self.write_message(57 if self.loc.is_aboveground else 58)
self.move_to()
return
self.oldloc2, self.oldloc = self.oldloc, self.loc
for move in self.loc.travel_table:
if move.is_forced or word in move.verbs:
c = move.condition
if c[0] is None or c[0] == 'not_dwarf':
allowed = True
elif c[0] == '%':
allowed = 100 * self.random() < c[1]
elif c[0] == 'carrying':
allowed = self.objects[c[1]].is_toting
elif c[0] == 'carrying_or_in_room_with':
allowed = self.is_here(self.objects[c[1]])
elif c[0] == 'prop!=':
allowed = self.objects[c[1]].prop != c[2]
if not allowed:
continue
if isinstance(move.action, Room):
self.move_to(move.action)
return
elif isinstance(move.action, Message):
self.write(move.action)
self.move_to()
return
elif move.action == 301: #30100
inv = self.inventory
if len(inv) != 0 and inv != [ self.emerald ]:
self.write_message(117)
self.move_to()
elif self.loc.n == 100:
self.move_to(self.rooms[99])
else:
self.move_to(self.rooms[100])
return
elif move.action == 302: #30200
self.emerald.drop(self.loc)
self.do_motion(word)
return
elif move.action == 303: #30300
troll, troll2 = self.troll, self.troll2
if troll.prop == 1:
self.write(troll.messages[1])
troll.prop = 0
troll.rooms = list(troll.starting_rooms)
troll2.destroy()
self.move_to()
return
else:
places = list(troll.starting_rooms)
places.remove(self.loc)
self.loc = places[0] # "the other side of the bridge"
if troll.prop == 0:
troll.prop = 1
if not self.bear.is_toting:
self.move_to()
return
self.write_message(162)
self.chasm.prop = 1
troll.prop = 2
self.bear.drop(self.loc)
self.bear.is_fixed = True
self.bear.prop = 3
if self.spices.prop < 0:
self.impossible_treasures += 1
self.oldloc2 = self.loc # refuse to strand belongings
self.die()
return
#50
n = word.n
if 29 <= n <= 30 or 43 <= n <= 50:
self.write_message(9)
elif n in (7, 36, 37):
self.write_message(10)
elif n in (11, 19):
self.write_message(11)
elif n in (62, 65):
self.write_message(42)
elif n == 17:
self.write_message(80)
else:
self.write_message(12)
self.move_to()
return
# Death and reincarnation.
def die_here(self): #90
self.write_message(23)
self.oldloc2 = self.loc
self.die()
def die(self): #99
self.deaths += 1
self.is_dead = True
if self.is_closing:
self.write_message(131)
self.score_and_exit()
return
def callback(yes):
if yes:
self.write_message(80 + self.deaths * 2)
if self.deaths < self.max_deaths:
# do water and oil thing
self.is_dead = False
if self.lamp.is_toting:
self.lamp.prop = 0
for obj in self.inventory:
if obj is self.lamp:
obj.drop(self.rooms[1])
else:
obj.drop(self.oldloc2)
self.loc = self.rooms[3]
self.describe_location()
return
else:
self.write_message(54)
self.score_and_exit()
self.yesno(self.messages[79 + self.deaths * 2], callback)
# Verbs.
def ask_verb_what(self, verb, *args): #8000
self.write('{} What?\n'.format(verb.text))
self.finish_turn()
i_walk = ask_verb_what
i_drop = ask_verb_what
i_say = ask_verb_what
i_nothing = say_okay_and_finish
i_wave = ask_verb_what
i_calm = ask_verb_what
i_rub = ask_verb_what
i_throw = ask_verb_what
i_find = ask_verb_what
i_feed = ask_verb_what
i_break = ask_verb_what
i_wake = ask_verb_what
def write_default_message(self, verb, *args):
self.write(verb.default_message)
self.finish_turn()
t_nothing = say_okay_and_finish
t_calm = write_default_message
t_quit = write_default_message
t_score = write_default_message
t_fee = write_default_message
t_brief = write_default_message
t_hours = write_default_message
def i_carry(self, verb): #8010
is_dwarf_here = any( dwarf.room == self.loc for dwarf in self.dwarves )
objs = self.objects_here
if len(objs) != 1 or is_dwarf_here:
self.ask_verb_what(verb)
else:
self.t_carry(verb, objs[0])
def t_carry(self, verb, obj): #9010
if obj.is_toting:
self.write(verb.default_message)
self.finish_turn()
return
if obj.is_fixed or len(obj.rooms) > 1:
if obj is self.plant and obj.prop <= 0:
self.write_message(115)
elif obj is self.bear and obj.prop == 1:
self.write_message(169)
elif obj is self.chain and self.chain.prop != 0:
self.write_message(170)
else:
self.write_message(25)
self.finish_turn()
return
if obj is self.water or obj is self.oil:
if self.is_here(self.bottle) and self.bottle.contents is obj:
# They want to carry the filled bottle.
obj = self.bottle
else:
# They must mean they want to fill the bottle.
if not self.bottle.is_toting:
self.write_message(104)
elif self.bottle.contents is not None:
self.write_message(105)
else:
self.t_fill(verb, self.bottle) # hand off control to "fill"
return
self.finish_turn()
return
if len(self.inventory) >= 7:
self.write_message(92)
self.finish_turn()
return
if obj is self.bird and obj.prop == 0:
if self.rod.is_toting:
self.write_message(26)
self.finish_turn(obj) # needs obj to decide to give hint
return
if not self.cage.is_toting:
self.write_message(27)
self.finish_turn()
return
self.bird.prop = 1
if (obj is self.bird or obj is self.cage) and self.bird.prop != 0:
self.bird.carry()
self.cage.carry()
else:
obj.carry()
if obj is self.bottle and self.bottle.contents is not None:
self.bottle.contents.carry()
self.say_okay_and_finish()
def t_drop(self, verb, obj): #9020
if obj is self.rod and not self.rod.is_toting and self.rod2.is_toting:
obj = self.rod2
if not obj.is_toting:
self.write(verb.default_message)
self.finish_turn()
return
bird, snake, dragon, bear, troll = self.bird, self.snake, self.dragon, \
self.bear, self.troll
if obj is bird and self.is_here(snake):
self.write_message(30)
if self.is_closed:
self.wake_repository_dwarves()
return
snake.prop = 1
snake.destroy()
elif obj is self.coins and self.is_here(self.machine):
obj.destroy()
self.batteries.drop(self.loc)
self.write(self.batteries.messages[0])
self.finish_turn()
return
elif obj is bird and self.is_here(dragon) and dragon.prop == 0:
self.write_message(154)
bird.destroy()
bird.prop = 0
if snake.rooms:
self.impossible_treasures += 1
self.finish_turn()
return
elif obj is bear and self.is_here(troll):
self.write_message(163)
troll.destroy()
self.troll2.rooms = list(self.troll.starting_rooms)
troll.prop = 2
elif obj is self.vase and self.loc is not self.rooms[96]:
if self.pillow.is_at(self.loc):
self.vase.prop = 0
else:
self.vase.prop = 2
self.vase.is_fixed = True
self.write(self.vase.messages[self.vase.prop + 1])
else:
self.write_message(54)
#9021
if obj is self.bottle.contents:
obj = self.bottle
if obj is self.bottle and self.bottle.contents:
self.bottle.contents.hide()
if obj is self.cage and self.bird.prop != 0:
bird.drop(self.loc)
elif obj is self.bird:
obj.prop = 0
obj.drop(self.loc)
self.finish_turn()
return
def t_say(self, verb, word): #9030
if word.n in (62, 65, 71, 2025):
self.dispatch_command([ word.text ])
else:
self.write('Okay, "{}".'.format(word.text))
self.finish_turn()
def i_unlock(self, verb): #8040 Handles "unlock" case as well
objs = (self.grate, self.door, self.oyster, self.clam, self.chain)
objs = list(filter(self.is_here, objs))
if len(objs) > 1:
self.ask_verb_what(verb)
elif len(objs) == 1:
self.t_unlock(verb, objs[0])
else:
self.write_message(28)
self.finish_turn()
i_lock = i_unlock
def t_unlock(self, verb, obj): #9040 Handles "lock" case as well
if obj is self.clam or obj is self.oyster:
#9046
oy = 1 if (obj is self.oyster) else 0
if verb == 'lock':
self.write_message(61)
elif not self.trident.is_toting:
self.write_message(122 + oy)
elif obj.is_toting:
self.write_message(120 + oy)
elif obj is self.oyster:
self.write_message(125)
else:
self.write_message(124)
self.clam.destroy()
self.oyster.drop(self.loc)
self.pearl.drop(self.rooms[105])
elif obj is self.door:
if obj.prop == 1: