1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX
9 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
12 self.id_ = self.game.new_thing_id()
15 self.position = position
19 class Thing(ThingBase):
25 def __init__(self, *args, **kwargs):
26 super().__init__(*args, **kwargs)
33 return self.__class__.get_type()
37 return cls.__name__[len('Thing_'):]
39 def sound(self, name, msg):
40 from plomrogue.mapping import DijkstraMap
41 from plomrogue.misc import quote
43 def lower_msg_by_volume(msg, volume, largest_audible_distance):
45 factor = largest_audible_distance / 4
49 while random.random() > volume * factor:
52 elif c != '.' and c != ' ':
59 largest_audible_distance = 20
60 # player's don't block sound (or should they?)
61 things = [t for t in self.game.things if t.type_ != 'Player']
62 dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
63 largest_audible_distance, self.game.get_map)
64 for c_id in self.game.sessions:
65 listener = self.game.get_player(c_id)
66 target_yx = dijkstra_map.target_yx(*listener.position, True)
69 listener_distance = dijkstra_map[target_yx]
70 if listener_distance > largest_audible_distance:
72 volume = 1 / max(1, listener_distance)
73 lowered_msg = lower_msg_by_volume(msg, volume,
74 largest_audible_distance)
75 lowered_nick = lower_msg_by_volume(name, volume,
76 largest_audible_distance)
77 self.game.io.send('CHAT ' +
78 quote('(volume: %.2f) %s: %s' % (volume,
85 class Thing_Item(Thing):
91 class ThingSpawner(Thing):
95 for t in [t for t in self.game.things
96 if t != self and t.position == self.position]:
98 t = self.game.thing_types[self.child_type](self.game,
99 position=self.position)
100 self.game.things += [t]
101 self.game.changed = True
105 class Thing_ItemSpawner(ThingSpawner):
110 class Thing_SpawnPointSpawner(ThingSpawner):
111 child_type = 'SpawnPoint'
115 class Thing_SpawnPoint(Thing):
122 class Thing_DoorSpawner(ThingSpawner):
127 class Thing_Door(Thing):
134 self.blocking = False
139 self.thing_char = '#'
142 self.portable = False
149 class Thing_Bottle(Thing):
156 self.thing_char = '_'
161 class Thing_BottleSpawner(ThingSpawner):
162 child_type = 'Bottle'
167 class Thing_MusicPlayer(Thing):
172 next_song_start = datetime.datetime.now()
176 def __init__(self, *args, **kwargs):
177 super().__init__(*args, **kwargs)
178 self.next_song_start = datetime.datetime.now()
182 if (not self.playing) or len(self.playlist) == 0:
184 if datetime.datetime.now() > self.next_song_start:
185 self.playlist_index += 1
186 if self.playlist_index == len(self.playlist):
187 self.playlist_index = 0
191 song_data = self.playlist[self.playlist_index]
192 self.next_song_start = datetime.datetime.now() +\
193 datetime.timedelta(seconds=song_data[1])
194 self.sound('MUSICPLAYER', song_data[0])
195 self.game.changed = True
197 def interpret(self, command):
199 if command == 'HELP':
200 msg_lines += ['available commands:']
201 msg_lines += ['HELP – show this help']
202 msg_lines += ['ON/OFF – toggle playback on/off']
203 msg_lines += ['REWIND – return to start of playlist']
204 msg_lines += ['LIST – list programmed songs, durations']
205 msg_lines += ['SKIP – to skip to next song']
206 msg_lines += ['REPEAT – toggle playlist repeat on/off']
207 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
209 elif command == 'LIST':
210 msg_lines += ['playlist:']
212 for entry in self.playlist:
213 minutes = entry[1] // 60
214 seconds = entry[1] % 60
216 seconds = '0%s' % seconds
217 selector = 'next:' if i == self.playlist_index else ' '
218 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
221 elif command == 'ON/OFF':
222 self.playing = False if self.playing else True
223 self.game.changed = True
228 elif command == 'REMOVE':
229 if len(self.playlist) == 0:
230 return ['playlist already empty']
231 del self.playlist[max(0, self.playlist_index)]
232 self.playlist_index -= 1
233 if self.playlist_index < -1:
234 self.playlist_index = -1
235 return ['removed song']
236 elif command == 'REWIND':
237 self.playlist_index = -1
238 self.next_song_start = datetime.datetime.now()
239 self.game.changed = True
240 return ['back at start of playlist']
241 elif command == 'SKIP':
242 self.next_song_start = datetime.datetime.now()
243 self.game.changed = True
245 elif command == 'REPEAT':
246 self.repeat = False if self.repeat else True
247 self.game.changed = True
249 return ['playlist repeat turned on']
251 return ['playlist repeat turned off']
252 elif command.startswith('ADD '):
253 tokens = command.split(' ', 2)
255 return ['wrong syntax, see HELP']
256 length = tokens[1].split(':')
258 return ['wrong syntax, see HELP']
260 minutes = int(length[0])
261 seconds = int(length[1])
263 return ['wrong syntax, see HELP']
264 self.playlist += [(tokens[2], minutes * 60 + seconds)]
265 self.game.changed = True
268 return ['cannot understand command']
272 class Thing_BottleDeposit(Thing):
277 if self.bottle_counter >= 3:
278 self.bottle_counter = 0
279 t = self.game.thing_types['MusicPlayer'](self.game,
280 position=self.position)
281 self.game.things += [t]
282 self.sound('BOTTLE DEPOSITOR',
283 'here is a gift as a reward for ecological consciousness –'
284 'use "command thing" on it to learn more!')
285 self.game.changed = True
288 self.bottle_counter += 1
289 self.sound('BOTTLE DEPOSITOR',
290 'thanks for this empty bottle – deposit %s more for a gift!' %
291 (3 - self.bottle_counter))
296 class ThingAnimate(Thing):
300 def __init__(self, *args, **kwargs):
301 super().__init__(*args, **kwargs)
302 self.next_task = [None]
306 def set_next_task(self, task_name, args=()):
307 task_class = self.game.tasks[task_name]
308 self.next_task = [task_class(self, args)]
310 def get_next_task(self):
311 if self.next_task[0]:
312 task = self.next_task[0]
313 self.next_task = [None]
320 for c_id in self.game.sessions:
321 if self.game.sessions[c_id]['thing_id'] == self.id_:
322 self.game.io.send('DEFAULT_COLORS', c_id)
323 self.game.io.send('CHAT "You sober up."', c_id)
325 self.game.changed = True
327 if self.task is None:
328 self.task = self.get_next_task()
332 except (PlayError, GameError) as e:
336 if self.task.todo <= 0:
338 self.game.changed = True
339 self.task = self.get_next_task()
341 def prepare_multiprocessible_fov_stencil(self):
342 fov_map_class = self.game.map_geometry.fov_map_class
343 fov_radius = 3 if self.drunk > 0 else 12
344 self._fov = fov_map_class(self.game.things, self.game.maps,
345 self.position, fov_radius, self.game.get_map)
347 def multiprocessible_fov_stencil(self):
348 self._fov.init_terrain()
351 def fov_stencil(self):
354 # due to the pre-multiprocessing in game.send_gamestate,
355 # the following should actually never be called
356 self.prepare_multiprocessible_fov_stencil()
357 self.multiprocessible_fov_stencil()
360 def fov_stencil_make(self):
363 def fov_test(self, big_yx, little_yx):
364 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
365 if self.fov_stencil.inside(test_position):
366 if self.fov_stencil[test_position] == '.':
370 def fov_stencil_map(self, map_type='normal'):
372 for yx in self.fov_stencil:
373 if self.fov_stencil[yx] == '.':
374 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
375 map_ = self.game.get_map(big_yx, map_type)
376 visible_terrain += map_[little_yx]
378 visible_terrain += ' '
379 return visible_terrain
383 class Thing_Player(ThingAnimate):
386 def __init__(self, *args, **kwargs):
387 super().__init__(*args, **kwargs)
390 def send_msg(self, msg):
391 for c_id in self.game.sessions:
392 if self.game.sessions[c_id]['thing_id'] == self.id_:
393 self.game.io.send(msg, c_id)