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,
51 factor = largest_audible_distance / 4
58 in_url = False if in_url else True
60 while random.random() > volume * factor:
63 elif c != '.' and c != ' ':
71 largest_audible_distance = 20
72 obstacles = [t.position for t in self.game.things if t.blocks_sound]
73 sound_blockers = self.game.get_sound_blockers()
74 dijkstra_map = DijkstraMap(sound_blockers, obstacles, self.game.maps,
75 self.position, largest_audible_distance,
78 for m in re.finditer('https?://[^\s]+', msg):
79 url_limits += [m.start(), m.end()]
80 for c_id in self.game.sessions:
81 listener = self.game.get_player(c_id)
82 target_yx = dijkstra_map.target_yx(*listener.position, True)
85 listener_distance = dijkstra_map[target_yx]
86 if listener_distance > largest_audible_distance:
88 volume = 1 / max(1, listener_distance)
89 lowered_msg = lower_msg_by_volume(msg, volume,
90 largest_audible_distance,
92 lowered_nick = lower_msg_by_volume(name, volume,
93 largest_audible_distance)
95 if listener.fov_test(self.position[0], self.position[1]):
96 self.game.io.send('CHATFACE %s' % self.id_, c_id)
97 if self.type_ == 'Player' and hasattr(self, 'thing_char'):
98 symbol = '/@' + self.thing_char
99 self.game.io.send('CHAT ' +
100 quote('vol:%.f%s %s%s: %s' % (volume * 100, '%',
101 lowered_nick, symbol,
107 class Thing_Item(Thing):
113 class ThingSpawner(Thing):
117 for t in [t for t in self.game.things
118 if t != self and t.position == self.position]:
120 self.game.add_thing(self.child_type, self.position)
124 class Thing_ItemSpawner(ThingSpawner):
129 class Thing_SpawnPointSpawner(ThingSpawner):
130 child_type = 'SpawnPoint'
134 class Thing_SpawnPoint(Thing):
141 class ThingInstallable(Thing):
146 self.portable = False
153 class Thing_DoorSpawner(ThingSpawner):
158 class Thing_Door(ThingInstallable):
160 blocks_movement = False
163 self.blocks_movement = False
164 self.blocks_light = False
165 self.blocks_sound = False
169 self.blocks_movement = True
170 self.blocks_light = True
171 self.blocks_sound = True
172 self.thing_char = '#'
176 class Thing_Psychedelic(Thing):
182 class Thing_PsychedelicSpawner(ThingSpawner):
184 child_type = 'Psychedelic'
188 class Thing_Bottle(Thing):
196 self.thing_char = '_'
200 all_players = [t for t in self.game.things if t.type_ == 'Player']
201 # TODO: refactor with ThingPlayer.prepare_multiprocessible_fov_stencil
202 # and ThingPlayer.fov_test
203 fov_map_class = self.game.map_geometry.fov_map_class
205 light_blockers = self.game.get_light_blockers()
206 obstacles = [t.position for t in self.game.things if t.blocks_light]
207 fov = fov_map_class(light_blockers, obstacles, self.game.maps,
208 self.position, fov_radius, self.game.get_map)
211 for p in all_players:
212 test_position = fov.target_yx(p.position[0], p.position[1])
213 if fov.inside(test_position) and fov[test_position] == '.':
214 visible_players += [p]
215 if len(visible_players) == 0:
216 self.sound('BOTTLE', 'no visible players in spin range')
217 pick = random.choice(visible_players)
218 self.sound('BOTTLE', 'BOTTLE picks: ' + pick.name)
222 class Thing_BottleSpawner(ThingSpawner):
223 child_type = 'Bottle'
227 class Thing_Hat(Thing):
230 design = ' +--+ ' + ' | | ' + '======'
235 new_design += self.design[12]
236 new_design += self.design[13]
237 new_design += self.design[6]
238 new_design += self.design[7]
239 new_design += self.design[0]
240 new_design += self.design[1]
241 new_design += self.design[14]
242 new_design += self.design[15]
243 new_design += self.design[8]
244 new_design += self.design[9]
245 new_design += self.design[2]
246 new_design += self.design[3]
247 new_design += self.design[16]
248 new_design += self.design[17]
249 new_design += self.design[10]
250 new_design += self.design[11]
251 new_design += self.design[4]
252 new_design += self.design[5]
253 self.design = ''.join(new_design)
257 class Thing_HatRemixer(Thing):
260 def accept(self, hat):
263 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
265 new_design += random.choice(list(legal_chars))
266 hat.design = new_design
267 self.sound('HAT REMIXER', 'remixing a hat …')
268 self.game.changed = True
269 self.game.record_change(self.position, 'other')
274 class Thing_MusicPlayer(Thing):
279 next_song_start = datetime.datetime.now()
283 def __init__(self, *args, **kwargs):
284 super().__init__(*args, **kwargs)
285 self.next_song_start = datetime.datetime.now()
289 if (not self.playing) or len(self.playlist) == 0:
291 if datetime.datetime.now() > self.next_song_start:
292 self.playlist_index += 1
293 if self.playlist_index == len(self.playlist):
294 self.playlist_index = 0
298 song_data = self.playlist[self.playlist_index]
299 self.next_song_start = datetime.datetime.now() +\
300 datetime.timedelta(seconds=song_data[1])
301 self.sound('MUSICPLAYER', song_data[0])
302 self.game.changed = True
304 def interpret(self, command):
306 if command == 'HELP':
307 msg_lines += ['available commands:']
308 msg_lines += ['HELP – show this help']
309 msg_lines += ['ON/OFF – toggle playback on/off']
310 msg_lines += ['REWIND – return to start of playlist']
311 msg_lines += ['LIST – list programmed songs, durations']
312 msg_lines += ['SKIP – to skip to next song']
313 msg_lines += ['REPEAT – toggle playlist repeat on/off']
314 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
316 elif command == 'LIST':
317 msg_lines += ['playlist:']
319 for entry in self.playlist:
320 minutes = entry[1] // 60
321 seconds = entry[1] % 60
323 seconds = '0%s' % seconds
324 selector = 'next:' if i == self.playlist_index else ' '
325 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
328 elif command == 'ON/OFF':
329 self.playing = False if self.playing else True
330 self.game.changed = True
335 elif command == 'REMOVE':
336 if len(self.playlist) == 0:
337 return ['playlist already empty']
338 del self.playlist[max(0, self.playlist_index)]
339 self.playlist_index -= 1
340 if self.playlist_index < -1:
341 self.playlist_index = -1
342 self.game.changed = True
343 return ['removed song']
344 elif command == 'REWIND':
345 self.playlist_index = -1
346 self.next_song_start = datetime.datetime.now()
347 self.game.changed = True
348 return ['back at start of playlist']
349 elif command == 'SKIP':
350 self.next_song_start = datetime.datetime.now()
351 self.game.changed = True
353 elif command == 'REPEAT':
354 self.repeat = False if self.repeat else True
355 self.game.changed = True
357 return ['playlist repeat turned on']
359 return ['playlist repeat turned off']
360 elif command.startswith('ADD '):
361 tokens = command.split(' ', 2)
363 return ['wrong syntax, see HELP']
364 length = tokens[1].split(':')
366 return ['wrong syntax, see HELP']
368 minutes = int(length[0])
369 seconds = int(length[1])
371 return ['wrong syntax, see HELP']
372 self.playlist += [(tokens[2], minutes * 60 + seconds)]
373 self.game.changed = True
376 return ['cannot understand command']
380 class Thing_BottleDeposit(Thing):
385 if self.bottle_counter >= 3:
386 self.bottle_counter = 0
387 choice = random.choice(['MusicPlayer', 'Hat'])
388 self.game.add_thing(choice, self.position)
389 msg = 'here is a gift as a reward for ecological consciousness –'
390 if choice == 'MusicPlayer':
391 msg += 'pick it up and then use "command thing" on it!'
392 elif choice == 'Hat':
393 msg += 'pick it up and then use "(un-)wear" on it!'
394 self.sound('BOTTLE DEPOSITOR', msg)
397 self.bottle_counter += 1
398 self.sound('BOTTLE DEPOSITOR',
399 'thanks for this empty bottle – deposit %s more for a gift!' %
400 (3 - self.bottle_counter))
404 class Thing_Cookie(Thing):
408 def __init__(self, *args, **kwargs):
410 super().__init__(*args, **kwargs)
411 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
412 self.thing_char = random.choice(list(legal_chars))
416 class Thing_CookieSpawner(Thing):
419 def accept(self, thing):
420 self.sound('OVEN', '*heat* *brrzt* here\'s a cookie!')
421 self.game.add_thing('Cookie', self.position)
425 class ThingAnimate(Thing):
426 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):
568 def __init__(self, *args, **kwargs):
569 super().__init__(*args, **kwargs)
576 if self.tripping >= 0:
578 if self.need_for_toilet > 0:
579 self.need_for_toilet += 1
580 terrain = self.game.maps[self.position[0]][self.position[1]]
581 if terrain in self.game.terrains:
582 terrain_type = self.game.terrains[terrain]
583 if 'toilet' in terrain_type.tags:
584 self.send_msg('CHAT "You use the toilet. What a relief!"')
585 self.need_for_toilet = 0
586 if 10000 * random.random() < self.need_for_toilet / 100000:
587 self.send_msg('CHAT "You need to use a toilet."')
589 self.send_msg('CHAT "You sober up."')
590 self.invalidate('fov')
591 self.game.changed = True
592 self.need_for_toilet *= 2
593 self.need_for_toilet += 1
594 if self.tripping == 0:
595 self.send_msg('DEFAULT_COLORS')
596 self.send_msg('CHAT "You sober up."')
597 self.game.changed = True
598 elif self.tripping > 0 and self.tripping % 250 == 0:
599 self.send_msg('RANDOM_COLORS')
600 self.game.changed = True
602 def send_msg(self, msg):
603 for c_id in self.game.sessions:
604 if self.game.sessions[c_id]['thing_id'] == self.id_:
605 self.game.io.send(msg, c_id)
614 def add_cookie_char(self, c):
615 if not self.name in self.game.players_hat_chars:
616 self.game.players_hat_chars[self.name] = ' #' # default
617 if not c in self.game.players_hat_chars[self.name]:
618 self.game.players_hat_chars[self.name] += c
620 def get_cookie_chars(self):
621 if self.name in self.game.players_hat_chars:
622 return self.game.players_hat_chars[self.name]
623 return ' #' # default