info = { type = "image", title = "Car at the traffic light", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Little car stops at a red light and drives off", api = 33, } local tr = ({ sk = { ["Car colour"] = "Farba auta", ["Red"] = "Červená", ["Blue"] = "Modrá", ["Yellow"] = "Žltá", ["Green"] = "Zelená", ["Purple"] = "Fialová" }, cs = { ["Car colour"] = "Barva auta", ["Red"] = "Červená", ["Blue"] = "Modrá", ["Yellow"] = "Žlutá", ["Green"] = "Zelená", ["Purple"] = "Fialová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "car", label = T("Car colour"), type = "int", default = "red", opts = { keys = { "red", "blue", "yellow", "green", "purple" }, T("Red"), T("Blue"), T("Yellow"), T("Green"), T("Purple") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, exp, cos, min, sqrt = math.floor, math.exp, math.cos, math.min, math.sqrt local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H = gfx.W, gfx.H local COLORS = { red = { 214, 32, 40 }, blue = { 40, 96, 220 }, yellow = { 240, 190, 20 }, green = { 40, 160, 70 }, purple = { 140, 56, 190 }, } local FIXED = { 120, 176, 230, 220, 240, 255, 200, 204, 214, 255, 244, 160, 190, 30, 30, 16, 16, 26, 76, 76, 92, 128, 130, 148, 240, 240, 250, 40, 40, 52 } local PAL function on_settings() local c = COLORS[cfg.car] or COLORS.red PAL = { 0, 0, 0, c[1], c[2], c[3], floor(c[1] + (255 - c[1]) * 0.45), floor(c[2] + (255 - c[2]) * 0.45), floor(c[3] + (255 - c[3]) * 0.45), floor(c[1] * 0.62), floor(c[2] * 0.62), floor(c[3] * 0.62) } for i = 1, #FIXED do PAL[#PAL + 1] = FIXED[i] end end local CW, CH = 26, 10 local SPAN = { { 8, 17 }, { 6, 19 }, { 5, 20 }, { 3, 22 }, { 1, 24 }, { 0, 25 }, { 0, 25 }, { 0, 25 }, { 0, 25 }, { 1, 24 } } local ROWC = { 2, 1, 1, 1, 2, 1, 1, 1, 3, 3 } local GLASS = { { 8, 12, 14, 17 }, { 7, 12, 14, 18 }, { 6, 12, 14, 19 } } local CAR, WHEEL = nil, {} local function bake_sprites() local g = {} for i = 1, CW * CH do g[i] = 0 end for y = 0, CH - 1 do for x = SPAN[y + 1][1], SPAN[y + 1][2] do g[y * CW + x + 1] = ROWC[y + 1] end end for y = 1, 3 do local gl = GLASS[y] for x = gl[1], gl[2] do g[y * CW + x + 1] = 4 end for x = gl[3], gl[4] do g[y * CW + x + 1] = 4 end g[y * CW + gl[1] + 1] = 5 g[y * CW + gl[3] + 1] = 5 end for y = 5, 8 do g[y * CW + 13 + 1] = 3 end g[5 * CW + 12 + 1] = 2 g[5 * CW + 26] = 7; g[6 * CW + 26] = 7; g[7 * CW + 26] = 6; g[8 * CW + 25] = 6; g[8 * CW + 26] = 6 g[5 * CW + 1] = 8; g[6 * CW + 1] = 8; g[8 * CW + 1] = 6; g[8 * CW + 2] = 6 for _, cx in ipairs({ 6, 19 }) do for y = 5, 9 do for x = cx - 4, cx + 4 do local dx, dy = x - cx, y - 8.5 if x >= 0 and x < CW and dx * dx + dy * dy < 13 and g[y * CW + x + 1] ~= 0 then g[y * CW + x + 1] = 9 end end end end local rows = {} for y = 0, CH - 1 do rows[y + 1] = char(table.unpack(g, y * CW + 1, y * CW + CW)) end CAR = concat(rows) local TW = { 3, 5, 7, 7, 7, 5, 3 } local MK = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 } } for p = 1, 8 do local w = {} for y = 0, 6 do for x = 0, 6 do local v = 0 if math.abs(x - 3) <= TW[y + 1] // 2 then v = 10 end if v == 10 and math.abs(x - 3) <= 1 and math.abs(y - 3) <= 1 then v = 11 elseif v == 10 and (math.abs(x - 3) <= 2 and math.abs(y - 3) <= 1 or math.abs(x - 3) <= 1 and math.abs(y - 3) <= 2) then v = 11 end w[y * 7 + x + 1] = v end end w[3 * 7 + 3 + 1] = 13 w[(3 + MK[p][2]) * 7 + 3 + MK[p][1] + 1] = 12 WHEEL[p] = char(table.unpack(w)) end end local BG local HOUSES = { { 0, 14, 14, { 236, 196, 150 }, { 170, 78, 60 } }, { 14, 12, 20, { 190, 112, 112 }, { 110, 66, 80 } }, { 26, 14, 12, { 130, 176, 160 }, { 82, 110, 130 } }, { 40, 12, 22, { 226, 214, 128 }, { 150, 90, 70 } }, { 52, 12, 16, { 152, 152, 206 }, { 96, 84, 140 } }, } local function bake_street() for y = 0, 40 do local t = y / 40 rect(0, y, W, 1, floor(110 + 80 * t), floor(170 + 54 * t), floor(236 + 12 * t)) end for _, c in ipairs({ { 6, 4, 18 }, { 36, 8, 14 } }) do rect(c[1] + 3, c[2], c[3] - 6, 1, 246, 250, 255); rect(c[1], c[2] + 1, c[3], 2, 246, 250, 255) rect(c[1] + 6, c[2] + 3, c[3] - 10, 1, 214, 230, 248) end for i, h in ipairs(HOUSES) do local x, w, top = h[1], h[2], h[3] rect(x, top, w, 41 - top, h[4][1], h[4][2], h[4][3]) rect(x, top - 3, w, 1, h[5][1], h[5][2], h[5][3]); rect(x + 1, top - 4, w - 2, 1, h[5][1], h[5][2], h[5][3]) rect(x + 2, top - 5, w - 4, 1, h[5][1], h[5][2], h[5][3]); rect(x, top - 2, w, 2, h[5][1] * 3 // 4, h[5][2] * 3 // 4, h[5][3] * 3 // 4) rect(x + w - 1, top, 1, 41 - top, h[4][1] * 4 // 5, h[4][2] * 4 // 5, h[4][3] * 4 // 5) for wy = top + 3, 34, 8 do for wx = x + 2, x + w - 6, 6 do local lit = umod(hash32(wx * 31 + wy * 7 + i), 3) == 0 rect(wx, wy, 3, 4, lit and 250 or 64, lit and 214 or 92, lit and 124 or 150) end end rect(x + w // 2 - 1, 35, 3, 6, 110, 70, 52) end rect(0, 41, W, 3, 196, 192, 182) for x = 3, W, 8 do rect(x, 41, 1, 3, 160, 156, 148) end rect(0, 44, W, 1, 120, 116, 112) rect(0, 45, W, H - 45, 78, 82, 98) rect(0, 45, W, 1, 96, 100, 116) for x = 2, W, 12 do rect(x, 60, 6, 1, 230, 210, 120) end rect(42, 46, 2, 12, 236, 236, 240) for x = 50, 61, 4 do rect(x, 46, 2, 12, 214, 214, 220) end rect(46, 22, 2, 22, 50, 54, 66) rect(43, 8, 8, 17, 30, 32, 40); rect(43, 8, 8, 1, 60, 64, 76) BG = snapshot(0, 0, W, H) end local CYC, V0, A, X0 = 330, 1.2, 0.025, -30 local TB, TS, TA = 13, 61, 124 local A2, VMAX = 0.03, 1.4 local XS = X0 + V0 * TB + V0 * (TS - TB) - 0.5 * A * (TS - TB) ^ 2 local TSAT = VMAX / A2 local function car_x(t) if t < TB then return X0 + V0 * t end if t < TS then local s = t - TB; return X0 + V0 * TB + V0 * s - 0.5 * A * s * s end if t < TA then return XS end local s = t - TA if s <= TSAT then return XS + 0.5 * A2 * s * s end return XS + 0.5 * A2 * TSAT * TSAT + VMAX * (s - TSAT) end local function body_bob(t) if t >= TS and t < TS + 30 then local s = t - TS return floor(1.4 * exp(-s / 6) * cos(0.55 * s) + 0.5) elseif t >= TA and t < TA + 24 then local s = t - TA return floor(-1.0 * exp(-s / 5) * cos(0.6 * s) + 0.5) end return 0 end local function lamp(y, on, r, g, b, dr, dg, db) if on then blend_rect(43, y - 1, 8, 6, r, g, b, 0.22) rect(45, y, 4, 4, r, g, b) px(45, y, 255, 255, 255) else rect(45, y, 4, 4, dr, dg, db) end end local PUFF = { 0, 6, 12 } function render() if not BG then bake_street() return end if not CAR then bake_sprites() blit_rgb(BG, 0, 0, W, H) return end blit_rgb(BG, 0, 0, W, H) local t = frame % CYC local red, yel = t < 106 or t >= 205, t >= 190 and t < 205 lamp(10, red, 240, 40, 40, 80, 20, 24) lamp(15, yel, 250, 210, 40, 86, 74, 24) lamp(20, not red and not yel, 50, 230, 90, 20, 70, 36) local x = car_x(t) local bx = floor(x + 0.5) for k = 1, 3 do local age = t - TA - PUFF[k] if age >= 0 and age < 20 then local ex = car_x(TA + PUFF[k]) blend_rect(floor(ex - 3 - age * 0.35), 52 - age // 6, 1 + age // 8, 1 + age // 8, 210, 212, 224, 0.4 * (1 - age / 20)) end end if bx > -28 and bx < W + 2 then local by = 45 + body_bob(t) local ph = floor((x - X0) / 2.4) % 8 + 1 blend_rect(bx + 1, 57, 24, 1, 20, 22, 32, 0.4) blit_indexed(CAR, PAL, bx, by, CW, CH, CW, CH, true) blit_indexed(WHEEL[ph], PAL, bx + 3, 50, 7, 7, 7, 7, true) blit_indexed(WHEEL[ph], PAL, bx + 16, 50, 7, 7, 7, 7, true) if t >= TB and t < TA then rect(bx, by + 5, 1, 2, 255, 70, 60) end if t >= TB and t < TA + 40 then blend_rect(bx + CW, by + 5, 3, 2, 255, 244, 160, 0.35) end end end