info = { type = "image", title = "Iso traffic", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Isometric city with traffic lights and walkers", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Cars per lane"] = "Áut na pruh", ["Walkers"] = "Chodci" }, cs = { ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Cars per lane"] = "Aut na pruh", ["Walkers"] = "Chodci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "dusk", "night" }, T("Day"), T("Dusk"), T("Night") } }, { key = "cars", label = T("Cars per lane"), type = "int", min = 1, max = 2, default = 2 }, { key = "walkers", label = T("Walkers"), type = "int", min = 0, max = 3, default = 3 }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, abs, min, max = math.floor, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H = gfx.W, gfx.H local OX, OY = 32, 26 local function P(gx, gy) return OX + (gx - gy) * 4, OY + (gx + gy) * 2 end local MODES = { day = { s1 = { 120, 190, 244 }, s2 = { 206, 234, 250 }, tint = { 1, 1, 1 }, lit = false }, dusk = { s1 = { 92, 100, 176 }, s2 = { 255, 184, 128 }, tint = { 1, 0.88, 0.8 }, lit = true }, night = { s1 = { 46, 54, 116 }, s2 = { 46, 54, 116 }, tint = { 0.58, 0.64, 0.86 }, lit = true }, } local M = MODES.day local function tint(c, k) k = k or 1 return { min(255, floor(c[1] * k * M.tint[1] + 0.5)), min(255, floor(c[2] * k * M.tint[2] + 0.5)), min(255, floor(c[3] * k * M.tint[3] + 0.5)) } end local function R(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local function rhomb(gx, gy, w, d, lift, c) local tx, ty = P(gx, gy) ty = ty - lift for k = 0, 2 * (w + d) - 1 do local xl = tx - 4 * d + abs(2 * k + 1 - 4 * d) local xr = tx + 4 * w - abs(2 * k + 1 - 4 * w) if xr > xl then R(xl, ty + k, xr - xl, 1, c) end end end local function box(gx, gy, w, d, h, top, left, right, sh) local tx, ty = P(gx, gy) ty = ty + (sh or 0) for x = tx - 4 * d + 1, tx + 4 * (w - d) - 1 do R(x, ty + (x - tx + 8 * d - 1) // 2 - h + 1, 1, h, left) end for x = tx + 4 * (w - d), tx + 4 * w - 2 do R(x, ty + (tx + 8 * w - 2 - x) // 2 - h + 1, 1, h, right) end rhomb(gx, gy, w, d, h - (sh or 0), top) end local DYN = {} local LIT = { 255, 222, 120 } local function windows(gx, gy, w, d, h, wall, lit) if h < 8 then return end local tx, ty = P(gx, gy) local glass = tint({ 70, 100, 150 }) local dark = tint(wall, 0.5) local function win(x, y) local v = umod(hash32(x * 131 + y * 7), 100) if M.lit and v < 55 then R(x, y, 1, 2, lit) elseif M.lit then R(x, y, 1, 2, dark) if v < 72 and #DYN < 14 then DYN[#DYN + 1] = { x = x, y = y, per = 60 + v * 4, ph = v } end else R(x, y, 1, 2, glass) end end for x = tx - 4 * d + 3, tx + 4 * (w - d) - 2, 3 do local kb = (x - tx + 8 * d - 1) // 2 for y = ty + kb - 3, ty + kb - h + 3, -4 do win(x, y) end end for x = tx + 4 * (w - d) + 1, tx + 4 * w - 3, 3 do local kb = (tx + 8 * w - 2 - x) // 2 for y = ty + kb - 3, ty + kb - h + 3, -4 do win(x, y) end end end local BASES = { A = { 156, 176, 214 }, B = { 226, 172, 122 }, C = { 204, 112, 102 }, D = { 232, 216, 164 }, E = { 132, 194, 174 }, F = { 192, 162, 204 }, G = { 242, 202, 92 }, } local function building(gx, gy, w, d, h, key) local b = BASES[key] box(gx, gy, w, d, h, tint(b, 1.14), tint(b, 1), tint(b, 0.74)) windows(gx, gy, w, d, h, b, tint({ 255, 222, 120 })) if h >= 12 then local cx, cy = P(gx + w / 2, gy + d / 2) R(floor(cx) - 1, floor(cy) - h - 1, 3, 2, tint(b, 0.66)) end end local function tree(gx, gy) local x, y = P(gx, gy) x, y = floor(x), floor(y) R(x, y - 2, 1, 3, tint({ 96, 66, 40 })) R(x - 1, y - 5, 3, 3, tint({ 62, 142, 70 })) R(x - 2, y - 4, 5, 1, tint({ 62, 142, 70 })) R(x, y - 5, 1, 1, tint({ 96, 178, 100 })) end local ROAD, MARK, ZEBRA local function bake_ground() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(M.s1[1] + (M.s2[1] - M.s1[1]) * t + 0.5), floor(M.s1[2] + (M.s2[2] - M.s1[2]) * t + 0.5), floor(M.s1[3] + (M.s2[3] - M.s1[3]) * t + 0.5)) end if M.lit then for i = 1, 22 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), umod(h >> 8, 30), 226, 228, 255) end end box(0, 0, 7, 7, 4, tint({ 112, 176, 92 }), tint({ 150, 132, 112 }), tint({ 118, 100, 84 }), 4) ROAD, MARK, ZEBRA = tint({ 86, 90, 100 }), tint({ 236, 226, 150 }), tint({ 214, 214, 222 }) rhomb(0, 3, 7, 1, 0, ROAD) rhomb(3, 0, 1, 7, 0, ROAD) for _, g in ipairs({ 0.5, 1.5, 5.5, 6.5 }) do local x, y = P(g, 3.5); R(floor(x) - 1, floor(y), 2, 1, MARK) x, y = P(3.5, g); R(floor(x) - 1, floor(y), 2, 1, MARK) end for _, g in ipairs({ 2, 4 }) do for _, u in ipairs({ 0.3, 0.7 }) do local x, y = P(g + u, 3.05) for k = 0, 3 do R(floor(x) - 2 * k - 1, floor(y) + k, 2, 1, ZEBRA) end x, y = P(3.05, g + u) for k = 0, 3 do R(floor(x) + 2 * k, floor(y) + k, 2, 1, ZEBRA) end end end building(0, 0, 2, 2, 22, "A"); building(2, 0, 1, 2, 12, "B") end local KEY = { 255, 0, 255 } local function bake_front() gfx.clear(KEY[1], KEY[2], KEY[3]) building(4, 0, 1, 2, 5, "E"); building(5, 0, 2, 2, 12, "F") building(0, 5, 1, 2, 6, "C"); building(1, 5, 1, 2, 9, "D") building(5, 5, 1, 2, 4, "G") for _, t in ipairs({ { 0.5, 2.3 }, { 1.7, 2.25 }, { 4.6, 2.3 }, { 6.3, 2.2 } }) do tree(t[1], t[2]) end end local KEYS = ".brkh" local MAP = {} for i = 1, #KEYS do MAP[KEYS:sub(i, i)] = char(i - 1) end local function sprite(rows) local w = #rows[1] return { s = (concat(rows):gsub(".", MAP)), w = w, h = #rows } end local function flip(rows) local o = {} for i = 1, #rows do o[i] = rows[i]:reverse() end return o end local SE = { "bbb....", "brrbb..", ".bbrkbh", "...bbb." } local NW = { "hbb....", "bkrbb..", ".bbrrbb", "...bbb." } local CAR = { sprite(SE), sprite(NW), sprite(flip(SE)), sprite(flip(NW)) } local CARCOL = { { 214, 50, 56 }, { 244, 200, 50 }, { 56, 110, 220 }, { 240, 240, 244 }, { 60, 170, 96 }, { 240, 132, 40 } } local function carpal(c) local function up(v) return min(255, floor(v * 1.25 + 24)) end return { 0, 0, 0, c[1], c[2], c[3], up(c[1]), up(c[2]), up(c[3]), 40, 48, 68, 255, 240, 150 } end local CARPAL = {} for i = 1, #CARCOL do CARPAL[i] = carpal(CARCOL[i]) end local LANES = { { f = function(s) return s, 3.7 end, spr = 1, axis = 1 }, { f = function(s) return 7 - s, 3.3 end, spr = 2, axis = 1 }, { f = function(s) return 3.3, s end, spr = 3, axis = 2 }, { f = function(s) return 3.7, 7 - s end, spr = 4, axis = 2 }, } local STOP, GAP, VCAR = 1.6, 1.25, 0.1 local CARS = {} local function lightX(p) return p < 240 and 1 or (p < 270 and 2 or 3) end local function lightY(p) return (p >= 300 and p < 540) and 1 or ((p >= 540 and p < 570) and 2 or 3) end local function go(axis, p) return (axis == 1 and lightX(p) or lightY(p)) == 1 end local WALK = { { pts = { { 0.4, 2.8 }, { 2.5, 2.8 }, { 2.5, 4.2 }, { 0.4, 4.2 } }, shirt = { 230, 70, 90 }, s = 0.6, ok = function(p) return p >= 300 and p < 480 end }, { pts = { { 2.8, 6.6 }, { 2.8, 4.5 }, { 4.2, 4.5 }, { 4.2, 6.6 } }, shirt = { 60, 130, 230 }, s = 1.5, ok = function(p) return p >= 10 and p < 215 end }, { pts = { { 6.6, 4.2 }, { 4.5, 4.2 }, { 4.5, 2.8 }, { 6.6, 2.8 } }, shirt = { 250, 190, 40 }, s = 3.2, ok = function(p) return p >= 300 and p < 480 end }, } for _, w in ipairs(WALK) do w.cum = { 0 } for i = 2, #w.pts do local a, b = w.pts[i - 1], w.pts[i] w.cum[i] = w.cum[i - 1] + math.sqrt((b[1] - a[1]) ^ 2 + (b[2] - a[2]) ^ 2) end w.L, w.dir, w.rest = w.cum[#w.cum], 1, 0 end local VWALK = 0.025 local function walker_pos(w) local s = w.s for i = 2, #w.pts do if s <= w.cum[i] or i == #w.pts then local a, b = w.pts[i - 1], w.pts[i] local u = (s - w.cum[i - 1]) / (w.cum[i] - w.cum[i - 1]) return a[1] + (b[1] - a[1]) * u, a[2] + (b[2] - a[2]) * u end end end local LAMP = { { 60, 220, 90 }, { 250, 210, 60 }, { 240, 60, 50 } } local POSTS = {} for i, q in ipairs({ { 2.8, 2.8, 1 }, { 4.2, 4.2, 1 }, { 4.2, 2.8, 2 }, { 2.8, 4.2, 2 } }) do local x, y = P(q[1], q[2]) POSTS[i] = { x = floor(x), y = floor(y), axis = q[3] } end local last = nil local function step(f) local p = f % 600 local n = cfg.cars or 2 for i = 1, #CARS do local c = CARS[i] if c.k <= n then local lane = LANES[c.lane] local nxt = c.s + VCAR local stop = c.s <= STOP and nxt > STOP and not go(lane.axis, p) if not stop then for j = 1, #CARS do local o = CARS[j] if o ~= c and o.lane == c.lane and o.k <= n and o.s > c.s and o.s - nxt < GAP then stop = true end end end if not stop then c.s = nxt if c.s > 8.5 then c.s = -1.5 end end end end for i = 1, cfg.walkers or 3 do local w = WALK[i] if w.rest > 0 then w.rest = w.rest - 1 else local nxt = w.s + w.dir * VWALK local s1, s2 = w.cum[2], w.cum[3] local hold = (w.dir > 0 and w.s < s1 and nxt >= s1 or w.dir < 0 and w.s > s2 and nxt <= s2) and not w.ok(p) if not hold then w.s = nxt end if w.s >= w.L then w.s, w.dir, w.rest = w.L, -1, 40 elseif w.s <= 0 then w.s, w.dir, w.rest = 0, 1, 40 end end end end local function splat(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy for dy = 0, 1 do for dx = 0, 1 do local a = (dx == 1 and fx or 1 - fx) * (dy == 1 and fy or 1 - fy) if a > 0.05 then blend(ix + dx, iy + dy, r, g, b, a) end end end end local BG, FRONT = nil, nil local STAGE = 0 function on_settings() M = MODES[cfg.time] or MODES.day BG, FRONT, STAGE, DYN = nil, nil, 0, {} LIT = tint({ 255, 222, 120 }) CARS = {} for lane = 1, 4 do for k = 1, 2 do CARS[#CARS + 1] = { lane = lane, k = k, s = -1.5 + (k - 1) * 4.6 + lane * 0.9, col = (lane * 2 + k) % #CARCOL + 1 } end end last = nil end function render() if STAGE == 0 then bake_ground(); BG = snapshot(0, 0, W, H); STAGE = 1 gfx.ready(false) return elseif STAGE == 1 then bake_front(); FRONT = snapshot(0, 0, W, H); STAGE = 2 end blit_rgb(BG, 0, 0, W, H) if not last then last = frame - 1 end for f = max(last + 1, frame - 39), frame do step(f) end last = frame local n = cfg.cars or 2 for i = 1, #CARS do local c = CARS[i] if c.k <= n then local lane = LANES[c.lane] local gx, gy = lane.f(c.s) local x, y = P(gx, gy) local sp = CAR[lane.spr] blit_indexed(sp.s, CARPAL[c.col], floor(x + 0.5) - 3, floor(y + 0.5) - 3, sp.w, sp.h, sp.w, sp.h, true) end end blit_rgb(FRONT, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) local p = frame % 600 if M.lit then local lc = LIT for i = 1, #DYN do local d = DYN[i] if ((frame // d.per + d.ph) % 2) == 1 then rect(d.x, d.y, 1, 2, lc[1], lc[2], lc[3]) end end end for i = 1, cfg.walkers or 3 do local w = WALK[i] local gx, gy = walker_pos(w) local x, y = P(gx, gy) splat(x, y - 2, 244, 206, 170) splat(x, y - 1, w.shirt[1], w.shirt[2], w.shirt[3]) end for i = 1, 4 do local q = POSTS[i] local c = LAMP[q.axis == 1 and lightX(p) or lightY(p)] rect(q.x, q.y - 5, 1, 5, 50, 52, 62) rect(q.x, q.y - 7, 2, 2, c[1], c[2], c[3]) end end