info = { type = "image", title = "Train window", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "View from a train window, scenery rushing by", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Sky"] = "Obloha", ["Day"] = "Deň", ["Sunset"] = "Západ slnka", ["Overcast"] = "Zamračené", ["Season"] = "Ročné obdobie", ["Summer"] = "Leto", ["Autumn"] = "Jeseň", ["Winter"] = "Zima", ["Speed"] = "Rýchlosť" }, cs = { ["Sky"] = "Obloha", ["Day"] = "Den", ["Sunset"] = "Západ slunce", ["Overcast"] = "Zataženo", ["Season"] = "Roční období", ["Summer"] = "Léto", ["Autumn"] = "Podzim", ["Winter"] = "Zima", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky"), type = "int", default = "day", opts = { keys = { "day", "sunset", "gray" }, T("Day"), T("Sunset"), T("Overcast") } }, { key = "season", label = T("Season"), type = "int", default = "summer", opts = { keys = { "summer", "autumn", "winter" }, T("Summer"), T("Autumn"), T("Winter") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, brect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local TAU, PI = 6.28318, 3.14159 local SKY = { day = { { 96, 160, 240 }, { 204, 232, 252 }, { 160, 186, 222 }, { 118, 152, 182 }, { 255, 244, 170 } }, sunset = { { 86, 64, 150 }, { 252, 176, 104 }, { 168, 120, 150 }, { 112, 76, 110 }, { 255, 214, 120 } }, gray = { { 128, 140, 164 }, { 198, 204, 216 }, { 160, 168, 186 }, { 128, 140, 156 }, false }, } local SEASON = { summer = { { 94, 160, 72 }, { 76, 140, 60 }, { 116, 176, 84 } }, autumn = { { 206, 156, 60 }, { 170, 100, 46 }, { 126, 146, 62 } }, winter = { { 236, 240, 248 }, { 206, 216, 234 }, { 180, 194, 218 } }, } local GX, GY0 = 5, 8 local SKYG, SKYP, OVG, OVP = "", { 0, 0, 0 }, "", { 0, 0, 0 } local TH = SKY.day local BAND = {} local ST = {} local FAR, NEAR = {}, {} local function stripes(seed) local o, x, i = {}, 0, 0 while x < 96 do i = i + 1 local h = hash32(seed * 0x9E3779B1 + i * 0x85EBCA6B) local w = 5 + umod(h, 6) if x + w > 90 then w = 96 - x end o[#o + 1] = x; o[#o + 1] = w; o[#o + 1] = 1 + umod(h >> 8, 3) x = x + w end return o end local function build() local ZERO = { 0 } do local n = 1 while n < 64 do table.move(ZERO, 1, n, n + 1); n = n * 2 end end local pal, pidx, cv = { 0, 0, 0 }, {}, {} local cw, ch = 64, 64 local function C(r, g, b) r, g, b = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) r = r > 255 and 255 or r; g = g > 255 and 255 or g; b = b > 255 and 255 or b local k = r * 65536 + g * 256 + b local i = pidx[k] if not i then i = #pal // 3 pidx[k] = i local n = #pal pal[n + 1], pal[n + 2], pal[n + 3] = r, g, b end return i end local function reset(w, h) cw, ch = w, h for y = 1, h do cv[y] = table.move(ZERO, 1, w, 1, cv[y] or {}) end end local function fill(r, a, n, c) r[a] = c local k = 1 while k < n do local m = (k <= n - k) and k or (n - k) table.move(r, a, a + m - 1, a + k) k = k + m end end local function R(x, y, w, h, c) local xa, xb = (x < 0 and 0 or x), (x + w - 1 > cw - 1 and cw - 1 or x + w - 1) local ya, yb = (y < 0 and 0 or y), (y + h - 1 > ch - 1 and ch - 1 or y + h - 1) if xa > xb or ya > yb then return end local n = xb - xa + 1 local o = xa + 1 if n >= 12 then local r = cv[ya + 1] fill(r, o, n, c) for yy = ya + 2, yb + 1 do table.move(r, o, o + n - 1, o, cv[yy]) end else for yy = ya + 1, yb + 1 do local r = cv[yy] for i = o, o + n - 1 do r[i] = c end end end end local function E(cx, cy, rx, ry, c) for y = floor(cy - ry), ceil(cy + ry) - 1 do local dy = (y + 0.5 - cy) / ry local q = 1 - dy * dy if q >= 0 then local hw = rx * sqrt(q) local x0 = ceil(cx - hw - 0.5) R(x0, y, floor(cx + hw - 0.5) - x0 + 1, 1, c) end end end local function P(x, y, c) R(x, y, 1, 1, c) end local function bake() local rows = {} for y = 0, ch - 1 do rows[y + 1] = string.char(table.unpack(cv[y + 1], 1, cw)) end return table.concat(rows), pal end local s1, s2, sun = TH[1], TH[2], TH[5] reset(54, 34) for y = 0, 33 do local k = y / 27 if k > 1 then k = 1 end R(0, y, 54, 1, C(s1[1] + (s2[1] - s1[1]) * k, s1[2] + (s2[2] - s1[2]) * k, s1[3] + (s2[3] - s1[3]) * k)) end if sun then local hm = C(s2[1] * 0.8 + sun[1] * 0.2, s2[2] * 0.8 + sun[2] * 0.2, s2[3] * 0.8 + sun[3] * 0.2) E(37.5, 11.5, 8, 8, hm) E(37.5, 11.5, 4.6, 4.6, C(sun[1], sun[2], sun[3])) end SKYG, SKYP = bake() pal, pidx = { 0, 0, 0 }, {} reset(64, 64) local WOOD, SEAM, DADO, TRIM = C(112, 66, 46), C(90, 50, 36), C(92, 52, 38), C(158, 104, 66) local FRM, FRM_D, FRM_L = C(228, 216, 192), C(176, 162, 138), C(246, 238, 220) R(0, 0, 64, 64, WOOD) for x = 4, 63, 10 do R(x, 0, 1, 64, SEAM) end R(0, 50, 64, 14, DADO) R(0, 49, 64, 1, TRIM) for x = 4, 63, 10 do R(x, 50, 1, 14, C(74, 40, 30)) end R(2, 4, 60, 38, FRM_D) R(2, 4, 60, 37, FRM) R(2, 4, 60, 1, FRM_L) R(5, 8, 54, 30, 0) R(4, 7, 56, 1, FRM_D); R(4, 38, 56, 1, FRM_D) R(4, 7, 1, 32, FRM_D); R(59, 7, 1, 32, FRM_D) R(31, 8, 2, 30, FRM) R(31, 8, 1, 30, FRM_L) R(2, 39, 60, 2, FRM_L) R(2, 41, 60, 1, FRM_D) R(16, 46, 48, 3, C(182, 120, 74)) R(16, 46, 48, 1, C(210, 146, 92)) R(16, 49, 48, 1, C(128, 78, 50)) local CU, CU_L, CU_D, GOLD = C(128, 30, 48), C(166, 48, 66), C(94, 20, 36), C(232, 190, 90) for y = 8, 37 do local e = 13 - floor(3 * 2.718 ^ (-(((y - 24) / 2.6) ^ 2)) + 0.5) + floor(1.2 * sin(y * 0.4) + 0.5) local r = cv[y + 1] for x = 5, e do local m = (x - 5) % 3 r[x + 1] = m == 0 and CU_L or (m == 2 and CU_D or CU) end end R(5, 8, 10, 2, CU_D) R(5, 24, 8, 1, GOLD) OVG, OVP = bake() end local H = {} local function mountains(tab, k1, k2, base, amp, ph) for i = 1, 256 do local xw = (i - 1) / 2 tab[i] = base + amp * abs(sin(PI * k1 * xw / 128 + ph)) + 1.2 * sin(TAU * k2 * xw / 128 + ph * 2) end end mountains(FAR, 2, 5, 17, 6, 0.3) mountains(NEAR, 3, 7, 22, 3.5, 1.1) function on_settings() TH = SKY[cfg.sky or "day"] or SKY.day local sc = SEASON[cfg.season or "summer"] or SEASON.summer local hz = TH[2] BAND = {} for b = 1, 3 do local mix = ({ 0.3, 0.1, 0 })[b] local dk = ({ 1, 0.95, 0.82 })[b] local cols = {} for i = 1, 3 do local c = sc[i] cols[i] = { (c[1] * (1 - mix) + hz[1] * mix) * dk, (c[2] * (1 - mix) + hz[2] * mix) * dk, (c[3] * (1 - mix) + hz[3] * mix) * dk } cols[i] = { floor(cols[i][1] + 0.5), floor(cols[i][2] + 0.5), floor(cols[i][3] + 0.5) } end BAND[b] = cols end ST = { stripes(1), stripes(2), stripes(3) } build() end local BY = { 28, 31, 34 } local BH = { 3, 3, 6 } local SPD = { 0.28, 0.62, 1.5 } local function srect(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if f < 0.04 then rect(ix, y, w, h, r, g, b); return end rect(ix + 1, y, w - 1, h, r, g, b) brect(ix, y, 1, h, r, g, b, 1 - f) brect(ix + w, y, 1, h, r, g, b, f) end local function ridge(tab, off, y0, base, col) local x0, yy = 0, -1 for x = GX, GX + 54 do local y = x < GX + 54 and floor(tab[floor((x - GX) * 2 + off * 2) % 256 + 1] + 0.5) + y0 or -2 if y ~= yy then if yy >= 0 then rect(x0, yy, x - x0, base - yy, col[1], col[2], col[3]) end x0, yy = x, y end end end local CUPX = 43 function render() FR = frame * STEP_HZ // gfx.FPS local t = FR local v = 0.4 + 0.3 * (cfg.speed or 3) local dy = floor(0.9 * sin(t * 0.09) + 0.5 * sin(t * 0.23) + 0.5) gfx.blit_indexed(SKYG, SKYP, GX, 6 + dy, 54, 34, 54, 34) ridge(FAR, (t * v * 0.05) % 128, dy, 28 + dy, TH[3]) ridge(NEAR, (t * v * 0.12) % 128, dy, 28 + dy, TH[4]) do local x = (60 - (t * v * 0.28) % 96) % 96 - 20 srect(x, 25 + dy, 6, 3, 186, 98, 76) srect(x - 1, 23 + dy, 8, 2, 92, 62, 74) end for b = 1, 3 do local s, cols = ST[b], BAND[b] local off = (t * v * SPD[b]) % 96 if b == 3 then off = floor(off) end local y, h = BY[b] + dy, BH[b] for i = 1, #s, 3 do local c = cols[s[i + 2]] local x = (s[i] - off) % 96 for rep = 0, 1 do local xx = x - rep * 96 + GX if xx < GX + 54 and xx + s[i + 1] > GX then if b == 3 then rect(xx, y, s[i + 1], h, c[1], c[2], c[3]) else srect(xx, y, s[i + 1], h, c[1], c[2], c[3]) end end end end end local po = floor(t * v * 2.6) % 64 for i = 0, 1 do local x = (i * 64 - po + 64) % 128 - 4 + GX if x < GX + 54 then rect(x, 6 + dy, 2, 34, 64, 46, 40) rect(x - 2, 10 + dy, 6, 1, 64, 46, 40) end end gfx.blit_indexed(OVG, OVP, 0, 0, 64, 64, 64, 64, true) local ang = 0.32 * sin(t * 0.09 - 0.9) + 0.12 * sin(t * 0.23 - 1.4) local sa, ca = sin(ang), cos(ang) for k = 1, 5 do px(8 + floor(sa * k + 0.5), 25 + floor(ca * k + 0.5), 232, 190, 90) end rect(7 + floor(sa * 7 + 0.5), 25 + floor(ca * 7 + 0.5) - 1, 3, 3, 232, 190, 90) rect(CUPX - 3, 45, 11, 1, 240, 240, 248) rect(CUPX, 41, 7, 4, 250, 250, 255) rect(CUPX, 44, 7, 1, 206, 210, 232) rect(CUPX + 7, 42, 2, 1, 250, 250, 255); px(CUPX + 8, 43, 250, 250, 255); rect(CUPX + 7, 44, 2, 1, 250, 250, 255) rect(CUPX, 41, 7, 1, 150, 84, 44) if (t // 5) % 3 == 0 then px(CUPX + 2 + (t // 15) % 3, 41, 190, 120, 72) end for p = 0, 3 do local a = (t * 0.5 + p * 15) % 60 local x = CUPX + 3 + 1.8 * sin(a * 0.12 + p * 1.7) brect(floor(x), floor(40 - a * 0.33), 2, 2, 255, 255, 255, 0.45 * sin(PI * a / 60)) end end