4b0388a0e1
- Allow 2 chunks to be mid-load concurrently instead of 1 (MAP_CHUNK_LOAD_CONCURRENCY). - Fix entitySetChunk silently losing track of an entity when its target chunk's entity slots are full - it now stays detached (and retries later) instead of claiming a chunk that never actually registered it. - DCF format bumped to v5: chunks can now declare entity spawns (global/NPC via the existing entityglobal registry, or one-shot item pickups) and map area triggers, resolved via a new callback-ID registry (mapareagloballist.h) mirroring the entity one. rpg.c's hardcoded TEST entity/item/area spawns are gone - chunk_0_0_0.json now carries that data instead. The player is still bootstrapped in code since it isn't map-authored content.
578 lines
21 KiB
Python
578 lines
21 KiB
Python
# Copyright (c) 2026 Dominic Masters
|
|
#
|
|
# This software is released under the MIT License.
|
|
# https://opensource.org/licenses/MIT
|
|
|
|
"""
|
|
Generates DCF + companion DMF files from raw chunk JSON files, or upgrades
|
|
legacy DCF files (version 1 or 2) to the current version.
|
|
|
|
JSON input (assetsraw/chunks/chunk_X_Y_Z.json):
|
|
{
|
|
"tiles": [
|
|
{ "pos": [x, y, z], "type": <tile_shape_int>, "tile": <uv_tile_int> }
|
|
],
|
|
"meshes": [
|
|
{ "file": "house_5_3.dmf", "pos": [x, y, z] }
|
|
],
|
|
"entities": [
|
|
{ "type": "global", "globalId": <int>, "pos": [x, y, z] },
|
|
{ "type": "item", "itemId": <int>, "quantity": <int>, "pos": [x, y, z] }
|
|
],
|
|
"areas": [
|
|
{
|
|
"min": [x, y, z], "max": [x, y, z],
|
|
"callbackId": <int>, "notify": <int>, "trigger": <int>
|
|
}
|
|
]
|
|
}
|
|
|
|
Tiles absent from the array default to TILE_SHAPE_NULL. Each (x, y)
|
|
column may only have ONE real tile - chunks store one tile per column,
|
|
not one per (x, y, z), and that tile records its own local z (see the
|
|
version 4 tile format below). If two entries share the same (x, y) with
|
|
different z, the later one in the array wins and a warning is printed.
|
|
Mesh files are located by searching under assets/meshes/ and referenced by
|
|
path from the assets root in the DCF.
|
|
|
|
"entities" spawns things into the world when this chunk loads. A "global"
|
|
entity is spawned via mapSpawnEntity() - globalId indexes
|
|
ENTITY_GLOBAL_LIST (src/dusk/rpg/entity/global/entitygloballist.h) and is
|
|
deduped automatically if already spawned, so it's safe to declare on a
|
|
chunk that streams in more than once. An "item" entity has no persistent
|
|
identity - it respawns fresh every time this chunk (re)loads, including
|
|
after being picked up, since nothing tracks "already collected" yet.
|
|
itemId is a raw ITEM_ID_* value (see src/dusk/rpg/item/item.json for the
|
|
name -> id mapping, same convention as the tile "type" ints above).
|
|
|
|
"areas" declares map trigger regions (see rpg/overworld/maparea.h) owned
|
|
by this chunk - they're removed when the chunk unloads and re-added if it
|
|
streams back in. callbackId indexes MAP_AREA_CALLBACK_LIST
|
|
(src/dusk/rpg/overworld/global/mapareagloballist.h; 0 is reserved and
|
|
invalid). notify is bitwise MAP_AREA_NOTIFY_PLAYER(1)|NOTIFY_NPC(2).
|
|
trigger is bitwise MAP_TRIGGER_STEP(1)|ENTER(2)|EXIT(4).
|
|
|
|
Output DCF is derived automatically:
|
|
assetsraw/chunks/chunk_X_Y_Z.json -> assets/chunks/X_Y_Z.dcf
|
|
|
|
Version 5 DCF format (after 8-byte header):
|
|
tile_t tiles[CHUNK_WIDTH * CHUNK_HEIGHT] (one per x/y column)
|
|
each tile: uint32_t shape, uint8_t z, 3 padding bytes (8 bytes total,
|
|
matching the C tile_t struct's layout: { tileshape_t shape; uint8_t z; })
|
|
uint8_t meshCount
|
|
for each model:
|
|
null-terminated string (relative asset path to .json model)
|
|
float32[3] (x, y, z offset)
|
|
uint8_t entitySpawnCount
|
|
for each entity spawn:
|
|
uint8_t kind (0 = global entity, 1 = item entity)
|
|
uint16_t a (globalId if kind 0, itemId if kind 1)
|
|
uint8_t b (unused if kind 0, quantity if kind 1)
|
|
int16_t x, y, z (world position, 3 fields)
|
|
uint8_t areaSpawnCount
|
|
for each area spawn:
|
|
int16_t minX, minY, minZ (3 fields)
|
|
int16_t maxX, maxY, maxZ (3 fields)
|
|
uint16_t callbackId
|
|
uint8_t notify
|
|
uint8_t trigger
|
|
|
|
DMF format:
|
|
Bytes 0-3: DMF\\x00
|
|
Bytes 4-7: uint32_t version = 1 (little-endian)
|
|
Bytes 8-11: uint32_t vertCount (little-endian)
|
|
Bytes 12+: meshvertex_t vertices[vertCount]
|
|
Each vertex: uv[2] + pos[3] = 5 x float32 LE = 20 bytes
|
|
|
|
Usage:
|
|
python3 -m tools.asset.chunk # process all assetsraw/chunks/*.json
|
|
python3 -m tools.asset.chunk <file.json> # process one JSON file
|
|
python3 -m tools.asset.chunk <file.dcf> # upgrade legacy (v1/v2) DCF in-place
|
|
"""
|
|
|
|
import json
|
|
import math
|
|
import os
|
|
import struct
|
|
import sys
|
|
|
|
_HERE = os.path.dirname(os.path.abspath(__file__))
|
|
PROJECT_ROOT = os.path.normpath(os.path.join(_HERE, '..', '..', '..'))
|
|
ASSETSRAW_DIR = os.path.join(PROJECT_ROOT, 'assetsraw')
|
|
ASSETS_DIR = os.path.join(PROJECT_ROOT, 'assets')
|
|
|
|
CHUNK_WIDTH = 16
|
|
CHUNK_HEIGHT = 16
|
|
CHUNK_DEPTH = 8
|
|
# One tile per (x, y) column - the column's local Z (0..CHUNK_DEPTH-1) is
|
|
# carried on the tile itself instead of storing a tile per (x, y, z).
|
|
CHUNK_TILE_COUNT = CHUNK_WIDTH * CHUNK_HEIGHT # 256
|
|
|
|
# World-space Z distance of one Z-layer/story, in world-float space.
|
|
# Must match WORLD_LAYER_HEIGHT in src/dusk/rpg/overworld/worldpos.h.
|
|
WORLD_LAYER_HEIGHT = 1.0 / math.sqrt(2)
|
|
|
|
CHUNK_MESH_COUNT_MAX = 10
|
|
CHUNK_MESH_NAME_MAX = 64
|
|
CHUNK_ENTITY_SPAWN_COUNT_MAX = 8
|
|
CHUNK_AREA_COUNT_MAX = 4
|
|
|
|
ENTITY_SPAWN_KIND_GLOBAL = 0
|
|
ENTITY_SPAWN_KIND_ITEM = 1
|
|
|
|
# Matches sizeof(tile_t) on the C side: uint32_t shape + uint8_t z, padded
|
|
# to 8 bytes ({ tileshape_t shape; uint8_t z; } with 4-byte enum alignment).
|
|
TILE_STRUCT_FORMAT = '<IB3x'
|
|
TILE_SIZE = struct.calcsize(TILE_STRUCT_FORMAT)
|
|
VERTEX_SIZE = 20
|
|
|
|
# Legacy (v1/v2) DCFs stored one uint32 shape per (x, y, z) triple across
|
|
# the full 3D grid, at whatever CHUNK_DEPTH was in effect when they were
|
|
# written (4). Frozen here, independent of the live CHUNK_DEPTH above, so
|
|
# a future depth change can't corrupt parsing of genuinely old files.
|
|
_LEGACY_CHUNK_DEPTH = 4
|
|
_LEGACY_TILE_SIZE = 4
|
|
_LEGACY_CHUNK_TILE_COUNT = CHUNK_WIDTH * CHUNK_HEIGHT * _LEGACY_CHUNK_DEPTH
|
|
|
|
TILE_SHAPE_NULL = 0
|
|
TILE_SHAPE_GROUND = 1
|
|
TILE_SHAPE_RAMP_NORTH = 2
|
|
TILE_SHAPE_RAMP_EAST = 3
|
|
TILE_SHAPE_RAMP_SOUTH = 4
|
|
TILE_SHAPE_RAMP_WEST = 5
|
|
TILE_SHAPE_RAMP_NORTHEAST = 6
|
|
TILE_SHAPE_RAMP_NORTHWEST = 7
|
|
TILE_SHAPE_RAMP_SOUTHEAST = 8
|
|
TILE_SHAPE_RAMP_SOUTHWEST = 9
|
|
TILE_SHAPE_RAMP_NORTHEAST_INNER = 10
|
|
TILE_SHAPE_RAMP_NORTHWEST_INNER = 11
|
|
TILE_SHAPE_RAMP_SOUTHEAST_INNER = 12
|
|
TILE_SHAPE_RAMP_SOUTHWEST_INNER = 13
|
|
|
|
FILE_MAGIC = b'DCF'
|
|
DMF_MAGIC = b'DMF\x00'
|
|
VERSION_OUT = 5
|
|
DMF_VERSION = 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def tile_index(x, y):
|
|
return x + y * CHUNK_WIDTH
|
|
|
|
|
|
def find_model(filename):
|
|
"""Search ASSETS_DIR/models/ recursively for filename.
|
|
Returns the path relative to ASSETS_DIR (forward slashes), or None."""
|
|
models_root = os.path.join(ASSETS_DIR, 'models')
|
|
for dirpath, _, filenames in os.walk(models_root):
|
|
if filename in filenames:
|
|
rel = os.path.relpath(
|
|
os.path.join(dirpath, filename), ASSETS_DIR
|
|
)
|
|
return rel.replace('\\', '/')
|
|
return None
|
|
|
|
|
|
def write_model_json(path, mesh_rel, texture_rel, color):
|
|
"""Write a model JSON descriptor file."""
|
|
obj = {'mesh': mesh_rel, 'color': color}
|
|
if texture_rel:
|
|
obj['texture'] = texture_rel
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, 'w') as f:
|
|
json.dump(obj, f, indent=2)
|
|
print(f' Wrote model JSON {path}')
|
|
|
|
|
|
def derive_dcf_path(json_path):
|
|
"""assetsraw/chunks/chunk_X_Y_Z.json -> assets/chunks/X_Y_Z.dcf"""
|
|
base = os.path.splitext(os.path.basename(json_path))[0]
|
|
if base.startswith('chunk_'):
|
|
base = base[len('chunk_'):]
|
|
rel_dir = os.path.relpath(os.path.dirname(os.path.abspath(json_path)), ASSETSRAW_DIR)
|
|
return os.path.join(ASSETS_DIR, rel_dir, base + '.dcf')
|
|
|
|
|
|
def write_dmf(path, vertex_bytes):
|
|
vert_count = len(vertex_bytes) // VERTEX_SIZE
|
|
buf = bytearray()
|
|
buf += DMF_MAGIC
|
|
buf += struct.pack('<I', DMF_VERSION)
|
|
buf += struct.pack('<I', vert_count)
|
|
buf += vertex_bytes
|
|
with open(path, 'wb') as f:
|
|
f.write(buf)
|
|
print(f' Wrote DMF {path}: {vert_count} vertices, {len(buf)} bytes')
|
|
|
|
|
|
def write_dcf(
|
|
dcf_path, tiles, mesh_names, mesh_offsets=None,
|
|
entity_spawns=None, area_spawns=None
|
|
):
|
|
"""Write a current-version DCF referencing the given DMF asset paths."""
|
|
mesh_count = len(mesh_names)
|
|
if mesh_offsets is None:
|
|
mesh_offsets = [(0.0, 0.0, 0.0)] * mesh_count
|
|
if entity_spawns is None:
|
|
entity_spawns = []
|
|
if area_spawns is None:
|
|
area_spawns = []
|
|
|
|
if len(entity_spawns) > CHUNK_ENTITY_SPAWN_COUNT_MAX:
|
|
raise ValueError(
|
|
f"Too many entity spawns ({len(entity_spawns)}) - max "
|
|
f"{CHUNK_ENTITY_SPAWN_COUNT_MAX}"
|
|
)
|
|
if len(area_spawns) > CHUNK_AREA_COUNT_MAX:
|
|
raise ValueError(
|
|
f"Too many area spawns ({len(area_spawns)}) - max "
|
|
f"{CHUNK_AREA_COUNT_MAX}"
|
|
)
|
|
|
|
buf = bytearray()
|
|
buf += FILE_MAGIC
|
|
buf += b'\x00'
|
|
buf += struct.pack('<I', VERSION_OUT)
|
|
buf += tiles
|
|
buf += struct.pack('<B', mesh_count)
|
|
for name, offset in zip(mesh_names, mesh_offsets):
|
|
encoded = name.encode('ascii')
|
|
if len(encoded) >= CHUNK_MESH_NAME_MAX:
|
|
raise ValueError(
|
|
f"Mesh name too long (>= {CHUNK_MESH_NAME_MAX}): {name}"
|
|
)
|
|
buf += encoded + b'\x00'
|
|
buf += struct.pack('<3f', offset[0], offset[1], offset[2])
|
|
|
|
buf += struct.pack('<B', len(entity_spawns))
|
|
for spawn in entity_spawns:
|
|
kind = spawn['kind']
|
|
x, y, z = spawn['pos']
|
|
if kind == ENTITY_SPAWN_KIND_GLOBAL:
|
|
a, b = spawn['globalId'], 0
|
|
else:
|
|
a, b = spawn['itemId'], spawn['quantity']
|
|
buf += struct.pack('<BHB3h', kind, a, b, x, y, z)
|
|
|
|
buf += struct.pack('<B', len(area_spawns))
|
|
for area in area_spawns:
|
|
minX, minY, minZ = area['min']
|
|
maxX, maxY, maxZ = area['max']
|
|
buf += struct.pack(
|
|
'<6hHBB',
|
|
minX, minY, minZ, maxX, maxY, maxZ,
|
|
area['callbackId'], area['notify'], area['trigger']
|
|
)
|
|
|
|
with open(dcf_path, 'wb') as f:
|
|
f.write(buf)
|
|
print(
|
|
f' Wrote DCF {dcf_path}: '
|
|
f'version {VERSION_OUT}, {mesh_count} mesh(es), '
|
|
f'{len(entity_spawns)} entity spawn(s), {len(area_spawns)} '
|
|
f'area(s), {len(buf)} bytes'
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# JSON -> DCF + DMF
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _tile_quad(u0, u1, v0, v1, fx, fy, sw_z, se_z, ne_z, nw_z):
|
|
"""Six vertices (2 CCW triangles) for a quad with per-corner Z heights.
|
|
Corner order: SW=(fx,fy), SE=(fx+1,fy), NE=(fx+1,fy+1), NW=(fx,fy+1).
|
|
NORTH=+Y, EAST=+X (matches entityDirGetRelative)."""
|
|
verts = [
|
|
(u0, v0, fx, fy, sw_z),
|
|
(u1, v0, fx+1, fy, se_z),
|
|
(u1, v1, fx+1, fy+1, ne_z),
|
|
(u0, v0, fx, fy, sw_z),
|
|
(u1, v1, fx+1, fy+1, ne_z),
|
|
(u0, v1, fx, fy+1, nw_z),
|
|
]
|
|
buf = bytearray()
|
|
for v in verts:
|
|
buf += struct.pack('<5f', *v)
|
|
return bytes(buf)
|
|
|
|
|
|
# Per-shape corner heights as (sw, se, ne, nw) offsets from tile base Z.
|
|
# NORTH=+Y, EAST=+X. Cardinal ramps: low face at 0, high face at 1.
|
|
# Diagonal outer-corner ramps: only the named corner raised, rest at 0.
|
|
# Diagonal inner-corner ramps: only the corner opposite the named one is
|
|
# lowered to 0, the rest (including the named corner) stay raised at 1.
|
|
_RAMP_CORNERS = {
|
|
TILE_SHAPE_GROUND: (0.0, 0.0, 0.0, 0.0),
|
|
TILE_SHAPE_RAMP_NORTH: (0.0, 0.0, 1.0, 1.0), # south=low, north=high
|
|
TILE_SHAPE_RAMP_SOUTH: (1.0, 1.0, 0.0, 0.0), # south=high, north=low
|
|
TILE_SHAPE_RAMP_EAST: (0.0, 1.0, 1.0, 0.0), # west=low, east=high
|
|
TILE_SHAPE_RAMP_WEST: (1.0, 0.0, 0.0, 1.0), # west=high, east=low
|
|
TILE_SHAPE_RAMP_NORTHEAST: (0.0, 0.0, 1.0, 0.0), # only NE raised
|
|
TILE_SHAPE_RAMP_NORTHWEST: (0.0, 0.0, 0.0, 1.0), # only NW raised
|
|
TILE_SHAPE_RAMP_SOUTHEAST: (0.0, 1.0, 0.0, 0.0), # only SE raised
|
|
TILE_SHAPE_RAMP_SOUTHWEST: (1.0, 0.0, 0.0, 0.0), # only SW raised
|
|
TILE_SHAPE_RAMP_NORTHEAST_INNER: (0.0, 1.0, 1.0, 1.0), # only SW low
|
|
TILE_SHAPE_RAMP_NORTHWEST_INNER: (1.0, 0.0, 1.0, 1.0), # only SE low
|
|
TILE_SHAPE_RAMP_SOUTHEAST_INNER: (1.0, 1.0, 1.0, 0.0), # only NW low
|
|
TILE_SHAPE_RAMP_SOUTHWEST_INNER: (1.0, 1.0, 0.0, 1.0), # only NE low
|
|
}
|
|
|
|
|
|
def build_terrain_verts(tiles_bytes):
|
|
"""Generate quads for every non-null tile, shaped to match tile type."""
|
|
buf = bytearray()
|
|
for y in range(CHUNK_HEIGHT):
|
|
for x in range(CHUNK_WIDTH):
|
|
idx = tile_index(x, y)
|
|
tile_type, z = struct.unpack_from(
|
|
'<IB', tiles_bytes, idx * TILE_SIZE
|
|
)
|
|
corners = _RAMP_CORNERS.get(tile_type)
|
|
if corners is None:
|
|
continue
|
|
fx, fy, fz = float(x), float(y), float(z)
|
|
u0 = x / CHUNK_WIDTH
|
|
u1 = (x + 1) / CHUNK_WIDTH
|
|
v0 = y / CHUNK_HEIGHT
|
|
v1 = (y + 1) / CHUNK_HEIGHT
|
|
sw, se, ne, nw = corners
|
|
buf += _tile_quad(
|
|
u0, u1, v0, v1, fx, fy,
|
|
(fz + sw) * WORLD_LAYER_HEIGHT,
|
|
(fz + se) * WORLD_LAYER_HEIGHT,
|
|
(fz + ne) * WORLD_LAYER_HEIGHT,
|
|
(fz + nw) * WORLD_LAYER_HEIGHT
|
|
)
|
|
return bytes(buf)
|
|
|
|
|
|
def from_json(json_path, dcf_path):
|
|
with open(json_path) as f:
|
|
data = json.load(f)
|
|
|
|
tiles = bytearray(CHUNK_TILE_COUNT * TILE_SIZE)
|
|
column_z = {}
|
|
for tile in data.get('tiles', []):
|
|
pos = tile['pos']
|
|
x, y, z = int(pos[0]), int(pos[1]), int(pos[2])
|
|
if (x, y) in column_z and column_z[(x, y)] != z:
|
|
print(
|
|
f' WARNING: {json_path}: column ({x}, {y}) has tiles at '
|
|
f'z={column_z[(x, y)]} and z={z} - only one tile per '
|
|
f'column is kept, z={z} wins (last one in the array)'
|
|
)
|
|
column_z[(x, y)] = z
|
|
struct.pack_into(
|
|
TILE_STRUCT_FORMAT, tiles, tile_index(x, y) * TILE_SIZE,
|
|
int(tile['type']), z
|
|
)
|
|
|
|
terrain_verts = build_terrain_verts(tiles)
|
|
model_names = []
|
|
mesh_offsets = []
|
|
|
|
dcf_base = os.path.splitext(os.path.basename(dcf_path))[0]
|
|
|
|
# Terrain mesh + model (only written if non-empty)
|
|
terrain_dir = os.path.join(ASSETS_DIR, 'meshes', 'chunks')
|
|
models_dir = os.path.join(ASSETS_DIR, 'models', 'chunks')
|
|
os.makedirs(terrain_dir, exist_ok=True)
|
|
os.makedirs(models_dir, exist_ok=True)
|
|
if terrain_verts:
|
|
dmf_name = f'chunk_{dcf_base}_0.dmf'
|
|
write_dmf(os.path.join(terrain_dir, dmf_name), terrain_verts)
|
|
mesh_rel = f'meshes/chunks/{dmf_name}'
|
|
json_name = f'chunk_{dcf_base}_0.json'
|
|
write_model_json(
|
|
os.path.join(models_dir, json_name),
|
|
mesh_rel,
|
|
'tiles.png',
|
|
[255, 255, 255, 255]
|
|
)
|
|
model_names.append(f'models/chunks/{json_name}')
|
|
mesh_offsets.append((0.0, 0.0, 0.0))
|
|
|
|
# External mesh references -> look up matching model JSON
|
|
for mesh in data.get('meshes', []):
|
|
filename = mesh['file']
|
|
pos = mesh.get('pos', [0, 0, 0])
|
|
model_filename = os.path.splitext(filename)[0] + '.json'
|
|
rel = find_model(model_filename)
|
|
if rel is None:
|
|
raise ValueError(
|
|
f"Model not found under assets/models/: {model_filename}"
|
|
)
|
|
model_names.append(rel)
|
|
mesh_offsets.append((float(pos[0]), float(pos[1]), float(pos[2])))
|
|
print(f' Resolved {filename} -> {rel}')
|
|
|
|
entity_spawns = []
|
|
for spawn in data.get('entities', []):
|
|
pos = tuple(int(v) for v in spawn['pos'])
|
|
if spawn['type'] == 'global':
|
|
entity_spawns.append({
|
|
'kind': ENTITY_SPAWN_KIND_GLOBAL,
|
|
'globalId': int(spawn['globalId']),
|
|
'pos': pos,
|
|
})
|
|
elif spawn['type'] == 'item':
|
|
entity_spawns.append({
|
|
'kind': ENTITY_SPAWN_KIND_ITEM,
|
|
'itemId': int(spawn['itemId']),
|
|
'quantity': int(spawn['quantity']),
|
|
'pos': pos,
|
|
})
|
|
else:
|
|
raise ValueError(f"Unknown entity spawn type: {spawn['type']}")
|
|
|
|
area_spawns = []
|
|
for area in data.get('areas', []):
|
|
area_spawns.append({
|
|
'min': tuple(int(v) for v in area['min']),
|
|
'max': tuple(int(v) for v in area['max']),
|
|
'callbackId': int(area['callbackId']),
|
|
'notify': int(area['notify']),
|
|
'trigger': int(area['trigger']),
|
|
})
|
|
|
|
write_dcf(
|
|
dcf_path, bytes(tiles), model_names, mesh_offsets,
|
|
entity_spawns, area_spawns
|
|
)
|
|
|
|
|
|
def process_json(json_path):
|
|
dcf_path = derive_dcf_path(json_path)
|
|
os.makedirs(os.path.dirname(dcf_path), exist_ok=True)
|
|
print(f"{json_path} -> {dcf_path}")
|
|
from_json(json_path, dcf_path)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Legacy DCF (v1/v2) -> current version
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def collapse_legacy_tiles(legacy_tiles, path):
|
|
"""Collapse the old one-tile-per-(x,y,z) grid into the current
|
|
one-tile-per-(x,y) format. If a column has more than one non-null tile
|
|
across its Z layers, the highest Z wins and a warning is printed."""
|
|
tiles = bytearray(CHUNK_TILE_COUNT * TILE_SIZE)
|
|
for y in range(CHUNK_HEIGHT):
|
|
for x in range(CHUNK_WIDTH):
|
|
found = []
|
|
for z in range(_LEGACY_CHUNK_DEPTH):
|
|
legacy_idx = x + y * CHUNK_WIDTH + z * CHUNK_WIDTH * CHUNK_HEIGHT
|
|
shape = struct.unpack_from(
|
|
'<I', legacy_tiles, legacy_idx * _LEGACY_TILE_SIZE
|
|
)[0]
|
|
if shape != TILE_SHAPE_NULL:
|
|
found.append((z, shape))
|
|
if not found:
|
|
continue
|
|
if len(found) > 1:
|
|
print(
|
|
f' WARNING: {path}: column ({x}, {y}) has tiles at '
|
|
f'z={[z for z, _ in found]} - only one tile per column '
|
|
f'is kept, z={found[-1][0]} wins (highest Z)'
|
|
)
|
|
z, shape = found[-1]
|
|
struct.pack_into(
|
|
TILE_STRUCT_FORMAT, tiles, tile_index(x, y) * TILE_SIZE,
|
|
shape, z
|
|
)
|
|
return bytes(tiles)
|
|
|
|
|
|
def read_legacy_dcf(path):
|
|
with open(path, 'rb') as f:
|
|
data = f.read()
|
|
if data[:3] != FILE_MAGIC:
|
|
raise ValueError(f"{path}: not a DCF file")
|
|
version = struct.unpack_from('<I', data, 4)[0]
|
|
if version not in (1, 2):
|
|
raise ValueError(f"{path}: expected version 1 or 2, got {version}")
|
|
|
|
offset = 8
|
|
tiles_size = _LEGACY_CHUNK_TILE_COUNT * _LEGACY_TILE_SIZE
|
|
tiles = collapse_legacy_tiles(data[offset:offset + tiles_size], path)
|
|
offset += tiles_size
|
|
|
|
meshes = []
|
|
if version == 1:
|
|
vert_count = struct.unpack_from('<I', data, offset)[0]
|
|
offset += 4
|
|
verts = data[offset:offset + vert_count * VERTEX_SIZE]
|
|
if len(verts) != vert_count * VERTEX_SIZE:
|
|
raise ValueError(f"{path}: truncated vertex data")
|
|
if vert_count > 0:
|
|
meshes.append(verts)
|
|
else:
|
|
mesh_count = data[offset]
|
|
offset += 1
|
|
for _ in range(mesh_count):
|
|
vert_count = struct.unpack_from('<I', data, offset)[0]
|
|
offset += 4
|
|
verts = data[offset:offset + vert_count * VERTEX_SIZE]
|
|
if len(verts) != vert_count * VERTEX_SIZE:
|
|
raise ValueError(f"{path}: truncated vertex data")
|
|
offset += vert_count * VERTEX_SIZE
|
|
if vert_count > 0:
|
|
meshes.append(verts)
|
|
return tiles, meshes
|
|
|
|
|
|
def upgrade_dcf(path):
|
|
print(f"Upgrading legacy DCF {path} ...")
|
|
tiles, meshes = read_legacy_dcf(path)
|
|
print(
|
|
f" tiles={CHUNK_TILE_COUNT}, meshes={len(meshes)}, "
|
|
f"total_verts={sum(len(m) // VERTEX_SIZE for m in meshes)}"
|
|
)
|
|
base = os.path.splitext(os.path.basename(path))[0]
|
|
terrain_dir = os.path.join(ASSETS_DIR, 'meshes', 'chunks')
|
|
os.makedirs(terrain_dir, exist_ok=True)
|
|
mesh_names = []
|
|
for idx, verts in enumerate(meshes):
|
|
dmf_name = f'chunk_{base}_{idx}.dmf'
|
|
write_dmf(os.path.join(terrain_dir, dmf_name), verts)
|
|
mesh_names.append(f'meshes/chunks/{dmf_name}')
|
|
write_dcf(path, tiles, mesh_names)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Entry point
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def main():
|
|
args = sys.argv[1:]
|
|
|
|
if not args:
|
|
chunks_dir = os.path.join(ASSETSRAW_DIR, 'chunks')
|
|
if not os.path.isdir(chunks_dir):
|
|
print(f"No directory found: {chunks_dir}")
|
|
sys.exit(1)
|
|
json_files = sorted(
|
|
os.path.join(chunks_dir, f)
|
|
for f in os.listdir(chunks_dir)
|
|
if f.endswith('.json')
|
|
)
|
|
if not json_files:
|
|
print(f"No JSON files found in {chunks_dir}")
|
|
sys.exit(0)
|
|
for p in json_files:
|
|
process_json(p)
|
|
return
|
|
|
|
src = args[0]
|
|
if os.path.splitext(src)[1].lower() == '.json':
|
|
process_json(src)
|
|
else:
|
|
upgrade_dcf(src)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|