info = { type = "image", title = "Train window", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Fields and hills slide past a train window", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Dusk"] = "Súmrak" }, cs = { ["Speed"] = "Rychlost", ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "tod", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, abs, sqrt, min = math.floor, math.sin, math.abs, math.sqrt, math.min local W, H = gfx.W, gfx.H local PAL = { day = { s0 = { 112, 200, 236 }, s1 = { 222, 240, 226 }, sun = { 255, 246, 200 }, mount = { 168, 194, 222 }, hill = { 150, 206, 150 }, pl0 = { 140, 204, 108 }, pl1 = { 112, 180, 92 }, grass = { 70, 136, 70 }, t1 = { 108, 184, 96 }, t2 = { 70, 146, 78 }, t3 = { 50, 114, 66 }, trunk = { 112, 84, 56 }, b1 = { 184, 230, 108 }, b2 = { 140, 198, 86 }, fence = { 238, 228, 202 }, pole = { 126, 96, 72 }, wire = { 64, 64, 84 }, cloud = { 255, 255, 255 }, frame = { 226, 208, 176 }, frame2 = { 160, 134, 106 }, }, dusk = { s0 = { 84, 78, 148 }, s1 = { 252, 178, 138 }, sun = { 255, 210, 150 }, mount = { 134, 112, 166 }, hill = { 148, 132, 158 }, pl0 = { 122, 150, 108 }, pl1 = { 100, 126, 96 }, grass = { 52, 82, 62 }, t1 = { 96, 134, 98 }, t2 = { 68, 104, 82 }, t3 = { 48, 80, 66 }, trunk = { 88, 68, 58 }, b1 = { 158, 188, 100 }, b2 = { 120, 152, 84 }, fence = { 226, 198, 176 }, pole = { 98, 72, 66 }, wire = { 58, 54, 84 }, cloud = { 255, 206, 190 }, frame = { 150, 118, 100 }, frame2 = { 98, 74, 66 }, }, } local P local N = 1.7 local function lerp3(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 span(y, a, b, c, al) local r, g, bl = c[1], c[2], c[3] local fa, fb = floor(a), floor(b) if fa == fb then if b > a then blend(fa, y, r, g, bl, (b - a) * al) end return end if a > fa then blend(fa, y, r, g, bl, (1 - a + fa) * al); fa = fa + 1 end if fb > fa then if al >= 1 then rect(fa, y, fb - fa, 1, r, g, bl) else blend_rect(fa, y, fb - fa, 1, r, g, bl, al) end end if b > fb then blend(fb, y, r, g, bl, (b - fb) * al) end end local function vbar(x, y, h, w, c) local r, g, b = c[1], c[2], c[3] local x1 = x + w local fa, fb = floor(x), floor(x1) if fa == fb then blend_rect(fa, y, 1, h, r, g, b, w); return end if x > fa then blend_rect(fa, y, 1, h, r, g, b, 1 - x + fa); fa = fa + 1 end if fb > fa then rect(fa, y, fb - fa, h, r, g, b) end if x1 > fb then blend_rect(fb, y, 1, h, r, g, b, x1 - fb) end end local function ridge(off, fn, bot, c) local r, g, b = c[1], c[2], c[3] for x = 0, W - 1 do local top = fn(x + off) local it = floor(top) blend(x, it, r, g, b, 1 - (top - it)) if bot - it - 1 > 0 then rect(x, it + 1, 1, bot - it - 1, r, g, b) end end end local function mount(wx) return 40 - (4 + 10 * (1 - abs(sin(wx * 0.07))) + 3 * sin(wx * 0.23 + 1)) end local function hill(wx) return 44 - (3.5 + 3 * sin(wx * 0.12 + 0.4) + 2 * sin(wx * 0.27 + 2)) end local BG local function disc(cx, cy, r, c, a) for j = -r, r do local hw = floor(sqrt(r * r - j * j) + 0.5) blend_rect(cx - hw, cy + j, 2 * hw + 1, 1, c[1], c[2], c[3], a) end end local function build_bg() for y = 0, 47 do rect(0, y, W, 1, lerp3(P.s0, P.s1, min(1, y / 43))) end local sx, sy, sr = 46, 12, 4 if cfg.tod == "dusk" then sx, sy, sr = 38, 37, 7 end disc(sx, sy, sr + 6, P.sun, 0.12) disc(sx, sy, sr + 3, P.sun, 0.25) disc(sx, sy, sr, P.sun, 1) BG = gfx.snapshot(0, 0, W, 48) end local TREES, TP = {}, 96 do local xs, hs = { 5, 22, 37, 53, 66, 84 }, { 11, 9, 13, 10, 12, 9 } for i = 1, #xs do local h, round = hs[i], i % 2 == 1 local hw = {} for r = 0, h - 1 do if round then local cy = (h - 1) / 2 hw[r] = (h * 0.42) * sqrt(1 - ((r - cy) / (cy + 0.6)) ^ 2) else hw[r] = 0.8 + r * 3.2 / h - (r % 4 == 0 and 0.9 or 0) end end TREES[i] = { x = xs[i], h = h, hw = hw } end end local function trees(off) local c1, c2, c3 = P.t1, P.t2, P.t3 for i = 1, #TREES do local t = TREES[i] local x = (t.x - off) % TP - 12 if x < W + 7 then local top = 45 - t.h for r = 0, t.h - 1 do local k = r / t.h span(top + r, x - t.hw[r], x + t.hw[r], k < 0.35 and c1 or (k < 0.7 and c2 or c3), 1) end span(45, x - 0.8, x + 0.8, P.trunk, 1) span(46, x - 0.8, x + 0.8, P.trunk, 1) end end end local BUSH_X, BP = { 4, 19, 31, 44, 57, 71 }, 80 local function bushes(off) for i = 1, #BUSH_X do local x = (BUSH_X[i] - off) % BP - 6 if x < W + 4 then span(47, x - 1.6, x + 1.6, P.b1, 1) span(48, x - 2.8, x + 2.8, P.b1, 1) span(49, x - 3.4, x + 3.4, P.b2, 1) end end end local CLOUDS = { { 8, 22, 0, 3 }, { 15, 16, 30, 4 }, { 5, 14, 52, 3 }, { 19, 20, 74, 4 } } local function clouds(off) local c = P.cloud for i = 1, #CLOUDS do local k = CLOUDS[i] local L = k[2] local x = (k[3] - off) % (W + 24) - L span(k[1], x + 4, x + L - 4, c, 0.55) span(k[1] + 1, x + 1, x + L - 1, c, 0.55) span(k[1] + 2, x, x + L, c, 0.55) span(k[1] + 3, x + 3, x + L - 5, c, 0.4) end end local SP = 84 local function poles(off) local w = P.wire local w1, w2, w3 = w[1], w[2], w[3] for c = 0, W - 1 do local u = (c + off - 20) % SP / SP local y = 12 + 20 * u * (1 - u) local iy = floor(y) local fy = y - iy if fy < 0.15 then blend(c, iy, w1, w2, w3, 0.65); blend(c, iy + 3, w1, w2, w3, 0.65) elseif fy > 0.85 then blend(c, iy + 1, w1, w2, w3, 0.65); blend(c, iy + 4, w1, w2, w3, 0.65) else local a, b = 0.65 * (1 - fy), 0.65 * fy blend(c, iy, w1, w2, w3, a); blend(c, iy + 1, w1, w2, w3, b) blend(c, iy + 3, w1, w2, w3, a); blend(c, iy + 4, w1, w2, w3, b) end end local k = floor((off - 20 + 4) / SP) + 1 local x = 20 + k * SP - off if x < W + 4 then vbar(x - 1, 9, 49, 2, P.pole) span(10, x - 4, x + 4, P.pole, 1) span(11, x - 4, x + 4, P.pole, 1) end x = 20 + (k - 1) * SP - off if x > -4 then vbar(x - 1, 9, 49, 2, P.pole) span(10, x - 4, x + 4, P.pole, 1) span(11, x - 4, x + 4, P.pole, 1) end end local function fence(off) rect(0, 51, W, 1, P.fence[1], P.fence[2], P.fence[3]) rect(0, 55, W, 1, P.fence[1], P.fence[2], P.fence[3]) blend_rect(0, 52, W, 1, 0, 0, 0, 0.25) blend_rect(0, 56, W, 1, 0, 0, 0, 0.25) local k0 = floor(off / 14) for k = k0, k0 + 5 do local x = k * 14 - off if x > -3 and x < W then vbar(x, 49, 9, 2.4, P.fence) end end local g = P.pl0 for k = floor(off / 9), floor(off / 9) + 8 do local x = k * 9 - off span(57, x, x + 4, g, 0.3) end end function on_settings() P = PAL[cfg.tod] or PAL.day N = 0.35 + 0.45 * (cfg.speed or 3) BG = nil end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, 48) local f = frame clouds(f * N * 0.025) ridge(f * N * 0.05, mount, 44, P.mount) ridge(f * N * 0.13, hill, 46, P.hill) rect(0, 45, W, 3, P.pl0[1], P.pl0[2], P.pl0[3]) rect(0, 48, W, 5, P.pl1[1], P.pl1[2], P.pl1[3]) trees(f * N * 0.28) bushes(f * N * 0.55) rect(0, 53, W, 5, P.grass[1], P.grass[2], P.grass[3]) poles(f * N) fence(f * N) local fr, f2 = P.frame, P.frame2 rect(0, 0, W, 2, fr[1], fr[2], fr[3]) rect(0, 0, 2, 58, fr[1], fr[2], fr[3]) rect(W - 2, 0, 2, 58, fr[1], fr[2], fr[3]) rect(0, 58, W, H - 58, f2[1], f2[2], f2[3]) rect(0, 58, W, 1, fr[1], fr[2], fr[3]) blend_rect(2, 2, W - 4, 1, 0, 0, 0, 0.3) blend_rect(2, 3, 1, 55, 0, 0, 0, 0.2) blend_rect(W - 3, 3, 1, 55, 0, 0, 0, 0.2) end