info = { type = "image", title = "Paper crane", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Paper folds into a crane and flies away", api = 44, } local tr = ({ sk = { ["Paper"] = "Papier", ["Red"] = "Červený", ["White"] = "Biely", ["Blue"] = "Modrý" }, cs = { ["Paper"] = "Papír", ["Red"] = "Červený", ["White"] = "Bílý", ["Blue"] = "Modrý" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "paper", label = T("Paper"), type = "int", default = "red", opts = { keys = { "red", "white", "blue" }, T("Red"), T("White"), T("Blue") } }, } local rect, blend_rect, blit_rgb, blit = gfx.rect, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, ceil, sqrt = math.floor, math.ceil, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local NS = 28 local INK = { red = { 208, 24, 34 }, white = { 232, 232, 226 }, blue = { 34, 84, 206 } } local CYC = 14.0 local PAL = nil function on_settings() local c = INK[cfg.paper] or INK.red PAL = { 0, 0, 0 } for i = 0, NS - 1 do local t = i / (NS - 1) if t < 0.55 then local f = 0.24 + 0.76 * t / 0.55 PAL[#PAL + 1] = floor(c[1] * f); PAL[#PAL + 1] = floor(c[2] * f); PAL[#PAL + 1] = floor(c[3] * f) else local k = 0.6 * ((t - 0.55) / 0.45) ^ 1.2 for j = 1, 3 do PAL[#PAL + 1] = floor(c[j] + (255 - c[j]) * k) end end end end local function crane(wa) local ax, ay = 30 - 7 * wa, 39 - 27 * wa return { { 0.30, 0, 0, 30, 37, ax + 12, ay + 4, 48, 38 }, { 0.62, 0.01, 0, 13, 39, 5, 24, 22, 37 }, { 0.36, 0, 0, 44, 38, 55, 23, 58, 25, 49, 42 }, { 0.80, 0, 0.01, 54, 24, 62, 27, 57, 28 }, { 0.55, 0, 0, 12, 39, 32, 39, 32, 53 }, { 0.30, 0, 0, 32, 39, 52, 40, 32, 53 }, { 0.88, -0.004, -0.006, 17, 39, ax, ay, 32, 42 }, { 0.58, 0, 0, ax, ay, 46, 41, 32, 42 }, } end local DEF = { function() return { { 0.72, -0.005, -0.012, 32, 35, 52, 44, 32, 53, 12, 44 } } end, function() return { { 0.42, 0, 0, 12, 44, 52, 44, 32, 53 }, { 0.80, -0.004, -0.02, 12, 44, 52, 44, 32, 51 } } end, function() return { { 0.74, 0, -0.005, 32, 32, 20, 44, 32, 56 }, { 0.44, 0, 0, 32, 32, 44, 44, 32, 56 }, { 0.58, 0, 0, 20, 44, 32, 56, 32, 47 }, { 0.30, 0, 0, 44, 44, 32, 56, 32, 47 } } end, function() return { { 0.76, 0, -0.006, 32, 28, 24, 42, 32, 46 }, { 0.46, 0, 0, 32, 28, 40, 42, 32, 46 }, { 0.62, 0, 0, 24, 42, 32, 46, 32, 52, 26, 60 }, { 0.30, 0, 0, 40, 42, 32, 46, 32, 52, 38, 60 } } end, function() return crane(0.15) end, function() return crane(1.0) end, function() return crane(0.55) end, function() return crane(-0.3) end, } local SH = { { 32, 17 }, { 32, 17 }, { 32, 12 }, { 32, 11 }, { 32, 21 }, { 32, 21 }, { 32, 21 }, { 32, 21 } } local BG = nil local POSE = {} local job = nil local nextp = 1 local function fill(g, X0, Y0, p) local n = (#p - 3) // 2 local cx, cy, ymin, ymax = 0, 0, 1e9, -1e9 for i = 0, n - 1 do local x, y = p[4 + 2 * i], p[5 + 2 * i] cx = cx + x; cy = cy + y if y < ymin then ymin = y end if y > ymax then ymax = y end end cx, cy = cx / n, cy / n local cnt = 0 for y = ceil(ymin - 0.5), floor(ymax - 0.5) do local yc = y + 0.5 local xl, xr = 1e9, -1e9 for i = 0, n - 1 do local j = (i + 1) % n local x0, y0, x1, y1 = p[4 + 2 * i], p[5 + 2 * i], p[4 + 2 * j], p[5 + 2 * j] if (y0 <= yc and y1 > yc) or (y1 <= yc and y0 > yc) then local x = x0 + (yc - y0) * (x1 - x0) / (y1 - y0) if x < xl then xl = x end if x > xr then xr = x end end end local row = g[y - Y0 + 1] for x = ceil(xl - 0.5), floor(xr - 0.5) do local s = p[1] + p[2] * (x + 0.5 - cx) + p[3] * (yc - cy) if s < 0 then s = 0 elseif s > 1 then s = 1 end row[x - X0 + 1] = floor(s * (NS - 1) + 0.5) + 1 cnt = cnt + 1 end end return cnt end local function start_job(n) local polys = DEF[n]() local x0, y0, x1, y1 = 99, 99, -99, -99 for _, p in ipairs(polys) do for i = 4, #p, 2 do if p[i] < x0 then x0 = p[i] end if p[i] > x1 then x1 = p[i] end if p[i + 1] < y0 then y0 = p[i + 1] end if p[i + 1] > y1 then y1 = p[i + 1] end end end x0, y0 = floor(x0), floor(y0) local w, h = ceil(x1) - x0 + 1, ceil(y1) - y0 + 1 local g = {} for y = 1, h do local row = {} for x = 1, w do row[x] = 0 end g[y] = row end job = { n = n, polys = polys, k = 1, g = g, x0 = x0, y0 = y0, w = w, h = h } end local function build_step() if not job then if nextp > #DEF then return end start_job(nextp); nextp = nextp + 1 end local left = 700 while job.k <= #job.polys and left > 0 do left = left - fill(job.g, job.x0, job.y0, job.polys[job.k]) job.k = job.k + 1 end if job.k > #job.polys then local rows, g, w = {}, job.g, job.w for y = 0, job.h - 1 do rows[y + 1] = string.char(table.unpack(g[y + 1], 1, w)) end POSE[job.n] = { table.concat(rows), job.x0, job.y0, w, job.h } job = nil end end local function build_bg() for y = 0, 29 do rect(0, y, W, 1, 46 + y * 46 // 29, 52 + y * 46 // 29, 64 + y * 48 // 29) end for y = 30, 63 do local t = (y - 30) / 33 rect(0, y, W, 1, floor(136 + 54 * t), floor(122 + 52 * t), floor(106 + 46 * t)) end rect(0, 30, W, 1, 168, 154, 138) BG = gfx.snapshot(0, 0, W, H) end local function shadow(cx, hw, a) if a < 0.02 then return end for j = -3, 3 do blend_rect(floor(cx - (hw + 3) * sqrt(1 - (j / 4.2) ^ 2) + 0.5), 53 + j, floor(2 * (hw + 3) * sqrt(1 - (j / 4.2) ^ 2) + 0.5), 1, 20, 14, 10, a) end for j = -2, 2 do blend_rect(floor(cx - hw * sqrt(1 - (j / 3.2) ^ 2) + 0.5), 53 + j, floor(2 * hw * sqrt(1 - (j / 3.2) ^ 2) + 0.5), 1, 20, 14, 10, a) end end function render() if not PAL then on_settings() end if not BG then build_bg() end build_step() blit_rgb(BG, 0, 0, W, H) local t = (frame / FPS + 6.4) % CYC local n, dx, dy, sa = 1, 0, 0, 0.2 if t < 1.2 then n = 1 elseif t < 2.8 then n = 2 elseif t < 4.4 then n = 3 elseif t < 6.0 then n = 4 elseif t < 7.6 then n = 5 elseif t < 9.4 then n = 6 elseif t < 12.1 then local u = t - 9.4 n = ({ 6, 7, 5, 8, 5, 7 })[floor(u * 12) % 6 + 1] dx, dy = 3 * u + 11 * u * u, -(3 * u + 9 * u * u) sa = 0.2 * (1 - (-dy) / 40) elseif t < 12.6 then n = 0 else local u = (t - 12.6) / 1.0 if u > 1 then u = 1 end n, dy = 1, -50 * (1 - u * u) sa = 0.2 * u end if n > 0 then local sh = SH[n] shadow(sh[1] + dx * 0.7, sh[2], sa) local p = POSE[n] if p then blit(p[1], PAL, p[2] + floor(dx + 0.5), p[3] + floor(dy + 0.5), p[4], p[5], p[4], p[5], true) else gfx.ready(false) end end end