info = { type = "image", title = "Snowy porthole", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A scarfed walker crosses a round snowy window", api = 33, } local ltr = ({ sk = { ["Snow"] = "Sneženie", ["Scarf"] = "Šál" }, cs = { ["Snow"] = "Sněžení", ["Scarf"] = "Šála" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "snow", label = LT("Snow"), type = "int", min = 1, max = 5, default = 3 }, { key = "scarf", label = LT("Scarf"), type = "color", default = "#C8283A" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, abs, min = math.floor, math.sin, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function rnd(v) return floor(v + 0.5) end local WX, WY, RI, RO = 32, 29, 19, 22 local VX, VY, VS = 13, 10, 39 local GY = 44 local WALL, VIEW = nil, nil local function bake_wall() for i = 0, 7 do local a = (i % 2 == 0) rect(i * 8, 0, 8, 54, a and 70 or 82, a and 42 or 50, a and 24 or 28) rect(i * 8, 0, 1, 54, 40, 22, 14) for k = 1, 3 do local h = hash32(i * 977 + k * 131) rect(i * 8 + 2 + umod(h, 5), umod(h >> 8, 40), 1, 3 + umod(h >> 16, 6), 60, 36, 20) end end rect(0, 54, W, 1, 96, 60, 34) rect(0, 55, W, 1, 44, 26, 16) rect(0, 56, W, 8, 92, 58, 32) rect(0, 60, W, 1, 62, 38, 22) for x = 5, W - 1, 19 do rect(x, 56, 1, 4, 62, 38, 22) end for x = 14, W - 1, 23 do rect(x, 61, 1, 3, 62, 38, 22) end for y = WY - 24, WY + 23 do local d = y + 0.5 - WY local ad = abs(d) for band = 1, 3 do local ro = band == 1 and 23.5 or band == 2 and 22 or 20 local ri = band == 1 and 22 or band == 2 and 20 or 19 if ad < ro then local xo = rnd(sqrt(ro * ro - d * d)) local xi = ad < ri and rnd(sqrt(ri * ri - d * d)) or 0 local dn = d / ro for side = -1, 1, 2 do local lum = 1 - 0.18 * side - 0.22 * dn local r, g, b if band == 1 then r, g, b = 34, 19, 12 elseif band == 2 then r, g, b = 156 * lum, 100 * lum, 54 * lum else r, g, b = 96 * lum, 60 * lum, 32 * lum end r, g, b = rnd(min(255, r)), rnd(min(255, g)), rnd(min(255, b)) if xi == 0 then if side == -1 then rect(WX - xo, y, 2 * xo, 1, r, g, b) end elseif side == -1 then rect(WX - xo, y, xo - xi, 1, r, g, b) else rect(WX + xi, y, xo - xi, 1, r, g, b) end end end end end rect(7, 51, 50, 3, 170, 112, 62) rect(7, 51, 50, 1, 204, 146, 88) rect(7, 53, 50, 1, 96, 58, 32) for y = WY - RI, WY + RI - 1 do local hw = rnd(sqrt(RI * RI - (y + 0.5 - WY) ^ 2)) rect(WX - hw, y, 2 * hw, 1, 255, 0, 255) end WALL = snapshot(0, 0, W, H) end local function pine(cx, base, tiers) rect(cx, base - 2, 1, 2, 54, 40, 36) local y = base - 2 for i = tiers, 1, -1 do local w = 1 + 2 * (tiers - i + 1) rect(cx - (w - 1) // 2, y - 2, w, 2, 28, 68, 66) rect(cx - (w - 1) // 2, y - 2, (w + 1) // 2, 1, 40, 94, 84) px(cx - (w - 1) // 2 + 1, y - 3, 236, 244, 255) y = y - 2 end px(cx, y - 1, 28, 68, 66) end local function bake_view() for y = VY, 34 do local t = (y - VY) / 24 rect(VX, y, VS, 1, rnd(84 + 92 * t), rnd(112 + 86 * t), rnd(200 + 42 * t)) end for y = 35, VY + VS - 1 do local t = (y - 35) / 13 rect(VX, y, VS, 1, rnd(234 - 50 * t), rnd(242 - 40 * t), rnd(254 - 16 * t)) end for dy = -5, 5 do local hw = rnd(sqrt(30 - dy * dy)) blend_rect(41 - hw, 16 + dy, 2 * hw, 1, 255, 244, 210, 0.16) end for dy = -3, 3 do local hw = rnd(sqrt(9.5 - dy * dy)) rect(41 - hw, 16 + dy, 2 * hw, 1, 252, 248, 222) end for x = VX, VX + VS - 1 do local hh = 2.5 + 2 * sin(x * 0.25) + 1.5 * sin(x * 0.11 + 1) local top = 35 - rnd(hh) rect(x, top, 1, 35 - top, 200, 216, 244) end pine(17, 36, 4); pine(22, 35, 3); pine(47, 36, 4); pine(43, 35, 3) blend_rect(15, 37, 10, 1, 150, 170, 215, 0.45) blend_rect(41, 37, 10, 1, 150, 170, 215, 0.45) for _, s in ipairs({ { 14, 46, 12 }, { 30, 47, 14 }, { 40, 45, 9 } }) do blend_rect(s[1], s[2], s[3], 1, 255, 255, 255, 0.35) end VIEW = snapshot(VX, VY, VS, VS) end local NF = 40 local FK = {} for i = 1, NF do local h = hash32(i * 0x9E3779B1) FK[i] = { x0 = VX + umod(h, VS), y0 = umod(h >> 8, 42), vy = 0.12 + umod(h >> 12, 24) / 100, sw = 1 + umod(h >> 16, 20) / 10, ws = 0.02 + umod(h >> 20, 30) / 1000, ph = umod(h >> 24, 628) / 100, a = 0.6 + umod(h >> 4, 40) / 100, } end local function snowfall(t) for i = 1, (cfg.snow or 3) * 8 do local f = FK[i] local y = (f.y0 + f.vy * t) % 42 + 8 local x = rnd(f.x0 + f.sw * sin(t * f.ws + f.ph)) local iy = floor(y) local fy = y - iy blend(x, iy, 255, 255, 255, f.a * (1 - fy)) blend(x, iy + 1, 255, 255, 255, f.a * fy) end end local function frect(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if f < 0.08 then rect(ix, y, w, h, r, g, b) elseif f > 0.92 then rect(ix + 1, y, w, h, r, g, b) else if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end end local CYC = 330 local X0, XS, V = 9, 30, 0.4 local T_STOP = (XS - X0) / V local T_OUT = 172 local function walker(t) local x, walking if t < T_STOP then x, walking = X0 + V * t, true elseif t < T_OUT then x, walking = XS, false else x, walking = XS + V * (t - T_OUT), true end if x > 62 then return end local x0 = x - 3.5 local gy = GY if t >= 150 and t < 166 then local s = (t - 150) / 16 gy = GY - rnd(12 * s * (1 - s)) end local arm = (t >= 80 and t < 130) and 2 or ((t >= 72 and t < 138) and 1 or 0) local up = t >= 66 and t < 134 local happy = t >= 100 and t < 130 local sc = cfg.scarf or { r = 200, g = 40, b = 58 } local ph = walking and (floor(x * 0.8) % 4) or 0 local sw = ph == 1 and 1 or (ph == 3 and -1 or 0) frect(x0 + 1 + sw, gy - 3, 2, 3, 22, 28, 52) frect(x0 + 4 - sw, gy - 3, 2, 3, 22, 28, 52) frect(x0 + 1 + sw, gy - 1, 2, 1, 70, 48, 44) frect(x0 + 4 - sw, gy - 1, 2, 1, 70, 48, 44) frect(x0, gy - 8, 7, 5, 30, 46, 98) frect(x0 + 5, gy - 8, 2, 5, 46, 66, 128) local tr, tg, tb = sc.r * 4 // 5, sc.g * 4 // 5, sc.b * 4 // 5 if walking then local w1, w2 = floor(sin(t * 0.5) + 1.5) - 1, floor(sin(t * 0.5 + 1.6) + 1.5) - 1 frect(x0 - 1.5, gy - 10 + w1, 2, 2, tr, tg, tb) frect(x0 - 3.5, gy - 10 + w2, 2, 2, tr, tg, tb) else frect(x0 - 0.5, gy - 9, 2, 4, tr, tg, tb) end frect(x0, gy - 10, 7, 2, sc.r, sc.g, sc.b) frect(x0 + 0.5, gy - 14, 6, 4, 246, 214, 200) frect(x0 + 0.5, gy - 15, 6, 1, 52, 72, 150) frect(x0 + 1.5, gy - 16, 4, 1, 68, 92, 176) frect(x0 + 2.5, gy - 17, 2, 1, 250, 250, 255) frect(x0 + 3.5, gy - 12, 1, 1, 240, 150, 150) frect(x0 + 6.5, gy - 12, 1, 1, 232, 120, 120) if happy then frect(x0 + 4.5, gy - 12, 1, 1, 40, 30, 44) frect(x0 + 5.5, gy - 11, 1, 1, 150, 70, 80) else frect(x0 + 4.5, up and gy - 14 or gy - 13, 1, 1, 40, 30, 44) end if arm == 2 then frect(x0 + 7, gy - 12, 2, 7, 46, 66, 128) frect(x0 + 7, gy - 14, 2, 2, 236, 226, 224) elseif arm == 1 then frect(x0 + 6, gy - 10, 2, 4, 46, 66, 128) frect(x0 + 6, gy - 11, 2, 1, 236, 226, 224) else frect(x0 + 2.5 + sw, gy - 8, 2, 4, 46, 66, 128) frect(x0 + 2.5 + sw, gy - 4, 2, 1, 236, 226, 224) end if t >= 48 and t < 130 then local fx, fy, a if t < 96 then local s = (t - 48) / 48 fx, fy, a = x0 + 8 + 1.5 * sin(t * 0.2) * (1 - s), 11 + (GY - 15 - 11) * s, 1 else fx, fy, a = x0 + 8, GY - 15, min(1, (130 - t) / 12) end local ix = rnd(fx) local iy = floor(fy) local f = fy - iy blend(ix, iy, 255, 255, 255, a * (1 - f)) blend(ix, iy + 1, 255, 255, 255, a * f) if t >= 90 then blend(ix - 1, iy + 1, 255, 255, 255, a * 0.6); blend(ix + 1, iy + 1, 255, 255, 255, a * 0.6) blend(ix, iy, 255, 255, 255, a) end if t >= 96 and t < 106 then local sp = 1 - (t - 96) / 10 blend(ix - 2, iy - 1, 255, 250, 200, sp); blend(ix + 2, iy - 1, 255, 250, 200, sp) blend(ix - 1, iy - 2, 255, 250, 200, sp); blend(ix + 1, iy - 2, 255, 250, 200, sp) end end end function render() if not VIEW then bake_view(); bake_wall() end blit_rgb(VIEW, VX, VY, VS, VS) snowfall(frame) walker(frame % CYC) blit_rgb(WALL, 0, 0, W, H, 255, 0, 255) end