Files
generate_spawn.py
README.md
Code
2 419
generate_spawn.py
#!/usr/bin/env python3
"""Generates a detailed vanilla ice spawn as a Litematica .litematic file."""
from array import array
from math import ceil, cos, log2, pi, sin
from pathlib import Path
from struct import pack
from time import time
import argparse
SIZE_X = 200
SIZE_Y = 80
SIZE_Z = 200
ORIGIN_Y = 0
TAG_END = 0
TAG_BYTE = 1
TAG_SHORT = 2
TAG_INT = 3
TAG_LONG = 4
TAG_FLOAT = 5
TAG_DOUBLE = 6
TAG_BYTE_ARRAY = 7
TAG_STRING = 8
TAG_LIST = 9
TAG_COMPOUND = 10
TAG_INT_ARRAY = 11
TAG_LONG_ARRAY = 12
def nbt_string(value):
encoded = value.encode("utf-8")
return pack(">H", len(encoded)) + encoded
def nbt_named(tag_type, name, payload):
return bytes((tag_type,)) + nbt_string(name) + payload
def nbt_string_payload(value):
return nbt_string(value)
def nbt_int_payload(value):
return pack(">i", value)
def nbt_long_payload(value):
return pack(">q", value)
def nbt_int_array_payload(values):
return pack(">i", len(values)) + b"".join(pack(">i", value) for value in values)
def nbt_long_array_payload(values):
return pack(">i", len(values)) + b"".join(pack(">q", value) for value in values)
def nbt_list_payload(element_type, payloads):
return bytes((element_type,)) + pack(">i", len(payloads)) + b"".join(payloads)
def nbt_compound_payload(entries):
data = bytearray()
for tag_type, name, payload in entries:
data.extend(nbt_named(tag_type, name, payload))
data.append(TAG_END)
return bytes(data)
class Spawn:
def __init__(self):
self.palette = ["minecraft:air"]
self.palette_ids = {"minecraft:air": 0}
self.volume = SIZE_X * SIZE_Y * SIZE_Z
self.blocks = array("H", [0]) * self.volume
def index(self, x, y, z):
return x + SIZE_X * (z + SIZE_Z * y)
def block_id(self, block):
if block not in self.palette_ids:
self.palette_ids[block] = len(self.palette)
self.palette.append(block)
return self.palette_ids[block]
def set_block(self, x, y, z, block):
if 0 <= x < SIZE_X and 0 <= y < SIZE_Y and 0 <= z < SIZE_Z:
self.blocks[self.index(x, y, z)] = self.block_id(block)
def fill_box(self, x1, y1, z1, x2, y2, z2, block):
for y in range(max(0, y1), min(SIZE_Y - 1, y2) + 1):
for z in range(max(0, z1), min(SIZE_Z - 1, z2) + 1):
for x in range(max(0, x1), min(SIZE_X - 1, x2) + 1):
self.set_block(x, y, z, block)
def disc(self, cx, cz, radius, y, block):
radius_squared = radius * radius
for z in range(cz - radius, cz + radius + 1):
for x in range(cx - radius, cx + radius + 1):
if (x - cx) * (x - cx) + (z - cz) * (z - cz) <= radius_squared:
self.set_block(x, y, z, block)
def ring(self, cx, cz, outer, inner, y, block):
outer_squared = outer * outer
inner_squared = inner * inner
for z in range(cz - outer, cz + outer + 1):
for x in range(cx - outer, cx + outer + 1):
distance = (x - cx) * (x - cx) + (z - cz) * (z - cz)
if inner_squared < distance <= outer_squared:
self.set_block(x, y, z, block)
def wall_box(self, x1, y1, z1, x2, y2, z2, block):
for y in range(y1, y2 + 1):
for x in range(x1, x2 + 1):
self.set_block(x, y, z1, block)
self.set_block(x, y, z2, block)
for z in range(z1 + 1, z2):
self.set_block(x1, y, z, block)
self.set_block(x2, y, z, block)
def line(self, x1, z1, x2, z2, y, width, block):
steps = max(abs(x2 - x1), abs(z2 - z1))
for step in range(steps + 1):
progress = step / steps if steps else 0
x = round(x1 + (x2 - x1) * progress)
z = round(z1 + (z2 - z1) * progress)
self.disc(x, z, width, y, block)
def spike(self, cx, cz, height, base_radius=4):
for level in range(height):
radius = max(1, int(base_radius * (height - level) / height))
self.disc(cx, cz, radius, 3 + level, "minecraft:blue_ice")
self.set_block(cx, 3 + height, cz, "minecraft:packed_ice")
if height > 9:
self.set_block(cx, 4 + height, cz, "minecraft:end_rod")
def lamp(self, x, z):
self.set_block(x, 3, z, "minecraft:quartz_block")
self.set_block(x, 4, z, "minecraft:sea_lantern")
self.set_block(x, 5, z, "minecraft:iron_bars")
self.set_block(x, 6, z, "minecraft:end_rod")
def build_island(self):
self.disc(100, 100, 97, 0, "minecraft:packed_ice")
self.disc(100, 100, 94, 1, "minecraft:snow_block")
self.ring(100, 100, 96, 93, 2, "minecraft:blue_ice")
self.ring(100, 100, 90, 87, 2, "minecraft:ice")
# Radial streets and diagonal shortcuts.
self.fill_box(96, 2, 3, 104, 2, 96, "minecraft:packed_ice")
self.fill_box(96, 2, 104, 104, 2, 197, "minecraft:packed_ice")
self.fill_box(3, 2, 96, 96, 2, 104, "minecraft:packed_ice")
self.fill_box(104, 2, 96, 197, 2, 104, "minecraft:packed_ice")
self.line(31, 31, 88, 88, 2, 3, "minecraft:ice")
self.line(169, 31, 112, 88, 2, 3, "minecraft:ice")
self.line(31, 169, 88, 112, 2, 3, "minecraft:ice")
self.line(169, 169, 112, 112, 2, 3, "minecraft:ice")
for x, z in ((18, 18), (182, 18), (18, 182), (182, 182),
(100, 14), (100, 186), (14, 100), (186, 100)):
self.spike(x, z, 12 if x in (100, 14, 186) or z in (100, 14, 186) else 8, 4)
def build_central_square(self):
self.disc(100, 100, 30, 2, "minecraft:snow_block")
self.ring(100, 100, 30, 27, 3, "minecraft:blue_ice")
self.disc(100, 100, 24, 3, "minecraft:quartz_block")
self.ring(100, 100, 24, 21, 4, "minecraft:light_blue_concrete")
# Eight-point snowflake mosaic.
for angle in range(0, 360, 45):
radians = angle * pi / 180
x = round(100 + cos(radians) * 21)
z = round(100 + sin(radians) * 21)
self.line(100, 100, x, z, 4, 1, "minecraft:white_concrete")
branch_x = round(100 + cos(radians + pi / 4) * 12)
branch_z = round(100 + sin(radians + pi / 4) * 12)
self.line(round(100 + cos(radians) * 10), round(100 + sin(radians) * 10),
branch_x, branch_z, 4, 1, "minecraft:white_concrete")
self.disc(100, 100, 8, 4, "minecraft:blue_ice")
self.disc(100, 100, 5, 5, "minecraft:gold_block")
self.set_block(100, 6, 100, "minecraft:beacon")
for x, z in ((96, 100), (104, 100), (100, 96), (100, 104)):
self.set_block(x, 5, z, "minecraft:sea_lantern")
def build_palace(self):
# Main hall and side wings.
self.fill_box(78, 3, 82, 122, 3, 118, "minecraft:quartz_block")
self.wall_box(78, 4, 82, 122, 17, 118, "minecraft:packed_ice")
self.wall_box(82, 4, 86, 118, 15, 114, "minecraft:snow_block")
self.fill_box(84, 4, 88, 116, 4, 112, "minecraft:smooth_quartz")
self.fill_box(92, 5, 82, 108, 10, 82, "minecraft:air")
self.fill_box(96, 5, 82, 104, 10, 82, "minecraft:blue_ice")
for x in (83, 89, 111, 117):
self.fill_box(x, 4, 82, x + 1, 14, 83, "minecraft:quartz_pillar")
self.fill_box(x, 4, 117, x + 1, 14, 118, "minecraft:quartz_pillar")