info = { type = "image", title = "Owl on watch", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Night owl watches a mouse run below", api = 33, } local tr = ({ sk = { ["Mouse"] = "Myš", ["Moon"] = "Mesiac", ["Stars"] = "Hviezdy" }, cs = { ["Mouse"] = "Myš", ["Moon"] = "Měsíc", ["Stars"] = "Hvězdy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mouse", label = T("Mouse"), type = "bool", default = true }, { key = "moon", label = T("Moon"), type = "bool", default = true }, { key = "stars", label = T("Stars"), type = "int", min = 0, max = 14, default = 10 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CX, BY, GY = 46, 41, 54 local MX, MY = 14, 14 local LOOP = 450 local BODY, BODY_D = { 128, 86, 52 }, { 92, 62, 38 } local HEAD, HEAD_D = { 150, 104, 62 }, { 108, 74, 44 } local BELLY, BELLY_D = { 226, 176, 128 }, { 188, 138, 98 } local DISC, EYE, PUPIL, BEAK = { 232, 196, 148 }, { 255, 212, 40 }, { 10, 10, 24 }, { 255, 132, 28 } local STAR, STAR_D = { 255, 240, 120 }, { 120, 120, 170 } local function c3(c) return c[1], c[2], c[3] end local function disc(cx, cy, r, c) for dy = -r, r do local dx = floor(sqrt(r * r - dy * dy) + 0.5) rect(cx - dx, cy + dy, 2 * dx + 1, 1, c3(c)) end end local function hr(x, y, w, h, c) local ix = floor(x) local f = x - ix if f < 0.04 then rect(ix, y, w, h, c3(c)) return end if w > 1 then rect(ix + 1, y, w - 1, h, c3(c)) end blend_rect(ix, y, 1, h, c[1], c[2], c[3], 1 - f) blend_rect(ix + w, y, 1, h, c[1], c[2], c[3], f) end local SX, SY, SPH = {}, {}, {} do local k, i = 0, 0 while k < 14 and i < 300 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, GY - 8) local dx, dy = x - MX, y - MY local inowl = x > CX - 12 and x < CX + 12 and y > 12 if dx * dx + dy * dy > 15 * 15 and not inowl then k = k + 1 SX[k], SY[k], SPH[k] = x, y, umod(h >> 16, 628) / 100.0 end end end local BG local function build_bg() for y = 0, GY - 1 do local t = y / (GY - 1) rect(0, y, W, 1, floor(8 + 10 * t + 0.5), floor(14 + 20 * t + 0.5), floor(64 + 52 * t + 0.5)) end if cfg.moon then disc(MX, MY, 11, { 34, 50, 122 }) disc(MX, MY, 9, { 54, 74, 146 }) disc(MX, MY, 6, { 242, 238, 198 }) px(MX - 3, MY - 2, 208, 200, 156); px(MX - 2, MY - 2, 208, 200, 156) px(MX + 2, MY + 1, 208, 200, 156); px(MX - 1, MY + 3, 208, 200, 156) px(MX + 3, MY - 3, 208, 200, 156) end rect(0, GY, W, H - GY, 14, 36, 50) rect(0, GY, W, 1, 38, 88, 84) for i = 1, 9 do local x = umod(hash32(i * 0x85EBCA6B), W) px(x, GY - 1, 38, 88, 84) px(x, GY - 2, 38, 88, 84) end rect(26, BY, 38, 1, 140, 96, 56) rect(22, BY + 1, 42, 2, 104, 70, 40) rect(26, BY + 3, 38, 1, 66, 44, 28) px(33, BY + 2, 66, 44, 28); px(56, BY + 1, 66, 44, 28) px(30, BY - 1, 104, 70, 40); px(29, BY - 2, 104, 70, 40) rect(26, BY - 5, 3, 2, 52, 124, 78); rect(30, BY - 4, 2, 2, 52, 124, 78) rect(CX - 6, 29, 13, 2, c3(BODY)) rect(CX - 7, 31, 14, 7, c3(BODY)) rect(CX - 6, 38, 12, 2, c3(BODY)) rect(CX - 7, 32, 2, 6, c3(BODY_D)); rect(CX + 5, 32, 2, 6, c3(BODY_D)) rect(CX - 4, 31, 8, 7, c3(BELLY)); rect(CX - 3, 38, 6, 1, c3(BELLY)) for _, p in ipairs({ { -2, 33 }, { 1, 34 }, { -3, 35 }, { 2, 36 }, { 0, 37 }, { -1, 32 }, { 3, 32 } }) do px(CX + p[1], p[2], c3(BELLY_D)) end rect(CX - 4, 40, 3, 1, c3(BEAK)); rect(CX + 2, 40, 3, 1, c3(BEAK)) BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end local function head(p, blink, gz, gy) local x0 = CX - 6 + p rect(x0 + 2, 18, 9, 1, c3(HEAD)) rect(x0 + 1, 19, 11, 1, c3(HEAD)) rect(x0, 20, 13, 8, c3(HEAD)) rect(x0 + 1, 28, 11, 1, c3(HEAD_D)) px(x0 + 1, 16, c3(HEAD_D)); rect(x0 + 1, 17, 2, 1, c3(HEAD_D)) px(x0 + 11, 16, c3(HEAD_D)); rect(x0 + 10, 17, 2, 1, c3(HEAD_D)) local dl, dr = 5, 5 if p > 0 then dl = 5 - p elseif p < 0 then dr = 5 + p end local xl = x0 + 1 + (5 - dl) local xr = xl + dl + 1 for k = 1, 2 do local x, dw = k == 1 and xl or xr, k == 1 and dl or dr rect(x, 22, dw, 3, c3(DISC)); rect(x + 1, 21, dw - 2, 5, c3(DISC)) local ex = x + (dw - 3) // 2 if blink then rect(ex, 23, 3, 1, c3(HEAD_D)) else rect(ex, 22, 3, 3, c3(EYE)) local ppx = min(ex + 2, max(ex, ex + 1 + gz)) rect(ppx, 22 + gy, 1, 2, c3(PUPIL)) end end rect(xl + dl, 25, 1, 2, c3(BEAK)) end local function mouse_x(t) if t < 150 or t > 312 then return nil end if t < 215 then return -9 + 0.55 * (t - 150) end if t < 245 then return 26.75 end return 26.75 + 0.7 * (t - 245) end local function pose(t, mx) if mx then return max(-2, min(2, floor((mx - 40) / 14 + 0.5))), 0, 1 end if t < 70 then return 0, 0, 0 end if t < 76 then return -1, -1, 0 end if t < 124 then return -2, -1, 0 end if t < 132 then return -1, 0, 0 end if t < 150 then return 0, 0, 0 end if t < 340 then return 2, 0, 1 end if t < 352 then return 1, 0, 0 end return 0, 0, 0 end local function blinking(t) return (t >= 44 and t < 48) or (t >= 172 and t < 176) or (t >= 388 and t < 392) end local function mouse(x, t, running) local ty = 56 local c, pink = { 178, 170, 162 }, { 226, 144, 144 } hr(x, ty + 2, 5, 2, c) hr(x + 5, ty + 1, 3, 3, c) hr(x + 5, ty, 1, 1, pink) hr(x + 6, ty + 1, 1, 1, { 10, 10, 20 }) hr(x + 7, ty + 2, 1, 1, pink) local w = running and (t // 4) % 2 or (t // 8) % 2 hr(x - 1, ty + 3 - w, 1, 1, pink); hr(x - 2, ty + 3 - w, 1, 1, pink); hr(x - 3, ty + 2 + w - 1, 1, 1, pink) if running then if w == 0 then hr(x + 1, ty + 4, 1, 1, c); hr(x + 4, ty + 4, 1, 1, c) else hr(x + 2, ty + 4, 1, 1, c); hr(x + 5, ty + 4, 1, 1, c) end else hr(x + 1, ty + 4, 1, 1, c); hr(x + 5, ty + 4, 1, 1, c) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame % LOOP local ns = cfg.stars or 10 for k = 1, min(ns, #SX) do local v = sin(frame * 0.07 + SPH[k] * 3) local c = v > -0.2 and STAR or STAR_D px(SX[k], SY[k], c[1], c[2], c[3]) end for k = 1, 2 do local a = 0.45 + 0.55 * (0.5 + 0.5 * sin(frame * 0.09 + k * 2.1)) local sx, sy = k == 1 and 56 or 6, k == 1 and 6 or 34 blend_rect(sx - 2, sy, 5, 1, STAR[1], STAR[2], STAR[3], a) blend_rect(sx, sy - 2, 1, 5, STAR[1], STAR[2], STAR[3], a) end local mx = cfg.mouse and mouse_x(t) or nil local p, gz, gy = pose(cfg.mouse and t or t % 150, mx) head(p, blinking(t), gz, gy) if mx then mouse(mx, frame, t < 215 or t >= 245) end end