info = { type = "image", title = "Wipers in the rain", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rainy night through a windscreen, wipers", api = 33, } local tr = ({ sk = { ["Rain strength"] = "Sila dažďa" }, cs = { ["Rain strength"] = "Síla deště" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rain", label = T("Rain strength"), 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, atan = math.floor, math.sin, math.sqrt, math.abs, math.atan local hash32 = math.hash32 local W, H = gfx.W, gfx.H local GLASS_B = 52 local WP_BY, NS_BY = { 150, 120, 96, 78, 62 }, { 14, 22, 32, 46, 64 } local FO, CYC = 0, {} local WP, NS, SW = 96, 32, 24 local TH0, TH1 = 5, 84 local PA, PB = { 5, 55, 47 }, { 32, 55, 38 } local AMBER, WHITE, RED = { 255, 172, 72 }, { 226, 234, 255 }, { 255, 52, 40 } local YEL, GREEN = { 255, 200, 80 }, { 70, 230, 150 } local function cl(v) v = floor(v + 0.5); return v < 0 and 0 or v > 255 and 255 or v end local MASK = {} local function bokeh(r) local ri = floor(r + 0.5) if ri < 1 then ri = 1 end local m = MASK[ri] if not m then local D, rows = 2 * ri, {} for j = 0, D - 1 do local row = {} for i = 0, D - 1 do local d = sqrt((i + 0.5 - ri) ^ 2 + (j + 0.5 - ri) ^ 2) / ri local v = 0 if d < 1.08 then local e = d < 0.8 and 1 or (1.08 - d) / 0.28 v = floor(255 * e * (0.6 + 0.4 * d * d)) end row[i + 1] = v end rows[j + 1] = string.char(table.unpack(row, 1, D)) end m = table.concat(rows) MASK[ri] = m end return m, 2 * ri end local function bk(cx, cy, r, c, a) local m, D = bokeh(r) local h = D // 2 gfx.blend_mask(m, c[1], c[2], c[3], floor(cx + 0.5) - h, floor(cy + 0.5) - h, D, D, D, D, a) end local RM local function reflect(cx, c, a, y0) if not RM then local t = {} for j = 0, 15 do local row = {} for i = 0, 4 do row[i + 1] = floor(255 * (1 - j / 16) * (1 - abs(i - 2) / 3.2)) end t[j + 1] = string.char(table.unpack(row, 1, 5)) end RM = table.concat(t) end gfx.blend_mask(RM, c[1], c[2], c[3], cx - 2, y0, 5, 16, 5, 16, a) end local LIGHTS = { { 18, 27, 10, AMBER, 0.16 }, { 48, 24, 9, AMBER, 0.14 }, { 33, 38, 8, RED, 0.16 }, { 7, 24, 6, AMBER, 0.6 }, { 14, 28, 4, AMBER, 0.62 }, { 20, 31, 3, AMBER, 0.66 }, { 25, 32, 2, AMBER, 0.7 }, { 57, 22, 6, WHITE, 0.4 }, { 51, 26, 4, AMBER, 0.6 }, { 44, 29, 3, AMBER, 0.66 }, { 39, 31, 2, AMBER, 0.7 }, { 32, 31, 1, WHITE, 0.7 }, { 29, 30, 1, AMBER, 0.6 }, { 12, 12, 3, AMBER, 0.3 }, { 19, 17, 2, WHITE, 0.3 }, { 26, 10, 3, AMBER, 0.25 }, { 34, 14, 2, AMBER, 0.32 }, { 41, 19, 3, WHITE, 0.26 }, { 46, 11, 3, AMBER, 0.28 }, { 58, 12, 2, AMBER, 0.3 }, { 9, 19, 2, WHITE, 0.28 }, { 30, 38, 3, RED, 0.8 }, { 37, 38, 3, RED, 0.8 }, { 47, 35, 2, RED, 0.7 }, { 50, 35, 2, RED, 0.7 }, } local BG local stage = 0 local function build_sky() for y = 0, GLASS_B - 1 do local t = y < 10 and 0 or (y < 35 and (y - 10) / 25 or 1) rect(0, y, W, 1, cl(4 + 22 * t), cl(8 + 12 * t), cl(16 + 4 * t)) end for x = 4, 59, 5 do local h = 4 + hash32(x * 7919) % 11 local w = 4 + hash32(x * 31) % 3 rect(x, 35 - h, w, h, 5, 8, 14) for k = 0, 3 do local g = hash32(x * 131 + k * 17) if g % 3 == 0 then blend_rect(x + 1 + g % (w - 1), 35 - h + 2 + (g >> 4) % (h - 3), 1, 1, 255, 190, 100, 0.45) end end end for y = 35, GLASS_B - 1 do local t = 1 - (y - 35) / 17 rect(0, y, W, 1, cl(4 + 14 * t * t), cl(9 + 8 * t * t), cl(13 + 3 * t * t)) end end local function build_lights(i0, i1) for k = i0, i1 do local L = LIGHTS[k] bk(L[1], L[2], L[3], L[4], L[5]) if L[2] < 40 and L[3] >= 2 and L[5] >= 0.5 then reflect(L[1], L[4], L[5] * 0.5, 36) end end end local function build_dash() rect(0, GLASS_B, W, H - GLASS_B, 6, 8, 10) rect(0, GLASS_B, W, 1, 36, 44, 52) rect(0, GLASS_B + 1, W, 1, 16, 20, 24) blend_rect(14, GLASS_B, 34, 1, 255, 170, 80, 0.25) for y = GLASS_B + 4, H - 1, 4 do rect(0, y, W, 1, 9, 12, 15) end rect(4, 57, 12, 5, 10, 14, 14) for i = 0, 5 do px(5 + i * 2, 58, 24, 60 + i * 4, 40) end rect(46, 56, 14, 6, 10, 12, 14); rect(47, 57, 12, 1, 20, 26, 32) rect(0, 0, 4, GLASS_B, 6, 9, 12); rect(3, 0, 1, GLASS_B, 24, 32, 38) rect(60, 0, 4, GLASS_B, 6, 9, 12); rect(60, 0, 1, GLASS_B, 24, 32, 38) rect(27, 0, 14, 5, 8, 11, 14); rect(28, 5, 12, 1, 8, 11, 14); rect(28, 4, 12, 1, 32, 40, 48) BG = gfx.snapshot(0, 0, W, H) end function on_settings() local k = cfg.rain or 3 WP, NS = WP_BY[k] or 96, NS_BY[k] or 32 FO = WP * 8 - 54 CYC = {} end local function pass_f(p, x, y) local dx, dy = x - p[1], p[2] - y if dx < 0 then return nil end if sqrt(dx * dx + dy * dy) > p[3] - 2 then return nil end local f = (atan(dy, dx) * 57.29578 - TH0) / (TH1 - TH0) if f < -0.02 or f > 1.02 then return nil end return f < 0 and 0 or f > 1 and 1 or f end local function make_cycle(c) local t, n = {}, 0 local half = SW / 2 for s = 1, NS do local h = hash32(c * 7919 + s * 104729 + 17) local ts = c * WP + (h & 0x7FFF) % WP local x = 5 + ((h >> 4) & 0x7FFF) % 54 local y = 3 + ((h >> 9) & 0x7FFF) % 46 local f = pass_f(PA, x, y) or pass_f(PB, x, y) if f then local tu, td, tu2 = c * WP + half * f, c * WP + SW - half * f, (c + 1) * WP + half * f local clear = tu >= ts + 8 and tu or (td >= ts + 8 and td or tu2) local g = hash32(h + 99) n = n + 1 t[n] = { ts = ts, clear = clear, x = x, y = y, big = (g & 3) ~= 0, large = (g & 0x300) == 0, slide = (g & 0xC) == 0 and 24 + ((g >> 4) & 63) or nil, c = (g >> 12) & 3 } end end return t end local TINT = { { 240, 196, 130 }, { 176, 208, 232 }, { 232, 128, 100 } } local DM = { string.char(230, 100, 100, 180), string.char(150, 200, 120, 200, 70, 230, 120, 230, 180), string.char(120, 230, 230, 120, 230, 60, 70, 200, 230, 70, 60, 230, 120, 200, 230, 160) } local function drop(d, f) local age = f - d.ts if age < 8 then return end local x, y = d.x, d.y if d.slide and age > d.slide then local k = age - d.slide local dy = 0.0035 * k * k if dy > 12 then dy = 12 end y = floor(y + dy) if y > d.y then blend_rect(x, d.y, 1, y - d.y, 150, 190, 210, 0.14) end end if y >= GLASS_B - 1 then return end local t = TINT[d.c % 3 + 1] if age > 40 and d.large then gfx.blend_mask(DM[3], t[1], t[2], t[3], x, y, 4, 4, 4, 4, 0.85) elseif age > 22 and d.big then gfx.blend_mask(DM[2], t[1], t[2], t[3], x, y, 3, 3, 3, 3, 0.85) else gfx.blend_mask(DM[1], t[1], t[2], t[3], x, y, 2, 2, 2, 2, 0.8) end end local function wline(x0, y0, x1, y1, w, r, g, b, a) local dx, dy = x1 - x0, y1 - y0 if abs(dx) >= abs(dy) then if dy == 0 then dy = 0.001 end local ya, yb = y0 < y1 and y0 or y1, y0 < y1 and y1 or y0 local xmin, xmax = x0 < x1 and x0 or x1, x0 < x1 and x1 or x0 for yy = floor(ya), floor(yb) do local p, q = x0 + (yy - y0) / dy * dx, x0 + (yy + 1 - y0) / dy * dx if p > q then p, q = q, p end if p < xmin then p = xmin end if q > xmax then q = xmax end local xs, xe = floor(p + 0.5), floor(q + 0.5) if xe <= xs then xe = xs + 1 end if a then blend_rect(xs, yy, xe - xs, w, r, g, b, a) else rect(xs, yy, xe - xs, w, r, g, b) end end else local xa, xb = x0 < x1 and x0 or x1, x0 < x1 and x1 or x0 local ymin, ymax = y0 < y1 and y0 or y1, y0 < y1 and y1 or y0 for xx = floor(xa), floor(xb) do local p, q = y0 + (xx - x0) / dx * dy, y0 + (xx + 1 - x0) / dx * dy if p > q then p, q = q, p end if p < ymin then p = ymin end if q > ymax then q = ymax end local ys, ye = floor(p + 0.5), floor(q + 0.5) if ye <= ys then ye = ys + 1 end if a then blend_rect(xx, ys, w, ye - ys, r, g, b, a) else rect(xx, ys, w, ye - ys, r, g, b) end end end end local function wiper(p, th) local rad = th * 0.0174533 local c, s = math.cos(rad), math.sin(rad) local L = p[3] local x1, y1 = p[1] + L * c, p[2] - L * s local bx, by = p[1] + 0.28 * L * c, p[2] - 0.28 * L * s wline(p[1], p[2], bx, by, 1, 56, 62, 72) wline(bx, by, x1, y1, 3, 10, 12, 14) wline(bx, by - 1, x1, y1 - 1, 1, 170, 186, 204, 0.5) end local function theta(u) local half = SW / 2 if u < half then return TH0 + (TH1 - TH0) * (u / half) end if u < SW then return TH0 + (TH1 - TH0) * (1 - (u - half) / half) end return TH0 end local function draw_cars(f) for k = 0, 1 do local p = ((f + k * 190) % 380) / 380 local q = p * p local x, y, r = 30 - 40 * q, 34 + 8 * q, 1.5 + 4.5 * q local sp = 3 + 12 * q local a = 0.5 + 0.35 * p bk(x, y, r, WHITE, a); bk(x + sp, y, r, WHITE, a) bk(x + sp / 2, y, r * 2.2, WHITE, 0.08 * p) if x > 3 then reflect(floor(x + sp / 2), WHITE, 0.2 + 0.2 * p, 37 + floor(3 * q)) end end end local TL = { { 53, 9 }, { 53, 16 }, { 53, 23 } } local function draw_traffic(f) local u = f % 630 local lit = u < 330 and 1 or (u < 360 and 4 or (u < 600 and 3 or 2)) local cols = { RED, YEL, GREEN } for i = 1, 3 do local on = lit == i or (lit == 4 and i <= 2) local c = cols[i] if on then bk(TL[i][1], TL[i][2], 6, c, 0.2) bk(TL[i][1], TL[i][2], 3, c, 0.85) reflect(TL[i][1], c, 0.3, 37 + (i - 1) * 2) else bk(TL[i][1], TL[i][2], 3, c, 0.1) end end end function render() if stage < 6 then stage = stage + 1 if stage == 1 then build_sky() elseif stage <= 5 then build_lights((stage - 2) * 8 + 1, math.min((stage - 1) * 8, #LIGHTS)) else build_dash() end return end local f = frame + FO gfx.blit_rgb(BG, 0, 0, W, H) draw_cars(f) draw_traffic(f) local c = f // WP for k = c - 1, c do local t = CYC[k] if not t then t = make_cycle(k) CYC[k] = t CYC[k - 2] = nil end for i = 1, #t do local d = t[i] if f >= d.ts and f < d.clear then drop(d, f) end end end local th = theta(f % WP) wiper(PA, th); wiper(PB, th) if (frame // 18) % 2 == 0 then rect(6, 57, 2, 2, 40, 220, 90) end end