Debian Codegraph @

adventure/model.py

raw
1 """Classes representing Adventure game components.
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 class Move(object):
8 """An entry in the travel table."""
9
10 is_forced = False
11 verbs = []
12 condition = None
13 action = None
14
15 def __repr__(self):
16 verblist = [ verb.text for verb in self.verbs ]
17
18 c = self.condition[0]
19 if c is None:
20 condition = ''
21 elif c == '%':
22 condition = ' %d%% of the time' % self.condition[1]
23 elif c == 'not_dwarf':
24 condition = ' if not a dwarf'
25 elif c == 'carrying':
26 condition = ' if carrying %s' % self.condition[1]
27 elif c == 'carrying_or_in_room_with':
28 condition = ' if carrying or in room with %s' % self.condition[1]
29 elif c == 'prop!=':
30 condition = ' if prop %d != %d' % self.condition[1:]
31
32 if isinstance(self.action, Room):
33 action = 'moves to %r' % (self.action.short_description
34 or self.action.long_description[:20]).strip()
35 elif isinstance(self.action, Message):
36 action = 'prints %r' % self.action.text
37 else:
38 action = 'special %d' % self.action
39
40 return '<{}{} {}>'.format('|'.join(verblist), condition, action)
41
42 class Room(object):
43 """A location in the game."""
44
45 long_description = ''
46 short_description = ''
47 times_described = 0
48 visited = False
49
50 is_light = False
51 is_forbidden_to_pirate = False
52 liquid = None
53 trying_to_get_into_cave = False
54 trying_to_catch_bird = False
55 trying_to_deal_with_snake = False
56 lost_in_maze = False
57 pondering_dark_room = False
58 at_witts_end = False
59
60 def __init__(self):
61 self.travel_table = []
62
63 def __repr__(self):
64 return '<room {} at {}>'.format(self.n, hex(id(self)))
65
66 @property
67 def is_forced(self):
68 return self.travel_table and self.travel_table[0].is_forced
69
70 @property
71 def is_aboveground(self):
72 return 1 <= self.n <= 8
73
74 @property
75 def is_before_hall_of_mists(self):
76 return self.n < 15
77
78 @property
79 def is_after_hall_of_mists(self):
80 return self.n >= 15
81
82 @property
83 def is_dark(self):
84 return not self.is_light
85
86 class Word(object):
87 """A word that can be used as part of a command."""
88
89 text = None
90 kind = None
91 default_message = None
92
93 def __init__(self):
94 self.synonyms = [ self ]
95
96 def __repr__(self):
97 return '<Word {}>'.format(self.text)
98
99 def __eq__(self, text):
100 return any( word.text == text for word in self.synonyms )
101
102 def add_synonym(self, other):
103 """Every word in a group of synonyms shares the same list."""
104 self.synonyms.extend(other.synonyms)
105 other.synonyms = self.synonyms
106
107 class Object(object):
108 """An object in the game, like a grate, or a rod with a rusty star."""
109
110 def __init__(self):
111 self.is_fixed = False
112 self.is_treasure = False
113 self.inventory_message = ''
114 self.messages = {}
115 self.names = []
116 self.prop = 0
117 self.rooms = []
118 self.starting_rooms = []
119 self.is_toting = False
120 self.contents = None # so the bottle can hold things
121
122 def __repr__(self):
123 return '<Object %d %s %x>' % (self.n, '/'.join(self.names), id(self))
124
125 def __hash__(self):
126 return self.n
127
128 def __eq__(self, other):
129 return any( text == other for text in self.names )
130
131 def is_at(self, room):
132 return room in self.rooms
133
134 def carry(self):
135 self.rooms[:] = []
136 self.is_toting = True
137
138 def drop(self, room):
139 self.rooms[:] = [ room ]
140 self.is_toting = False
141
142 def hide(self):
143 self.rooms[:] = []
144 self.is_toting = False
145
146 def destroy(self):
147 self.hide()
148
149 class Message(object):
150 """A message for printing."""
151 text = ''
152
153 def __str__(self):
154 return self.text
155
156 class Hint(object):
157 """A hint offered if the player loiters in one area too long."""
158
159 turns_needed = 0
160 turn_counter = 0
161 penalty = 0
162 question = None
163 message = None
164 used = False
165
166 def __init__(self):
167 self.rooms = []
168
169 class Dwarf(object):
170 is_dwarf = True
171 is_pirate = False
172
173 def __init__(self, room):
174 self.start_at(room)
175 self.has_seen_adventurer = False
176
177 def start_at(self, room):
178 self.room = room
179 self.old_room = room
180
181 def can_move(self, move):
182 if not isinstance(move.action, Room):
183 return False
184 room = move.action
185 return (room.is_after_hall_of_mists
186 and not room.is_forced
187 and not move.condition == ('%', 100))
188
189 class Pirate(Dwarf):
190 is_dwarf = False
191 is_pirate = True