-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tiles.py
201 lines (152 loc) · 6 KB
/
create_tiles.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
Create square tiles using zone images from the game.
"""
import argparse
import math
import os
import shutil
from PIL import Image
from pathlib import Path
from tqdm import tqdm
# Dimension for the height and width of each tile. Must divide the height and
# width of the entire map.
TILE_LENGTH = 600
ZONE_HEIGHT_PX = 25 * 24
ZONE_WIDTH_PX = 80 * 16
MAP_HEIGHT_PX = (25 * 3) * ZONE_HEIGHT_PX
MAP_WIDTH_PX = (80 * 3) * ZONE_WIDTH_PX
# Scale factor for second layer of tiles
SCALE_FACTOR = 4
LARGE_TILE_LENGTH = TILE_LENGTH * SCALE_FACTOR
if MAP_HEIGHT_PX % TILE_LENGTH != 0:
raise Exception("Tile length must divide height of world map in pixels")
if MAP_WIDTH_PX % TILE_LENGTH != 0:
raise Exception("Tile width must divide width of world map in pixels")
tile_width = MAP_WIDTH_PX // TILE_LENGTH
tile_height = MAP_HEIGHT_PX // TILE_LENGTH
def clamp(x: int, min: int, max: int) -> int:
if x < min:
return min
if x > max:
return max
return x
def get_zone_for_pixel(x: int, y: int, z: int) -> str:
# Pixel is interpreted with (0, 0) in the top left of the world map
wx = x // (ZONE_WIDTH_PX * 3)
wy = y // (ZONE_HEIGHT_PX * 3)
px = (x // ZONE_WIDTH_PX) % 3
py = (y // ZONE_HEIGHT_PX) % 3
return f"JoppaWorld.{wx}.{wy}.{px}.{py}.{z}"
def fetch_rectangle(
bottom: tuple[int, int],
top: tuple[int, int],
z: int,
basedir: str | os.PathLike | None = None,
) -> Image:
"""Fetch a rectangle of pixels using the bounding box defined by the
provided coordinates."""
if basedir is None:
basedir = Path("worldmap")
if isinstance(basedir, str):
basedir = Path(basedir)
assert bottom <= top
tile = Image.new("RGB", (top[0] - bottom[0], top[1] - bottom[1]))
# Stride over the pixels row-wise
x = bottom[0]
while x < top[0]:
x_max = x + ZONE_WIDTH_PX
x_max -= x % ZONE_WIDTH_PX
x_max = clamp(x_max, bottom[0], top[0])
y = bottom[1]
while y < top[1]:
# Get the zone containing this pixel
zoneid = get_zone_for_pixel(x, y, z)
zone_img = Image.open(basedir / f"{zoneid}.png")
# Crop zone
y_max = y + ZONE_HEIGHT_PX
y_max -= y % ZONE_HEIGHT_PX
y_max = clamp(y_max, bottom[1], top[1])
assert x_max >= x
assert y_max >= y
x_zone = x % ZONE_WIDTH_PX
y_zone = y % ZONE_HEIGHT_PX
x_zone_max = clamp((x_max - x) + x_zone, 0, ZONE_WIDTH_PX)
y_zone_max = clamp((y_max - y) + y_zone, 0, ZONE_HEIGHT_PX)
assert x_zone < x_zone_max
assert y_zone < y_zone_max
zone_img_cropped = zone_img.crop((x_zone, y_zone, x_zone_max, y_zone_max))
# Paste into tile
x_tile = x - bottom[0]
y_tile = y - bottom[1]
tile.paste(zone_img_cropped, (x_tile, y_tile))
y = y_max
x = x_max
# Return constructed tile
return tile
def main(args) -> None:
num_tiles = MAP_HEIGHT_PX * MAP_WIDTH_PX // (TILE_LENGTH**2)
pbar_iterator = iter(pbar := tqdm(range(num_tiles)))
basedir = Path(args.worldmap_dir)
(outdir := Path(args.output)).mkdir(exist_ok=True)
world = Image.open(basedir / "world.png")
world.save(outdir / "world.webp", lossless=True)
for x in range(0, MAP_WIDTH_PX // TILE_LENGTH):
for y in range(0, MAP_HEIGHT_PX // TILE_LENGTH):
if (outpath := outdir / f"tile_0_{x}_{y}_{args.zlevel}.webp").exists():
next(pbar_iterator)
continue
pixel_x = x * TILE_LENGTH
pixel_y = y * TILE_LENGTH
upper_corner = (pixel_x, pixel_y)
lower_corner = (pixel_x + TILE_LENGTH, pixel_y + TILE_LENGTH)
tile = fetch_rectangle(
upper_corner, lower_corner, args.zlevel, basedir=basedir
)
tile.save(outpath, lossless=True)
i = next(pbar_iterator)
# Recombine tiles into next tier
num_tiles = math.ceil(MAP_HEIGHT_PX / LARGE_TILE_LENGTH) * math.ceil(
MAP_WIDTH_PX / LARGE_TILE_LENGTH
)
pbar_iterator = iter(tqdm(range(num_tiles)))
for x in range(0, math.ceil(MAP_WIDTH_PX / LARGE_TILE_LENGTH)):
for y in range(0, math.ceil(MAP_HEIGHT_PX / LARGE_TILE_LENGTH)):
if (outpath := outdir / f"tile_1_{x}_{y}_{args.zlevel}.webp").exists():
continue
tile = Image.new("RGBA", (TILE_LENGTH, TILE_LENGTH), (255, 0, 0, 0))
tile_x_start = 4 * x
tile_x_end = min(4 * x + 4, MAP_WIDTH_PX // TILE_LENGTH)
tile_y_start = 4 * y
tile_y_end = min(4 * y + 4, MAP_HEIGHT_PX // TILE_LENGTH)
for tile_x in range(tile_x_start, tile_x_end):
for tile_y in range(tile_y_start, tile_y_end):
subtile_path = (
outdir / f"tile_0_{tile_x}_{tile_y}_{args.zlevel}.webp"
)
corner_x = (tile_x - tile_x_start) * (TILE_LENGTH // SCALE_FACTOR)
corner_y = (tile_y - tile_y_start) * (TILE_LENGTH // SCALE_FACTOR)
subtile = Image.open(subtile_path)
subtile = subtile.resize(
(TILE_LENGTH // SCALE_FACTOR, TILE_LENGTH // SCALE_FACTOR),
Image.Resampling.LANCZOS,
)
tile.paste(subtile, (corner_x, corner_y))
tile.save(outpath)
i = next(pbar_iterator)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-w",
"--worldmap_dir",
default="./worldmap",
help="Directory containing exported zone images",
)
parser.add_argument(
"-o", "--output", default="./tiles", help="Default directory to write output to"
)
parser.add_argument(
"-z", "--zlevel", type=int, default=10, help="z-level to create tiles for"
)
args = parser.parse_args()
main(args)