info = { type = "image", title = "Cabin on a snowy hill", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Dusk over a snowy cabin, snow drifting down", api = 44, } local ltr = ({ sk = { ["Time of day"] = "Denná doba", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Snowfall"] = "Sneženie" }, cs = { ["Time of day"] = "Denní doba", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Snowfall"] = "Sněžení" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "time", label = LT("Time of day"), type = "int", default = "dusk", opts = { keys = { "dusk", "night" }, LT("Dusk"), LT("Night") } }, { key = "snow", label = LT("Snowfall"), type = "int", min = 0, max = 3, default = 2 }, } 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, exp, min, max = math.floor, math.sin, math.sqrt, math.exp, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, GROUND, PX = 34, 42, 34 local TIMES = { dusk = { sky = { { 0, 36, 46, 116 }, { 12, 58, 60, 140 }, { 22, 118, 86, 158 }, { 28, 210, 130, 136 }, { 31, 255, 172, 112 }, { 33, 255, 204, 138 } }, fog = { 240, 170, 150 }, haze = { 112, 92, 160 }, forest = { 60, 54, 114 }, sh = { 78, 80, 154 }, mid = { 152, 140, 208 }, lit = { 238, 208, 232 }, rim = { 255, 198, 172 }, roofL = { 246, 210, 224 }, roofD = { 148, 138, 202 }, roofU = { 64, 48, 86 }, wallA = { 100, 62, 64 }, wallB = { 78, 48, 58 }, wallS = { 122, 74, 64 }, wallT = { 94, 58, 60 }, stone = { 88, 72, 102 }, door = { 44, 30, 48 }, win = { 255, 182, 88 }, winC = { 255, 232, 164 }, winDim = { 150, 66, 38 }, glow = { 255, 162, 74 }, glowA = 0.3, tree = { 36, 46, 98 }, treeL = { 66, 72, 130 }, treeS = { 180, 166, 228 }, treeSD = { 128, 122, 190 }, trunk = { 56, 40, 58 }, smoke = { 208, 186, 208 }, cloudT = { 206, 186, 236 }, cloudB = { 255, 198, 178 }, cloudA = 1, flake = { 240, 234, 255 }, stars = 0, }, night = { sky = { { 0, 12, 18, 58 }, { 14, 20, 30, 90 }, { 26, 36, 46, 118 }, { 31, 72, 66, 134 }, { 33, 112, 86, 136 } }, fog = { 120, 90, 130 }, haze = { 46, 52, 112 }, forest = { 30, 36, 86 }, sh = { 54, 64, 130 }, mid = { 98, 112, 186 }, lit = { 158, 172, 234 }, rim = { 198, 176, 212 }, roofL = { 172, 186, 242 }, roofD = { 92, 104, 172 }, roofU = { 40, 42, 82 }, wallA = { 82, 52, 60 }, wallB = { 64, 42, 54 }, wallS = { 96, 60, 62 }, wallT = { 76, 48, 58 }, stone = { 66, 62, 102 }, door = { 30, 24, 46 }, win = { 255, 186, 92 }, winC = { 255, 232, 164 }, winDim = { 150, 66, 38 }, glow = { 255, 166, 78 }, glowA = 0.5, tree = { 22, 30, 76 }, treeL = { 42, 50, 102 }, treeS = { 142, 152, 216 }, treeSD = { 98, 108, 172 }, trunk = { 40, 30, 52 }, smoke = { 132, 132, 186 }, cloudT = { 72, 78, 142 }, cloudB = { 112, 98, 152 }, cloudA = 0.7, flake = { 214, 222, 255 }, stars = 22, }, } local P = TIMES.dusk local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local function noise2(x, y, seed) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy fx = fx * fx * (3 - 2 * fx) fy = fy * fy * (3 - 2 * fy) local s = ix * 0x9E3779B1 + seed local a = (hash32(s + iy * 0x85EBCA6B) & 0x7FFF) / 32767 local b = (hash32(s + 0x9E3779B1 + iy * 0x85EBCA6B) & 0x7FFF) / 32767 local c = (hash32(s + (iy + 1) * 0x85EBCA6B) & 0x7FFF) / 32767 local d = (hash32(s + 0x9E3779B1 + (iy + 1) * 0x85EBCA6B) & 0x7FFF) / 32767 local top = a + (b - a) * fx return top + (c + (d - c) * fx - top) * fy end local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function c3(c) return c[1], c[2], c[3] end local function clamp01(v) return v < 0 and 0 or (v > 1 and 1 or v) end local RY = {} for x = 0, W - 1 do local u = max(0, x - PX) / 29 RY[x] = GROUND + (u > 0 and max(0, floor(14 * u ^ 1.3 + (noise(x / 3, 4) - 0.5) * 1.6 + 0.5)) or 0) end local function sky() rect(0, 0, W, H, c3(P.mid)) local s, k = P.sky, 1 for y = 0, HZ - 1 do while k < #s - 1 and y > s[k + 1][1] do k = k + 1 end local a, b = s[k], s[k + 1] local t = clamp01((y - a[1]) / (b[1] - a[1])) rect(0, y, W, 1, floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5), floor(a[4] + (b[4] - a[4]) * t + 0.5)) end end local function valley() for y = HZ, 57 do rect(0, y, W, 1, mix(P.fog, P.haze, min(1, (y - HZ) / 9))) end for x = 0, W - 1 do local ft = 38 + floor(noise(x / 2.2, 11) * 3 + 0.5) - (x % 4 == 1 and 1 or 0) rect(x, ft, 1, 2, mix(P.forest, P.haze, 0.5)) rect(x, ft + 2, 1, 4, mix(P.forest, P.haze, 0.22)) rect(x, ft + 6, 1, 58 - ft - 6, c3(P.forest)) end end local function snow(ya, yb) local SH, MID, LIT = P.sh, P.mid, P.lit for y = ya, yb do for x = 0, W - 1 do local d = y - RY[x] if d >= 0 then local l = 0.3 + 0.3 * (y - GROUND) / 21 l = l + (noise2((x - y * 1.1) / 9, (y + x * 0.35) / 24, 7) - 0.5) * 0.7 local tint = 0 local wt = min(1, (RY[x] - GROUND) / 4) if wt > 0 then local q = (d - 7) / 3.5 l = l + wt * (0.5 * exp(-d / 2.2) - 0.22 * exp(-q ^ 2)) if d < 4 then tint = wt * 0.6 * exp(-d / 1.5) end end if x > 8 and x < PX and y < GROUND + 3 then l = l - 0.3 * (1 - (y - GROUND) / 3) end l = clamp01(l) local r, g, b if l < 0.5 then r, g, b = mix(SH, MID, l * 2) else r, g, b = mix(MID, LIT, (l - 0.5) * 2) end if tint > 0 then r, g, b = floor(r + (P.rim[1] - r) * tint + 0.5), floor(g + (P.rim[2] - g) * tint + 0.5), floor(b + (P.rim[3] - b) * tint + 0.5) end rect(x, y, 1, 1, r, g, b) end end end end local function window_glow() local g, ga = P.glow, P.glowA for y = GROUND, GROUND + 7 do for x = 9, PX - 1 do local d = sqrt(((x - 21) / 12) ^ 2 + ((y - GROUND + 0.5) / 8) ^ 2) if d < 1 then blend(x, y, g[1], g[2], g[3], ga * (1 - d)) end end end end local WIN = { { 13, 36 }, { 22, 36 }, { 28, 36 } } local function cabin() for y = 35, 41 do rect(12, y, 14, 1, c3(y % 2 == 1 and P.wallA or P.wallB)) rect(26, y, 6, 1, c3(y % 2 == 1 and P.wallS or P.wallT)) if y % 2 == 0 then px(11, y, c3(P.wallA)) end if y % 2 == 1 then px(26, y, c3(P.wallS)) end end rect(12, 35, 20, 1, c3(P.roofU)) rect(17, 37, 3, 5, c3(P.door)) for i = 1, #WIN do local w = WIN[i] blend_rect(w[1] - 1, w[2] - 1, 5, 5, P.glow[1], P.glow[2], P.glow[3], 0.3) rect(w[1], w[2], 3, 3, c3(P.win)) px(w[1] + 1, w[2] + 1, c3(P.winC)) rect(w[1] - 1, w[2] + 3, 5, 1, c3(P.treeS)) end for r = 0, 7 do local y = 27 + r local xl, xr = floor(17 - r * 1.5), floor(25 + r * 1.5) for x = xl, xr do local l = 0.15 + 0.85 * (x - xl) / (xr - xl) + (noise2(x / 2, y / 2, 5) - 0.5) * 0.2 if r == 0 then l = 1 end px(x, y, mix(P.roofD, P.roofL, clamp01(l))) end end for x = 6, 35 do if vn(x, 9) > 0.6 then px(x, 34, c3(P.roofU)) end end rect(22, 22, 3, 5, c3(P.stone)) rect(24, 22, 1, 5, mix(P.stone, P.rim, 0.5)) rect(21, 21, 5, 1, c3(P.lit)) for x = 10, 33 do if vn(x, 21) > 0.45 then px(x, 41, mix(P.mid, P.lit, vn(x, 22))) end end end local function spruce(cx, yb, h, hw) local top = yb - h rect(cx, yb - 1, 1, 2, c3(P.trunk)) for r = 0, h - 2 do local y = top + 1 + r local w = max(0, floor(hw * (r + 1) / (h - 1) * 0.8 + (r % 4) * 0.4 + 0.3)) if r % 4 == 0 then rect(cx - w, y, w + 1, 1, c3(P.treeSD)) rect(cx + 1, y, w, 1, c3(P.treeS)) else rect(cx - w, y, w, 1, c3(P.tree)) rect(cx, y, w + 1, 1, c3(P.treeL)) end end px(cx, top, c3(P.treeS)) end local function trees() spruce(5, GROUND + 1, 22, 6) spruce(37, GROUND + 1, 14, 4) spruce(45, RY[45] + 1, 13, 4) spruce(54, RY[54] + 1, 12, 4) spruce(60, RY[60] + 1, 9, 3) end local STAGES = { sky, valley, function() snow(42, 44) end, function() snow(45, 48) end, function() snow(49, 52) end, function() snow(53, 56) end, function() snow(57, 59) end, function() snow(60, 63) end, window_glow, cabin, trees, } local STAGE, BG, PART = 1, nil, nil local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() PART = snapshot(0, 0, W, H) if STAGE == #STAGES then BG = PART end STAGE = STAGE + 1 end function on_settings() P = TIMES[cfg.time] or TIMES.dusk BG, STAGE = nil, 1 end local function box(x, y, w, h, a, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy for j = 0, h do local wy = j == 0 and 1 - fy or (j == h and fy or 1) for i = 0, w do local k = (i == 0 and 1 - fx or (i == w and fx or 1)) * wy * a if k > 0.03 then blend(ix + i, iy + j, r, g, b, k) end end end end local LAYERS = { { 30, 5.5, 1.6, 0.7, 1, 0.5 }, { 18, 11, 2.8, 0.9, 1, 0.8 }, { 8, 22, 4.5, 1.1, 2, 0.85 } } local FLAKES = {} for li = 1, 3 do local L = LAYERS[li] FLAKES[li] = {} for i = 1, L[1] do local h = hash32((li * 64 + i) * 0x85EBCA6B) FLAKES[li][i] = { x = umod(h, 1000) / 1000 * (W + 6), y = umod(h >> 10, 1000) / 1000 * (H + 6), v = L[2] * (0.85 + umod(h >> 20, 30) / 100), ph = umod(h >> 4, 628) / 100 } end end local STARS = {} for i = 1, 22 do local h = hash32(i * 0xC2B2AE35) STARS[i] = { x = umod(h, W), y = umod(h >> 8, 26), ph = umod(h >> 16, 628) / 100, w = 1 + umod(h >> 24, 20) / 10 } end local function mist_row(xl, xr, y, a, c) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end local function cloud(t) local cyc = floor(t / 100) local L = 24 local x = -L + (t % 100) * 1.7 if x > W then return end local y = 5 + umod(hash32(cyc * 0x9E3779B1), 9) local A = P.cloudA mist_row(x + L * 0.25, x + L * 0.75, y, 0.2 * A, P.cloudT) mist_row(x, x + L, y + 1, 0.3 * A, P.cloudT) mist_row(x - 2, x + L + 1, y + 2, 0.32 * A, P.cloudB) mist_row(x + L * 0.1, x + L * 0.9, y + 3, 0.22 * A, P.cloudB) end local SMOKE_N = 6 function render() if not BG then build() if not BG then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) if P.stars > 0 then for i = 1, P.stars do local s = STARS[i] local v = 0.5 + 0.5 * sin(t * s.w + s.ph) blend(s.x, s.y, 236, 238, 255, 0.25 + 0.5 * v) end end cloud(t) local gl = P.glow local fk = 0 for i = 1, #WIN do local w = WIN[i] local dip = umod(hash32((floor(t * 3) + i * 7) * 0x85EBCA6B), 23) == 0 and 0.3 or 0 local f = 0.12 * (0.5 + 0.5 * sin(t * 2.7 + i * 2.1)) + dip fk = fk + f blend_rect(w[1], w[2], 3, 3, P.winDim[1], P.winDim[2], P.winDim[3], f * 1.5) end fk = 1 - fk / 3 local ga = 0.05 * (1 + (fk - 0.9) * 6) for e = 1, 3 do local rx, ry = e * 4 + 2, e * 2 + 1 for dy = 0, ry - 1 do local hw = floor(rx * sqrt(1 - (dy / ry) ^ 2) + 0.5) blend_rect(max(9, 21 - hw), GROUND + dy, min(PX - 1, 21 + hw) - max(9, 21 - hw) + 1, 1, gl[1], gl[2], gl[3], ga) end end local sc = P.smoke for i = 0, SMOKE_N - 1 do local a = (t / 8 + i / SMOKE_N) % 1 local sz = a < 0.45 and 2 or 3 local al = 0.5 * min(1, a * 5) * (1 - a) box(22.5 + a * a * 11 + sin(a * 7 + i * 2) * 0.8, 20 - a * 17, sz, sz, al, sc[1], sc[2], sc[3]) end local lv = cfg.snow or 2 local fc = P.flake for li = 1, 3 do local L = LAYERS[li] local n = floor(L[1] * lv / 3 + 0.5) local fl = FLAKES[li] for i = 1, n do local f = fl[i] local x = (f.x + t * L[3] + sin(t * 0.9 + f.ph) * L[4] * 2) % (W + 6) - 3 local y = (f.y + t * f.v) % (H + 6) - 3 box(x, y, L[5], L[5], L[6], fc[1], fc[2], fc[3]) end end end