info = { type = "image", title = "Highway light trails", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Dusk skyline, long-exposure car light trails", api = 44, } local tr = ({ sk = { ["Traffic"] = "Premávka", ["Lit windows"] = "Svietiace okná" }, cs = { ["Traffic"] = "Provoz", ["Lit windows"] = "Svítící okna" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cars", label = T("Traffic"), type = "int", min = 3, max = 20, default = 12 }, { key = "windows", label = T("Lit windows"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor = math.floor local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local SKY = { { 14, 24, 70 }, { 64, 80, 150 }, { 236, 142, 112 } } local FAR, BUILD, EDGE = { 80, 90, 142 }, { 26, 32, 66 }, { 58, 74, 124 } local ROAD, WALL = { 22, 24, 42 }, { 30, 34, 60 } local ROAD_Y = 44 local TOWERS = { { 1, 10, 24 }, { 9, 9, 16 }, { 19, 6, 28 }, { 24, 11, 10, 5, 5 }, { 35, 8, 20 }, { 42, 9, 13 }, { 50, 6, 26 }, { 55, 9, 18 }, } local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local function sky_and_far() for y = 0, 40 do local a, b, t = SKY[1], SKY[2], y / 22 if y >= 22 then a, b, t = SKY[2], SKY[3], (y - 22) / 18 end rect(0, y, W, 1, lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end rect(0, 41, W, H - 41, SKY[3][1], SKY[3][2], SKY[3][3]) local x, i = 0, 0 while x < W do i = i + 1 local h = hash32(i * 0x85EBCA6B) local w, top = 5 + umod(h, 4), 26 + umod(h >> 8, 9) rect(x, top, w, ROAD_Y - top, FAR[1], FAR[2], FAR[3]) x = x + w end end local function towers() for _, t in ipairs(TOWERS) do local x, w, top, sw, st = t[1], t[2], t[3], t[4], t[5] rect(x, top, w, ROAD_Y - top, BUILD[1], BUILD[2], BUILD[3]) rect(x, top, 1, ROAD_Y - top, EDGE[1], EDGE[2], EDGE[3]) if sw then local sx = x + (w - sw) // 2 rect(sx, st, sw, top - st, BUILD[1], BUILD[2], BUILD[3]); rect(sx, st, 1, top - st, EDGE[1], EDGE[2], EDGE[3]) rect(sx + sw // 2, st - 3, 1, 3, BUILD[1], BUILD[2], BUILD[3]) end end for y = 14, ROAD_Y - 1 do blend_rect(0, y, W, 1, 214, 120, 80, (y - 14) / 30 * 0.34) end end local function road() rect(0, ROAD_Y, W, 2, WALL[1], WALL[2], WALL[3]); rect(0, ROAD_Y, W, 1, EDGE[1], EDGE[2], EDGE[3]) rect(0, ROAD_Y + 2, W, H - ROAD_Y - 2, ROAD[1], ROAD[2], ROAD[3]) for x = 0, W - 1, 6 do rect(x, 53, 3, 1, 90, 92, 120) end blend_rect(0, 47, W, 6, 255, 80, 80, 0.07) blend_rect(0, 54, W, 7, 200, 210, 255, 0.07) rect(0, 62, W, 2, 14, 16, 30) for _, x in ipairs({ 13, 41 }) do rect(x, 33, 1, ROAD_Y - 33, 20, 22, 40) blend_rect(x - 3, 29, 7, 6, 255, 190, 110, 0.14); blend_rect(x - 2, 30, 5, 4, 255, 200, 130, 0.2) rect(x - 1, 32, 3, 1, 255, 224, 160) end end local STAGES = { sky_and_far, towers, road } local BG, STAGE = nil, 0 local CYC = 960 local WX, WY, WON, WDUR, WK = {}, {}, {}, {}, {} local NW = 0 for b, t in ipairs(TOWERS) do local x, w, top = t[1], t[2], t[3] local row = 0 for y = top + 3, ROAD_Y - 3, 3 do row = row + 1 local hf = hash32(b * 7919 + row * 104729) local base, dur = umod(hf, CYC), floor(CYC * (0.55 + umod(hf >> 12, 30) / 100)) for cx = x + 1, x + w - 2, 2 do local hw = hash32(cx * 15485863 + y * 0x9E3779B1) if umod(hw, 100) < 60 then NW = NW + 1 WX[NW], WY[NW] = cx, y WON[NW], WDUR[NW] = base + umod(hw >> 8, 50), dur + umod(hw >> 16, 40) WK[NW] = umod(hw >> 24, 3) + 1 end end end end local WCOL = { { 255, 200, 120 }, { 255, 170, 84 }, { 190, 215, 255 } } local WDIM = { { 120, 92, 60 }, { 120, 80, 44 }, { 92, 104, 124 } } local SHOW_WINDOWS = true local LANE_Y, LANE_H, LANE_DIR = { 48, 51, 55, 59 }, { 1, 1, 2, 2 }, { -1, -1, 1, 1 } local LANE_V = { 0.5, 0.5, 0.8, 0.8 } local TRAIL = { [1] = { { 255, 110, 100 }, { 236, 54, 60 }, { 150, 30, 52 }, { 78, 24, 50 } }, [-1] = { { 255, 255, 250 }, { 214, 224, 255 }, { 132, 148, 206 }, { 64, 72, 120 } }, } local CARS = {} for i = 1, 20 do local h = hash32(i * 0x9E3779B1) local lane = (i - 1) % 4 + 1 local len = 12 + umod(h >> 9, 16) CARS[i] = { lane = lane, len = len, v = (0.7 + umod(h >> 4, 50) / 100) * LANE_V[lane] * 1.2, off = umod(h >> 14, 300), a = 2 + len // 6, b = len * 3 // 10, c = len * 35 // 100, d = len // 5 + 1, } end local NCARS = 12 function on_settings() NCARS = cfg.cars or 12 SHOW_WINDOWS = cfg.windows ~= false end local function car(c) local dir, y, h = LANE_DIR[c.lane], LANE_Y[c.lane], LANE_H[c.lane] local col = TRAIL[-dir] local span = W + c.len + 16 local p = (c.off + c.v * frame) % span local hx = dir == 1 and p - 8 or W + 8 - p local ix = floor(hx) local f = hx - ix local c1, c2, c3, c4 = col[1], col[2], col[3], col[4] if dir == 1 then blend_rect(ix, y, 1, h, c1[1], c1[2], c1[3], f) local e = ix rect(e - c.a, y, c.a, h, c1[1], c1[2], c1[3]); e = e - c.a rect(e - c.b, y, c.b, h, c2[1], c2[2], c2[3]); e = e - c.b rect(e - c.c, y, c.c, h, c3[1], c3[2], c3[3]); e = e - c.c rect(e - c.d, y, c.d, h, c4[1], c4[2], c4[3]) else blend_rect(ix, y, 1, h, c1[1], c1[2], c1[3], 1 - f) local e = ix + 1 rect(e, y, c.a, h, c1[1], c1[2], c1[3]); e = e + c.a rect(e, y, c.b, h, c2[1], c2[2], c2[3]); e = e + c.b rect(e, y, c.c, h, c3[1], c3[2], c3[3]); e = e + c.c rect(e, y, c.d, h, c4[1], c4[2], c4[3]) end end local function beacon(x, y, period, ph) if (frame + ph) % period < 9 then blend_rect(x - 1, y - 1, 3, 3, 255, 60, 50, 0.3) px(x, y, 255, 90, 70) end end function render() if STAGE < #STAGES then if BG then gfx.blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE + 1]() STAGE = STAGE + 1 BG = gfx.snapshot(0, 0, W, H) if STAGE < #STAGES then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, W, H) if SHOW_WINDOWS then for i = 1, NW do local d = (frame - WON[i]) % CYC local dur = WDUR[i] if d < dur then local k = WK[i] local c = (d < 10 or d > dur - 10) and WDIM[k] or WCOL[k] px(WX[i], WY[i], c[1], c[2], c[3]) end end end beacon(29, 2, 50, 0); beacon(46, 9, 64, 21); beacon(12, 15, 56, 33) for i = 1, NCARS do car(CARS[i]) end end