From: Christian Heller Date: Tue, 17 Nov 2020 23:20:44 +0000 (+0100) Subject: Shrink sound Dijkstra map, reach; refactor lots of mapping code. X-Git-Url: https://plomlompom.com/repos/?a=commitdiff_plain;h=70b12aa9c1fe6933cfc7e19775b67673d2e93768;p=plomrogue2 Shrink sound Dijkstra map, reach; refactor lots of mapping code. --- diff --git a/plomrogue/commands.py b/plomrogue/commands.py index c52dd4f..e40f530 100644 --- a/plomrogue/commands.py +++ b/plomrogue/commands.py @@ -1,6 +1,6 @@ from plomrogue.misc import quote from plomrogue.errors import GameError -from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map +from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, DijkstraMap @@ -25,11 +25,13 @@ cmd_TERRAINS.argtypes = '' def cmd_ALL(game, msg, connection_id): - def lower_msg_by_volume(msg, volume): + def lower_msg_by_volume(msg, volume, largest_audible_distance): + import random + factor = largest_audible_distance / 8 lowered_msg = '' for c in msg: c = c - while random.random() > volume * 8: + while random.random() > volume * factor: if c.isupper(): c = c.lower() elif c != '.' and c != ' ': @@ -39,48 +41,25 @@ def cmd_ALL(game, msg, connection_id): lowered_msg += c return lowered_msg - def dijkstra(speaker): - n_max = 255 - - map_size = game.map.size_i - dijkstra_map = [n_max for i in range(game.map.size_i)] - dijkstra_map[game.map.get_position_index(speaker.position)] = 0 - - shrunk = True - while shrunk: - shrunk = False - for i in range(map_size): - if game.map.terrain[i] == 'X': - continue - neighbors = game.map_geometry.get_neighbors_i(i) - for direction in [d for d in neighbors if neighbors[d]]: - j = neighbors[direction] - if dijkstra_map[j] < dijkstra_map[i] - 1: - dijkstra_map[i] = dijkstra_map[j] + 1 - shrunk = True - #print('DEBUG') - #line_to_print = [] - #x = 0 - #for n in dijkstra_map: - # line_to_print += ['%3s' % n] - # x += 1 - # if x >= game.map.size.x: - # x = 0 - # print(' '.join(line_to_print)) - # line_to_print = [] - return dijkstra_map - - import random if not connection_id in game.sessions: raise GameError('need to be logged in for this') speaker = game.get_thing(game.sessions[connection_id]) - dijkstra_map = dijkstra(speaker) + largest_audible_distance = 20 + dijkstra_map_class = game.map_geometry.dijkstra_map_class + dijkstra_map = dijkstra_map_class(game.map, speaker.position, + largest_audible_distance) for c_id in game.sessions: listener = game.get_thing(game.sessions[c_id]) - listener_vol = dijkstra_map[game.map.get_position_index(listener.position)] - volume = 1 / max(1, listener_vol) - lowered_msg = lower_msg_by_volume(msg, volume) - lowered_nick = lower_msg_by_volume(speaker.name, volume) + target_yx = dijkstra_map.target_yx(listener.position, True) + if not target_yx: + continue + listener_distance = dijkstra_map[target_yx] + if listener_distance > largest_audible_distance: + continue + volume = 1 / max(1, listener_distance) + lowered_msg = lower_msg_by_volume(msg, volume, largest_audible_distance) + lowered_nick = lower_msg_by_volume(speaker.name, volume, + largest_audible_distance) game.io.send('CHAT ' + quote('(volume: %.2f) %s: %s' % (volume, lowered_nick, lowered_msg)), @@ -146,31 +125,31 @@ cmd_TURN.argtypes = 'int:nonneg' def cmd_ANNOTATE(game, yx, msg, pw, connection_id): player = game.get_thing(game.sessions[connection_id]) - corrected_yx = yx + player.fov_stencil.offset - if not player.fov_test(corrected_yx): + source_yx = player.fov_stencil.source_yx(yx) + if not player.fov_test(source_yx): raise GameError('cannot annotate tile outside field of view') - if not game.can_do_tile_with_pw(corrected_yx, pw): + if not game.can_do_tile_with_pw(source_yx, pw): raise GameError('wrong password for tile') if msg == ' ': - if corrected_yx in game.annotations: - del game.annotations[corrected_yx] + if source_yx in game.annotations: + del game.annotations[source_yx] else: - game.annotations[corrected_yx] = msg + game.annotations[source_yx] = msg game.changed = True cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string' def cmd_PORTAL(game, yx, msg, pw, connection_id): player = game.get_thing(game.sessions[connection_id]) - corrected_yx = yx + player.fov_stencil.offset - if not player.fov_test(corrected_yx): + source_yx = player.fov_stencil.source_yx(yx) + if not player.fov_test(source_yx): raise GameError('cannot edit portal on tile outside field of view') - if not game.can_do_tile_with_pw(corrected_yx, pw): + if not game.can_do_tile_with_pw(source_yx, pw): raise GameError('wrong password for tile') if msg == ' ': - if corrected_yx in game.portals: - del game.portals[corrected_yx] + if source_yx in game.portals: + del game.portals[source_yx] else: - game.portals[corrected_yx] = msg + game.portals[source_yx] = msg game.changed = True cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string' @@ -186,12 +165,12 @@ cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string' def cmd_GET_ANNOTATION(game, yx, connection_id): player = game.get_thing(game.sessions[connection_id]) - corrected_yx = yx + player.fov_stencil.offset + source_yx = player.fov_stencil.source_yx(yx) annotation = '(unknown)'; - if player.fov_test(corrected_yx): + if player.fov_test(source_yx): annotation = '(none)'; - if corrected_yx in game.annotations: - annotation = game.annotations[corrected_yx] + if source_yx in game.annotations: + annotation = game.annotations[source_yx] game.io.send('ANNOTATION %s %s' % (yx, quote(annotation))) cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg' diff --git a/plomrogue/game.py b/plomrogue/game.py index 38a607e..0a33a56 100755 --- a/plomrogue/game.py +++ b/plomrogue/game.py @@ -118,17 +118,15 @@ class Game(GameBase): visible_control = player.fov_stencil_map(self.map_control) self.io.send('MAP_CONTROL %s' % quote(visible_control), c_id) for t in [t for t in self.things if player.fov_test(t.position)]: - corrected_yx = t.position - player.fov_stencil.offset - self.io.send('THING %s %s %s' % (corrected_yx, t.type_, t.id_), - c_id) + target_yx = player.fov_stencil.target_yx(t.position) + self.io.send('THING %s %s %s' % (target_yx, t.type_, t.id_), c_id) if hasattr(t, 'name'): self.io.send('THING_NAME %s %s' % (t.id_, quote(t.name)), c_id) if hasattr(t, 'player_char'): self.io.send('THING_CHAR %s %s' % (t.id_, quote(t.player_char)), c_id) for yx in [yx for yx in self.portals if player.fov_test(yx)]: - corrected_yx = yx - player.fov_stencil.offset - self.io.send('PORTAL %s %s' % (corrected_yx, + self.io.send('PORTAL %s %s' % (player.fov_stencil.target_yx(yx), quote(self.portals[yx])), c_id) self.io.send('GAME_STATE_COMPLETE') diff --git a/plomrogue/mapping.py b/plomrogue/mapping.py index 9fecffb..00b8b1d 100644 --- a/plomrogue/mapping.py +++ b/plomrogue/mapping.py @@ -75,6 +75,13 @@ class MapGeometrySquare(MapGeometryWithLeftRightMoves): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fov_map_class = FovMapSquare + self.dijkstra_map_class = DijkstraMapSquare + + def define_segment(self, source_center, radius): + size = YX(2 * radius + 1, 2 * radius + 1) + offset = YX(source_center.y - radius, source_center.x - radius) + center = YX(radius, radius) + return size, offset, center def move_UP(self, start_pos): return YX(start_pos.y - 1, start_pos.x) @@ -88,6 +95,14 @@ class MapGeometryHex(MapGeometryWithLeftRightMoves): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fov_map_class = FovMapHex + self.dijkstra_map_class = DijkstraMapHex + + def define_segment(self, source_center, radius): + indent = 1 if (source_center.y % 2) else 0 + size = YX(2 * radius + 1 + indent, 2 * radius + 1) + offset = YX(source_center.y - radius - indent, source_center.x - radius) + center = YX(radius + indent, radius) + return size, offset, center def move_UPLEFT(self, start_pos): start_indented = start_pos.y % 2 @@ -172,30 +187,91 @@ class Map(): -class FovMap(Map): +class SourcedMap(Map): + + def __init__(self, source_map, source_center, radius): + self.source_map = source_map + self.radius = radius + self.size, self.offset, self.center = \ + self.geometry_class.define_segment(None, source_center, radius) + self.geometry = self.geometry_class(self.size) + + def source_yx(self, yx, check=False): + source_yx = yx + self.offset + if check and not self.source_map.inside(source_yx): + return False + return source_yx + + def target_yx(self, yx, check=False): + target_yx = yx - self.offset + if check and not self.inside(target_yx): + return False + return target_yx + + + +class DijkstraMap(SourcedMap): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.terrain = [255] * self.size_i + self[self.center] = 0 + shrunk = True + source_map_segment = '' + for yx in self: + yx_in_source = self.source_yx(yx, True) + if yx_in_source: + source_map_segment += self.source_map[yx_in_source] + else: + source_map_segment += 'X' + while shrunk: + shrunk = False + for i in range(self.size_i): + if source_map_segment[i] == 'X': + continue + neighbors = self.geometry.get_neighbors_i(i) + for direction in [d for d in neighbors if neighbors[d]]: + j = neighbors[direction] + if self.terrain[j] < self.terrain[i] - 1: + self.terrain[i] = self.terrain[j] + 1 + shrunk = True + #print('DEBUG Dijkstra') + #line_to_print = [] + #x = 0 + #for n in self.terrain: + # line_to_print += ['%3s' % n] + # x += 1 + # if x >= self.size.x: + # x = 0 + # print(' '.join(line_to_print)) + # line_to_print = [] + + + +class DijkstraMapHex(DijkstraMap): + geometry_class = MapGeometryHex + + + +class DijkstraMapSquare(DijkstraMap): + geometry_class = MapGeometrySquare + + + +class FovMap(SourcedMap): # TODO: player visibility asymmetrical (A can see B when B can't see A): # does this make sense, or not? - def __init__(self, source_map, source_center): - self.source_map = source_map - self.fov_radius = 12 - self.set_size_offset_center(source_center) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.terrain = '?' * self.size.y * self.size.x self[self.center] = '.' - self.geometry = self.geometry_class(self.size) - self[self.center] = '.' self.shadow_cones = [] self.circle_out(self.center, self.shadow_process) def throws_shadow(self, source_yx): return self.source_map[source_yx] == 'X' - def source_yx(self, yx): - source_yx = yx + self.offset - if not self.source_map.inside(source_yx): - return False - return source_yx - def shadow_process(self, yx, source_yx, distance_to_center, dir_i, dir_progress): # Possible optimization: If no shadow_cones yet and self[yx] == '.', # skip all. @@ -271,13 +347,13 @@ class FovMap(Map): circle_in_map = True distance = 1 yx = YX(yx.y, yx.x) - while distance <= self.fov_radius: + while distance <= self.radius: yx = self.basic_circle_out_move(yx, 'RIGHT') for dir_i in range(len(self.circle_out_directions)): for dir_progress in range(distance): direction = self.circle_out_directions[dir_i] yx = self.circle_out_move(yx, direction) - source_yx = self.source_yx(yx) + source_yx = self.source_yx(yx, True) if source_yx: f(yx, source_yx, distance, dir_i, dir_progress) distance += 1 @@ -289,14 +365,6 @@ class FovMapHex(FovMap): 'UPRIGHT', 'RIGHT', 'DOWNRIGHT') geometry_class = MapGeometryHex - def set_size_offset_center(self, source_center): - indent = 1 if (source_center.y % 2) else 0 - self.size = YX(2 * self.fov_radius + 1 + indent, - 2 * self.fov_radius + 1) - self.offset = YX(source_center.y - self.fov_radius - indent, - source_center.x - self.fov_radius) - self.center = YX(self.fov_radius + indent, self.fov_radius) - def circle_out_move(self, yx, direction): return self.basic_circle_out_move(yx, direction) @@ -307,12 +375,6 @@ class FovMapSquare(FovMap): ('UP', 'RIGHT'), ('RIGHT', 'DOWN')) geometry_class = MapGeometrySquare - def set_size_offset_center(self, source_center): - self.size = YX(2 * self.fov_radius + 1, 2 * self.fov_radius + 1) - self.offset = YX(source_center.y - self.fov_radius, - source_center.x - self.fov_radius) - self.center = YX(self.fov_radius, self.fov_radius) - def circle_out_move(self, yx, direction): yx = self.basic_circle_out_move(yx, direction[0]) return self.basic_circle_out_move(yx, direction[1]) diff --git a/plomrogue/things.py b/plomrogue/things.py index da5cf77..dfb536c 100644 --- a/plomrogue/things.py +++ b/plomrogue/things.py @@ -94,11 +94,11 @@ class ThingAnimate(Thing): if self._fov: return self._fov fov_map_class = self.game.map_geometry.fov_map_class - self._fov = fov_map_class(self.game.map, self.position) + self._fov = fov_map_class(self.game.map, self.position, 12) return self._fov def fov_test(self, yx): - test_position = yx - self.fov_stencil.offset + test_position = self.fov_stencil.target_yx(yx) if self.fov_stencil.inside(test_position): if self.fov_stencil[test_position] == '.': return True @@ -108,8 +108,7 @@ class ThingAnimate(Thing): visible_terrain = '' for yx in self.fov_stencil: if self.fov_stencil[yx] == '.': - corrected_yx = yx + self.fov_stencil.offset - visible_terrain += map[corrected_yx] + visible_terrain += map[self.fov_stencil.source_yx(yx)] else: visible_terrain += ' ' return visible_terrain