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 …')
185 self.game.changed = True
190 class Thing_MusicPlayer(Thing):
195 next_song_start = datetime.datetime.now()
199 def __init__(self, *args, **kwargs):
200 super().__init__(*args, **kwargs)
201 self.next_song_start = datetime.datetime.now()
205 if (not self.playing) or len(self.playlist) == 0:
207 if datetime.datetime.now() > self.next_song_start:
208 self.playlist_index += 1
209 if self.playlist_index == len(self.playlist):
210 self.playlist_index = 0
214 song_data = self.playlist[self.playlist_index]
215 self.next_song_start = datetime.datetime.now() +\
216 datetime.timedelta(seconds=song_data[1])
217 self.sound('MUSICPLAYER', song_data[0])
218 self.game.changed = True
220 def interpret(self, command):
222 if command == 'HELP':
223 msg_lines += ['available commands:']
224 msg_lines += ['HELP – show this help']
225 msg_lines += ['ON/OFF – toggle playback on/off']
226 msg_lines += ['REWIND – return to start of playlist']
227 msg_lines += ['LIST – list programmed songs, durations']
228 msg_lines += ['SKIP – to skip to next song']
229 msg_lines += ['REPEAT – toggle playlist repeat on/off']
230 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
232 elif command == 'LIST':
233 msg_lines += ['playlist:']
235 for entry in self.playlist:
236 minutes = entry[1] // 60
237 seconds = entry[1] % 60
239 seconds = '0%s' % seconds
240 selector = 'next:' if i == self.playlist_index else ' '
241 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
244 elif command == 'ON/OFF':
245 self.playing = False if self.playing else True
246 self.game.changed = True
251 elif command == 'REMOVE':
252 if len(self.playlist) == 0:
253 return ['playlist already empty']
254 del self.playlist[max(0, self.playlist_index)]
255 self.playlist_index -= 1
256 if self.playlist_index < -1:
257 self.playlist_index = -1
258 return ['removed song']
259 elif command == 'REWIND':
260 self.playlist_index = -1
261 self.next_song_start = datetime.datetime.now()
262 self.game.changed = True
263 return ['back at start of playlist']
264 elif command == 'SKIP':
265 self.next_song_start = datetime.datetime.now()
266 self.game.changed = True
268 elif command == 'REPEAT':
269 self.repeat = False if self.repeat else True
270 self.game.changed = True
272 return ['playlist repeat turned on']
274 return ['playlist repeat turned off']
275 elif command.startswith('ADD '):
276 tokens = command.split(' ', 2)
278 return ['wrong syntax, see HELP']
279 length = tokens[1].split(':')
281 return ['wrong syntax, see HELP']
283 minutes = int(length[0])
284 seconds = int(length[1])
286 return ['wrong syntax, see HELP']
287 self.playlist += [(tokens[2], minutes * 60 + seconds)]
288 self.game.changed = True
291 return ['cannot understand command']
295 class Thing_BottleDeposit(Thing):
300 if self.bottle_counter >= 3:
301 self.bottle_counter = 0
302 choice = random.choice(['MusicPlayer', 'Hat'])
303 t = self.game.thing_types[choice](self.game, position=self.position)
304 self.game.things += [t]
305 msg = 'here is a gift as a reward for ecological consciousness –'
306 if choice == 'MusicPlayer':
307 msg += 'pick it up and then use "command thing" on it!'
308 elif choice == 'Hat':
309 msg += 'pick it up and then use "(un-)wear" on it!'
310 self.sound('BOTTLE DEPOSITOR', msg)
311 self.game.changed = True
314 self.bottle_counter += 1
315 self.sound('BOTTLE DEPOSITOR',
316 'thanks for this empty bottle – deposit %s more for a gift!' %
317 (3 - self.bottle_counter))
322 class ThingAnimate(Thing):
326 def __init__(self, *args, **kwargs):
327 super().__init__(*args, **kwargs)
328 self.next_task = [None]
332 def set_next_task(self, task_name, args=()):
333 task_class = self.game.tasks[task_name]
334 self.next_task = [task_class(self, args)]
336 def get_next_task(self):
337 if self.next_task[0]:
338 task = self.next_task[0]
339 self.next_task = [None]
346 for c_id in self.game.sessions:
347 if self.game.sessions[c_id]['thing_id'] == self.id_:
348 self.game.io.send('DEFAULT_COLORS', c_id)
349 self.game.io.send('CHAT "You sober up."', c_id)
351 self.game.changed = True
353 if self.task is None:
354 self.task = self.get_next_task()
358 except (PlayError, GameError) as e:
362 if self.task.todo <= 0:
364 self.game.changed = True
365 self.task = self.get_next_task()
367 def prepare_multiprocessible_fov_stencil(self):
368 fov_map_class = self.game.map_geometry.fov_map_class
369 fov_radius = 3 if self.drunk > 0 else 12
370 self._fov = fov_map_class(self.game.things, self.game.maps,
371 self.position, fov_radius, self.game.get_map)
373 def multiprocessible_fov_stencil(self):
374 self._fov.init_terrain()
377 def fov_stencil(self):
380 # due to the pre-multiprocessing in game.send_gamestate,
381 # the following should actually never be called
382 self.prepare_multiprocessible_fov_stencil()
383 self.multiprocessible_fov_stencil()
386 def fov_stencil_make(self):
389 def fov_test(self, big_yx, little_yx):
390 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
391 if self.fov_stencil.inside(test_position):
392 if self.fov_stencil[test_position] == '.':
396 def fov_stencil_map(self, map_type='normal'):
398 for yx in self.fov_stencil:
399 if self.fov_stencil[yx] == '.':
400 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
401 map_ = self.game.get_map(big_yx, map_type)
402 visible_terrain += map_[little_yx]
404 visible_terrain += ' '
405 return visible_terrain
409 class Thing_Player(ThingAnimate):
412 def __init__(self, *args, **kwargs):
413 super().__init__(*args, **kwargs)
416 def send_msg(self, msg):
417 for c_id in self.game.sessions:
418 if self.game.sessions[c_id]['thing_id'] == self.id_:
419 self.game.io.send(msg, c_id)