info = { type = "image", title = "Mossy branch bird", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Plump bird hops on a mossy branch, a leaf drips", api = 44, } local tr = ({ sk = { ["Bird"] = "Vtáčik", ["Green"] = "Zelený", ["Blue"] = "Modrý", ["Pink"] = "Ružový", ["Dripping leaf"] = "Kvapkajúci list" }, cs = { ["Bird"] = "Ptáček", ["Green"] = "Zelený", ["Blue"] = "Modrý", ["Pink"] = "Růžový", ["Dripping leaf"] = "Kapající list" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bird", label = T("Bird"), type = "int", default = "green", opts = { keys = { "green", "blue", "pink" }, T("Green"), T("Blue"), T("Pink") } }, { key = "drip", label = T("Dripping leaf"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, ceil, sin, cos, sqrt, min, max = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local char, unpack = string.char, table.unpack local W, H = gfx.W, gfx.H local RAMPS = { green = { body = { { 0, 38, 96, 52 }, { 0.4, 88, 170, 70 }, { 0.75, 150, 214, 96 }, { 1, 214, 246, 150 } }, wing = { { 0, 24, 70, 48 }, { 0.5, 60, 128, 70 }, { 1, 120, 190, 100 } } }, blue = { body = { { 0, 30, 70, 140 }, { 0.4, 70, 130, 220 }, { 0.75, 130, 180, 245 }, { 1, 200, 228, 255 } }, wing = { { 0, 20, 40, 100 }, { 0.5, 50, 90, 170 }, { 1, 100, 150, 220 } } }, pink = { body = { { 0, 150, 56, 96 }, { 0.4, 230, 110, 150 }, { 0.75, 250, 170, 190 }, { 1, 255, 225, 230 } }, wing = { { 0, 110, 36, 76 }, { 0.5, 190, 76, 120 }, { 1, 240, 140, 170 } } }, } local BELLY = { { 0, 176, 150, 96 }, { 0.5, 240, 222, 160 }, { 1, 255, 250, 214 } } local BEAK = { { 0, 170, 70, 20 }, { 1, 255, 170, 70 } } local BLUSH = { { 0, 190, 90, 80 }, { 1, 250, 160, 150 } } local function ramp(keys, t) for i = 1, #keys - 1 do local a, b = keys[i], keys[i + 1] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end local l = keys[#keys] return l[2], l[3], l[4] end local PAL = nil function on_settings() local R = RAMPS[cfg.bird] or RAMPS.green PAL = { 0, 0, 0 } for _, keys in ipairs({ R.body, BELLY, R.wing, BEAK, BLUSH }) do for k = 0, 19 do local r, g, b = ramp(keys, k / 19) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end end end local SW, SH = 34, 28 local LX, LY, LZ = -0.46, -0.58, 0.67 local function shade(nx, ny, nz) local d = (nx * LX + ny * LY + nz * LZ + 0.3) / 1.3 if d < 0 then d = 0 end local t = 0.15 + 0.7 * d if ny > 0.4 then t = t + 0.18 * (ny - 0.4) * (1 - d) end t = t * (0.78 + 0.22 * nz) if t > 1 then t = 1 elseif t < 0 then t = 0 end return floor(t * 19 + 0.5) end local function ell(g, cx, cy, rx, ry, ang, bi, ya, yb, pick) local c, s = cos(ang), sin(ang) local ex, ey = sqrt((rx * c) ^ 2 + (ry * s) ^ 2), sqrt((rx * s) ^ 2 + (ry * c) ^ 2) for y = max(ya or 0, floor(cy - ey)), min(yb or SH - 1, ceil(cy + ey)) do for x = max(0, floor(cx - ex)), min(SW - 1, ceil(cx + ex)) do local dx, dy = x + 0.5 - cx, y + 0.5 - cy local u, v = (dx * c + dy * s) / rx, (dy * c - dx * s) / ry local r2 = u * u + v * v if r2 <= 1 then g[y * SW + x + 1] = (pick and pick(x, y) or bi) + shade(u * c - v * s, u * s + v * c, sqrt(1 - r2)) end end end end local BCX, BCY, BRX, BRY = 17.5, 14, 13.2, 11.5 local function pick_body(x, y) local dx, dy = (x + 0.5 - 12) / 9.6, (y + 0.5 - 19.6) / 7 return dx * dx + dy * dy < 1 and 21 or 1 end local G0, SPR = {}, {} local function to_string(g) local rows = {} for y = 0, SH - 1 do rows[y + 1] = char(unpack(g, y * SW + 1, y * SW + SW)) end return table.concat(rows) end local function pose(wing_cx, wing_cy, wrx, wry, wang) local g = {} for i = 1, SW * SH do g[i] = G0[i] end ell(g, wing_cx, wing_cy, wrx, wry, wang, 41) return to_string(g) end local function bird_a() for i = 1, SW * SH do G0[i] = 0 end ell(G0, 29, 20.5, 5.2, 2.2, 0.55, 41) ell(G0, BCX, BCY, BRX, BRY, 0, 1, 0, 13, pick_body) end local function bird_b() ell(G0, BCX, BCY, BRX, BRY, 0, 1, 14, SH - 1, pick_body) ell(G0, 12.5, 3, 1.3, 2.8, -0.3, 1) ell(G0, 4.6, 13.2, 2.6, 1.6, 0, 61) for y = 14, 19 do for x = 5, 13 do local dx, dy = (x + 0.5 - 9.5) / 2.4, (y + 0.5 - 16.8) / 1.5 local i = y * SW + x + 1 if dx * dx + dy * dy < 1 and G0[i] >= 1 and G0[i] <= 40 then G0[i] = 81 + (G0[i] - 1) % 20 end end end end local function bird_c() SPR[1] = pose(18.5, 15.5, 7, 4.2, 0.35) end local function bird_d() SPR[2] = pose(19.5, 10.5, 7, 3.4, -0.95) end local BG = nil local ROUGH = { 0.30, -0.12, 0.45, 0.02, -0.35, 0.2 } local function branch_top(x) return floor(48 - 6.5 * x / 60 + 0.8 * sin(x * 0.25) + 0.5) end local function branch_th(x) return floor(7 - 3.5 * x / 60 + 0.5) end local function bg_sky() local stops = { { 196, 236, 224 }, { 150, 214, 196 }, { 98, 170, 140 } } for y = 0, H - 1 do local t = y / (H - 1) * 2 local a, b = stops[floor(t) + 1], stops[min(floor(t) + 2, 3)] local f = t - floor(t) rect(0, y, W, 1, floor(a[1] + (b[1] - a[1]) * f + 0.5), floor(a[2] + (b[2] - a[2]) * f + 0.5), floor(a[3] + (b[3] - a[3]) * f + 0.5)) end for _, d in ipairs({ { 50, 9, 8 }, { 22, 5, 5 }, { 59, 28, 6 }, { 38, 4, 4 }, { 28, 30, 5 } }) do for y = -d[3], d[3] do local hw = floor(sqrt(d[3] * d[3] - y * y) + 0.5) blend_rect(d[1] - hw, d[2] + y, 2 * hw + 1, 1, 236, 252, 240, 0.13) end end end local function bg_branch() for x = -2, 60 do local top, th = branch_top(x), branch_th(x) rect(x, top, 1, th, 112, 78, 56) rect(x, top, 1, 1, 160, 118, 88) rect(x, top + 1, 1, 1, 134, 94, 68) rect(x, top + th - 1, 1, 1, 78, 52, 40) end for i = 1, 16 do local h = hash32(i * 0x9E3779B1) local x = umod(h, 56) px(x, branch_top(x) + 2 + umod(h >> 8, 2), 82, 56, 42) end end local function mound(cx, w, h) local top = branch_top(cx) + 1 for k = 0, h - 1 do local rw = w - 2 * (h - 1 - k) local r, g, b = 66, 128, 56 if k == 0 then r, g, b = 146, 204, 92 elseif k == 1 then r, g, b = 100, 170, 70 elseif k == h - 1 then r, g, b = 48, 100, 50 end rect(cx - rw // 2, top - h + k + 1, rw, 1, r, g, b) end end local function bg_moss() local x, i = 1, 0 while x < 56 do i = i + 1 local h = hash32(i * 0x85EBCA6B) local w = 6 + umod(h, 4) mound(x + w // 2, w, 3 + umod(h >> 4, 2)) x = x + w - 1 - umod(h >> 8, 2) end for k = 1, 8 do local hx = 2 + umod(hash32(k * 0x27D4EB2F), 46) rect(hx, branch_top(hx) + branch_th(hx), 1, 2 + k % 2, 74, 140, 62) end end local function leaf(bx, by, ang, len, wid, tone) local c, s = cos(ang), sin(ang) local reach = len + wid for y = floor(by - reach), ceil(by + reach) do for x = floor(bx - reach), ceil(bx + reach) do local dx, dy = x + 0.5 - bx, y + 0.5 - by local u, v = (dx * c + dy * s) / len, (dy * c - dx * s) if u > 0 and u < 1 then local hw = wid * (1 - (2 * u - 1) ^ 2) ^ 0.75 if math.abs(v) <= hw then local side = (-v * 0.7 + (-dx * 0.2 - dy * 0.3)) / (wid + 1) local t = 0.55 + 0.45 * side - 0.12 * u if math.abs(v) < 0.5 then t = t - 0.25 end if t < 0.05 then t = 0.05 elseif t > 1 then t = 1 end px(x, y, floor(30 + 120 * t * tone[1]), floor(80 + 140 * t * tone[2]), floor(44 + 70 * t * tone[3])) end end end end end local TW, TH = { 0.80, 1.0, 0.9 }, { 0.95, 1.0, 0.7 } local LEAVES = { { 7, 41, -2.6, 9, 2.6, TW }, { 7, 36, -0.5, 10, 3, TH }, { 8, 31, -2.4, 10, 3, TW }, { 9, 26, -0.7, 11, 3.2, TH }, { 10, 22, -2.2, 10, 3, TW }, { 10.5, 18, -1.2, 9, 2.8, TH }, { 11, 17, -1.8, 8, 2.6, TW }, { 11.5, 23, 1.3, 12, 3.2, TH }, { 66, -2, 2.6, 14, 4, TW }, { 64, 6, 2.9, 10, 3, TH }, } local TIP_X, TIP_Y = 14, 35 local function bg_leaves(a, b) if a == 1 then for y = 18, 46 do rect(floor(6 + (46 - y) * 0.18 + 0.5), y, 1, 1, 96, 70, 46) end end for i = a, b do local l = LEAVES[i]; leaf(l[1], l[2], l[3], l[4], l[5], l[6]) end end local STAGES = { bg_sky, bg_branch, bg_moss, function() bg_leaves(1, 3) end, function() bg_leaves(4, 6) end, function() bg_leaves(7, 8) end, function() bg_leaves(9, 10) end, bird_a, bird_b, bird_c, bird_d, } local STAGE = 1 local NBG = 7 local SPX, SPY = 26, 16 local C, D = 210, 150 local function hop_height(t) local u = (t - 70) / 16 if u >= 0 and u < 1 then return 28 * u * (1 - u) end u = (t - 104) / 12 if u >= 0 and u < 1 then return 16 * u * (1 - u) end return 0 end local function eye(x, y, shut) if shut then rect(x, y + 1, 2, 1, 26, 48, 30) else rect(x, y, 2, 3, 24, 34, 28) px(x, y, 255, 255, 255) end end local function drip(t) local u = (t - 20) % D if u < 8 then blend_rect(TIP_X, TIP_Y, 1, 1, 90, 160, 210, 0.5) elseif u < 16 then px(TIP_X, TIP_Y, 90, 160, 210) elseif u < 25 then px(TIP_X, TIP_Y, 255, 255, 255); px(TIP_X, TIP_Y + 1, 90, 160, 210) elseif u < 37 then local s = u - 25 local y = TIP_Y + floor(0.045 * s * s + 0.2 * s) if y <= 42 then px(TIP_X, y, 255, 255, 255); px(TIP_X, y + 1, 80, 150, 210) end elseif u < 46 then local k = (u - 37) // 3 blend_rect(TIP_X - 1 - k, 43, 1, 1, 230, 250, 255, 0.6 - 0.18 * k) blend_rect(TIP_X + 1 + k, 43, 1, 1, 230, 250, 255, 0.6 - 0.18 * k) end end function render() if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= NBG then BG = snapshot(0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end blit_rgb(BG, 0, 0, W, H) local t = frame % C local h = hop_height(t) local y = SPY - floor(h + 0.5) local a = 0.30 - h * 0.03 blend_rect(SPX + 6 + floor(h * 0.5), SPY + 26, 22 - floor(h), 1, 24, 56, 30, a) blend_rect(SPX + 9 + floor(h * 0.5), SPY + 27, 16 - floor(h), 1, 24, 56, 30, a) if cfg.drip ~= false then drip(frame) end blit_indexed(SPR[h > 0.8 and 2 or 1], PAL, SPX, y, SW, SH, SW, SH, true) if h > 0.8 then rect(SPX + 14, y + 26, 1, 2, 214, 120, 44); rect(SPX + 19, y + 26, 1, 2, 214, 120, 44) else rect(SPX + 13, y + 26, 3, 1, 214, 120, 44); rect(SPX + 18, y + 26, 3, 1, 214, 120, 44) end local bl = t % 150 eye(SPX + 9, y + 11, (bl >= 20 and bl < 23) or (bl >= 31 and bl < 34)) end