info = { type = "image", title = "Sunset viaduct", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Steam train crossing a stone bridge at sunset", api = 33, } local tr = ({ sk = { ["Train speed"] = "Rýchlosť vlaku", ["Carriages"] = "Vagóny", ["Smoke"] = "Dym" }, cs = { ["Train speed"] = "Rychlost vlaku", ["Carriages"] = "Vagony", ["Smoke"] = "Kouř" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Train speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "cars", label = T("Carriages"), type = "int", min = 1, max = 4, default = 3 }, { key = "smoke", label = T("Smoke"), type = "bool", default = true }, } 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, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local STONE = { 60, 30, 54 } local ARCH_C, ARCH_R, HORIZON = { 9, 27, 45, 63 }, 7, 36 local TOP = 11 local SKY = { { 0, 104, 34, 76 }, { 14, 196, 70, 66 }, { 26, 246, 134, 62 }, { 36, 255, 196, 100 } } local function skycol(y) for i = 1, #SKY - 1 do local a, b = SKY[i], SKY[i + 1] if y <= b[1] then local u = (y - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5), floor(a[4] + (b[4] - a[4]) * u + 0.5) end end return 255, 196, 100 end local SCENE, SBG = nil, nil local function stone_rows(y0, y1, draw) for y = y0, y1 do local dy = y + 0.5 - HORIZON local x = 0 if dy >= -ARCH_R then local hw = floor(sqrt(ARCH_R * ARCH_R - dy * dy) + 0.5) for k = 1, #ARCH_C do if ARCH_C[k] - hw > x then draw(x, y, ARCH_C[k] - hw - x) end x = ARCH_C[k] + hw end if x < W then draw(x, y, W - x) end else draw(0, y, W) end end end local SSTAGE = 1 local SBUILD = { function() for y = 0, HORIZON do rect(0, y, W, 1, skycol(y)) end blend_rect(33, 22, 24, 16, 255, 200, 110, 0.07) blend_rect(36, 24, 18, 13, 255, 210, 120, 0.09) for dy = -7, 7 do local hw = floor(sqrt(7 * 7 - dy * dy) + 0.5) rect(45 - hw, 34 + dy, 2 * hw, 1, 255, 214, 124) end for dy = -6, 6 do local hw = floor(sqrt(6 * 6 - dy * dy) + 0.5) rect(45 - hw, 34 + dy, 2 * hw, 1, 255, 246, 208) end for y = HORIZON + 1, H - 1 do local t = (y - HORIZON - 1) / (H - HORIZON - 2) rect(0, y, W, 1, floor(222 - 128 * t + 0.5), floor(132 - 88 * t + 0.5), floor(74 - 2 * t + 0.5)) end end, function() for y = HORIZON + 1, H - 1 do local hw = floor(2 + (y - HORIZON) * 0.4) blend_rect(45 - hw, y, 2 * hw, 1, 255, 214, 124, 0.48 - (y - HORIZON) * 0.013) end stone_rows(22, 36, function(x, y, w) blend_rect(x, 2 * HORIZON + 1 - y, w, 1, 44, 22, 56, 0.6) end) stone_rows(22, 36, function(x, y, w) rect(x, y, w, 1, STONE[1], STONE[2], STONE[3]) end) rect(0, 22, W, 1, 124, 66, 66); rect(0, 26, W, 1, 76, 40, 66); rect(0, 27, W, 1, 46, 24, 46) for i = 1, 26 do local h = hash32(i * 0x9E3779B1 + 5) px(umod(h, W), 23 + umod(h >> 8, 3), 74, 40, 66) end for k = 1, #ARCH_C do for y = 29, 36 do local dy = y + 0.5 - HORIZON local hw = floor(sqrt(ARCH_R * ARCH_R - dy * dy) + 0.5) px(ARCH_C[k] - hw - 1, y, 118, 56, 62); px(ARCH_C[k] + hw, y, 118, 56, 62) end end for k = 1, 3 do local x0 = ARCH_C[k] + ARCH_R rect(x0, 37, 4, 4, STONE[1], STONE[2], STONE[3]) rect(x0 - 1, 37, 6, 1, 120, 70, 70) blend_rect(x0 - 2, 41, 8, 1, 255, 190, 110, 0.3) end end, } local function build_scene() if SBG then blit_rgb(SBG, 0, 0, W, H) end SBUILD[SSTAGE]() SBG = snapshot(0, 0, W, H) SSTAGE = SSTAGE + 1 if SSTAGE > #SBUILD then SCENE = SBG end end local CCOL = { [1] = { 34, 16, 38 }, [2] = { 255, 208, 104 }, [3] = { 112, 60, 72 }, [4] = { 130, 64, 64 }, [5] = { 255, 230, 150 } } local GRIDS, TL, TW = nil, 0, 0 local STRIPS = {} local function build_grids(ncars) TL = 12 * ncars + 16 TW = TL + 2 GRIDS = {} for pose = 0, 1 do local G = {} for r = 0, 10 do G[r] = {} for x = 0, TL - 1 do G[r][x] = 0 end end local function fill(x0, x1, r0, r1, c) for r = r0, r1 do for x = x0, x1 do G[r][x] = c end end end for i = 0, ncars - 1 do local a = 12 * i fill(a, a + 10, 3, 8, 1); fill(a + 1, a + 9, 2, 2, 4) fill(a + 1, a + 2, 4, 5, 2); fill(a + 4, a + 5, 4, 5, 2); fill(a + 7, a + 8, 4, 5, 2) fill(a + 1, a + 3, 9, 10, 1); fill(a + 7, a + 9, 9, 10, 1) G[8][a + 11] = 1 end local b = 12 * ncars fill(b, b + 5, 2, 8, 1); fill(b, b + 5, 1, 1, 4); fill(b + 2, b + 3, 3, 4, 2) fill(b + 6, b + 14, 4, 8, 1); fill(b + 15, b + 15, 5, 8, 1) fill(b + 11, b + 12, 1, 3, 1); fill(b + 10, b + 13, 0, 0, 1); fill(b + 7, b + 8, 3, 3, 1) fill(b + 13, b + 15, 9, 9, 1) for _, w in ipairs({ 1, 6, 11 }) do fill(b + w, b + w + 3, 9, 10, 1) end for _, w in ipairs({ 1, 6, 11 }) do if pose == 0 then G[9][b + w] = 3 else G[10][b + w + 3] = 3 end end G[5][b + 15] = 5 GRIDS[pose] = G end STRIPS = {} end local function strip(ph, pose) local key = ph * 2 + pose local s = STRIPS[key] if s then return s end local f = ph / 4 local G = GRIDS[pose] local out = {} for r = 0, 10 do local br, bg, bb = skycol(TOP + r) local row = G[r] local pr, pg, pb = br, bg, bb for c = 0, TW - 1 do local x = c - 1 local cl = (x >= 0 and x < TL) and row[x] or 0 local cr, cg, cb = br, bg, bb if cl ~= 0 then cr, cg, cb = CCOL[cl][1], CCOL[cl][2], CCOL[cl][3] end local i = (r * TW + c) * 3 out[i + 1], out[i + 2], out[i + 3] = floor(cr + (pr - cr) * f + 0.5), floor(cg + (pg - cg) * f + 0.5), floor(cb + (pb - cb) * f + 0.5) pr, pg, pb = cr, cg, cb end end local parts = {} for i = 1, #out, 120 do parts[#parts + 1] = char(unpack(out, i, min(#out, i + 119))) end s = concat(parts) STRIPS[key] = s return s end function on_settings() build_grids(cfg.cars or 3) SCENE, SBG, SSTAGE = nil, nil, 1 end local CLOUDS = { { 4, 3, 20, 3 }, { 40, 6, 24, 4 }, { 22, 8, 16, 2.5 }, { 58, 4, 14, 5 } } local function puff(x, y, w, r, g, b, a) local yi = floor(y) local f = y - yi blend_rect(x, yi, w, 1, r, g, b, a * (1 - f)) blend_rect(x, yi + 1, w, 1, r, g, b, a) blend_rect(x, yi + 2, w, 1, r, g, b, a * f) end local function ripple(i, s, x0, x1, y0, y1, r, g, b, amp) local p = 1.1 + (hash32(i * 0x2545F491) & 0xFF) / 100 local u = s / p + (hash32(i * 0x9E3779B1) & 0xFF) / 256 local k = floor(u) local h = hash32(i * 0x85EBCA6B + k * 0x27D4EB2F) local a = sin((u - k) * PI) * amp if a > 0.02 then blend_rect(x0 + umod(h, x1 - x0), y0 + umod(h >> 8, y1 - y0), 3 + umod(h >> 16, 5), 1, r, g, b, a) end end function render() if not GRIDS then on_settings() end if not SCENE then build_scene() return end gfx.blit_rgb(SCENE, 0, 0, W, H) local s = frame / FPS + 4 for i = 1, #CLOUDS do local c = CLOUDS[i] local x = floor((c[1] + s * (0.6 + i * 0.15)) % (W + 40)) - 20 blend_rect(x, c[2], c[3], 1, 150, 50, 84, 0.32) blend_rect(x + 2, c[2] + 1, c[3] - 5, 1, 255, 170, 90, 0.26) blend_rect(x + 5, c[2] - 1, c[3] - 10, 1, 150, 50, 84, 0.2) end for i = 1, 12 do ripple(i, s, 36, 54, 39, 62, 255, 226, 150, 0.5) end for i = 13, 24 do ripple(i, s, 0, 62, 40, 63, 255, 160, 100, 0.22) end for i = 25, 32 do ripple(i, s, 2, 58, 38, 52, 40, 20, 56, 0.3) end local v = 4 + 2 * (cfg.speed or 3) local D = W + TL + 4 local Tc = D / v + 4 local c = floor(s / Tc) local sl = s - c * Tc if sl * v <= D then local X = -TL - 2 + v * sl local xi = floor(X) local ph = floor((X - xi) * 4) local pose = floor(X / 2.2) % 2 blit_rgb(strip(ph, pose), xi - 1, TOP, TW, 11) end if cfg.smoke ~= false then local cx = 12 * (cfg.cars or 3) + 11 for j = floor((sl - 4.5) / 0.45), floor(sl / 0.45) do local tb = j * 0.45 if tb >= 0 and tb * v <= D then local age = sl - tb local u = age / 4.5 if u >= 0 and u < 1 then local wx = -TL - 2 + v * tb + cx - 1.6 * age local r = 1 + floor(u * 4) local a = 0.62 * (1 - u) * min(1, age * 3) puff(floor(wx - r + 0.5), 11 - 3.5 * age * (1 - u * 0.4), 2 * r, 255, 214, 186, a) end end end end end