info = { type = "image", title = "Sunbeam cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fat cat follows a sun patch across the room", api = 33, } local tr = ({ sk = { ["Day length"] = "Dĺžka dňa", ["Snore"] = "Chrápanie", ["long"] = "dlhý", ["normal"] = "bežný", ["short"] = "krátky" }, cs = { ["Day length"] = "Délka dne", ["Snore"] = "Chrápání", ["long"] = "dlouhý", ["normal"] = "běžný", ["short"] = "krátký" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "day", label = T("Day length"), type = "int", default = "normal", opts = { keys = { "long", "normal", "short" }, T("long"), T("normal"), T("short") } }, { key = "zzz", label = T("Snore"), type = "bool", default = true }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local floor, sin, sqrt, max, min = math.floor, math.sin, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PI = 3.14159 local function sstep(a, b, t) local u = (t - a) / (b - a) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local KF = { { 0.00, 28, 34, 66, 26, 24, 44, .40, .44, .70, 14, 18, 48 }, { 0.06, 150, 118, 118, 110, 84, 76, .78, .70, .76, 240, 160, 130 }, { 0.20, 196, 160, 134, 146, 104, 70, .90, .86, .82, 150, 200, 240 }, { 0.50, 206, 172, 142, 156, 112, 74, .96, .92, .86, 130, 196, 250 }, { 0.75, 200, 150, 112, 146, 96, 58, .95, .82, .70, 250, 190, 110 }, { 0.84, 110, 86, 112, 80, 60, 76, .60, .55, .70, 180, 100, 120 }, { 0.92, 28, 34, 66, 26, 24, 44, .40, .44, .70, 14, 18, 48 }, { 1.00, 28, 34, 66, 26, 24, 44, .40, .44, .70, 14, 18, 48 }, } local CUR = {} local function daylight(u) local i = 1 while KF[i + 1][1] < u do i = i + 1 end local a, b = KF[i], KF[i + 1] local t = (u - a[1]) / (b[1] - a[1]) for k = 2, 13 do CUR[k - 1] = a[k] + (b[k] - a[k]) * t end end local AR, AG, AB = 1, 1, 1 local function A(r, g, b) return min(255, floor(r * AR + 0.5)), min(255, floor(g * AG + 0.5)), min(255, floor(b * AB + 0.5)) end local PY0, PY1, SKEW = 30, 62, 9 local function lightbar(x, y, w, h, r, g, b, a) local ix = floor(x) local f = x - ix if ix + w < 0 or ix > W then return end if w > 1 then blend_rect(max(ix + 1, 0), y, min(ix + w, W) - max(ix + 1, 0), h, r, g, b, a) end if ix >= 0 and ix < W then blend_rect(ix, y, 1, h, r, g, b, a * (1 - f)) end if ix + w >= 0 and ix + w < W and f > 0.04 then blend_rect(ix + w, y, 1, h, r, g, b, a * f) end end local function xl(p, y) return p + SKEW * (y - PY0) / (PY1 - PY0) end local function patch(p, a, r, g, b) if a <= 0.01 then return end for blk = 1, 2 do local y, y1 = (blk == 1) and PY0 or 47, (blk == 1) and 44 or PY1 while y <= y1 do local h = min(3, y1 - y + 1) local x = xl(p, y + 1) lightbar(x, y, 10, h, r, g, b, a) lightbar(x + 12, y, 10, h, r, g, b, a) y = y + h end end end local POSE = { [0] = { b = { 0, -5, 9, 5 }, h = { 9, -4, 5, 4 }, paw = { 11, -1, 3, 1 }, tail = { -10, -3, 2, 3 } }, [1] = { b = { 3, -4, 11, 4 }, h = { 16, -6, 4, 4 }, paw = { 21, -1, 3, 1 }, tail = { -9, -3, 2, 2 } }, [2] = { b = { 0, -6, 8, 6 }, h = { 8, -5, 5, 4 }, paw = { 11, -1, 3, 1 }, tail = { -9, -4, 2, 3 } }, } local function oell(cx, cy, rx, ry, r, g, b) for dy = -ry, ry do local k = 1 - (dy / (ry + 0.6)) ^ 2 local hw = floor(rx * sqrt(k > 0 and k or 0) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b) end end local YG = 58 local function cat(ax, pose, awake) local P = POSE[pose] local wr, wg, wb = A(250, 247, 240) local sr, sg, sb = A(206, 204, 224) local pr, pg, pb = A(240, 150, 164) local dr, dg, db = A(70, 56, 66) blend_rect(ax - 10, YG - 1, 22, 2, 20, 14, 24, 0.28) local t = P.tail oell(ax + t[1], YG + t[2], t[3], t[4], sr, sg, sb) local b = P.b oell(ax + b[1], YG + b[2] + 1, b[3], b[4], sr, sg, sb) oell(ax + b[1], YG + b[2], b[3], b[4], wr, wg, wb) local pw = P.paw oell(ax + pw[1], YG + pw[2], pw[3], pw[4], wr, wg, wb) local h = P.h local hx, hy, rx, ry = ax + h[1], YG + h[2], h[3], h[4] oell(hx, hy + 1, rx, ry, sr, sg, sb) oell(hx, hy, rx, ry, wr, wg, wb) rect(hx - 3, hy - ry - 1, 2, 2, wr, wg, wb) rect(hx + 1, hy - ry - 1, 2, 2, wr, wg, wb) px(hx - 2, hy - ry, pr, pg, pb) px(hx + 2, hy - ry, pr, pg, pb) local e1, e2 = hx + rx - 5, hx + rx - 2 if awake then rect(e1, hy - 1, 1, 2, dr, dg, db); rect(e2, hy - 1, 1, 2, dr, dg, db) else rect(e1, hy, 2, 1, dr, dg, db); rect(e2, hy, 2, 1, dr, dg, db) end px(hx + rx - 1, hy + 1, pr, pg, pb) end local CYC, ZZZ = 2700, true function on_settings() local d = cfg.day CYC = d == "long" and 4500 or d == "short" and 1350 or 2700 ZZZ = cfg.zzz ~= false end local CRAWL = 54 local C0, C1, STEPS = 11, 49, 5 function render() local u = ((frame + floor(0.14 * CYC)) % CYC) / CYC daylight(u) AR, AG, AB = CUR[7], CUR[8], CUR[9] local night = (u >= 0.84) and sstep(0.84, 0.92, u) or (1 - sstep(0, 0.06, u)) rect(0, 0, W, 43, floor(CUR[1]), floor(CUR[2]), floor(CUR[3])) rect(0, 43, W, 2, floor(CUR[1] * 0.74), floor(CUR[2] * 0.74), floor(CUR[3] * 0.74)) rect(0, 45, W, H - 45, floor(CUR[4]), floor(CUR[5]), floor(CUR[6])) for _, y in ipairs({ 49, 54, 59 }) do rect(0, y, W, 1, floor(CUR[4] * 0.86), floor(CUR[5] * 0.86), floor(CUR[6] * 0.86)) end rect(5, 51, 54, 10, A(120, 52, 58)) rect(7, 53, 50, 6, A(160, 76, 70)) rect(4, 6, 19, 24, A(122, 86, 60)) rect(6, 8, 15, 20, floor(CUR[10]), floor(CUR[11]), floor(CUR[12])) rect(13, 8, 1, 20, A(122, 86, 60)) rect(6, 17, 15, 1, A(122, 86, 60)) if night > 0.2 then local a = night blend_rect(15, 10, 4, 4, 240, 240, 210, 0.9 * a) blend(8, 11, 255, 255, 255, a); blend(10, 22, 255, 255, 255, a); blend(18, 21, 255, 255, 255, a) end rect(2, 4, 3, 28, A(150, 70, 80)) rect(3, 30, 21, 2, A(140, 100, 70)) rect(37, 9, 14, 12, A(120, 86, 60)) rect(39, 11, 10, 8, A(176, 196, 170)) rect(39, 16, 10, 3, A(120, 160, 120)) rect(54, 39, 7, 6, A(176, 92, 58)) oell(57, 36, 4, 3, A(60, 128, 70)) oell(56, 32, 2, 3, A(80, 150, 80)) oell(59, 33, 2, 3, A(70, 140, 74)) local prog = max(0, min(1, (u - 0.06) / 0.74)) local C = C0 + (C1 - C0) * prog local m = (C >= 18) and min(STEPS, floor((C - 18) / 7) + 1) or 0 local ax, pose, awake = 12, 0, false if m > 0 then local j = m - 1 local uT = 0.06 + 0.74 * ((18 + 7 * j - C0) / (C1 - C0)) ax = 12 + 7 * m local q = ((u - uT) * CYC) / CRAWL if q >= 0 and q < 1 then awake = true local x0 = 12 + 7 * j if q < 0.15 then pose, ax = 1, x0 elseif q < 0.35 then pose, ax = 2, x0 + 2 elseif q < 0.55 then pose, ax = 1, x0 + 3.5 elseif q < 0.8 then pose, ax = 2, x0 + 5.5 else pose, ax = 0, x0 + 7 end end end ax = floor(ax + 0.5) cat(ax, pose, awake) local pa = 0.5 * sstep(0.05, 0.12, u) * (1 - sstep(0.76, 0.86, u)) local gold = sstep(0.45, 0.8, u) local lr, lg, lb = 255, floor(236 - 50 * gold), floor(176 - 70 * gold) local p = C - 17 patch(p, pa, lr, lg, lb) for k = 1, 6 do local h = hash32(k * 0x9E3779B1) local y = 34 + umod(h >> 8, 24) + 2 * sin(frame * 0.02 + k) local x = xl(p, y) + 1 + umod(h, 20) + 1.5 * sin(frame * 0.017 + k * 2) blend(floor(x), floor(y), 255, 244, 200, pa * 1.1 * (0.6 + 0.4 * sin(frame * 0.05 + k))) end if ZZZ and not awake and night < 0.5 then local tz = (frame % 90) / 90 local hx = ax + POSE[pose].h[1] local a = sin(PI * tz) * 0.85 gfx.text(hx + 2 + floor(3 * sin(tz * 5)), YG + POSE[pose].h[2] - 7 - floor(9 * tz), "z", 255, 255, 255, { font = "3x5", alpha = a }) end end