info = { type = "image", title = "Deer in the woods", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A stag strolls through layered misty forest", api = 33, } local tr = ({ sk = { ["Pace"] = "Tempo", ["Antlers"] = "Parohy", ["slow"] = "pomaly", ["normal"] = "bežne", ["fast"] = "rýchlo" }, cs = { ["Pace"] = "Tempo", ["Antlers"] = "Paroží", ["slow"] = "pomalu", ["normal"] = "běžně", ["fast"] = "rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pace", label = T("Pace"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("slow"), T("normal"), T("fast") } }, { key = "antlers", label = T("Antlers"), type = "bool", default = true }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PI = 3.14159 local function bar(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if f < 0.04 then rect(ix, y, w, h, r, g, b) return end if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end 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 CYC, KC = 840, 0.45 local GS = 6 local G, GT = {}, 0 do local acc = 0 G[0] = 0 for i = 1, CYC // GS do local t = i * GS acc = acc + GS * (sstep(0, 24, t) * (1 - sstep(330, 385, t)) + sstep(500, 555, t)) G[i] = acc end local k = 90 / (1 - KC) / acc for i = 1, CYC // GS do G[i] = G[i] * k end GT = G[CYC // GS] end local function make(seed, n, P, wmin, wmax) local L = {} for i = 1, n do local h = hash32(seed * 977 + i * 0x9E3779B1) L[i] = { x = (i - 1) * P / n + umod(h, floor(P / n * 0.55)), w = wmin + umod(h >> 9, wmax - wmin + 1), s = umod(h >> 16, 3) + 1, ly = 7 + umod(h >> 20, 24) } end return L end local FAR, PF = make(1, 11, 128, 2, 3), 128 local MID, PM = make(2, 4, 128, 4, 5), 128 local NEAR, PN = { { x = 109, w = 7, s = 1, ly = 0 }, { x = 159, w = 6, s = 1, ly = 0 } }, 192 local BUSH, PB = make(4, 9, 96, 7, 13), 96 local FAR_C = { { 150, 196, 204 }, { 134, 184, 194 }, { 164, 206, 208 } } local MID_C = { { 30, 92, 92 }, { 36, 100, 96 }, { 26, 82, 86 } } local TUFT = {} for i = 1, 10 do TUFT[i] = umod(hash32(i * 0x85EBCA6B), 64) end local BG, RAYS = nil, nil local function build_bg() for y = 0, 51 do local t = y / 51 rect(0, y, W, 1, floor(70 - 30 * t + 0.5), floor(132 - 32 * t + 0.5), floor(146 - 38 * t + 0.5)) end rect(0, 52, W, 3, 40, 108, 92) rect(0, 55, W, H - 55, 30, 90, 62) for i = 0, 13 do local h = hash32(i * 0xC2B2AE35 + 7) local x, w, d = i * 5 - 2, 6 + umod(h, 4), 2 + umod(h >> 6, 6) rect(x, 0, w, d, 22, 70, 66) rect(x + 1, d, w - 2, 1, 30, 88, 78) px(x + umod(h >> 12, w), d + 1, 30, 88, 78) end for i = 0, 11 do local h = hash32(i * 0x27D4EB2F + 3) px(umod(h, W), umod(h >> 8, 4), 70, 140, 100) end BG = gfx.snapshot(0, 0, W, H) end local function build_rays() local cells = {} for i = 1, 64 * 24 do cells[i] = 0 end local R = { { 6, 4 }, { 22, 6 }, { 38, 4 }, { 52, 5 } } for k = 1, #R do local c, hw0 = R[k][1], R[k][2] for y = 0, 23 do local hw = hw0 * (1 + y / 20) local cx = c + y local fade = 1 - y / 24 for x = max(0, floor(cx - hw)), min(63, floor(cx + hw)) do local v = (1 - abs(x - cx) / hw) * fade * 255 if v > cells[y * 64 + x + 1] then cells[y * 64 + x + 1] = floor(v) end end end end local rows, row = {}, {} for y = 0, 23 do for x = 1, 64 do row[x] = cells[y * 64 + x] end rows[y + 1] = string.char(table.unpack(row, 1, 64)) end RAYS = table.concat(rows) end local MULT, ANTLERS = 1.0, true function on_settings() local p = cfg.pace MULT = p == "slow" and 0.7 or p == "fast" and 1.4 or 1.0 ANTLERS = cfg.antlers ~= false end local BODY, BACK, BELLY = { 206, 134, 62 }, { 172, 102, 48 }, { 238, 190, 132 } local LEG_N, LEG_F, HOOF = { 188, 118, 54 }, { 140, 86, 42 }, { 46, 30, 26 } local ANT = { 226, 206, 160 } local function leg(x, F, hx, p, amp, c) p = p % 1 local off, lift if p < 0.6 then off, lift = 2.2 * amp * (1 - 2 * p / 0.6), 0 else off, lift = 2.2 * amp * (-1 + 2 * (p - 0.6) / 0.4), 2.4 * amp * sin(PI * (p - 0.6) / 0.4) end off, lift = floor(off + 0.5), floor(lift + 0.5) local lx = floor(x + hx + 0.5) rect(lx + floor(off * 0.4 + 0.5), F - 9, 1, 4, c[1], c[2], c[3]) rect(lx + off, F - 5, 1, 4 - lift, c[1], c[2], c[3]) rect(lx + off, F - 1 - lift, 1, 1, HOOF[1], HOOF[2], HOOF[3]) end local function deer(x, hl, ph, amp, ear) local F = 57 blend_rect(floor(x) + 1, F - 1, 17, 1, 8, 30, 28, 0.35) leg(x, F, 4, ph + 0.5, amp, LEG_F) leg(x, F, 11, ph + 0.75, amp, LEG_F) bar(x + 3, F - 15, 11, 1, BACK[1], BACK[2], BACK[3]) bar(x + 2, F - 14, 13, 4, BODY[1], BODY[2], BODY[3]) bar(x + 3, F - 10, 11, 1, BELLY[1], BELLY[2], BELLY[3]) bar(x + 1, F - 14, 1, 2, 244, 236, 220) leg(x, F, 3, ph, amp, LEG_N) leg(x, F, 12, ph + 0.25, amp, LEG_N) bar(x + 13, F - 19 - hl, 3, 5 + hl, BODY[1], BODY[2], BODY[3]) bar(x + 15, F - 20 - hl, 5, 3, BODY[1], BODY[2], BODY[3]) local hx = floor(x + 0.5) px(hx + 20, F - 19 - hl, 60, 40, 34) px(hx + 17, F - 19 - hl, 30, 20, 16) rect(hx + 15, F - 22 - hl + ear, 1, 2, BACK[1], BACK[2], BACK[3]) if ANTLERS then rect(hx + 16, F - 26 - hl, 1, 4, ANT[1], ANT[2], ANT[3]) rect(hx + 18, F - 25 - hl, 1, 3, ANT[1], ANT[2], ANT[3]) px(hx + 15, F - 25 - hl, ANT[1], ANT[2], ANT[3]) px(hx + 17, F - 27 - hl, ANT[1], ANT[2], ANT[3]) px(hx + 19, F - 26 - hl, ANT[1], ANT[2], ANT[3]) end end local function trunks(L, P, off, y0, y1, pal, hi) for i = 1, #L do local tk = L[i] local c = pal[tk.s] or pal[1] local sx = (tk.x - off) % P for rep = 0, 1 do local x = sx - rep * P if x < W and x + tk.w > 0 then bar(x, y0, tk.w, y1 - y0, c[1], c[2], c[3]) if hi then bar(x, y0, 1, y1 - y0, hi[1], hi[2], hi[3]) bar(x - 2, tk.ly, tk.w + 4, 2, 58, 138, 92) bar(x - 1, tk.ly - 1, tk.w + 2, 1, 58, 138, 92) end end end end end function render() if not BG then build_bg() return end if not RAYS then build_rays() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * MULT local n = floor(t / CYC) local ti = t - n * CYC local gi = ti / GS local i = floor(gi) local v = (G[i + 1] - G[i]) / GS local g = G[i] + v * (ti - i * GS) local function off(s, P) return ((s * KC * GT * n) % P + s * KC * g) % P end trunks(FAR, PF, off(0.2, PF), 0, 52, FAR_C) for k = 0, 9 do blend_rect(0, 28 + k * 2, W, 2, 200, 230, 226, 0.035 * (k + 1)) end trunks(MID, PM, off(0.5, PM), 0, 56, MID_C, { 52, 122, 110 }) gfx.blend_mask(RAYS, 238, 250, 214, 0, 0, W, 48, W, 24, 0.34 * (0.78 + 0.22 * sin(frame * 0.021))) local go = off(1.0, 64) for k = 1, #TUFT do bar((TUFT[k] - go) % 64 - 1, 55, 2, 2, 80, 168, 96) bar((TUFT[k] - go) % 64 + 31 - 1 - (k % 3), 59, 2, 1, 56, 130, 78) end local S = -24 + (1 - KC) * g local hl = 4 * (sstep(360, 400, ti) - sstep(470, 510, ti)) hl = floor(hl + 0.5) local ear = ((ti > 425 and ti < 431) or (ti > 447 and ti < 452)) and 1 or 0 if S > -24 and S < W then deer(S, hl, g / 12, min(1, v / 0.12), ear) end local sn = PN / (KC * GT) trunks(NEAR, PN, off(sn, PN), 0, 60, { { 12, 46, 44 } }, nil) local no = off(sn, PN) for k = 1, #NEAR do local sx = (NEAR[k].x - no) % PN for rep = 0, 1 do local x = sx - rep * PN if x < W and x + NEAR[k].w > 0 then bar(x + NEAR[k].w - 1, 0, 1, 60, 24, 74, 62) end end end local bo = off(sn, PB) for k = 1, #BUSH do local b = BUSH[k] local sx = (b.x - bo) % PB for rep = 0, 1 do local x = sx - rep * PB if x < W and x + b.w > 0 then bar(x, 59 + (k % 3), b.w, 6, 20, 70, 48) bar(x + 1, 58 + (k % 3), b.w - 2, 1, 40, 112, 68) end end end for k = 1, 7 do local h = hash32(k * 0x1B873593) local x = (umod(h, 64) + frame * (0.012 + umod(h >> 8, 8) * 0.002)) % 64 local y = 8 + umod(h >> 12, 34) + 2 * sin(frame * 0.03 + k) blend(floor(x), floor(y), 240, 250, 214, 0.45 + 0.35 * sin(frame * 0.05 + k * 2)) end end