1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX
3 from plomrogue.misc import quote
11 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
14 self.id_ = self.game.new_thing_id()
17 self.position = position
21 class Thing(ThingBase):
29 def __init__(self, *args, **kwargs):
30 super().__init__(*args, **kwargs)
37 return self.__class__.get_type()
41 return cls.__name__[len('Thing_'):]
43 def sound(self, name, msg):
44 from plomrogue.mapping import DijkstraMap
47 def lower_msg_by_volume(msg, volume, largest_audible_distance,
50 factor = largest_audible_distance / 4
57 in_url = False if in_url else True
59 while random.random() > volume * factor:
62 elif c != '.' and c != ' ':
70 largest_audible_distance = 20
71 # player's don't block sound (or should they?)
72 things = [t for t in self.game.things if t.type_ != 'Player']
73 dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
74 largest_audible_distance, self.game.get_map)
76 for m in re.finditer('https?://[^\s]+', msg):
77 url_limits += [m.start(), m.end()]
78 for c_id in self.game.sessions:
79 listener = self.game.get_player(c_id)
80 target_yx = dijkstra_map.target_yx(*listener.position, True)
83 listener_distance = dijkstra_map[target_yx]
84 if listener_distance > largest_audible_distance:
86 volume = 1 / max(1, listener_distance)
87 lowered_msg = lower_msg_by_volume(msg, volume,
88 largest_audible_distance,
90 lowered_nick = lower_msg_by_volume(name, volume,
91 largest_audible_distance)
92 self.game.io.send('CHAT ' +
93 quote('(volume: %.2f) %s: %s' % (volume,
100 class Thing_Item(Thing):
106 class ThingSpawner(Thing):
110 for t in [t for t in self.game.things
111 if t != self and t.position == self.position]:
113 self.game.add_thing(self.child_type, self.position)
114 self.game.changed = True
118 class Thing_ItemSpawner(ThingSpawner):
123 class Thing_SpawnPointSpawner(ThingSpawner):
124 child_type = 'SpawnPoint'
128 class Thing_SpawnPoint(Thing):
135 class Thing_DoorSpawner(ThingSpawner):
140 class Thing_Door(Thing):
147 self.blocking = False
152 self.thing_char = '#'
155 self.portable = False
162 class Thing_Bottle(Thing):
170 self.thing_char = '_'
175 all_players = [t for t in self.game.things if t.type_ == 'Player']
176 # TODO: refactor with ThingPlayer.prepare_multiprocessible_fov_stencil
177 # and ThingPlayer.fov_test
178 fov_map_class = self.game.map_geometry.fov_map_class
180 fov = fov_map_class(self.game.things, self.game.maps,
181 self.position, fov_radius, self.game.get_map)
184 for p in all_players:
185 test_position = fov.target_yx(p.position[0], p.position[1])
186 if fov.inside(test_position) and fov[test_position] == '.':
187 visible_players += [p]
188 if len(visible_players) == 0:
189 self.sound('BOTTLE', 'no visible players in spin range')
190 pick = random.choice(visible_players)
191 self.sound('BOTTLE', 'BOTTLE picks: ' + pick.name)
195 class Thing_BottleSpawner(ThingSpawner):
196 child_type = 'Bottle'
200 class Thing_Hat(Thing):
203 design = ' +--+ ' + ' | | ' + '======'
207 class Thing_HatRemixer(Thing):
210 def accept(self, hat):
213 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
215 new_design += random.choice(list(legal_chars))
216 hat.design = new_design
217 self.sound('HAT REMIXER', 'remixing a hat …')
218 self.game.changed = True
223 class Thing_MusicPlayer(Thing):
228 next_song_start = datetime.datetime.now()
232 def __init__(self, *args, **kwargs):
233 super().__init__(*args, **kwargs)
234 self.next_song_start = datetime.datetime.now()
238 if (not self.playing) or len(self.playlist) == 0:
240 if datetime.datetime.now() > self.next_song_start:
241 self.playlist_index += 1
242 if self.playlist_index == len(self.playlist):
243 self.playlist_index = 0
247 song_data = self.playlist[self.playlist_index]
248 self.next_song_start = datetime.datetime.now() +\
249 datetime.timedelta(seconds=song_data[1])
250 self.sound('MUSICPLAYER', song_data[0])
251 self.game.changed = True
253 def interpret(self, command):
255 if command == 'HELP':
256 msg_lines += ['available commands:']
257 msg_lines += ['HELP – show this help']
258 msg_lines += ['ON/OFF – toggle playback on/off']
259 msg_lines += ['REWIND – return to start of playlist']
260 msg_lines += ['LIST – list programmed songs, durations']
261 msg_lines += ['SKIP – to skip to next song']
262 msg_lines += ['REPEAT – toggle playlist repeat on/off']
263 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
265 elif command == 'LIST':
266 msg_lines += ['playlist:']
268 for entry in self.playlist:
269 minutes = entry[1] // 60
270 seconds = entry[1] % 60
272 seconds = '0%s' % seconds
273 selector = 'next:' if i == self.playlist_index else ' '
274 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
277 elif command == 'ON/OFF':
278 self.playing = False if self.playing else True
279 self.game.changed = True
284 elif command == 'REMOVE':
285 if len(self.playlist) == 0:
286 return ['playlist already empty']
287 del self.playlist[max(0, self.playlist_index)]
288 self.playlist_index -= 1
289 if self.playlist_index < -1:
290 self.playlist_index = -1
291 self.game.changed = True
292 return ['removed song']
293 elif command == 'REWIND':
294 self.playlist_index = -1
295 self.next_song_start = datetime.datetime.now()
296 self.game.changed = True
297 return ['back at start of playlist']
298 elif command == 'SKIP':
299 self.next_song_start = datetime.datetime.now()
300 self.game.changed = True
302 elif command == 'REPEAT':
303 self.repeat = False if self.repeat else True
304 self.game.changed = True
306 return ['playlist repeat turned on']
308 return ['playlist repeat turned off']
309 elif command.startswith('ADD '):
310 tokens = command.split(' ', 2)
312 return ['wrong syntax, see HELP']
313 length = tokens[1].split(':')
315 return ['wrong syntax, see HELP']
317 minutes = int(length[0])
318 seconds = int(length[1])
320 return ['wrong syntax, see HELP']
321 self.playlist += [(tokens[2], minutes * 60 + seconds)]
322 self.game.changed = True
325 return ['cannot understand command']
329 class Thing_BottleDeposit(Thing):
334 if self.bottle_counter >= 3:
335 self.bottle_counter = 0
336 choice = random.choice(['MusicPlayer', 'Hat'])
337 self.game.add_thing(choice, self.position)
338 msg = 'here is a gift as a reward for ecological consciousness –'
339 if choice == 'MusicPlayer':
340 msg += 'pick it up and then use "command thing" on it!'
341 elif choice == 'Hat':
342 msg += 'pick it up and then use "(un-)wear" on it!'
343 self.sound('BOTTLE DEPOSITOR', msg)
344 self.game.changed = True
347 self.bottle_counter += 1
348 self.sound('BOTTLE DEPOSITOR',
349 'thanks for this empty bottle – deposit %s more for a gift!' %
350 (3 - self.bottle_counter))
355 class ThingAnimate(Thing):
359 def __init__(self, *args, **kwargs):
360 super().__init__(*args, **kwargs)
361 self.next_task = [None]
363 self.invalidate_map_view()
365 def invalidate_map_view(self):
367 self._visible_terrain = None
368 self._visible_control = None
370 def set_next_task(self, task_name, args=()):
371 task_class = self.game.tasks[task_name]
372 self.next_task = [task_class(self, args)]
374 def get_next_task(self):
375 if self.next_task[0]:
376 task = self.next_task[0]
377 self.next_task = [None]
384 for c_id in self.game.sessions:
385 if self.game.sessions[c_id]['thing_id'] == self.id_:
386 # TODO: refactor with self.send_msg
387 self.game.io.send('DEFAULT_COLORS', c_id)
388 self.game.io.send('CHAT "You sober up."', c_id)
389 self.invalidate_map_view()
391 self.game.changed = True
392 if self.task is None:
393 self.task = self.get_next_task()
397 except (PlayError, GameError) as e:
401 if self.task.todo <= 0:
403 self.game.changed = True
404 self.task = self.get_next_task()
406 def prepare_multiprocessible_fov_stencil(self):
407 fov_map_class = self.game.map_geometry.fov_map_class
408 fov_radius = 3 if self.drunk > 0 else 12
409 self._fov = fov_map_class(self.game.things, self.game.maps,
410 self.position, fov_radius, self.game.get_map)
412 def multiprocessible_fov_stencil(self):
413 self._fov.init_terrain()
416 def fov_stencil(self):
419 # due to the pre-multiprocessing in game.send_gamestate,
420 # the following should actually never be called
421 self.prepare_multiprocessible_fov_stencil()
422 self.multiprocessible_fov_stencil()
425 def fov_stencil_make(self):
428 def fov_test(self, big_yx, little_yx):
429 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
430 if self.fov_stencil.inside(test_position):
431 if self.fov_stencil[test_position] == '.':
435 def fov_stencil_map(self, map_type):
437 for yx in self.fov_stencil:
438 if self.fov_stencil[yx] == '.':
439 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
440 map_ = self.game.get_map(big_yx, map_type)
441 visible_terrain += map_[little_yx]
443 visible_terrain += ' '
444 return visible_terrain
447 def visible_terrain(self):
448 if self._visible_terrain:
449 return self._visible_terrain
450 self._visible_terrain = self.fov_stencil_map('normal')
451 return self._visible_terrain
454 def visible_control(self):
455 if self._visible_control:
456 return self._visible_control
457 self._visible_control = self.fov_stencil_map('control')
458 return self._visible_control
462 class Thing_Player(ThingAnimate):
465 def __init__(self, *args, **kwargs):
466 super().__init__(*args, **kwargs)
469 def send_msg(self, msg):
470 for c_id in self.game.sessions:
471 if self.game.sessions[c_id]['thing_id'] == self.id_:
472 self.game.io.send(msg, c_id)