info = { type = "image", title = "Cherry pond", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A gust strips cherry petals onto a pond", api = 44, } local ltr = ({ sk = { ["Blossom"] = "Kvet", ["Pink"] = "Ružový", ["White"] = "Biely", ["Deep pink"] = "Sýto ružový", ["Petals"] = "Lupene", ["Wind"] = "Vietor", ["Calm"] = "Pokojný", ["Breezy"] = "Svieži", ["Gusty"] = "Nárazový" }, cs = { ["Blossom"] = "Květ", ["Pink"] = "Růžový", ["White"] = "Bílý", ["Deep pink"] = "Sytě růžový", ["Petals"] = "Lístky", ["Wind"] = "Vítr", ["Calm"] = "Klidný", ["Breezy"] = "Svěží", ["Gusty"] = "Nárazový" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "bloom", label = LT("Blossom"), type = "int", default = "pink", opts = { keys = { "pink", "white", "deep" }, LT("Pink"), LT("White"), LT("Deep pink") } }, { key = "petals", label = LT("Petals"), type = "int", min = 4, max = 20, default = 12 }, { key = "wind", label = LT("Wind"), type = "int", default = "breezy", opts = { keys = { "calm", "breezy", "gusty" }, LT("Calm"), LT("Breezy"), LT("Gusty") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, exp, pi, max, min = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.pi, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local BLOOMS = { pink = { sh = { 214, 112, 156 }, mid = { 255, 164, 200 }, lt = { 255, 214, 232 } }, white = { sh = { 190, 192, 226 }, mid = { 240, 238, 250 }, lt = { 255, 255, 255 } }, deep = { sh = { 150, 36, 104 }, mid = { 222, 70, 150 }, lt = { 255, 148, 196 } }, } local B = BLOOMS.pink local BARK, BARK_L, BARK_D = { 88, 54, 40 }, { 130, 84, 58 }, { 62, 38, 30 } local PERIODS = { calm = 22, breezy = 16, gusty = 12 } local PER = 16 local function frect(xl, xr, y, h, r, g, b, a) a = a or 1 local il, ir = floor(xl), floor(xr) if ir == il then if xr > xl then blend_rect(il, y, 1, h, r, g, b, (xr - xl) * a) end return end if ir > il + 1 then if a >= 1 then rect(il + 1, y, ir - il - 1, h, r, g, b) else blend_rect(il + 1, y, ir - il - 1, h, r, g, b, a) end end local fl, fr = (1 - (xl - il)) * a, (xr - ir) * a if fl > 0.02 then blend_rect(il, y, 1, h, r, g, b, fl) end if fr > 0.02 then blend_rect(ir, y, 1, h, r, g, b, fr) end end local function disc(cx, cy, r, c) local y0 = floor(cy + 0.5) for dy = -floor(r), floor(r) do local hw = sqrt(max(0, r * r - dy * dy * 1.15)) if hw > 0.4 then frect(cx - hw, cx + hw, y0 + dy, 1, c[1], c[2], c[3]) end end end local function clump(cx, cy, r) disc(cx, cy, r, B.sh) disc(cx - 0.1 * r, cy - 0.2 * r, 0.9 * r, B.mid) disc(cx - 0.3 * r, cy - 0.45 * r, 0.52 * r, B.lt) end local WY = 44 local RX0, RY0, RW, RH = 24, 5, 40, 24 local BASE, BRS, ROWS = nil, {}, {} local function bank_y(x) return floor(52 - x * 0.17 + 0.5) end local function st_world() for y = 0, WY - 1 do local t = y / (WY - 1) rect(0, y, W, 1, floor(126 + 88 * t + 0.5), floor(184 + 50 * t + 0.5), floor(238 + 12 * t + 0.5)) end for x = 0, W - 1 do local top = floor(39 + 1.6 * sin(x * 0.31) + 1.2 * sin(x * 0.77 + 1) + 0.5) rect(x, top, 1, WY - top, 132, 184, 188) end for y = WY, H - 1 do local t = (y - WY) / (H - 1 - WY) rect(0, y, W, 1, floor(88 - 42 * t + 0.5), floor(150 - 50 * t + 0.5), floor(196 - 36 * t + 0.5)) end for y = 27, 46 do local w = 4 + floor((y - 27) * 0.25) local x0 = 12 - w // 2 rect(x0, y, w, 1, BARK[1], BARK[2], BARK[3]) rect(x0 + w - 1, y, 1, 1, BARK_L[1], BARK_L[2], BARK_L[3]) px(x0, y, BARK_D[1], BARK_D[2], BARK_D[3]) end for k = 0, 8 do rect(11 - k, 26 - k // 2 - 0, 2, 2, BARK[1], BARK[2], BARK[3]) rect(13 + k, 26 - k // 2, 2, 2, BARK_L[1], BARK_L[2], BARK_L[3]) end local cl = { { 7, 15, 7 }, { 21, 17, 7 }, { 15, 9, 8 }, { 4, 22, 5 }, { 24, 10, 5 }, { 13, 19, 7 }, { 20, 23, 5 }, { 9, 25, 4 }, { 3, 12, 5 }, { 26, 24, 4 } } for i = 1, #cl do clump(cl[i][1], cl[i][2], cl[i][3]) end end local function st_shore() for y = 0, WY - 1 do ROWS[y] = snapshot(0, y, W, 1) end for y = WY, H - 1 do local t = (y - WY) / (H - 1 - WY) blit_rgb(ROWS[87 - y], 0, y, W, 1) blend_rect(0, y, W, 1, floor(88 - 42 * t + 0.5), floor(150 - 50 * t + 0.5), floor(196 - 36 * t + 0.5), 0.55 + 0.2 * t) end for x = 0, 31 do local top = floor(41 + 0.011 * (x - 12) ^ 2 + 0.5) local bot = bank_y(x) rect(x, top, 1, bot - top + 1, 70, 150, 64) if x % 3 ~= 1 then rect(x, top, 1, 1, 118, 196, 92) end rect(x, bot, 1, 1, 52, 112, 56) end for x = 0, 13, 2 do rect(x, 57 - x // 3, 2, 7 + x // 3, 70, 150, 64); px(x, 57 - x // 3, 118, 196, 92) end BASE = snapshot(0, 0, W, H) end local LIMB = { { 24, 24 }, { 30, 22 }, { 37, 19 }, { 44, 17 }, { 52, 15 }, { 58, 13 } } local function branch(s) for i = 1, #LIMB - 1 do local a, b = LIMB[i], LIMB[i + 1] for xx = a[1], b[1] - 1 do local y = floor(a[2] + (b[2] - a[2]) * (xx - a[1]) / (b[1] - a[1]) + 0.5) frect(xx + s, xx + 1 + s, y, 1, BARK_L[1], BARK_L[2], BARK_L[3]) frect(xx + s, xx + 1 + s, y + 1, 1, BARK[1], BARK[2], BARK[3]) end end local tw = { { 33, 21, 36, 13 }, { 45, 17, 50, 9 }, { 53, 15, 56, 22 } } for i = 1, #tw do local t = tw[i] for k = 0, 7 do local f = k / 7 frect(t[1] + (t[3] - t[1]) * f + s, t[1] + (t[3] - t[1]) * f + 1 + s, floor(t[2] + (t[4] - t[2]) * f + 0.5), 1, BARK[1], BARK[2], BARK[3]) end end local cl = { { 31, 19, 4 }, { 37, 23, 3 }, { 47, 21, 4 }, { 38, 14, 5 }, { 44, 11, 5 }, { 36, 12, 4 }, { 50, 12, 4 }, { 54, 9, 5 }, { 58, 12, 4 }, { 55, 20, 3 } } for i = 1, #cl do clump(cl[i][1] + s, cl[i][2], cl[i][3]) end end local STEPS = 5 local STAGES = { st_world, st_shore } for k = 0, STEPS - 1 do STAGES[#STAGES + 1] = function() blit_rgb(BASE, 0, 0, W, H) branch(k * 0.25) BRS[k] = snapshot(RX0, RY0, RW, RH) end end local STAGE, PART = 1, nil local function build() if PART then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then PART = snapshot(0, 0, W, H) end end function on_settings() B = BLOOMS[cfg.bloom] or BLOOMS.pink PER = PERIODS[cfg.wind] or 16 BASE, STAGE, PART = nil, 1, nil end local G0, GLEN = 1.0, 3.0 local NP = 20 local PT = {} for i = 1, NP do local h = hash32(i * 0x9E3779B1) local sy, yl, vt = 8 + umod(h >> 6, 16), 49 + umod(h >> 22, 10), 10 + umod(h >> 18, 5) PT[i] = { sx = 18 + umod(h, 22), sy = sy, v = 6 + umod(h >> 10, 5), tau = 1 + umod(h >> 14, 8) / 10, vt = vt, yl = yl, dl = umod(h >> 8, 100) / 100 * 2.2, ph = umod(h >> 12, 628) / 100, fw = 6 + umod(h >> 4, 40) / 10, tl = (yl - sy) / vt } end local LIFE = 3.2 local function dot(x, y, r, g, b, a) local fx, fy = floor(x), floor(y) local ax, ay = x - fx, y - fy blend(fx, fy, r, g, b, a * (1 - ax) * (1 - ay)) blend(fx + 1, fy, r, g, b, a * ax * (1 - ay)) blend(fx, fy + 1, r, g, b, a * (1 - ax) * ay) blend(fx + 1, fy + 1, r, g, b, a * ax * ay) end local function ring(x, y, r, a) local n = max(6, floor(r * 3)) for k = 0, n - 1 do local an = k / n * 2 * pi blend(floor(x + r * cos(an) + 0.5), floor(y + r * 0.45 * sin(an) + 0.5), 214, 238, 252, a) end end local function petal(P, t) local tl = (t - G0 - P.dl) % PER local fall = P.tl if tl > fall + LIFE then return end local m, l = B.mid, B.lt if tl < fall then local u = tl / P.tau local x = P.sx + P.v * 2.718 * P.tau * (1 - exp(-u) * (1 + u)) + 1.3 * sin(tl * P.fw + P.ph) local y = P.sy + P.vt * tl + 0.7 * cos(tl * P.fw * 0.8 + P.ph) local pose = floor(tl * 7 + P.ph) % 4 if pose == 3 then dot(x, y, l[1], l[2], l[3], 1) else dot(x, y, m[1], m[2], m[3], 1) end if pose == 1 then dot(x + 1, y, m[1], m[2], m[3], 0.6) elseif pose == 2 then dot(x, y + 1, m[1], m[2], m[3], 0.6) end else local u = fall / P.tau local xl = P.sx + P.v * 2.718 * P.tau * (1 - exp(-u) * (1 + u)) local d = tl - fall local fade = d > LIFE - 1.2 and (LIFE - d) / 1.2 or 1 dot(xl + d * 0.5, P.yl, m[1], m[2], m[3], fade) dot(xl + d * 0.5 + 1, P.yl, l[1], l[2], l[3], 0.7 * fade) if d < 1.5 then ring(xl, P.yl, 0.8 + 4.5 * d, 0.5 * (1 - d / 1.5)) end if d > 0.4 and d < 1.6 then ring(xl, P.yl, 0.5 + 3.5 * (d - 0.4), 0.3 * (1 - (d - 0.4) / 1.2)) end end end local SP = {} for i = 1, 6 do local h = hash32(i * 0x85EBCA6B) SP[i] = { x = 26 + umod(h, 34), y = 52 + umod(h >> 8, 10), w = 1.5 + umod(h >> 14, 8) * 0.25, ph = umod(h >> 18, 628) / 100 } end function render() if STAGE <= #STAGES then build() if STAGE <= 3 then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BASE, 0, 0, W, H) local u = (t % PER - G0) / GLEN local k = 0 if u > 0 and u < 1 then k = floor(sin(pi * u) ^ 2 * (STEPS - 1) + 0.5) end blit_rgb(BRS[k], RX0, RY0, RW, RH) for i = 1, #SP do local s = SP[i] local v = sin(t * s.w + s.ph) if v > 0.4 then blend_rect(s.x, s.y, 2, 1, 255, 255, 255, (v - 0.4) * 0.6) end end for i = 1, cfg.petals or 12 do petal(PT[i], t) end end