info = { type = "image", title = "Desert rally", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Vintage racers speed through a desert canyon", api = 33, } local tr = ({ sk = { ["Cars"] = "Autá", ["Speed"] = "Rýchlosť" }, cs = { ["Cars"] = "Auta", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cars", label = T("Cars"), type = "int", min = 1, max = 4, default = 4 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs = math.floor, math.sin, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local SKY = { 76, 158, 230 } local SKY_LO = { 150, 204, 240 } local SUN = { 255, 242, 170 } local HALO = { 206, 230, 246 } local FAR = { 190, 146, 160 } local MESA = { 212, 124, 80 } local MESA_D = { 166, 88, 62 } local MESA_L = { 236, 164, 108 } local SAND = { 234, 198, 132 } local SAND_D = { 198, 160, 104 } local ROAD = { 88, 86, 96 } local LINE = { 226, 226, 220 } local DASH = { 244, 200, 60 } local CHK = { 24, 24, 28 } local CACT = { 66, 142, 76 } local CACT_L = { 108, 180, 98 } local TIRE = { 40, 40, 48 } local HUB = { 206, 206, 214 } local GOG = { 150, 222, 250 } local SKIN = { 236, 176, 130 } local BAR = { 170, 170, 180 } local STREAK = { 244, 240, 228 } local WEED = { 172, 126, 72 } local function c3(c) return c[1], c[2], c[3] end local BG = nil local MESAS = { { -6, 18, 23 }, { 30, 42, 28 }, { 53, 58, 19 } } local function mesa(x1, x2, top) for x = x1 - 7, x2 + 7 do local d = x < x1 and x1 - x or (x > x2 and x - x2 or 0) local y = top + 2 * d if y < 40 then local c = x > x2 and MESA_D or MESA rect(x, y, 1, 40 - y, c3(c)) if y <= top + 3 then px(x, top + 3, c3(x > x2 and MESA or MESA_L)) end end end end local function cactus(x, base, h) rect(x, base - h, 3, h, c3(CACT)) rect(x, base - h, 1, h, c3(CACT_L)) rect(x - 3, base - h + 5, 3, 2, c3(CACT)); rect(x - 3, base - h + 2, 2, 3, c3(CACT)) rect(x + 3, base - h + 7, 3, 2, c3(CACT)); rect(x + 4, base - h + 4, 2, 3, c3(CACT)) px(x - 3, base - h + 2, c3(CACT_L)); px(x + 4, base - h + 4, c3(CACT_L)) end local function build_bg() rect(0, 0, W, H, c3(SKY)) rect(0, 18, W, 22, c3(SKY_LO)) for x = 0, W - 1, 2 do px(x, 17, c3(SKY_LO)) end for y = 1, 15 do for x = 43, 57 do local dx, dy = x - 50, y - 8 local d = sqrt(dx * dx + dy * dy) if d <= 4.5 then px(x, y, c3(SUN)) elseif d <= 6 then px(x, y, c3(HALO)) end end end for x = 0, W - 1 do local top = floor(31 + 2 * sin(x * 0.13) + 1.5 * sin(x * 0.31 + 2) + 0.5) rect(x, top, 1, 40 - top, c3(FAR)) end for i = 1, #MESAS do mesa(MESAS[i][1], MESAS[i][2], MESAS[i][3]) end rect(0, 40, W, 6, c3(SAND)) rect(0, 46, W, 1, c3(SAND_D)) cactus(9, 41, 13); cactus(57, 42, 11) px(20, 43, c3(SAND_D)); px(36, 42, c3(SAND_D)); px(47, 44, c3(SAND_D)); px(3, 44, c3(SAND_D)) rect(0, 47, W, 15, c3(ROAD)) rect(0, 48, W, 1, c3(LINE)); rect(0, 60, W, 1, c3(LINE)) for x = 2, W - 1, 10 do rect(x, 54, 5, 1, c3(DASH)) end rect(0, 62, W, 2, c3(SAND_D)) for y = 47, 61, 2 do for x = 44, 47, 2 do local c = ((x + y) // 2) % 2 == 0 and CHK or LINE rect(x, y, 2, 2, c3(c)) end end BG = gfx.snapshot(0, 0, W, H) end local CARS = { { { 222, 50, 44 }, { 160, 30, 30 }, { 250, 250, 250 }, { 250, 210, 40 }, { 250, 250, 250 }, nil, 0, 70 }, { { 40, 162, 96 }, { 24, 116, 68 }, { 250, 222, 60 }, { 232, 62, 52 }, { 250, 144, 40 }, nil, 1, 30 }, { { 250, 202, 40 }, { 196, 148, 24 }, { 50, 50, 70 }, { 52, 112, 232 }, { 236, 84, 164 }, "bar", 0, 170 }, { { 44, 152, 222 }, { 30, 106, 168 }, { 250, 250, 250 }, { 240, 240, 240 }, { 222, 40, 40 }, "wing", 1, 130 }, } local LANE_TOP = { [0] = 43, [1] = 50 } local SPAN = 200 local NC, VBASE = 4, 2.25 local SPOKE = { 0, -1, 1, 0, 0, 1, -1, 0 } local function wheel(x, y, ph) rect(x + 1, y, 3, 5, c3(TIRE)); rect(x, y + 1, 5, 3, c3(TIRE)) local k = ph * 2 + 1 px(x + 2, y + 2, c3(HUB)) px(x + 2 + SPOKE[k], y + 2 + SPOKE[k + 1], c3(HUB)) end local function car_x(i, t) local c = CARS[i] local v = VBASE * (c[7] == 1 and 1.18 or 1.0) return (t * v + c[8]) % SPAN - 22 + 3 * sin(t * 0.045 + i * 1.7) end local function draw_car(i, t) local c = CARS[i] local x = floor(car_x(i, t) + 0.5) if x < -22 or x >= W then return end local bump = ((t // 4 + i * 3) % 7 == 0) and -1 or 0 local y = LANE_TOP[c[7]] + bump local B, S, ST, HC, SC = c[1], c[2], c[3], c[4], c[5] for k = 0, 3 do px(x + 8 - k, y + 2 + ((t // 3 + k) % 2), c3(SC)) end rect(x + 9, y + 3, 3, 1, c3(SKIN)) rect(x + 10, y, 2, 1, c3(HC)); rect(x + 9, y + 1, 3, 2, c3(HC)) px(x + 11, y + 1, c3(GOG)) if c[6] == "bar" then rect(x + 7, y + 1, 1, 3, c3(BAR)); px(x + 8, y + 1, c3(BAR)) end if c[6] == "wing" then rect(x, y + 2, 1, 2, c3(ST)); rect(x - 1, y + 2, 3, 1, c3(ST)) end px(x + 13, y + 2, c3(GOG)); px(x + 13, y + 3, c3(GOG)) rect(x + 2, y + 4, 15, 1, c3(B)) rect(x + 1, y + 5, 19, 1, c3(B)) rect(x, y + 6, 20, 2, c3(B)) rect(x, y + 6, 20, 1, c3(ST)) rect(x + 1, y + 8, 18, 1, c3(S)) rect(x + 9, y + 5, 3, 3, c3(LINE)) px(x + 10, y + 6, c3(B)) px(x - 1, y + 7, c3(BAR)) local ph = (x // 4) % 4 wheel(x + 2, y + 6, ph) wheel(x + 14, y + 6, ph) end local function draw_streaks(i, t) local c = CARS[i] local x = floor(car_x(i, t) + 0.5) if x < -22 or x >= W + 8 then return end local y = LANE_TOP[c[7]] local k = (t + i) % 3 local r, g, b = c3(STREAK) blend_rect(x - 6 - k, y + 4, 4 + k, 1, r, g, b, 0.6) blend_rect(x - 4 - (2 - k), y + 7, 3 + (2 - k), 1, r, g, b, 0.6) end local function draw_weed(t) local a = (t + 100) % 300 if a >= 80 then return end local x = floor(66 - a * 1.1 + 0.5) local y = 41 - floor(abs(sin(a * 0.16)) * 3 + 0.5) local r, g, b = c3(WEED) rect(x + 1, y, 2, 1, r, g, b); rect(x + 1, y + 3, 2, 1, r, g, b) rect(x, y + 1, 1, 2, r, g, b); rect(x + 3, y + 1, 1, 2, r, g, b) if (a // 4) % 2 == 0 then px(x + 1, y + 1, r, g, b); px(x + 2, y + 2, r, g, b) else px(x + 2, y + 1, r, g, b); px(x + 1, y + 2, r, g, b) end end function on_settings() NC = cfg.cars or 4 VBASE = 1.2 + 0.35 * (cfg.speed or 3) 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_weed(t) for lane = 0, 1 do for i = 1, NC do if CARS[i][7] == lane then draw_streaks(i, t); draw_car(i, t) end end end end