Skip to content

Commit

Permalink
tiles and decals
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian321 committed Dec 6, 2024
0 parents commit 1d1ad7b
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/autopep8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: autopep8

on: [push]

jobs:
format:
name: Format (Python ${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- run: |
python -m pip install --upgrade pip
pip install autopep8
- run: |
autopep8 --diff --exit-code $(git ls-files '*.py')
24 changes: 24 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: pylint

on: [push]

jobs:
lint:
name: Lint (Python ${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- run: |
pylint $(git ls-files '*.py')
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: test

on: [push]

jobs:
lint:
name: Test (Python ${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- run: |
TMP=$(mktemp -d)
git clone --recurse-submodules https://github.com/space-wizards/space-station-14 $TMP
python -m ss14_tiled $TMP
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
.venv/
.vscode/
*.egg-info/
dist/
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright © 2024 Ignaz Kraft

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Space Station 14 - Tiled

Tooling to use [Tiled](https://www.mapeditor.org/) as map editor for [SS14](https://github.com/space-wizards/space-station-14).

```sh
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

## TODO

- [ ] Import
- [x] Decals
- [ ] Entities
- [x] Tiles
- [ ] Export
- ???
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[project]
name = "ss14_tiled"
license = {file = "LICENSE"}
readme = {file = "README.md", content-type = "text/markdown"}
dynamic = ["dependencies", "version"]

[project.scripts]
ss14-tiled = "ss14_tiled:main"

[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }

[tool.pylint."DESIGN"]
max-locals = 25

[tool.pylint."MESSAGES CONTROL"]
enable = "all"

[tool.pylint."TYPECHECK"]
generated-members = "cv2"
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
autopep8
opencv-python
pylint
pyyaml
3 changes: 3 additions & 0 deletions ss14_tiled/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Init file to expose the main command."""
from . import __main__
main = __main__.main
188 changes: 188 additions & 0 deletions ss14_tiled/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
"""Main module. To be split up on a later day."""
import sys
import xml.etree.ElementTree as ET
from pathlib import Path

import cv2
import yaml


def create_tiles(root: Path, out: Path):
"""Create the "tile"-tiles. As in the floor."""
tiles_out = out / ".images" / "tiles"
tiles_out.mkdir(parents=True, exist_ok=True)

resources_dir = root / "Resources"
yml_dir = resources_dir / "Prototypes/Tiles"
files = [x for x in yml_dir.glob("**/*") if x.is_file()]

root_element = ET.Element("tileset", name="Tiles")
tile_id = 0
for file in files:
for tile in yaml.safe_load(file.read_text("UTF-8")):
if tile["type"] != "tile":
continue # alias
if not "sprite" in tile:
continue # space
if not "variants" in tile:
tile["variants"] = 1

sprite = resources_dir / tile["sprite"].strip("/")
dest: Path = tiles_out / (tile["id"] + sprite.suffix)
img = cv2.imread(sprite, cv2.IMREAD_UNCHANGED)
height, width = img.shape[:2]
width //= tile["variants"] # only take the first variant
cv2.imwrite(dest, img[0:height, 0:width])

tile_id += 1
ET.SubElement(
ET.SubElement(root_element, "tile", id=str(tile_id)),
"image", source=f"./.images/tiles/{dest.name}",
width=str(width), height=str(height))

ET.ElementTree(root_element).write(out / "tiles.tsx",
encoding="UTF-8", xml_declaration=True)


def create_decals(root: Path, out: Path, name: str = "", color: str = "#FFF"):
"""Create the "decals"-tiles."""
decals = ".images/decals"
if name:
decals = f".images/decals_{name}"
decals_out = out / decals
decals_out.mkdir(parents=True, exist_ok=True)

resources_dir = root / "Resources"
yml_dir = resources_dir / "Prototypes/Decals"
files = [x for x in yml_dir.glob("**/*") if x.is_file()]

root_element = ET.Element("tileset", name="Decals")
if name:
root_element = ET.Element("tileset", name=f"Decals - {name}")

properties = ET.SubElement(root_element, "properties")
ET.SubElement(properties, "property", name="color_name", value=name)
ET.SubElement(properties, "property", name="color_value", value=color)

tile_id = 0
for file in files:
data = yaml.safe_load(file.read_text("UTF-8"))
for decal in data:
if decal["type"] != "decal":
continue # alias?
sprite: Path = resources_dir / "Textures" / \
decal["sprite"]["sprite"].strip("/Textures") / \
(str(decal["sprite"]["state"]) + ".png")
dest: Path = decals_out / (str(decal["id"]) + sprite.suffix)
img = cv2.imread(sprite, cv2.IMREAD_UNCHANGED)
height, width, dim = img.shape
if dim == 3:
img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
dim = 4

img = decal_colors(img, color)
cv2.imwrite(dest, img)

tile_id += 1
ET.SubElement(
ET.SubElement(root_element, "tile", id=str(tile_id)),
"image", source=f"./{decals}/{dest.name}",
width=str(width), height=str(height))

file_name = "decals.tsx"
if name:
file_name = f"decals_{name}.tsx"
ET.ElementTree(root_element).write(out / file_name,
encoding="UTF-8", xml_declaration=True)


def parse_hex(color: str):
"""Parse a hex string to RGBA uint8."""
if len(color) == 4:
r = int(color[1], 16)
r = r + (r << 4)
g = int(color[2], 16)
g = g + (g << 4)
b = int(color[3], 16)
b = b + (b << 4)
return (r, g, b, 255)
if len(color) == 5:
(r, g, b, _) = parse_hex(color[:-1])
a = int(color[4], 16)
a = a + (a << 4)
return (r, g, b, a)
if len(color) == 7:
r = int(color[1:3], 16)
g = int(color[3:5], 16)
b = int(color[5:7], 16)
return (r, g, b, 255)
if len(color) == 9:
(r, g, b, _) = parse_hex(color[:-2])
a = int(color[7:9], 16)
return (r, g, b, a)
raise ValueError("Unknown hex format.")


def decal_colors(img: cv2.Mat, color: str):
"""Scale the colors of an image."""
(red, green, blue, alpha) = parse_hex(color)
b, g, r, a = cv2.split(img)
b = b * (blue / 255)
g = g * (green / 255)
r = r * (red / 255)
a = a * (alpha / 255)
return cv2.merge((b, g, r, a))


def get_colors(root: Path) -> list[(str, str)]:
"""Get all color names and values (for decals).
Returns [("palette_color", "#value")]
"""
resources_dir = root / "Resources"
yml_dir = resources_dir / "Prototypes/Palettes"
glob = yml_dir.glob("**/*")
files = [x for x in glob if x.is_file()]

results = []
for file in files:
data = yaml.safe_load(file.read_text("UTF-8"))
for palette in data:
if palette["type"] != "palette":
continue # alias?
for color in palette["colors"]:
results.append((
palette["name"] + "_" + color,
palette["colors"][color]
))

return results


def setup(root: Path):
"""Create tile-sets for Tiled."""
out = Path("dist")
out.mkdir(exist_ok=True)

create_tiles(root, out)
create_decals(root, out)
for (name, color) in get_colors(root):
create_decals(root, out, name, color)


def eprint(*args, **kwargs):
"""Print to std-error."""
print(*args, file=sys.stderr, **kwargs)


def main():
"""Main entrypoint."""
if len(sys.argv) != 2:
eprint("Usage: python3 -m ss14_tiled </path/to/ss14.git/>")
sys.exit(1)

setup(Path(sys.argv[1]).expanduser())


if __name__ == "__main__":
main()

0 comments on commit 1d1ad7b

Please sign in to comment.