info = { type = "image", title = "Lantern walker", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hooded wanderer lights a dark forest path", api = 33, } local tr = ({ sk = { ["Walking speed"] = "Rýchlosť chôdze", ["Light"] = "Svetlo", ["Slow"] = "Pomaly", ["Normal"] = "Bežne", ["Brisk"] = "Svižne", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, cs = { ["Walking speed"] = "Rychlost chůze", ["Light"] = "Světlo", ["Slow"] = "Pomalu", ["Normal"] = "Běžně", ["Brisk"] = "Svižně", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Walking speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "brisk" }, T("Slow"), T("Normal"), T("Brisk") } }, { key = "light", label = T("Light"), type = "int", default = "warm", opts = { keys = { "warm", "cold" }, T("Warm"), T("Cold") } }, } local rect, px, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend_mask local floor, sin, cos, exp, sqrt, abs, min = math.floor, math.sin, math.cos, math.exp, math.sqrt, math.abs, math.min local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local LIGHTS = { warm = { 255, 190, 100, 255, 226, 130 }, cold = { 150, 205, 255, 210, 240, 255 } } local LR, LG, LB, FR, FG, FB = 255, 190, 100, 255, 226, 130 local function lit(r, g, b) local k = 1.8 / 255 return floor(min(255, r * LR * k)), floor(min(255, g * LG * k)), floor(min(255, b * LB * k)) end local DR, DG, DB, DA = 8, 24, 32, 0.88 local R0, KH, CR = 22, 47, 23 local TAU_S = 1.4 local FPP = 3 local TL, KW, LAM = 42, 62, 14 local PERIOD = 130 local FIG_Y = 41 local BG = nil local function build_scene() rect(0, 0, W, H, 0, 0, 0) for y = 53, 63 do local t = (y - 53) / 10 rect(0, y, W, 1, lit(floor(88 - 30 * t), floor(92 - 32 * t), floor(48 - 16 * t))) end rect(0, 52, W, 1, lit(120, 126, 70)) local TR = { { 14, 3, 1 }, { 22, 3, 1 }, { 44, 3, 1 }, { 50, 3, 1 }, { 28, 6, 2 }, { 0, 7, 2 }, { 56, 8, 2 } } for i = 1, #TR do local x, w, s = TR[i][1], TR[i][2], TR[i][3] local br, bg, bb = 86, 64, 44 if s == 2 then br, bg, bb = 74, 54, 38 end rect(x, 0, w, 54, lit(br, bg, bb)) rect(x, 0, 1, 54, lit(br + 36, bg + 30, bb + 20)) if w > 3 then rect(x + w - 2, 0, 2, 54, lit(br - 26, bg - 20, bb - 14)) for k = 0, 3 do local h = hash32(i * 977 + k * 131) & 0x7FFF rect(x + 1 + h % (w - 2), 6 + (h >> 4) % 40, 1, 5 + h % 7, lit(br - 30, bg - 24, bb - 16)) end end rect(x - 1, 51, w + 2, 3, lit(br - 8, bg - 6, bb - 4)) end for x = 0, W - 1, 4 do local h = hash32(x * 7919 + 3) & 0x7FFF local d = 4 + h % 7 rect(x, 0, 4, d, lit(44, 88, 54)) rect(x + 1, d, 2, 1 + (h >> 5) % 3, lit(58, 112, 64)) end rect(30, 40, 12, 2, lit(82, 60, 42)) rect(33, 31, 6, 9, lit(104, 82, 58)) rect(34, 34, 4, 5, lit(160, 132, 96)) rect(33, 30, 1, 2, lit(104, 82, 58)); rect(38, 30, 1, 2, lit(104, 82, 58)) rect(34, 39, 1, 2, lit(210, 170, 90)); rect(37, 39, 1, 2, lit(210, 170, 90)) local fx = { 7, 19, 38, 48, 61 } for i = 1, #fx do local x = fx[i] local gr = { lit(70, 144, 72) } for k = 1, 4 do px(x - k, 56 - (k > 2 and 2 or 1) - (k > 3 and 1 or 0) + (k == 4 and 1 or 0), gr[1], gr[2], gr[3]) px(x + k, 56 - (k > 2 and 2 or 1) - (k > 3 and 1 or 0) + (k == 4 and 1 or 0), gr[1], gr[2], gr[3]) end rect(x, 52, 1, 5, gr[1], gr[2], gr[3]) px(x - 1, 54, lit(112, 184, 92)); px(x + 1, 53, lit(112, 184, 92)) end local mx = { { 15, 3 }, { 45, 4 }, { 57, 3 } } for i = 1, #mx do local x, h = mx[i][1], mx[i][2] rect(x, 59 - h + 1, 1, h, lit(236, 226, 196)) rect(x - 2, 59 - h, 5, 1, lit(214, 70, 54)) rect(x - 1, 59 - h - 1, 3, 1, lit(214, 70, 54)) px(x - 1, 59 - h, lit(250, 244, 230)); px(x + 1, 59 - h - 1, lit(250, 244, 230)) end BG = gfx.snapshot(0, 0, W, H) end local KERN, KROWS = {}, {} local function build_kernel(k, part) local rows = KROWS[k] or {} KROWS[k] = rows local Rk = R0 + k - 2 local ex = {} for d = 1, TL do ex[d] = exp(-d / LAM) end local r0, r1 = part * 16, part == 2 and KH - 1 or part * 16 + 15 for r = r0, r1 do local dy = r - CR local ady = abs(dy) local strip = 1 - ady / Rk if strip < 0 then strip = 0 end local row = {} for c = 0, KW - 1 do local dx = c - TL local l = 0 if dx > -Rk then l = 1 - sqrt(dx * dx + dy * dy) / Rk if l < 0 then l = 0 end end if dx < 0 then local tl = strip * ex[-dx] if tl > l then l = tl end end row[c + 1] = floor(255 * DA * (1 - l) + 0.5) end rows[r + 1] = string.char(table.unpack(row, 1, KW)) end if part == 2 then KERN[k] = table.concat(rows); KROWS[k] = nil end end local BASE = { "....dddd......", "..ddccccdd....", ".dccccccccd...", ".dcccccckkkd..", ".dccccckekkd..", ".dcccccckkcd..", "..dccccccccd..", "..dcccccccccc.", "..dcccccccchh.", "..dcccccccc...", ".dccccccccccm.", ".dcccccccccccm", "dccccccccccccm", "dccccccccccccm" } local FEET = { { "dccdcccdcccdc.", "..bb......bb..", "..bbb....bbb.." }, { "dcdccdccdcdcc.", "....bb.bb.....", ".....bbb......" }, { "dccdcccdcccdc.", "...bb....bb...", "...bbb..bbb..." }, { "dcdccdccdcdcc.", "....bb.bb.....", "......bbb....." }, } local POSES = {} local function build_pose(p) local pal = { d = { 44, 34, 28 }, c = { 88, 70, 52 }, k = { 8, 10, 12 }, e = { 8, 10, 12 }, m = { 124, 98, 70 }, h = { lit(176, 132, 96) }, b = { 20, 18, 18 }, } rect(0, 0, 14, 17, 255, 0, 255) local rows = {} for r = 1, 14 do rows[r] = BASE[r] end for r = 1, 3 do rows[14 + r] = FEET[p][r] end for r = 1, 17 do local s = rows[r] for c = 1, #s do local col = pal[s:sub(c, c)] if col then px(c - 1, r - 1, col[1], col[2], col[3]) end end end POSES[p] = gfx.snapshot(0, 0, 14, 17) end local stage = 1 local STAGES = {} function on_settings() local L = LIGHTS[cfg.light or "warm"] or LIGHTS.warm LR, LG, LB, FR, FG, FB = L[1], L[2], L[3], L[4], L[5], L[6] FPP = ({ slow = 4, normal = 3, brisk = 2 })[cfg.speed or "normal"] or 3 LAM = (FPS / FPP) * TAU_S TL = min(56, math.ceil(3 * LAM)) KW = TL + R0 + 2 PERIOD = 63 + TL + floor(2 * FPS / FPP) BG, KERN, KROWS, POSES = nil, {}, {}, {} STAGES = { build_scene } for k = 1, 3 do for part = 0, 2 do STAGES[#STAGES + 1] = function() build_kernel(k, part) end end end for p = 1, 4 do STAGES[#STAGES + 1] = function() build_pose(p) end end stage = 1 end local function dark(x, y, w, h) if x < 0 then w = w + x; x = 0 end if y < 0 then h = h + y; y = 0 end if x + w > W then w = W - x end if y + h > H then h = H - y end if w > 0 and h > 0 then blend_rect(x, y, w, h, DR, DG, DB, DA) end end function render() if stage <= #STAGES then STAGES[stage](); stage = stage + 1 end if not BG then return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local step = frame // FPP local fx = step % PERIOD - 16 local pi = (fx // 3) % 4 + 1 local bob = (pi == 2 or pi == 4) and 1 or 0 local hx, hy = fx + 12, FIG_Y + 8 + bob local th = 0.5 * sin(TAU * (t - 0.25) / 1.2) - 0.1 local lx, ly = floor(hx + 4 * sin(th) + 0.5), floor(hy + 4 * cos(th) + 0.5) local cy = ly - 3 local v = sin(t * 13) + 0.7 * sin(t * 7.3 + 1) local k = v > 0.9 and 3 or (v < -0.9 and 1 or 2) local kx, ky = lx - TL, cy - CR if KERN[3] and POSES[4] then dark(0, 0, W, ky) dark(0, ky + KH, W, H - ky - KH) dark(0, ky, kx, KH) dark(kx + KW, ky, W - kx - KW, KH) blend_mask(KERN[k], DR, DG, DB, kx, ky, KW, KH, KW, KH, 1) local d = sqrt((lx - 35) * (lx - 35) + (cy - 35) * (cy - 35)) if d < 26 then local a = (26 - d) / 12 if a > 1 then a = 1 end if (frame // 4) % 41 < 2 then blend_rect(34, 35, 2, 1, FR, FG, FB, a); blend_rect(37, 35, 2, 1, FR, FG, FB, a) else blend_rect(34, 34, 2, 3, FR, FG, FB, a); blend_rect(37, 34, 2, 3, FR, FG, FB, a) blend_rect(35, 35, 1, 1, 20, 14, 8, a); blend_rect(37, 35, 1, 1, 20, 14, 8, a) end end gfx.blit_rgb(POSES[pi], fx, FIG_Y + bob, 14, 17, 255, 0, 255) rect(hx, hy + 1, 1, 1, 90, 80, 60) px(floor((hx + lx) / 2 + 0.5), hy + 3, 90, 80, 60) rect(lx - 1, ly + 1, 3, 4, 52, 40, 30) rect(lx, ly + 2, 1, 2, FR, FG, FB) blend_rect(lx - 2, ly + 1, 5, 5, FR, FG, FB, 0.20 + 0.05 * k) else dark(0, 0, W, H) end end