info = { type = "image", title = "Umbrella crossing", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Umbrellas cross a wet zebra seen from above", api = 33, } local tr = ({ sk = { ["Rain"] = "Dážď" }, cs = { ["Rain"] = "Déšť" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rain", label = T("Rain"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_indexed = gfx.blit_indexed local floor, sqrt, atan, min, max = math.floor, math.sqrt, math.atan, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CYC = 660 local ROAD, PAVE, JOINT, CURB = { 88, 94, 110 }, { 146, 140, 132 }, { 126, 120, 114 }, { 178, 172, 164 } local ZEBRA, DASH, PUDDLE, DRAIN = { 206, 210, 214 }, { 196, 166, 60 }, { 108, 120, 146 }, { 66, 70, 82 } local function c3(c) return c[1], c[2], c[3] end local BG local function puddle(x, y, w, h) rect(x + 1, y, w - 2, 1, c3(PUDDLE)); rect(x, y + 1, w, h - 2, c3(PUDDLE)); rect(x + 1, y + h - 1, w - 2, 1, c3(PUDDLE)) end local function build_bg() rect(0, 0, W, H, c3(ROAD)) for _, y0 in ipairs({ 0, 50 }) do rect(0, y0, W, 13 + (y0 == 50 and 1 or 0), c3(PAVE)) rect(0, y0 + 6, W, 1, c3(JOINT)) for x = 7 + (y0 == 50 and 4 or 0), W - 1, 9 do rect(x, y0, 1, 13, c3(JOINT)) end end rect(0, 13, W, 1, c3(CURB)); rect(0, 49, W, 1, c3(CURB)) rect(10, 11, 6, 2, c3(DRAIN)); rect(40, 51, 6, 2, c3(DRAIN)) for x = 0, 28, 12 do rect(x, 30, 6, 2, c3(DASH)) end for x = 57, W - 1, 12 do rect(x, 30, 6, 2, c3(DASH)) end for i = 0, 5 do rect(31, 15 + i * 6, 24, 3, c3(ZEBRA)) end rect(29, 35, 1, 10, c3(ZEBRA)); rect(56, 17, 1, 10, c3(ZEBRA)) puddle(4, 18, 15, 5); puddle(6, 44, 14, 4); puddle(58, 39, 6, 4) BG = gfx.snapshot(0, 0, W, H) end local RIP_N = { 8, 14, 20, 28, 36 } local RIP_P, RIP_O = {}, {} for i = 1, 36 do RIP_P[i] = 20 + (i * 7) % 13 RIP_O[i] = umod(hash32(i * 0x85EBCA6B), RIP_P[i]) end local function ring(x, y, r, a) blend_rect(x - r + 1, y - r, 2 * r - 1, 1, 200, 214, 232, a) blend_rect(x - r + 1, y + r, 2 * r - 1, 1, 200, 214, 232, a) blend_rect(x - r, y - r + 1, 1, 2 * r - 1, 200, 214, 232, a) blend_rect(x + r, y - r + 1, 1, 2 * r - 1, 200, 214, 232, a) end local function ripples() for i = 1, RIP_N[cfg.rain or 3] do local P = RIP_P[i] local u = frame + RIP_O[i] local s = u % P if s < 12 then local h = hash32((u // P) * 0x9E3779B1 + i * 0x7F4A7C15) local x, y = umod(h, W), umod(h >> 9, H) if s < 2 then blend_rect(x, y, 1, 1, 200, 214, 232, 0.7) elseif s < 6 then ring(x, y, 1, 0.6) elseif s < 9 then ring(x, y, 2, 0.4) else ring(x, y, 3, 0.22) end end end end local function car_grid(flip) local t = {} for y = 0, 7 do for x = 0, 13 do local u = flip and 13 - x or x local v = 1 if (u == 0 or u == 13) and (y == 0 or y == 7) then v = 0 elseif u == 13 and (y == 1 or y == 6) then v = 4 elseif u == 0 and (y == 1 or y == 2 or y == 5 or y == 6) then v = 5 elseif u >= 9 and u <= 10 and y >= 1 and y <= 6 then v = 3 elseif u >= 2 and u <= 3 and y >= 2 and y <= 5 then v = 3 elseif u >= 4 and u <= 8 and y >= 1 and y <= 6 then v = 2 end t[#t + 1] = string.char(v) end end return table.concat(t) end local GRID = { [false] = car_grid(false), [true] = car_grid(true) } local CAR = { { 214, 218, 224 }, { 58, 62, 76 }, { 150, 48, 56 }, { 70, 110, 160 }, { 112, 152, 122 } } local CPAL = {} for i, c in ipairs(CAR) do local function pal(tr, tg, tb) return { 0, 0, 0, c[1], c[2], c[3], min(255, c[1] * 11 // 10 + 18), min(255, c[2] * 11 // 10 + 18), min(255, c[3] * 11 // 10 + 18), 34, 42, 58, 255, 238, 170, tr, tg, tb } end CPAL[i] = { pal(236, 40, 40), pal(110, 28, 30) } end local EAST_Y, WEST_Y = 36, 18 local STOP_E, STOP_W = 28, 58 local ACC = 26 local GO_E, GO_W, ADV_E, ADV_W, ADV_T = 505, 517, 515, 527, 26 local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function carcol(j, lane) return umod(hash32(j * 0x9E3779B1 + lane * 0x85EBCA6B), 5) + 1 end local function cars(n, p) for s = 3, 0, -1 do local j = n + s local front, brake if s == 0 then if p < GO_E then front, brake = STOP_E, true else local dt = (p - GO_E) / FPS; front, brake = STOP_E + 0.5 * ACC * dt * dt, false end else local a = smooth((p - ADV_E) / ADV_T) front, brake = STOP_E - 15 * s + 15 * a, a <= 0 or a >= 1 end local x = floor(front + 0.5) - 13 if x < W and x > -14 then blit_indexed(GRID[false], CPAL[carcol(j, 0)][brake and 1 or 2], x, EAST_Y, 14, 8, 14, 8, true) end if s == 0 then if p < GO_W then front, brake = STOP_W, true else local dt = (p - GO_W) / FPS; front, brake = STOP_W - 0.5 * ACC * dt * dt, false end else local a = smooth((p - ADV_W) / ADV_T) front, brake = STOP_W + 15 * s - 15 * a, a <= 0 or a >= 1 end x = floor(front + 0.5) if x < W and x > -14 then blit_indexed(GRID[true], CPAL[carcol(j, 1)][brake and 1 or 2], x, WEST_Y, 14, 8, 14, 8, true) end end end local UCOL = { { { 226, 72, 62 }, { 246, 128, 104 } }, { { 232, 176, 40 }, { 248, 214, 110 } }, { { 28, 150, 146 }, { 88, 196, 186 } }, { { 136, 58, 140 }, { 192, 118, 190 } }, { { 236, 232, 222 }, { 186, 182, 172 } }, { { 50, 84, 190 }, { 112, 150, 226 } }, } local UPAL = {} for i, c in ipairs(UCOL) do local a, b = c[1], c[2] UPAL[i] = { 0, 0, 0, a[1], a[2], a[3], b[1], b[2], b[3], a[1] * 6 // 10, a[2] * 6 // 10, a[3] * 6 // 10, 250, 246, 230, a[1] * 3 // 4, a[2] * 3 // 4, a[3] * 3 // 4 } end local UGRID, UREST do local t = {} local pi = math.pi for y = 0, 10 do for x = 0, 10 do local dx, dy = x - 5, y - 5 local d2 = dx * dx + dy * dy local v = 0 if d2 <= 28 then local a = (atan(dy, dx) + pi) / (pi / 4) local r = sqrt(d2) local f = a % 1 v = (floor(a) % 2 == 0) and 1 or 2 if d2 > 20.5 then v = 5 elseif r > 1.2 and min(f, 1 - f) * (pi / 4) * r < 0.55 then v = 3 end if r < 1.3 then v = 4 end end t[#t + 1] = string.char(v) end end UGRID = table.concat(t) UREST = UGRID:sub(12) end local LANE_X = { 32, 43 } local STARTS = { { 0, 92, 185 }, { 30, 125, 225 } } local function umbrellas(n, p) for lane = 1, 2 do for k = 1, 3 do local h = hash32((n * 11 + lane * 3 + k) * 0x9E3779B1) local v = (10 + umod(h >> 10, 3)) / FPS local d = v * (p - STARTS[lane][k]) local yf = lane == 1 and -12 + d or 63 - d if yf > -12 and yf < H then local ci = umod(h >> 14, 6) + 1 local x = LANE_X[lane] + umod(h >> 6, 3) - 1 local y0 = floor(yf) local f = yf - y0 local pal = UPAL[ci] blit_indexed(UREST, pal, x, y0 + 1, 11, 10, 11, 10, true) blend_rect(x + 4, y0, 3, 1, pal[16], pal[17], pal[18], 1 - f) blend_rect(x + 4, y0 + 11, 3, 1, pal[16], pal[17], pal[18], f) end end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local n, p = frame // CYC, frame % CYC ripples() cars(n, p) umbrellas(n, p) end