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):
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)
93 if listener.fov_test(self.position[0], self.position[1]):
94 self.game.io.send('CHATFACE %s' % self.id_, c_id)
95 if self.type_ == 'Player' and hasattr(self, 'thing_char'):
96 symbol = '/@' + self.thing_char
97 self.game.io.send('CHAT ' +
98 quote('vol:%.f%s %s%s: %s' % (volume * 100, '%',
105 class Thing_Item(Thing):
111 class ThingSpawner(Thing):
115 for t in [t for t in self.game.things
116 if t != self and t.position == self.position]:
118 self.game.add_thing(self.child_type, self.position)
119 # self.game.changed = True handled by add_thing
123 class Thing_ItemSpawner(ThingSpawner):
128 class Thing_SpawnPointSpawner(ThingSpawner):
129 child_type = 'SpawnPoint'
133 class Thing_SpawnPoint(Thing):
140 class Thing_DoorSpawner(ThingSpawner):
145 class Thing_Door(Thing):
152 self.blocking = False
157 self.thing_char = '#'
160 self.portable = False
167 class Thing_Bottle(Thing):
175 self.thing_char = '_'
180 all_players = [t for t in self.game.things if t.type_ == 'Player']
181 # TODO: refactor with ThingPlayer.prepare_multiprocessible_fov_stencil
182 # and ThingPlayer.fov_test
183 fov_map_class = self.game.map_geometry.fov_map_class
185 fov = fov_map_class(self.game.things, self.game.maps,
186 self.position, fov_radius, self.game.get_map)
189 for p in all_players:
190 test_position = fov.target_yx(p.position[0], p.position[1])
191 if fov.inside(test_position) and fov[test_position] == '.':
192 visible_players += [p]
193 if len(visible_players) == 0:
194 self.sound('BOTTLE', 'no visible players in spin range')
195 pick = random.choice(visible_players)
196 self.sound('BOTTLE', 'BOTTLE picks: ' + pick.name)
200 class Thing_BottleSpawner(ThingSpawner):
201 child_type = 'Bottle'
205 class Thing_Hat(Thing):
208 design = ' +--+ ' + ' | | ' + '======'
213 new_design += self.design[12]
214 new_design += self.design[13]
215 new_design += self.design[6]
216 new_design += self.design[7]
217 new_design += self.design[0]
218 new_design += self.design[1]
219 new_design += self.design[14]
220 new_design += self.design[15]
221 new_design += self.design[8]
222 new_design += self.design[9]
223 new_design += self.design[2]
224 new_design += self.design[3]
225 new_design += self.design[16]
226 new_design += self.design[17]
227 new_design += self.design[10]
228 new_design += self.design[11]
229 new_design += self.design[4]
230 new_design += self.design[5]
231 self.design = ''.join(new_design)
235 class Thing_HatRemixer(Thing):
238 def accept(self, hat):
241 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
243 new_design += random.choice(list(legal_chars))
244 hat.design = new_design
245 self.sound('HAT REMIXER', 'remixing a hat …')
246 self.game.changed = True
247 # FIXME: pseudo-FOV-change actually
248 self.game.record_fov_change(self.thing.position)
253 class Thing_MusicPlayer(Thing):
258 next_song_start = datetime.datetime.now()
262 def __init__(self, *args, **kwargs):
263 super().__init__(*args, **kwargs)
264 self.next_song_start = datetime.datetime.now()
268 if (not self.playing) or len(self.playlist) == 0:
270 if datetime.datetime.now() > self.next_song_start:
271 self.playlist_index += 1
272 if self.playlist_index == len(self.playlist):
273 self.playlist_index = 0
277 song_data = self.playlist[self.playlist_index]
278 self.next_song_start = datetime.datetime.now() +\
279 datetime.timedelta(seconds=song_data[1])
280 self.sound('MUSICPLAYER', song_data[0])
281 self.game.changed = True
283 def interpret(self, command):
285 if command == 'HELP':
286 msg_lines += ['available commands:']
287 msg_lines += ['HELP – show this help']
288 msg_lines += ['ON/OFF – toggle playback on/off']
289 msg_lines += ['REWIND – return to start of playlist']
290 msg_lines += ['LIST – list programmed songs, durations']
291 msg_lines += ['SKIP – to skip to next song']
292 msg_lines += ['REPEAT – toggle playlist repeat on/off']
293 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
295 elif command == 'LIST':
296 msg_lines += ['playlist:']
298 for entry in self.playlist:
299 minutes = entry[1] // 60
300 seconds = entry[1] % 60
302 seconds = '0%s' % seconds
303 selector = 'next:' if i == self.playlist_index else ' '
304 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
307 elif command == 'ON/OFF':
308 self.playing = False if self.playing else True
309 self.game.changed = True
314 elif command == 'REMOVE':
315 if len(self.playlist) == 0:
316 return ['playlist already empty']
317 del self.playlist[max(0, self.playlist_index)]
318 self.playlist_index -= 1
319 if self.playlist_index < -1:
320 self.playlist_index = -1
321 self.game.changed = True
322 return ['removed song']
323 elif command == 'REWIND':
324 self.playlist_index = -1
325 self.next_song_start = datetime.datetime.now()
326 self.game.changed = True
327 return ['back at start of playlist']
328 elif command == 'SKIP':
329 self.next_song_start = datetime.datetime.now()
330 self.game.changed = True
332 elif command == 'REPEAT':
333 self.repeat = False if self.repeat else True
334 self.game.changed = True
336 return ['playlist repeat turned on']
338 return ['playlist repeat turned off']
339 elif command.startswith('ADD '):
340 tokens = command.split(' ', 2)
342 return ['wrong syntax, see HELP']
343 length = tokens[1].split(':')
345 return ['wrong syntax, see HELP']
347 minutes = int(length[0])
348 seconds = int(length[1])
350 return ['wrong syntax, see HELP']
351 self.playlist += [(tokens[2], minutes * 60 + seconds)]
352 self.game.changed = True
355 return ['cannot understand command']
359 class Thing_BottleDeposit(Thing):
364 if self.bottle_counter >= 3:
365 self.bottle_counter = 0
366 choice = random.choice(['MusicPlayer', 'Hat'])
367 self.game.add_thing(choice, self.position)
368 msg = 'here is a gift as a reward for ecological consciousness –'
369 if choice == 'MusicPlayer':
370 msg += 'pick it up and then use "command thing" on it!'
371 elif choice == 'Hat':
372 msg += 'pick it up and then use "(un-)wear" on it!'
373 self.sound('BOTTLE DEPOSITOR', msg)
374 # self.game.changed = True done by game.add_thing
377 self.bottle_counter += 1
378 self.sound('BOTTLE DEPOSITOR',
379 'thanks for this empty bottle – deposit %s more for a gift!' %
380 (3 - self.bottle_counter))
385 class ThingAnimate(Thing):
389 def __init__(self, *args, **kwargs):
390 super().__init__(*args, **kwargs)
391 self.next_task = [None]
393 self.invalidate_map_view()
395 def invalidate_map_view(self):
397 self._visible_terrain = None
398 self._visible_control = None
399 self._seen_things = None
401 def set_next_task(self, task_name, args=()):
402 task_class = self.game.tasks[task_name]
403 self.next_task = [task_class(self, args)]
405 def get_next_task(self):
406 if self.next_task[0]:
407 task = self.next_task[0]
408 self.next_task = [None]
415 for c_id in self.game.sessions:
416 if self.game.sessions[c_id]['thing_id'] == self.id_:
417 # TODO: refactor with self.send_msg
418 self.game.io.send('DEFAULT_COLORS', c_id)
419 self.game.io.send('CHAT "You sober up."', c_id)
420 #self.invalidate_map_view()
421 # FIXME: pseudo-FOV-change actually
422 self.game.record_fov_change(self.thing.position)
424 self.game.changed = True
425 if self.task is None:
426 self.task = self.get_next_task()
430 except (PlayError, GameError) as e:
434 if self.task.todo <= 0:
436 self.game.changed = True
437 self.task = self.get_next_task()
439 def prepare_multiprocessible_fov_stencil(self):
440 fov_map_class = self.game.map_geometry.fov_map_class
441 fov_radius = 3 if self.drunk > 0 else 12
442 self._fov = fov_map_class(self.game.things, self.game.maps,
443 self.position, fov_radius, self.game.get_map)
445 def multiprocessible_fov_stencil(self):
446 self._fov.init_terrain()
449 def fov_stencil(self):
452 # due to the pre-multiprocessing in game.send_gamestate,
453 # the following should actually never be called
454 self.prepare_multiprocessible_fov_stencil()
455 self.multiprocessible_fov_stencil()
458 def fov_stencil_make(self):
461 def fov_test(self, big_yx, little_yx):
462 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
463 if self.fov_stencil.inside(test_position):
464 if self.fov_stencil[test_position] == '.':
468 def fov_stencil_map(self, map_type):
470 for yx in self.fov_stencil:
471 if self.fov_stencil[yx] == '.':
472 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
473 map_ = self.game.get_map(big_yx, map_type)
474 visible_terrain += map_[little_yx]
476 visible_terrain += ' '
477 return visible_terrain
480 def visible_terrain(self):
481 if self._visible_terrain:
482 return self._visible_terrain
483 self._visible_terrain = self.fov_stencil_map('normal')
484 return self._visible_terrain
487 def visible_control(self):
488 if self._visible_control:
489 return self._visible_control
490 self._visible_control = self.fov_stencil_map('control')
491 return self._visible_control
494 def seen_things(self):
495 if self._seen_things is not None:
496 return self._seen_things
497 self._seen_things = [t for t in self.game.things
498 if self.fov_test(*t.position)]
499 return self._seen_things
502 class Thing_Player(ThingAnimate):
505 def __init__(self, *args, **kwargs):
506 super().__init__(*args, **kwargs)
509 def send_msg(self, msg):
510 for c_id in self.game.sessions:
511 if self.game.sessions[c_id]['thing_id'] == self.id_:
512 self.game.io.send(msg, c_id)