info = { type = "image", title = "Owl hollow", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "An owl peeks out of a hollow in an old tree", api = 33, } local tr = ({ sk = { ["Owl"] = "Sova", ["brown"] = "hnedá", ["grey"] = "sivá", ["snowy"] = "zasnežené", ["Sky"] = "Obloha", ["day"] = "deň", ["evening"] = "večer" }, cs = { ["Owl"] = "Sova", ["brown"] = "hnědá", ["grey"] = "šedá", ["snowy"] = "zasněžené", ["Sky"] = "Obloha", ["day"] = "den", ["evening"] = "večer" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "owl", label = T("Owl"), type = "int", default = 0, opts = { T("brown"), T("grey"), T("snowy") } }, { key = "sky", label = T("Sky"), type = "int", default = 0, opts = { T("day"), T("evening") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin = math.floor, math.sin local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local KR, KG, KB = 255, 0, 255 local function c3(c) return c[1], c[2], c[3] end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function lerp3(a, b, u) return floor(a[1] + (b[1] - a[1]) * u + 0.5), floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5) end local function runs(rows, pal) local S = {} for r = 1, #rows do local s, out, k, n = rows[r], {}, 1, #rows[r] while k <= n do local ch, e = s:sub(k, k), k while e < n and s:sub(e + 1, e + 1) == ch do e = e + 1 end if ch ~= "." then out[#out + 1] = { k - 1, e - k + 1, pal[ch] } end k = e + 1 end S[r] = out end return S end local function sprite(S, x, y) local ix = floor(x) local f = x - ix for r = 1, #S do local row, yy, m = S[r], y + r - 1, #S[r] for k = 1, m do local e = row[k] local a, w, c = ix + e[1], e[2], e[3] if k == 1 then blend_rect(a, yy, 1, 1, c[1], c[2], c[3], 1 - f) if w > 1 then rect(a + 1, yy, w - 1, 1, c[1], c[2], c[3]) end else rect(a, yy, w, 1, c[1], c[2], c[3]) end if k == m and f > 0.03 then blend_rect(a + w, yy, 1, 1, c[1], c[2], c[3], f) end end end end local SKIES = { [0] = { stops = { { 0, { 72, 146, 226 } }, { 57, { 176, 216, 246 } } }, hill = { 118, 176, 136 }, grass = { 84, 162, 64 }, grassl = { 130, 204, 90 }, cw = { 250, 252, 255 }, cs = { 204, 220, 240 } }, [1] = { stops = { { 0, { 64, 74, 158 } }, { 34, { 190, 112, 150 } }, { 57, { 250, 170, 112 } } }, hill = { 132, 104, 132 }, grass = { 76, 112, 60 }, grassl = { 120, 150, 76 }, cw = { 255, 214, 200 }, cs = { 222, 150, 162 } }, } local OWLS = { [0] = { b = { 150, 104, 66 }, f = { 222, 190, 146 }, c = { 196, 160, 118 }, y = { 255, 206, 50 }, k = { 30, 20, 20 }, o = { 240, 150, 50 } }, [1] = { b = { 128, 128, 138 }, f = { 216, 214, 206 }, c = { 176, 176, 182 }, y = { 255, 200, 60 }, k = { 30, 20, 20 }, o = { 240, 150, 50 } }, [2] = { b = { 236, 236, 242 }, f = { 255, 255, 255 }, c = { 124, 124, 136 }, y = { 255, 214, 60 }, k = { 30, 20, 20 }, o = { 70, 70, 76 } }, } local BARK, BARK_D, BARK_L = { 124, 84, 58 }, { 90, 60, 44 }, { 156, 108, 72 } local GROOVE, RIM_L, HOLE = { 78, 52, 38 }, { 178, 128, 88 }, { 46, 30, 26 } local LEAF, LEAF_L = { 74, 160, 64 }, { 140, 210, 96 } local OWL_BASE = { "..bbbb..", ".bbbbbb.", "bbbbbbbb", "bffbbffb", "fyyffyyf", "fykffkyf", "bffooffb", "bbffffbb", "bcbbbbcb", "bbcbbcbb" } local OWL_VAR = { front = {}, left = { [6] = "fkyffkyf", [7] = "bfoofffb" }, right = { [6] = "fykffykf", [7] = "bfffoofb" }, blink = { [5] = "ffffffff", [6] = "fkkffkkf" }, back = { [4] = "bbbbbbbb", [5] = "bbbbbbbb", [6] = "bbbbbbbb", [7] = "bbbbbbbb" }, } local HCX, HY0 = 27, 24 local HW = { 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 } local UP, DOWN = 26, 36 local function trunk_l(y) return 13 - (y > 50 and (y - 50) // 2 or 0) + floor(0.8 * sin(y * 0.21)) end local function trunk_r(y) return 41 + (y > 50 and (y - 50) * 2 // 3 or 0) + floor(0.8 * sin(y * 0.17 + 1)) end local CLOUD_ART = { { ".....wwww.......", "...wwwwwwww.www.", ".wwwwwwwwwwwwwww", "wwwwwwwwwwwwwwww", ".ssssssssssssss." }, { "..www...", ".wwwwww.", "wwwwwwww", ".ssssss." }, { "...wwww.....", ".wwwwwwwww..", "wwwwwwwwwwww", ".ssssssssss." }, } local CLOUDS = { { 1, 4, 0.055, 40 }, { 2, 13, 0.085, 10 }, { 3, 30, 0.04, 30 } } local SK, CS, OWL = SKIES[0], {}, {} local BG, FG = nil, nil local READY = false local function apply() READY = true local C = cfg or {} SK = SKIES[C.sky or 0] or SKIES[0] local pal = OWLS[C.owl or 0] or OWLS[0] for name, v in pairs(OWL_VAR) do local rows = {} for r = 1, #OWL_BASE do rows[r] = v[r] or OWL_BASE[r] end OWL[name] = runs(rows, pal) end for i = 1, #CLOUD_ART do CS[i] = runs(CLOUD_ART[i], { w = SK.cw, s = SK.cs }) end BG, FG = nil, nil end function on_settings() apply() end local function build_bg() local st = SK.stops for i = 1, #st - 1 do local a, b = st[i], st[i + 1] for y = a[1], b[1] do rect(0, y, W, 1, lerp3(a[2], b[2], (y - a[1]) / (b[1] - a[1]))) end end for x = 0, W - 1 do local hy = 50 + floor(2 * sin(x * 0.09 + 1) + 1.2 * sin(x * 0.23) + 0.5) rect(x, hy, 1, 58 - hy, c3(SK.hill)) end rect(0, 58, W, H - 58, c3(SK.grass)) rect(0, 58, W, 1, c3(SK.grassl)) BG = gfx.snapshot(0, 0, W, H) end local function build_fg() rect(0, 0, W, H, KR, KG, KB) for y = 0, H - 1 do local l, r = trunk_l(y), trunk_r(y) rect(l, y, r - l + 1, 1, c3(BARK)) rect(l, y, 3, 1, c3(BARK_D)) rect(r - 4, y, 4, 1, c3(BARK_L)) end rect(4, 60, 6, 4, c3(BARK_D)); rect(48, 59, 7, 5, c3(BARK)); rect(53, 61, 4, 3, c3(BARK_D)) for k = 1, 30 do local h = hash32(k * 0x27D4EB2F) local y = (h & 0x7FFF) % 58 local l, r = trunk_l(y), trunk_r(y) local x = l + 3 + ((h >> 15) & 0x7FFF) % (r - l - 6) local len = 3 + (h >> 28 & 7) if not (x >= HCX - 7 and x <= HCX + 6 and y + len >= HY0 - 2 and y <= HY0 + 13) then rect(x, y, 1, len, c3(GROOVE)) end end for i = 1, #HW do local y, hw = HY0 + i - 1, HW[i] rect(HCX - hw - 1, y, 2 * hw + 2, 1, c3(GROOVE)) rect(HCX - hw, y, 2 * hw, 1, KR, KG, KB) end rect(HCX - 2, HY0 - 1, 4, 1, c3(GROOVE)) rect(HCX - 5, HY0 + #HW, 10, 1, c3(RIM_L)) rect(41, 20, 5, 2, c3(BARK)); rect(46, 19, 3, 2, c3(BARK)); rect(49, 18, 2, 1, c3(BARK)) rect(51, 17, 2, 1, c3(BARK)); px(53, 16, c3(BARK)); px(48, 18, c3(BARK)); px(47, 17, c3(BARK)) rect(41, 20, 5, 1, c3(BARK_L)) for x = 0, W - 1 do if (hash32(x * 0x9E3779B1) & 3) ~= 0 then px(x, 59 + (x * 7) % 3, c3(SK.grass)) end end FG = gfx.snapshot(0, 0, W, H) end local function owl_state(p) if p < 60 then return nil end if p < 76 then return DOWN - (DOWN - UP) * ease((p - 60) / 16), "front" end if p < 232 then local d, v = p - 76, "front" if (d >= 14 and d < 18) or (d >= 112 and d < 116) or (d >= 130 and d < 134) then v = "blink" elseif d >= 34 and d < 64 then v = "left" elseif d >= 64 and d < 72 then v = "back" elseif d >= 72 and d < 100 then v = "right" end return UP, v end if p < 250 then return UP + (DOWN - UP) * ease((p - 232) / 18), "front" end return nil end local function draw_owl(S, y) local x = HCX - 4 local iy = floor(y) local f = y - iy if f < 0.05 then sprite(S, x, iy) return end sprite(S, x, iy + 1) local row = S[1] for k = 1, #row do local e = row[k] blend_rect(x + e[1], iy, e[2], 1, e[3][1], e[3][2], e[3][3], 1 - f) end end local LEAVES = { { 46, 17, 1 }, { 49, 15, -1 }, { 51, 15, 1 }, { 53, 14, 1 }, { 54, 17, 1 }, { 44, 18, -1 } } function render() if not READY then apply() end if not BG then build_bg(); build_fg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #CLOUDS do local c = CLOUDS[i] local w = #CLOUD_ART[c[1]][1] sprite(CS[c[1]], (c[4] + t * c[3]) % (W + w) - w, c[2]) end rect(HCX - 4, HY0, 8, #HW, c3(HOLE)) local y, v = owl_state((t + 40) % 300) if y then gfx.clip(HCX - 4, HY0, 8, #HW) draw_owl(OWL[v], y) gfx.clip() end gfx.blit_rgb(FG, 0, 0, W, H, KR, KG, KB) local gust = sin(t * 0.021) > 0.4 and 5 or 2 local slot = t // 3 for k = 1, #LEAVES do local L = LEAVES[k] local h = hash32(k * 0x9E3779B1 + slot * 0x85EBCA6B) local dy = (h & 7) < gust and 1 or 0 local dx = ((h >> 3) & 7) < gust - 1 and L[3] or 0 local x, yy = L[1] + dx, L[2] + dy rect(x, yy, 2, 1, c3(LEAF)) px(L[3] > 0 and x + 2 or x - 1, yy - 1, c3(LEAF_L)) end local d = (t + 120) % 360 if d < 150 then local x = 53 + 0.15 * d + 3 * sin(d * 0.07) local yy = 16 + 0.28 * d local ix, iy = floor(x), floor(yy) local fx, fy = x - ix, yy - iy local r, g, b = c3(LEAF) blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, r, g, b, fx * fy) end end