Skip to content

Commit

Permalink
Merge branch 'master' into ui-olympUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruor committed Feb 6, 2022
2 parents af448da + 848dd8c commit 526e045
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 98 deletions.
170 changes: 109 additions & 61 deletions src/autotiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ end

local function getPaddingOrCenterQuad(x, y, tile, tiles, meta, airTile, emptyTile, defaultQuad, defaultSprite)
if checkPadding(tiles, x, y, airTile, emptyTile) then
local padding = meta.padding[tile]
local padding = meta[tile].padding

return #padding > 0 and padding or defaultQuad, defaultSprite
return padding and #padding > 0 and padding or defaultQuad, defaultSprite

else
local center = meta.center[tile]
local center = meta[tile].center

return #center > 0 and center or defaultQuad, defaultSprite
return center and #center > 0 and center or defaultQuad, defaultSprite
end
end

Expand Down Expand Up @@ -165,9 +165,10 @@ end

function autotiler.getQuads(x, y, tiles, meta, airTile, emptyTile, wildcard, defaultQuad, defaultSprite, checkTile)
local tile = tiles:get(x, y)
local tileMeta = meta[tile]

local masks = meta.masks[tile]
local ignore = meta.ignores[tile]
local masks = tileMeta.masks
local ignore = tileMeta.ignores

local matches, quads, sprites = getMaskQuadsFromTiles(x, y, masks, tiles, tile, ignore, airTile, wildcard, checkTile)

Expand All @@ -181,9 +182,10 @@ end

function autotiler.getQuadsWithBitmask(x, y, tiles, meta, airTile, emptyTile, wildcard, defaultQuad, defaultSprite, checkTile, lshift, bxor, band)
local tile = tiles:get(x, y)
local tileMeta = meta[tile]

local masks = meta.masks[tile]
local ignore = meta.ignores[tile]
local masks = tileMeta.masks
local ignore = tileMeta.ignores

local matches, quads, sprites = getMaskQuadsFromTilesWithBitmask(x, y, masks, tiles, tile, ignore, airTile, wildcard, checkTile, lshift, bxor, band)

Expand Down Expand Up @@ -211,81 +213,127 @@ local function convertTileString(s)
return res
end

function autotiler.loadTilesetXML(fn)
local handler = require("lib.xml2lua.xmlhandler.tree")
local parser = xml2lua.parser(handler)
local xml = utils.stripByteOrderMark(utils.readAll(fn, "rb"))
-- X values are stored as nil in the mask matrix
local function countMaskXs(mask)
local maskMatrix = mask.mask
local width, height = maskMatrix:size()
local count = 0

parser:parse(xml)
for x = 1, width do
for y = 1, height do
if maskMatrix:getInbounds(x, y) == nil then
count += 1
end
end
end

local paths = {}
local masks = {}
local padding = {}
local center = {}
local ignores = {}
return count
end

local lshift = bit.lshift
local function maskCompare(lhs, rhs)
return countMaskXs(lhs) < countMaskXs(rhs)
end

-- Inline mask sort, more X -> later
local function sortTilesetMasks(masks)
table.sort(masks, maskCompare)

return masks
end

local function getTilesetStructure(id)
return {
id = id,
path = "",
padding = {},
center = {},
masks = {},
ignores = {}
}
end

local function readTilesetInfo(tileset, id, element)
local currentMasks = {}

for i, tileset in ipairs(handler.root.Data.Tileset) do
local id = tileset._attr.id
local path = tileset._attr.path
local copy = tileset._attr.copy
local ignore = tileset._attr.ignores
-- Doesn't store single child tags in list, pack it into a table for easier use
local elementSets = element.set and (#element.set > 0 and element.set or {element.set}) or {}

paths[id] = "tilesets/" .. path
for _, child in ipairs(elementSets) do
local attrs = child._attr or child

if ignore then
-- TODO - Check assumption, none of the XML files have mutliple ignores without wildcard
ignores[id] = table.flip($(ignore):split(";"))
local mask = attrs.mask
local tiles = attrs.tiles or ""
local sprites = attrs.sprites or ""

if mask == "padding" then
tileset.padding = convertTileString(tiles)

elseif mask == "center" then
tileset.center = convertTileString(tiles)

else
local maskMatrix = convertMaskString(mask)

table.insert(tileset.masks, {
mask = maskMatrix,
quads = convertTileString(tiles),
sprites = sprites,
tilesMask = maskToBitmask(maskMatrix, bit.lshift),
ignoresMask = maskToIgnoreBitmask(maskMatrix, bit.lshift)
})
end
end

padding[id] = copy and table.shallowcopy(padding[copy]) or {}
center[id] = copy and table.shallowcopy(center[copy]) or {}
masks[id] = copy and table.shallowcopy(masks[copy]) or {}
sortTilesetMasks(tileset.masks)
end

local currentMasks = {}
function autotiler.loadTilesetXML(filename)
local handler = require("lib.xml2lua.xmlhandler.tree")
local parser = xml2lua.parser(handler)
local xmlString = utils.readAll(filename, "rb")

-- Doesn't store single child tags in list, pack it into a table for easier use
local tilesetSets = tileset.set and (#tileset.set > 0 and tileset.set or {tileset.set}) or {}
if not xmlString then
error(string.format("Unable to read tileset xml from '%s'", filename))
end

for j, child in ipairs(tilesetSets) do
local attrs = child._attr or child
local xml = utils.stripByteOrderMark(xmlString)

local mask = attrs.mask
local tiles = attrs.tiles or ""
local sprites = attrs.sprites or ""
parser:parse(xml)

local tilesetsMeta = {}
local elementLookup = {}
local tilesetsRoot = handler.root.Data.Tileset

local lshift = bit.lshift

if mask == "padding" then
padding[id] = convertTileString(tiles)
for i, element in ipairs(tilesetsRoot) do
local id = element._attr.id
local copy = element._attr.copy
local ignores = element._attr.ignores
local path = element._attr.path
local tileset = getTilesetStructure(id)

elseif mask == "center" then
center[id] = convertTileString(tiles)
tileset.path = "tilesets/" .. path

else
local maskMatrix = convertMaskString(mask)
readTilesetInfo(tileset, id, element)

table.insert(currentMasks, {
mask = maskMatrix,
quads = convertTileString(tiles),
sprites = sprites,
tilesMask = maskToBitmask(maskMatrix, lshift),
ignoresMask = maskToIgnoreBitmask(maskMatrix, lshift)
})
if copy then
if not elementLookup[copy] then
error(string.format("Copied tilesets must be defined before the tileset coping from them: %s copies %s", id, copy))
end

readTilesetInfo(tileset, id, elementLookup[copy])
end

if #currentMasks > 0 then
masks[id] = currentMasks
if ignores then
tileset.ignores = table.flip($(ignores):split(";"))
end

tilesetsMeta[id] = tileset
elementLookup[id] = element
end

return {
paths = paths,
masks = masks,
center = center,
padding = padding,
ignores = ignores
}
return tilesetsMeta
end

return autotiler
19 changes: 13 additions & 6 deletions src/brushes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function brushHelper.updateRender(room, x, y, material, layer, randomMatrix)
local tx, ty = x + i - 1, y + j - 1

if tx >= 1 and ty >= 1 and tx <= tilesWidth and ty <= tilesHeight then
local target = tilesMatrix:get(tx, ty, "0")
local target = tilesMatrix:get(tx, ty, " ")
local mat = material:getInbounds(i, j)

if mat ~= target and mat ~= " " then
Expand Down Expand Up @@ -171,12 +171,15 @@ function brushHelper.updateRender(room, x, y, material, layer, randomMatrix)

if quadCount > 0 then
local randQuad = quads[utils.mod1(rng, quadCount)]
local texture = meta.paths[tile] or emptyTile
local texture = meta[tile].path or emptyTile

local spriteMeta = atlases.gameplay[texture]
local quad = celesteRender.getOrCacheTileSpriteQuad(cache, tile, texture, randQuad, fg)

batch:set(x, y, spriteMeta, quad, x * 8 - 8, y * 8 - 8)
if spriteMeta then
local quad = celesteRender.getOrCacheTileSpriteQuad(cache, tile, texture, randQuad, fg)

batch:set(x, y, spriteMeta, quad, x * 8 - 8, y * 8 - 8)
end
end
end
end
Expand All @@ -198,8 +201,12 @@ function brushHelper.getTile(room, x, y, layer)
end

function brushHelper.getValidTiles(layer, addAir)
local autoTiler = layer == "tilesFg" and celesteRender.tilesMetaFg or celesteRender.tilesMetaBg
local paths = utils.deepcopy(autoTiler.paths)
local tilerMeta = layer == "tilesFg" and celesteRender.tilesMetaFg or celesteRender.tilesMetaBg
local paths = {}

for id, tileset in ipairs(tilerMeta) do
paths[id] = tileset.path
end

if addAir ~= false then
paths["0"] = "Air"
Expand Down
9 changes: 5 additions & 4 deletions src/celeste_render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function celesteRender.loadCustomTilesetAutotiler(state)

else
if tilesMetaFg then
logging.warning("Loading custom foreground tile XML failed: ", tilesMetaFg)
logging.warning(string.format("Loading custom foreground tile XML failed: %s", tilesMetaFg))
end
end

Expand All @@ -84,7 +84,7 @@ function celesteRender.loadCustomTilesetAutotiler(state)

else
if tilesMetaBg then
logging.warning("Loading custom background tile XML failed: ", tilesMetaBg)
logging.warning(string.format("Loading custom background tile XML failed: %s", tilesMetaBg))
end
end
end
Expand Down Expand Up @@ -349,14 +349,15 @@ function celesteRender.getTilesBatch(room, tiles, meta, fg, randomMatrix, batchM
local tile = tilesMatrix:getInbounds(x, y)

if tile ~= airTile then
if meta.paths[tile] then
local tileMeta = meta[tile]
if tileMeta and tileMeta.path then
-- TODO - Render overlay sprites
local quads, sprites = autotiler.getQuadsWithBitmask(x, y, tilesMatrix, meta, airTile, emptyTile, wildcard, defaultQuad, defaultSprite, checkTile, lshift, bxor, band)
local quadCount = #quads

if quadCount > 0 then
local randQuad = quads[utils.mod1(rng, quadCount)]
local texture = meta.paths[tile] or emptyTile
local texture = tileMeta.path or emptyTile

local spriteMeta = atlases.gameplay[texture]

Expand Down
32 changes: 24 additions & 8 deletions src/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,13 @@ function entities.moveSelection(room, layer, selection, offsetX, offsetY)
local name = entity._name
local handler = entities.registeredEntities[name]

-- Notify movement
-- Notify movement, stop movement if it returns false
if handler.onMove then
handler.onMove(room, entity, node, offsetX, offsetY)
local handled = handler.onMove(room, entity, node, offsetX, offsetY)

if handled == false then
return false
end
end

-- Custom entity movement
Expand Down Expand Up @@ -544,9 +548,13 @@ function entities.resizeSelection(room, layer, selection, offsetX, offsetY, dire
return false
end

-- Notify resize
-- Notify resize, stop resize if it returns false
if handler.onResize then
handler.onResize(room, entity, offsetX, offsetY, directionX, directionY)
local handled = handler.onResize(room, entity, offsetX, offsetY, directionX, directionY)

if handled == false then
return false
end
end

local entityOffsetX = 0
Expand Down Expand Up @@ -631,9 +639,13 @@ function entities.deleteSelection(room, layer, selection)
end
end

-- Notify deletion
-- Notify deletion, stop deletion if it returns false
if handler.onDelete then
handler.onDelete(room, entity, node)
local handled = handler.onDelete(room, entity, node)

if handled == false then
return false
end
end

-- Custom deletion
Expand Down Expand Up @@ -678,9 +690,13 @@ function entities.addNodeToSelection(room, layer, selection)
entity.nodes = nodes
end

-- Notify addition
-- Notify addition, stop node addition if it returns false
if handler.onNodeAdded then
handler.onNodeAdded(room, entity, node)
local handled = handler.onNodeAdded(room, entity, node)

if handled == false then
return false
end
end

-- Custom node adding
Expand Down
4 changes: 2 additions & 2 deletions src/entities/bridge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local function addBridgeTileSprites(sprites, x, y, size)

sprite:useRelativeQuad(size.x, py, size.width, height)
sprite:setJustification(0.0, 0.0)
sprite:addPosition(x, y + py - 8)
sprite:addPosition(x, y + py - 3)

table.insert(sprites, sprite)

Expand All @@ -40,7 +40,7 @@ local function addBridgeTileSprites(sprites, x, y, size)

sprite:useRelativeQuad(size.x, size.y, size.width, size.height)
sprite:setJustification(0.0, 0.0)
sprite:addPosition(x, y - 8)
sprite:addPosition(x, y - 3)

table.insert(sprites, sprite)
end
Expand Down
2 changes: 1 addition & 1 deletion src/entities/spinner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end

function spinner.sprite(room, entity)
local color = string.lower(entity.color or defaultSpinnerColor)
local dusty = entity.dusty
local dusty = entity.dust

if dusty then
local textureBase = "danger/dustcreature/base00"
Expand Down
Loading

0 comments on commit 526e045

Please sign in to comment.