diff --git a/init.lua b/init.lua index f848286..09e2279 100644 --- a/init.lua +++ b/init.lua @@ -1402,6 +1402,62 @@ function M.get_override(layer_name, x, y, map_id) return normalize_override_entry(entry) end +-- 0.5.2: vertex-grid write APIs for the autotile painting path. +-- Vertices live on a (W+1) x (H+1) grid; painting a single vertex +-- "fills in" the 4 cells around it per the any-corner rule (cell +-- becomes material). The map-editor's Auto-Tile mode is the +-- primary consumer. + +local function check_vertex_coords(map, vx, vy, fn_name) + local vw = map.size.w + 1 + local vh = map.size.h + 1 + if vx < 0 or vy < 0 or vx >= vw or vy >= vh then + error(string.format("%s: vertex (%d,%d) out of bounds for vertex-grid %dx%d", + fn_name, vx, vy, vw, vh)) + end +end + +local function ensure_vertex_grid(layer, map) + if layer.vertices == nil then + local vw = map.size.w + 1 + local vh = map.size.h + 1 + local grid = {} + for i = 1, vw * vh do grid[i] = 0 end + layer.vertices = grid + end + return layer.vertices +end + +function M.set_vertex(layer_name, vx, vy, painted, map_id) + local map = require_map(map_id) + check_vertex_coords(map, vx, vy, "maps.set_vertex") + local layer = require_v3_layer(map, layer_name, "maps.set_vertex") + local v = ensure_vertex_grid(layer, map) + local vw = map.size.w + 1 + local idx = vy * vw + vx + 1 + local new_val = (painted == true or painted == 1) and 1 or 0 + if v[idx] ~= new_val then + v[idx] = new_val + map._dirty = true + map._opaque_ceiling = nil -- vertex change may affect opaque ceiling + -- _layer_has_content goes true on paint; never auto-cleared + -- on erase (stale-true is harmless). + if new_val == 1 and map._layer_has_content then + map._layer_has_content[layer_name] = true + end + end +end + +function M.get_vertex(layer_name, vx, vy, map_id) + local map = require_map(map_id) + check_vertex_coords(map, vx, vy, "maps.get_vertex") + local layer = require_v3_layer(map, layer_name, "maps.get_vertex") + if layer.vertices == nil then return false end + local vw = map.size.w + 1 + local v = layer.vertices[vy * vw + vx + 1] + return v == 1 or v == true +end + function M.set_roof(x, y, value, map_id) local map = require_map(map_id) if x < 0 or y < 0 or x >= map.size.w or y >= map.size.h then diff --git a/manifest.lib b/manifest.lib index 5f44935..9c70cef 100644 --- a/manifest.lib +++ b/manifest.lib @@ -1 +1 @@ -{"id":"lib-core.maps","version":"0.5.1","api_min":"0.1"} +{"id":"lib-core.maps","version":"0.5.2","api_min":"0.1"}