1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX
3 from plomrogue.misc import quote
12 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
15 self.id_ = self.game.new_thing_id()
18 self.position = position
22 class Thing(ThingBase):
23 blocks_movement = False
31 def __init__(self, *args, **kwargs):
32 super().__init__(*args, **kwargs)
39 return self.__class__.get_type()
43 return cls.__name__[len('Thing_'):]
45 def sound(self, name, msg):
46 from plomrogue.mapping import DijkstraMap
49 def lower_msg_by_volume(msg, volume, largest_audible_distance,
52 factor = largest_audible_distance / 4
59 in_url = False if in_url else True
61 while random.random() > volume * factor:
64 elif c != '.' and c != ' ':
72 largest_audible_distance = 20
73 obstacles = [t.position for t in self.game.things if t.blocks_sound]
74 sound_blockers = self.game.get_sound_blockers()
75 dijkstra_map = DijkstraMap(sound_blockers, obstacles, self.game.maps,
76 self.position, largest_audible_distance,
79 for m in re.finditer('https?://[^\s]+', msg):
80 url_limits += [m.start(), m.end()]
81 for c_id in self.game.sessions:
82 listener = self.game.get_player(c_id)
83 target_yx = dijkstra_map.target_yx(*listener.position, True)
86 listener_distance = dijkstra_map[target_yx]
87 if listener_distance > largest_audible_distance:
89 volume = 1 / max(1, listener_distance)
90 lowered_msg = lower_msg_by_volume(msg, volume,
91 largest_audible_distance,
93 lowered_nick = lower_msg_by_volume(name, volume,
94 largest_audible_distance)
96 if listener.fov_test(self.position[0], self.position[1]):
97 self.game.io.send('CHATFACE %s' % self.id_, c_id)
98 if self.type_ == 'Player' and hasattr(self, 'thing_char'):
99 symbol = '/@' + self.thing_char
100 self.game.io.send('CHAT ' +
101 quote('vol:%.f%s %s%s: %s' % (volume * 100, '%',
102 lowered_nick, symbol,
108 class Thing_Item(Thing):
114 class ThingSpawner(Thing):
118 for t in [t for t in self.game.things
119 if t != self and t.position == self.position]:
121 self.game.add_thing(self.child_type, self.position)
125 class Thing_ItemSpawner(ThingSpawner):
130 class Thing_SpawnPointSpawner(ThingSpawner):
131 child_type = 'SpawnPoint'
135 class Thing_SpawnPoint(Thing):
142 class Thing_DoorSpawner(ThingSpawner):
147 class Thing_Door(Thing):
149 blocks_movement = False
154 self.blocks_movement = False
155 self.blocks_light = False
156 self.blocks_sound = False
160 self.blocks_movement = True
161 self.blocks_light = True
162 self.blocks_sound = True
163 self.thing_char = '#'
166 self.portable = False
173 class Thing_Psychedelic(Thing):
179 class Thing_PsychedelicSpawner(ThingSpawner):
181 child_type = 'Psychedelic'
185 class Thing_Bottle(Thing):
193 self.thing_char = '_'
198 all_players = [t for t in self.game.things if t.type_ == 'Player']
199 # TODO: refactor with ThingPlayer.prepare_multiprocessible_fov_stencil
200 # and ThingPlayer.fov_test
201 fov_map_class = self.game.map_geometry.fov_map_class
203 light_blockers = self.game.get_light_blockers()
204 obstacles = [t.position for t in self.game.things if t.blocks_light]
205 fov = fov_map_class(light_blockers, obstacles, self.game.maps,
206 self.position, fov_radius, self.game.get_map)
209 for p in all_players:
210 test_position = fov.target_yx(p.position[0], p.position[1])
211 if fov.inside(test_position) and fov[test_position] == '.':
212 visible_players += [p]
213 if len(visible_players) == 0:
214 self.sound('BOTTLE', 'no visible players in spin range')
215 pick = random.choice(visible_players)
216 self.sound('BOTTLE', 'BOTTLE picks: ' + pick.name)
220 class Thing_BottleSpawner(ThingSpawner):
221 child_type = 'Bottle'
225 class Thing_Hat(Thing):
228 design = ' +--+ ' + ' | | ' + '======'
233 new_design += self.design[12]
234 new_design += self.design[13]
235 new_design += self.design[6]
236 new_design += self.design[7]
237 new_design += self.design[0]
238 new_design += self.design[1]
239 new_design += self.design[14]
240 new_design += self.design[15]
241 new_design += self.design[8]
242 new_design += self.design[9]
243 new_design += self.design[2]
244 new_design += self.design[3]
245 new_design += self.design[16]
246 new_design += self.design[17]
247 new_design += self.design[10]
248 new_design += self.design[11]
249 new_design += self.design[4]
250 new_design += self.design[5]
251 self.design = ''.join(new_design)
255 class Thing_HatRemixer(Thing):
258 def accept(self, hat):
261 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
263 new_design += random.choice(list(legal_chars))
264 hat.design = new_design
265 self.sound('HAT REMIXER', 'remixing a hat …')
266 self.game.changed = True
267 self.game.record_change(self.position, 'other')
272 class Thing_MusicPlayer(Thing):
277 next_song_start = datetime.datetime.now()
281 def __init__(self, *args, **kwargs):
282 super().__init__(*args, **kwargs)
283 self.next_song_start = datetime.datetime.now()
287 if (not self.playing) or len(self.playlist) == 0:
289 if datetime.datetime.now() > self.next_song_start:
290 self.playlist_index += 1
291 if self.playlist_index == len(self.playlist):
292 self.playlist_index = 0
296 song_data = self.playlist[self.playlist_index]
297 self.next_song_start = datetime.datetime.now() +\
298 datetime.timedelta(seconds=song_data[1])
299 self.sound('MUSICPLAYER', song_data[0])
300 self.game.changed = True
302 def interpret(self, command):
304 if command == 'HELP':
305 msg_lines += ['available commands:']
306 msg_lines += ['HELP – show this help']
307 msg_lines += ['ON/OFF – toggle playback on/off']
308 msg_lines += ['REWIND – return to start of playlist']
309 msg_lines += ['LIST – list programmed songs, durations']
310 msg_lines += ['SKIP – to skip to next song']
311 msg_lines += ['REPEAT – toggle playlist repeat on/off']
312 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
314 elif command == 'LIST':
315 msg_lines += ['playlist:']
317 for entry in self.playlist:
318 minutes = entry[1] // 60
319 seconds = entry[1] % 60
321 seconds = '0%s' % seconds
322 selector = 'next:' if i == self.playlist_index else ' '
323 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
326 elif command == 'ON/OFF':
327 self.playing = False if self.playing else True
328 self.game.changed = True
333 elif command == 'REMOVE':
334 if len(self.playlist) == 0:
335 return ['playlist already empty']
336 del self.playlist[max(0, self.playlist_index)]
337 self.playlist_index -= 1
338 if self.playlist_index < -1:
339 self.playlist_index = -1
340 self.game.changed = True
341 return ['removed song']
342 elif command == 'REWIND':
343 self.playlist_index = -1
344 self.next_song_start = datetime.datetime.now()
345 self.game.changed = True
346 return ['back at start of playlist']
347 elif command == 'SKIP':
348 self.next_song_start = datetime.datetime.now()
349 self.game.changed = True
351 elif command == 'REPEAT':
352 self.repeat = False if self.repeat else True
353 self.game.changed = True
355 return ['playlist repeat turned on']
357 return ['playlist repeat turned off']
358 elif command.startswith('ADD '):
359 tokens = command.split(' ', 2)
361 return ['wrong syntax, see HELP']
362 length = tokens[1].split(':')
364 return ['wrong syntax, see HELP']
366 minutes = int(length[0])
367 seconds = int(length[1])
369 return ['wrong syntax, see HELP']
370 self.playlist += [(tokens[2], minutes * 60 + seconds)]
371 self.game.changed = True
374 return ['cannot understand command']
378 class Thing_BottleDeposit(Thing):
383 if self.bottle_counter >= 3:
384 self.bottle_counter = 0
385 choice = random.choice(['MusicPlayer', 'Hat'])
386 self.game.add_thing(choice, self.position)
387 msg = 'here is a gift as a reward for ecological consciousness –'
388 if choice == 'MusicPlayer':
389 msg += 'pick it up and then use "command thing" on it!'
390 elif choice == 'Hat':
391 msg += 'pick it up and then use "(un-)wear" on it!'
392 self.sound('BOTTLE DEPOSITOR', msg)
395 self.bottle_counter += 1
396 self.sound('BOTTLE DEPOSITOR',
397 'thanks for this empty bottle – deposit %s more for a gift!' %
398 (3 - self.bottle_counter))
402 class Thing_Cookie(Thing):
406 def __init__(self, *args, **kwargs):
408 super().__init__(*args, **kwargs)
409 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
410 self.thing_char = random.choice(list(legal_chars))
414 class Thing_CookieSpawner(Thing):
417 def accept(self, thing):
418 self.sound('OVEN', '*heat* *brrzt* here\'s a cookie!')
419 self.game.add_thing('Cookie', self.position)
423 class ThingAnimate(Thing):
424 blocks_movement = True
428 def __init__(self, *args, **kwargs):
429 super().__init__(*args, **kwargs)
430 self.next_task = [None]
432 self.invalidate('fov')
433 self.invalidate('other') # currently redundant though
435 def invalidate(self, type_):
438 self._visible_terrain = None
439 self._visible_control = None
440 self.invalidate('other')
441 elif type_ == 'other':
442 self._seen_things = None
443 self._seen_annotation_positions = None
444 self._seen_portal_positions = None
446 def set_next_task(self, task_name, args=()):
447 task_class = self.game.tasks[task_name]
448 self.next_task = [task_class(self, args)]
450 def get_next_task(self):
451 if self.next_task[0]:
452 task = self.next_task[0]
453 self.next_task = [None]
458 if self.task is None:
459 self.task = self.get_next_task()
463 except (PlayError, GameError) as e:
467 if self.task.todo <= 0:
469 self.game.changed = True
470 self.task = self.get_next_task()
472 def prepare_multiprocessible_fov_stencil(self):
473 fov_map_class = self.game.map_geometry.fov_map_class
474 fov_radius = 3 if self.drunk > 0 else 12
475 light_blockers = self.game.get_light_blockers()
476 obstacles = [t.position for t in self.game.things if t.blocks_light]
477 self._fov = fov_map_class(light_blockers, obstacles, self.game.maps,
478 self.position, fov_radius, self.game.get_map)
480 def multiprocessible_fov_stencil(self):
481 self._fov.init_terrain()
484 def fov_stencil(self):
487 # due to the pre-multiprocessing in game.send_gamestate,
488 # the following should actually never be called
489 self.prepare_multiprocessible_fov_stencil()
490 self.multiprocessible_fov_stencil()
493 def fov_stencil_make(self):
496 def fov_test(self, big_yx, little_yx):
497 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
498 if self.fov_stencil.inside(test_position):
499 if self.fov_stencil[test_position] == '.':
503 def fov_stencil_map(self, map_type):
505 for yx in self.fov_stencil:
506 if self.fov_stencil[yx] == '.':
507 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
508 map_ = self.game.get_map(big_yx, map_type)
509 visible_terrain += map_[little_yx]
511 visible_terrain += ' '
512 return visible_terrain
515 def visible_terrain(self):
516 if self._visible_terrain:
517 return self._visible_terrain
518 self._visible_terrain = self.fov_stencil_map('normal')
519 return self._visible_terrain
522 def visible_control(self):
523 if self._visible_control:
524 return self._visible_control
525 self._visible_control = self.fov_stencil_map('control')
526 return self._visible_control
529 def seen_things(self):
530 if self._seen_things is not None:
531 return self._seen_things
532 self._seen_things = [t for t in self.game.things
533 if self.fov_test(*t.position)]
534 return self._seen_things
537 def seen_annotation_positions(self):
538 if self._seen_annotation_positions is not None:
539 return self._seen_annotation_positions
540 self._seen_annotation_positions = []
541 for big_yx in self.game.annotations:
542 for little_yx in [little_yx for little_yx
543 in self.game.annotations[big_yx]
544 if self.fov_test(big_yx, little_yx)]:
545 self._seen_annotation_positions += [(big_yx, little_yx)]
546 return self._seen_annotation_positions
549 def seen_portal_positions(self):
550 if self._seen_portal_positions is not None:
551 return self._seen_portal_positions
552 self._seen_portal_positions = []
553 for big_yx in self.game.portals:
554 for little_yx in [little_yx for little_yx
555 in self.game.portals[big_yx]
556 if self.fov_test(big_yx, little_yx)]:
557 self._seen_portal_positions += [(big_yx, little_yx)]
558 return self._seen_portal_positions
562 class Thing_Player(ThingAnimate):
565 def __init__(self, *args, **kwargs):
566 super().__init__(*args, **kwargs)
573 self.send_msg('CHAT "You sober up."')
574 self.invalidate('fov')
575 self.game.changed = True
577 if self.tripping == 0:
578 self.send_msg('DEFAULT_COLORS')
579 self.send_msg('CHAT "You sober up."')
580 self.game.changed = True
581 elif self.tripping > 0 and self.tripping % 250 == 0:
582 self.send_msg('RANDOM_COLORS')
583 self.game.changed = True
585 def send_msg(self, msg):
586 for c_id in self.game.sessions:
587 if self.game.sessions[c_id]['thing_id'] == self.id_:
588 self.game.io.send(msg, c_id)
597 def add_cookie_char(self, c):
598 if not self.name in self.game.players_hat_chars:
599 self.game.players_hat_chars[self.name] = ' #' # default
600 if not c in self.game.players_hat_chars[self.name]:
601 self.game.players_hat_chars[self.name] += c
603 def get_cookie_chars(self):
604 if self.name in self.game.players_hat_chars:
605 return self.game.players_hat_chars[self.name]
606 return ' #' # default