Debian Codegraph @

adventure/game.py

raw
1 """How we keep track of the state of the game.
2
3 Copyright 2010-2015 Brandon Rhodes. Licensed as free software under the
4 Apache License, Version 2.0 as detailed in the accompanying README.txt.
5
6 """
7 # Numeric comments scattered through this file refer to FORTRAN line
8 # numbers, for those comparing this file and `advent.for`; so "#2012"
9 # refers to FORTRAN line number 2012 (which you can find easily in the
10 # FORTRAN using Emacs with an interactive search for newline-2012-tab,
11 # that is typed C-s C-q C-j 2 0 1 2 C-i).
12
13 import os
14 import pickle
15 import random
16 import zlib
17 from operator import attrgetter
18 from .data import Data
19 from .model import Room, Message, Dwarf, Pirate
20
21 YESNO_ANSWERS = {'y': True, 'yes': True, 'n': False, 'no': False}
22
23 class Game(Data):
24
25 look_complaints = 3 # how many times to "SORRY, BUT I AM NOT ALLOWED..."
26 full_description_period = 5 # how often we use a room's full description
27 full_wests = 0 # how many times they have typed "west" instead of "w"
28 dwarf_stage = 0 # DFLAG how active the dwarves are
29 dwarves_killed = 0 # DKILL
30 knife_location = None # KNFLOC
31 foobar = -1 # FOOBAR turn number of most recent still-valid "fee"
32 gave_up = False
33 treasures_not_found = 0 # TALLY how many treasures have not yet been seen
34 impossible_treasures = 0 # TALLY2 how many treasures can never be retrieved
35 lamp_turns = 330
36 warned_about_dim_lamp = False
37 bonus = 0 # how they exited the final bonus round
38 is_dead = False # whether we are currently dead
39 deaths = 0 # how many times the player has died
40 max_deaths = 3 # how many times the player can die
41 turns = 0
42
43 def __init__(self, seed=None):
44 Data.__init__(self)
45 self.output = ''
46 self.yesno_callback = False
47 self.yesno_casual = False # whether to insist they answer
48
49 self.clock1 = 30 # counts down from finding last treasure
50 self.clock2 = 50 # counts down until cave closes
51 self.is_closing = False # is the cave closing?
52 self.panic = False # they tried to leave during closing?
53 self.is_closed = False # is the cave closed?
54 self.is_done = False # caller can check for "game over"
55 self.could_fall_in_pit = False # could the player fall into a pit?
56
57 self.random_generator = random.Random()
58 if seed is not None:
59 self.random_generator.seed(seed)
60
61 def random(self):
62 return self.random_generator.random()
63
64 def choice(self, seq):
65 return self.random_generator.choice(seq)
66
67 def write(self, more):
68 """Append the Unicode representation of `s` to our output."""
69 if more:
70 self.output += str(more).upper()
71 self.output += '\n'
72
73 def write_message(self, n):
74 self.write(self.messages[n])
75
76 def yesno(self, s, yesno_callback, casual=False):
77 """Ask a question and prepare to receive a yes-or-no answer."""
78 self.write(s)
79 self.yesno_callback = yesno_callback
80 self.yesno_casual = casual
81
82 # Properties of the cave.
83
84 @property
85 def is_dark(self):
86 lamp = self.objects['lamp']
87 if self.is_here(lamp) and lamp.prop:
88 return False
89 return self.loc.is_dark
90
91 @property
92 def inventory(self):
93 return [ obj for obj in self.object_list if obj.is_toting ]
94
95 @property
96 def treasures(self):
97 return [ obj for obj in self.object_list if obj.is_treasure ]
98
99 @property
100 def objects_here(self):
101 return self.objects_at(self.loc)
102
103 def objects_at(self, room):
104 return [obj for obj in self.object_list if obj.is_at(room)]
105
106 def is_here(self, obj):
107 if isinstance(obj, Dwarf):
108 return self.loc is obj.room
109 else:
110 return obj.is_toting or obj.is_at(self.loc)
111
112 @property
113 def is_finished(self):
114 return (self.is_dead or self.is_done) and not self.yesno_callback
115
116 # Game startup
117
118 def start(self):
119 """Start the game."""
120
121 # For old-fashioned players, accept five-letter truncations like
122 # "inven" instead of insisting on full words like "inventory".
123
124 for key, value in list(self.vocabulary.items()):
125 if isinstance(key, str) and len(key) > 5:
126 self.vocabulary[key[:5]] = value
127
128 # Set things going.
129
130 self.chest_room = self.rooms[114]
131 self.bottle.contents = self.water
132 self.yesno(self.messages[65], self.start2) # want instructions?
133
134 def start2(self, yes):
135 """Display instructions if the user wants them."""
136 if yes:
137 self.write_message(1)
138 self.hints[3].used = True
139 self.lamp_turns = 1000
140
141 self.oldloc2 = self.oldloc = self.loc = self.rooms[1]
142 self.dwarves = [ Dwarf(self.rooms[n]) for n in (19, 27, 33, 44, 64) ]
143 self.pirate = Pirate(self.chest_room)
144
145 treasures = self.treasures
146 self.treasures_not_found = len(treasures)
147 for treasure in treasures:
148 treasure.prop = -1
149
150 self.describe_location()
151
152 # Routines that handle the aftermath of "big" actions like movement.
153 # Although these are called at the end of each `do_command()` cycle,
154 # we place here at the top of `game.py` to mirror the order in the
155 # advent.for file.
156
157 def move_to(self, newloc=None): #2
158 loc = self.loc
159 if newloc is None:
160 newloc = loc
161
162 if self.is_closing and newloc.is_aboveground:
163 self.write_message(130)
164 newloc = loc # cancel move and put him back underground
165 if not self.panic:
166 self.clock2 = 15
167 self.panic = True
168
169 must_allow_move = ((newloc is loc) or (loc.is_forced)
170 or (loc.is_forbidden_to_pirate))
171
172 dwarf_blocking_the_way = any(
173 dwarf.old_room is newloc and dwarf.has_seen_adventurer
174 for dwarf in self.dwarves
175 )
176
177 if not must_allow_move and dwarf_blocking_the_way:
178 newloc = loc # cancel move they were going to make
179 self.write_message(2) # dwarf is blocking the way
180
181 self.loc = loc = newloc #74
182
183 # IF LOC.EQ.0 ?
184 is_dwarf_area = not (loc.is_forced or loc.is_forbidden_to_pirate)
185 if is_dwarf_area and self.dwarf_stage > 0:
186 self.move_dwarves()
187 else:
188 if is_dwarf_area and loc.is_after_hall_of_mists:
189 self.dwarf_stage = 1
190 self.describe_location()
191
192 def move_dwarves(self):
193
194 #6000
195 if self.dwarf_stage == 1:
196
197 # 5% chance per turn of meeting first dwarf
198 if self.loc.is_before_hall_of_mists or self.random() < .95:
199 self.describe_location()
200 return
201 self.dwarf_stage = 2
202 for i in range(2): # randomly remove 0, 1, or 2 dwarves
203 if self.random() < .5:
204 self.dwarves.remove(self.choice(self.dwarves))
205 for dwarf in self.dwarves:
206 if dwarf.room is self.loc: # move dwarf away from our loc
207 dwarf.start_at(self.rooms[18])
208 self.write_message(3) # dwarf throws axe and curses
209 self.axe.drop(self.loc)
210 self.describe_location()
211 return
212
213 #6010
214 dwarf_count = dwarf_attacks = knife_wounds = 0
215
216 for dwarf in self.dwarves + [ self.pirate ]:
217
218 locations = { move.action for move in dwarf.room.travel_table
219 if dwarf.can_move(move)
220 and move.action is not dwarf.old_room
221 and move.action is not dwarf.room }
222 # Without stabilizing the order with a sort, the room chosen
223 # would depend on how the Room addresses in memory happen to
224 # order the rooms in the set() - and make it impossible to
225 # test the game by setting the random number generator seed
226 # and then playing through the game.
227 locations = sorted(locations, key=attrgetter('n'))
228 if locations:
229 new_room = self.choice(locations)
230 else:
231 new_room = dwarf.old_room
232 dwarf.old_room, dwarf.room = dwarf.room, new_room
233 if self.loc in (dwarf.room, dwarf.old_room):
234 dwarf.has_seen_adventurer = True
235 elif self.loc.is_before_hall_of_mists:
236 dwarf.has_seen_adventurer = False
237
238 if not dwarf.has_seen_adventurer:
239 continue
240
241 dwarf.room = self.loc
242
243 if dwarf.is_dwarf:
244 dwarf_count += 1
245 # A dwarf cannot walk and attack at the same time.
246 if dwarf.room is dwarf.old_room:
247 dwarf_attacks += 1
248 self.knife_location = self.loc
249 if self.random() < .095 * (self.dwarf_stage - 2):
250 knife_wounds += 1
251
252 else: # the pirate
253 pirate = dwarf
254
255 if self.loc is self.chest_room or self.chest.prop >= 0:
256 continue # decide that the pirate is not really here
257
258 treasures = [ t for t in self.treasures if t.is_toting ]
259 if (self.platinum in treasures and self.loc.n in (100, 101)):
260 treasures.remove(self.platinum)
261
262 if not treasures:
263 h = any( t for t in self.treasures if self.is_here(t) )
264 one_treasure_left = (self.treasures_not_found ==
265 self.impossible_treasures + 1)
266 shiver_me_timbers = (
267 one_treasure_left and not h and not(self.chest.rooms)
268 and self.is_here(self.lamp) and self.lamp.prop == 1
269 )
270
271 if not shiver_me_timbers:
272 if (pirate.old_room != pirate.room
273 and self.random() < .2):
274 self.write_message(127)
275 continue # pragma: no cover
276
277 self.write_message(186)
278 self.chest.drop(self.chest_room)
279 self.message.drop(self.rooms[140])
280
281 else:
282 #6022 I'll just take all this booty
283 self.write_message(128)
284 if not self.message.rooms:
285 self.chest.drop(self.chest_room)
286 self.message.drop(self.rooms[140])
287 for treasure in treasures:
288 treasure.drop(self.chest_room)
289
290 #6024
291 pirate.old_room = pirate.room = self.chest_room
292 pirate.has_seen_adventurer = False # free to move
293
294 # Report what has happened.
295
296 if dwarf_count == 1:
297 self.write_message(4)
298 elif dwarf_count:
299 self.write('There are {} threatening little dwarves in the'
300 ' room with you.\n'.format(dwarf_count))
301
302 if dwarf_attacks and self.dwarf_stage == 2:
303 self.dwarf_stage = 3
304
305 if dwarf_attacks == 1:
306 self.write_message(5)
307 k = 52
308 elif dwarf_attacks:
309 self.write('{} of them throw knives at you!\n'.format(dwarf_attacks))
310 k = 6
311
312 if not dwarf_attacks:
313 pass
314 elif not knife_wounds:
315 self.write_message(k)
316 else:
317 if knife_wounds == 1:
318 self.write_message(k + 1)
319 else:
320 self.write('{} of them get you!\n'.format(knife_wounds))
321 self.oldloc2 = self.loc
322 self.die()
323 return
324
325 self.describe_location()
326
327 def describe_location(self): #2000
328
329 loc = self.loc
330
331 if loc.n == 0:
332 self.die()
333
334 could_fall = self.is_dark and self.could_fall_in_pit
335 if could_fall and not loc.is_forced and self.random() < .35:
336 self.die_here()
337 return
338
339 if self.bear.is_toting:
340 self.write_message(141)
341
342 if self.is_dark and not loc.is_forced:
343 self.write_message(16)
344 else:
345 do_short = loc.times_described % self.full_description_period
346 loc.times_described += 1
347 if do_short and loc.short_description:
348 self.write(loc.short_description)
349 else:
350 self.write(loc.long_description)
351
352 if loc.is_forced:
353 self.do_motion(self.vocabulary[2]) # dummy motion verb
354 return
355
356 if loc.n == 33 and self.random() < .25 and not self.is_closing:
357 self.write_message(8)
358
359 # for obj in self.objects.values():
360 # if obj.rooms and [room.n for room in obj.rooms] != [115]:
361 # if (len(obj.messages) == 0) or (0 not in obj.messages):
362 # raise ValueError('%r %r' % (obj, obj.rooms))
363 # print(obj, obj.rooms)
364
365 if not self.is_dark:
366 for obj in self.objects_here:
367
368 if obj is self.steps and self.gold.is_toting:
369 continue
370
371 if obj.prop < 0: # finding a treasure the first time
372 if self.is_closed:
373 continue
374 obj.prop = 1 if obj in (self.rug, self.chain) else 0
375 self.treasures_not_found -= 1
376 if (self.treasures_not_found > 0 and
377 self.treasures_not_found == self.impossible_treasures):
378 self.lamp_turns = min(35, self.lamp_turns)
379
380 if obj is self.steps and self.loc is self.steps.rooms[1]:
381 prop = 1
382 else:
383 prop = obj.prop
384
385 self.write(obj.messages[prop])
386
387 self.finish_turn()
388
389 def say_okay_and_finish(self, *ignored): #2009
390 self.write_message(54)
391 self.finish_turn()
392
393 #2009 sets SPK="OK" then...
394 #2010 sets SPK to K
395 #2011 speaks SPK then...
396 #2012 blanks VERB and OBJ and calls:
397 def finish_turn(self, obj=None): #2600
398
399 # Advance random number generator so each input affects future.
400 self.random()
401
402 # Check whether we should offer a hint.
403 for hint in self.hints.values():
404 if hint.turns_needed == 9999 or hint.used:
405 continue
406 if self.loc in hint.rooms:
407 hint.turn_counter += 1
408 if hint.turn_counter >= hint.turns_needed:
409 if hint.n != 5: # hint 5 counter does not get reset
410 hint.turn_counter = 0
411 if self.should_offer_hint(hint, obj):
412 hint.turn_counter = 0
413
414 def callback(yes):
415 if yes:
416 self.write(hint.message)
417 hint.used = True
418 else:
419 self.write_message(54)
420
421 self.yesno(hint.question, callback)
422 return
423 else:
424 hint.turn_counter = 0
425
426 if self.is_closed:
427 if self.oyster.prop < 0 and self.oyster.is_toting:
428 self.write(self.oyster.messages[1])
429 for obj in self.inventory:
430 if obj.prop < 0:
431 obj.prop = - 1 - obj.prop
432
433 self.could_fall_in_pit = self.is_dark #2605
434 if self.knife_location and self.knife_location is not self.loc:
435 self.knife_location = None
436
437 # The central do_command() method, that should be called over and
438 # over again with words supplied by the user.
439
440 def do_command(self, words):
441 """Parse and act upon the command in the list of strings `words`."""
442 self.output = ''
443 self._do_command(words)
444 return self.output
445
446 def _do_command(self, words):
447 if self.yesno_callback is not None:
448 answer = YESNO_ANSWERS.get(words[0], None)
449 if answer is None:
450 if self.yesno_casual:
451 self.yesno_callback = None
452 else:
453 self.write('Please answer the question.')
454 return
455 else:
456 callback = self.yesno_callback
457 self.yesno_callback = None
458 callback(answer)
459 return
460
461 if self.is_dead:
462 self.write('You have gotten yourself killed.')
463 return
464
465 #2608
466 self.turns += 1
467 if (self.treasures_not_found == 0
468 and self.loc.n >= 15 and self.loc.n != 33):
469 self.clock1 -= 1
470 if self.clock1 == 0:
471 self.start_closing_cave() # no "return", to do their command
472 if self.clock1 < 0:
473 self.clock2 -= 1
474 if self.clock2 == 0:
475 return self.close_cave() # "return", to cancel their command
476
477 if self.lamp.prop == 1:
478 self.lamp_turns -= 1
479
480 if self.lamp_turns <= 30 and self.is_here(self.batteries) \
481 and self.batteries.prop == 0 and self.is_here(self.lamp):
482 #12000
483 self.write_message(188)
484 self.batteries.prop = 1
485 if self.batteries.is_toting:
486 self.batteries.drop(self.loc)
487 self.lamp_turns += 2500
488 self.warned_about_dim_lamp = False
489 elif self.lamp_turns == 0:
490 #12400
491 self.lamp_turns = -1
492 self.lamp.prop = 0
493 if self.is_here(self.lamp):
494 self.write_message(184)
495 elif self.lamp_turns < 0 and self.loc.is_aboveground:
496 #12600
497 self.write_message(185)
498 self.gave_up = True
499 self.score_and_exit()
500 return
501 elif self.lamp_turns <= 30 and not self.warned_about_dim_lamp \
502 and self.is_here(self.lamp):
503 #12200
504 self.warned_about_dim_lamp = True
505 if self.batteries.prop == 1:
506 self.write_message(189)
507 elif not self.batteries.rooms:
508 self.write_message(183)
509 else:
510 self.write_message(187)
511
512 self.dispatch_command(words)
513
514 def dispatch_command(self, words): #19999
515
516 if not 1 <= len(words) <= 2:
517 return self.dont_understand()
518
519 if words[0] == 'save' and len(words) > 1:
520 # Handle suspend separately, since filename can be anything,
521 # and is not restricted to being a vocabulary word (and, in
522 # fact, it can be an open file).
523 return self.t_suspend(words[0], words[1])
524
525 words = [ self.vocabulary.get(word) for word in words ]
526 if None in words:
527 return self.dont_understand()
528
529 word1 = words[0]
530 word2 = words[1] if len(words) == 2 else None
531
532 if word1 == 'enter' and (word2 == 'stream' or word2 == 'water'):
533 if self.loc.liquid is self.water:
534 self.write_message(70)
535 else:
536 self.write_message(43)
537 return self.finish_turn()
538
539 if (word1 == 'enter' or word1 == 'walk') and word2:
540 #2800 'enter house' becomes simply 'house' and so forth
541 word1, word2 = word2, None
542
543 if ((word1 == 'water' or word1 == 'oil') and
544 (word2 == 'plant' or word2 == 'door') and
545 self.is_here(self.referent(word2))):
546 word1, word2 = self.vocabulary['pour'], word1
547
548 if word1 == 'say':
549 return self.t_say(word1, word2) if word2 else self.i_say(word1)
550
551 if word2 == 'say':
552 return self.t_say(word2, word1)
553
554 kinds = (word1.kind, word2.kind if word2 else None)
555
556 #2630
557 if kinds == ('travel', None):
558 if word1.text == 'west': #2610
559 self.full_wests += 1
560 if self.full_wests == 10:
561 self.write_message(17)
562 return self.do_motion(word1)
563
564 if kinds == ('snappy_comeback', None):
565 self.write_message(word1.n % 1000)
566 return self.finish_turn()
567
568 if kinds == ('noun', None):
569 verb, noun = None, word1
570 elif kinds == ('verb', None):
571 verb, noun = word1, None
572 elif kinds == ('verb', 'noun'):
573 verb, noun = word1, word2
574 elif kinds == ('noun', 'verb'):
575 noun, verb = word1, word2
576 else:
577 return self.dont_understand()
578
579 if not noun:
580 obj = None
581 else:
582 obj = self.referent(noun)
583 obj_here = self.is_here(obj)
584 if not obj_here:
585 if obj is self.grate:
586 if self.loc.n in (1, 4, 7):
587 return self.dispatch_command([ 'depression' ])
588 elif 9 < self.loc.n < 15:
589 return self.dispatch_command([ 'entrance' ])
590 elif noun == 'dwarf':
591 obj_here = any( d.room is self.loc for d in self.dwarves )
592 elif obj is self.bottle.contents and self.is_here(self.bottle):
593 obj_here = True
594 elif obj is self.loc.liquid:
595 obj_here = True
596 elif (obj is self.plant and self.is_here(self.plant2)
597 and self.plant2.prop != 0):
598 obj = self.plant2
599 obj_here = True
600 elif obj is self.knife and self.knife_location is self.loc:
601 self.knife_location = None
602 self.write_message(116)
603 return self.finish_turn()
604 elif obj is self.rod and self.is_here(self.rod2):
605 obj = self.rod2
606 obj_here = True
607 elif verb and (verb == 'find' or verb == 'inventory'):
608 obj_here = True # lie; these verbs work for absent objects
609
610 if not obj_here:
611 return self.i_see_no(noun)
612
613 if not verb:
614 self.write('What do you want to do with the {}?\n'.format(
615 noun.text))
616 return self.finish_turn()
617
618 verb_name = verb.synonyms[0].text
619 if obj:
620 method_name = 't_' + verb_name
621 args = (verb, obj)
622 else:
623 method_name = 'i_' + verb_name
624 args = (verb,)
625 method = getattr(self, method_name)
626 method(*args)
627
628 def dont_understand(self):
629 #3000 (a bit earlier than in the Fortran code)
630 n = self.random()
631 if n < 0.20: # 20% of the entire 1.0 range of random()
632 self.write_message(61)
633 elif n < 0.36: # 20% of the remaining 0.8 left
634 self.write_message(13)
635 else:
636 self.write_message(60)
637 self.finish_turn()
638
639 def i_see_no(self, thing):
640 self.write('I see no {} here.\n'.format(getattr(thing, 'text', thing)))
641 self.finish_turn()
642
643 # Motion.
644
645 def do_motion(self, word): #8
646
647 if word == 'null': #2
648 self.move_to()
649 return
650
651 elif word == 'back': #20
652 dest = self.oldloc2 if self.oldloc.is_forced else self.oldloc
653 self.oldloc2, self.oldloc = self.oldloc, self.loc
654 if dest is self.loc:
655 self.write_message(91)
656 self.move_to()
657 return
658 alt = None
659 for move in self.loc.travel_table:
660 if move.action is dest:
661 word = move.verbs[0] # arbitrary verb going to `dest`
662 break # Fall through, to attempt the move.
663 elif (isinstance(move.action, Room)
664 and move.action.is_forced
665 and move.action.travel_table[0].action is dest):
666 alt = move.verbs[0]
667 else: # no direct route is available
668 if alt is not None:
669 word = alt # take a forced move if it's the only option
670 else:
671 self.write_message(140)
672 self.move_to()
673 return
674
675 elif word == 'look': #30
676 if self.look_complaints > 0:
677 self.write_message(15)
678 self.look_complaints -= 1
679 self.loc.times_described = 0
680 self.move_to()
681 self.could_fall_in_pit = False
682 return
683
684 elif word == 'cave': #40
685 self.write_message(57 if self.loc.is_aboveground else 58)
686 self.move_to()
687 return
688
689 self.oldloc2, self.oldloc = self.oldloc, self.loc
690
691 for move in self.loc.travel_table:
692 if move.is_forced or word in move.verbs:
693 c = move.condition
694
695 if c[0] is None or c[0] == 'not_dwarf':
696 allowed = True
697 elif c[0] == '%':
698 allowed = 100 * self.random() < c[1]
699 elif c[0] == 'carrying':
700 allowed = self.objects[c[1]].is_toting
701 elif c[0] == 'carrying_or_in_room_with':
702 allowed = self.is_here(self.objects[c[1]])
703 elif c[0] == 'prop!=':
704 allowed = self.objects[c[1]].prop != c[2]
705
706 if not allowed:
707 continue
708
709 if isinstance(move.action, Room):
710 self.move_to(move.action)
711 return
712
713 elif isinstance(move.action, Message):
714 self.write(move.action)
715 self.move_to()
716 return
717
718 elif move.action == 301: #30100
719 inv = self.inventory
720 if len(inv) != 0 and inv != [ self.emerald ]:
721 self.write_message(117)
722 self.move_to()
723 elif self.loc.n == 100:
724 self.move_to(self.rooms[99])
725 else:
726 self.move_to(self.rooms[100])
727 return
728
729 elif move.action == 302: #30200
730 self.emerald.drop(self.loc)
731 self.do_motion(word)
732 return
733
734 elif move.action == 303: #30300
735 troll, troll2 = self.troll, self.troll2
736 if troll.prop == 1:
737 self.write(troll.messages[1])
738 troll.prop = 0
739 troll.rooms = list(troll.starting_rooms)
740 troll2.destroy()
741 self.move_to()
742 return
743 else:
744 places = list(troll.starting_rooms)
745 places.remove(self.loc)
746 self.loc = places[0] # "the other side of the bridge"
747 if troll.prop == 0:
748 troll.prop = 1
749 if not self.bear.is_toting:
750 self.move_to()
751 return
752 self.write_message(162)
753 self.chasm.prop = 1
754 troll.prop = 2
755 self.bear.drop(self.loc)
756 self.bear.is_fixed = True
757 self.bear.prop = 3
758 if self.spices.prop < 0:
759 self.impossible_treasures += 1
760 self.oldloc2 = self.loc # refuse to strand belongings
761 self.die()
762 return
763
764 #50
765 n = word.n
766 if 29 <= n <= 30 or 43 <= n <= 50:
767 self.write_message(9)
768 elif n in (7, 36, 37):
769 self.write_message(10)
770 elif n in (11, 19):
771 self.write_message(11)
772 elif n in (62, 65):
773 self.write_message(42)
774 elif n == 17:
775 self.write_message(80)
776 else:
777 self.write_message(12)
778 self.move_to()
779 return
780
781 # Death and reincarnation.
782
783 def die_here(self): #90
784 self.write_message(23)
785 self.oldloc2 = self.loc
786 self.die()
787
788 def die(self): #99
789 self.deaths += 1
790 self.is_dead = True
791
792 if self.is_closing:
793 self.write_message(131)
794 self.score_and_exit()
795 return
796
797 def callback(yes):
798 if yes:
799 self.write_message(80 + self.deaths * 2)
800 if self.deaths < self.max_deaths:
801 if self.bottle.contents is not None:
802 self.bottle.contents.hide()
803 self.is_dead = False
804 if self.lamp.is_toting:
805 self.lamp.prop = 0
806 for obj in self.inventory:
807 if obj is self.lamp:
808 obj.drop(self.rooms[1])
809 else:
810 obj.drop(self.oldloc2)
811 self.loc = self.rooms[3]
812 self.describe_location()
813 return
814 else:
815 self.write_message(54)
816 self.score_and_exit()
817
818 self.yesno(self.messages[79 + self.deaths * 2], callback)
819
820 # Verbs.
821
822 def ask_verb_what(self, verb, *args): #8000
823 self.write('{} What?\n'.format(verb.text))
824 self.finish_turn()
825
826 i_walk = ask_verb_what
827 i_drop = ask_verb_what
828 i_say = ask_verb_what
829 i_nothing = say_okay_and_finish
830 i_wave = ask_verb_what
831 i_calm = ask_verb_what
832 i_rub = ask_verb_what
833 i_throw = ask_verb_what
834 i_find = ask_verb_what
835 i_feed = ask_verb_what
836 i_break = ask_verb_what
837 i_wake = ask_verb_what
838
839 def write_default_message(self, verb, *args):
840 self.write(verb.default_message)
841 self.finish_turn()
842
843 t_nothing = say_okay_and_finish
844 t_calm = write_default_message
845 t_quit = write_default_message
846 t_score = write_default_message
847 t_fee = write_default_message
848 t_brief = write_default_message
849 t_hours = write_default_message
850 t_walk = write_default_message
851
852 def i_carry(self, verb): #8010
853 is_dwarf_here = any( dwarf.room == self.loc for dwarf in self.dwarves )
854 objs = self.objects_here
855 if len(objs) != 1 or is_dwarf_here:
856 self.ask_verb_what(verb)
857 else:
858 self.t_carry(verb, objs[0])
859
860 def t_carry(self, verb, obj): #9010
861 if obj.is_toting:
862 self.write(verb.default_message)
863 self.finish_turn()
864 return
865 if obj.is_fixed or len(obj.rooms) > 1:
866 if obj is self.plant and obj.prop <= 0:
867 self.write_message(115)
868 elif obj is self.bear and obj.prop == 1:
869 self.write_message(169)
870 elif obj is self.chain and self.chain.prop != 0:
871 self.write_message(170)
872 else:
873 self.write_message(25)
874 self.finish_turn()
875 return
876 if obj is self.water or obj is self.oil:
877 if self.is_here(self.bottle) and self.bottle.contents is obj:
878 # They want to carry the filled bottle.
879 obj = self.bottle
880 else:
881 # They must mean they want to fill the bottle.
882 if not self.bottle.is_toting:
883 self.write_message(104)
884 elif self.bottle.contents is not None:
885 self.write_message(105)
886 else:
887 self.t_fill(verb, self.bottle) # hand off control to "fill"
888 return
889 self.finish_turn()
890 return
891 if len(self.inventory) >= 7:
892 self.write_message(92)
893 self.finish_turn()
894 return
895 if obj is self.bird and obj.prop == 0:
896 if self.rod.is_toting:
897 self.write_message(26)
898 self.finish_turn(obj) # needs obj to decide to give hint
899 return
900 if not self.cage.is_toting:
901 self.write_message(27)
902 self.finish_turn()
903 return
904 self.bird.prop = 1
905 if (obj is self.bird or obj is self.cage) and self.bird.prop != 0:
906 self.bird.carry()
907 self.cage.carry()
908 else:
909 obj.carry()
910 if obj is self.bottle and self.bottle.contents is not None:
911 self.bottle.contents.carry()
912 self.say_okay_and_finish()
913
914 def t_drop(self, verb, obj): #9020
915 if obj is self.rod and not self.rod.is_toting and self.rod2.is_toting:
916 obj = self.rod2
917
918 if not obj.is_toting:
919 self.write(verb.default_message)
920 self.finish_turn()
921 return
922
923 bird, snake, dragon, bear, troll = self.bird, self.snake, self.dragon, \
924 self.bear, self.troll
925
926 if obj is bird and self.is_here(snake):
927 self.write_message(30)
928 if self.is_closed:
929 self.wake_repository_dwarves()
930 return
931 snake.prop = 1
932 snake.destroy()
933
934 elif obj is self.coins and self.is_here(self.machine):
935 obj.destroy()
936 self.batteries.drop(self.loc)
937 self.write(self.batteries.messages[0])
938 self.finish_turn()
939 return
940
941 elif obj is bird and self.is_here(dragon) and dragon.prop == 0:
942 self.write_message(154)
943 bird.destroy()
944 bird.prop = 0
945 if snake.rooms:
946 self.impossible_treasures += 1
947 self.finish_turn()
948 return
949
950 elif obj is bear and self.is_here(troll):
951 self.write_message(163)
952 troll.destroy()
953 self.troll2.rooms = list(self.troll.starting_rooms)
954 troll.prop = 2
955
956 elif obj is self.vase and self.loc is not self.rooms[96]:
957 if self.pillow.is_at(self.loc):
958 self.vase.prop = 0
959 else:
960 self.vase.prop = 2
961 self.vase.is_fixed = True
962 self.write(self.vase.messages[self.vase.prop + 1])
963
964 else:
965 self.write_message(54)
966
967 #9021
968 if obj is self.bottle.contents:
969 obj = self.bottle
970 if obj is self.bottle and self.bottle.contents:
971 self.bottle.contents.hide()
972 if obj is self.cage and self.bird.prop != 0:
973 bird.drop(self.loc)
974 elif obj is self.bird:
975 obj.prop = 0
976 obj.drop(self.loc)
977 self.finish_turn()
978 return
979
980 def t_say(self, verb, word): #9030
981 if word.n in (62, 65, 71, 2025):
982 self.dispatch_command([ word.text ])
983 else:
984 self.write('Okay, "{}".'.format(word.text))
985 self.finish_turn()
986
987 def i_unlock(self, verb): #8040 Handles "unlock" case as well
988 objs = (self.grate, self.door, self.oyster, self.clam, self.chain)
989 objs = list(filter(self.is_here, objs))
990 if len(objs) > 1:
991 self.ask_verb_what(verb)
992 elif len(objs) == 1:
993 self.t_unlock(verb, objs[0])
994 else:
995 self.write_message(28)
996 self.finish_turn()
997
998 i_lock = i_unlock
999
1000 def t_unlock(self, verb, obj): #9040 Handles "lock" case as well
1001 if obj is self.clam or obj is self.oyster:
1002 #9046
1003 oy = 1 if (obj is self.oyster) else 0
1004 if verb == 'lock':
1005 self.write_message(61)
1006 elif not self.trident.is_toting:
1007 self.write_message(122 + oy)
1008 elif obj.is_toting:
1009 self.write_message(120 + oy)
1010 elif obj is self.oyster:
1011 self.write_message(125)
1012 else:
1013 self.write_message(124)
1014 self.clam.destroy()
1015 self.oyster.drop(self.loc)
1016 self.pearl.drop(self.rooms[105])
1017 elif obj is self.door:
1018 if obj.prop == 1:
1019 self.write_message(54)
1020 else:
1021 self.write_message(111)
1022 elif obj is self.cage:
1023 self.write_message(32)
1024 elif obj is self.keys:
1025 self.write_message(55)
1026 elif obj is self.grate or obj is self.chain:
1027 if not self.is_here(self.keys):
1028 self.write_message(31)
1029 elif obj is self.chain:
1030 #9048
1031 if verb == 'unlock':
1032 if self.chain.prop == 0:
1033 self.write_message(37)
1034 elif self.bear.prop == 0:
1035 self.write_message(41)
1036 else:
1037 self.chain.prop = 0
1038 self.chain.is_fixed = False
1039 if self.bear.prop != 3:
1040 self.bear.prop = 2
1041 self.bear.is_fixed = 2 - self.bear.prop
1042 self.write_message(171)
1043 else:
1044 #9049
1045 if self.loc not in self.chain.starting_rooms:
1046 self.write_message(173)
1047 elif self.chain.prop != 0:
1048 self.write_message(34)
1049 else:
1050 self.chain.prop = 2
1051 if self.chain.is_toting:
1052 self.chain.drop(self.loc)
1053 self.chain.is_fixed = True
1054 self.write_message(172)
1055 elif self.is_closing:
1056 if not self.panic:
1057 self.clock2 = 15
1058 self.panic = True
1059 self.write_message(130)
1060 else:
1061 #9043
1062 oldprop = obj.prop
1063 obj.prop = 0 if verb == 'lock' else 1
1064 self.write_message(34 + oldprop + 2 * obj.prop)
1065 else:
1066 self.write(verb.default_message)
1067 self.finish_turn()
1068
1069 t_lock = t_unlock
1070
1071 def t_light(self, verb, obj=None): #9070
1072 if not self.is_here(self.lamp):
1073 self.write(verb.default_message)
1074 elif self.lamp_turns <= 0:
1075 self.write_message(184)
1076 else:
1077 self.lamp.prop = 1
1078 self.write_message(39)
1079 if self.loc.is_dark:
1080 return self.describe_location()
1081 self.finish_turn()
1082
1083 i_light = t_light
1084
1085 def t_extinguish(self, verb, obj=None): #9080
1086 if not self.is_here(self.lamp):
1087 self.write(verb.default_message)
1088 else:
1089 self.lamp.prop = 0
1090 self.write_message(40)
1091 if self.loc.is_dark:
1092 self.write_message(16)
1093 self.finish_turn()
1094
1095 i_extinguish = t_extinguish
1096
1097 def t_wave(self, verb, obj): #9090
1098 fissure = self.fissure
1099
1100 if (obj is self.rod and obj.is_toting and self.is_here(fissure)
1101 and not self.is_closing):
1102 fissure.prop = 0 if fissure.prop else 1
1103 self.write(fissure.messages[2 - fissure.prop])
1104 else:
1105 if obj.is_toting or (obj is self.rod and self.rod2.is_toting):
1106 self.write(verb.default_message)
1107 else:
1108 self.write_message(29)
1109
1110 self.finish_turn()
1111
1112 def i_attack(self, verb): #9120
1113 enemies = [ self.snake, self.dragon, self.troll, self.bear ]
1114 if self.dwarf_stage >= 2:
1115 enemies.extend(self.dwarves)
1116 dangers = list(filter(self.is_here, enemies))
1117 if len(dangers) > 1:
1118 return self.ask_verb_what(verb)
1119 if len(dangers) == 1:
1120 return self.t_attack(verb, dangers[0])
1121 targets = []
1122 if self.is_here(self.bird) and verb != 'throw':
1123 targets.append(self.bird)
1124 if self.is_here(self.clam) or self.is_here(self.oyster):
1125 targets.append(self.clam)
1126 if len(targets) > 1:
1127 return self.ask_verb_what(verb)
1128 elif len(targets) == 1:
1129 return self.t_attack(verb, targets[0])
1130 else:
1131 return self.t_attack(verb, None)
1132
1133 def t_attack(self, verb, obj): #9124 (but control goes to 9120 first)
1134 if obj is self.bird:
1135 if self.is_closed:
1136 self.write_message(137)
1137 else:
1138 obj.destroy()
1139 obj.prop = 0
1140 if self.snake.rooms:
1141 self.impossible_treasures += 1
1142 self.write_message(45)
1143 elif obj is self.clam or obj is self.oyster:
1144 self.write_message(150)
1145 elif obj is self.snake:
1146 self.write_message(46)
1147 elif obj is self.dwarf:
1148 if self.is_closed:
1149 self.wake_repository_dwarves()
1150 return
1151 self.write_message(49)
1152 elif obj is self.dragon:
1153 if self.dragon.prop != 0:
1154 self.write_message(167)
1155 else:
1156 def callback(yes):
1157 self.write(obj.messages[1])
1158 obj.prop = 2
1159 obj.is_fixed = True
1160 oldroom1 = obj.rooms[0]
1161 oldroom2 = obj.rooms[1]
1162 newroom = self.rooms[ (oldroom1.n + oldroom2.n) // 2 ]
1163 obj.drop(newroom)
1164 self.rug.prop = 0
1165 self.rug.is_fixed = False
1166 self.rug.drop(newroom)
1167 for oldroom in (oldroom1, oldroom2):
1168 for o in self.objects_at(oldroom):
1169 o.drop(newroom)
1170 self.move_to(newroom)
1171 self.yesno(self.messages[49], callback, casual=True)
1172 return
1173 elif obj is self.troll:
1174 self.write_message(157)
1175 elif obj is self.bear:
1176 self.write_message(165 + (self.bear.prop + 1) // 2)
1177 else:
1178 self.write_message(44)
1179 self.finish_turn()
1180
1181 def i_pour(self, verb): #9130
1182 if self.bottle.contents is None:
1183 self.ask_verb_what(verb)
1184 else:
1185 self.t_pour(verb, self.bottle.contents)
1186
1187 def t_pour(self, verb, obj):
1188 if obj is self.bottle:
1189 return self.i_pour(verb)
1190 if not obj.is_toting:
1191 self.write(verb.default_message)
1192 elif obj is not self.oil and obj is not self.water:
1193 self.write_message(78)
1194 else:
1195 self.bottle.prop = 1
1196 self.bottle.contents = None
1197 obj.hide()
1198 if self.is_here(self.plant):
1199 if obj is not self.water:
1200 self.write_message(112)
1201 else:
1202 self.write(self.plant.messages[self.plant.prop + 1])
1203 self.plant.prop = (self.plant.prop + 2) % 6
1204 self.plant2.prop = self.plant.prop // 2
1205 return self.move_to()
1206 elif self.is_here(self.door):
1207 #9132
1208 self.door.prop = 1 if obj is self.oil else 0
1209 self.write_message(113 + self.door.prop)
1210 else:
1211 self.write_message(77)
1212 return self.finish_turn()
1213
1214 def i_eat(self, verb): #8140
1215 if self.is_here(self.food):
1216 self.t_eat(verb, self.food)
1217 else:
1218 self.ask_verb_what(verb)
1219
1220 def t_eat(self, verb, obj): #9140
1221 if obj is self.food:
1222 #8142
1223 self.food.destroy()
1224 self.write_message(72)
1225 elif obj in (self.bird, self.snake, self.clam, self.oyster,
1226 self.dwarf, self.dragon, self.troll, self.bear):
1227 self.write_message(71)
1228 else:
1229 self.write(verb.default_message)
1230 self.finish_turn()
1231
1232 def i_drink(self, verb): #9150
1233 if self.is_here(self.water) or self.loc.liquid is self.water:
1234 self.t_drink(verb, self.water)
1235 else:
1236 self.ask_verb_what(verb)
1237
1238 def t_drink(self, verb, obj): #9150
1239 if obj is not self.water:
1240 self.write_message(110)
1241 elif self.is_here(self.water):
1242 self.bottle.prop = 1
1243 self.bottle.contents = None
1244 self.water.destroy()
1245 self.write_message(74)
1246 elif self.loc.liquid is self.water:
1247 self.write(verb.default_message)
1248 self.finish_turn()
1249
1250 def t_rub(self, verb, obj): #9160
1251 if obj is self.lamp:
1252 self.write(verb.default_message)
1253 else:
1254 self.write_message(71)
1255 self.finish_turn()
1256
1257 def t_throw(self, verb, obj): #9170
1258 if obj is self.rod and not self.rod.is_toting and self.rod2.is_toting:
1259 obj = self.rod2
1260
1261 if not obj.is_toting:
1262 self.write(verb.default_message)
1263 self.finish_turn()
1264 return
1265
1266 if obj.is_treasure and self.is_here(self.troll):
1267 # Pay the troll toll
1268 self.write_message(159)
1269 obj.destroy()
1270 self.troll.destroy()
1271 self.troll2.rooms = list(self.troll.starting_rooms)
1272 self.finish_turn()
1273 return
1274
1275 if obj is self.food and self.is_here(self.bear):
1276 self.t_feed(verb, self.bear)
1277 return
1278
1279 if obj is not self.axe:
1280 self.t_drop(verb, obj)
1281 return
1282
1283 dwarves_here = [ d for d in self.dwarves if d.room is self.loc ]
1284 if dwarves_here:
1285 # 1/3rd chance that throwing the axe kills a dwarf
1286 if self.choice((True, False, False)):
1287 self.dwarves.remove(dwarves_here[0])
1288 self.dwarves_killed += 1
1289 if self.dwarves_killed == 1:
1290 self.write_message(149)
1291 else:
1292 self.write_message(47)
1293 else:
1294 self.write_message(48) # Miss
1295 self.axe.drop(self.loc)
1296 self.do_motion(self.vocabulary['null'])
1297 return
1298
1299 if self.is_here(self.dragon) and self.dragon.prop == 0:
1300 self.write_message(152)
1301 self.axe.drop(self.loc)
1302 self.do_motion(self.vocabulary['null'])
1303 return
1304
1305 if self.is_here(self.troll):
1306 self.write_message(158)
1307 self.axe.drop(self.loc)
1308 self.do_motion(self.vocabulary['null'])
1309 return
1310
1311 if self.is_here(self.bear) and self.bear.prop == 0:
1312 self.write_message(164)
1313 self.axe.drop(self.loc)
1314 self.axe.is_fixed = True
1315 self.axe.prop = 1
1316 self.finish_turn()
1317 return
1318
1319 self.t_attack(verb, None)
1320
1321 def i_quit(self, verb): #8180
1322 def callback(yes):
1323 self.write_message(54)
1324 if yes:
1325 self.score_and_exit()
1326 self.yesno(self.messages[22], callback)
1327
1328 def t_find(self, verb, obj): #9190
1329 if obj.is_toting:
1330 self.write_message(24)
1331 elif self.is_closed:
1332 self.write_message(138)
1333 elif (self.is_here(obj) or
1334 obj is self.loc.liquid or
1335 obj is self.dwarf and any(d.room is self.loc for d in self.dwarves)):
1336 self.write_message(94)
1337 else:
1338 self.write(verb.default_message)
1339 self.finish_turn()
1340
1341 t_inventory = t_find
1342
1343 def i_inventory(self, verb): #8200
1344 first = True
1345 objs = [ obj for obj in self.inventory if obj is not self.bear ]
1346 for obj in objs:
1347 if first:
1348 self.write_message(99)
1349 first = False
1350 self.write(obj.inventory_message)
1351 if self.bear.is_toting:
1352 self.write_message(141)
1353 if not objs:
1354 self.write_message(98)
1355 self.finish_turn()
1356
1357 def t_feed(self, verb, obj): #9210
1358 if obj is self.bird:
1359 self.write_message(100)
1360 elif obj is self.troll:
1361 self.write_message(182)
1362 elif obj is self.dragon:
1363 if self.dragon.prop != 0:
1364 self.write_message(110)
1365 else:
1366 self.write_message(102)
1367 elif obj is self.snake:
1368 if self.is_closed or not self.is_here(self.bird):
1369 self.write_message(102)
1370 else:
1371 self.write_message(101)
1372 self.bird.destroy()
1373 self.bird.prop = 0
1374 self.impossible_treasures += 1
1375 elif obj is self.dwarf:
1376 if self.is_here(self.food):
1377 self.write_message(103)
1378 self.dwarf_stage += 1
1379 else:
1380 self.write(verb.default_message)
1381 elif obj is self.bear:
1382 if not self.is_here(self.food):
1383 if self.bear.prop == 0:
1384 self.write_message(102)
1385 elif self.bear.prop == 3:
1386 self.write_message(110)
1387 else:
1388 self.write(verb.default_message)
1389 else:
1390 self.food.destroy()
1391 self.bear.prop = 1
1392 self.axe.is_fixed = False
1393 self.axe.prop = 0
1394 self.write_message(168)
1395 else:
1396 self.write_message(14)
1397 self.finish_turn()
1398
1399 def i_fill(self, verb): #9220
1400 if self.is_here(self.bottle):
1401 return self.t_fill(verb, self.bottle)
1402 self.ask_verb_what(verb)
1403
1404 def t_fill(self, verb, obj):
1405 if obj is self.bottle:
1406 liquid = self.loc.liquid
1407 if liquid is None:
1408 self.write_message(106)
1409 elif self.bottle.contents:
1410 self.write_message(105)
1411 else:
1412 self.bottle.contents = liquid
1413 self.bottle.prop = 0 if (liquid is self.water) else 2
1414 if self.bottle.is_toting:
1415 liquid.is_toting = True
1416 if liquid is self.oil:
1417 self.write_message(108)
1418 else:
1419 self.write_message(107)
1420 elif obj is self.vase:
1421 #9222
1422 if self.vase.is_toting:
1423 if self.loc.liquid is None:
1424 self.write_message(144)
1425 else:
1426 self.write_message(145)
1427 self.vase.drop(self.loc)
1428 self.vase.prop = 2
1429 self.vase.is_fixed = True
1430 else:
1431 self.write(verb.default_message)
1432 else:
1433 self.write(verb.default_message)
1434 self.finish_turn()
1435
1436 def t_blast(self, verb, obj=None): #9230
1437 if self.rod2.prop < 0 or not self.is_closed:
1438 self.write(verb.default_message)
1439 self.finish_turn()
1440 return
1441 if self.is_here(self.rod2):
1442 self.bonus = 135
1443 elif self.loc.n == 115:
1444 self.bonus = 134
1445 else:
1446 self.bonus = 133
1447 self.write_message(self.bonus)
1448 self.score_and_exit()
1449
1450 i_blast = t_blast
1451
1452 def i_score(self, verb): #8240
1453 score, max_score = self.compute_score(for_score_command=True)
1454 self.write('If you were to quit now, you would score {}'
1455 ' out of a possible {}.\n'.format(score, max_score))
1456 def callback(yes):
1457 self.write_message(54)
1458 if yes:
1459 self.score_and_exit()
1460 self.yesno(self.messages[143], callback)
1461
1462 def i_fee(self, verb): #8250
1463 for n in range(5):
1464 if verb.synonyms[n].text == verb.text:
1465 break # so that 0=fee, 1=fie, 2=foe, 3=foo, 4=fum
1466 if n == 0:
1467 self.foobar = self.turns
1468 self.write_message(54)
1469 elif n != self.turns - self.foobar:
1470 self.write_message(151)
1471 elif n < 3:
1472 self.write_message(54)
1473 else:
1474 self.foobar = -1
1475 eggs = self.eggs
1476 start = eggs.starting_rooms[0]
1477 if (eggs.is_at(start) or eggs.is_toting and self.loc is start):
1478 self.write_message(54)
1479 else:
1480 troll = self.troll
1481 if not eggs.rooms and not troll.rooms and not troll.prop:
1482 self.troll.prop = 1
1483 if self.loc is start:
1484 self.write(eggs.messages[0])
1485 elif self.is_here(eggs):
1486 self.write(eggs.messages[1])
1487 else:
1488 self.write(eggs.messages[2])
1489 eggs.rooms = list(eggs.starting_rooms)
1490 eggs.is_toting = False
1491 self.finish_turn()
1492
1493 def i_brief(self, verb): #8260
1494 self.write_message(156)
1495 self.full_description_period = 10000
1496 self.look_complaints = 0
1497 self.finish_turn()
1498
1499 def i_read(self, verb): #8270
1500 if self.is_closed and self.oyster.is_toting:
1501 return self.t_read(verb, self.oyster)
1502 objs = (self.magazine, self.tablet, self.message)
1503 objs = list(filter(self.is_here, objs))
1504 if len(objs) != 1 or self.is_dark:
1505 self.ask_verb_what(verb)
1506 else:
1507 self.t_read(verb, objs[0])
1508
1509 def t_read(self, verb, obj): #9270
1510 if self.is_dark:
1511 return self.i_see_no(obj.names[0])
1512 elif (obj is self.oyster and not self.hints[2].used and
1513 self.oyster.is_toting):
1514 def callback(yes):
1515 if yes:
1516 self.hints[2].used = True
1517 self.write_message(193)
1518 else:
1519 self.write_message(54)
1520 self.yesno(self.messages[192], callback)
1521 elif obj is self.oyster and self.hints[2].used:
1522 self.write_message(194)
1523 elif obj is self.message:
1524 self.write_message(191)
1525 elif obj is self.tablet:
1526 self.write_message(196)
1527 elif obj is self.magazine:
1528 self.write_message(190)
1529 else:
1530 self.write(verb.default_message)
1531 self.finish_turn()
1532
1533 def t_break(self, verb, obj): #9280
1534 if obj is self.vase and self.vase.prop == 0:
1535 self.write_message(198)
1536 if self.vase.is_toting:
1537 self.vase.drop(self.loc)
1538 self.vase.prop = 2
1539 self.vase.is_fixed = True
1540 elif obj is self.mirror and self.is_closed:
1541 self.write_message(197)
1542 self.wake_repository_dwarves()
1543 return
1544 elif obj is self.mirror:
1545 self.write_message(148)
1546 else:
1547 self.write(verb.default_message)
1548 self.finish_turn()
1549
1550 def t_wake(self, verb, obj): #9290
1551 if obj is self.dwarf and self.is_closed:
1552 self.write_message(199)
1553 self.wake_repository_dwarves()
1554 else:
1555 self.write(verb.default_message)
1556 self.finish_turn()
1557
1558 def i_suspend(self, verb):
1559 self.write('Provide "{}" with a filename or open file'.format(
1560 verb.text))
1561 self.finish_turn()
1562
1563 def t_suspend(self, verb, obj):
1564 if isinstance(obj, str):
1565 if os.path.exists(obj): # pragma: no cover
1566 self.write('I refuse to overwrite an existing file.')
1567 return
1568 savefile = open(obj, 'wb')
1569 else:
1570 savefile = obj
1571 r = self.random_generator # must replace live object with static state
1572 self.random_state = r.getstate()
1573 try:
1574 del self.random_generator
1575 savefile.write(zlib.compress(pickle.dumps(self), 9))
1576 finally:
1577 self.random_generator = r
1578 if savefile is not obj:
1579 savefile.close()
1580 self.write('Game saved')
1581
1582 def i_hours(self, verb):
1583 self.write('Open all day')
1584
1585 @classmethod
1586 def resume(self, obj):
1587 """Returns an Adventure game saved to the given file."""
1588 if isinstance(obj, str):
1589 savefile = open(obj, 'rb')
1590 else:
1591 savefile = obj
1592 game = pickle.loads(zlib.decompress(savefile.read()))
1593 if savefile is not obj:
1594 savefile.close()
1595 # Reinstate the random number generator.
1596 game.random_generator = random.Random()
1597 game.random_generator.setstate(game.random_state)
1598 del game.random_state
1599 return game
1600
1601 def should_offer_hint(self, hint, obj): #40000
1602 if hint.n == 4: # cave
1603 return self.grate.prop == 0 and not self.is_here(self.keys)
1604
1605 elif hint.n == 5: # bird
1606 bird = self.bird
1607 return self.is_here(bird) and self.rod.is_toting and obj is bird
1608
1609 elif hint.n == 6: # snake
1610 return self.is_here(self.snake) and not self.is_here(self.bird)
1611
1612 elif hint.n == 7: # maze
1613 return (not len(self.objects_here) and
1614 not len(self.objects_at(self.oldloc)) and
1615 not len(self.objects_at(self.oldloc2)) and
1616 len(self.inventory) > 1)
1617
1618 elif hint.n == 8: # dark
1619 return self.emerald.prop != 1 and self.platinum.prop != 1
1620
1621 elif hint.n == 9: # witt
1622 return True
1623
1624 def start_closing_cave(self): #10000
1625 self.grate.prop = 0
1626 self.fissure.prop = 0
1627 del self.dwarves[:]
1628 self.troll.destroy()
1629 self.troll2.rooms = list(self.troll.starting_rooms)
1630 if self.bear.prop != 3:
1631 self.bear.destroy()
1632 for obj in self.chain, self.axe:
1633 obj.prop = 0
1634 obj.is_fixed = False
1635 self.write_message(129)
1636 self.clock1 = -1
1637 self.is_closing = True
1638
1639 def close_cave(self): #11000
1640 ne = self.rooms[115] # ne end of repository
1641 sw = self.rooms[116]
1642 for obj in (self.bottle, self.plant, self.oyster, self.lamp,
1643 self.rod, self.dwarf):
1644 obj.prop = -2 if obj is self.bottle else -1
1645 obj.drop(ne)
1646 self.loc = self.oldloc = self.oldloc2 = ne
1647 for obj in (self.grate, self.snake, self.bird, self.cage,
1648 self.rod2, self.pillow):
1649 obj.prop = -2 if (obj is self.bird or obj is self.snake) else -1
1650 obj.drop(sw)
1651 self.mirror.rooms = [ne, sw]
1652 self.mirror.is_fixed = 1
1653 self.is_closed = True
1654 for obj in self.inventory:
1655 obj.is_toting = False
1656 self.write_message(132)
1657 self.move_to()
1658
1659 # TODO: 12000
1660 # TODO: 12200
1661 # TODO: 12400
1662 # TODO: 12600
1663
1664 def wake_repository_dwarves(self): #19000
1665 self.write_message(136)
1666 self.score_and_exit()
1667
1668 def compute_score(self, for_score_command=False): #20000
1669 score = maxscore = 2
1670
1671 for treasure in self.treasures:
1672 # if ptext(0) is zero?
1673 if treasure.n > self.chest.n:
1674 value = 16
1675 elif treasure is self.chest:
1676 value = 14
1677 else:
1678 value = 12
1679
1680 maxscore += value
1681
1682 if treasure.prop >= 0:
1683 score += 2
1684 if treasure.rooms and treasure.rooms[0].n == 3 \
1685 and treasure.prop == 0:
1686 score += value - 2
1687
1688 maxscore += self.max_deaths * 10
1689 score += (self.max_deaths - self.deaths) * 10
1690
1691 maxscore += 4
1692 if not for_score_command and not self.gave_up:
1693 score += 4
1694
1695 maxscore += 25
1696 if self.dwarf_stage:
1697 score += 25
1698
1699 maxscore += 25
1700 if self.is_closing:
1701 score += 25
1702
1703 maxscore += 45
1704 if self.is_closed:
1705 score += {0: 10, 135: 25, 134: 30, 133: 45}[self.bonus]
1706
1707 maxscore += 1
1708 if 108 in (room.n for room in self.magazine.rooms):
1709 score += 1
1710
1711 for hint in list(self.hints.values()):
1712 if hint.used:
1713 score -= hint.penalty
1714
1715 return score, maxscore
1716
1717 def score_and_exit(self):
1718 score, maxscore = self.compute_score()
1719 self.write('\nYou scored {} out of a possible {} using {} turns.'
1720 .format(score, maxscore, self.turns))
1721 for i, (minimum, text) in enumerate(self.class_messages):
1722 if minimum >= score:
1723 break
1724 self.write('\n{}\n'.format(text))
1725 if i < len(self.class_messages) - 1:
1726 d = self.class_messages[i+1][0] + 1 - score
1727 self.write('To achieve the next higher rating, you need'
1728 ' {} more point{}\n'.format(d, 's' if d > 1 else ''))
1729 else:
1730 self.write('To achieve the next higher rating '
1731 'would be a neat trick!\n\nCongratulations!!\n')
1732 self.is_done = True