merge ws/engines

This commit is contained in:
4DBug committed 2026-11-01 20:14:34 -06:00
commit 8bd15840d8
66 files changed
+31751 -1331

No files matched your search

+278
View File
@@ -0,0 +1,278 @@
-- vector/ldtk tests: LDtk's sample projects (and a multi-world one) against the editor's own JSON (levels, layers,
-- tile lists, IntGrid values, entities), external levels through resolve; our writer's output summarised by Python;
-- round trips; a TileMap from Tiled written as LDtk; errors; fuzz. Fixtures: tools/fixtures/ldtk.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ldtk = require("@rosetta/vector/ldtk")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ldtk")
local Ours = require("@tests/gen/ldtk_ours")
local t = T.suite("ldtk")
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return Ldtk.decode(Bytes.reader(b), opts)
end
t:eq(Ldtk.name, "ldtk", "name")
t:eq(Ldtk.category, "vector", "category")
t:check(Ldtk.caps.decode and Ldtk.caps.encode, "caps")
local function levelsOf(map: any): { { group: any, world: string } }
local out = {}
for _, l in map.layers do
if l.world then
for _, lv in l.layers do
table.insert(out, { group = lv, world = l.name })
end
else
table.insert(out, { group = l, world = "" })
end
end
return out
end
-- our TileMap summarised like tools/fixtures/ldtk.py summarises the JSON
local function stats(map: any): any
local levels = {}
for _, it in levelsOf(map) do
local g = it.group
local layers = {}
for _, l in g.layers do
local tl = l.tiles or {}
local sum, flips = 0, 0
for i, x in tl do
sum += x.tile * ((i - 1) % 7 + 1)
if x.flip ~= 0 then
flips += 1
end
end
local ic, is = 0, 0
for _, v in l.intGrid or {} do
if v ~= 0 then
ic += 1
end
is += v
end
local names, epx = {}, 0
for _, o in l.objects or {} do
table.insert(names, o.class)
epx += (o.x + o.pivot[1] * o.width) * 3 + (o.y + o.pivot[2] * o.height)
end
table.insert(layers, {
identifier = l.name,
type = l.ldtkType,
cWid = l.width or 0,
cHei = l.height or 0,
grid = l.gridSize,
tiles = #tl,
tileSum = sum,
flips = flips,
intCount = ic,
intSum = is,
entities = #(l.objects or {}),
entityNames = table.concat(names, ","),
entityPx = epx,
})
end
local nf = 0
for _ in g.properties do
nf += 1
end
table.insert(levels, { identifier = g.name, worldX = g.offsetX, worldY = g.offsetY, pxWid = g.pxWidth, pxHei = g.pxHeight, world = it.world, fields = nf, layers = layers })
end
local ts = {}
for i, x in map.tilesets do
ts[i] = x.name
end
return { levels = levels, tilesets = ts }
end
local function diff(a: any, b: any, path: string, out: { string })
if #out > 6 then
return
end
if type(a) == "table" and type(b) == "table" then
for k, v in a do
diff(v, b[k], path .. "." .. tostring(k), out)
end
for k, v in b do
if a[k] == nil then
diff(nil, v, path .. "." .. tostring(k), out)
end
end
elseif a ~= b then
table.insert(out, path .. ": " .. tostring(a) .. " vs " .. tostring(b))
end
end
local function sameStats(got: any, want: any, label: string)
local out = {}
-- entity-less layers report cWid 0 in ours when unknown: compare only layers' known widths
for _, lv in got.levels do
for _, l in lv.layers do
if l.type == "Entities" then
l.cWid, l.cHei = nil, nil
end
end
end
for _, lv in want.levels do
for _, l in lv.layers do
if l.type == "Entities" then
l.cWid, l.cHei = nil, nil
end
end
end
diff(got, want, "", out)
t:check(#out == 0, label .. ": " .. table.concat(out, "; "))
end
local maps: { [string]: any } = {}
local samples = {}
for _, c in Fx.cases do
local function resolve(p: string): buffer?
local f = c.files[p]
return if f then buffer.fromstring(f) else nil
end
local map, err = decode(c.data, { resolve = resolve })
t:check(map, c.name .. ": decode: " .. tostring(err))
if map then
maps[c.name] = map
t:eq(map.kind, "tilemap", c.name .. ": kind")
sameStats(stats(map), c.stats, c.name .. ": = LDtk's JSON")
end
t:check(Ldtk.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.8, c.name .. ": sniff")
if #c.data < 200000 then
table.insert(samples, c.data)
end
end
-- Python's summary of our output = LDtk's
for _, o in Ours do
local want
for _, c in Fx.cases do
if c.name == o.name then
want = c.stats
end
end
sameStats(o.stats, want, o.name .. ": our LDtk output (Python)")
end
-- details of the API sample
do
local m = maps["Test_file_for_API_showing_all_features.ldtk"]
t:eq(m.meta.source.version, "1.5.3", "json version")
local lv = m.layers[1]
t:check(lv.type == "group" and lv.level == true and lv.name ~= "", "level group")
local ents
for _, l in lv.layers do
if l.type == "objects" then
ents = l
end
end
t:check(ents and #ents.objects > 0, "entities layer")
local e = ents.objects[1]
t:eq(e.class, "EntityFieldsTest", "entity identifier")
t:eq(e.properties.Integer, 1, "Int field")
t:eq(e.properties.Float, 1.5, "Float field")
t:eq(e.properties.String_singleLine, "foo", "String field")
t:eq(e.propertyTypes.Enum, "LocalEnum.SomeEnum", "enum field type")
t:eq(e.properties.Color, "#D181E8", "Color field")
t:eq(e.x, 152 - 16, "entity x = px - pivot * width")
local top = lv.layers[#lv.layers]
t:eq(top.ldtkType, "Entities", "LDtk's first layer is drawn last")
local tiles
for _, l in lv.layers do
if l.ldtkType == "Tiles" then
tiles = l
end
end
local ts = m.tilesets[1]
t:eq(ts.imageSource, "atlas/Inca_front_by_Kronbits-extended.png", "tileset image path")
local first = tiles.tiles[1]
local cell = tiles.data[1 + first.x // 16 + (first.y // 16) * tiles.width]
t:check(bit32.band(cell, 0x0FFFFFFF) - ts.firstGid >= 0, "gid grid from the tile list")
end
do
local m = maps["SeparateLevelFiles.ldtk"]
t:check(m.meta.source.externalLevels, "external levels flag")
t:check(m.layers[1].external and #m.layers[1].layers > 0, "external level loaded through resolve")
local c
for _, x in Fx.cases do
if x.name == "SeparateLevelFiles.ldtk" then
c = x
end
end
local nores = decode(c.data)
t:check(nores and nores.layers[1].unresolved and #nores.layers[1].layers == 0, "external level without resolve")
end
do
local m = maps["multiworld.ldtk"]
t:check(m.meta.source.multiWorld and #m.layers == 2 and m.layers[1].world and m.layers[2].name == "Caves", "worlds")
end
-- edits survive a save
do
local m = maps["Test_file_for_API_showing_all_features.ldtk"]
local lv = m.layers[1]
lv.properties = lv.properties or {}
local ents
for _, l in lv.layers do
if l.type == "objects" then
ents = l
end
end
ents.objects[1].properties.Integer = 42
ents.objects[1].x += 16
local out = assert(Ldtk.encode(m))
local back = assert(decode(out))
local e2
for _, l in back.layers[1].layers do
if l.type == "objects" then
e2 = l.objects[1]
end
end
t:eq(e2.properties.Integer, 42, "edited field")
t:eq(e2.x, 152, "moved entity")
local s = buffer.tostring(out)
t:check(string.find(s, "\"V_Int\",%s*\"params\":%s*%[%s*42%s*%]") ~= nil, "realEditorValues updated")
t:check(F.detect(out) == "ldtk", "F.detect")
end
-- a Tiled map as LDtk: a tile layer and objects
do
local map = Types.TileMap.new(3, 2, 16, 16)
table.insert(map.tilesets, { name = "terrain", firstGid = 1, tileWidth = 16, tileHeight = 16, columns = 4, count = 8, imageSource = "terrain.png", imageWidth = 64, imageHeight = 32 })
table.insert(map.layers, { type = "tiles", name = "Ground", width = 3, height = 2, visible = true, opacity = 1, properties = {}, data = { 1, 2, 0, 0, 6, bit32.bor(8, 0x80000000) } })
table.insert(map.layers, { type = "objects", name = "Things", visible = true, opacity = 1, properties = {}, objects = { { name = "spawn", class = "Spawn", x = 16, y = 0, width = 16, height = 16, properties = { hp = 3 } } } })
local out = assert(F.save(map, nil, { format = "ldtk" }))
local back = assert(decode(out))
local lv = back.layers[1]
t:eq(#lv.layers, 2, "tiled -> ldtk: one level, two layers")
local g = lv.layers[1]
t:eq(g.ldtkType, "Tiles", "tile layer")
t:eq(#g.tiles, 4, "tiles")
t:eq(g.data[6], bit32.bor(8, 0x80000000), "gid + flip")
t:eq(g.tiles[4].srcX, 48, "src x from the tileset columns")
local o = lv.layers[2].objects[1]
t:check(o.class == "Spawn" and o.x == 16 and o.properties.hp == 3, "entity with a made field definition")
end
do
local _, e = decode("{\"jsonVersion\": \"1.5.3\"}")
t:check(e and e.code == Err.CORRUPT, "no defs")
local _, e2 = decode("{")
t:check(e2 ~= nil, "bad JSON")
local _, e3 = Ldtk.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return decode(b)
end, samples, { mutations = 60, garbage = 20 })
t:done()
+212
View File
@@ -0,0 +1,212 @@
-- vector/ogmo tests: Ogmo Editor 3 levels (HaxeFlixel's TurnBasedRPG and levels made in every save layout) against a
-- Python reading of the editor's format; projects; our writer's output (level + project) read by the same Python;
-- round trips; errors; fuzz. Fixtures: tools/fixtures/ogmo.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ogmo = require("@rosetta/vector/ogmo")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ogmo")
local Ours = require("@tests/gen/ogmo_ours")
local t = T.suite("ogmo")
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return Ogmo.decode(Bytes.reader(b), opts)
end
t:eq(Ogmo.name, "ogmo", "name")
t:eq(Ogmo.category, "vector", "category")
t:check(Ogmo.caps.decode and Ogmo.caps.encode, "caps")
local function fmt(v: number): string
if v == math.floor(v) then
return string.format("%d", v)
end
return string.format("%.6g", v)
end
local function jsonValue(v: any): string
if type(v) == "string" then
return "\"" .. v .. "\""
elseif type(v) == "boolean" then
return tostring(v)
end
return if v == math.floor(v) then string.format("%d", v) else string.format("%.17g", v)
end
local function valuesStr(props: any): string
local keys = {}
for k in props do
table.insert(keys, k)
end
table.sort(keys)
local out = {}
for _, k in keys do
table.insert(out, k .. "=" .. jsonValue(props[k]))
end
return table.concat(out, ",")
end
-- our TileMap summarised like tools/fixtures/ogmo.py summarises a level
local function stats(map: any): any
local layers = {}
for _, l in map.layers do
local d: any = { name = l.name, kind = l.ogmoType }
if l.ogmoType == "entity" then
d.items = {}
for _, o in l.objects do
table.insert(d.items, string.format("%s@%s,%s %sx%s r%d n%d %s", o.class, fmt(o.x), fmt(o.y), fmt(o.width), fmt(o.height), math.floor(o.rotation + 0.5), if o.nodes then #o.nodes else 0, valuesStr(o.properties)))
end
elseif l.ogmoType == "decal" then
d.items = {}
for _, o in l.objects do
table.insert(d.items, string.format("%s@%s,%s s%s,%s r%d", o.texture, fmt(o.x), fmt(o.y), fmt(o.scaleX), fmt(o.scaleY), math.floor(o.rotation + 0.5)))
end
elseif l.ogmoType == "grid" then
d.cells = table.concat(l.grid)
else
local ts
for _, x in map.tilesets do
if x.name == l.tileset then
ts = x
end
end
local ne, ids, fl = 0, 0, 0
for k, g in l.data do
local gid = bit32.band(g, 0x0FFFFFFF)
local id = if gid == 0 then -1 else gid - (if ts then ts.firstGid else 1)
if id >= 0 then
ne += 1
end
ids += (id + 1) * ((k - 1) % 7 + 1)
local f = (if bit32.btest(g, 0x80000000) then 4 else 0) + (if bit32.btest(g, 0x40000000) then 2 else 0) + (if bit32.btest(g, 0x20000000) then 1 else 0)
fl += f * ((k - 1) % 5 + 1)
end
d.tileset, d.nonEmpty, d.idSum, d.flagSum = l.tileset or "", ne, ids, fl
end
table.insert(layers, d)
end
return { width = map.pxWidth, height = map.pxHeight, values = valuesStr(map.properties), layers = layers }
end
local function diff(a: any, b: any, path: string, out: { string })
if #out > 6 then
return
end
if type(a) == "table" and type(b) == "table" then
for k, v in a do
diff(v, b[k], path .. "." .. tostring(k), out)
end
for k, v in b do
if a[k] == nil then
diff(nil, v, path .. "." .. tostring(k), out)
end
end
elseif a ~= b then
table.insert(out, path .. ": " .. tostring(a) .. " vs " .. tostring(b))
end
end
local function same(got: any, want: any, label: string)
-- w / h are compared through the layer data sizes
for _, x in { got, want } do
for _, l in x.layers do
l.w, l.h = nil, nil
end
end
local out = {}
diff(got, want, "", out)
t:check(#out == 0, label .. ": " .. table.concat(out, "; "))
end
local samples = {}
local maps = {}
for _, c in Fx.cases do
local map, err = decode(c.data, { project = c.project })
t:check(map, c.name .. ": decode: " .. tostring(err))
if map then
maps[c.name] = map
same(stats(map), c.stats, c.name .. ": = the editor's layout (Python)")
local out = assert(Ogmo.encode(map))
local back = assert(decode(out, { project = c.project }))
same(stats(back), c.stats, c.name .. ": round trip")
end
t:eq(Ogmo.sniff(Bytes.reader(buffer.fromstring(c.data))), 0.9, c.name .. ": sniff level")
table.insert(samples, c.data)
end
for _, o in Ours do
local want
for _, c in Fx.cases do
if c.name == o.name then
want = c.stats
end
end
same(o.stats, want, o.name .. ": our level + project (Python)")
end
t:eq(#Ours, #Fx.cases, "every level written")
for _, p in Fx.projects do
local m, err = decode(p.data)
t:check(m and m.meta.project, p.name .. ": project: " .. tostring(err))
t:eq(Ogmo.sniff(Bytes.reader(buffer.fromstring(p.data))), 1, p.name .. ": sniff project")
if m then
local back = assert(decode(assert(Ogmo.encode(m))))
t:check(back.meta.project and #back.layers == #m.layers and #back.tilesets == #m.tilesets, p.name .. ": project round trip")
end
table.insert(samples, p.data)
end
do
local m = maps["made/level0"]
local ents
for _, l in m.layers do
if l.ogmoType == "entity" then
ents = l
end
end
local p = ents.objects[1]
t:check(p.class == "player" and p.x == 16 and p.y == 16 and p.width == 8 and p.height == 16, "entity: origin, template size")
t:check(math.abs(p.rotation - 90) < 1e-9 and p.flippedX == true and #p.nodes == 2, "entity: radians -> degrees, flip, nodes")
t:eq(p.propertyTypes.hp, "int", "entity value types from the template")
t:eq(m.propertyTypes.gravity, "float", "level value types")
t:eq(m.layers[#m.layers].ogmoType, "entity", "Ogmo's first layer is on top")
local small = m.tilesets[2]
t:check(small.columns == 3 and small.imageWidth == 36 and type(small.imageData) == "buffer", "tileset from the embedded PNG")
local rpg = maps["turnBasedRPG/room-001"]
t:check(rpg.width == 80 and rpg.tileWidth == 8 and rpg.pxWidth == 640, "map size from the project grid")
-- without the project: tiles keep their ids
local c = Fx.cases[1]
local bare = assert(decode(c.data))
t:check(#bare.tilesets == 0 and bare.layers[1].data ~= nil, "level without a project")
end
-- a Tiled-style map as an Ogmo level
do
local map = Types.TileMap.new(2, 2, 16, 16)
table.insert(map.tilesets, { name = "t", firstGid = 1, tileWidth = 16, tileHeight = 16, columns = 2, count = 4 })
table.insert(map.layers, { type = "tiles", name = "ground", width = 2, height = 2, visible = true, opacity = 1, properties = {}, data = { 1, 0, bit32.bor(4, 0x80000000), 2 } })
table.insert(map.layers, { type = "objects", name = "things", visible = true, opacity = 1, properties = {}, objects = { { class = "coin", x = 4, y = 6, width = 8, height = 8, properties = { value = 10 } } } })
local out = assert(F.save(map, nil, { format = "ogmo" }))
local back = assert(decode(out, { project = assert(Ogmo.encode(map, { kind = "project" })) }))
t:eq(back.layers[1].data[3], bit32.bor(4, 0x80000000), "tile + flip")
t:check(back.layers[2].objects[1].properties.value == 10 and back.layers[2].objects[1].x == 4, "object")
end
do
local _, e = decode("{\"ogmoVersion\": \"3.4.0\"}")
t:check(e and e.code == Err.CORRUPT, "no layers")
local _, e2 = decode("[1]")
t:check(e2 and e2.code == Err.CORRUPT, "not an object")
local _, e3 = Ogmo.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode arg")
local _, e4 = Ogmo.encode(maps["made/level0"], { kind = "x" :: any })
t:check(e4 and e4.code == Err.ARG, "bad kind")
end
t:fuzz("decode", function(b: buffer)
return decode(b, { project = Fx.projects[2].data })
end, samples, { mutations = 60, garbage = 20 })
t:done()