info = { type = "image", title = "Finch tree", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Birds on a hilltop tree flee when a cat passes", api = 33, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Spring"] = "Jar", ["Summer"] = "Leto", ["Autumn"] = "Jeseň", ["Birds"] = "Vtáky" }, cs = { ["Season"] = "Roční období", ["Spring"] = "Jaro", ["Summer"] = "Léto", ["Autumn"] = "Podzim", ["Birds"] = "Ptáci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "summer", opts = { keys = { "spring", "summer", "autumn" }, T("Spring"), T("Summer"), T("Autumn") } }, { key = "birds", label = T("Birds"), type = "int", min = 2, max = 5, default = 4 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, exp, abs = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local SEASONS = { spring = { sky0 = { 120, 184, 240 }, sky1 = { 214, 236, 250 }, dark = { 58, 128, 70 }, mid = { 98, 176, 90 }, light = { 152, 220, 122 }, spot = { 255, 200, 222 }, g0 = { 120, 198, 96 }, g1 = { 84, 160, 70 }, far = { 150, 204, 190 }, leaf = { 255, 206, 224 } }, summer = { sky0 = { 58, 138, 230 }, sky1 = { 170, 216, 248 }, dark = { 30, 96, 58 }, mid = { 52, 142, 72 }, light = { 98, 192, 92 }, spot = { 130, 214, 110 }, g0 = { 86, 168, 72 }, g1 = { 60, 132, 56 }, far = { 118, 178, 176 }, leaf = { 98, 192, 92 } }, autumn = { sky0 = { 150, 172, 212 }, sky1 = { 250, 214, 164 }, dark = { 150, 58, 30 }, mid = { 214, 112, 36 }, light = { 246, 178, 66 }, spot = { 190, 44, 36 }, g0 = { 156, 168, 72 }, g1 = { 122, 136, 56 }, far = { 196, 170, 150 }, leaf = { 226, 120, 40 } }, } local S = SEASONS.summer local NB = 4 local GY = {} for x = 0, W - 1 do GY[x] = 57 - floor(11 * exp(-((x - 15) / 17) ^ 2) + 0.5) end local BR_R = { 18, 43, 53, 37 } local BR_L = { 15, 44, 1, 40 } local function branch_y(b, x) return floor(b[2] + (x - b[1]) * (b[4] - b[2]) / (b[3] - b[1]) + 0.5) end local SPOTS = { { x = 36, y = branch_y(BR_R, 38), right = true }, { x = 46, y = branch_y(BR_R, 48), right = true }, { x = 26, y = branch_y(BR_R, 28), right = true }, { x = 9, y = branch_y(BR_L, 11), right = false }, { x = 3, y = branch_y(BR_L, 5), right = false }, } local BG, PART local function disc(cx, cy, r, c) for dy = -r, r do local hw = floor(sqrt(r * r + r * 0.6 - dy * dy) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end local function crown_layer(c, dx, dy, shrink) disc(17 + dx, 23 + dy, 13 - shrink, c) for k = 0, 7 do local a = k * 0.7854 + 0.2 disc(floor(17 + dx + 10.5 * cos(a) + 0.5), floor(23 + dy + 10.5 * sin(a) + 0.5), 5 - shrink // 2, c) end end local function scene() for y = 0, 56 do local t = y / 56 rect(0, y, W, 1, floor(S.sky0[1] + (S.sky1[1] - S.sky0[1]) * t + 0.5), floor(S.sky0[2] + (S.sky1[2] - S.sky0[2]) * t + 0.5), floor(S.sky0[3] + (S.sky1[3] - S.sky0[3]) * t + 0.5)) end rect(38, 8, 14, 2, 255, 255, 255); rect(36, 9, 18, 2, 255, 255, 255); rect(40, 11, 12, 1, 224, 236, 252) rect(52, 22, 10, 2, 255, 255, 255); rect(54, 24, 7, 1, 224, 236, 252) for x = 24, W - 1 do local top = floor(48 - 6 * sin(x * 0.12 + 1) - 3 * sin(x * 0.31) + 0.5) rect(x, top, 1, 58 - top, S.far[1], S.far[2], S.far[3]) end for x = 0, W - 1 do local top = GY[x] rect(x, top, 1, H - top, S.g1[1], S.g1[2], S.g1[3]) rect(x, top, 1, 4, S.g0[1], S.g0[2], S.g0[3]) rect(x, top, 1, 1, floor(S.g0[1] * 1.12), floor(S.g0[2] * 1.1), floor(S.g0[3] * 1.1)) end for i = 1, 22 do local h = hash32(i * 0x9E3779B1) local x = umod(h, W) local y = GY[x] + 3 + umod(h >> 8, 64 - GY[x] - 4) local c = (i % 3 == 0) and S.light or S.dark px(x, y, c[1] * 0.9, c[2] * 0.9, c[3] * 0.9) end rect(14, 34, 4, 13, 104, 68, 40); rect(17, 34, 1, 13, 76, 48, 30); rect(14, 34, 1, 13, 132, 92, 58) rect(13, 44, 6, 3, 104, 68, 40) for _, b in ipairs({ BR_R, BR_L }) do local step = b[3] > b[1] and 1 or -1 for x = b[1], b[3], step do local th = (abs(x - b[3]) < 8) and 1 or 2 rect(x, branch_y(b, x), 1, th, 104, 68, 40) if th == 2 then px(x, branch_y(b, x) + 1, 76, 48, 30) end end end px(50, 36, 104, 68, 40); px(51, 35, 104, 68, 40) crown_layer(S.dark, 1, 1, 0) crown_layer(S.mid, 0, 0, 1) crown_layer(S.light, -2, -3, 4) for i = 1, 60 do local h = hash32(i * 0x85EBCA6B) local a, r = umod(h, 628) / 100, 3 + umod(h >> 10, 11) local x, y = floor(17 + r * cos(a)), floor(23 + r * sin(a)) local c = (i % 3 == 0) and S.dark or ((i % 3 == 1) and S.light or S.spot) if i % 3 == 2 and cfg.season == "summer" then c = S.light end rect(x, y, 2, 1, c[1], c[2], c[3]) end end local STAGE = 1 local function build() if STAGE == 1 then scene(); BG = snapshot(0, 0, W, H) end STAGE = 2 end local SPEC = { { body = { 150, 104, 70 }, belly = { 226, 200, 164 }, wing = { 98, 66, 44 }, head = { 120, 84, 58 }, beak = { 236, 176, 70 } }, { body = { 92, 150, 214 }, belly = { 250, 226, 110 }, wing = { 58, 98, 160 }, head = { 236, 240, 250 }, beak = { 60, 56, 60 } }, { body = { 192, 88, 72 }, belly = { 238, 176, 146 }, wing = { 112, 52, 42 }, head = { 214, 108, 90 }, beak = { 236, 176, 70 } }, } local EYE = { 20, 16, 18 } local PER, TS, FLEE, FARR = 480, 170, 44, 44 local BIRDS = {} for i = 1, 5 do local sp = SPOTS[i] local h = hash32(i * 0xC2B2AE35) local ang = (10 + umod(h, 55)) * 0.01745 BIRDS[i] = { i = i, x = sp.x, y = sp.y, spec = SPEC[(i - 1) % 3 + 1], d = umod(h >> 8, 6), off = umod(h >> 12, 200), hp = 70 + umod(h >> 4, 60), sx = sp.right and (W + 8) or -10, sy = 2 + umod(h >> 16, 16), dx = sp.right and cos(ang) or -cos(ang), dy = -sin(ang), sp = 2.0 + umod(h >> 20, 8) / 10, } BIRDS[i].adir = BIRDS[i].x > BIRDS[i].sx and 1 or -1 BIRDS[i].fdir = sp.right and 1 or -1 end local function perch(b, t) local tt = t + b.off local h, ph = tt // b.hp, tt % b.hp local hop = ph < 10 and 3 * 4 * (ph / 10) * (1 - ph / 10) or 0 local face = (h % 2 == 0) and b.adir or -b.adir return b.x, b.y - floor(hop + 0.5), face, hop > 0 and 3 or nil, (tt % 53) < 6 end local function where(b, t) local tf = TS + b.d if t < tf then return perch(b, t) end local u = t - tf if u < FLEE then local d = (u < 8) and b.sp * u * u / 16 or b.sp * (u - 4) return b.x + b.dx * d, b.y + b.dy * d, b.dx > 0 and 1 or -1, (u // 2) % 4 end u = t - (TS + 60 + (b.i - 1) * 38) if u < 0 then return end if u < FARR - 3 then local k = u / FARR local e = 1 - (1 - k) * (1 - k) return b.sx + (b.x - b.sx) * e, b.sy + (b.y - b.sy) * e - 6 * sin(3.1416 * k) + 1.6 * sin(k * 20) * (1 - k), b.adir, (u // 2) % 4 end return perch(b, t) end local function bird(b, x, y, face, flap, peck) x, y = floor(x + 0.5), floor(y + 0.5) local sp = b.spec local function R(lx, ly, w, h, c) rect(x + (face > 0 and lx or 4 - lx - w + 1), y + ly, w, h, c[1], c[2], c[3]) end local dh = peck and 1 or 0 R(-2, -2, 2, 1, sp.wing) R(0, -3, 4, 3, sp.body) R(1, -1, 3, 1, sp.belly) if flap == 0 then R(0, -6, 3, 3, sp.wing) elseif flap == 1 or flap == 3 then R(-2, -3, 4, 2, sp.wing) elseif flap == 2 then R(0, 0, 3, 2, sp.wing) else R(1, -3, 2, 2, sp.wing) end R(3, -4 + dh, 3, 3, sp.head) R(4, -3 + dh, 1, 1, EYE) R(6, -3 + dh, 1, 1, sp.beak) end local LEGX = { 4, -3, 2, -5 } local CAT, CAT_S, CAT_L, CAT_B = { 216, 132, 56 }, { 160, 84, 32 }, { 244, 200, 148 }, { 40, 30, 24 } local function cat(cx, dir, t) local gy = GY[math.max(0, math.min(W - 1, cx))] - 1 local function R(lx, ly, w, h, c) rect(dir > 0 and cx + lx or cx - lx - w + 1, gy + ly, w, h, c[1], c[2], c[3]) end blend_rect(cx - 6, gy + 1, 13, 1, 10, 40, 10, 0.25) local k = (t // 3) % 4 for j = 1, 4 do local ph = k * 1.5708 + ((j == 1 or j == 4) and 0 or 3.1416) local dx = floor(-cos(ph) + 0.5) local lift = sin(ph) > 0.5 and 1 or 0 R(LEGX[j] + dx, -3, 1, 3 - lift, (j <= 2) and CAT or CAT_S) end for i = 1, 5 do R(-6 - (i > 1 and 1 or 0) + floor(sin(t * 0.2 - i * 0.8) * i / 3 + 0.5), -7 - i, 1, 1, i == 5 and CAT_S or CAT) end R(-5, -7, 10, 4, CAT) R(-4, -4, 8, 1, CAT_L) for i = 0, 2 do R(-3 + i * 2, -7, 1, 2, CAT_S) end R(4, -9, 5, 5, CAT) R(4, -10, 1, 1, CAT); R(8, -10, 1, 1, CAT) R(7, -8, 1, 1, CAT_B) R(9, -7, 1, 1, { 240, 150, 150 }) end local function splat(x, y, c, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, c[1], c[2], c[3], a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, c[1], c[2], c[3], a * fx * (1 - fy)) blend(ix, iy + 1, c[1], c[2], c[3], a * (1 - fx) * fy) blend(ix + 1, iy + 1, c[1], c[2], c[3], a * fx * fy) end local LEAF_T = 190 local function leaf(slot) local n, u = (frame + slot * LEAF_T // 2) // LEAF_T, (frame + slot * LEAF_T // 2) % LEAF_T local h = hash32(n * 0x27D4EB2F + slot * 977) local x0 = 8 + umod(h, 20) local x = x0 + 5 * sin(u * 0.07 + slot) + u * 0.04 local y = 34 + u * 0.115 local a = u > LEAF_T - 14 and (LEAF_T - u) / 14 or 1 if y > GY[math.max(0, math.min(W - 1, floor(x)))] - 1 then a = 0 end if a > 0 then splat(x, y, S.leaf, a) splat(x + 1, y + 0.4, S.leaf, a * 0.8) end end function on_settings() S = SEASONS[cfg.season] or SEASONS.summer NB = cfg.birds or 4 STAGE = 1 end function render() if STAGE == 1 then build() end blit_rgb(BG, 0, 0, W, H) local t = frame % PER for i = 1, NB do local b = BIRDS[i] local x, y, face, flap, peck = where(b, t) if x then bird(b, x, y, face, flap, peck) end end local cx = 78 - (t - (TS - 44)) if cx > -14 and cx < 80 then cat(cx, -1, t) end leaf(0); leaf(1) end