From 0985141fb91a6ae85e80d9f9575004b489b1f849 Mon Sep 17 00:00:00 2001 From: Axel Meyer Date: Thu, 21 May 2026 15:20:32 +0200 Subject: [PATCH] feat: add v1->v2 schema auto-upgrade for map tables Converts legacy single-tilemap maps (tiles[], tile_rotations[]) into the schema-v2 format (atlases[], layers.surface with packed GID cells). Co-Authored-By: Claude Opus 4.7 (1M context) --- init.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/init.lua b/init.lua index 95b1694..f354013 100644 --- a/init.lua +++ b/init.lua @@ -43,6 +43,40 @@ local function decode_gid(gid) return atlas_index, tile_id, rotation end +-- ===================================================================== +-- Schema v1→v2 Auto-Upgrade (transparent beim Load) +-- v1: { id, tilemap, size, tiles[], tile_rotations? } +-- v2: { schema_version, id, size, atlases[], layers: { surface: { tiles[] } }, roof? } +-- ===================================================================== + +local function upgrade_v1_to_v2(v1) + local atlas_id = v1.tilemap or "default_tilemap" + local v1_tiles = v1.tiles or {} + local v1_rots = v1.tile_rotations or {} + + local gids = {} + for i = 1, #v1_tiles do + local tile_id = v1_tiles[i] + local rot_deg = v1_rots[i] or 0 + local rot_quad = math.floor(rot_deg / 90) % 4 + if tile_id == 0 then + gids[i] = 0 + else + gids[i] = encode_gid(0, tile_id, rot_quad) + end + end + + return { + schema_version = 2, + id = v1.id, + size = v1.size, + atlases = { atlas_id }, + layers = { + surface = { tiles = gids } + } + } +end + -- ===================================================================== -- Internal helpers -- ===================================================================== @@ -354,5 +388,6 @@ end M.encode_gid = encode_gid M.decode_gid = decode_gid +M.upgrade_v1_to_v2 = upgrade_v1_to_v2 return M