info = { type = "image", title = "Snow lane", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Snow falls on a lamp-lit lane at night", api = 33, } local tr = ({ sk = { ["Snow"] = "Sneh", ["Lamp flicker"] = "Blikanie lampy", ["Never"] = "Nikdy", ["Rare"] = "Zriedka", ["Often"] = "Často", ["Constant"] = "Stále" }, cs = { ["Snow"] = "Sníh", ["Lamp flicker"] = "Blikání lampy", ["Never"] = "Nikdy", ["Rare"] = "Zřídka", ["Often"] = "Často", ["Constant"] = "Stále" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snow"), type = "int", min = 1, max = 5, default = 3 }, { key = "flicker", label = T("Lamp flicker"), type = "int", default = "rare", opts = { keys = { "never", "rare", "often", "constant" }, T("Never"), T("Rare"), T("Often"), T("Constant") } }, } local rect, px, blend, blend_rect, blit_idx = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local K, S1, S2, S3, S4, MN, MS, D1, D2, WA, WB, RF, SW, SL, SD, SE, BR, WD, WL, PL, PH, CB, CD, CW, HL = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 local PAL = { 0, 0, 0, 6, 8, 18, 10, 14, 44, 18, 26, 70, 30, 44, 100, 54, 70, 130, 252, 246, 206, 206, 198, 150, 26, 32, 64, 40, 48, 90, 66, 48, 70, 92, 66, 84, 40, 36, 64, 226, 236, 252, 168, 186, 224, 108, 128, 178, 66, 82, 132, 104, 54, 46, 20, 26, 50, 255, 208, 98, 20, 22, 34, 58, 60, 78, 120, 44, 54, 74, 28, 40, 34, 50, 92, 255, 238, 170, } local PATS = {} local function pat(a, b, ph) local k = (a * 32 + b) * 2 + ph local s = PATS[k] if not s then s = (ph == 0 and string.char(a, b) or string.char(b, a)):rep(32) PATS[k] = s end return s end local function hrow(y, x0, x1, a, b) local w = x1 - x0 + 1 if w > 0 then blit_idx(pat(a, b, (x0 + y) % 2):sub(1, w), PAL, x0, y, w, 1, w, 1) end end local function col(c, x, y, w, h) rect(x, y, w, h, PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3]) end local function dot(c, x, y) px(x, y, PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3]) end local function disc(cx, cy, r, c, a) for dy = -r, r do local hw = floor(sqrt(r * r + 0.5 - dy * dy) + 0.5) if a then blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, PAL[c * 3 + 1], PAL[c * 3 + 2], PAL[c * 3 + 3], a) else col(c, cx - hw, cy + dy, 2 * hw + 1, 1) end end end local function gable(cx, peak, base, k) for y = peak, base - 1 do local hw = floor(1 + (y - peak) * k) col(SL, cx - hw, y, hw + 1, 1) col(SW, cx + 1, y, hw, 1) end hrow(base, cx - floor(1 + (base - 1 - peak) * k), cx + floor(1 + (base - 1 - peak) * k), RF, RF) end local function wire(x0, y0, x1, y1, sag) local n = math.abs(x1 - x0) for i = 0, n do local s = i / n dot(PL, floor(x0 + (x1 - x0) * s + 0.5), floor(y0 + (y1 - y0) * s + sag * 4 * s * (1 - s) + 0.5)) end end local WINDOWS = { { 5, 37 }, { 12, 37 }, { 19, 37 }, { 39, 41 }, { 51, 41 } } local BG, CONE local function build_bg() for y = 0, 35 do if y < 8 then hrow(y, 0, 63, S1, S1) elseif y < 11 then hrow(y, 0, 63, S1, S2) elseif y < 17 then hrow(y, 0, 63, S2, S2) elseif y < 20 then hrow(y, 0, 63, S2, S3) elseif y < 26 then hrow(y, 0, 63, S3, S3) elseif y < 29 then hrow(y, 0, 63, S3, S4) else hrow(y, 0, 63, S4, S4) end end for i = 1, 16 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 22) if (x - 47) ^ 2 + (y - 10) ^ 2 > 100 then px(x, y, 150, 166, 220) end end col(D1, 0, 36, 64, 12) disc(47, 10, 8, MN, 0.1) disc(47, 10, 6, MN, 0.18) disc(47, 10, 5, MN) dot(MS, 45, 9); dot(MS, 46, 9); dot(MS, 48, 12); dot(MS, 46, 12); dot(MS, 49, 8) col(D1, 28, 27, 7, 9); col(D2, 28, 26, 7, 1); col(D1, 38, 21, 4, 15); col(D1, 39, 18, 2, 3); dot(D1, 39, 17) dot(D2, 38, 21); col(D1, 53, 27, 10, 9); col(D2, 53, 26, 10, 1) dot(WL, 30, 30); dot(WL, 56, 31); dot(WL, 40, 26) for y = 48, 63 do if y < 50 then hrow(y, 0, 63, SW, SL) elseif y < 53 then hrow(y, 0, 63, SL, SL) elseif y < 59 then hrow(y, 0, 63, SL, SD) elseif y < 61 then hrow(y, 0, 63, SD, SD) else hrow(y, 0, 63, SW, SL) end end hrow(57, 0, 63, SD, SE); hrow(58, 0, 63, SD, SE) col(SW, 0, 61, 64, 1) gable(13, 22, 33, 1.25) for y = 34, 47 do hrow(y, 2, 25, y < 37 and WA or WB, WB) end hrow(33, 0, 26, RF, RF) col(WA, 2, 44, 24, 4) col(BR, 21, 19, 3, 10); col(SW, 20, 18, 5, 1); col(SL, 21, 19, 1, 2) col(WA, 11, 42, 4, 6); dot(HL, 13, 45) gable(48, 29, 38, 1.5) for y = 39, 47 do hrow(y, 36, 60, y < 41 and WA or WB, WB) end hrow(38, 35, 61, RF, RF) col(WA, 44, 43, 4, 5); dot(HL, 46, 45) for _, w in ipairs(WINDOWS) do col(WD, w[1], w[2], 4, 4) col(SD, w[1] - 1, w[2] + 4, 6, 1) col(WB, w[1] + 1, w[2] + 1, 1, 2) end col(SL, 24, 47, 3, 2); col(SL, 36, 48, 8, 1) col(PL, 31, 28, 2, 21); col(PH, 29, 26, 6, 1); col(PH, 29, 27, 1, 1); col(PH, 34, 27, 1, 1); col(HL, 30, 27, 4, 1) col(PL, 57, 12, 2, 52); col(PL, 52, 16, 12, 1); dot(PH, 53, 15); dot(PH, 60, 15) wire(53, 16, 33, 26, 4) wire(53, 17, 26, 33, 5) wire(61, 17, 63, 21, 1) col(CB, 3, 54, 25, 6); col(CD, 3, 59, 25, 1) col(CB, 8, 50, 14, 4); col(CB, 7, 52, 1, 2); col(CB, 22, 52, 1, 2) col(CW, 10, 51, 4, 3); col(CW, 15, 51, 4, 3) col(SW, 8, 49, 14, 1); col(SW, 9, 50, 12, 1); col(SW, 22, 53, 6, 1); col(SL, 3, 53, 4, 1) col(PL, 6, 59, 5, 3); col(PL, 21, 59, 5, 3); col(SW, 6, 61, 5, 1); col(SW, 21, 61, 5, 1) col(HL, 27, 55, 1, 2); dot(CD, 3, 55) BG = snapshot(0, 0, W, H) end local CW_, CH_ = 36, 32 local function build_cone() local rows, half = {}, {} for y = 0, CH_ - 1 do for x = 0, 17 do local dx, dy = 17.5 - x, y + 0.5 local hw = 3 + 0.55 * dy half[x + 1] = floor(255 * max(0, 1 - dx / hw) * max(0, 1 - dy / CH_) * 0.9) end local s = string.char(table.unpack(half, 1, 18)) rows[y + 1] = s .. s:reverse() end CONE = table.concat(rows) end local FL = { 0, 12, 7, 3.5 } local function lamp(t, mode) if mode == "constant" then return umod(hash32(floor(t * 14)), 3) == 0 and 0.35 or 1 end local cyc = FL[({ never = 1, rare = 2, often = 3 })[mode] or 2] if cyc == 0 then return 1 end local e = floor(t / cyc) local local_t = t - e * cyc - umod(hash32(e), 100) / 100 * (cyc - 1) if local_t >= 0 and local_t < 0.7 then return umod(hash32(floor(local_t * 12) + e * 17), 2) == 0 and 0.2 or 1 end return 1 end local NF = 0 local FX, FY, FP = {}, {}, {} local function flakes() for i = 1, 100 do local h = hash32(i * 0x85EBCA6B) FX[i], FY[i], FP[i] = umod(h, 640) / 10, umod(h >> 10, 700) / 10, umod(h >> 20, 628) / 100 end NF = 100 end local function snow(t, d) local i = 0 for _ = 1, 8 * d do i = i + 1 local y = (FY[i] + t * 5) % 70 - 3 local x = (FX[i] + t * 2 + 2 * sin(t * 0.7 + FP[i])) % W local iy = floor(y) local f = y - iy local ix = floor(x) blend(ix, iy, 150, 166, 214, 0.7 * (1 - f)) blend(ix, iy + 1, 150, 166, 214, 0.7 * f) end for _ = 1, 5 * d do i = i + 1 local y = (FY[i] + t * 11) % 70 - 3 local x = (FX[i] + t * 4 + 3 * sin(t * 0.9 + FP[i])) % W local iy = floor(y) local f = y - iy local ix = floor(x) blend(ix, iy, 206, 218, 242, 0.9 * (1 - f)) blend(ix, iy + 1, 206, 218, 242, 0.9 * f) end for _ = 1, 2 * d do i = i + 1 local y = (FY[i] + t * 22) % 72 - 4 local x = (FX[i] + t * 7 + 4 * sin(t * 1.1 + FP[i])) % W rect(floor(x), floor(y), 2, 2, 244, 248, 255) end end local WIN_LIT = { 255, 208, 98 } local function windows(t) for i, w in ipairs(WINDOWS) do local P = 14 + i * 5 local v = (t / P + i * 0.37) % 1 local a = v < 0.55 and min(1, v * P) or max(0, 1 - (v - 0.55) * P) if a > 0 then blend_rect(w[1], w[2], 4, 4, 255, 208, 98, a) blend_rect(w[1] + 1, w[2] + 1, 2, 2, 255, 238, 170, a * 0.6) end end end local function smoke(t) for i = 0, 5 do local u = (t * 0.2 + i / 6) % 1 blend_rect(floor(22 + 9 * u + 1.5 * sin(u * 7 + i)), floor(17 - 15 * u), 2 + floor(2 * u), 2 + floor(2 * u), 190, 200, 226, 0.6 * (1 - u)) end end function render() if not BG then build_bg() return end if not CONE then build_cone() flakes() end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local f = lamp(t, cfg.flicker) blend_mask(CONE, 255, 214, 120, 14, 28, CW_, CH_, CW_, CH_, 0.55 * f) if f > 0.5 then blend_rect(29, 25, 6, 1, 255, 240, 190, 0.5) end if f < 0.5 then col(PH, 30, 27, 4, 1) end windows(t) smoke(t) snow(t, cfg.snow or 3) end