Mothback/code/in_out.lua

88 lines
2.4 KiB
Lua

function ExportLevel(levelname, filename)
os.execute( "mkdir \"./export\"" )
filename = filename or "output.lua"
filename = "export/"..filename
exportFile = io.open(filename, "w+")
if exportFile then
logPrint("Exporting level \"".. levelname .. "\"...")
exportFile:write("return {")
logPrint("- level name")
exportFile:write("\n name = \"" .. levelname .. "\",")
logPrint("- tileset")
for k, v in pairs(tileset) do
if v == LevelData.tileset then
exportFile:write("\n tileset = tileset." .. k .. ",")
end
end
logPrint("- properties")
exportFile:write("\n properties = {")
exportFile:write("\n darkness = true")
exportFile:write("\n },")
logPrint("- tiles")
exportFile:write("\n tiles = {")
local rows = #LevelTiles
for i = 1, #LevelTiles do
exportFile:write("\n { ")
logPrint(" - Row "..i.."/"..rows.." "..math.floor(100*((i-1)*100/rows))/100 .."%")
for j = 1, #LevelTiles[i] do
if j ~= 1 then
exportFile:write(", ")
end
exportFile:write(tostring(LevelTiles[i][j].id))
end
exportFile:write("}")
if i ~= #LevelTiles then
exportFile:write(", ")
end
end
logPrint(" - All rows 100%")
exportFile:write("\n },")
logPrint("- objects")
exportFile:write("\n objects = {}")
logPrint("Exporting complete.")
exportFile:write("\n}")
exportFile:close()
end
end
-- Source https://stackoverflow.com/a/11130774
function scandir(directory)
local i, t, popen = 0, {}, io.popen
local pfile = popen('ls "'..directory..'"')
for filename in pfile:lines() do
i = i + 1
t[i] = filename
end
pfile:close()
return t
end
--[[
return {
name = "level1",
tileset = tileset.library,
tiles = {
{13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,25,26, 6,25,26, 7, 0, 5,25,26, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,49,50, 6,49,50, 7, 0, 5,49,50, 7, 0, 0, 0, 0, 0, 0},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
},
objects = {}
}
]]
if logging then
-- Make log stuff
os.execute( "mkdir \"./logs\"" )
logFile = io.open("logs/mothback_"..os.date("%Y-%m-%d_%H-%M-%S")..".log", "a+")
end