info = { type = "image", title = "Snowy dusk street", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Camera drifts down a snowy street at dusk", api = 33, } local tr = ({ sk = { ["Camera speed"] = "Rýchlosť kamery", ["Snowfall"] = "Sneženie", ["Chimney smoke"] = "Dym z komínov" }, cs = { ["Camera speed"] = "Rychlost kamery", ["Snowfall"] = "Sněžení", ["Chimney smoke"] = "Kouř z komínů" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Camera speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "snow", label = T("Snowfall"), type = "int", min = 0, max = 10, default = 5 }, { key = "smoke", label = T("Chimney smoke"), type = "bool", default = true }, } local rect, px, blend, blend_rect, blit_rgb = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb local floor, sin, abs, sqrt = math.floor, math.sin, math.abs, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local GY = 50 local SCW, FARW = 192, 128 local KR, KG, KB = 255, 0, 255 local function R(x, y, w, h, r, g, b) rect(x, y, w, h, floor(r + 0.5), floor(g + 0.5), floor(b + 0.5)) end local function B(x, y, w, h, r, g, b, a) blend_rect(x, y, w, h, floor(r + 0.5), floor(g + 0.5), floor(b + 0.5), a) end local WALLS = { { 96, 74, 86 }, { 70, 82, 112 }, { 104, 88, 76 }, { 78, 70, 102 }, { 88, 100, 104 } } local HOUSES = { { x = 16, w = 26, h = 26, wall = 2, gable = true, ch = 19 }, { x = 44, w = 20, h = 22, wall = 1 }, { x = 66, w = 28, h = 28, wall = 4, gable = true, ch = 20 }, { x = 96, w = 20, h = 20, wall = 3, shop = true }, { x = 118, w = 24, h = 27, wall = 5, gable = true }, { x = 144, w = 22, h = 23, wall = 1, ch = 4 }, { x = 168, w = 20, h = 25, wall = 2, gable = true, ch = 14 }, } local TREES = { 7 } local LAMPS = { 43, 95, 143 } local WINS, CHIM = {}, {} for _, b in ipairs(HOUSES) do b.win = {} local yt = GY - b.h local nw, nr = (b.w - 4) // 7, (b.h - 6) // 9 if b.shop then nr = nr - 1 end for r = 0, nr - 1 do for k = 1, nw do local wx = b.x + floor(k * (b.w - 4 * nw) / (nw + 1) + 4 * (k - 1) + 0.5) local wy = yt + 3 + r * 9 if not b.shop and r == nr - 1 and k == (nw + 1) // 2 then b.door = wx else local lit = (hash32(b.x * 131 + r * 17 + k * 7) & 7) < 5 b.win[#b.win + 1] = { x = wx, y = wy, w = 4, h = 5, lit = lit, kind = lit and ((k + r) % 3 == 0 and 2 or 1) or 0, ph = umod(hash32(wx * 31 + wy), 628) / 100 } end end end if b.shop then b.win[#b.win + 1] = { x = b.x + 3, y = GY - 10, w = b.w - 6, h = 7, lit = true, kind = 1, ph = 1 } end b.top = yt if b.gable then b.ph = floor(b.w * 0.4) b.top = yt - b.ph if b.ch then local d = abs(b.ch - b.w / 2) b.chtop = floor(b.top + d / (b.w / 2) * b.ph) - 6 end elseif b.ch then b.chtop = yt - 10 end for _, w in ipairs(b.win) do if w.kind > 0 then WINS[#WINS + 1] = w end end if b.chtop then CHIM[#CHIM + 1] = { x = b.x + b.ch + 2, y = b.chtop - 1 } end end local function snowc(t) return 216 - 62 * t, 226 - 56 * t, 248 - 34 * t end local function house(b, ox) local x0, w, h = b.x - ox, b.w, b.h if x0 >= W or x0 + w < -2 then return end local yt = GY - h local wc = WALLS[b.wall] for y = yt, GY - 1 do local d = 12 - 24 * (y - yt) / (h - 1) R(x0, y, w, 1, wc[1] + d, wc[2] + d * 0.9, wc[3] + d * 0.8) end R(x0 + w - 1, yt, 1, h, wc[1] * 0.6, wc[2] * 0.6, wc[3] * 0.65) B(x0, GY - 3, w, 3, 190, 204, 236, 0.75) if b.gable then local ph = b.ph local cx = x0 + w // 2 for r = 0, ph do local y = b.top + r local hw = floor((r + 1) * (w / 2 + 1) / (ph + 1) + 0.5) R(cx - hw, y, 2 * hw, 1, snowc(r / ph)) R(cx - hw, y, 1, 1, 110, 122, 170) R(cx + hw - 1, y, 1, 1, 110, 122, 170) end R(x0 - 1, yt - 1, w + 2, 1, 92, 100, 150) else for r = 0, 3 do R(x0 + 1 - r, yt - 4 + r, w - 2 + 2 * r, 1, snowc(r / 3)) end R(x0 - 2, yt - 1, w + 4, 1, 120, 132, 182) end R(x0, yt, w, 1, 40, 38, 58) for k = 0, 3 do px(x0 + 2 + k * 6, yt + 1, 196, 208, 238) end if b.chtop then local cx = x0 + b.ch R(cx, b.chtop, 4, GY - b.h - b.chtop + (b.gable and 4 or 3), 98, 66, 70) R(cx - 1, b.chtop - 1, 6, 2, 214, 224, 246) end if b.door then local dx = b.door - ox R(dx - 1, GY - 9, 7, 9, 38, 32, 44) R(dx, GY - 8, 5, 8, 66, 46, 52) R(dx, GY - 8, 5, 1, 255, 214, 140) B(dx - 3, GY - 9, 11, 4, 255, 200, 110, 0.16) B(dx - 2, GY, 9, 3, 255, 200, 110, 0.2) end for _, wn in ipairs(b.win) do local wx, wy, ww, wh = wn.x - ox, wn.y, wn.w, wn.h R(wx - 1, wy - 1, ww + 2, wh + 2, 38, 32, 48) if wn.lit then B(wx - 3, wy - 2, ww + 6, wh + 4, 255, 196, 110, 0.1) R(wx, wy, ww, wh - 2, 255, 222, 130) R(wx, wy + wh - 2, ww, 2, 255, 176, 84) B(wx + ww // 2, wy, 1, wh, 120, 70, 40, 0.5) else R(wx, wy, ww, wh, 40, 48, 80) R(wx, wy, ww, 1, 62, 72, 112) end R(wx - 1, wy + wh, ww + 2, 1, 214, 224, 244) end end local function fir(cx, ox) local x = cx - ox R(x - 1, GY - 3, 2, 4, 52, 36, 36) for j = 0, 2 do local y0 = GY - 21 + j * 6 for r = 0, 6 do local hw = 1 + floor(r * (1.5 + 0.6 * j) / 2 + 0.5) + j local y = y0 + r R(x - hw, y, 2 * hw + 1, 1, 24 + 4 * r, 54 + 3 * r, 62 + 3 * r) if r < 2 then B(x - hw + 1, y, 2 * hw - 1, 1, 220, 230, 250, 0.55) end if r == 6 then B(x - hw, y, 2 * hw + 1, 1, 214, 224, 246, 0.8) end end end end local function lamp(cx, ox) local x = cx - ox for k = -3, 3 do local hw = floor(11 * sqrt(1 - (k / 3.6) ^ 2)) B(x - hw, GY + 4 + k, 2 * hw, 1, 255, 206, 120, 0.09) B(x - hw * 6 // 10, GY + 4 + k, hw * 12 // 10, 1, 255, 206, 120, 0.12) end R(x, GY - 17, 1, 19, 34, 34, 50) R(x - 1, GY - 19, 3, 2, 255, 228, 150) R(x - 1, GY - 20, 3, 1, 90, 96, 140) end local function ground(ox) for y = GY, H - 1 do local t = (y - GY) / (H - 1 - GY) R(0, y, W, 1, 180 - 78 * t, 194 - 80 * t, 236 - 70 * t) end B(0, GY + 3, W, 1, 120, 134, 190, 0.5) B(0, 58, W, 1, 70, 84, 140, 0.18) B(0, 61, W, 1, 70, 84, 140, 0.18) end local SKYSTOPS = { { 0, 24, 30, 78 }, { 24, 70, 70, 128 }, { 44, 170, 120, 140 }, { 52, 214, 150, 130 } } local function sky_color(y) for i = 1, #SKYSTOPS - 1 do local a, b = SKYSTOPS[i], SKYSTOPS[i + 1] if y <= b[1] then local u = (y - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end return 214, 150, 130 end local function far_top(wx) local ph = wx * TAU / FARW return floor(22 + 8 * sin(ph + 0.6) + 4 * sin(ph * 2 + 2.0) + 2.5 * sin(ph * 5 + 1) + 0.5) end local SKYPAL, SKYCELLS = {}, {} for y = 0, H - 1 do local r, g, b = sky_color(y) SKYPAL[#SKYPAL + 1], SKYPAL[#SKYPAL + 2], SKYPAL[#SKYPAL + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) SKYCELLS[y + 1] = y end SKYCELLS = string.char(table.unpack(SKYCELLS)) local FARPAL = { 0, 0, 0 } for _, s in ipairs({ -10, 0, 14 }) do FARPAL[#FARPAL + 1], FARPAL[#FARPAL + 2], FARPAL[#FARPAL + 3] = 150 + s, 162 + s, 206 + s end for _, s in ipairs({ -10, 0, 14 }) do FARPAL[#FARPAL + 1], FARPAL[#FARPAL + 2], FARPAL[#FARPAL + 3] = 86 + s, 96 + s, 148 + s end local FAR, NEAR = {}, {} local BAKE = {} for k = 0, 1 do BAKE[#BAKE + 1] = function() local top, shade = {}, {} for x = 0, 63 do local wx = (k * 64 + x) % FARW top[x] = far_top(wx) local lit = far_top((wx + 1) % FARW) - far_top((wx - 1) % FARW) shade[x] = lit < 0 and 3 or (lit > 0 and 1 or 2) end local rows = {} for y = 0, H - 1 do local row = {} for x = 0, 63 do local t = top[x] row[x + 1] = (y < t or y > GY) and 0 or (y < t + 5 and shade[x] or 3 + shade[x]) end rows[y + 1] = string.char(table.unpack(row)) end FAR[k] = table.concat(rows) end end for k = 0, 2 do BAKE[#BAKE + 1] = function() local ox = k * 64 rect(0, 0, W, H, KR, KG, KB) for _, b in ipairs(HOUSES) do house(b, ox) end ground(ox) for _, t in ipairs(TREES) do fir(t, ox) end for _, l in ipairs(LAMPS) do lamp(l, ox) end NEAR[k] = gfx.snapshot(0, 0, W, H) end end local STAGE = 1 local FL = {} for i = 1, 80 do local h = hash32(i * 0x9E3779B1) FL[i] = { x = umod(h, 64), y = umod(h >> 6, 66), z = i % 3, ph = umod(h >> 12, 628) / 100 } end local FVY = { 11, 17, 26 } local FPAR = { 0.6, 1.0, 1.4 } local function sx(wx, cam, pad) local d = (wx - cam) % SCW if d > SCW - pad then d = d - SCW end return d end function render() if STAGE <= #BAKE then BAKE[STAGE](); STAGE = STAGE + 1 end local t = frame local cam = frame // (6 - (cfg.speed or 3)) gfx.blit_indexed(SKYCELLS, SKYPAL, 0, 0, W, H, 1, H) local fx = floor(cam * 0.4) for i = 0, 1 do for off = 0, FARW, FARW do local xs = 64 * i - fx % FARW + off if FAR[i] and xs > -64 and xs < 64 then gfx.blit_indexed(FAR[i], FARPAL, xs, 0, 64, 64, 64, 64, true) end end end local nc = cam % SCW for i = 0, 2 do for off = 0, SCW, SCW do local xs = 64 * i - nc + off if NEAR[i] and xs > -64 and xs < 64 then blit_rgb(NEAR[i], xs, 0, W, H, KR, KG, KB) end end end for i = 1, #WINS do local wn = WINS[i] local x = sx(wn.x, nc, 16) if x < 64 then if wn.kind == 1 then B(x, wn.y, wn.w, wn.h, 255, 236, 170, 0.12 + 0.12 * sin(t * 0.06 + wn.ph)) else local c = (t / FPS + wn.ph * 3) % 24 local a = c < 9 and 0 or (c < 11 and (c - 9) / 2 or (c < 16 and 1 or (c < 18 and 1 - (c - 16) / 2 or 0))) if a > 0 then B(x, wn.y, wn.w, wn.h, 40, 48, 80, 0.85 * a) end end end end for i = 1, #LAMPS do local x = sx(LAMPS[i], nc, 8) if x < 66 then B(x - 3, GY - 22, 7, 5, 255, 214, 130, 0.14) B(x - 2, GY - 21, 5, 3, 255, 226, 150, 0.2) end end if cfg.smoke ~= false then for i = 1, #CHIM do local c = CHIM[i] local x0 = sx(c.x, nc, 12) if x0 < 70 then for j = 0, 4 do local a = ((t / FPS) * 0.25 + j * 0.2 + i * 0.13) % 1 local x = x0 + floor(a * a * 9 + 1.5 * sin(a * 7 + j + i) + 0.5) local y = c.y - floor(a * 11) local s = 1 + floor(a * 2.4) B(x, y, s, s, 214, 218, 240, 0.7 * (1 - a)) end end end end for i = 1, (cfg.snow or 5) * 8 do local f = FL[i] local z = f.z + 1 local x = floor(f.x + t * 5 / FPS + 2 * sin(t * 0.04 + f.ph) - cam * FPAR[z]) % 64 local y = floor(f.y + t * FVY[z] / FPS) % 66 - 1 if z == 3 then B(x, y, 1, 2, 244, 248, 255, 0.85) else blend(x, y, 236, 242, 255, z == 1 and 0.5 or 0.75) end end end