info = { type = "image", title = "Rain crossing", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Umbrellas cross a wet zebra seen from above", api = 33, } local tr = ({ sk = { ["Walkers"] = "Chodci", ["Umbrellas"] = "Dáždniky", ["Mixed"] = "Pestré", ["Yellow"] = "Žlté", ["Red"] = "Červené", ["Blue"] = "Modré" }, cs = { ["Walkers"] = "Chodci", ["Umbrellas"] = "Deštníky", ["Mixed"] = "Pestré", ["Yellow"] = "Žluté", ["Red"] = "Červené", ["Blue"] = "Modré" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Walkers"), type = "int", min = 2, max = 12, default = 7 }, { key = "col", label = T("Umbrellas"), type = "int", default = 0, opts = { T("Mixed"), T("Yellow"), T("Red"), T("Blue") } }, } local rect, blend_rect, blend_mask, blit_rgb = gfx.rect, gfx.blend_rect, gfx.blend_mask, gfx.blit_rgb local floor, ceil, sqrt, sin, atan = math.floor, math.ceil, math.sqrt, math.sin, math.atan local hash32, schar, unpack = math.hash32, string.char, table.unpack local W, H = gfx.W, gfx.H local CYC, CAR0 = 420, 330 local ASPH, ASPH2 = { 52, 58, 78 }, { 74, 86, 116 } local WALK, TILE = { 94, 98, 116 }, { 78, 82, 100 } local ZEBRA = { 206, 212, 226 } local PUD = { { 17, 8, 6, 3 }, { 44, 54, 7, 3 }, { 24, 56, 4, 2 }, { 4, 30, 3, 2 }, { 58, 14, 3, 2 }, { 36, 11, 3, 2 } } local MIXED = { { 230, 58, 72 }, { 250, 200, 52 }, { 40, 172, 192 }, { 238, 110, 170 }, { 76, 112, 232 }, { 250, 140, 40 } } local ONE = { nil, { 250, 200, 52 }, { 230, 58, 72 }, { 76, 112, 232 } } local CARS = { { 176, 40, 52 }, { 50, 110, 172 }, { 214, 216, 224 }, { 232, 184, 44 } } local BG = nil local function ellipse(cx, cy, rx, ry, c) for y = floor(cy - ry), ceil(cy + ry) do local dy = (y + 0.5 - cy) / ry if dy * dy < 1 then local hw = rx * sqrt(1 - dy * dy) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c[1], c[2], c[3]) end end end end local function build_bg() rect(0, 0, W, H, ASPH[1], ASPH[2], ASPH[3]) rect(0, 0, 9, H, WALK[1], WALK[2], WALK[3]); rect(54, 0, 10, H, WALK[1], WALK[2], WALK[3]) for k = 0, 8 do rect(0, k * 8, 9, 1, TILE[1], TILE[2], TILE[3]); rect(54, k * 8, 10, 1, TILE[1], TILE[2], TILE[3]) end rect(4, 0, 1, H, TILE[1], TILE[2], TILE[3]); rect(59, 0, 1, H, TILE[1], TILE[2], TILE[3]) rect(9, 0, 1, H, 128, 134, 152); rect(53, 0, 1, H, 128, 134, 152) for y = 0, H - 1, 9 do rect(31, y, 2, 5, 176, 168, 112) end rect(31, 18, 2, 28, ASPH[1], ASPH[2], ASPH[3]) for k = 0, 6 do rect(11 + 6 * k, 20, 3, 24, ZEBRA[1], ZEBRA[2], ZEBRA[3]) end rect(10, 16, 21, 2, 190, 196, 210); rect(33, 46, 20, 2, 190, 196, 210) for _, p in ipairs(PUD) do ellipse(p[1], p[2], p[3], p[4], ASPH2) end blend_rect(20, 0, 2, H, 255, 160, 80, 0.07); blend_rect(41, 0, 2, H, 255, 160, 80, 0.07) blend_rect(15, 0, 1, H, 120, 170, 255, 0.06); blend_rect(48, 0, 1, H, 120, 170, 255, 0.06) BG = gfx.snapshot(0, 0, W, H) end local MASKS = {} local function clamp1(v) return v < 0 and 0 or (v > 1 and 1 or v) end local function build_masks(cls, q) local R = cls == 1 and 4.5 or 3.6 local S = ceil(R) * 2 + 2 local cx, cy = S / 2 + q / 4, S / 2 local full, alt, rim, knob = {}, {}, {}, {} for j = 0, S - 1 do local f, a, r, k = {}, {}, {}, {} for i = 0, S - 1 do local dx, dy = i + 0.5 - cx, j + 0.5 - cy local d = sqrt(dx * dx + dy * dy) local cov = clamp1(R + 0.5 - d) local sector = floor(atan(dy, dx) / 0.7854) % 2 f[i + 1] = floor(cov * 255) a[i + 1] = (sector == 1 and d > 0.9) and floor(cov * 255) or 0 r[i + 1] = floor(cov * clamp1(d - (R - 1)) * (dx + dy > 0 and 255 or 130)) k[i + 1] = floor(clamp1(1.4 - d) * 255) end full[j + 1], alt[j + 1], rim[j + 1], knob[j + 1] = schar(unpack(f)), schar(unpack(a)), schar(unpack(r)), schar(unpack(k)) end local m = { S = S, full = table.concat(full), alt = table.concat(alt), rim = table.concat(rim), knob = table.concat(knob) } MASKS[cls * 4 + q] = m return m end local built_now = false local function mask(cls, q) local m = MASKS[cls * 4 + q] if m then return m end if built_now then return nil end built_now = true return build_masks(cls, q) end local function draw_umbrella(x, yc, cls, c) local ix = floor(x) local q = floor((x - ix) * 4) local m = mask(cls, q) for k = 0, 3 do m = m or MASKS[cls * 4 + k] end if not m then return end local S = m.S local x0, y0 = ix - S // 2, yc - S // 2 local light = { floor(c[1] + (255 - c[1]) * 0.45), floor(c[2] + (255 - c[2]) * 0.45), floor(c[3] + (255 - c[3]) * 0.45) } blend_mask(m.full, 0, 0, 0, x0 + 1, y0 + 2, S, S, S, S, 0.32) blend_mask(m.full, c[1], c[2], c[3], x0, y0, S, S, S, S, 1) blend_mask(m.alt, light[1], light[2], light[3], x0, y0, S, S, S, S, 1) blend_mask(m.rim, 8, 10, 22, x0, y0, S, S, S, S, 0.45) blend_mask(m.knob, 240, 240, 250, x0, y0, S, S, S, S, 0.8) end local CUR = -1 local TS, TD, TX, TY, TDIR, TCOL, TCLS = {}, {}, {}, {}, {}, {}, {} local NT = 0 local CAR_DIR, CAR_COL = 1, 1 local function setup(n) NT = (cfg.n or 7) * 2 for i = 1, NT do local function h(a) return (hash32(n * 1009 + i * 31 + a * 0x9E3779B1) & 0x7FFF) / 32768 end local v = 0.34 + 0.36 * h(1) local d = floor(80 / v) TD[i] = d TS[i] = floor((i - 1 + h(2)) / NT * (325 - d)) TY[i] = 25 + (i * 7 + floor(h(3) * 3)) % 15 TDIR[i] = ((i + floor(h(4) * 2)) % 2 == 0) and 1 or -1 TCLS[i] = h(5) < 0.25 and 0 or 1 local mode = cfg.col or 0 TCOL[i] = mode == 0 and MIXED[1 + floor(h(6) * 6)] or ONE[mode + 1] end local hc = (hash32(n * 7 + 3) & 0x7FFF) / 32768 CAR_DIR, CAR_COL = hc < 0.5 and 1 or -1, CARS[1 + floor(hc * 8) % 4] end function on_settings() CUR = -1 end local NR, LIFE = 12, 14 local function ripple(i, t) local P = 15 + (i * 7) % 11 local ph = (i * 5) % P local age = (t + ph) % P if age >= LIFE then return end local idx = (t + ph) // P local hh = hash32(i * 977 + idx * 131) local u, v = (hh & 255) / 255, ((hh >> 8) & 255) / 255 local cx, cy, a if ((hh >> 16) & 127) < 80 then local p = PUD[1 + ((hh >> 20) & 15) % #PUD] cx, cy, a = floor(p[1] + (u - 0.5) * p[3] * 1.6), floor(p[2] + (v - 0.5) * p[4] * 1.6), 0.5 else cx, cy, a = 2 + floor(u * 60), 2 + floor(v * 60), 0.28 end a = a * (1 - age / LIFE) local ri = floor(0.6 + age * 0.27 + 0.5) if ri < 1 then ri = 1 end local h = ri > 1 and ri - 1 or 1 if ri >= 3 then h = ri - 2 local d = ri - 1 blend_rect(cx - d, cy - d, 1, 1, 206, 224, 250, a); blend_rect(cx + d, cy - d, 1, 1, 206, 224, 250, a) blend_rect(cx - d, cy + d, 1, 1, 206, 224, 250, a); blend_rect(cx + d, cy + d, 1, 1, 206, 224, 250, a) end blend_rect(cx - h, cy - ri, 2 * h + 1, 1, 206, 224, 250, a) blend_rect(cx - h, cy + ri, 2 * h + 1, 1, 206, 224, 250, a) blend_rect(cx - ri, cy - h, 1, 2 * h + 1, 206, 224, 250, a) blend_rect(cx + ri, cy - h, 1, 2 * h + 1, 206, 224, 250, a) end local function draw_car(t) local k = t - CAR0 if k < 0 or k > 86 then return end local fy = CAR_DIR == 1 and -16 + k * 1.4 or 80 - k * 1.4 local x = CAR_DIR == 1 and 17 or 38 local c = CAR_COL local y0 = floor(fy - 10 + 0.5) local front = CAR_DIR == 1 and y0 + 20 or y0 for b = 0, 4 do local d0 = b * 4 local w = 9 + 3 * b local yy = CAR_DIR == 1 and front + d0 or front - d0 - 4 blend_rect(x + 4 - w // 2, yy, w, 4, 255, 236, 170, 0.3 - 0.05 * b) end local ys = CAR_DIR == 1 and front or front - 30 blend_rect(x + 3, ys, 3, 30, 255, 244, 200, 0.18) local rear = CAR_DIR == 1 and y0 - 3 or y0 + 20 blend_rect(x, rear, 9, 3, 255, 40, 40, 0.18) gfx.round_rect(x, y0, 9, 20, c[1], c[2], c[3], c[1] * 6 // 10, c[2] * 6 // 10, c[3] * 6 // 10, 3) local wf = CAR_DIR == 1 and y0 + 12 or y0 + 4 rect(x + 1, wf, 7, 4, 28, 38, 58) rect(x + 1, CAR_DIR == 1 and y0 + 3 or y0 + 13, 7, 3, 34, 44, 66) rect(x + 1, CAR_DIR == 1 and y0 + 7 or y0 + 8, 7, 4, c[1] * 8 // 10, c[2] * 8 // 10, c[3] * 8 // 10) local hy = CAR_DIR == 1 and y0 + 19 or y0 rect(x, hy, 2, 1, 255, 248, 214); rect(x + 7, hy, 2, 1, 255, 248, 214) local ty = CAR_DIR == 1 and y0 or y0 + 19 rect(x, ty, 2, 1, 255, 60, 60); rect(x + 7, ty, 2, 1, 255, 60, 60) end function render() if not BG then build_bg() end built_now = false local t = frame % CYC local n = frame // CYC if n ~= CUR then setup(n); CUR = n end blit_rgb(BG, 0, 0, W, H) for i = 1, NR do ripple(i, frame) end for i = 1, NT do local k = t - TS[i] if k >= 0 and k < TD[i] then local x = TDIR[i] == 1 and -6 + 76 * k / TD[i] or 70 - 76 * k / TD[i] draw_umbrella(x, TY[i], TCLS[i], TCOL[i]) end end draw_car(t) end