info = { type = "image", title = "Sunset meadow", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Violet flower meadow above the sea at sunset", api = 44, } local tr = ({ sk = { ["Mood"] = "Nálada", ["Amber"] = "Jantárová", ["Rose"] = "Ružová", ["Dusk"] = "Súmrak", ["Light rays"] = "Lúče" }, cs = { ["Mood"] = "Nálada", ["Amber"] = "Jantarová", ["Rose"] = "Růžová", ["Dusk"] = "Soumrak", ["Light rays"] = "Paprsky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mood", label = T("Mood"), type = "int", default = "amber", opts = { keys = { "amber", "rose", "dusk" }, T("Amber"), T("Rose"), T("Dusk") } }, { key = "rays", label = T("Light rays"), type = "int", min = 0, max = 5, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, sqrt, atan, min, max = math.floor, math.sin, math.sqrt, math.atan, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat, unpack = string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, SX, SY = 31, 45, 30 local MOODS = { amber = { top = { 52, 46, 126 }, mid = { 176, 84, 142 }, hor = { 255, 168, 86 }, glow = { 255, 196, 120 }, ray = { 255, 206, 140 }, sun1 = { 255, 246, 214 }, sun2 = { 255, 214, 130 }, cb = { 104, 56, 112 }, cf = { 232, 140, 150 }, rim = { 255, 176, 96 }, sh = { 246, 150, 100 }, sm = { 150, 84, 150 }, sn = { 44, 52, 118 }, haze = { 196, 110, 140 } }, rose = { top = { 70, 58, 150 }, mid = { 222, 104, 164 }, hor = { 255, 186, 150 }, glow = { 255, 176, 176 }, ray = { 255, 196, 196 }, sun1 = { 255, 248, 236 }, sun2 = { 255, 214, 196 }, cb = { 118, 62, 128 }, cf = { 240, 160, 186 }, rim = { 255, 168, 150 }, sh = { 250, 166, 160 }, sm = { 160, 92, 168 }, sn = { 50, 58, 130 }, haze = { 214, 126, 164 } }, dusk = { top = { 28, 30, 96 }, mid = { 112, 68, 166 }, hor = { 236, 120, 126 }, glow = { 236, 134, 150 }, ray = { 240, 150, 170 }, sun1 = { 255, 226, 196 }, sun2 = { 255, 170, 130 }, cb = { 56, 40, 104 }, cf = { 170, 100, 170 }, rim = { 250, 130, 120 }, sh = { 222, 110, 120 }, sm = { 110, 66, 150 }, sn = { 28, 40, 100 }, haze = { 150, 84, 150 } }, } local P = MOODS.amber local FLOWER = { { 88, 38, 138 }, { 132, 52, 170 }, { 182, 84, 204 }, { 226, 140, 226 } } local GRASS, GRASS2 = { 36, 62, 42 }, { 64, 104, 52 } local function lerp(a, b, t) return a + (b - a) * t end local function mix(c1, c2, t) return lerp(c1[1], c2[1], t), lerp(c1[2], c2[2], t), lerp(c1[3], c2[3], t) end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function vn(i, j, seed) return (hash32(i * 0x1F3D5B79 + j * 0x2545F491 + seed) & 0x7FFF) / 32767 end local G = {} do local pts = { 0, 37, 10, 36, 24, 41, 38, 46, 50, 48, 63, 47 } for x = 0, W - 1 do for i = 1, #pts - 2, 2 do if x <= pts[i + 2] then G[x] = floor(pts[i + 1] + (pts[i + 3] - pts[i + 1]) * (x - pts[i]) / (pts[i + 2] - pts[i]) + (vn(x // 2, 0, 5) - 0.5) * 1.6 + 0.5) break end end end end local function sky() for y = 0, HZ - 1 do local t = y / (HZ - 1) local r, g, b if t < 0.55 then r, g, b = mix(P.top, P.mid, t / 0.55) else r, g, b = mix(P.mid, P.hor, ((t - 0.55) / 0.45) ^ 1.3) end rect(0, y, W, 1, rgb(r, g, b)) end end local function glow(ya, yb) local g = P.glow for y = ya, yb do for x = 0, W - 1 do local dx, dy = (x - SX) / 26, (y - SY) / 17 local d = sqrt(dx * dx + dy * dy) if d < 1 then blend(x, y, g[1], g[2], g[3], 0.6 * (1 - d)) end end end end local function sun() for dy = -5, 5 do local hw = floor(sqrt(max(0, 20.5 - dy * dy)) + 0.5) rect(SX - hw, SY + dy, 2 * hw + 1, 1, P.sun2[1], P.sun2[2], P.sun2[3]) local h2 = floor(sqrt(max(0, 7.5 - dy * dy)) + 0.5) if dy * dy < 7.5 then rect(SX - h2, SY + dy, 2 * h2 + 1, 1, P.sun1[1], P.sun1[2], P.sun1[3]) end end end local function sea() for y = HZ, 52 do local t = (y - HZ) / (52 - HZ) local r, g, b if t < 0.35 then r, g, b = mix(P.sh, P.sm, t / 0.35) else r, g, b = mix(P.sm, P.sn, (t - 0.35) / 0.65) end rect(0, y, W, 1, rgb(r, g, b)) local half = 2 + 0.22 * (y - HZ) local a = 0.5 * (1 - t * 0.7) local c = P.glow blend_rect(floor(SX - half - 2), y, floor(2 * half + 5), 1, c[1], c[2], c[3], a * 0.4) blend_rect(floor(SX - half), y, floor(2 * half + 1), 1, c[1], c[2], c[3], a) end for i = 1, 44 do local h = hash32(i * 0x9E3779B1) local y = HZ + 1 + umod(h, 18) local len = 3 + (y - HZ) // 3 + umod(h >> 8, 5) local x = umod(h >> 12, W) if umod(h >> 20, 2) == 0 then blend_rect(x, y, len, 1, P.sh[1], P.sh[2], P.sh[3], 0.2) else blend_rect(x, y, len, 1, P.sn[1], P.sn[2], P.sn[3], 0.3) end end end local BANDS = { { 34, 42, 1, 175, 0.5, 1 }, { 42, 48, 2, 172, 0.25, 1 }, { 48, 54, 2, 178, 0.05, 0.97 }, { 54, 59, 3, 172, 0, 0.92 }, { 59, 64, 3, 165, 0, 0.84 } } local function shade(c, b) local r, g, bl = c[1], c[2], c[3] if b[5] > 0 then r, g, bl = mix({ r, g, bl }, P.haze, b[5]) end return rgb(r * b[6], g * b[6], bl * b[6]) end local function meadow(first, last) for bi = first, last do local b = BANDS[bi] local s = b[3] local FC = {} for k = 1, 5 do local f = FLOWER[k] or { 250, 204, 240 } local r, g, bl = shade(f, b) local r2, g2, b2 = rgb(mix(f, P.rim, 0.6)) local r3, g3, b3 = rgb(mix(f, P.rim, 0.3)) FC[k] = { r, g, bl, r2, g2, b2, floor(f[1] * 0.45), floor(f[2] * 0.4), floor(f[3] * 0.5), r3, g3, b3 } end local gr, gg, gb = shade(b[3] >= 2 and GRASS2 or GRASS, b) for x = 0, W - 1 do local y0 = max(b[1], G[x]) if y0 < b[2] then rect(x, y0, 1, b[2] - y0, gr, gg, gb) end end for cy = (b[1] + s - 1) // s, (b[2] - 1) // s do local y0 = cy * s for cx = 0, (W - 1) // s do local x0 = cx * s if y0 + s // 2 >= G[min(W - 1, x0 + s // 2)] then local h = hash32(cx * 0x1F3D5B79 + cy * 0x2545F491 + s * 977) if (h & 255) < b[4] then local k = (h >> 8) & 255 local c = FC[(h >> 16) & 31 == 0 and 5 or k < 60 and 1 or k < 140 and 2 or k < 210 and 3 or 4] rect(x0, y0, s, s, c[1], c[2], c[3]) if s >= 2 then px(x0 + s - 1, y0, c[4], c[5], c[6]) px(x0, y0 + s - 1, c[7], c[8], c[9]) if s >= 3 then px(x0 + s - 2, y0, c[10], c[11], c[12]) end end elseif s >= 3 and (h >> 20) & 3 == 0 then rect(x0 + 1, y0, 1, s, 28, 66, 38) end end end end end if last == #BANDS then for x = 0, W - 1 do px(x, G[x], rgb(mix(GRASS, P.rim, 0.45))) end end end local RA, RB, RAYA, RAYB, CM = {}, {}, nil, nil, {} local CP, CH = 64, 12 local RW, RH = 32, 16 local function ray_rows(ya, yb) local ra, rb = {}, {} for y = ya, yb do for x = 0, RW - 1 do local dx, dy = (x + 0.5) * 2 - SX, min(-0.4, (y + 0.5) * 2 - SY) local a = atan(dx, -dy) local fade = max(0, 1 - sqrt(dx * dx + dy * dy) / 48) * 230 ra[x + 1] = floor(min(1, max(0, sin(a * 11 + 0.7) + 0.55 * sin(a * 23 + 2.1) - 0.15)) * fade) rb[x + 1] = floor(min(1, max(0, sin(a * 13 + 2.2) + 0.55 * sin(a * 29 + 0.4) - 0.15)) * fade) end RA[y + 1] = char(unpack(ra, 1, RW)) RB[y + 1] = char(unpack(rb, 1, RW)) end end local N1, N2 = 6, 12 local LAT1, LAT2 local function lattice(n, rows, seed) local t = {} for j = 0, rows do local r = {} for i = 0, n - 1 do r[i] = vn(i, j, seed) end t[j] = r end return t end local CC, COL = {}, {} local function cloud_cols(p) if not LAT1 then LAT1, LAT2 = lattice(N1, 8, 11), lattice(N2, 17, 29) end for x = 0, CP - 1 do local xs = x - p / 4 + CP local u1, u2 = xs * N1 / CP, xs * N2 / CP + 0.25 local i1, i2 = floor(u1), floor(u2) local f1, f2 = u1 - i1, u2 - i2 COL[x] = { i1 % N1, (i1 + 1) % N1, f1 * f1 * (3 - 2 * f1), i2 % N2, (i2 + 1) % N2, f2 * f2 * (3 - 2 * f2) } end end local function cloud_rows(p, ya, yb) if ya == 0 then cloud_cols(p) end for y = ya, yb do local v1, v2 = y / 1.6, y / 0.8 local j1, j2 = floor(v1), floor(v2) local fv1, fv2 = v1 - j1, v2 - j2 fv1 = fv1 * fv1 * (3 - 2 * fv1); fv2 = fv2 * fv2 * (3 - 2 * fv2) local a0, a1, b0, b1 = LAT1[j1], LAT1[j1 + 1], LAT2[j2], LAT2[j2 + 1] local env = max(0, min(1, (y + 0.5) / 2.5, (CH - y) / 3)) local row = {} for x = 0, CP - 1 do local c = COL[x] local c0, c1, f1, d0, d1, f2 = c[1], c[2], c[3], c[4], c[5], c[6] local t1, t2 = a0[c0] + (a0[c1] - a0[c0]) * f1, a1[c0] + (a1[c1] - a1[c0]) * f1 local s1, s2 = b0[d0] + (b0[d1] - b0[d0]) * f2, b1[d0] + (b1[d1] - b1[d0]) * f2 local f = (t1 + (t2 - t1) * fv1) * 0.65 + (s1 + (s2 - s1) * fv2) * 0.35 row[x] = max(0, min(1, (f - 0.5) * 3.2)) * env end CC[y] = row end end local function cloud_masks(p) local body, rim, rb, rr = {}, {}, {}, {} for y = 0, CH - 1 do local c, c2 = CC[y], CC[y + 1] for x = 0, CP - 1 do rb[x + 1] = floor(c[x] * 255) rr[x + 1] = floor(max(0, min(1, (c[x] - c2[x]) * 2.5)) * 255) end body[y + 1] = char(unpack(rb, 1, CP)) rim[y + 1] = char(unpack(rr, 1, CP)) end CM[p + 1] = { body = concat(body), rim = concat(rim) } end local BG, PART, STAGE = nil, nil, 1 local function finish() BG = snapshot(0, 0, W, H) PART = nil end local STAGES = { sky, function() glow(8, 19) end, function() glow(20, HZ - 1) end, sun, sea, function() meadow(1, 1) end, function() meadow(2, 2) end, function() meadow(3, 3) end, function() meadow(4, 4) end, function() meadow(5, 5) end, function() if not RAYA then ray_rows(0, 7) end end, function() if not RAYA then ray_rows(8, RH - 1); RAYA, RAYB = concat(RA), concat(RB) end end, } for p = 0, 3 do STAGES[#STAGES + 1] = function() if not CM[p + 1] then cloud_rows(p, 0, 6) end end STAGES[#STAGES + 1] = function() if not CM[p + 1] then cloud_rows(p, 7, CH); cloud_masks(p) end end end STAGES[#STAGES + 1] = finish function on_settings() P = MOODS[cfg.mood] or MOODS.amber BG, PART, STAGE = nil, nil, 1 end local SPK = {} for i = 1, 22 do local h = hash32(i * 0x85EBCA6B) local y = HZ + 2 + umod(h, 16) local half = 2 + 0.22 * (y - HZ) local x = floor(SX + (umod(h >> 8, 100) / 50 - 1) * half + 0.5) if G[max(0, min(W - 1, x))] > y then SPK[#SPK + 1] = { x = x, y = y, w = 1.5 + umod(h >> 16, 10) * 0.25, ph = umod(h >> 20, 628) / 100, len = 1 + umod(h >> 4, 3) } end end local function cloud_layer(t, speed, off, y, h, c, a, ra) local s = (off + t * speed) % CP local ip = floor(s) local M = CM[floor((s - ip) * 4) + 1] local xo = ip - CP for k = 0, 1 do blend_mask(M.body, c[1], c[2], c[3], xo + k * CP, y, CP, h, CP, CH, a) blend_mask(M.rim, P.rim[1], P.rim[2], P.rim[3], xo + k * CP, y, CP, h, CP, CH, ra) end end function render() if not BG then if PART then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if not BG then PART = snapshot(0, 0, W, H) end if not BG then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local rays = cfg.rays or 3 if rays > 0 then local rc = P.ray blend_mask(RAYA, rc[1], rc[2], rc[3], 0, 0, W, HZ, RW, RH, rays * 0.09 * (0.75 + 0.25 * sin(t * 0.8))) blend_mask(RAYB, rc[1], rc[2], rc[3], 0, 0, W, HZ, RW, RH, rays * 0.09 * (0.75 + 0.25 * sin(t * 0.8 + 3.1))) end cloud_layer(t, 0.5, 0, 2, 13, P.cf, 0.42, 0.3) cloud_layer(t, 1.3, 40, 4, 2 * CH, P.cb, 0.5, 0.55) local sg = P.glow for i = 1, #SPK do local s = SPK[i] local v = sin(t * s.w + s.ph) if v > 0.15 then blend_rect(s.x, s.y, s.len, 1, sg[1], sg[2], sg[3], (v - 0.15) * 0.9) end end end