1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX, FovMap
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
34 def __init__(self, *args, **kwargs):
35 super().__init__(*args, **kwargs)
42 return self.__class__.get_type()
46 return cls.__name__[len('Thing_'):]
48 def sound(self, name, msg):
49 from plomrogue.mapping import DijkstraMap
52 def lower_msg_by_volume(msg, volume, largest_audible_distance,
54 factor = largest_audible_distance / 4
61 in_url = False if in_url else True
63 while random.random() > volume * factor:
66 elif c != '.' and c != ' ':
74 largest_audible_distance = 20
75 obstacles = [t.position for t in self.game.things if t.blocks_sound]
76 targets = [t.position for t in self.game.things if t.type_ == 'Player']
77 sound_blockers = self.game.get_sound_blockers()
78 dijkstra_map = DijkstraMap(targets, sound_blockers, obstacles,
79 self.game.maps, self.position,
80 largest_audible_distance, self.game.get_map)
82 for m in re.finditer('https?://[^\s]+', msg):
83 url_limits += [m.start(), m.end()]
84 for c_id in self.game.sessions:
85 listener = self.game.get_player(c_id)
86 target_yx = dijkstra_map.target_yx(*listener.position, True)
89 listener_distance = dijkstra_map[target_yx]
90 if listener_distance > largest_audible_distance:
92 volume = 1 / max(1, listener_distance)
93 lowered_msg = lower_msg_by_volume(msg, volume,
94 largest_audible_distance,
96 lowered_nick = lower_msg_by_volume(name, volume,
97 largest_audible_distance)
99 # if listener.fov_test(self.position[0], self.position[1]):
100 # TODO: We might want to only show chat faces of players that are
101 # in the listener's FOV. However, if we do a fov_test here,
102 # this might set up a listener._fov where previously there was None,
103 # with ._fov = None serving to Game.send_gamestate() as an indicator
104 # that map view data for listener might be subject to change and
105 # therefore needs to be re-sent. If we generate an un-set ._fov
106 # here, this inhibits send_gamestate() from sending new map view
107 # data to listener. We need to re-structure this whole process
108 # if we want to use a FOV test on listener here.
109 if listener_distance < largest_audible_distance / 2:
110 self.game.io.send('CHATFACE %s' % self.id_, c_id)
111 if self.type_ == 'Player' and hasattr(self, 'thing_char'):
112 symbol = '/@' + self.thing_char
113 self.game.io.send('CHAT ' +
114 quote('vol:%.f%s %s%s: %s' % (volume * 100, '%',
115 lowered_nick, symbol,
121 class Thing_Item(Thing):
127 class ThingSpawner(Thing):
131 for t in [t for t in self.game.things
132 if t != self and t.position == self.position]:
134 return self.game.add_thing(self.child_type, self.position)
138 class Thing_ItemSpawner(ThingSpawner):
143 class Thing_SpawnPointSpawner(ThingSpawner):
144 child_type = 'SpawnPoint'
148 class Thing_SpawnPoint(Thing):
154 def __init__(self, *args, **kwargs):
155 super().__init__(*args, **kwargs)
156 self.created_at = datetime.datetime.now()
160 if self.temporary and datetime.datetime.now() >\
161 self.created_at + datetime.timedelta(minutes=10):
162 self.game.remove_thing(self)
166 class ThingInstallable(Thing):
171 self.portable = False
178 class Thing_SignSpawner(ThingSpawner):
183 class Thing_Sign(ThingInstallable):
185 design_size = YX(16, 36)
187 def __init__(self, *args, **kwargs):
188 super().__init__(*args, **kwargs)
189 self.design = 'x' * self.design_size.y * self.design_size.x
193 class Thing_DoorSpawner(ThingSpawner):
197 door = super().proceed()
199 key = self.game.add_thing('DoorKey', self.position)
204 class Thing_DoorKey(Thing):
211 class Thing_Door(ThingInstallable):
213 blocks_movement = False
217 self.blocks_movement = False
218 self.blocks_light = False
219 self.blocks_sound = False
224 self.blocks_movement = True
225 self.blocks_light = True
226 self.blocks_sound = True
227 self.thing_char = '#'
231 self.thing_char = 'L'
235 class Thing_Psychedelic(Thing):
243 class Thing_PsychedelicSpawner(ThingSpawner):
245 child_type = 'Psychedelic'
249 class Thing_Bottle(Thing):
259 self.thing_char = '_'
263 all_players = [t for t in self.game.things if t.type_ == 'Player']
264 # TODO: refactor with ThingPlayer.prepare_multiprocessible_fov_stencil
265 # and ThingPlayer.fov_test
267 light_blockers = self.game.get_light_blockers()
268 obstacles = [t.position for t in self.game.things if t.blocks_light]
269 fov = FovMap(light_blockers, obstacles, self.game.maps,
270 self.position, fov_radius, self.game.get_map)
273 for p in all_players:
274 test_position = fov.target_yx(p.position[0], p.position[1])
275 if fov.inside(test_position) and fov[test_position] == '.':
276 visible_players += [p]
277 if len(visible_players) == 0:
278 self.sound('BOTTLE', 'no visible players in spin range')
279 pick = random.choice(visible_players)
280 self.sound('BOTTLE', 'BOTTLE picks: ' + pick.name)
284 class Thing_BottleSpawner(ThingSpawner):
285 child_type = 'Bottle'
289 class Thing_Hat(Thing):
292 design = ' +--+ ' + ' | | ' + '======'
295 design_size = YX(3, 6)
299 new_design += self.design[12]
300 new_design += self.design[13]
301 new_design += self.design[6]
302 new_design += self.design[7]
303 new_design += self.design[0]
304 new_design += self.design[1]
305 new_design += self.design[14]
306 new_design += self.design[15]
307 new_design += self.design[8]
308 new_design += self.design[9]
309 new_design += self.design[2]
310 new_design += self.design[3]
311 new_design += self.design[16]
312 new_design += self.design[17]
313 new_design += self.design[10]
314 new_design += self.design[11]
315 new_design += self.design[4]
316 new_design += self.design[5]
317 self.design = ''.join(new_design)
321 class Thing_HatRemixer(Thing):
324 def accept(self, hat):
327 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
329 new_design += random.choice(list(legal_chars))
330 hat.design = new_design
331 self.sound('HAT REMIXER', 'remixing a hat …')
332 self.game.changed = True
333 self.game.record_change(self.position, 'other')
338 class Thing_MusicPlayer(Thing):
343 next_song_start = datetime.datetime.now()
348 def __init__(self, *args, **kwargs):
349 super().__init__(*args, **kwargs)
350 self.next_song_start = datetime.datetime.now()
354 if (not self.playing) or len(self.playlist) == 0:
356 if datetime.datetime.now() > self.next_song_start:
357 self.playlist_index += 1
358 if self.playlist_index == len(self.playlist):
359 self.playlist_index = 0
363 song_data = self.playlist[self.playlist_index]
364 self.next_song_start = datetime.datetime.now() +\
365 datetime.timedelta(seconds=song_data[1])
366 self.sound('MUSICPLAYER', song_data[0])
367 self.game.changed = True
369 def interpret(self, command):
371 if command == 'HELP':
372 msg_lines += ['available commands:']
373 msg_lines += ['HELP – show this help']
374 msg_lines += ['ON/OFF – toggle playback on/off']
375 msg_lines += ['REWIND – return to start of playlist']
376 msg_lines += ['LIST – list programmed item, durations']
377 msg_lines += ['REMOVE – remove current item']
378 msg_lines += ['SKIP – to skip to next item']
379 msg_lines += ['REPEAT – toggle playlist repeat on/off']
380 msg_lines += ['ADD LENGTH ITEM – add ITEM to playlist, with LENGTH in format "minutes:seconds" (something like "0:47" or "11:02")']
382 elif command == 'LIST':
383 msg_lines += ['playlist:']
385 for entry in self.playlist:
386 minutes = entry[1] // 60
387 seconds = entry[1] % 60
389 seconds = '0%s' % seconds
390 selector = 'next:' if i == self.playlist_index else ' '
391 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
394 elif command == 'ON/OFF':
395 self.playing = False if self.playing else True
396 self.game.changed = True
401 elif command == 'REMOVE':
402 if len(self.playlist) == 0:
403 return ['playlist already empty']
404 del self.playlist[max(0, self.playlist_index)]
405 self.playlist_index -= 1
406 if self.playlist_index < -1:
407 self.playlist_index = -1
408 self.game.changed = True
409 return ['removed song']
410 elif command == 'REWIND':
411 self.playlist_index = -1
412 self.next_song_start = datetime.datetime.now()
413 self.game.changed = True
414 return ['back at start of playlist']
415 elif command == 'SKIP':
416 self.next_song_start = datetime.datetime.now()
417 self.game.changed = True
419 elif command == 'REPEAT':
420 self.repeat = False if self.repeat else True
421 self.game.changed = True
423 return ['playlist repeat turned on']
425 return ['playlist repeat turned off']
426 elif command.startswith('ADD '):
427 tokens = command.split(' ', 2)
429 return ['wrong syntax, see HELP']
430 length = tokens[1].split(':')
432 return ['wrong syntax, see HELP']
434 minutes = int(length[0])
435 seconds = int(length[1])
437 return ['wrong syntax, see HELP']
438 self.playlist += [(tokens[2], minutes * 60 + seconds)]
439 self.game.changed = True
442 return ['cannot understand command']
446 class Thing_BottleDeposit(Thing):
451 if self.bottle_counter >= 3:
452 self.bottle_counter = 0
453 choice = random.choice(['MusicPlayer', 'Hat', 'Stimulant', 'Psychedelic'])
454 self.game.add_thing(choice, self.position)
455 msg = 'here is a gift as a reward for ecological consciousness –'
456 if choice == 'MusicPlayer':
457 msg += 'pick it up and then use "command thing" on it!'
458 elif choice == 'Hat':
459 msg += 'pick it up and then use "(un-)wear" on it!'
460 elif choice in {'Psychedelic', 'Stimulant'}:
461 msg += 'pick it up and then use "consume" on it!'
462 self.sound('BOTTLE DEPOSITOR', msg)
465 self.bottle_counter += 1
466 self.sound('BOTTLE DEPOSITOR',
467 'thanks for this empty bottle – deposit %s more for a gift!' %
468 (3 - self.bottle_counter))
472 class Thing_Stimulant(Thing):
480 class Thing_StimulantSpawner(ThingSpawner):
482 child_type = 'Stimulant'
486 class Thing_Chair(Thing):
493 class Thing_ChairSpawner(ThingSpawner):
499 class Thing_Cookie(Thing):
504 def __init__(self, *args, **kwargs):
506 super().__init__(*args, **kwargs)
507 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
508 self.thing_char = random.choice(list(legal_chars))
512 class Thing_CookieSpawner(Thing):
515 def accept(self, thing):
516 self.sound('OVEN', '*heat* *brrzt* here\'s a cookie!')
517 self.game.add_thing('Cookie', self.position)
521 class Thing_Crate(Thing):
525 def __init__(self, *args, **kwargs):
526 super().__init__(*args, **kwargs)
529 def accept(self, thing):
530 self.content += [thing]
532 def remove_from_crate(self, thing):
533 self.content.remove(thing)
537 class Thing_CrateSpawner(ThingSpawner):
543 class ThingAnimate(Thing):
546 def __init__(self, *args, **kwargs):
547 super().__init__(*args, **kwargs)
548 self.next_task = [None]
550 self.invalidate('fov')
551 self.invalidate('other') # currently redundant though
553 def invalidate(self, type_):
556 self._visible_terrain = None
557 self._visible_control = None
558 self.invalidate('other')
559 elif type_ == 'other':
560 self._seen_things = None
561 self._seen_annotation_positions = None
562 self._seen_portal_positions = None
564 def set_next_task(self, task_name, args=()):
565 task_class = self.game.tasks[task_name]
566 self.next_task = [task_class(self, args)]
568 def get_next_task(self):
569 if self.next_task[0]:
570 task = self.next_task[0]
571 self.next_task = [None]
573 task.todo += max(0, -self.energy * 10)
577 if self.task is None:
578 self.task = self.get_next_task()
582 except (PlayError, GameError) as e:
586 if self.task.todo <= 0:
588 self.game.changed = True
589 self.task = self.get_next_task()
591 def prepare_multiprocessible_fov_stencil(self):
592 fov_radius = 3 if self.drunk > 0 else 12
593 light_blockers = self.game.get_light_blockers()
594 obstacles = [t.position for t in self.game.things if t.blocks_light]
595 self._fov = FovMap(light_blockers, obstacles, self.game.maps,
596 self.position, fov_radius, self.game.get_map)
598 def multiprocessible_fov_stencil(self):
599 self._fov.init_terrain()
602 def fov_stencil(self):
605 # due to the pre-multiprocessing in game.send_gamestate,
606 # the following should actually never be called
607 self.prepare_multiprocessible_fov_stencil()
608 self.multiprocessible_fov_stencil()
611 def fov_test(self, big_yx, little_yx):
612 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
613 if self.fov_stencil.inside(test_position):
614 if self.fov_stencil[test_position] == '.':
618 def fov_stencil_map(self, map_type):
620 for yx in self.fov_stencil:
621 if self.fov_stencil[yx] == '.':
622 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
623 map_ = self.game.get_map(big_yx, map_type)
624 visible_terrain += map_[little_yx]
626 visible_terrain += ' '
627 return visible_terrain
630 def visible_terrain(self):
631 if self._visible_terrain:
632 return self._visible_terrain
633 self._visible_terrain = self.fov_stencil_map('normal')
634 return self._visible_terrain
637 def visible_control(self):
638 if self._visible_control:
639 return self._visible_control
640 self._visible_control = self.fov_stencil_map('control')
641 return self._visible_control
644 def seen_things(self):
645 if self._seen_things is not None:
646 return self._seen_things
647 self._seen_things = [t for t in self.game.things
648 if self.fov_test(*t.position)]
649 return self._seen_things
652 def seen_annotation_positions(self):
653 if self._seen_annotation_positions is not None:
654 return self._seen_annotation_positions
655 self._seen_annotation_positions = []
656 for big_yx in self.game.annotations:
657 for little_yx in [little_yx for little_yx
658 in self.game.annotations[big_yx]
659 if self.fov_test(big_yx, little_yx)]:
660 self._seen_annotation_positions += [(big_yx, little_yx)]
661 return self._seen_annotation_positions
664 def seen_portal_positions(self):
665 if self._seen_portal_positions is not None:
666 return self._seen_portal_positions
667 self._seen_portal_positions = []
668 for big_yx in self.game.portals:
669 for little_yx in [little_yx for little_yx
670 in self.game.portals[big_yx]
671 if self.fov_test(big_yx, little_yx)]:
672 self._seen_portal_positions += [(big_yx, little_yx)]
673 return self._seen_portal_positions
677 class Thing_Player(ThingAnimate):
685 def __init__(self, *args, **kwargs):
686 super().__init__(*args, **kwargs)
693 if self.tripping >= 0:
695 if self.need_for_toilet > 0:
696 terrain = self.game.maps[self.position[0]][self.position[1]]
697 if terrain in self.game.terrains:
698 terrain_type = self.game.terrains[terrain]
699 if 'toilet' in terrain_type.tags:
700 self.send_msg('CHAT "You use the toilet. What a relief!"')
701 self.need_for_toilet = 0
702 if self.need_for_toilet > 0:
703 if random.random() > 0.9999:
704 self.need_for_toilet += 1
705 self.game.changed = True
706 if 100000 * random.random() < self.need_for_toilet:
707 self.send_msg('CHAT "You need to go to a toilet."')
708 if self.need_for_toilet > 100:
709 self.send_msg('CHAT "You pee into your pants. Eww!"')
710 self.need_for_toilet = 0
711 self.game.changed = True
713 self.send_msg('CHAT "You sober up."')
714 self.invalidate('fov')
715 self.game.changed = True
716 if self.tripping == 0:
717 self.send_msg('DEFAULT_COLORS')
718 self.send_msg('CHAT "You sober up."')
719 self.game.changed = True
720 elif self.tripping > 0 and self.tripping % 250 == 0:
721 self.send_msg('RANDOM_COLORS')
722 self.game.changed = True
723 if random.random() > 0.9999:
728 if self.energy < 0 and self.standing and self.energy % 5 == 0:
729 self.send_msg('CHAT "All that walking or standing uses up '
730 'your energy, which makes you slower. Find a'
731 ' place to sit or lie down to regain it."')
732 self.game.changed = True
733 if self.dancing and random.random() > 0.99 and not self.next_task[0]:
735 direction = random.choice(self.game.map_geometry.directions)
736 self.set_next_task('MOVE', [direction])
737 if random.random() > 0.9:
739 self.game.changed = True
740 if 1000000 * random.random() < self.energy - 50:
741 self.send_msg('CHAT "Your body tries to '
742 'dance off its energy surplus."')
744 self.game.changed = True
746 def send_msg(self, msg):
747 for c_id in self.game.sessions:
748 if self.game.sessions[c_id]['thing_id'] == self.id_:
749 self.game.io.send(msg, c_id)
758 def add_cookie_char(self, c):
759 if not self.name in self.game.players_hat_chars:
760 self.game.players_hat_chars[self.name] = ' #' # default
761 if not c in self.game.players_hat_chars[self.name]:
762 self.game.players_hat_chars[self.name] += c
764 def get_cookie_chars(self):
765 chars = ' #' # default
766 if self.name in self.game.players_hat_chars:
767 chars = self.game.players_hat_chars[self.name]
768 chars_split = list(chars)
770 return ''.join(chars_split)