Files
generate_pack.py
README.md
Code
2 582
generate_pack.py
#!/usr/bin/env python3
import argparse
import hashlib
import json
import math
import os
import random
import shutil
import struct
import zipfile
import zlib
from pathlib import Path
from typing import List, Sequence, Tuple
Pixel = Tuple[int, int, int, int]
BLACK = (7, 8, 11, 255)
DARK_GREY = (27, 30, 36, 255)
GREY = (72, 77, 86, 255)
LIGHT_GREY = (132, 138, 147, 255)
DARK_RED = (76, 7, 13, 255)
RED = (164, 15, 25, 255)
BRIGHT_RED = (224, 32, 35, 255)
DARK_FIRE = (176, 38, 12, 255)
FIRE = (246, 91, 14, 255)
BRIGHT_FIRE = (255, 184, 37, 255)
PALETTE = (
DARK_GREY,
GREY,
LIGHT_GREY,
DARK_RED,
RED,
BRIGHT_RED,
DARK_FIRE,
FIRE,
BRIGHT_FIRE,
)
def png_chunk(chunk_type: bytes, data: bytes) -> bytes:
checksum = zlib.crc32(chunk_type + data) & 0xFFFFFFFF
return (
struct.pack(">I", len(data))
+ chunk_type
+ data
+ struct.pack(">I", checksum)
)
def encode_png(pixels: Sequence[Sequence[Pixel]]) -> bytes:
height = len(pixels)
width = len(pixels[0])
raw = bytearray()
for row in pixels:
raw.append(0)
for red, green, blue, alpha in row:
raw.extend((red, green, blue, alpha))
header = struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0)
return (
b"\x89PNG\r\n\x1a\n"
+ png_chunk(b"IHDR", header)
+ png_chunk(b"IDAT", zlib.compress(bytes(raw), 9))
+ png_chunk(b"IEND", b"")
)
def rng_for(texture_name: str) -> random.Random:
digest = hashlib.sha256(texture_name.encode("utf-8")).digest()
return random.Random(int.from_bytes(digest[:8], "big"))
def set_pixel(
pixels: List[List[Pixel]],
x: int,
y: int,
color: Pixel,
) -> None:
height = len(pixels)
width = len(pixels[0])
if 0 <= x < width and 0 <= y < height:
pixels[y][x] = color
def draw_line(
pixels: List[List[Pixel]],
start_x: int,
start_y: int,
delta_x: int,
delta_y: int,
length: int,
colors: Sequence[Pixel],
rng: random.Random,
) -> None:
x = start_x
y = start_y
for _ in range(length):
set_pixel(pixels, x, y, rng.choice(colors))
if rng.random() < 0.35:
set_pixel(pixels, x + 1, y, rng.choice(colors))
if rng.random() < 0.25:
set_pixel(pixels, x, y + 1, rng.choice(colors))
x += delta_x
y += delta_y
def block_texture(texture_name: str, size: int) -> List[List[Pixel]]:
rng = rng_for(texture_name)
pixels: List[List[Pixel]] = []
for y in range(size):
row: List[Pixel] = []
for x in range(size):
edge = x in (0, size - 1) or y in (0, size - 1)
chance = rng.random()
if edge:
color = BLACK
elif chance < 0.38:
color = DARK_GREY
elif chance < 0.63:
color = GREY
elif chance < 0.72:
color = LIGHT_GREY
elif chance < 0.82:
color = DARK_RED
elif chance < 0.91:
color = RED
elif chance < 0.97:
color = DARK_FIRE
else:
color = FIRE
if y < size * 0.4 and rng.random() < 0.055:
color = BRIGHT_FIRE
row.append(color)
pixels.append(row)
crack_count = max(3, size // 5)
for _ in range(crack_count):
start_x = rng.randrange(1, max(2, size - 1))
start_y = rng.randrange(1, max(2, size - 1))
delta_x, delta_y = rng.choice(
((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, 1))
)
length = rng.randrange(max(2, size // 4), max(3, size // 2))
colors = (DARK_RED, RED, BRIGHT_RED, FIRE)
draw_line(
pixels,
start_x,
start_y,
delta_x,
delta_y,
length,
colors,
rng,
)
return pixels
def item_mask(texture_name: str, x: float, y: float) -> bool:
lower_name = texture_name.lower()
if any(
word in lower_name
for word in ("sword", "axe", "pickaxe", "shovel", "hoe", "trident")
):
diagonal = abs(y - (0.96 - x * 0.92))
handle = 0.66 < y < 0.94 and abs(x - 0.72) < 0.08
return diagonal < 0.10 or handle
if any(
word in lower_name
for word in ("helmet", "chestplate", "leggings", "boots")
):
if "helmet" in lower_name:
return 0.18 < x < 0.82 and 0.18 < y < 0.65
if "chestplate" in lower_name:
return 0.20 < x < 0.80 and 0.18 < y < 0.82
if "leggings" in lower_name:
return 0.25 < x < 0.75 and 0.18 < y < 0.86
return 0.26 < x < 0.74 and 0.45 < y < 0.88
if any(word in lower_name for word in ("bucket", "bottle", "potion")):
return 0.28 < x < 0.72 and 0.16 < y < 0.86
distance = abs(x - 0.5) + abs(y - 0.5)