1 from plomrogue.errors import GameError
2 from plomrogue.mapping import YX
9 def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
12 self.id_ = self.game.new_thing_id()
15 self.position = position
19 class Thing(ThingBase):
25 def __init__(self, *args, **kwargs):
26 super().__init__(*args, **kwargs)
33 return self.__class__.get_type()
37 return cls.__name__[len('Thing_'):]
39 def sound(self, name, msg):
40 from plomrogue.mapping import DijkstraMap
41 from plomrogue.misc import quote
43 def lower_msg_by_volume(msg, volume, largest_audible_distance):
45 factor = largest_audible_distance / 4
49 while random.random() > volume * factor:
52 elif c != '.' and c != ' ':
59 largest_audible_distance = 20
60 # player's don't block sound (or should they?)
61 things = [t for t in self.game.things if t.type_ != 'Player']
62 dijkstra_map = DijkstraMap(things, self.game.maps, self.position,
63 largest_audible_distance, self.game.get_map)
64 for c_id in self.game.sessions:
65 listener = self.game.get_player(c_id)
66 target_yx = dijkstra_map.target_yx(*listener.position, True)
69 listener_distance = dijkstra_map[target_yx]
70 if listener_distance > largest_audible_distance:
72 volume = 1 / max(1, listener_distance)
73 lowered_msg = lower_msg_by_volume(msg, volume,
74 largest_audible_distance)
75 lowered_nick = lower_msg_by_volume(name, volume,
76 largest_audible_distance)
77 self.game.io.send('CHAT ' +
78 quote('(volume: %.2f) %s: %s' % (volume,
85 class Thing_Item(Thing):
91 class ThingSpawner(Thing):
95 for t in [t for t in self.game.things
96 if t != self and t.position == self.position]:
98 t = self.game.thing_types[self.child_type](self.game,
99 position=self.position)
100 self.game.things += [t]
101 self.game.changed = True
105 class Thing_ItemSpawner(ThingSpawner):
110 class Thing_SpawnPointSpawner(ThingSpawner):
111 child_type = 'SpawnPoint'
115 class Thing_SpawnPoint(Thing):
122 class Thing_DoorSpawner(ThingSpawner):
127 class Thing_Door(Thing):
133 self.blocking = False
139 self.portable = False
140 self.thing_char = '#'
144 class Thing_Consumable(Thing):
150 class Thing_ConsumableSpawner(ThingSpawner):
151 child_type = 'Consumable'
156 class Thing_MusicPlayer(Thing):
162 next_song_start = datetime.datetime.now()
166 def __init__(self, *args, **kwargs):
167 super().__init__(*args, **kwargs)
168 self.next_song_start = datetime.datetime.now()
171 if (not self.playing) or len(self.playlist) == 0:
173 if datetime.datetime.now() > self.next_song_start:
174 song_data = self.playlist[self.playlist_index]
175 self.playlist_index += 1
176 if self.playlist_index == len(self.playlist):
177 self.playlist_index = 0
180 self.next_song_start = datetime.datetime.now() +\
181 datetime.timedelta(seconds=song_data[1])
182 self.sound('MUSICPLAYER', song_data[0])
183 self.game.changed = True
185 def interpret(self, command):
186 if command == 'HELP':
187 msg = 'available commands:\n'
188 msg += 'HELP – show this help\n'
189 msg += 'PLAY – toggle playback on/off\n'
190 msg += 'REWIND – return to start of playlist\n'
191 msg += 'LIST – list programmed songs, durations\n'
192 msg += 'SKIP – to skip to next song\n'
193 msg += 'REPEAT – toggle playlist repeat on/off\n'
194 msg += 'ADD LENGTH SONG – add SONG to playlist, with LENGTH in format "minutes:seconds", i.e. something like "0:47" or "11:02"'
196 elif command == 'LIST':
199 for entry in self.playlist:
201 minutes = entry[1] // 60
202 seconds = entry[1] % 60
204 seconds = '0%s' % seconds
205 selector = 'next:' if i == self.playlist_index else ' '
206 msg += '%s %s:%s – %s' % (selector, minutes, seconds, entry[0])
209 elif command == 'PLAY':
210 self.playing = False if self.playing else True
211 self.game.changed = True
216 elif command == 'REWIND':
217 self.playlist_index = 0
218 self.next_song_start = datetime.datetime.now()
219 self.game.changed = True
220 return 'back at start of playlist'
221 elif command == 'SKIP':
222 self.next_song_start = datetime.datetime.now()
223 self.game.changed = True
225 elif command == 'REPEAT':
226 self.repeat = False if self.repeat else True
227 self.game.changed = True
229 return 'playlist repeat turned on'
231 return 'playlist repeat turned off'
232 elif command.startswith('ADD '):
233 tokens = command.split(' ', 2)
235 return 'wrong syntax, see HELP'
236 length = tokens[1].split(':')
238 return 'wrong syntax, see HELP'
240 minutes = int(length[0])
241 seconds = int(length[1])
243 return 'wrong syntax, see HELP'
244 self.playlist += [(tokens[2], minutes * 60 + seconds)]
245 self.game.changed = True
248 return 'cannot understand command'
252 class ThingAnimate(Thing):
256 def __init__(self, *args, **kwargs):
257 super().__init__(*args, **kwargs)
259 self.set_task('WAIT')
262 def set_task(self, task_name, args=()):
263 task_class = self.game.tasks[task_name]
264 self.task = task_class(self, args)
265 self.task.check() # will throw GameError if necessary
267 def set_next_task(self, task_name, args=()):
268 task_class = self.game.tasks[task_name]
269 self.next_tasks += [task_class(self, args)]
271 def get_next_task(self):
272 if len(self.next_tasks) > 0:
273 task = self.next_tasks.pop(0)
282 for c_id in self.game.sessions:
283 if self.game.sessions[c_id]['thing_id'] == self.id_:
284 self.game.io.send('DEFAULT_COLORS', c_id)
285 self.game.io.send('CHAT "You sober up."', c_id)
286 self.game.changed = True
288 if self.task is None:
289 self.task = self.get_next_task()
293 except GameError as e:
297 if self.task.todo <= 0:
298 self._last_task_result = self.task.do()
299 self.game.changed = True
300 self.task = self.get_next_task()
303 def fov_stencil(self):
306 fov_map_class = self.game.map_geometry.fov_map_class
307 self._fov = fov_map_class(self.game.things, self.game.maps, self.position,
308 12, self.game.get_map)
311 def fov_test(self, big_yx, little_yx):
312 test_position = self.fov_stencil.target_yx(big_yx, little_yx)
313 if self.fov_stencil.inside(test_position):
314 if self.fov_stencil[test_position] == '.':
318 def fov_stencil_map(self, map_type='normal'):
320 for yx in self.fov_stencil:
321 if self.fov_stencil[yx] == '.':
322 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
323 map_ = self.game.get_map(big_yx, map_type)
324 visible_terrain += map_[little_yx]
326 visible_terrain += ' '
327 return visible_terrain
331 class Thing_Player(ThingAnimate):
334 def __init__(self, *args, **kwargs):
335 super().__init__(*args, **kwargs)