aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2021-02-28 17:47:41 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2021-02-28 17:47:41 +0100
commit6a7dbc5473b36c61ebbfca1eb86e8061083a91ab (patch)
tree7c120e135f84be6cfcde706b603c403e342dac7a /scripts
parent0692ccd33afc4fdd80550e9979b824c1c25e973b (diff)
downloadmuhqs-game-6a7dbc5473b36c61ebbfca1eb86e8061083a91ab.tar.gz
muhqs-game-6a7dbc5473b36c61ebbfca1eb86e8061083a91ab.zip
convert map definition to YAML
We don't use our own map definition syntax which was not documented and fragile. YAML is more common and is easier to parse.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/generate_map_img.py47
1 files changed, 11 insertions, 36 deletions
diff --git a/scripts/generate_map_img.py b/scripts/generate_map_img.py
index f4e29148..84a72abb 100755
--- a/scripts/generate_map_img.py
+++ b/scripts/generate_map_img.py
@@ -5,6 +5,7 @@ import pathlib
import numpy
import os
from typing import MutableMapping
+import yaml
MAP_ROOT = pathlib.Path(os.getcwd())
TILES_DIR = MAP_ROOT / '../assets/tiles'
@@ -14,24 +15,15 @@ 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': {}}
+def generate_img(map_path: pathlib.Path):
+ """Generate a image from a map file"""
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
+ map_definition = yaml.full_load(map_file)
- 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
+ # remove the trailing new line
+ map_string = map_definition['map'][:-1]
+ # ensure that all keys are actually strings
+ symbols = {str(k): v for k, v in map_definition['symbols'].items()}
# build up tiles
tiles = []
@@ -40,30 +32,13 @@ def read_map(map_path: pathlib.Path) -> MutableMapping:
for symbol in line:
tile = "neutral"
if symbol != ' ':
- if symbol not in map_definition['symbols']:
+ if symbol not in symbols:
print(f'WARNING unknown symbol "{symbol}"')
else:
- tile = map_definition['symbols'][symbol]
+ tile = 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 = []
@@ -140,7 +115,7 @@ def main():
map_path = pathlib.Path(args.map_input)
map_files = [map_path]
if map_path.is_dir():
- map_files = map_path.glob('*.map')
+ map_files = map_path.glob('*.yml')
for m in map_files:
generate_img(m)