info = { type = "image", title = "Bridge at dusk", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Cable lights switch on as cars cross at sunset", api = 33, } local tr = ({ sk = { ["Cars"] = "Autá", ["Light colour"] = "Farba svetiel" }, cs = { ["Cars"] = "Auta", ["Light colour"] = "Barva světel" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cars", label = T("Cars"), type = "int", min = 0, max = 8, default = 6 }, { key = "glow", label = T("Light colour"), type = "color", default = "#FFC678" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local HORIZON, DECK = 36, 38 local P1, P2, TOP = 12, 46, 11 local SIL = { 24, 14, 44 } local SUN = { 255, 218, 130 } local function c3(c) return c[1], c[2], c[3] end local function lerp(a, b, t) return a + (b - a) * t end local function cable(x) if x <= P1 then return TOP + (P1 - x) * 22 / P1 end if x >= P2 then return TOP + (x - P2) * 22 / (W - 1 - P2) end local u = (x - (P1 + P2) / 2) / ((P2 - P1) / 2) return TOP + 17 * (1 - u * u) end local HX, HY, HD = {}, {}, {} for x = 3, W - 2, 3 do if abs(x - P1) > 2 and abs(x - P2) > 2 then local i = #HX + 1 HX[i], HY[i] = x, floor(cable(x) + 0.5) HD[i] = min(abs(x - P1), abs(x - P2)) end end local GR, GG, GB = 255, 198, 120 local BG, NCARS = nil, 6 function on_settings() local c = cfg.glow or { r = 255, g = 198, b = 120 } GR, GG, GB = c.r, c.g, c.b NCARS = cfg.cars or 6 BG = nil end local RINGS = { { 10, 255, 150, 90, 0.22 }, { 8, 255, 180, 100, 0.3 }, { 5.5, SUN[1], SUN[2], SUN[3], 1 } } local function build_bg() for y = 0, HORIZON - 1 do local t = y / HORIZON if t < 0.62 then local k = t / 0.62 rect(0, y, W, 1, floor(lerp(34, 150, k)), floor(lerp(22, 46, k)), floor(lerp(74, 104, k))) else local k = (t - 0.62) / 0.38 rect(0, y, W, 1, floor(lerp(150, 252, k)), floor(lerp(46, 142, k)), floor(lerp(104, 70, k))) end end for y = 20, HORIZON do local dy = y + 0.5 - 34 for _, ring in ipairs(RINGS) do local q = ring[1] * ring[1] - dy * dy if q > 0 then local hw = math.sqrt(q) local x0 = floor(33 - hw + 0.5) blend_rect(x0, y, floor(33 + hw + 0.5) - x0, 1, ring[2], ring[3], ring[4], ring[5]) end end end for x = 0, W - 1 do local top = floor(HORIZON - 2 + 1.6 * sin(x * 0.17) + 1.2 * sin(x * 0.41 + 2) + 0.5) rect(x, top, 1, H, 74, 28, 76) end for y = HORIZON + 1, H - 1 do local t = (y - HORIZON - 1) / (H - HORIZON - 2) rect(0, y, W, 1, floor(lerp(196, 40, t ^ 0.8)), floor(lerp(100, 22, t ^ 0.8)), floor(lerp(70, 70, t))) end rect(0, DECK, W, 2, c3(SIL)); rect(0, DECK + 2, W, 1, 90, 40, 80) for _, c in ipairs({ P1, P2 }) do rect(c - 1, TOP, 3, 33, c3(SIL)) rect(c - 2, TOP - 1, 5, 1, c3(SIL)); rect(c - 2, 17, 5, 1, c3(SIL)); rect(c - 2, 26, 5, 1, c3(SIL)) rect(c - 2, 36, 5, 8, c3(SIL)) end for x = 0, W - 1 do local a, b = floor(cable(x) + 0.5), floor(cable(min(W - 1, x + 1)) + 0.5) local y0 = min(a, b) rect(x, y0, 1, abs(b - a) + 1, 56, 30, 70) end for i = 1, #HX do rect(HX[i], HY[i] + 1, 1, DECK - HY[i] - 1, 44, 24, 62) end BG = gfx.snapshot(0, 0, W, H) end local function frect(x, y, w, R, G, B, a) local ia, xb = floor(x), x + w local ib = floor(xb) if ib == ia then blend_rect(ia, y, 1, 1, R, G, B, a * w); return end blend_rect(ia, y, 1, 1, R, G, B, a * (1 - (x - ia))) if ib - ia > 1 then blend_rect(ia + 1, y, ib - ia - 1, 1, R, G, B, a) end if xb > ib then blend_rect(ib, y, 1, 1, R, G, B, a * (xb - ib)) end end local CYCLE, FADE_AT = 960, 780 local function cars() local nA, nB = (NCARS + 1) // 2, NCARS // 2 for lane = 0, 1 do local n = lane == 0 and nA or nB local v = lane == 0 and 0.22 or -0.31 local y = lane == 0 and DECK - 2 or DECK - 1 for k = 0, n - 1 do local span = W + 16 local x = ((frame * v + k * span / n + lane * 17) % span + span) % span - 8 local d = v > 0 and 1 or -1 frect(x, y, 3, 40, 24, 56, 1) frect(x + (d > 0 and 3 or -1), y, 1, 255, 236, 180, 1) frect(x + (d > 0 and -1 or 3), y, 1, 240, 50, 40, 0.9) frect(x + (d > 0 and 4 or -3), y, 2, 255, 220, 150, 0.25) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) for y = DECK + 3, 58 do local t = (y - DECK - 3) / 20 local wob = 1.6 * sin(y * 0.9 - frame * 0.09) * (0.4 + t) frect(33 + wob - (6 - 4 * t) / 2, y, 6 - 4 * t, 255, 190, 110, 0.4 * (1 - t * 0.9)) end for r = 0, 8 do local x = (frame * (0.05 + 0.02 * (r % 3)) + r * 17) % 80 - 10 frect(x, DECK + 6 + floor(r * 2.6), 5 + r % 3, 255, 170, 130, 0.14) end local t = frame % CYCLE local g = t > FADE_AT and max(0, 1 - (t - FADE_AT) / 60) or 1 for i = 1, #HX do local a = min(1, max(0, (t - 30 - HD[i] * 3.2) / 8)) * g if a > 0 then local x, y = HX[i], HY[i] blend_rect(x - 1, y - 1, 3, 3, GR, GG, GB, 0.22 * a) blend(x, y, GR, GG, GB, a) for k = 0, (i % 2 == 0 and 4 or -1) do local wx = x + 0.5 + 1.4 * sin((DECK + 3 + k * 3) * 0.9 - frame * 0.09 + i) frect(wx - 0.5, DECK + 4 + k * 3, 1, GR, GG, GB, a * 0.42 * (1 - k / 6)) end end end for _, c in ipairs({ P1, P2 }) do if (frame // 15) % 2 == 0 then blend(c, TOP - 2, 255, 60, 50, 1) end end if NCARS > 0 then cars() end end