diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2021-12-12 15:10:56 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2021-12-12 15:10:56 +0100 |
| commit | 322225830ff9e497d442c3c6c5f26786fc241c54 (patch) | |
| tree | 3375d3cdf5c1afc6cb8678742e7b9a67cea06388 /scripts | |
| parent | 8939a55b4e632f021a423b4508810b2720a7f3b3 (diff) | |
| download | muhqs-game-322225830ff9e497d442c3c6c5f26786fc241c54.tar.gz muhqs-game-322225830ff9e497d442c3c6c5f26786fc241c54.zip | |
improve fortification tiles
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/generate_map_img.py | 109 |
1 files changed, 79 insertions, 30 deletions
diff --git a/scripts/generate_map_img.py b/scripts/generate_map_img.py index 8dff559e..a2bdf434 100755 --- a/scripts/generate_map_img.py +++ b/scripts/generate_map_img.py @@ -22,22 +22,44 @@ def get_tile_path(tile: str) -> pathlib.Path: return TILES_DIR / f'{tile.lower().replace(" ", "_")}.png' +def is_right_edge(tiles: Tiles, x: int, y: int) -> bool: + """Return true if (x,y) is on the right edge of the map""" + return len(tiles[y]) - 1 == x + + +def is_left_edge(tiles: Tiles, x: int) -> bool: + """Return true if (x,y) is on the left edge of the map""" + return x == 0 + + +def is_kind(tile: str, kind) -> bool: + """Return if the tile is one of kind""" + if kind is None: + return tile != 'neutral' + + if not hasattr(kind, '__iter__'): + kind = [kind] + + return tile in kind + + def is_connected_to(tiles: Tiles, x: int, y: int, kind=None) -> Tuple[bool, bool, bool, bool]: - is_kind = lambda tile: tile == kind if kind else tile != 'neutral' + """Return a Quadruple of bools if the naeighbours of (x,y) are one of kind""" row = tiles[y] - left = x > 0 and is_kind(row[x - 1]) - right = x < len(row) - 1 and is_kind(row[x + 1]) - above = y > 0 and is_kind(tiles[y - 1][x]) - below = y < len(tiles) - 1 and is_kind(tiles[y + 1][x]) + left = x > 0 and is_kind(row[x - 1], kind) + right = x < len(row) - 1 and is_kind(row[x + 1], kind) + above = y > 0 and is_kind(tiles[y - 1][x], kind) + below = y < len(tiles) - 1 and is_kind(tiles[y + 1][x], kind) return left, right, above, below def count_connections(left: bool, right: bool, above: bool, below: bool) -> int: + """Count the true values in the four connections""" return len([b for b in [left, right, above, below] if b is True]) @@ -91,13 +113,16 @@ def find_street_tile(tiles: Tiles, x: int, y: int): def find_wall_tile(tiles: Sequence[Sequence[str]], x: int, y: int): - # find wall continuations - left, right, above, below = is_connected_to(tiles, x, y, kind='wall') + # find fortification continuations + left, right, above, below = is_connected_to(tiles, + x, + y, + kind=['wall', 'tower', 'gate']) connections = count_connections(left, right, above, below) if connections == 0: - # This wall is not connected to another wall -> + # This wall is not connected to another fortification -> # check any other non neutral tiles left, right, above, below = is_connected_to(tiles, x, y, kind=None) @@ -134,32 +159,60 @@ def find_wall_tile(tiles: Sequence[Sequence[str]], x: int, y: int): def find_gate_tile(tiles: Sequence[Sequence[str]], x: int, y: int): + # find relevant structure tiles + left, right, above, below = is_connected_to(tiles, + x, + y, + kind=['wall', 'tower']) + + connections = count_connections(left, right, above, below) + + if connections == 0: + return cv2.imread(str(get_tile_path('gate'))) + + # straight gates are the only special gates we have + if above or below: + return cv2.imread(str(get_tile_path('gate'))) + + if left or right: + return cv2.imread(str(get_tile_path('gate_wall_h'))) + + return cv2.imread(str(get_tile_path('gate'))) + + +def find_tower_tile(tiles: Sequence[Sequence[str]], x: int, y: int): # find wall continuations - left, right, above, below = is_connected_to(tiles, x, y, kind='wall') + left, right, above, below = is_connected_to(tiles, + x, + y, + kind=['wall', 'gate']) connections = count_connections(left, right, above, below) + # This tower is not connected to a relevant structure if connections == 0: - # This gate is not connected to a wall -> check towers next - left, right, above, below = is_connected_to(tiles, x, y, kind='tower') + return cv2.imread(str(get_tile_path('tower'))) - connections = count_connections(left, right, above, below) + if left and (not right and not is_right_edge(tiles, x, y)): + return cv2.imread(str(get_tile_path('tower_left_wall'))) - if connections == 0: - # If the gate is not connected to any fortification structure use default tile - return cv2.imread(str(get_tile_path('gate'))) + if right and (not left and not is_left_edge(tiles, x)): + return cv2.imread(str(get_tile_path('tower_right_wall'))) - # straight gates are the only special gates we have - if connections == 2 and ((left and right) or (above and below)): - if above or below: - tile_img = cv2.imread(str(get_tile_path('gate'))) - else: - tile_img = cv2.imread(str(get_tile_path('gate_wall_h'))) + # We reach this if either right and left are connected or + # one side is connected and this tile is on the edge + if right or left: + return cv2.imread(str(get_tile_path('tower_in_wall'))) - else: - tile_img = cv2.imread(str(get_tile_path('gate'))) + return cv2.imread(str(get_tile_path('tower'))) - return tile_img + +TILE_SELECTORS = { + 'street': find_street_tile, + 'gate': find_gate_tile, + 'tower': find_tower_tile, + 'wall': find_wall_tile, +} def generate_img(map_path: pathlib.Path): @@ -191,12 +244,8 @@ def generate_img(map_path: pathlib.Path): tile_imgs = [] for x, tile in enumerate(row): # find the right street and rotation - if tile == 'street': - tile_img = find_street_tile(tiles, x, y) - elif tile == 'wall': - tile_img = find_wall_tile(tiles, x, y) - elif tile == 'gate': - tile_img = find_gate_tile(tiles, x, y) + if tile in TILE_SELECTORS: + tile_img = TILE_SELECTORS[tile](tiles, x, y) else: tile_img = cv2.imread(str(get_tile_path(tile))) |
