| 1 | #!/usr/bin/env python3 |
| 2 | """Build server/lib/blocks.json (append-only registry) and new unlockable |
| 3 | block PNGs in the existing 64×64 style: coloured silhouette, dark outline, |
| 4 | soft highlight, drop shadow. |
| 5 | |
| 6 | Official catalog colours are listed first (Terra's five, then the other |
| 7 | planets in catalog order) and their existing PNGs are left untouched. |
| 8 | New icons are appended and rendered here. |
| 9 | |
| 10 | Run from the repo root: python3 scripts/gen_unlock_blocks.py |
| 11 | """ |
| 12 | from __future__ import annotations |
| 13 | |
| 14 | import json |
| 15 | import math |
| 16 | from pathlib import Path |
| 17 | |
| 18 | from PIL import Image, ImageDraw, ImageFilter |
| 19 | |
| 20 | ROOT = Path(__file__).resolve().parents[1] |
| 21 | CATALOG = ROOT / 'server' / 'lib' / 'planets.json' |
| 22 | OUT_JSON = ROOT / 'server' / 'lib' / 'blocks.json' |
| 23 | OUT_IMG = ROOT / 'build' / 'images' |
| 24 | SIZE = 64 |
| 25 | SCALE = 4 |
| 26 | SS = SIZE * SCALE |
| 27 | |
| 28 | |
| 29 | def hex_rgb(h): |
| 30 | h = h.lstrip('#') |
| 31 | return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4)) |
| 32 | |
| 33 | |
| 34 | def lighten(rgb, f=0.35): |
| 35 | return tuple(min(255, int(c + (255 - c) * f)) for c in rgb) |
| 36 | |
| 37 | |
| 38 | def darken(rgb, f=0.28): |
| 39 | return tuple(max(0, int(c * (1 - f))) for c in rgb) |
| 40 | |
| 41 | |
| 42 | def regular_poly(cx, cy, r, n, rot=-math.pi / 2): |
| 43 | pts = [] |
| 44 | for i in range(n): |
| 45 | a = rot + i * 2 * math.pi / n |
| 46 | pts.append((cx + r * math.cos(a), cy + r * math.sin(a))) |
| 47 | return pts |
| 48 | |
| 49 | |
| 50 | def star_pts(cx, cy, r, n=5, inner=0.42, rot=-math.pi / 2): |
| 51 | pts = [] |
| 52 | for i in range(n * 2): |
| 53 | a = rot + i * math.pi / n |
| 54 | rr = r if i % 2 == 0 else r * inner |
| 55 | pts.append((cx + rr * math.cos(a), cy + rr * math.sin(a))) |
| 56 | return pts |
| 57 | |
| 58 | |
| 59 | # ---------- shape primitives (filled polygons / lines) ---------- |
| 60 | |
| 61 | def poly_stroke(draw, pts, fill, outline, width): |
| 62 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 63 | |
| 64 | |
| 65 | def thick_lines(draw, segs, color, width, closed=False): |
| 66 | """segs: list of point lists.""" |
| 67 | for pts in segs: |
| 68 | if len(pts) < 2: |
| 69 | continue |
| 70 | draw.line(pts, fill=color, width=width, joint='curve') |
| 71 | # round caps |
| 72 | r = width / 2 |
| 73 | for p in (pts[0], pts[-1]): |
| 74 | draw.ellipse([p[0] - r, p[1] - r, p[0] + r, p[1] + r], fill=color) |
| 75 | if closed and pts[0] != pts[-1]: |
| 76 | draw.line([pts[-1], pts[0]], fill=color, width=width) |
| 77 | |
| 78 | |
| 79 | def icon_path(cx, cy, r, kind): |
| 80 | """Return ('poly', pts) or ('polys', [pts, ...]) or ('lines', [pts,...], wfrac) |
| 81 | or ('combo', list of commands). Coordinates in actual pixels.""" |
| 82 | def P(x, y): |
| 83 | return (cx + x * r, cy + y * r) |
| 84 | |
| 85 | k = kind.lower() |
| 86 | if k == 'circle': |
| 87 | return ('ellipse', [-1, -1, 1, 1]) |
| 88 | if k == 'square': |
| 89 | s = 0.86 |
| 90 | return ('roundrect', [-s, -s, s, s], 0.12) |
| 91 | if k == 'triangle': |
| 92 | return ('poly', regular_poly(cx, cy + r * 0.08, r * 1.08, 3, -math.pi / 2)) |
| 93 | if k == 'diamond': |
| 94 | return ('poly', regular_poly(cx, cy, r * 1.05, 4, 0)) |
| 95 | if k == 'star': |
| 96 | return ('poly', star_pts(cx, cy, r * 1.12)) |
| 97 | if k == 'hexagon': |
| 98 | return ('poly', regular_poly(cx, cy, r * 1.05, 6, 0)) |
| 99 | if k == 'pentagon': |
| 100 | return ('poly', regular_poly(cx, cy, r * 1.08, 5)) |
| 101 | if k == 'arrow': |
| 102 | return ('poly', [ |
| 103 | P(0, -1.1), P(0.85, 0.05), P(0.28, 0.05), P(0.28, 1), |
| 104 | P(-0.28, 1), P(-0.28, 0.05), P(-0.85, 0.05), |
| 105 | ]) |
| 106 | if k == 'chevron': |
| 107 | return ('poly', [ |
| 108 | P(0, -1.05), P(0.95, 0.15), P(0.95, 0.55), P(0, -0.15), |
| 109 | P(-0.95, 0.55), P(-0.95, 0.15), |
| 110 | ]) |
| 111 | if k == 'double_arrow': |
| 112 | return ('poly', [ |
| 113 | P(0, -1.15), P(0.72, -0.25), P(0.28, -0.25), P(0.28, 0.05), |
| 114 | P(0.72, 0.05), P(0, 1.15), P(-0.72, 0.05), P(-0.28, 0.05), |
| 115 | P(-0.28, -0.25), P(-0.72, -0.25), |
| 116 | ]) |
| 117 | if k == 'return_arrow': |
| 118 | return ('poly', [ |
| 119 | P(-0.15, -1.1), P(0.7, -0.4), P(0.25, -0.4), P(0.25, 0.15), |
| 120 | P(0.85, 0.15), P(0.85, 0.55), P(-0.25, 0.55), P(-0.25, -0.4), |
| 121 | P(-0.7, -0.4), |
| 122 | ]) |
| 123 | # Runes — stylized, thick, readable |
| 124 | if k == 'rune_fehu': |
| 125 | return ('lines', [ |
| 126 | [P(-0.15, 1.0), P(-0.15, -1.0)], |
| 127 | [P(-0.15, -0.55), P(0.85, -0.95)], |
| 128 | [P(-0.15, -0.05), P(0.85, -0.45)], |
| 129 | ], 0.28) |
| 130 | if k == 'rune_ansuz': |
| 131 | return ('lines', [ |
| 132 | [P(-0.2, 1.0), P(-0.2, -1.0)], |
| 133 | [P(-0.2, -0.85), P(0.85, -0.25)], |
| 134 | [P(-0.2, -0.15), P(0.7, 0.45)], |
| 135 | ], 0.28) |
| 136 | if k == 'rune_thurisaz': |
| 137 | return ('lines', [ |
| 138 | [P(-0.35, 1.0), P(-0.35, -1.0)], |
| 139 | [P(-0.35, -0.55), P(0.7, 0), P(-0.35, 0.55)], |
| 140 | ], 0.28) |
| 141 | if k == 'rune_raido': |
| 142 | return ('lines', [ |
| 143 | [P(-0.3, 1.0), P(-0.3, -1.0)], |
| 144 | [P(-0.3, -1.0), P(0.65, -0.55), P(-0.3, -0.1)], |
| 145 | [P(-0.05, -0.1), P(0.7, 1.0)], |
| 146 | ], 0.26) |
| 147 | if k == 'rune_sowilo': |
| 148 | return ('lines', [ |
| 149 | [P(0.55, -1.05), P(-0.45, -0.25), P(0.45, 0.25), P(-0.55, 1.05)], |
| 150 | ], 0.30) |
| 151 | if k == 'rune_algiz': |
| 152 | return ('lines', [ |
| 153 | [P(0, 1.05), P(0, -1.05)], |
| 154 | [P(0, -0.15), P(-0.75, -0.95)], |
| 155 | [P(0, -0.15), P(0.75, -0.95)], |
| 156 | ], 0.28) |
| 157 | # Figures |
| 158 | if k == 'person': |
| 159 | # head + body + arms + legs as one silhouette |
| 160 | head = [] |
| 161 | for i in range(16): |
| 162 | a = i * 2 * math.pi / 16 |
| 163 | head.append(P(0.0 + 0.28 * math.cos(a), -0.72 + 0.28 * math.sin(a))) |
| 164 | body = [ |
| 165 | P(-0.22, -0.42), P(-0.72, -0.15), P(-0.72, 0.12), P(-0.22, -0.08), |
| 166 | P(-0.28, 0.35), P(-0.55, 1.05), P(-0.22, 1.05), P(0, 0.5), |
| 167 | P(0.22, 1.05), P(0.55, 1.05), P(0.28, 0.35), P(0.22, -0.08), |
| 168 | P(0.72, 0.12), P(0.72, -0.15), P(0.22, -0.42), |
| 169 | ] |
| 170 | return ('polys', [head, body]) |
| 171 | if k == 'walker': |
| 172 | head = [P(0.15 + 0.26 * math.cos(i * 2 * math.pi / 16), |
| 173 | -0.7 + 0.26 * math.sin(i * 2 * math.pi / 16)) for i in range(16)] |
| 174 | body = [ |
| 175 | P(-0.05, -0.42), P(-0.65, 0.05), P(-0.45, 0.25), P(-0.05, -0.05), |
| 176 | P(-0.15, 0.35), P(-0.7, 0.95), P(-0.4, 1.08), P(0.05, 0.45), |
| 177 | P(0.45, 1.08), P(0.7, 0.85), P(0.25, 0.3), P(0.25, -0.05), |
| 178 | P(0.7, -0.25), P(0.5, -0.45), P(0.2, -0.42), |
| 179 | ] |
| 180 | return ('polys', [head, body]) |
| 181 | if k == 'hero': |
| 182 | # arms up in a V |
| 183 | head = [P(0.0 + 0.26 * math.cos(i * 2 * math.pi / 16), |
| 184 | -0.62 + 0.26 * math.sin(i * 2 * math.pi / 16)) for i in range(16)] |
| 185 | body = [ |
| 186 | P(-0.18, -0.35), P(-0.95, -0.95), P(-0.7, -1.08), P(-0.1, -0.4), |
| 187 | P(-0.25, 0.35), P(-0.5, 1.08), P(-0.18, 1.08), P(0, 0.5), |
| 188 | P(0.18, 1.08), P(0.5, 1.08), P(0.25, 0.35), P(0.1, -0.4), |
| 189 | P(0.7, -1.08), P(0.95, -0.95), P(0.18, -0.35), |
| 190 | ] |
| 191 | return ('polys', [head, body]) |
| 192 | # Faces |
| 193 | if k == 'smile': |
| 194 | return ('face', 'smile') |
| 195 | if k == 'wink': |
| 196 | return ('face', 'wink') |
| 197 | if k == 'cool': |
| 198 | return ('face', 'cool') |
| 199 | if k == 'grin': |
| 200 | return ('face', 'grin') |
| 201 | # Icons |
| 202 | if k == 'heart': |
| 203 | pts = [] |
| 204 | for i in range(32): |
| 205 | t = i * 2 * math.pi / 32 |
| 206 | # classic heart parametric, scaled |
| 207 | x = 16 * math.sin(t) ** 3 |
| 208 | y = -(13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)) |
| 209 | pts.append(P(x / 16.5, y / 16.5 + 0.08)) |
| 210 | return ('poly', pts) |
| 211 | if k == 'lightning': |
| 212 | return ('poly', [ |
| 213 | P(0.15, -1.1), P(0.55, -1.1), P(0.05, -0.1), P(0.45, -0.1), |
| 214 | P(-0.55, 1.15), P(-0.05, 0.15), P(-0.5, 0.15), |
| 215 | ]) |
| 216 | if k == 'shield': |
| 217 | return ('poly', [ |
| 218 | P(0, -1.1), P(0.95, -0.7), P(0.85, 0.25), P(0, 1.1), |
| 219 | P(-0.85, 0.25), P(-0.95, -0.7), |
| 220 | ]) |
| 221 | if k == 'crown': |
| 222 | return ('poly', [ |
| 223 | P(-0.95, 0.45), P(-0.95, -0.15), P(-0.5, 0.25), P(0, -0.95), |
| 224 | P(0.5, 0.25), P(0.95, -0.15), P(0.95, 0.45), |
| 225 | P(0.85, 0.85), P(-0.85, 0.85), |
| 226 | ]) |
| 227 | if k == 'eye': |
| 228 | return ('eye',) |
| 229 | if k == 'star6': |
| 230 | return ('poly', star_pts(cx, cy, r * 1.12, n=6, inner=0.48)) |
| 231 | if k == 'spiral': |
| 232 | pts = [] |
| 233 | for i in range(60): |
| 234 | t = i / 59 * 3.4 * math.pi |
| 235 | rr = 0.12 + 0.88 * (i / 59) |
| 236 | pts.append(P(rr * math.cos(t), rr * math.sin(t))) |
| 237 | return ('lines', [pts], 0.26) |
| 238 | if k == 'wave': |
| 239 | pts_top, pts_bot = [], [] |
| 240 | for i in range(21): |
| 241 | x = -1.05 + i * 2.1 / 20 |
| 242 | y = 0.28 * math.sin(x * math.pi * 1.4) |
| 243 | pts_top.append(P(x, y - 0.28)) |
| 244 | pts_bot.append(P(x, y + 0.28)) |
| 245 | return ('poly', pts_top + list(reversed(pts_bot))) |
| 246 | if k == 'mountain': |
| 247 | return ('poly', [ |
| 248 | P(-1.1, 0.9), P(-0.45, -0.55), P(-0.15, -0.05), |
| 249 | P(0.15, -1.05), P(0.5, -0.25), P(0.7, -0.55), |
| 250 | P(1.1, 0.9), |
| 251 | ]) |
| 252 | if k == 'fish': |
| 253 | return ('poly', [ |
| 254 | P(-1.05, 0), P(-0.55, -0.55), P(0.35, -0.55), P(0.75, -0.15), |
| 255 | P(1.1, -0.55), P(0.85, 0), P(1.1, 0.55), P(0.75, 0.15), |
| 256 | P(0.35, 0.55), P(-0.55, 0.55), |
| 257 | ]) |
| 258 | if k == 'bird': |
| 259 | return ('poly', [ |
| 260 | P(-1.05, 0.15), P(-0.3, -0.15), P(-0.05, -0.85), P(0.2, -0.15), |
| 261 | P(0.55, -0.25), P(1.05, 0.05), P(0.45, 0.15), |
| 262 | P(0.2, 0.85), P(-0.05, 0.2), P(-0.55, 0.45), |
| 263 | ]) |
| 264 | if k == 'key': |
| 265 | # head circle + shaft + teeth as one poly |
| 266 | pts = [] |
| 267 | for i in range(12): |
| 268 | a = -math.pi * 0.6 + i * 1.6 * math.pi / 11 |
| 269 | pts.append(P(-0.55 + 0.42 * math.cos(a), -0.55 + 0.42 * math.sin(a))) |
| 270 | pts += [ |
| 271 | P(-0.25, -0.2), P(1.05, -0.2), P(1.05, 0.15), |
| 272 | P(0.85, 0.15), P(0.85, 0.5), P(0.6, 0.5), P(0.6, 0.15), |
| 273 | P(0.4, 0.15), P(0.4, 0.38), P(0.15, 0.38), P(0.15, 0.15), |
| 274 | P(-0.25, 0.15), |
| 275 | ] |
| 276 | return ('poly', pts) |
| 277 | if k == 'comet': |
| 278 | return ('poly', [ |
| 279 | P(0.95, -0.95), P(0.35, -0.45), P(0.15, -0.7), P(-0.15, -0.15), |
| 280 | P(0.15, -0.05), P(-1.05, 1.05), P(-0.05, 0.15), P(0.15, 0.35), |
| 281 | P(0.55, -0.15), P(0.45, -0.45), |
| 282 | ]) |
| 283 | if k == 'clover': |
| 284 | lobes = [] |
| 285 | for ang, ox, oy in ((0, 0, -0.42), (2.1, -0.4, 0.18), (4.2, 0.4, 0.18)): |
| 286 | pts = [] |
| 287 | for i in range(16): |
| 288 | a = i * 2 * math.pi / 16 |
| 289 | pts.append(P(ox + 0.42 * math.cos(a), oy + 0.42 * math.sin(a))) |
| 290 | lobes.append(pts) |
| 291 | stem = [P(-0.08, 0.35), P(0.08, 0.35), P(0.18, 1.05), P(-0.05, 1.05)] |
| 292 | lobes.append(stem) |
| 293 | return ('polys', lobes) |
| 294 | if k == 'paw': |
| 295 | pads = [] |
| 296 | for ox, oy, rr in ((-0.5, -0.55, 0.28), (0, -0.75, 0.28), (0.5, -0.55, 0.28), |
| 297 | (0.0, 0.28, 0.52)): |
| 298 | pts = [] |
| 299 | for i in range(16): |
| 300 | a = i * 2 * math.pi / 16 |
| 301 | pts.append(P(ox + rr * math.cos(a), oy + rr * math.sin(a))) |
| 302 | pads.append(pts) |
| 303 | return ('polys', pads) |
| 304 | if k == 'sword': |
| 305 | return ('poly', [ |
| 306 | P(-0.18, 1.1), P(-0.18, 0.45), P(-0.55, 0.45), P(-0.55, 0.22), |
| 307 | P(-0.18, 0.22), P(-0.18, -0.95), P(0, -1.15), P(0.18, -0.95), |
| 308 | P(0.18, 0.22), P(0.55, 0.22), P(0.55, 0.45), P(0.18, 0.45), |
| 309 | P(0.18, 1.1), |
| 310 | ]) |
| 311 | if k == 'bell': |
| 312 | pts = [] |
| 313 | for i in range(18): |
| 314 | a = math.pi + i * math.pi / 17 |
| 315 | pts.append(P(0.85 * math.cos(a), -0.25 + 0.85 * math.sin(a))) |
| 316 | pts += [P(0.95, 0.55), P(0.7, 0.75), P(0.15, 0.75), P(0.15, 0.95), |
| 317 | P(-0.15, 0.95), P(-0.15, 0.75), P(-0.7, 0.75), P(-0.95, 0.55)] |
| 318 | return ('poly', pts) |
| 319 | if k == 'mushroom': |
| 320 | cap = [] |
| 321 | for i in range(18): |
| 322 | a = math.pi + i * math.pi / 17 |
| 323 | cap.append(P(0.95 * math.cos(a), -0.25 + 0.7 * math.sin(a))) |
| 324 | cap += [P(0.95, 0.05), P(-0.95, 0.05)] |
| 325 | stem = [P(-0.32, 0.05), P(0.32, 0.05), P(0.38, 1.05), P(-0.38, 1.05)] |
| 326 | return ('polys', [cap, stem]) |
| 327 | if k == 'cactus': |
| 328 | return ('poly', [ |
| 329 | P(-0.22, 1.05), P(-0.22, 0.15), P(-0.75, 0.15), P(-0.75, -0.35), |
| 330 | P(-0.5, -0.35), P(-0.5, -0.05), P(-0.22, -0.05), P(-0.22, -1.05), |
| 331 | P(0.22, -1.05), P(0.22, -0.25), P(0.55, -0.25), P(0.55, -0.7), |
| 332 | P(0.8, -0.7), P(0.8, -0.02), P(0.22, -0.02), P(0.22, 1.05), |
| 333 | ]) |
| 334 | if k == 'ring': |
| 335 | return ('ring', 1.0, 0.55) |
| 336 | if k == 'flag': |
| 337 | return ('poly', [ |
| 338 | P(-0.7, 1.1), P(-0.7, -1.05), P(-0.5, -1.05), P(-0.5, -0.85), |
| 339 | P(1.0, -0.85), P(0.7, -0.4), P(1.0, 0.05), P(-0.5, 0.05), |
| 340 | P(-0.5, 1.1), |
| 341 | ]) |
| 342 | if k == 'snowflake': |
| 343 | arms = [] |
| 344 | for k0 in range(6): |
| 345 | a = k0 * math.pi / 3 |
| 346 | c, s = math.cos(a), math.sin(a) |
| 347 | def rot(x, y): |
| 348 | return P(x * c - y * s, x * s + y * c) |
| 349 | arms.append([rot(0, 0), rot(0, -1.05)]) |
| 350 | arms.append([rot(-0.28, -0.6), rot(0, -0.4), rot(0.28, -0.6)]) |
| 351 | return ('lines', arms, 0.22) |
| 352 | if k == 'flame_icon': |
| 353 | return ('poly', [ |
| 354 | P(0, -1.15), P(0.35, -0.45), P(0.55, -0.7), P(0.75, -0.15), |
| 355 | P(0.55, 0.55), P(0.2, 1.0), P(-0.25, 1.0), P(-0.6, 0.5), |
| 356 | P(-0.7, -0.05), P(-0.35, -0.55), P(-0.15, -0.2), |
| 357 | ]) |
| 358 | if k == 'ankh': |
| 359 | return ('lines', [ |
| 360 | [P(0, 1.05), P(0, -0.25)], |
| 361 | [P(-0.55, 0.15), P(0.55, 0.15)], |
| 362 | ], 0.28) # loop added in combo |
| 363 | if k == 'yinyang': |
| 364 | return ('yinyang',) |
| 365 | if k == 'crescent': |
| 366 | # moon |
| 367 | return ('crescent',) |
| 368 | # fallback |
| 369 | return ('ellipse', [-1, -1, 1, 1]) |
| 370 | |
| 371 | |
| 372 | def draw_kind(draw, img, kind, cx, cy, r, fill, outline, width): |
| 373 | cmd = icon_path(cx, cy, r, kind) |
| 374 | kind_l = kind.lower() |
| 375 | |
| 376 | def fill_poly(pts): |
| 377 | if len(pts) >= 3: |
| 378 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 379 | |
| 380 | def fill_lines(segs, wfrac): |
| 381 | w = max(width, int(r * wfrac)) |
| 382 | # draw in outline color slightly thicker, then fill |
| 383 | thick_lines(draw, segs, outline, w + max(2, width // 2)) |
| 384 | thick_lines(draw, segs, fill, w) |
| 385 | |
| 386 | if cmd[0] == 'poly': |
| 387 | fill_poly(cmd[1]) |
| 388 | elif cmd[0] == 'polys': |
| 389 | for pts in cmd[1]: |
| 390 | fill_poly(pts) |
| 391 | elif cmd[0] == 'lines': |
| 392 | fill_lines(cmd[1], cmd[2]) |
| 393 | if kind_l == 'ankh': |
| 394 | # loop at top |
| 395 | box = [cx - r * 0.42, cy - r * 1.15, cx + r * 0.42, cy - r * 0.25] |
| 396 | draw.ellipse(box, outline=outline, width=int(r * 0.28) + width) |
| 397 | draw.ellipse([box[0] + r * 0.14, box[1] + r * 0.14, |
| 398 | box[2] - r * 0.14, box[3] - r * 0.14], |
| 399 | outline=fill, width=int(r * 0.16)) |
| 400 | # fill ring by a second pass |
| 401 | # simpler: thick ellipse |
| 402 | w = int(r * 0.26) |
| 403 | draw.ellipse(box, outline=outline, width=w + width) |
| 404 | draw.ellipse(box, outline=fill, width=w) |
| 405 | elif cmd[0] == 'ellipse': |
| 406 | a, b, c, d = cmd[1] |
| 407 | box = [cx + a * r, cy + b * r, cx + c * r, cy + d * r] |
| 408 | draw.ellipse(box, fill=fill, outline=outline, width=width) |
| 409 | elif cmd[0] == 'roundrect': |
| 410 | a, b, c, d = cmd[1] |
| 411 | box = [cx + a * r, cy + b * r, cx + c * r, cy + d * r] |
| 412 | draw.rounded_rectangle(box, radius=r * cmd[2], fill=fill, outline=outline, width=width) |
| 413 | elif cmd[0] == 'ring': |
| 414 | _, ro, ri = cmd |
| 415 | outer = [cx - r * ro, cy - r * ro, cx + r * ro, cy + r * ro] |
| 416 | inner = [cx - r * ri, cy - r * ri, cx + r * ri, cy + r * ri] |
| 417 | draw.ellipse(outer, fill=fill, outline=outline, width=width) |
| 418 | # punch inner with destination-out |
| 419 | # We'll handle punch at a higher level; draw inner as transparent later |
| 420 | draw.ellipse(inner, fill=(0, 0, 0, 0), outline=outline, width=width) |
| 421 | elif cmd[0] == 'face': |
| 422 | box = [cx - r, cy - r, cx + r, cy + r] |
| 423 | draw.ellipse(box, fill=fill, outline=outline, width=width) |
| 424 | eye_c = outline |
| 425 | er = r * 0.12 |
| 426 | if cmd[1] == 'wink': |
| 427 | draw.ellipse([cx - r * 0.38 - er, cy - r * 0.28 - er, |
| 428 | cx - r * 0.38 + er, cy - r * 0.28 + er], fill=eye_c) |
| 429 | draw.line([(cx + r * 0.18, cy - r * 0.28), (cx + r * 0.52, cy - r * 0.28)], |
| 430 | fill=eye_c, width=max(width, int(r * 0.12))) |
| 431 | elif cmd[1] == 'cool': |
| 432 | # sunglasses |
| 433 | draw.rounded_rectangle([cx - r * 0.7, cy - r * 0.42, cx - r * 0.08, cy - r * 0.08], |
| 434 | radius=r * 0.12, fill=eye_c) |
| 435 | draw.rounded_rectangle([cx + r * 0.08, cy - r * 0.42, cx + r * 0.7, cy - r * 0.08], |
| 436 | radius=r * 0.12, fill=eye_c) |
| 437 | draw.line([(cx - r * 0.08, cy - r * 0.25), (cx + r * 0.08, cy - r * 0.25)], |
| 438 | fill=eye_c, width=max(3, int(r * 0.1))) |
| 439 | else: |
| 440 | draw.ellipse([cx - r * 0.38 - er, cy - r * 0.28 - er, |
| 441 | cx - r * 0.38 + er, cy - r * 0.28 + er], fill=eye_c) |
| 442 | draw.ellipse([cx + r * 0.38 - er, cy - r * 0.28 - er, |
| 443 | cx + r * 0.38 + er, cy - r * 0.28 + er], fill=eye_c) |
| 444 | # smile |
| 445 | smile = [] |
| 446 | for i in range(9): |
| 447 | a = math.pi * 0.2 + i * math.pi * 0.6 / 8 |
| 448 | smile.append((cx + r * 0.48 * math.cos(a), cy + r * 0.12 + r * 0.42 * math.sin(a))) |
| 449 | sw = max(width, int(r * 0.12)) |
| 450 | if cmd[1] == 'grin': |
| 451 | draw.chord([cx - r * 0.5, cy + r * 0.05, cx + r * 0.5, cy + r * 0.65], |
| 452 | start=0, end=180, fill=eye_c, outline=eye_c) |
| 453 | else: |
| 454 | draw.line(smile, fill=eye_c, width=sw, joint='curve') |
| 455 | elif cmd[0] == 'eye': |
| 456 | # almond |
| 457 | pts = [] |
| 458 | for i in range(24): |
| 459 | t = i * 2 * math.pi / 24 |
| 460 | x = math.cos(t) |
| 461 | y = 0.5 * math.sin(t) |
| 462 | pts.append((cx + r * 1.05 * x, cy + r * 0.85 * y)) |
| 463 | fill_poly(pts) |
| 464 | iris_c = outline |
| 465 | draw.ellipse([cx - r * 0.38, cy - r * 0.38, cx + r * 0.38, cy + r * 0.38], |
| 466 | fill=iris_c) |
| 467 | draw.ellipse([cx - r * 0.14, cy - r * 0.18, cx + r * 0.06, cy + r * 0.02], |
| 468 | fill=lighten(fill[:3] if isinstance(fill, tuple) else (255, 255, 255), 0.8) + (255,)) |
| 469 | elif cmd[0] == 'yinyang': |
| 470 | draw.ellipse([cx - r, cy - r, cx + r, cy + r], fill=fill, outline=outline, width=width) |
| 471 | # right half dark via chord is messy; draw a dark semicircle + two dots |
| 472 | # Use a path: top-center to bottom via right, with small circles |
| 473 | # We'll approximate with a pie and two discs |
| 474 | dark = outline |
| 475 | draw.pieslice([cx - r, cy - r, cx + r, cy + r], start=90, end=270, fill=dark) |
| 476 | draw.ellipse([cx - r * 0.5, cy - r, cx + r * 0.5, cy], fill=dark) |
| 477 | draw.ellipse([cx - r * 0.5, cy, cx + r * 0.5, cy + r], fill=fill) |
| 478 | # dots |
| 479 | draw.ellipse([cx - r * 0.16, cy - r * 0.58, cx + r * 0.16, cy - r * 0.26], fill=fill) |
| 480 | draw.ellipse([cx - r * 0.16, cy + r * 0.26, cx + r * 0.16, cy + r * 0.58], fill=dark) |
| 481 | draw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=outline, width=width) |
| 482 | elif cmd[0] == 'crescent': |
| 483 | # moon: two circles, punch later — draw a crescent polygon |
| 484 | outer, inner = [], [] |
| 485 | for i in range(24): |
| 486 | a = -math.pi * 0.75 + i * (1.5 * math.pi) / 23 |
| 487 | outer.append((cx + r * math.cos(a) - r * 0.05, cy + r * math.sin(a))) |
| 488 | ox, oy = cx + r * 0.42, cy - r * 0.05 |
| 489 | for i in range(20, -1, -1): |
| 490 | a = -math.pi * 0.7 + i * (1.4 * math.pi) / 20 |
| 491 | inner.append((ox + r * 0.72 * math.cos(a), oy + r * 0.72 * math.sin(a))) |
| 492 | fill_poly(outer + inner) |
| 493 | else: |
| 494 | draw.ellipse([cx - r, cy - r, cx + r, cy + r], fill=fill, outline=outline, width=width) |
| 495 | |
| 496 | |
| 497 | def render_block(hex_color, shape): |
| 498 | rgb = hex_rgb(hex_color) |
| 499 | hi = lighten(rgb, 0.42) |
| 500 | lo = darken(rgb, 0.32) |
| 501 | img = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 502 | cx = cy = SS / 2 |
| 503 | r = SS * 0.36 |
| 504 | width = max(2, SCALE) |
| 505 | # drop shadow |
| 506 | shadow = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 507 | sd = ImageDraw.Draw(shadow) |
| 508 | draw_kind(sd, shadow, shape, cx + SCALE * 1.2, cy + SCALE * 1.6, r, |
| 509 | (0, 0, 0, 90), None, 0) |
| 510 | shadow = shadow.filter(ImageFilter.GaussianBlur(radius=SCALE * 1.1)) |
| 511 | img = Image.alpha_composite(img, shadow) |
| 512 | # For ring, punch a hole after drawing body |
| 513 | body = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 514 | bd = ImageDraw.Draw(body) |
| 515 | draw_kind(bd, body, shape, cx, cy, r, rgb + (255,), lo + (255,), width) |
| 516 | if shape == 'ring': |
| 517 | # punch inner hole |
| 518 | mask = Image.new('L', (SS, SS), 0) |
| 519 | md = ImageDraw.Draw(mask) |
| 520 | ri = r * 0.55 |
| 521 | md.ellipse([cx - ri, cy - ri, cx + ri, cy + ri], fill=255) |
| 522 | clear = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 523 | body = Image.composite(clear, body, mask) |
| 524 | # redraw inner outline |
| 525 | bd = ImageDraw.Draw(body) |
| 526 | bd.ellipse([cx - ri, cy - ri, cx + ri, cy + ri], outline=lo + (255,), width=width) |
| 527 | img = Image.alpha_composite(img, body) |
| 528 | # highlight |
| 529 | hi_layer = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 530 | hd = ImageDraw.Draw(hi_layer) |
| 531 | draw_kind(hd, hi_layer, shape, cx - r * 0.06, cy - r * 0.10, r * 0.55, |
| 532 | hi + (110,), None, 0) |
| 533 | hi_layer = hi_layer.filter(ImageFilter.GaussianBlur(radius=SCALE * 0.6)) |
| 534 | # clip highlight to body alpha |
| 535 | body_a = body.split()[3] |
| 536 | ha = hi_layer.split()[3] |
| 537 | from PIL import ImageChops |
| 538 | ha = ImageChops.multiply(ha, body_a) |
| 539 | hi_layer.putalpha(ha) |
| 540 | img = Image.alpha_composite(img, hi_layer) |
| 541 | return img.resize((SIZE, SIZE), Image.Resampling.LANCZOS) |
| 542 | |
| 543 | |
| 544 | # New unlockable blocks, appended after official catalog colours. |
| 545 | # ids are stable and must never be reordered or reused. |
| 546 | NEW_BLOCKS = [ |
| 547 | # Runes |
| 548 | {'id': 'rune-fehu', 'name': 'Fehu', 'hex': '#e8a44a', 'shape': 'rune_fehu'}, |
| 549 | {'id': 'rune-ansuz', 'name': 'Ansuz', 'hex': '#6ab0e8', 'shape': 'rune_ansuz'}, |
| 550 | {'id': 'rune-thurisaz', 'name': 'Thurisaz', 'hex': '#c05040', 'shape': 'rune_thurisaz'}, |
| 551 | {'id': 'rune-raido', 'name': 'Raido', 'hex': '#d0c070', 'shape': 'rune_raido'}, |
| 552 | {'id': 'rune-sowilo', 'name': 'Sowilo', 'hex': '#f0d040', 'shape': 'rune_sowilo'}, |
| 553 | {'id': 'rune-algiz', 'name': 'Algiz', 'hex': '#80d0a0', 'shape': 'rune_algiz'}, |
| 554 | # Arrows |
| 555 | {'id': 'arrow-up', 'name': 'Arrow', 'hex': '#f07040', 'shape': 'arrow'}, |
| 556 | {'id': 'arrow-chevron', 'name': 'Chevron', 'hex': '#50c0e0', 'shape': 'chevron'}, |
| 557 | {'id': 'arrow-double', 'name': 'Double Arrow', 'hex': '#e05080', 'shape': 'double_arrow'}, |
| 558 | {'id': 'arrow-return', 'name': 'Return', 'hex': '#a080e0', 'shape': 'return_arrow'}, |
| 559 | # Figures |
| 560 | {'id': 'fig-person', 'name': 'Figure', 'hex': '#f0c0a0', 'shape': 'person'}, |
| 561 | {'id': 'fig-walker', 'name': 'Walker', 'hex': '#c09070', 'shape': 'walker'}, |
| 562 | {'id': 'fig-hero', 'name': 'Hero', 'hex': '#e8b070', 'shape': 'hero'}, |
| 563 | # Faces |
| 564 | {'id': 'face-smile', 'name': 'Smile', 'hex': '#f0d060', 'shape': 'smile'}, |
| 565 | {'id': 'face-wink', 'name': 'Wink', 'hex': '#f0a050', 'shape': 'wink'}, |
| 566 | {'id': 'face-cool', 'name': 'Cool', 'hex': '#6090d0', 'shape': 'cool'}, |
| 567 | {'id': 'face-grin', 'name': 'Grin', 'hex': '#f0c848', 'shape': 'grin'}, |
| 568 | # Icons |
| 569 | {'id': 'icon-heart', 'name': 'Heart', 'hex': '#e04060', 'shape': 'heart'}, |
| 570 | {'id': 'icon-moon', 'name': 'Moon', 'hex': '#d0d8f0', 'shape': 'crescent'}, |
| 571 | {'id': 'icon-bolt', 'name': 'Bolt', 'hex': '#f0e050', 'shape': 'lightning'}, |
| 572 | {'id': 'icon-shield', 'name': 'Shield', 'hex': '#5080c0', 'shape': 'shield'}, |
| 573 | {'id': 'icon-crown', 'name': 'Crown', 'hex': '#f0c030', 'shape': 'crown'}, |
| 574 | {'id': 'icon-eye', 'name': 'Eye', 'hex': '#70d0c0', 'shape': 'eye'}, |
| 575 | {'id': 'icon-star6', 'name': 'Hexastar', 'hex': '#f0e8a0', 'shape': 'star6'}, |
| 576 | {'id': 'icon-spiral', 'name': 'Spiral', 'hex': '#c070d0', 'shape': 'spiral'}, |
| 577 | {'id': 'icon-wave', 'name': 'Wave', 'hex': '#3090d0', 'shape': 'wave'}, |
| 578 | {'id': 'icon-mountain', 'name': 'Mountain', 'hex': '#809070', 'shape': 'mountain'}, |
| 579 | {'id': 'icon-fish', 'name': 'Fish', 'hex': '#40a8c8', 'shape': 'fish'}, |
| 580 | {'id': 'icon-bird', 'name': 'Bird', 'hex': '#e07050', 'shape': 'bird'}, |
| 581 | {'id': 'icon-key', 'name': 'Key', 'hex': '#d0b050', 'shape': 'key'}, |
| 582 | {'id': 'icon-comet', 'name': 'Comet', 'hex': '#90d0f0', 'shape': 'comet'}, |
| 583 | {'id': 'icon-clover', 'name': 'Clover', 'hex': '#40b060', 'shape': 'clover'}, |
| 584 | {'id': 'icon-paw', 'name': 'Paw', 'hex': '#c08050', 'shape': 'paw'}, |
| 585 | {'id': 'icon-sword', 'name': 'Sword', 'hex': '#b0b8c0', 'shape': 'sword'}, |
| 586 | {'id': 'icon-bell', 'name': 'Bell', 'hex': '#f0c848', 'shape': 'bell'}, |
| 587 | {'id': 'icon-mushroom', 'name': 'Mushroom', 'hex': '#e06050', 'shape': 'mushroom'}, |
| 588 | {'id': 'icon-cactus', 'name': 'Cactus', 'hex': '#50b068', 'shape': 'cactus'}, |
| 589 | {'id': 'icon-ring', 'name': 'Ring', 'hex': '#e8d080', 'shape': 'ring'}, |
| 590 | {'id': 'icon-flag', 'name': 'Flag', 'hex': '#d04050', 'shape': 'flag'}, |
| 591 | {'id': 'icon-snow', 'name': 'Snowflake', 'hex': '#c8e0f8', 'shape': 'snowflake'}, |
| 592 | {'id': 'icon-flame', 'name': 'Flame', 'hex': '#ff7030', 'shape': 'flame_icon'}, |
| 593 | {'id': 'icon-ankh', 'name': 'Ankh', 'hex': '#d0a060', 'shape': 'ankh'}, |
| 594 | {'id': 'icon-yin', 'name': 'Yin Yang', 'hex': '#606080', 'shape': 'yinyang'}, |
| 595 | ] |
| 596 | |
| 597 | |
| 598 | def official_blocks(catalog): |
| 599 | out = [] |
| 600 | seen = set() |
| 601 | for planet in catalog['planets']: |
| 602 | pid = planet['id'] |
| 603 | for c in planet['colors']: |
| 604 | bid = f"{pid}-{c['name']}" |
| 605 | if bid in seen: |
| 606 | raise SystemExit(f'duplicate official id {bid}') |
| 607 | seen.add(bid) |
| 608 | out.append({ |
| 609 | 'id': bid, |
| 610 | 'name': c['name'].capitalize() if pid == 'terra' else f"{planet['name']} {c['name']}", |
| 611 | 'hex': c['hex'], |
| 612 | 'shape': c.get('shape') or 'circle', |
| 613 | 'image': c['image'], |
| 614 | 'terra': pid == 'terra', |
| 615 | }) |
| 616 | return out |
| 617 | |
| 618 | |
| 619 | def main(): |
| 620 | catalog = json.loads(CATALOG.read_text()) |
| 621 | official = official_blocks(catalog) |
| 622 | terra = [b for b in official if b.get('terra')] |
| 623 | others = [b for b in official if not b.get('terra')] |
| 624 | if len(terra) != 5 or terra[0]['id'] != 'terra-ember': |
| 625 | raise SystemExit(f'Terra five unexpected: {[b["id"] for b in terra]}') |
| 626 | |
| 627 | new = [] |
| 628 | seen = {b['id'] for b in official} |
| 629 | OUT_IMG.mkdir(parents=True, exist_ok=True) |
| 630 | for b in NEW_BLOCKS: |
| 631 | if b['id'] in seen: |
| 632 | raise SystemExit(f'duplicate new id {b["id"]}') |
| 633 | seen.add(b['id']) |
| 634 | image = f"images/block-{b['id']}.png" |
| 635 | path = ROOT / 'build' / image |
| 636 | img = render_block(b['hex'], b['shape']) |
| 637 | img.save(path, 'PNG', optimize=True) |
| 638 | new.append({ |
| 639 | 'id': b['id'], |
| 640 | 'name': b['name'], |
| 641 | 'hex': b['hex'], |
| 642 | 'shape': b['shape'], |
| 643 | 'image': image, |
| 644 | 'terra': False, |
| 645 | }) |
| 646 | print(f' wrote {path.relative_to(ROOT)} ({path.stat().st_size} bytes)') |
| 647 | |
| 648 | registry = { |
| 649 | '_comment': ( |
| 650 | 'Append-only block registry. Never reorder, rename ids, or insert ' |
| 651 | 'in the middle. New blocks go at the end. The first five (Terra) ' |
| 652 | 'are unlocked by default; index 5 requires 500 lifetime points, ' |
| 653 | 'each next index requires 14.5% more (rounded).' |
| 654 | ), |
| 655 | 'terraCount': 5, |
| 656 | 'firstThreshold': 500, |
| 657 | 'thresholdGrowth': 1.145, |
| 658 | 'blocks': terra + new + others, |
| 659 | } |
| 660 | OUT_JSON.write_text(json.dumps(registry, indent=2, ensure_ascii=False) + '\n') |
| 661 | print(f'wrote {OUT_JSON.relative_to(ROOT)}: {len(terra)} terra + {len(new)} new + {len(others)} official = {len(registry["blocks"])} total') |
| 662 | |
| 663 | |
| 664 | if __name__ == '__main__': |
| 665 | main() |
projects-timfalken-com meteors