info = { type = "image", title = "Acacia dusk", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Panning sunset valley with a lone acacia", api = 44, } local tr = ({ sk = { ["Time"] = "Čas", ["Sunset"] = "Západ", ["Twilight"] = "Súmrak", ["Camera speed"] = "Rýchlosť kamery" }, cs = { ["Time"] = "Čas", ["Sunset"] = "Západ", ["Twilight"] = "Soumrak", ["Camera speed"] = "Rychlost kamery" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time"), type = "int", default = "sunset", opts = { keys = { "sunset", "twilight" }, T("Sunset"), T("Twilight") } }, { key = "speed", label = T("Camera speed"), type = "int", min = 1, 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, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local KR, KG, KB = 255, 0, 255 local PAN = 56 local SX, SY, SR = 46, 33, 5 local PALS = { sunset = { sky = { { 0, 62, 54, 118 }, { 16, 150, 70, 130 }, { 30, 238, 112, 74 }, { 40, 255, 176, 96 }, { 47, 255, 214, 130 } }, sun = { 255, 246, 206 }, glow = { 255, 214, 140 }, far = { deep = { 98, 78, 140 }, haze = { 238, 150, 112 }, hz = 0.85, rim = { 255, 206, 128 } }, mid = { deep = { 76, 90, 126 }, haze = { 236, 150, 104 }, hz = 0.7, rim = { 255, 190, 120 } }, hill = { deep = { 46, 92, 66 }, haze = { 206, 134, 90 }, hz = 0.3, rim = { 236, 196, 96 } }, spruce = { 20, 52, 56 }, spruceL = { 214, 150, 80 }, ground = { 26, 48, 30 }, grass = { 176, 160, 66 }, tip = { 255, 196, 90 }, acacia = { 28, 38, 24 }, acaciaD = { 18, 26, 18 }, acaciaL = { 236, 156, 60 }, trunk = { 30, 22, 18 }, mist = { 255, 214, 170 }, bird = { 70, 34, 56 }, }, twilight = { sky = { { 0, 46, 44, 112 }, { 16, 104, 58, 130 }, { 30, 206, 96, 112 }, { 40, 244, 150, 118 }, { 47, 252, 190, 130 } }, sun = { 255, 226, 190 }, glow = { 255, 170, 140 }, far = { deep = { 70, 62, 124 }, haze = { 220, 130, 126 }, hz = 0.85, rim = { 255, 170, 140 } }, mid = { deep = { 62, 70, 116 }, haze = { 214, 128, 120 }, hz = 0.7, rim = { 246, 160, 140 } }, hill = { deep = { 38, 68, 70 }, haze = { 186, 120, 112 }, hz = 0.3, rim = { 226, 150, 110 } }, spruce = { 18, 36, 52 }, spruceL = { 190, 120, 110 }, ground = { 20, 34, 34 }, grass = { 130, 124, 80 }, tip = { 236, 150, 110 }, acacia = { 24, 28, 36 }, acaciaD = { 16, 20, 28 }, acaciaL = { 224, 130, 96 }, trunk = { 24, 20, 24 }, mist = { 246, 170, 150 }, bird = { 52, 30, 60 }, }, } local P = PALS.sunset local function lerp3(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[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, 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 LAY = { { key = "far", r0 = 20, r1 = 44, f = 0.18 }, { key = "mid", r0 = 36, r1 = 50, f = 0.36 }, { key = "hill", r0 = 42, r1 = 58, f = 0.62 }, { key = "gnd", r0 = 26, r1 = 63, f = 1.0 }, } local function ridge(l, x) if l.key == "far" then local pts = { 0,36, 12,28, 20,24, 30,30, 40,36, 51,40, 62,37, 72,29, 82,25, 92,30, 104,36, 118,30, 127,33 } local y = 33 for i = 1, #pts - 2, 2 do if x <= pts[i + 2] then y = pts[i + 1] + (pts[i + 3] - pts[i + 1]) * (x - pts[i]) / (pts[i + 2] - pts[i]) break end end return y + 2.2 * (noise(x / 2.4, 3) - 0.5) + 1.2 * (noise(x / 1.1, 11) - 0.5) elseif l.key == "mid" then return 42 + 4 * sin(x * 0.055 + 0.3) + 2.5 * (noise(x / 4, 21) - 0.5) + 1.5 * sin(x * 0.21) elseif l.key == "hill" then return 47 + 3.2 * sin(x * 0.09 + 2) + 1.6 * sin(x * 0.23 + 0.4) + (noise(x / 5, 31) - 0.5) end return 57 + 2.2 * sin(x * 0.06 + 0.8) + 1.3 * sin(x * 0.17) + (noise(x / 3, 41) - 0.5) end local function terrain(l) l.Y, l.C = {}, {} local yf = {} for x = 0, 127 do yf[x] = ridge(l, x) l.Y[x] = floor(yf[x] + 0.5) end for x = 0, 127 do local s = (yf[min(x + 5, 127)] - yf[max(x - 5, 0)]) * 0.1 l.C[x] = s > 0.3 and 3 or s < -0.3 and 1 or 2 end end local CH = {} local BG, MASK, STAGE = nil, nil, 1 local function spruce(wx, base, h, off) local c, cl = P.spruce, P.spruceL local x = wx - off if x < -8 or x > 72 then return end for r = 0, h - 1 do local hw = floor(r * 0.28 + (r % 3 == 0 and 0.9 or 0.2) + 0.5) rect(x - hw, base - h + r, 2 * hw + 1, 1, c[1], c[2], c[3]) if r % 3 == 1 and r > 1 then px(x + hw, base - h + r, cl[1], cl[2], cl[3]) end end rect(x, base, 1, 2, c[1], c[2], c[3]) end local function acacia(tx, off) local x0 = tx - off local a, d, l, tk = P.acacia, P.acaciaD, P.acaciaL, P.trunk for y = 62, 36, -1 do local k = (62 - y) / 26 local w = y > 52 and 3 or 2 local cx = x0 + floor(2.5 * sin(k * 2.2) + 0.5) rect(cx - 1, y, w, 1, tk[1], tk[2], tk[3]) end for k = 0, 9 do px(x0 + 2 + k // 2 * 1 + 1, 46 - k, tk[1], tk[2], tk[3]); px(x0 + 3 + k // 2, 46 - k, tk[1], tk[2], tk[3]) px(x0 - 3 - k // 2, 46 - k // 1, tk[1], tk[2], tk[3]) end local crowns = { { -2, 33, 15, 3 }, { 9, 37, 9, 3 }, { -13, 38, 8, 2 } } for _, c in ipairs(crowns) do local cx, cy, rx, ry = x0 + c[1], c[2], c[3], c[4] for dy = -ry, ry do local hw = floor(rx * sqrt(1 - (dy * dy) / (ry * ry + 0.7)) + 0.5) local col = dy == ry and d or a rect(cx - hw, cy + dy, 2 * hw + 1, 1, col[1], col[2], col[3]) end for dx = -rx + 2, rx - 2 do local hw0 = floor(rx * sqrt(1 - ((-ry) * (-ry)) / (ry * ry + 0.7)) + 0.5) if abs(dx) <= hw0 or umod(hash32(dx * 13 + cx), 3) == 0 then local top = cy - ry - (umod(hash32(dx * 7 + cx), 3) == 0 and 1 or 0) px(cx + dx, top, l[1], l[2], l[3]) end end end end local function bake_chunk(li, c) local l = LAY[li] if not l.Y then terrain(l) end local h = l.r1 - l.r0 + 1 rect(0, l.r0, 64, h, KR, KG, KB) local base = c * 64 local sp = P[l.key] local tint = { { 40, 40, 100 }, { 0, 0, 0 }, { 255, 196, 120 } } for y = l.r0, l.r1 do local t = (y - l.r0) / (l.r1 - l.r0) local cols if l.key == "gnd" then cols = { P.ground, P.ground, P.ground } else local br, bg, bb = lerp3(sp.deep, sp.haze, sp.hz * t ^ 0.8) cols = {} for k = 1, 3 do local tc, w = tint[k], k == 1 and 0.28 or k == 3 and 0.22 or 0 cols[k] = { br + (tc[1] - br) * w, bg + (tc[2] - bg) * w, bb + (tc[3] - bb) * w } end end local a, ca = 0, nil for x = 0, 64 do local cl if x < 64 and l.Y[base + x] <= y then cl = l.C[base + x] end if cl ~= ca then if ca then local col = cols[ca] rect(a, y, x - a, 1, rgb(col[1], col[2], col[3])) end a, ca = x, cl end end end if sp then for x = 0, 63 do if l.C[base + x] == 3 then local rr = sp.rim px(x, l.Y[base + x], rr[1], rr[2], rr[3]) end end end if l.key == "hill" then for i = 1, 6 do local wx = 84 + i * 5 + umod(hash32(i * 77), 3) spruce(wx, l.Y[min(127, wx)] + 2, 9 + umod(hash32(i * 31), 6), base) end for i = 1, 3 do local wx = 14 + i * 7 + umod(hash32(i * 91), 3) spruce(wx, l.Y[wx] + 1, 6 + umod(hash32(i * 13), 3), base) end elseif l.key == "mid" then for x = 3, 127, 4 do local y0 = l.Y[x] if umod(hash32(x * 5), 3) ~= 0 then local c2 = sp.deep px(x - base, y0 - 1, c2[1] - 14, c2[2] - 8, c2[3] - 8) end end elseif l.key == "gnd" then local g, tp = P.grass, P.tip for x = 0, 63 do local h2 = hash32((base + x) * 0x85EBCA6B) if umod(h2, 4) == 0 then local top = l.Y[base + x] local len = 2 + umod(h2 >> 8, 3) rect(x, top - len, 1, len, g[1] * 0.45, g[2] * 0.5, g[3] * 0.5) px(x, top - len, tp[1], tp[2], tp[3]) if umod(h2 >> 12, 2) == 0 then px(x + 1, top - len + 1, g[1], g[2], g[3]) end end end acacia(60, base) end CH[li][c + 1] = snapshot(0, l.r0, 64, h) end local function bake_sky() local S = P.sky for y = 0, 63 do local k = 1 while k < #S - 1 and y > S[k + 1][1] do k = k + 1 end local a, b = S[k], S[min(k + 1, #S)] local t = b[1] == a[1] and 0 or min(1, max(0, (y - a[1]) / (b[1] - a[1]))) rect(0, y, W, 1, rgb(a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t, a[4] + (b[4] - a[4]) * t)) end local s = P.sun for dy = -SR, SR do local hw = floor(sqrt(SR * SR + 0.6 - dy * dy) + 0.5) rect(SX - hw, SY + dy, 2 * hw + 1, 1, s[1], s[2], s[3]) end BG = snapshot(0, 0, W, H) end local MR = 21 local function bake_mask() local rows, n = {}, 2 * MR + 1 for j = 0, n - 1 do local r = {} for i = 0, n - 1 do local d = sqrt((i - MR) ^ 2 + (j - MR) ^ 2) r[i + 1] = d < MR and floor(255 * (1 - d / MR) + 0.5) or 0 end rows[j + 1] = string.char(table.unpack(r)) end MASK = table.concat(rows) end local STAGES = {} STAGES[1] = bake_sky STAGES[2] = bake_mask for li = 1, 4 do for c = 0, 1 do STAGES[#STAGES + 1] = function() bake_chunk(li, c) end end end function on_settings() P = PALS[cfg.time] or PALS.sunset STAGE = 1 BG, MASK = nil, nil CH = { {}, {}, {}, {} } end CH = { {}, {}, {}, {} } local MB = {} for i = 1, 4 do local h = hash32(i * 0x27D4EB2F) MB[i] = { y = 41 + i * 2, len = 18 + umod(h, 12), x0 = umod(h >> 6, 90), v = (1.0 + umod(h >> 12, 10) / 10) * (i % 2 == 0 and -1 or 1) } end local BIRDS = {} for i = 1, 3 do local h = hash32(i * 0xC2B2AE35) BIRDS[i] = { y = 10 + i * 5 + umod(h, 4), x0 = umod(h >> 4, 80), v = 5 + umod(h >> 10, 20) / 10, ph = umod(h >> 14, 100) / 10 } end local function mist_row(xl, xr, y, a, m) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, m[1], m[2], m[3], a) blend_rect(il, y, 1, 1, m[1], m[2], m[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, m[1], m[2], m[3], a * (xr - ir)) end local function scroll(li, off) local l = LAY[li] local h = l.r1 - l.r0 + 1 local o = off % 64 local c0 = off // 64 local a, b = CH[li][c0 + 1], CH[li][c0 + 2] if a then blit_rgb(a, -o, l.r0, 64, h, KR, KG, KB) end if b then blit_rgb(b, 64 - o, l.r0, 64, h, KR, KG, KB) end end local SPEEDS = { 120, 80, 60, 42, 30 } function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then gfx.ready(false) return end end if not BG then return end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local cam = PAN / 2 - PAN / 2 * cos(t * 6.2832 / SPEEDS[cfg.speed or 3] + 1.0) if MASK then local g = P.glow blend_mask(MASK, g[1], g[2], g[3], SX - MR, SY - MR, 2 * MR + 1, 2 * MR + 1, 2 * MR + 1, 2 * MR + 1, 0.42 + 0.14 * sin(t * 0.7)) end local br = P.bird for i = 1, 3 do local b = BIRDS[i] local x = (b.x0 + t * b.v) % (W + 20) - 10 local ix = floor(x) local f = x - ix local y = floor(b.y + 2 * sin(t * 0.4 + b.ph) + 0.5) local up = floor(t * 3 + b.ph) % 2 == 0 local dyv = up and -1 or 1 local pts = { { -2, dyv }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, dyv } } for k = 1, 5 do local p = pts[k] blend(ix + p[1], y + p[2], br[1], br[2], br[3], 0.9 * (1 - f)) blend(ix + p[1] + 1, y + p[2], br[1], br[2], br[3], 0.9 * f) end end for li = 1, 4 do scroll(li, floor(cam * LAY[li].f)) if li == 2 then local m = P.mist for i = 1, 4 do local d = MB[i] local L = d.len local x = (d.x0 + t * d.v) % (W + 2 * L) - L mist_row(x + L * 0.3, x + L * 0.7, d.y, 0.07, m) mist_row(x + L * 0.1, x + L * 0.9, d.y + 1, 0.11, m) mist_row(x, x + L, d.y + 2, 0.08, m) end end end end