info = { type = "image", title = "River vale", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A river winds through green hills, clouds drift", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Sunset"] = "Západ", ["Clouds"] = "Oblaky", ["Flow speed"] = "Rýchlosť toku", ["Birds"] = "Vtáky" }, cs = { ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Sunset"] = "Západ", ["Clouds"] = "Oblaky", ["Flow speed"] = "Rychlost toku", ["Birds"] = "Ptáci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "sunset" }, T("Day"), T("Sunset") } }, { key = "clouds", label = T("Clouds"), type = "int", min = 0, max = 5, default = 3 }, { key = "flow", label = T("Flow speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "birds", label = T("Birds"), type = "bool", default = true }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, exp = math.floor, math.sin, math.cos, math.sqrt, math.exp local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PALS = { day = { sky = { { 92, 160, 230 }, { 150, 204, 240 }, { 208, 234, 246 } }, glow = { 255, 246, 210 }, sun = { 255, 249, 206 }, sunx = 55, suny = 6, cloudL = { 255, 255, 255 }, cloudS = { 196, 214, 238 }, farT = { 126, 176, 156 }, farB = { 98, 150, 124 }, hillT = { 112, 176, 70 }, hillB = { 40, 100, 46 }, rim = { 150, 206, 92 }, tree = { 30, 86, 40 }, bank = { 172, 202, 112 }, waterT = { 128, 182, 230 }, waterB = { 42, 86, 170 }, glint = { 236, 248, 255 }, ripple = { 30, 66, 140 }, bird = { 40, 52, 74 }, }, sunset = { sky = { { 58, 50, 118 }, { 184, 86, 116 }, { 255, 176, 98 } }, glow = { 255, 204, 124 }, sun = { 255, 232, 156 }, sunx = 48, suny = 28, cloudL = { 255, 176, 134 }, cloudS = { 150, 84, 124 }, farT = { 156, 98, 120 }, farB = { 118, 74, 104 }, hillT = { 92, 118, 60 }, hillB = { 24, 52, 42 }, rim = { 188, 156, 82 }, tree = { 18, 40, 34 }, bank = { 160, 126, 92 }, waterT = { 156, 122, 180 }, waterB = { 40, 44, 116 }, glint = { 255, 216, 152 }, ripple = { 30, 30, 84 }, bird = { 48, 30, 62 }, }, } local P local function C(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function mix(a, b, t) return C(a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t) end local function span(xl, xr, y, c, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend(il, y, c[1], c[2], c[3], a * (1 - (xl - il))) if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) end blend(ir, y, c[1], c[2], c[3], a * (xr - ir)) end local function disc(cx, cy, r, c, a) for y = -floor(r), floor(r) do local hw = floor(sqrt(r * r - y * y) + 0.5) if a then blend_rect(cx - hw, cy + y, 2 * hw + 1, 1, c[1], c[2], c[3], a) else rect(cx - hw, cy + y, 2 * hw + 1, 1, c[1], c[2], c[3]) end end end local function gauss(x, c, w) local d = (x - c) / w; return exp(-d * d) end local FAR, NEAR = {}, {} for x = 0, W - 1 do FAR[x] = floor(30 + 2.2 * sin(x * 0.21 + 1) + 1.2 * sin(x * 0.5) + 0.5) NEAR[x] = floor(47 - 14 * gauss(x, 6, 15) - 11 * gauss(x, 58, 13) + 1.5 * sin(x * 0.3) + 0.5) end local RY0 = 32 local function cx_of(y) return 31 + 8 * sin((y - RY0) * 0.11 + 0.5) end local function hw_of(y) return 0.8 + (y - RY0) * 0.42 end local function st_sky() for y = 0, 31 do local t = y / 31 if t < 0.5 then rect(0, y, W, 1, mix(P.sky[1], P.sky[2], t * 2)) else rect(0, y, W, 1, mix(P.sky[2], P.sky[3], t * 2 - 1)) end end for r = 13, 5, -1 do disc(P.sunx, P.suny, r, P.glow, 0.05) end disc(P.sunx, P.suny, 4.6, P.glow); disc(P.sunx, P.suny, 3.6, P.sun) end local function layer(top, y0, y1, c0, c1, ylo) for y = y0, y1 do local x = 0 local cr, cg, cb = mix(c0, c1, (y - ylo) / 30) while x < W do if top[x] <= y then local s = x while x < W and top[x] <= y do x = x + 1 end rect(s, y, x - s, 1, cr, cg, cb) else x = x + 1 end end end end local function st_hills() layer(FAR, 30, H - 1, P.farT, P.farB, 30) layer(NEAR, 33, H - 1, P.hillT, P.hillB, 33) for x = 0, W - 1 do rect(x, NEAR[x], 1, 1, P.rim[1], P.rim[2], P.rim[3]) end local TR = { { 11, 44 }, { 17, 51 }, { 4, 55 }, { 52, 45 }, { 59, 53 }, { 8, 60 }, { 56, 60 } } for _, t in ipairs(TR) do rect(t[1], t[2], 3, 2, P.tree[1], P.tree[2], P.tree[3]) rect(t[1], t[2], 1, 1, P.hillT[1] * 0.75, P.hillT[2] * 0.75, P.hillT[3] * 0.75) end end local function st_river() for y = RY0, H - 1 do local c, h = cx_of(y), hw_of(y) local xl, xr = floor(c - h + 0.5), floor(c + h + 0.5) rect(xl, y, xr - xl, 1, mix(P.waterT, P.waterB, (y - RY0) / 31)) rect(xl - 1, y, 1, 1, P.bank[1], P.bank[2], P.bank[3]) rect(xr, y, 1, 1, P.bank[1], P.bank[2], P.bank[3]) end end local STAGES = { st_sky, st_hills, st_river } local stage, BG = 0, nil function on_settings() P = PALS[cfg.time] or PALS.day stage, BG = 0, nil end local NG = 16 local GU, GQ, GS, GD = {}, {}, {}, {} for i = 1, NG do local h = hash32(i * 0x9E3779B1) GU[i] = (umod(h, 1000) / 1000 - 0.5) * 1.7 GQ[i] = umod(h >> 10, 1000) / 1000 GS[i] = 0.8 + umod(h >> 20, 40) / 100 GD[i] = umod(h >> 5, 3) == 0 end local function draw_glints(t, flow) for i = 1, NG do local q = (GQ[i] + 0.045 * flow * GS[i] * t) % 1 local y = RY0 + 2 + 29 * q ^ 1.5 local iy = floor(y) local f = y - iy local x = floor(cx_of(y) + GU[i] * hw_of(y) + 0.5) local len = 1 + floor(q * 3) local a = sin(q * 3.14159) ^ 0.6 * 0.85 local c = GD[i] and P.ripple or P.glint blend_rect(x, iy, len, 1, c[1], c[2], c[3], a * (1 - f)) blend_rect(x, iy + 1, len, 1, c[1], c[2], c[3], a * f) end end local CY, CL, CV, CX = { 6, 15, 22, 11, 19 }, { 22, 18, 26, 14, 20 }, { 2.6, 1.7, 3.3, 2.1, 1.3 }, { 8, 40, 20, 56, 2 } local function draw_clouds(n, t) for i = 1, n do local L = CL[i] local xl = (CX[i] + CV[i] * t) % (W + L) - L local y = CY[i] span(xl + L * 0.3, xl + L * 0.65, y - 1, P.cloudL, 0.9) span(xl + 3, xl + L - 3, y, P.cloudL, 0.92) span(xl, xl + L, y + 1, P.cloudL, 0.95) span(xl + 2, xl + L - 2, y + 2, P.cloudS, 0.9) end end local BEV, BLEN = 480, 160 local FORM = { { 0, 0 }, { -5, -2 }, { -5, 2 }, { -10, -4 }, { -10, 4 } } local function draw_birds() local c, p = (frame + 140) // BEV, (frame + 140) % BEV if p >= BLEN then return end local dir = c % 2 == 0 and 1 or -1 local x0 = dir == 1 and (-12 + p * 0.55) or (76 - p * 0.55) local y0 = 8 + umod(hash32(c * 0x9E3779B1), 12) local col = P.bird for k = 1, #FORM do local x = x0 + FORM[k][1] * dir local y = y0 + FORM[k][2] local ix = floor(x) local f = x - ix local up = ((frame // 5) + k) % 2 == 0 local a, b = 1 - f, f local function dot(dx, dy) blend(ix + dx, y + dy, col[1], col[2], col[3], a) blend(ix + dx + 1, y + dy, col[1], col[2], col[3], b) end if up then dot(0, 0); dot(2, 0); dot(1, 1) else dot(0, 1); dot(1, 0); dot(2, 1) end end end function render() P = P or PALS.day if stage < #STAGES then if BG then gfx.blit_rgb(BG, 0, 0, W, H) else gfx.clear(P.sky[2][1], P.sky[2][2], P.sky[2][3]) end stage = stage + 1 STAGES[stage]() BG = gfx.snapshot(0, 0, W, H) if stage < #STAGES then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_clouds(cfg.clouds or 3, t) draw_glints(t, cfg.flow or 3) if cfg.birds ~= false then draw_birds() end end