info = { type = "image", title = "Leaky pipe", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Drops fall from a pipe and splash in a puddle", api = 33, } local tr = ({ sk = { ["Drip rate"] = "Rýchlosť kvapkania", ["Leaks"] = "Úniky", ["Water colour"] = "Farba vody" }, cs = { ["Drip rate"] = "Rychlost kapání", ["Leaks"] = "Úniky", ["Water colour"] = "Barva vody" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rate", label = T("Drip rate"), type = "int", min = 1, max = 5, default = 3 }, { key = "leaks", label = T("Leaks"), type = "int", min = 1, max = 3, default = 2 }, { key = "water", label = T("Water colour"), type = "color", default = "#64B4FF" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sqrt = math.floor, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local MORTAR = { 98, 86, 82 } local BRICKS = { { 150, 66, 50 }, { 130, 56, 44 }, { 168, 80, 58 } } local SKIRT, FLOOR = { 44, 40, 46 }, { 84, 86, 96 } local CRACK = { 62, 64, 74 } local P_HI, P_BODY = { 178, 186, 198 }, { 122, 130, 144 } local P_DARK, P_SHADOW = { 96, 102, 116 }, { 62, 66, 78 } local FLANGE, BOLT = { 88, 94, 106 }, { 196, 202, 212 } local RUST = { 150, 84, 46 } local WALL_B = 50 local PY0, PY1 = 6, 11 local VX0, VX1 = 42, 47 local LX = { 24, 10, 34 } local DY0, IY = 13, 56 local CX, CY, PRX, PRY = 22, 57, 21, 4 local function c3(c) return c[1], c[2], c[3] end local function mix(c, d, k) return floor(c[1] + (d[1] - c[1]) * k), floor(c[2] + (d[2] - c[2]) * k), floor(c[3] + (d[3] - c[3]) * k) end local RMAX = 11 local RING = {} for r = 1, RMAX do local s, ro, ri = {}, 0.3 * r + 0.4, 0.3 * (r - 1.3) local n = math.ceil(ro) for dy = -n, n do if dy * dy <= ro * ro then local xo = floor(r * sqrt(1 - dy * dy / (ro * ro)) + 0.5) local xi = -1 if ri > 0 and dy * dy < ri * ri then xi = floor((r - 1.3) * sqrt(1 - dy * dy / (ri * ri)) + 0.5) end if xi < 0 then s[#s + 1] = -xo; s[#s + 1] = 2 * xo + 1; s[#s + 1] = dy elseif xi < xo then s[#s + 1] = -xo; s[#s + 1] = xo - xi; s[#s + 1] = dy s[#s + 1] = xi + 1; s[#s + 1] = xo - xi; s[#s + 1] = dy end end end RING[r] = s end local BG = nil local WR, WG, WB = 100, 180, 255 local DR, DG, DB = 178, 218, 255 local NLEAK, RATE = 2, 3 local function pipe_h(x, w) rect(x, PY0, w, 1, c3(P_HI)); rect(x, PY0 + 1, w, 2, c3(P_BODY)) rect(x, PY0 + 3, w, 2, c3(P_DARK)); rect(x, PY1, w, 1, c3(P_SHADOW)) end local function flange_h(x) rect(x - 1, PY0 - 1, 3, PY1 - PY0 + 3, c3(FLANGE)) px(x, PY0 - 1, c3(BOLT)); px(x, PY1 + 1, c3(BOLT)) end local function build_bg() rect(0, 0, W, WALL_B, c3(MORTAR)) for k = 0, WALL_B // 4 do local y = k * 4 for j = -1, W // 8 do local x = j * 8 + (k % 2) * 4 local c = BRICKS[umod(hash32(k * 0x9E3779B1 + j * 0x85EBCA6B), 3) + 1] rect(x, y, 7, (y + 3 <= WALL_B) and 3 or WALL_B - y, c3(c)) end end for i = 1, NLEAK do local x = LX[i] for y = PY1 + 2, WALL_B - 1 do local w = 3 + floor(1.2 * math.sin(y * 0.35 + i) + 1.2 + 0.5) blend_rect(x - w // 2, y, w, 1, 20, 10, 30, 0.16) end rect(x, PY1 + 2, 1, 3, c3(RUST)); px(x + 1, PY1 + 2, c3(RUST)) end rect(0, WALL_B, W, 1, c3(SKIRT)) rect(0, WALL_B + 1, W, H - WALL_B - 1, c3(FLOOR)) rect(50, 58, 5, 1, c3(CRACK)); rect(55, 59, 3, 1, c3(CRACK)); rect(48, 53, 3, 1, c3(CRACK)) pipe_h(0, VX1 + 1) rect(VX0, PY1 + 1, 1, WALL_B - PY1 - 1, c3(P_HI)); rect(VX0 + 1, PY1 + 1, 2, WALL_B - PY1 - 1, c3(P_BODY)) rect(VX0 + 3, PY1 + 1, 2, WALL_B - PY1 - 1, c3(P_DARK)) rect(VX1, PY0, 1, WALL_B - PY0, c3(P_SHADOW)) rect(VX0 - 1, WALL_B - 1, VX1 - VX0 + 3, 2, c3(FLANGE)) rect(VX0 - 1, 28, VX1 - VX0 + 3, 2, c3(FLANGE)); px(VX0 - 1, 28, c3(BOLT)); px(VX1 + 1, 28, c3(BOLT)) for i = 1, #LX do flange_h(LX[i]) end rect(17, PY0 - 2, 2, PY1 - PY0 + 5, c3(P_SHADOW)) local mr, mg, mb = mix(FLOOR, { WR, WG, WB }, 0.45) local lr, lg, lb = mix(FLOOR, { WR, WG, WB }, 0.75) for dy = -PRY, PRY do local hw = floor(PRX * sqrt(1 - dy * dy / (PRY * PRY + 0.5)) + 0.5) rect(CX - hw, CY + dy, 2 * hw + 1, 1, mr, mg, mb) if dy == -PRY or dy == -PRY + 1 then px(CX - hw, CY + dy, lr, lg, lb); px(CX + hw, CY + dy, lr, lg, lb) end end rect(CX - 10, CY - PRY, 21, 1, lr, lg, lb) rect(CX + 6, CY + 1, 6, 1, lr, lg, lb); rect(CX - 14, CY + 2, 4, 1, lr, lg, lb) BG = gfx.snapshot(0, 0, W, H) end local BASE_P = { 61, 83, 71 } local OFF = { 0, 37, 19 } local RATE_K = { 1.7, 1.3, 1.0, 0.8, 0.6 } local P = { 61, 83, 71 } local FT, G = 18, 2 * (IY - DY0 - 1) / (18 * 18) local SPX = { -0.55, -0.3, 0.3, 0.6 } local SPY = { -1.3, -1.7, -1.6, -1.2 } local SG = 0.22 function on_settings() local c = cfg.water or { r = 100, g = 180, b = 255 } WR, WG, WB = c.r, c.g, c.b DR, DG, DB = (c.r + 255) // 2, (c.g + 255) // 2, (c.b + 255) // 2 NLEAK = cfg.leaks or 2 local k = RATE_K[cfg.rate or 3] or 1.0 for i = 1, 3 do P[i] = floor(BASE_P[i] * k) end BG = nil end local function ring(x, r, a) local s = RING[r] for k = 1, #s, 3 do blend_rect(x + s[k], IY + s[k + 2], s[k + 1], 1, DR, DG, DB, a) end end local function leak(i, t) local x, Pi = LX[i], P[i] local u = (t + OFF[i]) % Pi local form = Pi - FT if u < form then local s = u / form if s < 0.7 then blend(x, DY0, DR, DG, DB, 0.3 + s) else px(x, DY0, DR, DG, DB) blend(x, DY0 + 1, DR, DG, DB, (s - 0.7) / 0.3) end else local tt = u - form local y = DY0 + 1 + 0.5 * G * tt * tt local yi = floor(y) local f = y - yi local ln = floor(G * tt) if ln > 5 then ln = 5 end if ln > 0 then blend_rect(x, yi - ln, 1, ln, WR, WG, WB, 0.4) end blend(x, yi, DR, DG, DB, 1 - f * 0.5) blend(x, yi + 1, DR, DG, DB, f) end if u < 3 then blend_rect(x - 1, IY - 1, 3, 1, DR, DG, DB, 0.8) end if u >= 1 and u < 16 then for k = 1, 4 do local sy = IY + SPY[k] * u + 0.5 * SG * u * u if sy < IY then blend(floor(x + SPX[k] * u + 0.5), floor(sy), DR, DG, DB, 0.9) end end end end local function rings(i, t) local u = (t + OFF[i]) % P[i] for d = 0, 7, 7 do local r = u - d if r >= 1 and r <= RMAX then ring(LX[i], r, 0.7 * (1 - r / (RMAX + 1))) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS gfx.clip(CX - PRX, CY - PRY, 2 * PRX + 1, 2 * PRY + 1) for i = 1, NLEAK do rings(i, t) end gfx.clip() for i = 1, NLEAK do leak(i, t) end end