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
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
251 class Thing_MusicPlayer(Thing):
256 next_song_start = datetime.datetime.now()
260 def __init__(self, *args, **kwargs):
261 super().__init__(*args, **kwargs)
262 self.next_song_start = datetime.datetime.now()
266 if (not self.playing) or len(self.playlist) == 0:
268 if datetime.datetime.now() > self.next_song_start:
269 self.playlist_index += 1
270 if self.playlist_index == len(self.playlist):
271 self.playlist_index = 0
275 song_data = self.playlist[self.playlist_index]
276 self.next_song_start = datetime.datetime.now() +\
277 datetime.timedelta(seconds=song_data[1])
278 self.sound('MUSICPLAYER', song_data[0])
279 self.game.changed = True
281 def interpret(self, command):
283 if command == 'HELP':
284 msg_lines += ['available commands:']
285 msg_lines += ['HELP – show this help']
286 msg_lines += ['ON/OFF – toggle playback on/off']
287 msg_lines += ['REWIND – return to start of playlist']
288 msg_lines += ['LIST – list programmed songs, durations']
289 msg_lines += ['SKIP – to skip to next song']
290 msg_lines += ['REPEAT – toggle playlist repeat on/off']
291 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
293 elif command == 'LIST':
294 msg_lines += ['playlist:']
296 for entry in self.playlist:
297 minutes = entry[1] // 60
298 seconds = entry[1] % 60
300 seconds = '0%s' % seconds
301 selector = 'next:' if i == self.playlist_index else ' '
302 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
305 elif command == 'ON/OFF':
306 self.playing = False if self.playing else True
307 self.game.changed = True
312 elif command == 'REMOVE':
313 if len(self.playlist) == 0:
314 return ['playlist already empty']
315 del self.playlist[max(0, self.playlist_index)]
316 self.playlist_index -= 1
317 if self.playlist_index < -1:
318 self.playlist_index = -1
319 self.game.changed = True
320 return ['removed song']
321 elif command == 'REWIND':
322 self.playlist_index = -1
323 self.next_song_start = datetime.datetime.now()
324 self.game.changed = True
325 return ['back at start of playlist']
326 elif command == 'SKIP':
327 self.next_song_start = datetime.datetime.now()
328 self.game.changed = True
330 elif command == 'REPEAT':
331 self.repeat = False if self.repeat else True
332 self.game.changed = True
334 return ['playlist repeat turned on']
336 return ['playlist repeat turned off']
337 elif command.startswith('ADD '):
338 tokens = command.split(' ', 2)
340 return ['wrong syntax, see HELP']
341 length = tokens[1].split(':')
343 return ['wrong syntax, see HELP']
345 minutes = int(length[0])
346 seconds = int(length[1])
348 return ['wrong syntax, see HELP']
349 self.playlist += [(tokens[2], minutes * 60 + seconds)]
350 self.game.changed = True
353 return ['cannot understand command']
357 class Thing_BottleDeposit(Thing):
362 if self.bottle_counter >= 3:
363 self.bottle_counter = 0
364 choice = random.choice(['MusicPlayer', 'Hat'])
365 self.game.add_thing(choice, self.position)
366 msg = 'here is a gift as a reward for ecological consciousness –'
367 if choice == 'MusicPlayer':
368 msg += 'pick it up and then use "command thing" on it!'
369 elif choice == 'Hat':
370 msg += 'pick it up and then use "(un-)wear" on it!'
371 self.sound('BOTTLE DEPOSITOR', msg)
372 self.game.changed = True
375 self.bottle_counter += 1
376 self.sound('BOTTLE DEPOSITOR',
377 'thanks for this empty bottle – deposit %s more for a gift!' %
378 (3 - self.bottle_counter))
383 class ThingAnimate(Thing):
387 def __init__(self, *args, **kwargs):
388 super().__init__(*args, **kwargs)
389 self.next_task = [None]
391 self.invalidate_map_view()
393 def invalidate_map_view(self):
395 self._visible_terrain = None
396 self._visible_control = None
398 def set_next_task(self, task_name, args=()):
399 task_class = self.game.tasks[task_name]
400 self.next_task = [task_class(self, args)]
402 def get_next_task(self):
403 if self.next_task[0]:
404 task = self.next_task[0]
405 self.next_task = [None]
412 for c_id in self.game.sessions:
413 if self.game.sessions[c_id]['thing_id'] == self.id_:
414 # TODO: refactor with self.send_msg
415 self.game.io.send('DEFAULT_COLORS', c_id)
416 self.game.io.send('CHAT "You sober up."', c_id)
417 self.invalidate_map_view()
419 self.game.changed = True
420 if self.task is None:
421 self.task = self.get_next_task()
425 except (PlayError, GameError) as e:
429 if self.task.todo <= 0:
431 self.game.changed = True
432 self.task = self.get_next_task()
434 def prepare_multiprocessible_fov_stencil(self):
435 fov_map_class = self.game.map_geometry.fov_map_class
436 fov_radius = 3 if self.drunk > 0 else 12
437 self._fov = fov_map_class(self.game.things, self.game.maps,
438 self.position, fov_radius, self.game.get_map)
440 def multiprocessible_fov_stencil(self):
441 self._fov.init_terrain()
444 def fov_stencil(self):
447 # due to the pre-multiprocessing in game.send_gamestate,
448 # the following should actually never be called
449 self.prepare_multiprocessible_fov_stencil()
450 self.multiprocessible_fov_stencil()
453 def fov_stencil_make(self):
456 def fov_test(self, big_yx, little_yx):
457 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
458 if self.fov_stencil.inside(test_position):
459 if self.fov_stencil[test_position] == '.':
463 def fov_stencil_map(self, map_type):
465 for yx in self.fov_stencil:
466 if self.fov_stencil[yx] == '.':
467 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
468 map_ = self.game.get_map(big_yx, map_type)
469 visible_terrain += map_[little_yx]
471 visible_terrain += ' '
472 return visible_terrain
475 def visible_terrain(self):
476 if self._visible_terrain:
477 return self._visible_terrain
478 self._visible_terrain = self.fov_stencil_map('normal')
479 return self._visible_terrain
482 def visible_control(self):
483 if self._visible_control:
484 return self._visible_control
485 self._visible_control = self.fov_stencil_map('control')
486 return self._visible_control
490 class Thing_Player(ThingAnimate):
493 def __init__(self, *args, **kwargs):
494 super().__init__(*args, **kwargs)
497 def send_msg(self, msg):
498 for c_id in self.game.sessions:
499 if self.game.sessions[c_id]['thing_id'] == self.id_:
500 self.game.io.send(msg, c_id)