diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2020-10-20 16:10:14 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2021-01-02 22:23:40 +0100 |
| commit | d3a6d4098a0c1b4fe598d0ab610fd2d91d28d715 (patch) | |
| tree | 2a1a4bf5e3961b8bb0c99b89fc3102c03fb94546 | |
| parent | 2e06b4e63daa323fe475640853af62343178a3d6 (diff) | |
| download | muhqs-game-d3a6d4098a0c1b4fe598d0ab610fd2d91d28d715.tar.gz muhqs-game-d3a6d4098a0c1b4fe598d0ab610fd2d91d28d715.zip | |
add script to generate images from map files and tile assets
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | maps/2P-river-king.map | 2 | ||||
| -rw-r--r-- | maps/3P-ring-street.map | 1 | ||||
| -rw-r--r-- | maps/tiles/deep_water.png | bin | 0 -> 17321 bytes | |||
| -rw-r--r-- | maps/tiles/docks.png | bin | 0 -> 13164 bytes | |||
| -rw-r--r-- | maps/tiles/farm.png | bin | 0 -> 11162 bytes | |||
| -rw-r--r-- | maps/tiles/house.png | bin | 0 -> 12776 bytes | |||
| -rw-r--r-- | maps/tiles/neutral.png | bin | 0 -> 6194 bytes | |||
| -rw-r--r-- | maps/tiles/not_part_of_the_map.png | bin | 0 -> 5479 bytes | |||
| -rw-r--r-- | maps/tiles/spawn_player_1.png | bin | 0 -> 8062 bytes | |||
| -rw-r--r-- | maps/tiles/spawn_player_2.png | bin | 0 -> 10427 bytes | |||
| -rw-r--r-- | maps/tiles/spawn_player_3.png | bin | 0 -> 10840 bytes | |||
| -rw-r--r-- | maps/tiles/stick_of_victory.png | bin | 0 -> 10884 bytes | |||
| -rw-r--r-- | maps/tiles/street.png | bin | 0 -> 8940 bytes | |||
| -rw-r--r-- | maps/tiles/street_2.png | bin | 0 -> 9363 bytes | |||
| -rw-r--r-- | maps/tiles/street_3.png | bin | 0 -> 10492 bytes | |||
| -rw-r--r-- | maps/tiles/street_4.png | bin | 0 -> 10891 bytes | |||
| -rw-r--r-- | maps/tiles/tower.png | bin | 0 -> 11874 bytes | |||
| -rwxr-xr-x | scripts/generate_map_img.py | 143 |
19 files changed, 146 insertions, 1 deletions
@@ -1,2 +1,3 @@ latex/build rules/*.html +maps/*.png diff --git a/maps/2P-river-king.map b/maps/2P-river-king.map index 8b8d1c00..0fa00e15 100644 --- a/maps/2P-river-king.map +++ b/maps/2P-river-king.map @@ -6,7 +6,7 @@ WWWWWSWWWWW WWWWWSWWWWW HWWTS TWW 2HSSSSSFDW -T F HH H +T F HH H T := tower H := house diff --git a/maps/3P-ring-street.map b/maps/3P-ring-street.map index 43312d43..32bdd02e 100644 --- a/maps/3P-ring-street.map +++ b/maps/3P-ring-street.map @@ -19,3 +19,4 @@ S := street 3 := spawn player 3 * := stick of victory # := not part of the map + diff --git a/maps/tiles/deep_water.png b/maps/tiles/deep_water.png Binary files differnew file mode 100644 index 00000000..efe80ad7 --- /dev/null +++ b/maps/tiles/deep_water.png diff --git a/maps/tiles/docks.png b/maps/tiles/docks.png Binary files differnew file mode 100644 index 00000000..a1a97179 --- /dev/null +++ b/maps/tiles/docks.png diff --git a/maps/tiles/farm.png b/maps/tiles/farm.png Binary files differnew file mode 100644 index 00000000..b083ee29 --- /dev/null +++ b/maps/tiles/farm.png diff --git a/maps/tiles/house.png b/maps/tiles/house.png Binary files differnew file mode 100644 index 00000000..9e86e72f --- /dev/null +++ b/maps/tiles/house.png diff --git a/maps/tiles/neutral.png b/maps/tiles/neutral.png Binary files differnew file mode 100644 index 00000000..c7ecf31e --- /dev/null +++ b/maps/tiles/neutral.png diff --git a/maps/tiles/not_part_of_the_map.png b/maps/tiles/not_part_of_the_map.png Binary files differnew file mode 100644 index 00000000..cf739f9e --- /dev/null +++ b/maps/tiles/not_part_of_the_map.png diff --git a/maps/tiles/spawn_player_1.png b/maps/tiles/spawn_player_1.png Binary files differnew file mode 100644 index 00000000..57d63821 --- /dev/null +++ b/maps/tiles/spawn_player_1.png diff --git a/maps/tiles/spawn_player_2.png b/maps/tiles/spawn_player_2.png Binary files differnew file mode 100644 index 00000000..fd29adf0 --- /dev/null +++ b/maps/tiles/spawn_player_2.png diff --git a/maps/tiles/spawn_player_3.png b/maps/tiles/spawn_player_3.png Binary files differnew file mode 100644 index 00000000..311a4e8e --- /dev/null +++ b/maps/tiles/spawn_player_3.png diff --git a/maps/tiles/stick_of_victory.png b/maps/tiles/stick_of_victory.png Binary files differnew file mode 100644 index 00000000..e2c55455 --- /dev/null +++ b/maps/tiles/stick_of_victory.png diff --git a/maps/tiles/street.png b/maps/tiles/street.png Binary files differnew file mode 100644 index 00000000..6c0b5a76 --- /dev/null +++ b/maps/tiles/street.png diff --git a/maps/tiles/street_2.png b/maps/tiles/street_2.png Binary files differnew file mode 100644 index 00000000..c34d4f6f --- /dev/null +++ b/maps/tiles/street_2.png diff --git a/maps/tiles/street_3.png b/maps/tiles/street_3.png Binary files differnew file mode 100644 index 00000000..5194e842 --- /dev/null +++ b/maps/tiles/street_3.png diff --git a/maps/tiles/street_4.png b/maps/tiles/street_4.png Binary files differnew file mode 100644 index 00000000..1c70dd7a --- /dev/null +++ b/maps/tiles/street_4.png diff --git a/maps/tiles/tower.png b/maps/tiles/tower.png Binary files differnew file mode 100644 index 00000000..f69b2cb7 --- /dev/null +++ b/maps/tiles/tower.png diff --git a/scripts/generate_map_img.py b/scripts/generate_map_img.py new file mode 100755 index 00000000..608c1fe6 --- /dev/null +++ b/scripts/generate_map_img.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +import argparse +import cv2 +import pathlib +import numpy +import os +from typing import MutableMapping + +MAP_ROOT = pathlib.Path(os.getcwd()) +TILES_DIR = MAP_ROOT / 'tiles' + +def get_tile_path(tile: str) -> pathlib.Path: + """Return the corresponding image file name for a tile""" + return TILES_DIR / f'{tile.lower().replace(" ", "_")}.png' + +def read_map(map_path: pathlib.Path) -> MutableMapping: + """Read and parse a map file""" + map_definition = {'symbols': {}, 'rules': {}} + with open(map_path, 'r') as map_file: + lines = [l[:-1] for l in map_file.readlines()] # remove new lines + + # sections in map files are seperated by an empty line + section_end = lines.index('') + map_string = '\n'.join(lines[:section_end]) + lines = lines [section_end+1:] # consume map string + + section_end = lines.index('') + for symbol_def in lines[:section_end]: + symbol, tile = symbol_def.split(':=') + symbol = symbol.strip() + tile = tile.strip() + map_definition['symbols'][symbol] = tile + lines = lines[section_end + 1:] # consume symbol definitions + + # build up tiles + tiles = [] + for line in map_string.splitlines(): + row = [] + for symbol in line: + tile = "neutral" + if symbol != ' ': + if symbol not in map_definition['symbols']: + print(f'WARNING unknown symbol "{symbol}"') + else: + tile = map_definition['symbols'][symbol] + row.append(tile) + tiles.append(row) + + map_definition['tiles'] = tiles + + for rule in lines: + name, rule = symbol_def.split(':=') + name = name.strip() + rule = rule.strip() + map_definition['rules'][name] = rule + + return map_definition + + +def generate_img(map_path: pathlib.Path): + """Generate a image from a map file""" + map_definition = read_map(map_path) + + tiles = map_definition['tiles'] + + rows_imgs = [] + for i, row in enumerate(tiles): + tile_imgs = [] + for j, tile in enumerate(row): + # find the right street and rotation + if tile == 'street': + # check continuations + left = j > 1 and row[j-1] == 'street' + right = j < len(row) - 1 and row[j+1] == 'street' + above = i > 1 and tiles[i-1][j] == 'street' + below = i < len(tiles) - 1 and tiles[i+1][j] == 'street' + + connections = len([b for b in [left, right, above, below] if b == True]) + # straight lines + if connections == 1 or (connections == 2 and ((left and right) or (above and below))): + tile_img = cv2.imread(str(get_tile_path('street'))) + if above or below: + tile_img = numpy.rot90(tile_img) + # elbow + elif connections == 2: + # normal orientation above and right + tile_img = cv2.imread(str(get_tile_path('street_2'))) + if right and below: + tile_img = numpy.rot90(tile_img, 3) + elif left and below: + tile_img = numpy.rot90(tile_img, 2) + elif above and left: + tile_img = numpy.rot90(tile_img) + elif connections == 3: + # normal orientation left above right + tile_img = cv2.imread(str(get_tile_path('street_3'))) + if above and right and below: + tile_img = numpy.rot90(tile_img, 3) + elif left and below and right: + tile_img = numpy.rot90(tile_img, 2) + elif below and left and above: + tile_img = numpy.rot90(tile_img) + elif connections == 4: + tile_img = cv2.imread(str(get_tile_path('street_4'))) + else: + tile_img = cv2.imread(str(get_tile_path(tile))) + + tile_imgs.append(tile_img) + + row_img = cv2.hconcat(tile_imgs) + rows_imgs.append(row_img) + + map_img = cv2.vconcat(rows_imgs) + cv2.imshow(f'{map_path.stem}.png', map_img) + cv2.waitKey(0) + cv2.imwrite(f'{map_path.stem}.png', map_img) + +def main(): + """Generate map images from map files""" + + parser = argparse.ArgumentParser(description='generate latex standalone cards') + parser.add_argument('map_input', help='map file or directory containing map files') + parser.add_argument('--map-root', help='path to the map root directory') + + args = parser.parse_args() + + if args.map_root: + global MAP_ROOT + MAP_ROOT = pathlib.Path(args.latex_root) + global TILES_DIR + TILES_DIR = MAP_ROOT / 'tiles' + + + map_path = pathlib.Path(args.map_input) + map_files = [map_path] + if map_path.is_dir(): + map_files = map_path.glob('*.map') + + for m in map_files: + generate_img(m) + +if __name__ == '__main__': + main() |
