info = { type = "image", title = "Cloud and sprout", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A smiling cloud rains a flower into bloom", api = 44, } local tr = ({ sk = { ["Flower"] = "Kvet", ["Speed"] = "Rýchlosť" }, cs = { ["Flower"] = "Květ", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bloom", label = T("Flower"), type = "color", default = "#FFA8C8" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect, blend, blit_rgb = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend, gfx.blit_rgb local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local CYC = 600 local SPEED = { 0.7, 1, 1.4 } local KEY = { 7, 3, 9 } local BX, BY = 40, 45 local CW, CHH, CY, CX_REST = 30, 17, 4, 25 local function clamp(v, a, b) return v < a and a or (v > b and b or v) end local function ease(e) return e * e * (3 - 2 * e) end local function sky_rgb(y) local k = y / (H - 1) return floor(168 + 78 * k + 0.5), floor(180 + 34 * k + 0.5), floor(232 - 6 * k + 0.5) end local CIRC = { { 8, 10, 6 }, { 15, 7, 7 }, { 23, 10, 6 } } local CLOUD, EL, ER = {}, {}, {} local CPAL = { 0, 0, 0, 248, 250, 255, 166, 208, 238, 212, 228, 246, 84, 82, 112, 250, 170, 190, 255, 255, 255 } local OUT = { 166, 208, 238 } local function build_cloud() local ins = {} for y = 0, CHH - 1 do ins[y] = {} for x = 0, CW - 1 do local a = false if y <= 15 then for i = 1, 3 do local c = CIRC[i] local dx, dy = x + 0.5 - c[1], y + 0.5 - c[2] if dx * dx + dy * dy <= c[3] * c[3] then a = true end end if y >= 12 and x >= 6 and x <= 24 then a = true end end ins[y][x] = a end end local base = {} for y = 0, CHH - 1 do for x = 0, CW - 1 do local c = 0 if ins[y][x] then local edge = not (ins[y][x - 1] and ins[y][x + 1] and ins[y - 1] and ins[y - 1][x] and ins[y + 1] and ins[y + 1][x]) c = edge and 2 or ((y >= 13) and 3 or 1) end base[y * CW + x + 1] = c end end for v = 0, 1 do local g = table.move(base, 1, CW * CHH, 1, {}) local function P(x, y, c) g[y * CW + x + 1] = c end P(12, 1, 6); P(13, 1, 6); P(7, 5, 6) P(8, 11, 5); P(9, 11, 5); P(21, 11, 5); P(22, 11, 5) P(14, 11, 4); P(16, 11, 4); P(15, 12, 4) if v == 0 then P(11, 8, 4); P(10, 9, 4); P(12, 9, 4); P(19, 8, 4); P(18, 9, 4); P(20, 9, 4) else P(10, 8, 4); P(11, 8, 4); P(10, 9, 4); P(11, 9, 4); P(10, 8, 6) P(19, 8, 4); P(20, 8, 4); P(19, 9, 4); P(20, 9, 4); P(19, 8, 6) end local rows = {} for y = 0, CHH - 1 do rows[y + 1] = string.char(table.unpack(g, y * CW + 1, y * CW + CW)) end CLOUD[v] = table.concat(rows) end for y = 0, CHH - 1 do local lo, hi for x = 0, CW - 1 do if ins[y][x] then lo = lo or x; hi = x end end EL[y], ER[y] = lo, hi end end local function draw_cloud(xf, v) local xi = floor(xf) local f = xf - xi gfx.blit_indexed(CLOUD[v], CPAL, xi, CY, CW, CHH, CW, CHH, true) if f > 0.03 then for y = 0, CHH - 1 do if EL[y] then local sr, sg, sb = sky_rgb(CY + y) blend(xi + EL[y], CY + y, sr, sg, sb, f) blend(xi + ER[y] + 1, CY + y, OUT[1], OUT[2], OUT[3], f) end end end end local SKY, FG = nil, nil local function disc(cx, cy, r, cr, cg, cb) for dy = -r, r do local dx = floor(sqrt(r * r + 0.5 - dy * dy) + 0.5) rect(cx - dx, cy + dy, 2 * dx + 1, 1, cr, cg, cb) end end local function build_sky() for y = 0, H - 1 do rect(0, y, W, 1, sky_rgb(y)) end SKY = gfx.snapshot(0, 0, W, H) end local function build_fg() gfx.clear(KEY[1], KEY[2], KEY[3]) for x = 0, W - 1 do local hy = floor(50 + 3 * sin(x * 0.09 + 1) + 2 * sin(x * 0.23) + 0.5) rect(x, hy, 1, H - hy, 186, 206, 224) end rect(0, 55, W, 9, 170, 214, 170) rect(0, 55, W, 1, 198, 232, 188) rect(0, 60, W, 4, 150, 198, 152) for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 56 + umod(h >> 8, 7) px(x, y, 140, 190, 142) end rect(32, 58, 17, 1, 132, 178, 136) rect(33, 47, 15, 3, 244, 176, 154); rect(33, 47, 15, 1, 252, 202, 182) rect(34, 50, 13, 3, 238, 164, 142); rect(35, 53, 11, 3, 238, 164, 142); rect(36, 56, 9, 3, 238, 164, 142) rect(43, 50, 4, 3, 214, 134, 120); rect(42, 53, 4, 3, 214, 134, 120); rect(41, 56, 4, 3, 214, 134, 120) rect(36, 50, 2, 8, 250, 190, 170) FG = gfx.snapshot(0, 40, W, 24) end local LEAF, LEAF_L, STEM = { 150, 212, 150 }, { 190, 236, 176 }, { 112, 184, 118 } local function leaves(n, y0) if n >= 1 then rect(37, y0 + 3, 3, 2, LEAF[1], LEAF[2], LEAF[3]); px(36, y0 + 3, LEAF[1], LEAF[2], LEAF[3]); px(38, y0 + 3, LEAF_L[1], LEAF_L[2], LEAF_L[3]) rect(41, y0 + 1, 3, 2, LEAF[1], LEAF[2], LEAF[3]); px(44, y0 + 1, LEAF[1], LEAF[2], LEAF[3]); px(42, y0 + 1, LEAF_L[1], LEAF_L[2], LEAF_L[3]) end end local function plant(stage, dy, bc) local y = BY + dy if stage == 0 then rect(39, y - 1, 3, 1, 196, 160, 112); px(40, y - 2, 224, 192, 140) return end if stage == 1 then rect(40, y - 5, 1, 6, STEM[1], STEM[2], STEM[3]) rect(38, y - 6, 2, 1, LEAF[1], LEAF[2], LEAF[3]); rect(41, y - 7, 2, 1, LEAF[1], LEAF[2], LEAF[3]) return end local top = (stage == 2) and 10 or 16 rect(40, y - top, 1, top + 1, STEM[1], STEM[2], STEM[3]) leaves(1, y - 11) if stage == 2 then rect(38, y - top - 1, 2, 1, LEAF[1], LEAF[2], LEAF[3]); rect(41, y - top - 2, 2, 1, LEAF[1], LEAF[2], LEAF[3]) elseif stage == 3 then rect(39, y - top - 4, 3, 4, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5) px(40, y - top - 5, bc[1], bc[2], bc[3]) rect(39, y - top, 3, 1, LEAF[1], LEAF[2], LEAF[3]) else local cy = y - 18 disc(40, cy, 5, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5) disc(40, cy, 4, bc[1], bc[2], bc[3]) px(37, cy - 3, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5); px(43, cy - 3, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5) px(37, cy + 3, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5); px(43, cy + 3, bc[1] * 4 // 5, bc[2] * 4 // 5, bc[3] * 4 // 5) disc(40, cy, 2, 255, 226, 128) px(39, cy - 1, 255, 244, 180) end end local GROW_T = { 120, 160, 200, 250 } local GROW_H = { 5, 10, 16, 23 } local SPARK = { { -5, -2 }, { 5, -4 }, { -4, 4 }, { 6, 2 }, { 0, -7 } } local DROP_X, DROP_P = {}, {} for i = 1, 10 do DROP_X[i] = 33 + (i * 7) % 15; DROP_P[i] = (i * 13) % 36 end local HEART = { ".#.#.", "#####", ".###.", "..#.." } function render() if not CLOUD[0] then build_cloud(); blit_rgb(string.rep("\200\210\240", W * H), 0, 0, W, H); gfx.ready(false) return end if not SKY then build_sky(); gfx.ready(false) return end if not FG then build_fg(); blit_rgb(SKY, 0, 0, W, H); gfx.ready(false) return end local bcfg = cfg.bloom or { r = 255, g = 168, b = 200 } local bc = { bcfg.r, bcfg.g, bcfg.b } local tt = floor(frame * SPEED[cfg.speed or 2] + 0.5) % CYC blit_rgb(SKY, 0, 0, W, H) local rain = clamp((tt - 70) / 10, 0, 1) * clamp((235 - tt) / 10, 0, 1) if rain > 0 then blend_rect(0, 0, W, 56, 120, 130, 170, 0.14 * rain) end local sy = 70 if tt >= 330 and tt < 440 then sy = 70 - 55 * ease((tt - 330) / 110) elseif tt >= 440 and tt < 540 then sy = 15 elseif tt >= 540 and tt < 590 then sy = 15 + 55 * ease((tt - 540) / 50) end if sy < 64 then local warm = 0.4 * clamp((64 - sy) / 49, 0, 1) blend_rect(0, 0, W, 56, 255, 226, 170, warm) local cy = floor(sy + 0.5) for dy = -9, 9 do local dx = floor(sqrt(81.5 - dy * dy)) blend_rect(14 - dx, cy + dy, 2 * dx + 1, 1, 255, 244, 200, 0.16) end disc(14, cy, 6, 255, 236, 140) disc(14, cy, 3, 255, 250, 200) end blit_rgb(FG, 0, 40, W, 24, KEY[1], KEY[2], KEY[3]) local wet = (tt < 300) and clamp((tt - 80) / 150, 0, 1) or clamp(1 - (tt - 300) / 250, 0, 1) rect(35, 45, 11, 2, floor(186 - 68 * wet), floor(138 - 54 * wet), floor(110 - 32 * wet)) local stage = 0 for i = 1, 4 do if tt >= GROW_T[i] then stage = i end end if tt >= 595 then stage = 0 end if tt >= 565 and tt < 595 then gfx.clip(0, 0, W, BY) plant(4, floor(26 * ease((tt - 565) / 30) + 0.5), bc) gfx.clip() else plant(stage, 0, bc) for i = 1, 4 do local d = tt - GROW_T[i] if d >= 0 and d < 14 then for k = 1, 5 do if (d + k) % 3 ~= 0 then blend(BX + SPARK[k][1], BY - GROW_H[i] + SPARK[k][2], 255, 252, 210, 0.9 - d / 18) end end end end end local cx = nil if tt < 70 then cx = -32 + 57 * (1 - (1 - tt / 70) ^ 3) elseif tt < 300 then cx = CX_REST elseif tt < 370 then local e = (tt - 300) / 70; cx = CX_REST + 45 * e * e end if cx and cx < W then local smile = (tt >= 245 and tt < 380) and 1 or 0 draw_cloud(cx, smile) if rain > 0 then for i = 1, 10 do local y = 20 + (frame * 2 + DROP_P[i]) % 36 if y < 44 then rect(DROP_X[i], y, 1, 3, 150, 200, 255) elseif y < 46 then px(DROP_X[i] - 1, 44, 176, 220, 255); px(DROP_X[i] + 1, 44, 176, 220, 255) end end end if smile == 1 and tt < 372 then local hx, hy = floor(cx) + 24, CY - 4 - ((tt - 245) // 40 > 0 and 1 or 0) for r = 1, 4 do local row = HEART[r] for c = 1, 5 do if row:sub(c, c) == "#" then px(hx + c - 1, hy + r - 1, 250, 140, 170) end end end end end end