info = { type = "image", title = "Cactus bloom", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Time-lapse of a cactus blooming by a window", api = 33, } local tr = ({ sk = { ["Flower"] = "Kvet", ["Pink"] = "Ružový", ["Yellow"] = "Žltý", ["Red"] = "Červený", ["Pace"] = "Tempo", ["Calm"] = "Pokojné", ["Normal"] = "Bežné", ["Fast"] = "Rýchle" }, cs = { ["Flower"] = "Květ", ["Pink"] = "Růžový", ["Yellow"] = "Žlutý", ["Red"] = "Červený", ["Pace"] = "Tempo", ["Calm"] = "Klidné", ["Normal"] = "Běžné", ["Fast"] = "Rychlé" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "flower", label = T("Flower"), type = "int", default = 0, opts = { T("Pink"), T("Yellow"), T("Red") } }, { key = "pace", label = T("Pace"), type = "int", default = 1, opts = { T("Calm"), T("Normal"), T("Fast") } }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, ceil, sin, sqrt, max, min = math.floor, math.ceil, math.sin, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU, PI = 6.28318, 3.14159 local CYCLE = 42 local DAY = CYCLE / 2 local PACES = { 0.7, 1, 1.5 } local PW, PH = 24, 34 local GR = {} local function new_grid() for i = 1, PW * PH do GR[i] = 0 end; return GR end local function put(g, x, y, c) if x >= 0 and x < PW and y >= 0 and y < PH then g[y * PW + x + 1] = c end end local function box(g, x, y, w, h, c) for yy = y, y + h - 1 do for xx = x, x + w - 1 do put(g, xx, yy, c) end end end local function capsule(g, cx, ytop, ybot, rx) for y = ytop, ybot do local hw, d = rx, ytop + rx - (y + 0.5) if d > 0 then hw = sqrt(max(0, rx * rx - d * d)) end for x = floor(cx - hw + 0.5), floor(cx + hw - 0.5) do local dx = (x + 0.5 - cx) / rx local c = dx < -0.45 and 2 or (dx > 0.5 and 3 or 1) if x == cx - 1 and y > ytop + 2 then c = 3 end if y % 4 == 1 and y > ytop + 1 and (x == cx - 3 or x == cx + 1) then c = 4 end put(g, x, y, c) end end end local STAGES = { [0] = { 15, 2, false, 0 }, [1] = { 11, 3, false, 0 }, [2] = { 6, 4, true, 0 }, [3] = { 6, 4, true, 1 }, [4] = { 6, 4, true, 2 }, [5] = { 6, 4, true, 3 }, [6] = { 6, 4, true, 4 }, [7] = { 6, 4, true, 5 }, } local function flower(g, kind, f) if kind == 1 then box(g, 11, f - 2, 2, 2, 9); put(g, 11, f - 2, 10) elseif kind == 2 then box(g, 11, f - 3, 2, 1, 9); box(g, 10, f - 2, 4, 2, 9); put(g, 11, f - 3, 10); put(g, 10, f - 2, 10) elseif kind == 3 then box(g, 9, f - 4, 2, 1, 9); box(g, 13, f - 4, 2, 1, 9) box(g, 9, f - 3, 6, 2, 9); box(g, 11, f - 3, 2, 2, 11); box(g, 10, f - 1, 4, 1, 9) put(g, 9, f - 3, 10); put(g, 14, f - 3, 10) elseif kind == 4 then box(g, 10, f - 5, 4, 1, 9); box(g, 8, f - 4, 8, 1, 9); box(g, 6, f - 3, 12, 2, 9) box(g, 7, f - 1, 10, 1, 9); box(g, 9, f, 6, 1, 9) box(g, 10, f - 3, 4, 2, 11) box(g, 8, f - 4, 2, 1, 10); box(g, 6, f - 3, 2, 1, 10); box(g, 14, f - 4, 2, 1, 10); box(g, 16, f - 3, 2, 1, 10) elseif kind == 5 then box(g, 7, f - 1, 10, 1, 12); box(g, 9, f - 2, 6, 1, 12); box(g, 10, f - 1, 4, 1, 13) box(g, 6, f, 2, 2, 12); box(g, 16, f, 2, 2, 12); put(g, 6, f + 2, 13); put(g, 17, f + 2, 13) end end local function build_plant(st) local g = new_grid() box(g, 2, 22, 20, 3, 5); box(g, 2, 22, 20, 1, 7); box(g, 2, 24, 20, 1, 6) for i = 0, 8 do local w = 16 - (i * 4) // 8 local x = 12 - w // 2 box(g, x, 25 + i, w, 1, 5); box(g, x, 25 + i, 2, 1, 7); box(g, x + w - 3, 25 + i, 3, 1, 6) end box(g, 4, 21, 16, 1, 8) local s = STAGES[st] capsule(g, 12, s[1], 20, s[2]) if s[3] then box(g, 16, 14, 2, 3, 1) capsule(g, 19, 9, 16, 2) end flower(g, s[4], s[1]) local t = {} for y = 0, PH - 1 do t[y + 1] = string.char(table.unpack(g, y * PW + 1, y * PW + PW)) end return table.concat(t) end local SPR = {} local function plant(st) local s = SPR[st] if not s then s = build_plant(st); SPR[st] = s end return s end local OW, OH = 50, 34 local HILLS local function build_hills() local far, near = {}, {} for x = 0, OW - 1 do far[x] = 25 + 2.2 * sin(x * 0.17 + 0.5) + 1.5 * sin(x * 0.41) near[x] = 29 + 1.8 * sin(x * 0.11 + 2) + 1.2 * sin(x * 0.33 + 1) end local t = {} for y = 0, OH - 1 do local row = {} for x = 0, OW - 1 do row[x + 1] = y >= near[x] and 1 or (y >= far[x] and 2 or 0) end t[y + 1] = string.char(table.unpack(row)) end HILLS = table.concat(t) end local FL = { [0] = { { 244, 112, 164 }, { 255, 176, 208 }, { 255, 224, 90 } }, [1] = { { 255, 208, 60 }, { 255, 238, 140 }, { 230, 120, 40 } }, [2] = { { 226, 40, 60 }, { 255, 112, 120 }, { 255, 214, 80 } }, } local BASE = { { 62, 140, 78 }, { 104, 190, 100 }, { 34, 92, 60 }, { 240, 236, 190 }, { 196, 100, 60 }, { 140, 66, 44 }, { 228, 140, 92 }, { 64, 40, 30 }, false, false, false, { 156, 112, 98 }, { 112, 82, 72 }, } local WALL, FRAME, SILL, SILL_D = { 206, 188, 158 }, { 236, 226, 202 }, { 226, 212, 184 }, { 150, 128, 104 } local CAN, CAN_L = { 128, 156, 176 }, { 184, 208, 222 } local KEYS = { { 0.00, 10, 14, 46, 22, 30, 78, 1 }, { 0.10, 12, 16, 52, 26, 34, 84, 1 }, { 0.17, 74, 92, 170, 250, 150, 120, 0.4 }, { 0.27, 84, 160, 246, 170, 214, 255, 0 }, { 0.55, 84, 160, 246, 170, 214, 255, 0 }, { 0.66, 96, 84, 170, 255, 140, 80, 0.4 }, { 0.76, 14, 18, 56, 26, 34, 84, 1 }, { 1.00, 10, 14, 46, 22, 30, 78, 1 }, } local function sky(p) local a, b = KEYS[1], KEYS[2] for i = 1, #KEYS - 1 do if p >= KEYS[i][1] then a, b = KEYS[i], KEYS[i + 1] end end local t = (p - a[1]) / (b[1] - a[1]) local o = {} for i = 2, 8 do o[i - 1] = a[i] + (b[i] - a[i]) * t end return o end local PAL = {} local function tint(c, n) return floor(c[1] * (1 - 0.5 * n) + 16 * n + 0.5), floor(c[2] * (1 - 0.5 * n) + 22 * n + 0.5), floor(c[3] * (1 - 0.3 * n) + 56 * n + 0.5) end local function span(xl, xr, y, r, g, b) local il, ir = ceil(xl), floor(xr) if ir > il then rect(il, y, ir - il, 1, r, g, b) end if il - xl > 0.04 then blend(il - 1, y, r, g, b, il - xl) end if xr - ir > 0.04 then blend(ir, y, r, g, b, xr - ir) end end local function fb(x, y, w, h, r, g, b, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, w - 1, h, r, g, b, a) blend_rect(ix, y, 1, h, r, g, b, a * (1 - f)) blend_rect(ix + w, y, 1, h, r, g, b, a * f) end local function disc(cx, cy, rr, r, g, b) for y = floor(cy - rr), ceil(cy + rr) - 1 do local d = y + 0.5 - cy local q = rr * rr - d * d if q > 0 then local hw = sqrt(q) span(cx - hw, cx + hw, y, r, g, b) end end end local STARS = {} for i = 1, 16 do local h = hash32(i * 0x9E3779B1) STARS[i] = { 8 + umod(h, 48), 6 + umod(h >> 8, 20), umod(h >> 16, 628) / 100 } end local PX0, PY0 = 8, 7 local PETAL_X = { 8, 12, 28, 31, 10, 30 } function render() if not HILLS then build_hills() end local pace = PACES[(cfg.pace or 1) + 1] or 1 local u = (frame * pace / FPS) % CYCLE local p = (u / DAY + 0.88) % 1 local sk = sky(p) local n = sk[7] for i = 1, 13 do local c = BASE[i] if i == 9 or i == 10 or i == 11 then c = FL[cfg.flower or 0][i - 8] end if c then PAL[i * 3 + 1], PAL[i * 3 + 2], PAL[i * 3 + 3] = tint(c, n) end end PAL[1], PAL[2], PAL[3] = 0, 0, 0 rect(0, 0, W, H, tint(WALL, n)) rect(0, 60, W, 4, tint(SILL_D, n)) rect(4, 2, 56, 40, tint(FRAME, n)) gfx.clip(7, 5, 50, 34) for y = 5, 38 do local t = (y - 5) / 33 rect(7, y, 50, 1, floor(sk[1] + (sk[4] - sk[1]) * t + 0.5), floor(sk[2] + (sk[5] - sk[2]) * t + 0.5), floor(sk[3] + (sk[6] - sk[3]) * t + 0.5)) end if n > 0.5 then for i = 1, #STARS do local s = STARS[i] blend(s[1], s[2], 230, 234, 255, (n - 0.5) * 2 * (0.45 + 0.4 * sin(u * 1.6 + s[3]))) end end if p >= 0.10 and p <= 0.72 then local s = (p - 0.10) / 0.62 disc(9 + 46 * s, 35 - 24 * sin(PI * s), 4.5, 255, 214, 120) disc(9 + 46 * s, 35 - 24 * sin(PI * s), 3.5, 255, 242, 168) end local q = ((p - 0.74) % 1) / 0.38 if q <= 1 then local cx, cy = 9 + 46 * q, 35 - 22 * sin(PI * q) disc(cx, cy, 3.5, 236, 232, 200) blend(floor(cx - 1), floor(cy - 1), 176, 172, 150, 0.8) blend(floor(cx + 1), floor(cy + 1), 176, 172, 150, 0.8) end local day = 1 - n if day > 0.05 then for i = 0, 1 do local cx = (u * 1.3 + i * 31) % 74 - 12 local a = 0.85 * day fb(cx + 3, 11 + i * 8, 9, 1, 250, 252, 255, a) fb(cx, 12 + i * 8, 14, 2, 250, 252, 255, a) end end local h1, h2 = { tint({ 150, 190, 140 }, n) }, { tint({ 96, 156, 100 }, n) } blit(HILLS, { 0, 0, 0, h1[1], h1[2], h1[3], h2[1], h2[2], h2[3] }, 7, 5, OW, OH, OW, OH, true) gfx.clip() rect(31, 5, 2, 34, tint(FRAME, n)) rect(7, 21, 50, 2, tint(FRAME, n)) rect(2, 41, 60, 3, tint(SILL, n)) rect(2, 41, 60, 1, tint({ 246, 238, 216 }, n)) rect(3, 44, 58, 1, tint(SILL_D, n)) rect(46, 36, 9, 5, tint(CAN, n)); rect(46, 36, 9, 1, tint(CAN_L, n)) rect(55, 36, 3, 1, tint(CAN, n)); rect(57, 34, 1, 3, tint(CAN, n)) rect(44, 34, 1, 7, tint(CAN, n)); rect(45, 34, 3, 1, tint(CAN, n)) local st if u < 3 then st = 0 elseif u < 6.5 then st = 1 elseif u < 10 then st = 2 elseif u < 18 then st = 3 elseif u < 29 then st = 4 elseif u < 30.5 then st = 5 elseif u < 38 then st = 6 elseif u < 40.2 then st = 7 else st = 2 end blit(plant(st), PAL, PX0, PY0, PW, PH, PW, PH, true) local marks = { 0, 3, 6.5, 10, 18, 29, 30.5, 38 } for i = 1, #marks do local d = (u - marks[i]) * FPS / pace if d >= 0 and d < 12 then local top = PY0 + STAGES[st][1] - 3 local r = 2 + d * 0.5 local a = 1 - d / 12 for k = 0, 5 do local ang = k * 1.0472 + 0.5 blend(floor(20 + r * 1.6 * sin(ang)), floor(top + r * sin(ang + 1.5708)), 255, 248, 200, a) end end end if u >= 40.2 then local c = FL[cfg.flower or 0][1] local pr, pg, pb = tint(c, n) for i = 1, #PETAL_X do local t0 = 40.2 + (i - 1) * 0.15 local k = (u - t0) / 1.0 local lx = PETAL_X[i] if k >= 1 then rect(lx, 40, 2, 1, pr, pg, pb) elseif k >= 0 then local x = 20 + (lx - 20) * k + 2 * sin(k * 9 + i) local y = 12 + 28 * k * k * 0.5 + 14 * k blend_rect(floor(x), floor(y), 2, 1, pr, pg, pb, 1) end end end end