info = { type = "image", title = "Cloud meadow", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Cloud shadows crossing a meadow with a tower", api = 44, } local ltr = ({ sk = { ["Time of day"] = "Denná doba", ["Morning"] = "Ráno", ["Noon"] = "Poludnie", ["Dusk"] = "Súmrak", ["Clouds"] = "Oblaky", ["Birds"] = "Vtáky" }, cs = { ["Time of day"] = "Denní doba", ["Morning"] = "Ráno", ["Noon"] = "Poledne", ["Dusk"] = "Soumrak", ["Clouds"] = "Mraky", ["Birds"] = "Ptáci" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "time", label = LT("Time of day"), type = "int", default = "noon", opts = { keys = { "morning", "noon", "dusk" }, LT("Morning"), LT("Noon"), LT("Dusk") } }, { key = "clouds", label = LT("Clouds"), type = "int", min = 0, max = 5, default = 4 }, { key = "birds", label = LT("Birds"), type = "int", min = 0, max = 9, default = 6 }, } 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, exp, abs, min = math.floor, math.sin, math.sqrt, math.exp, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 44 local TIMES = { noon = { skyT = { 52, 128, 232 }, skyH = { 176, 224, 252 }, cl = { 255, 255, 255 }, cs = { 190, 208, 236 }, ridge = { 150, 196, 214 }, hillL = { 134, 198, 112 }, hillS = { 84, 146, 96 }, left = { 118, 184, 104 }, stone = { 222, 222, 224 }, stoneS = { 148, 154, 172 }, roof = { 56, 54, 96 }, path = { 226, 208, 160 }, med0 = { 130, 206, 86 }, med1 = { 50, 148, 50 }, glt = { 176, 230, 104 }, gdk = { 28, 108, 46 }, sh = { 14, 64, 58 }, bird = { 52, 62, 92 }, }, morning = { skyT = { 98, 152, 238 }, skyH = { 255, 216, 176 }, cl = { 255, 238, 222 }, cs = { 214, 176, 190 }, ridge = { 208, 186, 204 }, hillL = { 176, 204, 116 }, hillS = { 108, 148, 104 }, left = { 150, 190, 108 }, stone = { 240, 216, 202 }, stoneS = { 168, 138, 152 }, roof = { 74, 62, 106 }, path = { 240, 214, 170 }, med0 = { 176, 218, 104 }, med1 = { 66, 156, 62 }, glt = { 214, 240, 126 }, gdk = { 36, 112, 58 }, sh = { 30, 62, 78 }, bird = { 84, 72, 104 }, }, dusk = { skyT = { 70, 88, 192 }, skyH = { 255, 172, 112 }, cl = { 255, 198, 152 }, cs = { 190, 116, 150 }, ridge = { 190, 140, 172 }, hillL = { 204, 178, 102 }, hillS = { 108, 118, 94 }, left = { 170, 160, 100 }, stone = { 236, 186, 152 }, stoneS = { 148, 108, 130 }, roof = { 60, 46, 92 }, path = { 238, 190, 130 }, med0 = { 200, 198, 86 }, med1 = { 78, 140, 60 }, glt = { 232, 226, 112 }, gdk = { 38, 96, 54 }, sh = { 40, 40, 96 }, bird = { 62, 42, 74 }, }, } local P = TIMES.noon 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 vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function tower_hill(x) return 44 - 13 * exp(-((x - 49) / 15) ^ 2) + 0.5 * sin(x * 0.5) end local function left_hill(x) return 41 - 3.2 * exp(-((x - 10) / 13) ^ 2) + 0.4 * sin(x * 0.4) end local function far_ridge(x) return 37 + 2.6 * sin(x * 0.13 + 1.0) + 1.6 * sin(x * 0.31 + 0.4) end local function s_sky() gfx.clear(P.med0[1], P.med0[2], P.med0[3]) for y = 0, HZ + 1 do rect(0, y, W, 1, mix(P.skyT, P.skyH, (y / HZ) ^ 1.2)) end end local function s_hills() for x = 0, W - 1 do local fy = floor(far_ridge(x) + 0.5) rect(x, fy, 1, HZ - fy + 1, P.ridge[1], P.ridge[2], P.ridge[3]) local ly = floor(left_hill(x) + 0.5) if ly < HZ then local s = left_hill(x + 1) - left_hill(x - 1) rect(x, ly, 1, HZ - ly + 1, mix(P.hillS, P.left, math.max(0, math.min(1, 0.5 - s * 0.35)))) end local ty = floor(tower_hill(x) + 0.5) if ty < HZ then local s = tower_hill(x + 1) - tower_hill(x - 1) rect(x, ty, 1, HZ - ty + 1, mix(P.hillS, P.hillL, math.max(0, math.min(1, 0.5 - s * 0.35)))) end end for i = 1, 9 do local x = 33 + floor(vn(i, 5) * 30) if x < 44 or x > 54 then local y = floor(tower_hill(x)) + 1 + floor(vn(i, 6) * 3) px(x, y, P.hillS[1] - 30, P.hillS[2] - 14, P.hillS[3] - 10) px(x + 1, y, P.hillS[1] - 30, P.hillS[2] - 14, P.hillS[3] - 10) px(x, y - 1, P.hillS[1] - 6, P.hillS[2] + 4, P.hillS[3] - 4) end end for k = 0, 12 do local x = 49 + floor(4 * sin(k * 0.9) * (1 - k / 12) + 0.5) px(x, 43 - k, P.path[1], P.path[2], P.path[3]) end end local function s_tower() local T = P local lit = { mix(T.stone, { 255, 255, 255 }, 0.35) } rect(47, 20, 5, 12, T.stone[1], T.stone[2], T.stone[3]) rect(47, 20, 1, 12, lit[1], lit[2], lit[3]) rect(51, 20, 1, 12, T.stoneS[1], T.stoneS[2], T.stoneS[3]) for y = 22, 30, 4 do rect(48, y, 3, 1, mix(T.stone, T.stoneS, 0.4)) end rect(46, 18, 7, 2, T.stone[1], T.stone[2], T.stone[3]) rect(52, 18, 1, 2, T.stoneS[1], T.stoneS[2], T.stoneS[3]) rect(46, 18, 1, 2, lit[1], lit[2], lit[3]) rect(46, 17, 7, 1, T.roof[1], T.roof[2], T.roof[3]) rect(47, 16, 5, 1, T.roof[1], T.roof[2], T.roof[3]) rect(48, 15, 3, 1, T.roof[1], T.roof[2], T.roof[3]) px(49, 14, T.roof[1], T.roof[2], T.roof[3]) px(47, 16, mix(T.roof, T.stone, 0.3)) rect(49, 11, 1, 3, T.roof[1] + 40, T.roof[2] + 40, T.roof[3] + 40) rect(49, 23, 1, 2, T.roof[1], T.roof[2], T.roof[3]) rect(49, 26, 1, 2, T.roof[1], T.roof[2], T.roof[3]) rect(49, 30, 1, 2, T.roof[1], T.roof[2], T.roof[3]) end local function s_meadow() for y = HZ, H - 1 do rect(0, y, W, 1, mix(P.med0, P.med1, ((y - HZ) / (H - 1 - HZ)) ^ 0.8)) end blend_rect(0, 46, W, 2, 20, 70, 20, 0.12) blend_rect(0, 51, W, 3, 20, 70, 20, 0.12) blend_rect(0, 58, W, 5, 20, 70, 20, 0.12) blend_rect(0, HZ, W, 2, P.skyH[1], P.skyH[2], P.skyH[3], 0.3) end local FL = { { 255, 255, 246 }, { 255, 224, 80 }, { 244, 160, 206 } } local function s_grass() local lt, dk = { mix(P.glt, P.med1, 0.35) }, { mix(P.gdk, P.med1, 0.5) } for i = 1, 180 do local h = hash32(i * 0x9E3779B1 + 77) local x = umod(h, W) local u = (umod(h >> 8, 1000) / 1000) ^ 0.7 local y = HZ + 2 + floor(u * (H - HZ - 3)) local c = ((h >> 20) & 1) == 1 and lt or dk if y >= 56 then rect(x, y - 1, 1, 2, c[1], c[2], c[3]) else px(x, y, c[1], c[2], c[3]) end end for i = 1, 16 do local h = hash32(i * 0x85EBCA6B + 9) local x, y = umod(h, W), 49 + umod(h >> 8, 14) local c = FL[umod(h >> 16, 3) + 1] px(x, y, c[1], c[2], c[3]) if y >= 56 then px(x + 1, y, c[1], c[2], c[3]) end end end local STAGES = { s_sky, s_hills, s_tower, s_meadow, s_grass } 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 local MARG = 26 local PC = W + 2 * MARG local CL = { { y = 2, L = 28, h = 7, v = 1.5, x0 = 10 }, { y = 13, L = 22, h = 6, v = 1.1, x0 = 60 }, { y = 22, L = 17, h = 4, v = 0.8, x0 = 35 }, { y = 7, L = 18, h = 5, v = 1.3, x0 = 96 }, { y = 28, L = 12, h = 3, v = 0.6, x0 = 80 }, } local SH = { { y = 56, hw = 20, h = 4 }, { y = 50, hw = 15, h = 3 }, { y = 47, hw = 10, h = 2 } } local CALPHA = { 0.55, 0.8, 0.9 } function on_settings() P = TIMES[cfg.time] or TIMES.noon STAGE = 1 for i = 1, #CL do local c = CL[i] local L, h = c.L, c.h local puffs = { { 0.26 * L, 0.24 * L, h * 0.62 }, { 0.52 * L, 0.30 * L, h }, { 0.78 * L, 0.22 * L, h * 0.55 } } c.rows = {} for r = 0, h - 1 do local lo, hi = 1e9, -1e9 for k = 1, 3 do local p = puffs[k] local d = (h - 0.5 - r) / p[3] if d < 1 then local w = p[2] * sqrt(1 - d * d) if p[1] - w < lo then lo = p[1] - w end if p[1] + w > hi then hi = p[1] + w end end end local cr, cg, cb = mix(P.cl, P.cs, (r + 0.5) / h * 0.85) c.rows[r] = { lo - L / 2, hi - L / 2, cr, cg, cb, CALPHA[r + 1] or 0.9 } end end end local function frow(xl, xr, y, r, g, b, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, r, g, b, a) blend_rect(il, y, 1, 1, r, g, b, a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, r, g, b, a * (xr - ir)) end local function cloud_cx(c, t) return (c.x0 + t * c.v) % PC - MARG end local BO = { { 0, 0 }, { -5, 2 }, { 5, 2 }, { -10, 4 }, { 10, 4 }, { -3, 5 }, { 3, 5 }, { -14, 7 }, { 14, 7 } } local POSE = { { -1, -1, 0, 0, 1, -1 }, { -1, 0, 0, 0, 1, 0 }, { -1, 0, 0, -1, 1, 0 }, { -1, 0, 0, 0, 1, 0 }, } function render() if STAGE <= #STAGES then build() if STAGE <= #STAGES then gfx.ready(false) end return end blit_rgb(PART, 0, 0, W, H) local t = frame / FPS local nc = cfg.clouds or 4 local sh = P.sh for i = 1, min(nc, 3) do local s = SH[i] local cx = cloud_cx(CL[i], t) - 6 for r = -s.h, s.h do local half = s.hw * sqrt(1 - (r / (s.h + 1)) ^ 2) frow(cx - half, cx + half, s.y + r, sh[1], sh[2], sh[3], 0.42 * (1 - abs(r) / (s.h + 1))) end end for i = nc, 1, -1 do local c = CL[i] local cx = cloud_cx(c, t) for r = 0, c.h - 1 do local w = c.rows[r] frow(cx + w[1], cx + w[2], c.y + r, w[3], w[4], w[5], w[6]) end end local nb = cfg.birds or 6 if nb > 0 then local fx = (t * 4.5) % (W + 70) - 35 local fy = 15 + 2.5 * sin(t * 0.45) local bc = P.bird for i = 1, nb do local o = BO[i] local x = fx + o[1] + 1.5 * sin(t * 0.7 + i) local y = floor(fy + o[2] + sin(t * 0.9 + i * 2) * 0.8 + 0.5) local ix = floor(x) local f = x - ix local q = POSE[(frame // 5 + i) % 4 + 1] for k = 1, 5, 2 do blend_rect(ix + q[k], y + q[k + 1], 1, 1, bc[1], bc[2], bc[3], 0.85 * (1 - f)) blend_rect(ix + q[k] + 1, y + q[k + 1], 1, 1, bc[1], bc[2], bc[3], 0.85 * f) end end end for k = 0, 2 do px(50 + k, 11 + floor(sin(t * 5 - k * 1.3) + 0.5), 214, 60, 60) end end