info = { type = "image", title = "Morning commute", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Cars and a bus cross a city at sunrise", api = 33, } local ltr = ({ sk = { ["Traffic"] = "Premávka", ["Bus colour"] = "Farba autobusu" }, cs = { ["Traffic"] = "Provoz", ["Bus colour"] = "Barva autobusu" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "traffic", label = LT("Traffic"), type = "int", min = 1, max = 5, default = 3 }, { key = "bus", label = LT("Bus colour"), type = "color", default = "#F0B428" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local function c3(c) return c[1], c[2], c[3] end local SKY = { { 44, 56, 136 }, { 88, 68, 156 }, { 154, 84, 152 }, { 222, 106, 122 }, { 250, 144, 92 }, { 255, 190, 100 } } local SUN, HALO1, HALO2 = { 255, 236, 150 }, { 255, 214, 120 }, { 255, 168, 96 } local FAR = { 128, 76, 128 } local WALK, CURB, ROAD, LINE = { 156, 144, 156 }, { 104, 96, 112 }, { 58, 58, 72 }, { 226, 222, 200 } local TRUNK, LEAF, LEAF_L = { 96, 60, 40 }, { 48, 136, 70 }, { 104, 186, 92 } local LIT = { 255, 208, 112 } local GLASS, TYRE = { 150, 196, 230 }, { 26, 26, 34 } local HEAD, TAIL = { 255, 240, 170 }, { 230, 40, 40 } local CLOUD = { 255, 214, 196 } local BLD = { { 0, 13, 20, { 52, 96, 128 }, { 118, 128, 178 }, { 2, 6, 10 }, 4 }, { 15, 10, 29, { 150, 72, 64 }, { 120, 110, 160 }, { 2, 6 }, 5 }, { 27, 12, 12, { 84, 70, 130 }, { 128, 116, 186 }, { 2, 5, 8 }, 4 }, { 41, 9, 24, { 62, 82, 112 }, { 118, 130, 176 }, { 2, 5 }, 4 }, { 52, 12, 18, { 168, 116, 78 }, { 126, 116, 160 }, { 2, 5, 8 }, 4 }, } local GROUND_Y = 44 local WX, WY, WP, WO = {}, {}, {}, {} do local n = 0 for b = 1, #BLD do local B = BLD[b] for y = B[3] + 3, GROUND_Y - 4, B[7] do for c = 1, #B[6] do local x = B[1] + B[6][c] local tree = (y >= 32) and ((x >= 8 and x <= 15) or (x >= 46 and x <= 53)) if not tree then n = n + 1 local h = hash32(n * 0x9E3779B1) WX[n], WY[n] = x, y WP[n] = 3 * STEP_HZ + umod(h, 8 * STEP_HZ) WO[n] = umod(h >> 12, 600) end end end end end local BG = nil local function tree(x) rect(x, 38, 1, GROUND_Y + 2 - 38, c3(TRUNK)) for y = -4, 4 do local w = floor(sqrt(20 - y * y) + 0.5) rect(x - w, 35 + y, 2 * w + 1, 1, c3(LEAF)) end rect(x - 2, 32, 2, 2, c3(LEAF_L)); px(x - 3, 34, c3(LEAF_L)) end local function build_bg() for i = 1, #SKY do local y0 = (i - 1) * 5 rect(0, y0, W, GROUND_Y - y0, c3(SKY[i])) if i > 1 then for x = 0, W - 1, 2 do px(x, y0 - 1, c3(SKY[i])) end end end local SX, SY, R = 22, 31, 6 for y = SY - R - 4, SY + R + 4 do for x = SX - R - 4, SX + R + 4 do local d = (x - SX) * (x - SX) + (y - SY) * (y - SY) if d <= (R + 0.5) * (R + 0.5) then px(x, y, c3(SUN)) elseif d <= (R + 2) * (R + 2) then px(x, y, c3(HALO1)) elseif d <= (R + 3.5) * (R + 3.5) then px(x, y, c3(HALO2)) end end end for _, f in ipairs({ { 0, 8, 26 }, { 8, 6, 22 }, { 13, 4, 34 }, { 27, 7, 28 }, { 34, 9, 25 }, { 43, 6, 20 }, { 49, 9, 27 }, { 58, 6, 23 } }) do rect(f[1], f[3], f[2], GROUND_Y - f[3], c3(FAR)) end for b = 1, #BLD do local B = BLD[b] rect(B[1], B[3], B[2], GROUND_Y - B[3], c3(B[4])) rect(B[1], B[3], B[2], 1, B[4][1] + 30, B[4][2] + 30, B[4][3] + 30) end for i = 1, #WX do for b = 1, #BLD do local B = BLD[b] if WX[i] >= B[1] and WX[i] < B[1] + B[2] then rect(WX[i], WY[i], 2, 2, c3(B[5])) end end end rect(33, 8, 1, 4, c3(BLD[3][4])) rect(29, 9, 3, 3, 110, 92, 150); rect(28, 8, 5, 1, 110, 92, 150) rect(19, 25, 2, 4, c3(BLD[2][4])) tree(12); tree(50) rect(0, GROUND_Y, W, 4, c3(WALK)) rect(0, 48, W, 1, c3(CURB)) rect(0, 49, W, 12, c3(ROAD)) for x = 1, W - 1, 8 do rect(x, 55, 4, 1, c3(LINE)) end rect(0, 61, W, 1, c3(CURB)) rect(0, 62, W, 2, c3(WALK)) BG = gfx.snapshot(0, 0, W, H) end local function soft_h(x, y, w, h, r, g, b) local xi = floor(x) local f = x - xi if w > 1 then rect(xi + 1, y, w - 1, h, r, g, b) end blend_rect(xi, y, 1, h, r, g, b, 1 - f) blend_rect(xi + w, y, 1, h, r, g, b, f) end local CAR = { L = 12, S = { { 0, -3, 12, 3, true }, { 3, -5, 6, 2, true } }, D = { { 4, -4, 2, 1, GLASS }, { 7, -4, 1, 1, GLASS }, { 2, 0, 2, 1, TYRE }, { 8, 0, 2, 1, TYRE }, { 11, -3, 1, 1, HEAD }, { 0, -3, 1, 1, TAIL } } } local TAXI = { L = 12, S = CAR.S, D = { { 4, -4, 2, 1, GLASS }, { 7, -4, 1, 1, GLASS }, { 2, 0, 2, 1, TYRE }, { 8, 0, 2, 1, TYRE }, { 11, -3, 1, 1, HEAD }, { 0, -3, 1, 1, TAIL }, { 5, -6, 2, 1, { 250, 250, 240 } }, { 1, -2, 10, 1, { 40, 40, 40 } } } } local VAN = { L = 14, S = { { 0, -6, 11, 6, true }, { 11, -4, 3, 4, true } }, D = { { 11, -4, 2, 2, GLASS }, { 2, 0, 2, 1, TYRE }, { 10, 0, 2, 1, TYRE }, { 13, -2, 1, 1, HEAD }, { 0, -2, 1, 1, TAIL }, { 1, -4, 9, 1, { 90, 150, 200 } } } } local BUS = { L = 22, S = { { 0, -7, 22, 7, true } }, D = { { 2, -6, 3, 2, GLASS }, { 6, -6, 3, 2, GLASS }, { 10, -6, 3, 2, GLASS }, { 14, -6, 3, 2, GLASS }, { 19, -6, 3, 3, GLASS }, { 17, -5, 1, 4, { 70, 80, 90 } }, { 0, -3, 22, 1, { 250, 248, 236 } }, { 3, 0, 3, 1, TYRE }, { 15, 0, 3, 1, TYRE }, { 21, -2, 1, 1, HEAD }, { 0, -2, 1, 1, TAIL } } } local PAINT = { { 220, 56, 48 }, { 56, 116, 220 }, { 238, 238, 238 }, { 70, 170, 110 }, { 150, 90, 200 } } local BUSC = { 240, 180, 40 } local function draw_vehicle(T, col, x, y, dir) local L = T.L for i = 1, #T.S do local p = T.S[i] local dx = dir > 0 and p[1] or (L - p[1] - p[3]) local c = p[5] == true and col or p[5] soft_h(x + dx, y + p[2], p[3], p[4], c[1], c[2], c[3]) end local xr = floor(x + 0.5) for i = 1, #T.D do local p = T.D[i] local dx = dir > 0 and p[1] or (L - p[1] - p[3]) local c = p[5] rect(xr + dx, y + p[2], p[3], p[4], c[1], c[2], c[3]) end end local LANES = { { -1, 0.8, 53, 0x9E3779B1 }, { 1, 1.15, 60, 0x85EBCA6B } } local PERIOD = { 120, 90, 72, 60, 50 } local P = 72 local function pick(h) local r = umod(h >> 8, 100) if r < 14 then return BUS, BUSC elseif r < 30 then return TAXI, { 250, 200, 40 } elseif r < 46 then return VAN, (umod(h >> 16, 2) == 0 and { 236, 236, 228 } or { 150, 100, 60 }) end return CAR, PAINT[umod(h >> 16, #PAINT) + 1] end local function draw_lane(ln, t) local dir, v, y, salt = ln[1], ln[2], ln[3], ln[4] local travel = (W + 26) / v local n1 = t // P local n0 = floor((t - travel) / P) - 1 for n = n0, n1 do local h = hash32(n * salt + 11) if umod(h, 100) < 88 then local s = n * P + umod(h >> 20, P // 3) local d = (t - s) * v if d >= 0 then local T, col = pick(h) local x = dir > 0 and (d - T.L) or (W - d) if x > -T.L - 1 and x < W + 1 then draw_vehicle(T, col, x, y, dir) end end end end end local function draw_windows(t) for i = 1, #WX do local e = (t + WO[i]) // WP[i] if umod(hash32(i * 0x27D4EB2F + e * 0x165667B1), 100) < 38 then rect(WX[i], WY[i], 2, 2, LIT[1], LIT[2], LIT[3]) end end if t % 45 < 8 then px(33, 7, 255, 40, 40) end end local CLOUDS = { { 4, 1, 16, 0.10 }, { 40, 5, 12, -0.07 } } local function draw_clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[3] local x = (c[1] + t * c[4]) % (W + L) - L soft_h(x + 3, c[2], L - 6, 1, c3(CLOUD)) soft_h(x, c[2] + 1, L, 1, c3(CLOUD)) soft_h(x + 2, c[2] + 2, L - 5, 1, c3(CLOUD)) end end function on_settings() P = PERIOD[cfg.traffic or 3] or 72 local c = cfg.bus or { r = 240, g = 180, b = 40 } BUSC = { c.r, c.g, c.b } end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS draw_clouds(t) draw_windows(t) draw_lane(LANES[1], t) draw_lane(LANES[2], t) end