-- 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()