1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX
10 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
13 self.id_ = self.game.new_thing_id()
16 self.position = position
20 class Thing(ThingBase):
26 def __init__(self, *args, **kwargs):
27 super().__init__(*args, **kwargs)
34 return self.__class__.get_type()
38 return cls.__name__[len('Thing_'):]
40 def sound(self, name, msg):
41 from plomrogue.mapping import DijkstraMap
42 from plomrogue.misc import quote
44 def lower_msg_by_volume(msg, volume, largest_audible_distance):
46 factor = largest_audible_distance / 4
50 while random.random() > volume * factor:
53 elif c != '.' and c != ' ':
60 largest_audible_distance = 20
61 # player's don't block sound (or should they?)
62 things = [t for t in self.game.things if t.type_ != 'Player']
63 dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
64 largest_audible_distance, self.game.get_map)
65 for c_id in self.game.sessions:
66 listener = self.game.get_player(c_id)
67 target_yx = dijkstra_map.target_yx(*listener.position, True)
70 listener_distance = dijkstra_map[target_yx]
71 if listener_distance > largest_audible_distance:
73 volume = 1 / max(1, listener_distance)
74 lowered_msg = lower_msg_by_volume(msg, volume,
75 largest_audible_distance)
76 lowered_nick = lower_msg_by_volume(name, volume,
77 largest_audible_distance)
78 self.game.io.send('CHAT ' +
79 quote('(volume: %.2f) %s: %s' % (volume,
86 class Thing_Item(Thing):
92 class ThingSpawner(Thing):
96 for t in [t for t in self.game.things
97 if t != self and t.position == self.position]:
99 t = self.game.thing_types[self.child_type](self.game,
100 position=self.position)
101 self.game.things += [t]
102 self.game.changed = True
106 class Thing_ItemSpawner(ThingSpawner):
111 class Thing_SpawnPointSpawner(ThingSpawner):
112 child_type = 'SpawnPoint'
116 class Thing_SpawnPoint(Thing):
123 class Thing_DoorSpawner(ThingSpawner):
128 class Thing_Door(Thing):
135 self.blocking = False
140 self.thing_char = '#'
143 self.portable = False
150 class Thing_Bottle(Thing):
157 self.thing_char = '_'
162 class Thing_BottleSpawner(ThingSpawner):
163 child_type = 'Bottle'
167 class Thing_Hat(Thing):
170 design = ' +--+ ' + ' | | ' + '======'
174 class Thing_HatRemixer(Thing):
177 def accept(self, hat):
180 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
182 new_design += random.choice(list(legal_chars))
183 hat.design = new_design
184 self.sound('HAT REMIXER', 'remixing a hat …')
189 class Thing_MusicPlayer(Thing):
194 next_song_start = datetime.datetime.now()
198 def __init__(self, *args, **kwargs):
199 super().__init__(*args, **kwargs)
200 self.next_song_start = datetime.datetime.now()
204 if (not self.playing) or len(self.playlist) == 0:
206 if datetime.datetime.now() > self.next_song_start:
207 self.playlist_index += 1
208 if self.playlist_index == len(self.playlist):
209 self.playlist_index = 0
213 song_data = self.playlist[self.playlist_index]
214 self.next_song_start = datetime.datetime.now() +\
215 datetime.timedelta(seconds=song_data[1])
216 self.sound('MUSICPLAYER', song_data[0])
217 self.game.changed = True
219 def interpret(self, command):
221 if command == 'HELP':
222 msg_lines += ['available commands:']
223 msg_lines += ['HELP – show this help']
224 msg_lines += ['ON/OFF – toggle playback on/off']
225 msg_lines += ['REWIND – return to start of playlist']
226 msg_lines += ['LIST – list programmed songs, durations']
227 msg_lines += ['SKIP – to skip to next song']
228 msg_lines += ['REPEAT – toggle playlist repeat on/off']
229 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
231 elif command == 'LIST':
232 msg_lines += ['playlist:']
234 for entry in self.playlist:
235 minutes = entry[1] // 60
236 seconds = entry[1] % 60
238 seconds = '0%s' % seconds
239 selector = 'next:' if i == self.playlist_index else ' '
240 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
243 elif command == 'ON/OFF':
244 self.playing = False if self.playing else True
245 self.game.changed = True
250 elif command == 'REMOVE':
251 if len(self.playlist) == 0:
252 return ['playlist already empty']
253 del self.playlist[max(0, self.playlist_index)]
254 self.playlist_index -= 1
255 if self.playlist_index < -1:
256 self.playlist_index = -1
257 return ['removed song']
258 elif command == 'REWIND':
259 self.playlist_index = -1
260 self.next_song_start = datetime.datetime.now()
261 self.game.changed = True
262 return ['back at start of playlist']
263 elif command == 'SKIP':
264 self.next_song_start = datetime.datetime.now()
265 self.game.changed = True
267 elif command == 'REPEAT':
268 self.repeat = False if self.repeat else True
269 self.game.changed = True
271 return ['playlist repeat turned on']
273 return ['playlist repeat turned off']
274 elif command.startswith('ADD '):
275 tokens = command.split(' ', 2)
277 return ['wrong syntax, see HELP']
278 length = tokens[1].split(':')
280 return ['wrong syntax, see HELP']
282 minutes = int(length[0])
283 seconds = int(length[1])
285 return ['wrong syntax, see HELP']
286 self.playlist += [(tokens[2], minutes * 60 + seconds)]
287 self.game.changed = True
290 return ['cannot understand command']
294 class Thing_BottleDeposit(Thing):
299 if self.bottle_counter >= 3:
300 self.bottle_counter = 0
301 choice = random.choice(['MusicPlayer', 'Hat'])
302 t = self.game.thing_types[choice](self.game, position=self.position)
303 self.game.things += [t]
304 msg = 'here is a gift as a reward for ecological consciousness –'
305 if choice == 'MusicPlayer':
306 msg += 'pick it up and then use "command thing" on it!'
307 elif choice == 'Hat':
308 msg += 'pick it up and then use "(un-)wear" on it!'
309 self.sound('BOTTLE DEPOSITOR', msg)
310 self.game.changed = True
313 self.bottle_counter += 1
314 self.sound('BOTTLE DEPOSITOR',
315 'thanks for this empty bottle – deposit %s more for a gift!' %
316 (3 - self.bottle_counter))
321 class ThingAnimate(Thing):
325 def __init__(self, *args, **kwargs):
326 super().__init__(*args, **kwargs)
327 self.next_task = [None]
331 def set_next_task(self, task_name, args=()):
332 task_class = self.game.tasks[task_name]
333 self.next_task = [task_class(self, args)]
335 def get_next_task(self):
336 if self.next_task[0]:
337 task = self.next_task[0]
338 self.next_task = [None]
345 for c_id in self.game.sessions:
346 if self.game.sessions[c_id]['thing_id'] == self.id_:
347 self.game.io.send('DEFAULT_COLORS', c_id)
348 self.game.io.send('CHAT "You sober up."', c_id)
350 self.game.changed = True
352 if self.task is None:
353 self.task = self.get_next_task()
357 except (PlayError, GameError) as e:
361 if self.task.todo <= 0:
363 self.game.changed = True
364 self.task = self.get_next_task()
366 def prepare_multiprocessible_fov_stencil(self):
367 fov_map_class = self.game.map_geometry.fov_map_class
368 fov_radius = 3 if self.drunk > 0 else 12
369 self._fov = fov_map_class(self.game.things, self.game.maps,
370 self.position, fov_radius, self.game.get_map)
372 def multiprocessible_fov_stencil(self):
373 self._fov.init_terrain()
376 def fov_stencil(self):
379 # due to the pre-multiprocessing in game.send_gamestate,
380 # the following should actually never be called
381 self.prepare_multiprocessible_fov_stencil()
382 self.multiprocessible_fov_stencil()
385 def fov_stencil_make(self):
388 def fov_test(self, big_yx, little_yx):
389 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
390 if self.fov_stencil.inside(test_position):
391 if self.fov_stencil[test_position] == '.':
395 def fov_stencil_map(self, map_type='normal'):
397 for yx in self.fov_stencil:
398 if self.fov_stencil[yx] == '.':
399 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
400 map_ = self.game.get_map(big_yx, map_type)
401 visible_terrain += map_[little_yx]
403 visible_terrain += ' '
404 return visible_terrain
408 class Thing_Player(ThingAnimate):
411 def __init__(self, *args, **kwargs):
412 super().__init__(*args, **kwargs)
415 def send_msg(self, msg):
416 for c_id in self.game.sessions:
417 if self.game.sessions[c_id]['thing_id'] == self.id_:
418 self.game.io.send(msg, c_id)