aboutsummaryrefslogtreecommitdiff
path: root/scripts/generate_map_img.py
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-09-04 16:11:05 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2025-09-04 18:10:36 +0200
commiteb98e84558f1211eb28f7839fc178d9974187e37 (patch)
treeac6259a5261295d1512f063b5bde6485b7c87bfd /scripts/generate_map_img.py
parent07758e639074a89b580f67cc807543a209672898 (diff)
downloadmuhqs-game-eb98e84558f1211eb28f7839fc178d9974187e37.tar.gz
muhqs-game-eb98e84558f1211eb28f7839fc178d9974187e37.zip
improve python scripts and pipenv environment
Diffstat (limited to 'scripts/generate_map_img.py')
-rwxr-xr-xscripts/generate_map_img.py62
1 files changed, 46 insertions, 16 deletions
diff --git a/scripts/generate_map_img.py b/scripts/generate_map_img.py
index 39c41a45..6923e2e5 100755
--- a/scripts/generate_map_img.py
+++ b/scripts/generate_map_img.py
@@ -6,7 +6,8 @@ import os
from typing import Sequence, Tuple
import yaml
-import cv2 # type: ignore
+import cv2
+import cv2.typing
import numpy
MAP_ROOT = pathlib.Path(os.getcwd())
@@ -49,7 +50,7 @@ def is_connected_to(tiles: Tiles,
x: int,
y: int,
kind=None) -> Tuple[bool, bool, bool, bool]:
- """Return a Quadruple of bools if the naeighbours of (x,y) are one of kind"""
+ """Return a Quadruple of bools if the neighbors of (x,y) are one of kind"""
row = tiles[y]
left = x > 0 and is_kind(row[x - 1], kind)
right = x < len(row) - 1 and is_kind(row[x + 1], kind)
@@ -65,7 +66,8 @@ def count_connections(left: bool, right: bool, above: bool,
return len([b for b in [left, right, above, below] if b is True])
-def find_street_tile(tiles: Tiles, x: int, y: int):
+def find_street_tile(tiles: Tiles, x: int, y: int) -> cv2.typing.MatLike:
+ """Return the appropriate street tile image"""
# find street continuations
left, right, above, below = is_connected_to(tiles, x, y, kind='street')
@@ -87,12 +89,14 @@ def find_street_tile(tiles: Tiles, x: int, y: int):
elif connections == 1 or (connections == 2 and ((left and right) or
(above and below))):
tile_img = cv2.imread(str(get_tile_path('street')))
+ assert tile_img is not None
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')))
+ assert tile_img is not None
if right and below:
tile_img = numpy.rot90(tile_img, 3)
elif left and below:
@@ -102,6 +106,7 @@ def find_street_tile(tiles: Tiles, x: int, y: int):
elif connections == 3:
# normal orientation left above right
tile_img = cv2.imread(str(get_tile_path('street_3')))
+ assert tile_img is not None
if above and right and below:
tile_img = numpy.rot90(tile_img, 3)
elif left and below and right:
@@ -111,10 +116,13 @@ def find_street_tile(tiles: Tiles, x: int, y: int):
elif connections == 4:
tile_img = cv2.imread(str(get_tile_path('street_4')))
+ assert tile_img is not None
return tile_img
-def find_wall_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
+def find_wall_tile(tiles: Sequence[Sequence[str]], x: int,
+ y: int) -> cv2.typing.MatLike:
+ """Return the appropriate wall tile image"""
# find fortification continuations
left, right, above, below = is_connected_to(tiles,
x,
@@ -150,17 +158,21 @@ def find_wall_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
else:
tile_img = cv2.imread(str(get_tile_path('wall_elbow_down')))
+ assert tile_img is not None
if left:
tile_img = cv2.flip(tile_img, 1)
- elif connections == 3 or connections == 4:
+ elif connections in (3, 4):
# We don't have wall tiles for those structures yet use default vertical wall
tile_img = cv2.imread(str(get_tile_path('wall')))
+ assert tile_img is not None
return tile_img
-def find_gate_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
+def find_gate_tile(tiles: Sequence[Sequence[str]], x: int,
+ y: int) -> cv2.typing.MatLike:
+ """Return the appropriate gate tile image"""
# find relevant structure tiles
left, right, above, below = is_connected_to(tiles,
x,
@@ -170,19 +182,30 @@ def find_gate_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
connections = count_connections(left, right, above, below)
if connections == 0:
- return cv2.imread(str(get_tile_path('gate')))
+ tile_img = cv2.imread(str(get_tile_path('gate')))
+
+ # Prefer double connected gates over single connected ones.
+ # Prefer horizontal gates over vertical ones.
+ elif left and right:
+ tile_img = cv2.imread(str(get_tile_path('gate_lr')))
+
+ elif above and below:
+ tile_img = cv2.imread(str(get_tile_path('gate_ud')))
# straight gates are the only special gates we have
- if above or below:
- return cv2.imread(str(get_tile_path('gate_ud')))
+ elif left or right:
+ tile_img = cv2.imread(str(get_tile_path('gate_lr')))
- if left or right:
- return cv2.imread(str(get_tile_path('gate_lr')))
+ elif above or below:
+ tile_img = cv2.imread(str(get_tile_path('gate_ud')))
- return cv2.imread(str(get_tile_path('gate')))
+ assert tile_img is not None
+ return tile_img
-def find_tower_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
+def find_tower_tile(tiles: Sequence[Sequence[str]], x: int,
+ y: int) -> cv2.typing.MatLike:
+ """Return the appropriate tower tile image"""
# find wall continuations
left, right, above, below = is_connected_to(tiles,
x,
@@ -193,7 +216,9 @@ def find_tower_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
# This tower is not connected to a relevant structure
if connections == 0:
- return cv2.imread(str(get_tile_path('tower')))
+ img = cv2.imread(str(get_tile_path('tower')))
+ assert img is not None
+ return img
# generate connection selector
selector = ''
@@ -216,7 +241,10 @@ def find_tower_tile(tiles: Sequence[Sequence[str]], x: int, y: int):
tile_name = 'tower'
if selector:
tile_name = f'{tile_name}_{selector}'
- return cv2.imread(str(get_tile_path(tile_name)))
+
+ img = cv2.imread(str(get_tile_path(tile_name)))
+ assert img is not None
+ return img
TILE_SELECTORS = {
@@ -259,7 +287,9 @@ def generate_img(map_path: pathlib.Path):
if tile in TILE_SELECTORS:
tile_img = TILE_SELECTORS[tile](tiles, x, y)
else:
- tile_img = cv2.imread(str(get_tile_path(tile)))
+ img = cv2.imread(str(get_tile_path(tile)))
+ assert img is not None
+ tile_img = img
tile_imgs.append(tile_img)