From d544660c6e954d3391d83717a66bb3caa17dcf5c Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Fri, 9 Jul 2021 05:23:25 -0400 Subject: [PATCH] Support spawn points and metadata in map generation --- png/map/intro_coll.png | Bin 718 -> 721 bytes scripts/generate_map.py | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/png/map/intro_coll.png b/png/map/intro_coll.png index 5192a4f33dda65721690ed83b1f075d0b806036f..d4e0b03f88544e965949eedf774b5f7ed463fb4a 100644 GIT binary patch literal 721 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5D>38$lZxy-8q?;Ku%78Plzj! zR#Gr=)v!skH0%$vzF(+zb3xj>!?S*WGMo7{AZwq2`IJ2GO#L8l6(bu(#sZr+anf8`S=1_z$!L zjCDTjXklPr683a)45^s&_LgB@vjc;xWBk#Rf8WnI<|&?dO^1KuXD#sue`j$i%Efno zU%`Hb5fA>$Xk)JBevaqBdw!r5K=8n(VeRL0w-_JXb##~K0Sm2TDJTOo7#JE#8P^Fl zR5{4P@mg*-`G_?812UowYq_DSpwxjbhH`!f+XZ~M@q@c*dpj5lj{Q((Le;CybcPGJ zHhe}xuMJj6c(GJ119Jesy%w@c28Inv2QnC5 zF_z-Q=j;l;96h?mmLcu$$9hzo8ble_iNV7O6lQzPW8OMGyUg%rFRDt0JB$y0huSUy zr8^*qkX#@az>bU)zOWvTWeVV50dyi(94+^No&ECV4OeBSSA&f3boFyt=akR{08i86 AGynhq literal 718 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5D>38$lZxy-8q?;Ku%_WPlzj! zR#Gr=)v!skH0%$vzF(+zb3xj>!?S*WGMo7{AZwq2`IJ2GO#L8l6(bu(#sZr+anf8`S=1_z$$> zKf~0d3(FZ8n1nrD978JRyuD?Z*X+RH>KL!O^S@%SYHUm7=HCL=>x&rgtzRkd=_~S3gn?SIJ*}^LSDj&mpzFof(z#;|e4OgL(4BuFHcr(0W zEQRAfJ&D?cnSU9ILmB$?z{(MXHIt1k!`B8YJh*)JS0%OsT_3|)5ZXYjGL{*lc$|&T zOAtAKdjpBT1rOg?Ze~I?8EEthzI~{kc*PiQ`ip^Wfm~fEs^SFqhKmeWm|x+mdKI;Vst04)IX;s5{u diff --git a/scripts/generate_map.py b/scripts/generate_map.py index d4c8e62..ccf6eef 100644 --- a/scripts/generate_map.py +++ b/scripts/generate_map.py @@ -3,12 +3,13 @@ import pathlib import subprocess import sys import tempfile -from typing import NoReturn +from typing import NoReturn, Tuple from PIL import Image GREEN = (0, 255, 0) RED = (255, 0, 0) +BLUE = (0, 0, 255) def abort(msg: str) -> NoReturn: @@ -16,9 +17,12 @@ def abort(msg: str) -> NoReturn: sys.exit(1) +SpawnPoint = Tuple[int, int] + + def generate_coll_map( in_path: pathlib.Path, width: int, height: int, compress: bool = False -) -> str: +) -> Tuple[str, SpawnPoint]: png = Image.open(in_path).convert("RGB") if png.width % 8 != 0 or png.height % 8 != 0: abort(f"file '{in_path}' has invalid dimensions (should be multiple of 8)") @@ -28,16 +32,20 @@ def generate_coll_map( out_bytes = [] bits = [] + spawn = None for y in range(png.height // 8): for x in range(png.width // 8): pixel = png.getpixel((x * 8, y * 8)) - bit = None + if pixel == RED: bit = 0 elif pixel == GREEN: bit = 1 + elif pixel == BLUE: + bit = 1 + spawn = (x, y) else: abort(f"unsupported pixel in collision map: {pixel}") @@ -51,7 +59,11 @@ def generate_coll_map( out_bytes.append(bit) png.close() - return format_bytes(out_bytes, width=width) + + if spawn is None: + abort(f"no spawn point located") + + return format_bytes(out_bytes, width=width), spawn def format_bytes(data: bytes, width: int = 16) -> str: @@ -74,6 +86,11 @@ def generate_map(pngfile: str, compress: bool = False) -> None: width = png.width // 8 height = png.height // 8 + if width > 127 or height > 127: + abort( + f"file '{pngfile}' has invalid dimensions (width/height greater than 127 tiles)" + ) + png.close() with tempfile.NamedTemporaryFile() as tilef, tempfile.NamedTemporaryFile() as mapf: @@ -95,7 +112,7 @@ def generate_map(pngfile: str, compress: bool = False) -> None: section = pngpath.name.replace(".png", "") collpath = pngpath.parent / pngpath.name.replace(".png", "_coll.png") - coll_map = generate_coll_map(collpath, width, height, compress=compress) + coll_map, spawn = generate_coll_map(collpath, width, height, compress=compress) with open(incpath, "w") as outf: outf.write( @@ -111,7 +128,16 @@ ASSERT {section}_MAP_SIZE == {section}_WIDTH * {section}_HEIGHT with open(spath, "w") as outf: outf.write( - f"""SECTION "MAP - {section}", ROMX + f"""SECTION "MAP - {section}", ROM0 + +{section}_Data:: +{section}_TILE_PTR: DW {section}_TILES +{section}_TILE_SIZE: DB ({section}_TILES_end - {section}_TILES) +{section}_MAP_PTR: DW {section}_MAP +{section}_MAP_WIDTH: DB {width} +{section}_MAP_HEIGHT: DB {height} +{section}_SPAWN_X: DB {spawn[0]} +{section}_SPAWN_Y: DB {spawn[1]} {section}_MAP:: {map_data}