info = { type = "image", title = "Dusk drive", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Driving down a lit city street at dusk", api = 33, } local tr = ({ sk = { ["Time"] = "Denná doba", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Speed"] = "Rýchlosť", ["Traffic"] = "Premávka" }, cs = { ["Time"] = "Denní doba", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Speed"] = "Rychlost", ["Traffic"] = "Provoz" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time"), type = "int", default = "dusk", opts = { keys = { "dusk", "night" }, T("Dusk"), T("Night") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "traffic", label = T("Traffic"), 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, cos, sqrt, max, min, abs = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min, math.abs local ceil = math.ceil local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, DASH, ROWS = 24, 46, 21 local ZK, C1 = 36, 0.085 local KEY = 255 local LW = 1.9 local BAY, IEV = 4, 7 local TIMES = { dusk = { skyT = { 34, 38, 112 }, skyH = { 250, 146, 96 }, line = { 78, 58, 116 }, lit = 2, F = { { 64, 54, 92 }, { 78, 60, 92 }, { 52, 62, 100 }, { 88, 70, 78 } }, road = { 78, 78, 94 }, side = { 124, 108, 118 }, glow = 0.30 }, night = { skyT = { 4, 6, 22 }, skyH = { 24, 28, 74 }, line = { 16, 18, 42 }, lit = 3, F = { { 28, 30, 56 }, { 36, 32, 60 }, { 26, 36, 64 }, { 42, 36, 54 } }, road = { 32, 34, 48 }, side = { 52, 50, 68 }, glow = 0.42 }, } local TM, RD, SD = TIMES.dusk, {}, {} local SKY = nil local function mixc(a, b, t) return { floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) } end function on_settings() TM = TIMES[cfg.time] or TIMES.dusk for r = 1, ROWS do RD[r] = mixc(TM.skyH, TM.road, 0.65 + 0.35 * r / ROWS) SD[r] = mixc(TM.skyH, TM.side, 0.65 + 0.35 * r / ROWS) end SKY = nil end local HF = {} for i = 0, 255 do HF[i + 1] = 3 + umod(hash32((i // 4) * 0x9E3779B1), 100) / 100 * 8 end local function hills(shift, c) for x = 0, W - 2, 2 do local p = (x + shift) % 256 local i = floor(p) local f = p - i i = i % 256 local top = HZ - (HF[i + 1] * (1 - f) + HF[(i + 1) % 256 + 1] * f) local iy = floor(top) rect(x, iy + 1, 2, HZ - iy, c[1], c[2], c[3]) blend_rect(x, iy, 2, 1, c[1], c[2], c[3], 1 - (top - iy)) end end local function kap(s) return 0.55 * sin(0.07 * s + 1.2) + 0.35 * sin(0.031 * s + 3.3) + 0.22 * sin(0.17 * s + 0.4) end local function heading(s) return -(7.86 * cos(0.07 * s + 1.2) + 11.29 * cos(0.031 * s + 3.3) + 1.29 * cos(0.17 * s + 0.4)) end local OFF = {} local function offat(rf) local i = floor(rf) if i < 1 then return OFF[1] end if i >= ROWS then return OFF[ROWS] end return OFF[i] + (OFF[i + 1] - OFF[i]) * (rf - i) end local WX, WY, WRX, WRY = 32, 60, 22, 14 local OVL = nil local function disc(x, y, r, cr, cg, cb) for j = -r, r do local w = floor(sqrt(r * r - j * j) + 0.5) rect(x - w, y + j, 2 * w + 1, 1, cr, cg, cb) end end local function bake_cockpit() gfx.clear(KEY, 0, KEY) rect(0, 0, W, 3, 30, 32, 40) rect(31, 3, 2, 2, 30, 32, 40) rect(25, 5, 14, 6, 26, 28, 36) rect(26, 6, 12, 4, 126, 156, 188) rect(26, 6, 12, 1, 176, 204, 228) for y = 0, DASH - 1 do local w = floor(8.5 - y * 0.12) rect(0, y, w, 1, 38, 40, 50) rect(W - w, y, w, 1, 38, 40, 50) px(w, y, 92, 96, 110) px(W - w - 1, y, 92, 96, 110) end rect(0, DASH, W, H - DASH, 36, 38, 48) rect(0, DASH, W, 1, 112, 116, 132) rect(0, DASH + 1, W, 1, 64, 68, 82) for _, x in ipairs({ 2, 53 }) do rect(x, 50, 9, 4, 24, 26, 32) rect(x + 1, 51, 7, 1, 66, 70, 84) end rect(25, 48, 14, 8, 70, 74, 88) rect(26, 49, 12, 6, 8, 10, 16) px(22, 52, 70, 220, 110); px(42, 52, 240, 180, 40); px(22, 54, 230, 60, 50) for a = 0, 357, 3 do local r = a * 0.0174533 local f = 0.5 - 0.5 * sin(r) rect(floor(WX + WRX * cos(r) + 0.5) - 1, floor(WY + WRY * sin(r) + 0.5) - 1, 3, 3, floor(58 + 92 * f), floor(60 + 92 * f), floor(70 + 96 * f)) end disc(WX, WY, 5, 56, 58, 70) disc(WX, WY, 3, 38, 40, 50) px(WX, WY, 190, 194, 206); px(WX - 1, WY, 130, 134, 148); px(WX + 1, WY, 130, 134, 148) OVL = snapshot(0, 0, W, H) end local function rim_dot(a, c1, c2, c3) rect(floor(WX + WRX * cos(a) + 0.5) - 1, floor(WY + WRY * sin(a) + 0.5) - 1, 3, 3, c1, c2, c3) end local function wheel(deg) local a = deg * 0.0174533 for k = 0, 2 do local ak = a + (k == 0 and 3.14159 or (k == 1 and 0 or 1.5708)) local ca, sa = cos(ak), sin(ak) for i = 0, 5 do local t = 0.3 + i * 0.12 local d = i < 3 and 3 or 2 rect(floor(WX + ca * WRX * t + 0.5) - 1, floor(WY + sa * WRY * t + 0.5) - 1, d, d, 124, 128, 142) end end for _, g in ipairs({ 0, 3.14159 }) do for d = -0.2, 0.2, 0.2 do rim_dot(a + g + d, 30, 32, 40) end end for d = -0.03, 0.03, 0.06 do rim_dot(a - 1.5708 + d, 236, 196, 40) end end local KMH, KMH_S = 0, "" local function readout(v) v = floor(v) if v ~= KMH then KMH, KMH_S = v, tostring(v) end gfx.text(28, 50, KMH_S, 110, 240, 140, { font = "3x5" }) end local function bake_sky() local a, b = TM.skyT, TM.skyH for y = 0, HZ do local t = y / HZ rect(0, y, W, 1, floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5)) end if TM == TIMES.night then for i = 1, 22 do local h = hash32(i * 0x85EBCA6B) px(umod(h, W), umod(h >> 8, 14), 150, 160, 220) end end SKY = snapshot(0, 0, W, HZ + 1) end local function wall(side, id, s) local z0 = id * BAY - s if z0 + BAY < 1.4 or z0 > 34 or id % IEV == 6 then return end local zn = max(z0, 0.8) local rn, rf = ZK / zn, ZK / (z0 + BAY) local h = hash32(id * 0x85EBCA6B + (side > 0 and 99 or 0)) local hb = 4.5 + umod(h, 100) / 100 * 4.5 local F = TM.F[umod(h >> 8, 4) + 1] local sk = TM.skyH local fog = min(1, zn / 34) * 0.6 local sf = side < 0 and 1 or 0.86 local fr = floor((F[1] + (sk[1] - F[1]) * fog) * sf) local fg = floor((F[2] + (sk[2] - F[2]) * fog) * sf) local fb = floor((F[3] + (sk[3] - F[3]) * fog) * sf) local xn = 32 + offat(rn) + side * LW * rn local xf = 32 + offat(rf) + side * LW * rf local ytn, ytf = HZ - (hb - 1) * rn, HZ - (hb - 1) * rf for x = max(0, ceil(min(xn, xf))), min(W - 1, floor(max(xn, xf))) do local u = (x - xf) / (xn - xf) local yt = floor(ytf + (ytn - ytf) * u) if yt < 0 then yt = 0 end local yb = min(DASH, floor(HZ + rf + (rn - rf) * u) + 3) rect(x, yt, 1, yb - yt, fr, fg, fb) end if z0 > 18 then return end local nf = floor((hb - 0.7) / 1.3) local lit, bits = TM.lit, hash32(id * 0x2545F491 + side) for j = 0, 1 do local zA, zB = max(z0 + BAY * (0.12 + 0.46 * j), 0.8), z0 + BAY * (0.42 + 0.46 * j) if zB > 0.9 then local rA, rB = ZK / zA, ZK / zB local rm = (rA + rB) * 0.5 local ox = 32 + offat(rm) local x1, x2 = ox + side * LW * rA, ox + side * LW * rB local xl = floor(min(x1, x2) + 0.5) local w = max(1, floor(abs(x1 - x2) + 0.5)) if xl + w > 0 and xl < W then for k = 0, nf - 1 do local y1, y2 = floor(HZ - (0.8 + 1.3 * k + 0.8 - 1) * rm + 0.5), floor(HZ - (0.8 + 1.3 * k - 1) * rm + 0.5) if y2 > 0 and y1 < DASH then local n = j * 6 + k if (bits >> (2 * n)) & 3 < lit then if (bits >> n) & 1 == 0 then rect(xl, y1, w, max(1, y2 - y1), 255, 214, 120) else rect(xl, y1, w, max(1, y2 - y1), 255, 236, 170) end elseif z0 < 9 then rect(xl, y1, w, max(1, y2 - y1), fr * 6 // 10, fg * 6 // 10, fb * 7 // 10) end end end end end end end local LG = 9 local function lamps(s) local k0 = floor(s / LG) for i = floor(32 / LG) + 1, 0, -1 do local id = k0 + i local z = id * LG - s if z > 2.5 and z < 30 then local r = ZK / z local side = id % 2 == 0 and -1 or 1 local x = floor(32 + offat(r) + side * 1.55 * r + 0.5) local yb = HZ + floor(r + 0.5) local yt = floor(HZ - 2 * r + 0.5) local ax = floor(x - side * 0.5 * r) rect(x, yt, r > 8 and 2 or 1, yb - yt, 30, 30, 40) rect(min(x, ax), yt, abs(ax - x) + 1, 1, 30, 30, 40) local g = max(1, floor(r * 0.22)) if r > 4 then blend_rect(ax - 2 * g, yt - g - 1, 4 * g + 1, 3 * g, 255, 200, 110, TM.glow * 0.5) blend_rect(ax - g, yt - g // 2 - 1, 2 * g + 1, 2 * g, 255, 210, 130, TM.glow) end rect(ax - 1, yt, 3, 2, 255, 238, 190) end end end local LIGHT = { { 255, 60, 50 }, { 255, 190, 40 }, { 70, 230, 110 } } local function signals(s, t) local k0 = floor(s / BAY) for i = floor(32 / BAY) + 1, 0, -1 do local id = k0 + i if id % IEV == 6 then local z = id * BAY - s + 0.3 if z > 2.2 and z < 30 then local r = ZK / z local x = floor(32 + offat(r) + 1.7 * r + 0.5) local yb = HZ + floor(r + 0.5) local yt = floor(HZ - 1.6 * r + 0.5) rect(x, yt, 1, yb - yt, 30, 30, 40) local bw, bh = max(2, floor(r * 0.22)), max(6, floor(r * 0.6)) rect(x - bw // 2, yt, bw, bh, 20, 22, 28) local cyc = (t + id * 3.1) % 9 local on = cyc < 5 and 3 or (cyc < 6.5 and 2 or 1) local ch = bh // 3 for q = 1, 3 do local c = LIGHT[q] if q == on then rect(x - bw // 2, yt + (q - 1) * ch, bw, ch, c[1], c[2], c[3]) blend_rect(x - bw // 2 - 2, yt + (q - 1) * ch - 1, bw + 4, ch + 2, c[1], c[2], c[3], 0.3) else rect(x - bw // 2, yt + (q - 1) * ch, bw, ch, c[1] // 5, c[2] // 5, c[3] // 5) end end end end end end local CARS = { { 12, -0.55, -0.15 }, { 26, -0.55, -0.15 }, { 20, 0.5, 0.12 } } local function cars(vel) for i = 1, #CARS do local c = CARS[i] local z = 2 + (c[1] + (c[3] - vel) * frame - 2) % 30 local r = ZK / z if r < 20 then local x = floor(32 + offat(r) + c[2] * r + 0.5) local y = HZ + floor(r + 0.5) local bw, bh = max(2, floor(r * 0.7)), max(2, floor(r * 0.45)) rect(x - bw // 2, y - bh, bw, bh, 44, 46, 72) rect(x - bw // 2 + 1, y - bh, max(1, bw - 2), 1, 70, 72, 104) local lr, lg, lb = 255, 246, 210 if c[3] > 0 then lr, lg, lb = 255, 50, 40 end local lw = max(1, bw // 3) rect(x - bw // 2, y - bh + bh // 3, lw, max(1, lw // 2 + 1), lr, lg, lb) rect(x + bw // 2 - lw, y - bh + bh // 3, lw, max(1, lw // 2 + 1), lr, lg, lb) blend_rect(x - bw // 2 - 1, y - bh + bh // 3 - 1, bw + 2, 3, lr, lg, lb, 0.25) end end end local VEL = { 0.16, 0.22, 0.28, 0.36, 0.46 } function render() if not SKY then bake_sky() end if not OVL then bake_cockpit() end local vel = VEL[cfg.speed or 3] local s = frame * vel local t = frame / FPS blit_rgb(SKY, 0, 0, W, HZ + 1) for r = 1, ROWS do local c = SD[r]; rect(0, HZ + r, W, 1, c[1], c[2], c[3]) end hills(heading(s) * 0.8, TM.line) local dx, xo = 0, 0 for r = ROWS, 1, -1 do dx = dx + kap(s + ZK / r) * C1 xo = xo + dx OFF[r] = xo end local k0 = floor(s / BAY) for id = k0 + floor(36 / BAY) + 1, k0, -1 do wall(-1, id, s) wall(1, id, s) end for r = 1, ROWS do local y = HZ + r local zr = ZK / r local c, sd = RD[r], SD[r] local cx = 32 + OFF[r] local hw = r * 0.95 + 0.5 local xl, xr = floor(cx - hw + 0.5), floor(cx + hw + 0.5) local ph = (zr + s) % BAY if r >= 4 and floor((zr + s) / BAY) % IEV == 6 then rect(0, y, W, 1, c[1], c[2], c[3]) if ph < 0.8 and r >= 5 then for q = 0, 5 do rect(floor(cx + (-0.85 + 0.3 * q) * r + 0.5), y, max(1, floor(0.15 * r + 0.5)), 1, 220, 218, 206) end end else local sl, sr = floor(cx - LW * r + 0.5), floor(cx + LW * r + 0.5) rect(sl, y, sr - sl, 1, sd[1], sd[2], sd[3]) rect(xl, y, xr - xl, 1, c[1], c[2], c[3]) if r >= 3 and (zr + s) % 8 < 3.5 then local dw = max(1, floor(r * 0.08 + 0.5)) rect(floor(cx - dw * 0.5 + 0.5), y, dw, 1, 224, 220, 200) end end end lamps(s) signals(s, t) if cfg.traffic ~= false then cars(vel) end blit_rgb(OVL, 0, 0, W, H, KEY, 0, KEY) local deg = 75 * kap(s + 3) + 2 * sin(s * 0.55) wheel(max(-105, min(105, deg))) readout(20 + 6 * (cfg.speed or 3) + 2 * sin(t * 0.9)) end