info = { type = "image", title = "Shadow puppets", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Hand shadows bark, wiggle and fly by lantern", api = 44, } local tr = ({ sk = { ["Animals"] = "Zvieratá", ["All"] = "Všetky", ["Dog"] = "Pes", ["Rabbit"] = "Zajac", ["Bird"] = "Vták", ["Light"] = "Svetlo", ["Amber"] = "Jantár", ["Honey"] = "Med", ["Ember"] = "Žiar" }, cs = { ["Animals"] = "Zvířata", ["All"] = "Všechna", ["Dog"] = "Pes", ["Rabbit"] = "Králík", ["Bird"] = "Pták", ["Light"] = "Světlo", ["Amber"] = "Jantar", ["Honey"] = "Med", ["Ember"] = "Žár" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "show", label = T("Animals"), type = "int", default = "all", opts = { keys = { "all", "dog", "rabbit", "bird" }, T("All"), T("Dog"), T("Rabbit"), T("Bird") } }, { key = "light", label = T("Light"), type = "int", default = "amber", opts = { keys = { "amber", "honey", "ember" }, T("Amber"), T("Honey"), T("Ember") } }, } local rect, px, blit_indexed = gfx.rect, gfx.px, gfx.blit_indexed local floor, ceil, sin, sqrt, max, min = math.floor, math.ceil, math.sin, math.sqrt, math.max, math.min local char, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local LX, LY, NPAL = 56, 8, 78 local LIGHTS = { amber = { 255, 198, 96, 122, 66, 26 }, honey = { 255, 226, 128, 142, 98, 34 }, ember = { 255, 156, 84, 110, 44, 24 }, } local LIGHT = LIGHTS.amber local PALS = {} local function wall_pal(lv) local p = PALS[lv] if p then return p end local f = 0.94 + 0.03 * lv local L = LIGHT p = { 0, 0, 0 } for k = 1, NPAL - 1 do local t = (k - 1) / (NPAL - 2) for c = 1, 3 do p[#p + 1] = min(255, floor((L[c] + (L[c + 3] - L[c]) * t) * f + 0.5)) end end PALS[lv] = p return p end local function cap(ax, ay, ar, bx, by, br) return { ax, ay, ar, bx, by, br } end local function ell(cx, cy, rx, ry) return { cx, cy, rx, ry } end local function cut(x, y) return { x, y } end local rep = string.rep local function raster(prims, rim, S) local rows, minx, maxx, miny, maxy = {}, 1e9, -1e9, 1e9, -1e9 local cuts = {} local function disk(cx, cy, rx, ry, lo, hi) for y = ceil(cy - ry), floor(cy + ry) do local d = (y - cy) / ry local hw = rx * sqrt(max(0, 1 - d * d)) local a, b = ceil(cx - hw), floor(cx + hw) if a <= b then if not lo[y] or a < lo[y] then lo[y] = a end if not hi[y] or b > hi[y] then hi[y] = b end end end end for _, p in ipairs(prims) do if S then local q = {} for i = 1, #p do q[i] = p[i] * S end p = q end if #p == 2 then cuts[#cuts + 1] = p else local lo, hi = {}, {} if #p == 4 then disk(p[1], p[2], p[3], p[4], lo, hi) else local n = max(1, ceil(sqrt((p[4] - p[1]) ^ 2 + (p[5] - p[2]) ^ 2) / max(1, 0.7 * min(p[3], p[6])))) for i = 0, n do local t = i / n local r = p[3] + (p[6] - p[3]) * t disk(p[1] + (p[4] - p[1]) * t, p[2] + (p[5] - p[2]) * t, r, r, lo, hi) end end for y, a in pairs(lo) do local r = rows[y] if not r then r = {}; rows[y] = r end r[#r + 1] = a; r[#r + 1] = hi[y] if a < minx then minx = a end if hi[y] > maxx then maxx = hi[y] end if y < miny then miny = y end if y > maxy then maxy = y end end end end local w, h = maxx - minx + 3, maxy - miny + 3 local ox, oy = minx - 1, miny - 1 local cutx = {} for _, c in ipairs(cuts) do cutx[floor(c[2] + 0.5)] = floor(c[1] + 0.5) end local cur local function before(p, q) return cur[p] < cur[q] end local sets = {} local function set(y) local s = sets[y] if s == nil then s = false if rows[y] then s = {} local iv = rows[y] for i = 1, #iv, 2 do for x = iv[i], iv[i + 1] do s[x] = true end end if cutx[y] then s[cutx[y]] = nil end end sets[y] = s end return s end local out = {} for j = 1, h do local y = oy + j - 1 local iv = rows[y] if rim then local r, up = set(y), set(y - 1) local rv = {} for i = 1, w do local x = ox + i - 1 local c = 0 if r and r[x] then c = 2 if not (up and up[x]) or not r[x + 1] then c = 1 end end rv[i] = c end out[j] = char(unpack(rv, 1, w)) elseif not iv then out[j] = rep("\0", w) else cur = iv local order = {} for i = 1, #iv, 2 do order[#order + 1] = i end table.sort(order, before) local parts, pos = {}, ox for _, i in ipairs(order) do local a, b = iv[i], iv[i + 1] if b >= pos then if a < pos then a = pos end if a > pos then parts[#parts + 1] = rep("\0", a - pos) end parts[#parts + 1] = rep("\2", b - a + 1) pos = b + 1 end end parts[#parts + 1] = rep("\0", ox + w - pos) local row = concat(parts) local cx = cutx[y] if cx then row = row:sub(1, cx - ox) .. "\0" .. row:sub(cx - ox + 2) end out[j] = row end end return { s = concat(out), w = w, h = h, ox = ox, oy = oy } end local ARM1 = { cap(5, 28, 6, 0, 1, 4.4) } local ARM2 = { cap(-11, 28, 5.5, -0.5, 0, 3.2), cap(12, 28, 5.5, 1, 0, 3.2) } local function dog(open) local p = { ell(0, -4, 6.5, 6), ell(-1, -8, 5.5, 4.5), cap(3, -9, 1.8, 6, -14, 1.2), cap(-4, -7, 2.6, -13, -6, 1.9), ell(-14, -6, 1.6, 1.6), cut(-5, -8), } if open then p[#p + 1] = cap(-3, -2, 2.2, -10, 3, 1.3) p[#p + 1] = cap(-17, -7, 0.6, -20, -9, 0.6) p[#p + 1] = cap(-17, -4, 0.6, -21, -4, 0.6) p[#p + 1] = cap(-17, -1, 0.6, -20, 1, 0.6) else p[#p + 1] = cap(-3, -2, 2.2, -11, -3, 1.3) end return p end local function rabbit(ears) local p = { ell(0, -4, 6, 5.5), cap(-4, -4, 3, -9, -3, 2), cut(-4, -6) } if ears == 1 then p[#p + 1] = cap(1, -8, 2, -2, -18, 1.4) p[#p + 1] = cap(-2, -18, 1.4, -9, -22, 1) else p[#p + 1] = cap(1, -8, 2, -1, -24, 1.3) end if ears == 2 then p[#p + 1] = cap(4, -7, 2, 5, -17, 1.4) p[#p + 1] = cap(5, -17, 1.4, 11, -20, 1) else p[#p + 1] = cap(4, -7, 2, 5, -23, 1.3) end return p end local WING = { { { 1, -5, 2.8, 10, -20, 0.8 }, { 0, -5, 2.6, 4, -21, 0.7 } }, { { 1, -5, 2.8, 14, -9, 0.8 }, { 0, -5, 2.6, 13, -5, 0.7 } }, { { 1, -4, 2.8, 11, 6, 0.8 }, { 0, -4, 2.6, 6, 9, 0.7 } }, } local function bird(w) local p = { ell(0, -3, 6, 4.5), cap(-4, -6, 2.8, -8, -10, 2.2), cap(-9, -10, 1.2, -14, -9, 0.6), cap(5, -1, 2, 12, 3, 1), cut(-8, -10), } for i = 1, 2 do p[#p + 1] = WING[w][i] end return p end local function kid(hx, hy, hr, sx, sy, srx, sry) return { ell(hx, hy, hr, hr * 1.05), cap(hx, hy + hr * 0.6, 2.2, sx, sy - sry + 1, 3), ell(sx, sy, srx, sry) } end local POSE = {} local JOBS = {} local function job(name, prims, rim, S) JOBS[#JOBS + 1] = function() POSE[name] = raster(prims, rim, S) end end local WALL_ROWS, WALL = {}, nil for s = 0, 3 do JOBS[#JOBS + 1] = function() for y = s * 16, s * 16 + 15 do local dy2 = (y - LY) ^ 2 local r = {} for x = 0, W - 1 do r[x + 1] = min(NPAL - 1, floor(sqrt((x - LX) ^ 2 + dy2)) + 1) end WALL_ROWS[y + 1] = char(unpack(r, 1, W)) end if s == 3 then WALL = concat(WALL_ROWS) end end end job("k1", kid(7, 52, 4.2, 7, 70, 9.5, 13), true) job("k2a", kid(20, 56, 3.5, 20, 68, 7.5, 9), true) job("k2b", kid(22, 57, 3.5, 20, 68, 7.5, 9), true) job("k3a", kid(57, 53, 4.2, 57, 69, 9.5, 12), true) job("k3b", kid(55, 54, 4.2, 57, 69, 9.5, 12), true) local S = 1.6 job("arm1", ARM1, false, S) job("arm2", ARM2, false, S) job("dog0", dog(false), false, S); job("dog1", dog(true), false, S) for e = 0, 2 do job("rab" .. e, rabbit(e), false, S) end for w = 1, 3 do job("bird" .. w, bird(w), false, S) end local PAL_SH = { 0, 0, 0, 70, 34, 14, 70, 34, 14 } local PAL_KID = { 0, 0, 0, 92, 48, 20, 22, 10, 6 } local function put(name, x, y, pal) local p = POSE[name] if p then blit_indexed(p.s, pal or PAL_SH, x + p.ox, y + p.oy, p.w, p.h, p.w, p.h, true) end end local RISE, DROP, GAP, WY = 26, 60, 36, 44 local function smooth(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local function lift(tl, len) if tl < RISE then return floor(DROP * (1 - smooth(tl / RISE)) + 0.5) end if tl > len - RISE then return floor(DROP * smooth((tl - (len - RISE)) / RISE) + 0.5) end return 0 end local function scene_dog(tl) local y = WY + lift(tl, 150) local a = tl - RISE local open = a >= 18 and a < 90 and (a - 18) % 14 < 6 put("arm1", 40, y) put(open and "dog1" or "dog0", 40, open and y - 1 or y) end local EARS = "0001110002220001122002100" local function scene_rabbit(tl) local y = WY + lift(tl, 170) local i = max(0, min(#EARS - 1, (tl - RISE) // 6)) put("arm1", 34, y) put("rab" .. EARS:sub(i + 1, i + 1), 34, y) end local TAKEOFF, FLIGHT = 104, 92 local FLAP = { 1, 2, 3, 2 } local function scene_bird(tl) local w = FLAP[(tl // 4) % 4 + 1] if tl < TAKEOFF then local y = WY + lift(tl, 10000) put("arm2", 40, y) put("bird" .. w, 40, y - (w == 3 and 1 or 0)) else local u = (tl - TAKEOFF) / FLIGHT local y = WY + floor(DROP * smooth((tl - TAKEOFF) / RISE) + 0.5) if y < WY + DROP then put("arm2", 40, y) end put("bird" .. w, floor(40 - 64 * u ^ 1.3 + 0.5), floor(WY - 34 * u - (w == 3 and 1 or 0) + 0.5)) end end local SCENES = { dog = { scene_dog, 150 }, rabbit = { scene_rabbit, 170 }, bird = { scene_bird, TAKEOFF + FLIGHT } } local ORDER = { "dog", "rabbit", "bird" } local SEQ, PERIOD = {}, 1 function on_settings() LIGHT = LIGHTS[cfg.light] or LIGHTS.amber PALS = {} SEQ, PERIOD = {}, 0 for _, n in ipairs(ORDER) do if cfg.show == "all" or cfg.show == n then SEQ[#SEQ + 1] = { SCENES[n][1], PERIOD + GAP, PERIOD + GAP + SCENES[n][2] } PERIOD = PERIOD + GAP + SCENES[n][2] end end PERIOD = PERIOD + GAP end local METAL, METAL_L = { 40, 24, 14 }, { 96, 62, 30 } local function lantern(fr) px(56, 0, METAL[1], METAL[2], METAL[3]); px(56, 1, METAL[1], METAL[2], METAL[3]) rect(55, 2, 3, 1, METAL_L[1], METAL_L[2], METAL_L[3]) rect(53, 3, 7, 1, METAL[1], METAL[2], METAL[3]) rect(53, 4, 1, 8, METAL[1], METAL[2], METAL[3]); rect(59, 4, 1, 8, METAL[1], METAL[2], METAL[3]) rect(54, 4, 5, 8, 255, 226, 140) local hot = (fr // 2) % 3 rect(55, 6 + (hot == 1 and 1 or 0), 3, 5 - (hot == 1 and 1 or 0), 255, 250, 206) rect(56, 5 + (hot == 2 and 1 or 0), 1, 3, 255, 255, 240) rect(53, 12, 7, 1, METAL[1], METAL[2], METAL[3]) end local NJ = 0 function render() local fr = frame NJ = NJ + 1 if JOBS[NJ] then JOBS[NJ]() end gfx.ready(JOBS[NJ + 1] == nil) local v = sin(fr * 0.23) + 0.8 * sin(fr * 0.61 + 1.7) + 0.6 * sin(fr * 0.097 + 0.5) local lv = max(0, min(4, floor((v + 2.4) / 4.8 * 5))) if WALL then blit_indexed(WALL, wall_pal(lv), 0, 0, W, H, W, H) else rect(0, 0, W, H, 150, 90, 40) end lantern(fr) local t = fr % PERIOD for i = 1, #SEQ do local s = SEQ[i] if t >= s[2] and t < s[3] then s[1](t - s[2]) end end put("k1", 0, 0, PAL_KID) put((fr + 200) % 390 < 70 and "k2b" or "k2a", 0, 0, PAL_KID) put((fr + 90) % 520 < 60 and "k3b" or "k3a", 0, 0, PAL_KID) end