info = { type = "image", title = "Pink bloom", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Three flowers sway in the wind under pink sky", api = 41, } local tr = ({ sk = { ["Sky colour"] = "Farba oblohy", ["Wind"] = "Vietor" }, cs = { ["Sky colour"] = "Barva oblohy", ["Wind"] = "VĂ­tr" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky colour"), type = "color", default = "#F2168E" }, { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, max = math.floor, math.sin, math.max local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local function runs(rows, pal) local S = {} for r = 1, #rows do local s, out, k, n = rows[r], {}, 1, #rows[r] while k <= n do local ch, e = s:sub(k, k), k while e < n and s:sub(e + 1, e + 1) == ch do e = e + 1 end if ch ~= "." then out[#out + 1] = { k - 1, e - k + 1, pal[ch] } end k = e + 1 end S[r] = out end return S end local function sprite(S, x, y, sh) local n = #S for r = 1, n do local xr = x + sh * (n - r) local ix = floor(xr) local f = xr - ix local row, yy, m = S[r], y + r - 1, #S[r] for k = 1, m do local e = row[k] local a, w, c = ix + e[1], e[2], e[3] if k == 1 then blend_rect(a, yy, 1, 1, c[1], c[2], c[3], 1 - f) if w > 1 then rect(a + 1, yy, w - 1, 1, c[1], c[2], c[3]) end else rect(a, yy, w, 1, c[1], c[2], c[3]) end if k == m and f > 0.03 then blend_rect(a + w, yy, 1, 1, c[1], c[2], c[3], f) end end end end local TULIP = runs({ "a..b..a", "ab.b.ba", "abbbbba", "aabbbaa", "caabaac", "ccaaacc", ".ccacc.", "..ccc.." }, { a = { 166, 74, 214 }, b = { 208, 146, 246 }, c = { 114, 42, 164 } }) local POPPY = runs({ "..aa.aa..", ".aaaaaaa.", "aabaaabaa", "aaaykyaaa", "aabkkkbaa", ".aaykyaa.", "..aaaaa..", "...ggg..." }, { a = { 232, 36, 48 }, b = { 255, 116, 104 }, y = { 255, 206, 64 }, k = { 70, 20, 34 }, g = { 60, 150, 56 } }) local CORN = runs({ "b...b...b", ".b.aba.b.", "baaabaaab", ".aaaaaaa.", "aaaapaaaa", ".caaaaac.", "..ccgcc.." }, { a = { 52, 112, 240 }, b = { 130, 186, 255 }, c = { 30, 70, 184 }, p = { 116, 64, 196 }, g = { 60, 150, 56 } }) local FL = { { bx = 12, by = 57, L = 21, S = TULIP, hw = 3.5, A = 0.085, w = TAU / 3.1, ph = 0.0, lh = 8, lf = 1 }, { bx = 31, by = 58, L = 29, S = POPPY, hw = 4.5, A = 0.065, w = TAU / 3.7, ph = 1.9, lh = 11, lf = -1 }, { bx = 50, by = 57, L = 18, S = CORN, hw = 4.5, A = 0.095, w = TAU / 2.8, ph = 3.7, lh = 7, lf = 1 }, } local STEM, LEAF, LEAF_L, LEAF_D = { 62, 150, 58 }, { 84, 184, 70 }, { 140, 220, 110 }, { 48, 120, 48 } local GRASS, GRASS_D, GRASS_L = { 76, 172, 72 }, { 56, 140, 62 }, { 128, 212, 96 } local CLOUD_ART = { { ".......wwww.........", "....wwwwwwwww.......", "..wwwwwwwwwwwwwww...", ".wwwwwwwwwwwwwwwwww.", "wwwwwwwwwwwwwwwwwwww", ".ssssssssssssssssss." }, { "..www...", ".wwwwww.", "wwwwwwww", ".ssssss." }, { "...wwww.....", ".wwwwwwwww..", "wwwwwwwwwwww", ".ssssssssss." }, } local CLOUDS = { { 1, 4, 1.5, 30 }, { 2, 16, 2.7, 5 }, { 3, 25, 1.95, 50 } } local CS = {} local SKY = { 242, 22, 142 } local WIND = 1.3 local BG = nil local READY = false local function apply() READY = true local C = cfg or {} local c = C.sky or { r = 242, g = 22, b = 142 } SKY = { c.r, c.g, c.b } WIND = 0.4 + 0.3 * (C.wind or 3) local s = { floor(255 - (255 - c.r) * 0.28), floor(255 - (255 - c.g) * 0.28), floor(255 - (255 - c.b) * 0.28) } for i = 1, #CLOUD_ART do CS[i] = runs(CLOUD_ART[i], { w = { 255, 255, 255 }, s = s }) end BG = nil end function on_settings() apply() end local function ground(x) return 55 + floor(1.5 * sin(x * 0.12 + 0.5) + 1.2 * sin(x * 0.31) + 0.5) end local function build_bg() for y = 0, H - 1 do local u = 0.30 * y / (H - 1) rect(0, y, W, 1, floor(SKY[1] + (255 - SKY[1]) * u + 0.5), floor(SKY[2] + (255 - SKY[2]) * u + 0.5), floor(SKY[3] + (255 - SKY[3]) * u + 0.5)) end for x = 0, W - 1 do local g = ground(x) rect(x, g, 1, H - g, c3(GRASS)) px(x, g, c3(GRASS_L)) if (hash32(x * 0x9E3779B1) & 3) == 0 then px(x, g - 1, c3(GRASS)) end end rect(0, 61, W, 3, c3(GRASS_D)) for k = 1, 9 do local h = hash32(k * 0x85EBCA6B) local x = 2 + (h & 0x7FFF) % (W - 4) local y = ground(x) + 2 + ((h >> 15) & 0x7FFF) % 3 px(x, y, 250, 250, 250); px(x, y - 1, 255, 220, 80) end BG = gfx.snapshot(0, 0, W, H) end local function theta(F, t) local g = max(0, sin(TAU * (t - F.bx * 0.027) / 9)) return WIND * F.A * (0.55 + sin(F.w * t + F.ph) + 0.35 * sin(2.3 * F.w * t + 2 * F.ph) + 0.8 * g * g) end local SX = {} local function stem(F, th, thh) local L, by = F.L, F.by for h = 0, L do local u = h / L SX[h] = F.bx - 1 + L * u * u * ((1 - u) * th + u * thh) end local r, g, b = c3(STEM) local h = 0 while h <= L do local ix = floor(SX[h]) local fs, h2 = SX[h] - ix, h while h2 < L and floor(SX[h2 + 1]) == ix do h2 = h2 + 1; fs = fs + SX[h2] - ix end local n = h2 - h + 1 local f = fs / n local y = by - h2 blend_rect(ix, y, 1, n, r, g, b, 1 - f) rect(ix + 1, y, 1, n, r, g, b) if f > 0.04 then blend_rect(ix + 2, y, 1, n, r, g, b, f) end h = h2 + 1 end local lx, ly = floor(SX[F.lh] + 0.5), by - F.lh if F.lf > 0 then rect(lx + 2, ly, 3, 1, c3(LEAF_D)); rect(lx + 3, ly - 1, 3, 1, c3(LEAF)); px(lx + 5, ly - 2, c3(LEAF_L)) else rect(lx - 3, ly, 3, 1, c3(LEAF_D)); rect(lx - 4, ly - 1, 3, 1, c3(LEAF)); px(lx - 4, ly - 2, c3(LEAF_L)) end return SX[L] + 1 end local BUTTER, BUTTER_T, BODY = { 255, 226, 70 }, { 255, 150, 40 }, { 70, 40, 40 } local PETAL = { 232, 36, 48 } local function window(t, shift) local c = data.time if c.ok then return (c.unix + shift) % 14 + c.ms / 1000 end return (t + shift) % 14 end local function butterfly(t) local d = window(t, 0) if d > 2.4 then return end local x = floor(W + 3 - d * 30) local y = floor(20 + 6 * sin(d * 2.7) + 2 * sin(d * 9.3) + 0.5) if floor(t * 10) % 2 == 0 then rect(x - 2, y - 1, 2, 2, c3(BUTTER)); rect(x + 1, y - 1, 2, 2, c3(BUTTER)) px(x - 2, y - 1, c3(BUTTER_T)); px(x + 2, y - 1, c3(BUTTER_T)) else rect(x - 1, y - 2, 1, 2, c3(BUTTER)); rect(x + 1, y - 2, 1, 2, c3(BUTTER)) end rect(x, y - 1, 1, 3, c3(BODY)) end local function petal(t) local d = window(t, 7) if d > 5 then return end local F = FL[2] local x0 = F.bx + F.L * theta(F, t - d - 0.27) + 2 local x = x0 - 7.5 * d + 2.5 * sin(d * 2.4) local y = F.by - F.L - 4 + 6.6 * d if y >= 56 then return end local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, b = c3(PETAL) blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, r, g, b, fx * fy) end function render() if not READY then apply() end if not BG then build_bg() end local t = frame / FPS gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #CLOUDS do local c = CLOUDS[i] local w = #CLOUD_ART[c[1]][1] sprite(CS[c[1]], (c[4] - t * c[3]) % (W + w) - w, c[2], 0) end for i = 1, #FL do local F = FL[i] local th, thh = theta(F, t), theta(F, t - 0.27) local hx = stem(F, th, thh) sprite(F.S, hx - F.hw, F.by - F.L - #F.S, thh * 0.9) end butterfly(t) petal(t) end