info = { type = "image", title = "Mist meadow", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sunrise meadow with drifting valley mist", api = 44, } local tr = ({ sk = { ["Mist"] = "Hmla", ["Dew"] = "Rosa", ["Bird"] = "Vtáčik" }, cs = { ["Mist"] = "Mlha", ["Dew"] = "Rosa", ["Bird"] = "Ptáček" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mist", label = T("Mist"), type = "int", min = 0, max = 5, default = 3 }, { key = "dew", label = T("Dew"), type = "int", min = 0, max = 5, default = 3 }, { key = "bird", label = T("Bird"), 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, sqrt, max, min, abs = math.floor, math.sin, math.sqrt, math.max, math.min, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 33 local SX, SY = 44, 29 local MEAD = 42 local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local function sky(y) local t = y / HZ if t < 0.45 then local k = t / 0.45 return 66 + 70 * k, 112 + 62 * k, 192 + 28 * k end local k = (t - 0.45) / 0.55 return 136 + 119 * k, 174 + 40 * k, 220 - 70 * k end local FAR, NEAR = {}, {} for x = 0, W - 1 do FAR[x] = floor(31 + 1.8 * sin(x * 0.09 + 0.5) + (noise(x / 3, 11) - 0.5) * 2.4 + 0.5) NEAR[x] = floor(37 + 1.4 * sin(x * 0.17 + 2) + (noise(x / 1.6, 7) - 0.5) * 2.6 - (63 - x) * 0.03 + 0.5) end local BLADE_TIPS = {} local function s_sky() for y = 0, HZ do rect(0, y, W, 1, rgb(sky(y))) end local cl = { { 4, 6, 22, 1 }, { 10, 8, 30, 1 }, { 30, 5, 28, 1 }, { 38, 10, 24, 1 }, { 0, 13, 26, 1 }, { 22, 15, 34, 1 }, { 46, 17, 18, 1 }, { 8, 19, 18, 1 }, { 28, 21, 26, 1 }, { 40, 23, 22, 1 }, { 12, 25, 24, 1 } } for _, c in ipairs(cl) do local warm = c[2] / 26 blend_rect(c[1], c[2], c[3], 2, 255, 226 - 30 * (1 - warm), 200 - 40 * (1 - warm), 0.30 + 0.30 * warm) blend_rect(c[1] + 3, c[2] - 1, c[3] - 7, 1, 255, 240, 220, 0.18 + 0.2 * warm) end end local function s_sun() local R = 15 for y = SY - R, HZ do local sr, sg, sb = sky(y) for x = SX - R, SX + R do local d = sqrt((x - SX) ^ 2 + (y - SY) ^ 2) if d < R and x >= 0 and x < W then if d < 3.3 then px(x, y, 255, 250, 228) else local k = (1 - d / R) * 0.8 px(x, y, rgb(sr + (255 - sr) * k, sg + (232 - sg) * k, sb + (170 - sb) * k)) end end end end end local function s_ridges() for x = 0, W - 1 do local y0 = FAR[x] rect(x, y0, 1, HZ + 1 - y0, 186, 168, 190) px(x, y0, 236, 198, 176) end end local function s_hill() for x = 0, W - 1 do local y0 = NEAR[x] rect(x, y0, 1, MEAD - y0 + 1, 64, 100, 70) if vn(x, 31) > 0.45 then px(x, y0 - 1, 64, 100, 70) end if vn(x, 32) > 0.7 then px(x, y0 - 2, 64, 100, 70) end if vn(x, 33) > 0.5 then px(x, y0, 172, 168, 104) end rect(x, y0 + 3, 1, 1, 48, 82, 56) end rect(0, MEAD - 1, W, 1, 150, 164, 90) end local function s_meadow() for y = MEAD, H - 1 do local t = (y - MEAD) / (H - 1 - MEAD) local r, g, b = 188 - 112 * t, 176 - 62 * t, 88 - 44 * t rect(0, y, W, 1, rgb(r, g, b)) local x = 0 local i = 0 while x < W do i = i + 1 local w = 2 + umod(hash32(y * 131 + i * 7), 3) local f = 0.84 + 0.32 * (hash32(y * 977 + x * 13) & 0x7FFF) / 32767 f = 1 + (f - 1) * (0.5 + t) rect(x, y, w, 1, rgb(r * f, g * f, b * f)) x = x + w end end for y = HZ + 1, MEAD - 1 do local t = (y - HZ - 1) / (MEAD - HZ - 2) rect(0, y, W, 1, rgb(214 - 40 * t, 196 - 28 * t, 150 - 40 * t)) end end local function s_light() for _, m in ipairs({ -1.9, -1.1, -0.45, 0.2, 0.8, 1.5 }) do for y = MEAD, H - 1 do local w = 2 + (y - SY) * 0.1 local x = SX + (y - SY) * m blend_rect(floor(x - w / 2), y, floor(w), 1, 255, 224, 150, 0.13) end end for y = HZ + 2, MEAD + 5 do blend_rect(0, y, W, 1, 255, 228, 196, 0.44 * (1 - abs(y - 40) / 8)) end local k = 0 for i = 1, 27 do local x = floor(i * 2.4 + (vn(i, 3) - 0.5) * 2) local h = 6 + umod(hash32(i * 0x85EBCA6B), 9) local lean = (i % 3 == 0) and -1 or 1 local nseg = (h + 3) // 4 for j = 0, nseg - 1 do local sx = x + lean * j local c = j % 2 == 0 and 38 or 50 rect(sx, H - 4 * (j + 1), (j < 2) and 2 or 1, 4, c, c + 38, 36) if j < 2 then rect(sx + 1, H - 4 * (j + 1), 1, 4, 96, 124, 52) end end k = k + 1 BLADE_TIPS[k] = { x + lean * (nseg - 1), H - 4 * nseg } px(x + lean * (nseg - 1), H - 4 * nseg, 190, 196, 104) end end local STAGES = { s_sky, s_sun, s_ridges, s_meadow, s_hill, s_light } local STAGE, PART, BG = 1, nil, nil local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then PART = snapshot(0, 0, W, H) else BG = snapshot(0, 0, W, H) end end local DEW = {} local function make_dew() for i = 1, #BLADE_TIPS do DEW[i] = { BLADE_TIPS[i][1], BLADE_TIPS[i][2], umod(hash32(i * 0x27D4EB2F), 628) / 100, 1.6 + umod(hash32(i * 31), 8) * 0.25 } end for i = 1, 14 do local h = hash32(i * 0xC2B2AE35) DEW[#DEW + 1] = { umod(h, W), 46 + umod(h >> 8, 14), umod(h >> 16, 628) / 100, 1.4 + umod(h >> 22, 8) * 0.25 } end end local MB = {} for i = 1, 5 do local h = hash32(i * 0x27D4EB2F) MB[i] = { y = 33 + umod(h, 8), len = 22 + umod(h >> 6, 16), x0 = umod(h >> 10, 100) * 0.9, v = (3 + umod(h >> 16, 40) / 10) * (umod(h >> 22, 2) * 2 - 1) } end local function mist_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end local fl, fr = 1 - (xl - il), xr - ir blend_rect(il + 1, y, ir - il - 1, 1, 255, 232, 204, a) blend_rect(il, y, 1, 1, 255, 232, 204, a * fl) blend_rect(ir, y, 1, 1, 255, 232, 204, a * fr) end local WING = { { { 0, -1 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 3, 0 }, { 4, -1 } }, { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 3, 0 }, { 4, 0 } }, { { 0, 1 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 3, 0 }, { 4, 1 } }, { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 3, 0 }, { 4, 0 } }, } local CYC = 450 local function bird(c) if c < 60 or c > 330 then return end local u = (c - 60) / 270 local x = -6 + 76 * u local y = 13 + floor(3 * sin(u * 5) + 0.5) local f = floor(x) local fx = x - f for _, p in ipairs(WING[(c // 3) % 4 + 1]) do blend_rect(f + p[1], y + p[2], 1, 1, 40, 36, 56, 0.9 * (1 - fx)) blend_rect(f + p[1] + 1, y + p[2], 1, 1, 40, 36, 56, 0.9 * fx) end end function render() if not BG then build() if not BG then gfx.ready(false) return end make_dew() end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS for i = 1, cfg.mist or 3 do local b = MB[i] local L = b.len local x = (b.x0 + t * b.v) % (W + 2 * L) - L mist_row(x + L * 0.25, x + L * 0.75, b.y, 0.26) mist_row(x, x + L, b.y + 1, 0.36) mist_row(x - L * 0.1, x + L * 1.1, b.y + 2, 0.30) mist_row(x + L * 0.1, x + L * 0.9, b.y + 3, 0.2) end local nd = floor(#DEW * (cfg.dew or 3) / 5 + 0.5) for i = 1, nd do local d = DEW[i] local v = sin(t * d[4] + d[3]) if v > 0.55 then blend_rect(d[1], d[2], 1, 1, 255, 252, 232, min(1, (v - 0.55) * 2.4)) if v > 0.88 then blend_rect(d[1] - 1, d[2], 1, 1, 255, 246, 210, 0.4); blend_rect(d[1] + 1, d[2], 1, 1, 255, 246, 210, 0.4) blend_rect(d[1], d[2] - 1, 1, 1, 255, 246, 210, 0.4) end end end if cfg.bird ~= false then bird(frame % CYC) end end