1 from plomrogue.errors import GameError, PlayError
2 from plomrogue.mapping import YX
10 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
13 self.id_ = self.game.new_thing_id()
16 self.position = position
20 class Thing(ThingBase):
26 def __init__(self, *args, **kwargs):
27 super().__init__(*args, **kwargs)
34 return self.__class__.get_type()
38 return cls.__name__[len('Thing_'):]
40 def sound(self, name, msg):
41 from plomrogue.mapping import DijkstraMap
42 from plomrogue.misc import quote
44 def lower_msg_by_volume(msg, volume, largest_audible_distance):
46 factor = largest_audible_distance / 4
50 while random.random() > volume * factor:
53 elif c != '.' and c != ' ':
60 largest_audible_distance = 20
61 # player's don't block sound (or should they?)
62 things = [t for t in self.game.things if t.type_ != 'Player']
63 dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
64 largest_audible_distance, self.game.get_map)
65 for c_id in self.game.sessions:
66 listener = self.game.get_player(c_id)
67 target_yx = dijkstra_map.target_yx(*listener.position, True)
70 listener_distance = dijkstra_map[target_yx]
71 if listener_distance > largest_audible_distance:
73 volume = 1 / max(1, listener_distance)
74 lowered_msg = lower_msg_by_volume(msg, volume,
75 largest_audible_distance)
76 lowered_nick = lower_msg_by_volume(name, volume,
77 largest_audible_distance)
78 self.game.io.send('CHAT ' +
79 quote('(volume: %.2f) %s: %s' % (volume,
86 class Thing_Item(Thing):
92 class ThingSpawner(Thing):
96 for t in [t for t in self.game.things
97 if t != self and t.position == self.position]:
99 t = self.game.thing_types[self.child_type](self.game,
100 position=self.position)
101 self.game.things += [t]
102 self.game.changed = True
103 self.game.changed_fovs = True
107 class Thing_ItemSpawner(ThingSpawner):
112 class Thing_SpawnPointSpawner(ThingSpawner):
113 child_type = 'SpawnPoint'
117 class Thing_SpawnPoint(Thing):
124 class Thing_DoorSpawner(ThingSpawner):
129 class Thing_Door(Thing):
136 self.blocking = False
141 self.thing_char = '#'
144 self.portable = False
151 class Thing_Bottle(Thing):
158 self.thing_char = '_'
163 class Thing_BottleSpawner(ThingSpawner):
164 child_type = 'Bottle'
168 class Thing_Hat(Thing):
171 design = ' +--+ ' + ' | | ' + '======'
175 class Thing_HatRemixer(Thing):
178 def accept(self, hat):
181 legal_chars = string.ascii_letters + string.digits + string.punctuation + ' '
183 new_design += random.choice(list(legal_chars))
184 hat.design = new_design
185 self.sound('HAT REMIXER', 'remixing a hat …')
186 self.game.changed = True
191 class Thing_MusicPlayer(Thing):
196 next_song_start = datetime.datetime.now()
200 def __init__(self, *args, **kwargs):
201 super().__init__(*args, **kwargs)
202 self.next_song_start = datetime.datetime.now()
206 if (not self.playing) or len(self.playlist) == 0:
208 if datetime.datetime.now() > self.next_song_start:
209 self.playlist_index += 1
210 if self.playlist_index == len(self.playlist):
211 self.playlist_index = 0
215 song_data = self.playlist[self.playlist_index]
216 self.next_song_start = datetime.datetime.now() +\
217 datetime.timedelta(seconds=song_data[1])
218 self.sound('MUSICPLAYER', song_data[0])
219 self.game.changed = True
221 def interpret(self, command):
223 if command == 'HELP':
224 msg_lines += ['available commands:']
225 msg_lines += ['HELP – show this help']
226 msg_lines += ['ON/OFF – toggle playback on/off']
227 msg_lines += ['REWIND – return to start of playlist']
228 msg_lines += ['LIST – list programmed songs, durations']
229 msg_lines += ['SKIP – to skip to next song']
230 msg_lines += ['REPEAT – toggle playlist repeat on/off']
231 msg_lines += ['ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"']
233 elif command == 'LIST':
234 msg_lines += ['playlist:']
236 for entry in self.playlist:
237 minutes = entry[1] // 60
238 seconds = entry[1] % 60
240 seconds = '0%s' % seconds
241 selector = 'next:' if i == self.playlist_index else ' '
242 msg_lines += ['%s %s:%s – %s' % (selector, minutes, seconds, entry[0])]
245 elif command == 'ON/OFF':
246 self.playing = False if self.playing else True
247 self.game.changed = True
252 elif command == 'REMOVE':
253 if len(self.playlist) == 0:
254 return ['playlist already empty']
255 del self.playlist[max(0, self.playlist_index)]
256 self.playlist_index -= 1
257 if self.playlist_index < -1:
258 self.playlist_index = -1
259 self.game.changed = True
260 return ['removed song']
261 elif command == 'REWIND':
262 self.playlist_index = -1
263 self.next_song_start = datetime.datetime.now()
264 self.game.changed = True
265 return ['back at start of playlist']
266 elif command == 'SKIP':
267 self.next_song_start = datetime.datetime.now()
268 self.game.changed = True
270 elif command == 'REPEAT':
271 self.repeat = False if self.repeat else True
272 self.game.changed = True
274 return ['playlist repeat turned on']
276 return ['playlist repeat turned off']
277 elif command.startswith('ADD '):
278 tokens = command.split(' ', 2)
280 return ['wrong syntax, see HELP']
281 length = tokens[1].split(':')
283 return ['wrong syntax, see HELP']
285 minutes = int(length[0])
286 seconds = int(length[1])
288 return ['wrong syntax, see HELP']
289 self.playlist += [(tokens[2], minutes * 60 + seconds)]
290 self.game.changed = True
293 return ['cannot understand command']
297 class Thing_BottleDeposit(Thing):
302 if self.bottle_counter >= 3:
303 self.bottle_counter = 0
304 choice = random.choice(['MusicPlayer', 'Hat'])
305 t = self.game.thing_types[choice](self.game, position=self.position)
306 self.game.things += [t]
307 msg = 'here is a gift as a reward for ecological consciousness –'
308 if choice == 'MusicPlayer':
309 msg += 'pick it up and then use "command thing" on it!'
310 elif choice == 'Hat':
311 msg += 'pick it up and then use "(un-)wear" on it!'
312 self.sound('BOTTLE DEPOSITOR', msg)
313 self.game.changed = True
314 self.game.changed_fovs = True
317 self.bottle_counter += 1
318 self.sound('BOTTLE DEPOSITOR',
319 'thanks for this empty bottle – deposit %s more for a gift!' %
320 (3 - self.bottle_counter))
325 class ThingAnimate(Thing):
329 def __init__(self, *args, **kwargs):
330 super().__init__(*args, **kwargs)
331 self.next_task = [None]
333 self.invalidate_map_view()
335 def invalidate_map_view(self):
337 self._visible_terrain = None
338 self._visible_control = None
340 def set_next_task(self, task_name, args=()):
341 task_class = self.game.tasks[task_name]
342 self.next_task = [task_class(self, args)]
344 def get_next_task(self):
345 if self.next_task[0]:
346 task = self.next_task[0]
347 self.next_task = [None]
354 for c_id in self.game.sessions:
355 if self.game.sessions[c_id]['thing_id'] == self.id_:
356 # TODO: refactor with self.send_msg
357 self.game.io.send('DEFAULT_COLORS', c_id)
358 self.game.io.send('CHAT "You sober up."', c_id)
359 self.game.changed_fovs = True
361 self.game.changed = True
362 if self.task is None:
363 self.task = self.get_next_task()
367 except (PlayError, GameError) as e:
371 if self.task.todo <= 0:
373 self.game.changed = True
374 self.task = self.get_next_task()
376 def prepare_multiprocessible_fov_stencil(self):
377 fov_map_class = self.game.map_geometry.fov_map_class
378 fov_radius = 3 if self.drunk > 0 else 12
379 self._fov = fov_map_class(self.game.things, self.game.maps,
380 self.position, fov_radius, self.game.get_map)
382 def multiprocessible_fov_stencil(self):
383 self._fov.init_terrain()
386 def fov_stencil(self):
389 # due to the pre-multiprocessing in game.send_gamestate,
390 # the following should actually never be called
391 self.prepare_multiprocessible_fov_stencil()
392 self.multiprocessible_fov_stencil()
395 def fov_stencil_make(self):
398 def fov_test(self, big_yx, little_yx):
399 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
400 if self.fov_stencil.inside(test_position):
401 if self.fov_stencil[test_position] == '.':
405 def fov_stencil_map(self, map_type):
407 for yx in self.fov_stencil:
408 if self.fov_stencil[yx] == '.':
409 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
410 map_ = self.game.get_map(big_yx, map_type)
411 visible_terrain += map_[little_yx]
413 visible_terrain += ' '
414 return visible_terrain
417 def visible_terrain(self):
418 if self._visible_terrain:
419 return self._visible_terrain
420 self._visible_terrain = self.fov_stencil_map('normal')
421 return self._visible_terrain
424 def visible_control(self):
425 if self._visible_control:
426 return self._visible_control
427 self._visible_control = self.fov_stencil_map('control')
428 return self._visible_control
432 class Thing_Player(ThingAnimate):
435 def __init__(self, *args, **kwargs):
436 super().__init__(*args, **kwargs)
439 def send_msg(self, msg):
440 for c_id in self.game.sessions:
441 if self.game.sessions[c_id]['thing_id'] == self.id_:
442 self.game.io.send(msg, c_id)