info = { type = "image", title = "Moon blossom", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Blossoming branch under a haloed violet moon", api = 44, } local ltr = ({ sk = { ["Petals"] = "Lupene", ["Stars"] = "Hviezdy", ["Haze"] = "Opar" }, cs = { ["Petals"] = "Okvětní lístky", ["Stars"] = "Hvězdy", ["Haze"] = "Opar" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "petals", label = LT("Petals"), type = "int", min = 0, max = 14, default = 9 }, { key = "stars", label = LT("Stars"), type = "int", min = 0, max = 20, default = 14 }, { key = "haze", label = LT("Haze"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local SKYT, SKYB = { 58, 34, 116 }, { 176, 98, 184 } local HALO, MOON, MOON_S = { 238, 172, 236 }, { 255, 242, 250 }, { 214, 176, 222 } local HILL_F, HILL_N = { 134, 72, 160 }, { 86, 42, 116 } local TWIG, RIM = { 30, 12, 52 }, { 112, 58, 138 } local PK_L, PK_M, PK_D = { 255, 224, 240 }, { 240, 150, 208 }, { 178, 86, 154 } local MX, MY, MR, HR = 15, 14, 6.5, 18 local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function rnd(i, s) return (hash32(i * 0x9E3779B1 + s) & 0x7FFF) / 32767 end local SEG = { { 66, 63, 50, 49, 9, 6 }, { 50, 49, 38, 41, 6, 4 }, { 38, 41, 28, 36, 4, 2.2 }, { 28, 36, 21, 31, 2.2, 1.2 }, { 50, 49, 55, 33, 4, 3 }, { 55, 33, 58, 22, 3, 1.5 }, { 55, 33, 63, 27, 2.2, 1.2 }, { 40, 40, 44, 29, 2.4, 1.8 }, { 44, 29, 46, 20, 1.8, 1.2 }, { 44, 29, 37, 24, 1.6, 1.0 }, { 47, 52, 35, 57, 3, 1.5 }, { 35, 57, 27, 59, 1.5, 1.0 }, } local SQ = {} for i = 1, #SEG do local s = SEG[i] local n = floor(max(abs(s[3] - s[1]), abs(s[4] - s[2])) * 1.5) + 1 for k = 0, n do local t = k / n local w = max(1, floor(s[5] + (s[6] - s[5]) * t + 0.5)) SQ[#SQ + 1] = { floor(s[1] + (s[3] - s[1]) * t - w / 2 + 0.5), floor(s[2] + (s[4] - s[2]) * t - w / 2 + 0.5), w } end end local BL = {} for i = 3, #SEG do local s = SEG[i] for _, t in ipairs({ 0.45, 1.0 }) do BL[#BL + 1] = { s[1] + (s[3] - s[1]) * t, s[2] + (s[4] - s[2]) * t, 2.4 + s[6] * 0.3 } end end BL[#BL + 1] = { 44, 45, 2.6 } BL[#BL + 1] = { 52, 41, 2.6 } local function sky_col(y) return mix(SKYT, SKYB, y / (H - 1)) end local function s_sky() for y = 0, H - 1 do rect(0, y, W, 1, sky_col(y)) end for x = 0, W - 1 do local fy = floor(53 + 2.2 * sin(x * 0.12 + 0.6) + 1.4 * sin(x * 0.33) + 0.5) rect(x, fy, 1, H - fy, HILL_F[1], HILL_F[2], HILL_F[3]) local ny = floor(59 + 1.6 * sin(x * 0.17 + 2.0) + 0.5) rect(x, ny, 1, H - ny, HILL_N[1], HILL_N[2], HILL_N[3]) end end local function halo(ya, yb) for y = max(ya, MY - HR), min(yb, MY + HR) do local t = y / (H - 1) local sr, sg, sb = SKYT[1] + (SKYB[1] - SKYT[1]) * t, SKYT[2] + (SKYB[2] - SKYT[2]) * t, SKYT[3] + (SKYB[3] - SKYT[3]) * t for x = max(0, MX - HR), min(W - 1, MX + HR) do local dx, dy = x - MX, y - MY local d = sqrt(dx * dx + dy * dy) if d <= MR - 0.5 then px(x, y, mix(MOON, MOON_S, max(0, (dx + dy) / (1.7 * MR)))) elseif d < HR then local k = (1 - (d - MR) / (HR - MR)) * 0.62 local r, g, b = sr + (HALO[1] - sr) * k, sg + (HALO[2] - sg) * k, sb + (HALO[3] - sb) * k if d < MR + 0.5 then local m = MR + 0.5 - d r, g, b = r + (MOON_S[1] - r) * m, g + (MOON_S[2] - g) * m, b + (MOON_S[3] - b) * m end px(x, y, floor(r + 0.5), floor(g + 0.5), floor(b + 0.5)) end end end end local function s_moon_detail() for _, c in ipairs({ { -3, -2 }, { -2, -2 }, { 2, 1 }, { -1, 3 }, { 3, -3 }, { -4, 1 }, { 1, -4 } }) do px(MX + c[1], MY + c[2], 226, 196, 232) end end local function s_rim() for i = 1, #SQ do local q = SQ[i] if q[3] >= 2 then rect(q[1] - 1, q[2] - 1, q[3], q[3], RIM[1], RIM[2], RIM[3]) end end end local function s_body() for i = 1, #SQ do local q = SQ[i] rect(q[1], q[2], q[3], q[3], TWIG[1], TWIG[2], TWIG[3]) end end local function s_bloom() for i = 1, #BL do local b = BL[i] local cx, cy, r = b[1], b[2], b[3] for k = 1, 10 do local a, d = rnd(i * 31 + k, 3) * 2 * PI, sqrt(rnd(i * 31 + k, 4)) * r local ox, oy = cos(a) * d, sin(a) * d * 0.8 local lit = -(ox + oy) / r + (rnd(i * 31 + k, 5) - 0.5) * 0.5 local c = lit > 0.3 and PK_L or (lit < -0.35 and PK_D or PK_M) local x, y = floor(cx + ox + 0.5), floor(cy + oy + 0.5) px(x, y, c[1], c[2], c[3]) if rnd(i * 31 + k, 6) < 0.4 then px(x + 1, y, c[1], c[2], c[3]) end end end end local STAGES = { s_sky, function() halo(0, 15) end, function() halo(16, 32) end, s_moon_detail, s_rim, s_body, s_bloom } local STAGE, PART = 1, nil local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() PART = snapshot(0, 0, W, H) STAGE = STAGE + 1 end function on_settings() STAGE = 1 end local ST = {} do local i, k = 0, 0 while k < 20 and i < 400 do i = i + 1 local h = hash32(i * 0x85EBCA6B + 5) local x, y = umod(h, W), umod(h >> 8, 46) local dx, dy = x - MX, y - MY if dx * dx + dy * dy > 21 * 21 and not (x > 34 and y > 14) then k = k + 1 ST[k] = { x, y, umod(h >> 16, 628) / 100, 0.8 + umod(h >> 20, 20) / 10, k <= 4 } end end end local WISP = { { y = 9, L = 30, v = 1.7, x0 = 20 }, { y = 21, L = 24, v = 1.1, x0 = 90 } } local MARG = 30 local PC = W + 2 * MARG local function frow(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, 206, 140, 214, a) blend_rect(il, y, 1, 1, 206, 140, 214, a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, 206, 140, 214, a * (xr - ir)) end local function petal(i, t) local T = 11 + (i * 7) % 7 local u0 = t / T + i * 0.37 local n, u = floor(u0), u0 % 1 local x0, y0 = 40 + rnd(i, n) * 22, 18 + rnd(i + 50, n) * 30 local x = x0 - (28 + rnd(i + 99, n) * 20) * u + 2 * sin(u * 9 + i) local y = y0 + (14 + rnd(i + 150, n) * 14) * u local a = sqrt(sin(PI * u)) * 0.9 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix, iy, 1, 1, 255, 206, 230, a * (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, 255, 206, 230, a * fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, 255, 206, 230, a * (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, 255, 206, 230, a * fx * fy) end function render() if STAGE <= #STAGES then build() gfx.ready(false) return end blit_rgb(PART, 0, 0, W, H) local t = frame / FPS for i = 1, cfg.stars or 14 do local s = ST[i] local a = 0.3 + 0.7 * (0.5 + 0.5 * sin(t * s[4] + s[3])) blend_rect(s[1], s[2], 1, 1, 255, 236, 255, a) if s[5] then blend_rect(s[1] - 1, s[2], 3, 1, 255, 236, 255, a * 0.45) blend_rect(s[1], s[2] - 1, 1, 3, 255, 236, 255, a * 0.45) end end if cfg.haze ~= false then for i = 1, #WISP do local w = WISP[i] local x = (w.x0 + t * w.v) % PC - MARG frow(x + 4, x + w.L - 4, w.y, 0.12) frow(x, x + w.L, w.y + 1, 0.18) frow(x + 6, x + w.L - 2, w.y + 2, 0.10) end end for i = 1, cfg.petals or 9 do petal(i, t) end end