info = { type = "image", title = "Foggy alley", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bare trees in fog over wet cobblestones", api = 44, } local tr = ({ sk = { ["Fog"] = "Hmla", ["Season"] = "Ročné obdobie", ["Bare"] = "Holé", ["Autumn"] = "Jeseň", ["Light"] = "Svetlo", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, cs = { ["Fog"] = "Mlha", ["Season"] = "Roční období", ["Bare"] = "Holé", ["Autumn"] = "Podzim", ["Light"] = "Světlo", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fog", label = T("Fog"), type = "int", min = 1, max = 5, default = 3 }, { key = "season", label = T("Season"), type = "int", default = "bare", opts = { keys = { "bare", "autumn" }, T("Bare"), T("Autumn") } }, { key = "light", label = T("Light"), type = "int", default = "warm", opts = { keys = { "warm", "cold" }, T("Warm"), T("Cold") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local VX, HY = 33, 35 local SX, SY = 34, 11 local LIGHTS = { warm = { skyT = { 118, 122, 148 }, skyH = { 206, 194, 196 }, sun = { 255, 240, 214 }, glow = { 255, 228, 186 }, fog = { 200, 192, 198 }, grF = { 176, 170, 176 }, grN = { 46, 44, 56 }, shaft = { 255, 230, 196 }, tree = { 36, 34, 46 }, lit = { 104, 94, 100 }, pud = { 214, 204, 204 } }, cold = { skyT = { 112, 124, 156 }, skyH = { 190, 198, 218 }, sun = { 236, 244, 255 }, glow = { 214, 228, 255 }, fog = { 186, 194, 212 }, grF = { 168, 174, 190 }, grN = { 42, 46, 62 }, shaft = { 226, 236, 255 }, tree = { 32, 34, 48 }, lit = { 88, 96, 118 }, pud = { 200, 210, 230 } }, } local P, AUT = LIGHTS.warm, false local function T3(c) return c[1], c[2], c[3] end local function BR(x, y, w, h, c, a) blend_rect(x, y, w, h, c[1], c[2], c[3], a) 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 BG, PART, STAGE = nil, nil, 1 local TB = {} local PUD = { { 24, 58, 16 }, { 47, 50, 10 }, { 22, 45, 7 } } local function st_sky() for y = 0, HY do rect(0, y, W, 1, mix(P.skyT, P.skyH, (y / HY) ^ 1.2)) end local R = 22 local n = 2 * R + 1 local rows, row = {}, {} for gy = 0, 2 * R do for gx = 0, 2 * R do local d = sqrt((gx - R) ^ 2 + (gy - R) ^ 2) row[gx + 1] = d < R and floor(255 * (1 - d / R) * 1.0) or 0 end rows[gy + 1] = string.char(table.unpack(row, 1, n)) end blend_mask(table.concat(rows), P.glow[1], P.glow[2], P.glow[3], SX - R, SY - R, n, n, n, n, 1) blend_rect(SX - 3, SY - 3, 7, 7, P.sun[1], P.sun[2], P.sun[3], 0.4) rect(SX - 1, SY - 1, 3, 3, T3(P.sun)) end local function st_ground() for y = HY, H - 1 do rect(0, y, W, 1, mix(P.grF, P.grN, ((y - HY) / (H - 1 - HY)) ^ 0.8)) end for y = HY + 1, H - 1 do local e = floor(0.83 * (y - HY) + 0.5) blend_rect(0, y, VX - e, 1, 24, 22, 30, 0.24) blend_rect(VX + e + 1, y, W - VX - e - 1, 1, 24, 22, 30, 0.24) blend_rect(VX - e, y, 1, 1, 214, 210, 220, 0.4) blend_rect(VX + e, y, 1, 1, 214, 210, 220, 0.4) end local z = 1.04 while z < 14 do local y = floor(HY + 24 / z) local e = floor(0.83 * (y - HY)) if y > HY + 1 and y < H then blend_rect(VX - e, y, 2 * e + 1, 1, 26, 24, 34, 0.1 + 0.3 * (y - HY) / 28) end z = z * 1.2 end for y = HY + 5, H - 1 do local e = 0.83 * (y - HY) local a = 0.08 + 0.2 * (y - HY) / 28 for k = -3, 3 do blend_rect(floor(VX + k * e / 4 + 0.5), y, 1, 1, 26, 24, 34, a) end end if AUT then for i = 1, 36 do local h = hash32(i * 0x9E3779B1) local y = HY + 3 + umod(h, 28) local x = VX + (umod(h >> 8, 100) / 50 - 1) * (0.9 * (y - HY) + 6) blend_rect(floor(x), y, 1 + umod(h >> 16, 2), 1, 176, 104, 44, 0.6) end end end local function leaves(x, y, h) local c = umod(h, 2) == 0 and 196 or 150 px(floor(x + 0.5), floor(y + 0.5), c, floor(c * 0.6), 44) px(floor(x + 0.5) + 1 - umod(h >> 4, 3), floor(y + 0.5) + 1, 176, 108, 48) end local function limb(x, y, ang, len, dep, hh, w) local dx, dy = sin(ang), -cos(ang) local n = max(1, floor(len + 0.5)) local c = P.tree for k = 1, n do local cx, cy = floor(x + dx * k + 0.5), floor(y + dy * k + 0.5) if w > 1 then rect(cx - w // 2, cy, w, 1, T3(c)) else px(cx, cy, T3(c)) end end local ex, ey = x + dx * n, y + dy * n local h2 = hash32(hh + dep * 0x632BE5AB + n) if dep > 0 then local sp = 0.45 + umod(h2, 30) / 100 limb(ex, ey, ang - sp, len * 0.72, dep - 1, h2, 1) limb(ex, ey, ang + sp * (0.7 + umod(h2 >> 6, 40) / 100), len * 0.72, dep - 1, h2 >> 1, 1) if dep >= 2 then limb(ex, ey, ang + (umod(h2 >> 12, 20) - 10) / 100, len * 0.85, dep - 1, h2 >> 2, 1) end elseif AUT then leaves(ex, ey, h2) end end local function tree(i, sgn, z) local hh = hash32(i * 0x85EBCA6B) local bx = VX + sgn * (31 + (umod(hh, 100) / 100 - 0.5) * 6) / z local yb = floor(HY + 24 / z + 0.5) local h = 100 / z local w = max(1, floor(6 / z + 0.5)) local cs = h * 0.45 local lean = (umod(hh >> 8, 100) / 100 - 0.5) * 0.12 local x0 = floor(bx - w / 2 + 0.5) rect(x0, floor(yb - cs), w, floor(cs) + 1, T3(P.tree)) if w >= 3 then rect(sgn < 0 and x0 + w - 1 or x0, floor(yb - cs), 1, floor(cs) + 1, T3(P.lit)) end TB[#TB + 1] = { bx, yb, w, h } local dep = z < 3.5 and 3 or (z < 8 and 2 or 1) limb(bx, yb - cs, lean, h * 0.2 + 1, dep, hh, max(1, w - 1)) end local ZR = { 26, 15, 10, 6.8, 4.6, 3.1, 2.1, 1.4 } local ZL = { 22, 16, 11, 7.4, 5.0, 3.4, 2.3, 1.55 } local function group(g) tree(g * 2, 1, ZR[g]) tree(g * 2 + 1, -1, ZL[g]) local yb = floor(HY + 24 / ZR[g] + 1.5) BR(0, 0, W, yb, P.fog, 0.27) end local function st_fin() local W4 = { 1, 0.72, 0.48, 0.26 } for y = SY + 5, H - 1 do local hw = 3 + (y - SY) * 0.24 local cx = SX - (y - SY) * 0.03 local a = y > HY and 0.12 or 0.07 for k = 1, 4 do local w = floor(hw * W4[k]) BR(floor(cx) - w, y, 2 * w, 1, P.shaft, a) end end for i = 1, #PUD do local p = PUD[i] local x, y, w = p[1] - p[3] // 2, p[2], p[3] BR(x + 1, y - 1, w - 2, 1, P.pud, 0.4) BR(x, y, w, 1, P.pud, 0.5) BR(x + 1, y + 1, w - 2, 1, P.pud, 0.4) end for i = 1, #TB do local b = TB[i] if b[2] > HY + 5 then local len = min(10, floor(b[4] * 0.22)) BR(floor(b[1] - b[3] / 2 + 0.5), b[2] + 1, b[3], len, P.tree, 0.3) end end BG = snapshot(0, 0, W, H) end local STAGES = { st_sky, st_ground, function() group(1); group(2); group(3) end, function() group(4); group(5) end, function() group(6) end, function() group(7) end, function() group(8) end, st_fin, } function on_settings() P = LIGHTS[cfg.light] or LIGHTS.warm AUT = cfg.season == "autumn" BG, STAGE, TB = nil, 1, {} end local BANDS = {} for i = 1, 8 do local h = hash32(i * 0x27D4EB2F) BANDS[i] = { y = 30 + umod(h, 20), len = 22 + umod(h >> 6, 18), x0 = umod(h >> 10, 100) * 0.9, v = (1.2 + umod(h >> 16, 18) / 10) * (umod(h >> 20, 2) * 2 - 1) } end local GL = {} for i = 1, 12 do local h = hash32(i * 0xC2B2AE35) local y = HY + 5 + umod(h, 23) GL[i] = { y = y, dx = (umod(h >> 8, 100) / 100 - 0.5) * (0.7 * (y - HY)), w = 1.4 + umod(h >> 16, 8) * 0.25, ph = umod(h >> 10, 628) / 100 } end local function band_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end local f = P.fog blend_rect(il + 1, y, ir - il - 1, 1, f[1], f[2], f[3], a) blend_rect(il, y, 1, 1, f[1], f[2], f[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, f[1], f[2], f[3], a * (xr - ir)) end local function ring(cx, cy, r, maxr, a) if r > maxr then return end local ry = r * 0.34 local c = P.pud local w = max(1, floor(r * 1.2 + 0.5)) local yt, yb = floor(cy - ry + 0.5), floor(cy + ry + 0.5) blend_rect(floor(cx - r * 0.6 + 0.5), yt, w, 1, c[1], c[2], c[3], a) blend_rect(floor(cx - r * 0.6 + 0.5), yb, w, 1, c[1], c[2], c[3], a) blend_rect(floor(cx - r + 0.5), floor(cy + 0.5), 1, 1, c[1], c[2], c[3], a) blend_rect(floor(cx + r + 0.5), floor(cy + 0.5), 1, 1, c[1], c[2], c[3], a) end function render() if not BG then if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then PART = snapshot(0, 0, W, H) end if not BG then gfx.ready(false) end return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local sh = P.shaft blend_rect(SX - 2, SY - 2, 5, 5, sh[1], sh[2], sh[3], 0.1 + 0.07 * sin(t * 0.9)) local fog = cfg.fog or 3 local ka = 0.05 + 0.045 * fog for i = 1, 2 + fog do local b = BANDS[i] local L = b.len local x = (b.x0 + t * b.v) % (W + 2 * L) - L band_row(x + L * 0.25, x + L * 0.75, b.y, ka * 0.8) band_row(x, x + L, b.y + 1, ka) band_row(x - L * 0.1, x + L * 1.1, b.y + 2, ka * 0.9) band_row(x + L * 0.15, x + L * 0.85, b.y + 3, ka * 0.6) end for i = 1, #GL do local g = GL[i] local v = sin(t * g.w + g.ph) if v > 0.3 then blend_rect(floor(SX - (g.y - SY) * 0.03 + g.dx + 0.5), g.y, 2, 1, sh[1], sh[2], sh[3], (v - 0.3) * 0.95) end end local k = floor(t / 2.7) local tau = t - k * 2.7 local p = PUD[1 + umod(hash32(k * 0x9E3779B1) >> 3, 3)] if tau < 0.2 then blend_rect(p[1], floor(p[2] - 15 + tau / 0.2 * 15), 1, 2, 255, 255, 255, 0.6) else local u = tau - 0.2 ring(p[1], p[2], 0.8 + u * 7, p[3] * 0.5, 0.6 * max(0, 1 - u / 1.1)) if u > 0.3 then ring(p[1], p[2], 0.8 + (u - 0.3) * 7, p[3] * 0.5, 0.45 * max(0, 1 - (u - 0.3) / 1.1)) end end if AUT then local u = (t % 12) / 12 local x, y = 6 + 50 * u + 5 * sin(u * 15), 4 + 46 * u local ix = floor(x) local f = x - ix local yy = floor(y + 0.5) blend_rect(ix, yy, 1, 1, 190, 112, 48, 0.9 * (1 - f)) blend_rect(ix + 1, yy, 1, 1, 190, 112, 48, 0.9) blend_rect(ix + 2, yy, 1, 1, 190, 112, 48, 0.9 * f) end end