Files
generate_plant_stem_dungeon.py
Code
1 248
generate_plant_stem_dungeon.py
#!/usr/bin/env python3
"""Generate a Sponge/WorldEdit .schem plant-stem dungeon without dependencies."""
import argparse
import gzip
import math
import struct
from pathlib import Path
TAG_END = 0
TAG_SHORT = 2
TAG_INT = 3
TAG_LONG = 4
TAG_STRING = 8
TAG_LIST = 9
TAG_COMPOUND = 10
TAG_BYTE_ARRAY = 7
TAG_INT_ARRAY = 11
def encoded_string(value):
data = value.encode("utf-8")
return struct.pack(">H", len(data)) + data
def named_tag(tag_type, name, payload):
return bytes((tag_type,)) + encoded_string(name) + payload
def short_tag(name, value):
return named_tag(TAG_SHORT, name, struct.pack(">h", value))
def int_tag(name, value):
return named_tag(TAG_INT, name, struct.pack(">i", value))
def long_tag(name, value):
return named_tag(TAG_LONG, name, struct.pack(">q", value))
def string_tag(name, value):
return named_tag(TAG_STRING, name, encoded_string(value))
def byte_array_tag(name, value):
return named_tag(TAG_BYTE_ARRAY, name, struct.pack(">i", len(value)) + value)
def int_array_tag(name, value):
payload = struct.pack(">i", len(value))
payload += b"".join(struct.pack(">i", entry) for entry in value)
return named_tag(TAG_INT_ARRAY, name, payload)
def compound_payload(tags):
return b"".join(tags) + bytes((TAG_END,))
def compound_tag(name, tags):
return named_tag(TAG_COMPOUND, name, compound_payload(tags))
def compound_list_tag(name, compounds):
payload = bytes((TAG_COMPOUND,)) + struct.pack(">i", len(compounds))
payload += b"".join(compound_payload(compound) for compound in compounds)
return named_tag(TAG_LIST, name, payload)
def block_state(material, **properties):
if not properties:
return material
values = ",".join(f"{key}={value}" for key, value in properties.items())
return f"{material}[{values}]"
def cardinal_facing(angle):
x = math.cos(angle)
z = math.sin(angle)
if abs(x) >= abs(z):
return "east" if x >= 0 else "west"
return "south" if z >= 0 else "north"
class SchematicBuilder:
def __init__(self, width, height, length):
self.width = width
self.height = height
self.length = length
self.palette = {"minecraft:air": 0}
self.blocks = [0] * (width * height * length)
self.block_entities = []
def palette_id(self, state):
if state not in self.palette:
self.palette[state] = len(self.palette)
return self.palette[state]
def index(self, x, y, z):
return x + z * self.width + y * self.width * self.length
def inside(self, x, y, z):
return 0 <= x < self.width and 0 <= y < self.height and 0 <= z < self.length
def set_block(self, x, y, z, state):
if self.inside(x, y, z):
self.blocks[self.index(x, y, z)] = self.palette_id(state)
def add_spawner(self, x, y, z, mob):
self.set_block(x, y, z, "minecraft:spawner")
spawn_entity = compound_tag("entity", [string_tag("id", mob)])
spawn_data = compound_tag("SpawnData", [spawn_entity])
potential_data = compound_tag("data", [compound_tag("entity", [string_tag("id", mob)])])
spawn_potential = [potential_data, int_tag("weight", 1)]
self.block_entities.append([
string_tag("Id", "minecraft:spawner"),
int_array_tag("Pos", [x, y, z]),
short_tag("Delay", 20),
short_tag("MinSpawnDelay", 200),
short_tag("MaxSpawnDelay", 800),
short_tag("SpawnCount", 4),
short_tag("MaxNearbyEntities", 6),
short_tag("RequiredPlayerRange", 16),
short_tag("SpawnRange", 4),
spawn_data,
compound_list_tag("SpawnPotentials", [spawn_potential]),
])
def add_loot_chest(self, x, y, z, loot_table, seed):
self.set_block(x, y, z, block_state("minecraft:chest", facing="south", type="single"))
self.block_entities.append([
string_tag("Id", "minecraft:chest"),
int_array_tag("Pos", [x, y, z]),
string_tag("LootTable", loot_table),
long_tag("LootTableSeed", seed),
])
def block_data(self):
encoded = bytearray()
for value in self.blocks:
while True:
current = value & 0x7F
value >>= 7
encoded.append(current | (0x80 if value else 0))
if not value:
break
return bytes(encoded)
def write(self, output):
palette_tags = [int_tag(state, identifier) for state, identifier in self.palette.items()]
root_tags = [
int_tag("Version", 2),
int_tag("DataVersion", 3465),
short_tag("Width", self.width),
short_tag("Height", self.height),
short_tag("Length", self.length),
int_array_tag("Offset", [0, 0, 0]),
int_tag("PaletteMax", len(self.palette)),
compound_tag("Palette", palette_tags),
byte_array_tag("BlockData", self.block_data()),
compound_list_tag("BlockEntities", self.block_entities),
compound_list_tag("Entities", []),
compound_tag("Metadata", []),
]
nbt = bytes((TAG_COMPOUND,)) + encoded_string("") + compound_payload(root_tags)
with gzip.open(output, "wb", compresslevel=9) as schematic_file:
schematic_file.write(nbt)
def add_platform(builder, center_x, center_z, y, material, radius=3):
for x in range(center_x - radius, center_x + radius + 1):
for z in range(center_z - radius, center_z + radius + 1):
if (x - center_x) ** 2 + (z - center_z) ** 2 <= radius ** 2:
builder.set_block(x, y, z, material)
def build_dungeon(arguments):
diameter = arguments.diameter
height = arguments.height
center = (diameter - 1) / 2.0
outer_radius = diameter / 2.0 - 0.25
inner_radius = outer_radius - 4.5
builder = SchematicBuilder(diameter, height, diameter)
for y in range(height):
for x in range(diameter):
for z in range(diameter):
distance = math.hypot(x - center, z - center)
if distance > outer_radius:
continue
if y == 0 or y == height - 1:
builder.set_block(x, y, z, arguments.floor_material)
elif distance >= inner_radius:
builder.set_block(x, y, z, arguments.wall_material)
stair_top = height - 13
stair_radius = inner_radius - 1.5
for y in range(1, stair_top + 1):
angle = -math.pi / 2 + y * (math.tau / 16.0)