aboutsummaryrefslogtreecommitdiff
path: root/tools/map.py
blob: c959c5076ce088aa11944a50c01d86775aacdc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3

from argparse import ArgumentParser
import subprocess
import os
import json

__version__ = "1.0"

ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld")
strip = os.environ.get("STRIP", "i586-pc-msdosdjgpp-strip")

entity_types = (
    "Player",
    "Snake",
    "Bat",
    "Old",
    "Bones",
    "Time",
    "Bonus",
    "Pickaxe",
    "GoldKey",
    "SilverKey",
)


def get_layer(data, name):
    for layer in data["layers"]:
        if layer["name"] == name:
            return layer
    raise ValueError("Layer %r not found" % name)


def get_prop(data, name, default):
    if "properties" not in data:
        return default
    for prop in data["properties"]:
        if prop["name"] == name:
            return prop["value"]
    return default


def main():
    parser = ArgumentParser(
        description="Tiled JSON Map to .o",
        epilog="Copyright (C) 2023 Juan J Martinez <jjm@usebox.net>",
    )

    parser.add_argument(
        "--version", action="version", version="%(prog)s " + __version__
    )
    parser.add_argument(
        "--max-ents",
        dest="max_ents",
        type=int,
        default=None,
        help="limit of entities in the map",
    )
    parser.add_argument("file_json", help="JSON map to convert")
    parser.add_argument("output", help="object name for the map data")

    args = parser.parse_args()

    with open(args.file_json, "rt") as fd:
        data = json.load(fd)

    if len(data["tilesets"]) != 1:
        parser.error("Unsupported number of tilesets %d" % len(data["tilesets"]))

    tileset = data["tilesets"][0]

    map_layer = get_layer(data, "Map")

    out = list(map(lambda x: (x - tileset["firstgid"]) & 0xFF, map_layer["data"]))

    gold_layer = get_layer(data, "Gold")

    total_gold = 0
    for t in gold_layer["data"]:
        if t != 0:
            total_gold += 1
    if total_gold > 255:
        parser.error("There are more than 255 gold pieces on the screen")

    out.extend(map(lambda x: (x - tileset["firstgid"]) & 0xFF, gold_layer["data"]))

    entity_layer = get_layer(data, "Entities")

    # some entities will never change their y coordinate, so sorting them by y
    # should help the drawing order in the game
    objs_sorted = sorted(entity_layer["objects"], key=lambda o: o["y"])

    if args.max_ents and len(objs_sorted) > args.max_ents:
        parser.error("Too many entities (limit: %s)" % args.max_ents)

    for ent in objs_sorted:
        try:
            typ = entity_types.index(ent["name"])
        except ValueError:
            parser.error("Entity in map %r not found in entity types" % ent["name"])

        x = ent["x"] // tileset["tilewidth"]
        y = ent["y"] // tileset["tileheight"]

        flags = 0

        dir = get_prop(ent, "dir", "right")
        if dir not in ("left", "right"):
            parser.error("Invalid value for property dir: %r" % dir)
        if dir == "left":
            flags |= 1

        out.extend([typ, x, y, flags])

        # end of entity list
    out.append(0xFF)

    tmp = args.output.removesuffix(".o")
    with open(tmp, "wb") as fd:
        fd.write(bytearray(out))
        fd.flush()

        # create an object file from the binary
        rc = subprocess.call(
            [
                ld,
                "-r",
                "-b",
                "binary",
                "-o",
                args.output,
                tmp,
            ]
        )
        os.unlink(tmp)
        if rc != 0:
            parser.error("Failed to run %s" % ld)

        # strip unwanted symbols
        rc = subprocess.call([strip, "-w", "-K", "*_%s_*" % tmp, args.output])
        if rc != 0:
            parser.error("Failed to run %s" % ld)


if __name__ == "__main__":
    main()