info = { type = "image", title = "Overgrown factory", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Mossy ruined hall, light shafts and dripping", api = 44, } local tr = ({ sk = { ["Dust"] = "Prach", ["Dripping"] = "Kvapkanie", ["Moss"] = "Mach" }, cs = { ["Dust"] = "Prach", ["Dripping"] = "Kapání", ["Moss"] = "Mech" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "dust", label = T("Dust"), type = "int", min = 0, max = 5, default = 3 }, { key = "drip", label = T("Dripping"), type = "int", min = 1, max = 3, default = 2 }, { key = "moss", label = T("Moss"), type = "bool", default = true }, } local rect, px, blend_rect, blend, blit_rgb = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend, gfx.blit_rgb local blend_mask = gfx.blend_mask local floor, sin, cos, sqrt, min, max, abs = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local function seg(x0, y0, x1, y1, r, g, b) local n = max(abs(x1 - x0), abs(y1 - y0)) if n == 0 then px(x0, y0, r, g, b); return end for i = 0, n do px(floor(x0 + (x1 - x0) * i / n + 0.5), floor(y0 + (y1 - y0) * i / n + 0.5), r, g, b) end end local function ellipse_rows(cx, cy, rx, ry, fn) for dy = -ry, ry do local dx = floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) fn(cx - dx, cy + dy, 2 * dx + 1, dy) end end local function s_roof_wall() gfx.clear(20, 24, 24) for y = 10, 43 do local k = (y - 10) / 33 rect(0, y, W, 1, floor(86 + 40 * k), floor(98 + 40 * k), floor(90 + 30 * k)) end rect(0, 0, W, 10, 30, 34, 34) local hole = { { 28, 58 }, { 29, 57 }, { 30, 55 }, { 31, 53 }, { 32, 51 }, { 34, 49 }, { 36, 47 } } for i = 1, #hole do local k = (i - 1) / 6 rect(hole[i][1], i - 1, hole[i][2] - hole[i][1] + 1, 1, floor(232 - 40 * k), floor(242 - 26 * k), floor(246 - 14 * k)) end rect(0, 9, W, 2, 24, 28, 28); rect(0, 11, W, 1, 14, 16, 16) for _, bx in ipairs({ 12, 52 }) do rect(bx, 0, 2, 9, 22, 26, 26) end for i = 0, 1 do seg(0, 12 + i, 26, 2 + i, 22, 26, 26) seg(62, 10 + i, 40, 2 + i, 22, 26, 26) end seg(0, 12, 26, 2, 52, 58, 58) for _, t in ipairs({ { 34, 3 }, { 40, 2 }, { 46, 4 }, { 52, 2 } }) do rect(t[1], 7, 1, t[2], 26, 30, 30) end end local function window(x, y, w, h) rect(x - 1, y - 1, w + 2, h + 2, 34, 38, 38) for r = 0, h - 1 do local k = r / (h - 1) rect(x, y + r, w, 1, floor(196 - 70 * k), floor(222 - 56 * k), floor(184 - 64 * k)) end blend_rect(x + 1, y + h - 6, w - 2, 6, 52, 92, 44, 0.75) rect(x + w // 2, y, 1, h, 34, 38, 38) rect(x, y + h // 3, w, 1, 34, 38, 38); rect(x, y + 2 * h // 3, w, 1, 34, 38, 38) rect(x + 1, y + 1, w // 2 - 1, h // 3 - 1, 30, 34, 34) for i = 0, w - 1, 2 do local l = 2 + (hash32(i * 7 + x) & 3) * 2 rect(x + i, y, 1, l, 52, 96, 44) end rect(x - 2, y + h + 1, w + 4, 1, 60, 66, 62) end local function s_windows_machines() window(23, 15, 10, 14) window(50, 15, 9, 14) for x = 3, 20 do local u = (x - 3) / 17 local b = 0.5 + 0.5 * cos((u - 0.62) * 2.6) rect(x, 26, 1, 21, floor(142 * b), floor(162 * b), floor(154 * b)) end rect(5, 22, 14, 1, 84, 100, 96); rect(4, 23, 16, 1, 92, 108, 104); rect(3, 24, 18, 2, 100, 116, 112) rect(2, 46, 20, 2, 46, 54, 52) for _, y in ipairs({ 30, 37, 42 }) do rect(3, y, 18, 1, 38, 46, 44) end blend_rect(8, 30, 1, 8, 150, 84, 40, 0.45); blend_rect(13, 31, 2, 12, 150, 84, 40, 0.4); blend_rect(17, 36, 1, 10, 150, 84, 40, 0.45) rect(22, 27, 1, 20, 46, 54, 52); rect(25, 27, 1, 20, 46, 54, 52) for y = 29, 45, 3 do rect(22, y, 4, 1, 52, 60, 58) end rect(20, 31, 44, 3, 68, 84, 78); rect(20, 31, 44, 1, 112, 130, 118); rect(20, 33, 44, 1, 36, 44, 40) rect(30, 30, 2, 5, 40, 46, 44); rect(56, 30, 2, 5, 40, 46, 44) rect(45, 10, 3, 21, 68, 84, 78); rect(47, 10, 1, 21, 112, 130, 118); rect(45, 10, 1, 21, 36, 44, 40) rect(44, 29, 5, 2, 52, 62, 58) rect(45, 34, 3, 3, 52, 62, 58); rect(45, 36, 3, 1, 86, 74, 52) rect(52, 36, 10, 12, 54, 62, 62); rect(52, 36, 10, 1, 86, 96, 94); rect(54, 38, 6, 4, 30, 36, 36) px(55, 39, 120, 44, 32); rect(54, 44, 6, 1, 36, 42, 42); rect(54, 46, 6, 1, 36, 42, 42) end local function s_floor() for y = 44, 63 do local k = (y - 44) / 19 rect(0, y, W, 1, floor(110 - 56 * k), floor(114 - 58 * k), floor(104 - 56 * k)) end rect(0, 43, W, 1, 40, 44, 42) seg(10, 50, 18, 57, 40, 44, 42); seg(34, 46, 40, 49, 44, 48, 46); seg(52, 52, 58, 58, 36, 40, 38) rect(28, 47, 3, 2, 70, 68, 62); rect(30, 46, 2, 1, 110, 108, 98); rect(56, 50, 4, 2, 66, 64, 58) ellipse_rows(42, 56, 17, 5, function(x, y, w) rect(x, y, w, 1, 44, 50, 50) end) ellipse_rows(42, 56, 15, 4, function(x, y, w, dy) local k = (dy + 4) / 8 rect(x, y, w, 1, floor(112 - 40 * k), floor(138 - 44 * k), floor(148 - 46 * k)) end) ellipse_rows(43, 55, 8, 1, function(x, y, w) rect(x, y, w, 1, 196, 220, 230) end) rect(45, 57, 2, 3, 60, 72, 68) end local LAT = {} local function lattice(ix, iy) local k = iy * 64 + ix local v = LAT[k] if not v then v = (hash32(ix * 0x9E3779B1 + iy * 0x85EBCA6B + 3) & 255) / 255; LAT[k] = v end return v end local function noise(x, y, cell) local fx, fy = x / cell, y / cell local ix, iy = floor(fx), floor(fy) local tx, ty = fx - ix, fy - iy tx, ty = tx * tx * (3 - 2 * tx), ty * ty * (3 - 2 * ty) local a = lattice(ix, iy) * (1 - tx) + lattice(ix + 1, iy) * tx local b = lattice(ix, iy + 1) * (1 - tx) + lattice(ix + 1, iy + 1) * tx return a * (1 - ty) + b * ty end local function moss_weight(x, y) local w = 0 if y >= 34 and x >= 3 and x <= 20 and y <= 47 then w = 0.9 end if y >= 40 then w = max(w, 0.35 + 0.65 * min(1, (y - 40) / 8)) end if y >= 30 and y <= 32 then w = max(w, 0.8) end if y >= 34 and y < 40 then w = max(w, 0.4) end return w end local MOSS_ROWS = {} local ZERO_ROW = string.rep("\0", 32) local function moss_rows(seed, cy0, cy1) for cy = cy0, cy1 do local row = {} for cx = 0, 31 do local w = moss_weight(cx * 2, cy * 2) local v = w > 0 and (noise(cx + seed, cy * 1.3 + seed, 5) - 0.42) * 2.4 * w or 0 row[cx + 1] = v <= 0 and 0 or min(255, floor(v * 255)) end MOSS_ROWS[cy + 1] = string.char(table.unpack(row)) end end local function moss_pass(seed, cr, cg, cb, alpha, part) if cfg.moss == false then return end if part == 1 then for cy = 0, 14 do MOSS_ROWS[cy + 1] = ZERO_ROW end moss_rows(seed, 15, 23) else moss_rows(seed, 24, 31) blend_mask(table.concat(MOSS_ROWS), cr, cg, cb, 0, 0, W, H, 32, 32, alpha) end end local SHAFTS = { { 34, 6 }, { 42, 4 }, { 48, 6 } } local SLOPE = 0.52 local function s_light() for _, s in ipairs(SHAFTS) do for y = 7, 61 do local f = (1 - (y - 7) / 90) * min(1, (y - 6) / 6) local x0 = s[1] - floor((y - 7) * SLOPE + 0.5) blend_rect(x0 - 1, y, s[2] + 2, 1, 255, 240, 196, 0.10 * f) blend_rect(x0, y, s[2], 1, 255, 240, 196, 0.17 * f) blend_rect(x0 + 1, y, s[2] - 2, 1, 255, 240, 196, (y >= 44 and 0.28 or 0.15) * f) end end for y = 44, 62 do blend_rect(0, y, W, 1, 196, 210, 204, 0.006 * (y - 40)) end end local function s_foreground() rect(0, 0, 4, H, 14, 18, 18); rect(3, 0, 1, H, 34, 40, 38) rect(60, 0, 4, H, 14, 18, 18); rect(60, 0, 1, H, 34, 40, 38) for _, b in ipairs({ { 26, 0, 4, 3 }, { 56, 0, 4, 2 }, { 58, 2, 2, 2 }, { 30, 3, 2, 2 }, { 54, 5, 3, 1 } }) do rect(b[1], b[2], b[3], b[4], 58, 100, 46) px(b[1], b[2], 90, 134, 66) end for _, vx in ipairs({ 14, 18, 52, 56 }) do local len = 8 + (hash32(vx) & 7) for i = 0, len do local x = vx + floor(sin(i * 0.6 + vx) + 0.5) px(x, 12 + i, 34, 72, 36) if i % 3 == 1 then px(x + 1, 12 + i, 62, 108, 52); px(x - 1, 13 + i, 48, 90, 44) end end end local function fern(bx, by, dir, len) for i = 0, len do local x = bx + dir * floor(i * 0.7 + (i * i) * 0.03) local y = by - i px(x, y, 22, 50, 30); px(x + 1, y, 22, 50, 30) if i > len - 4 then px(x, y, 60, 104, 50) end if i % 3 == 0 and i > 2 then px(x - dir, y + 1, 40, 80, 40); px(x + dir * 2, y, 40, 80, 40) end end end fern(6, 63, 1, 10); fern(8, 63, 1, 7); fern(5, 63, -1, 6); fern(10, 63, 1, 12) fern(57, 63, -1, 11); fern(56, 63, -1, 7); fern(59, 63, 1, 5) end local GR_ROWS = {} local function grain_rows(y0, y1) for y = y0, y1 do local row = {} for x = 1, W do row[x] = hash32(y * 0x9E3779B1 + x * 0x85EBCA6B) & 255 end GR_ROWS[y + 1] = string.char(table.unpack(row)) end end local function s_grain() local gr = table.concat(GR_ROWS) blend_mask(gr, 0, 0, 0, 0, 0, W, H, W, H, 0.12) blend_mask(gr, 255, 250, 235, 1, 1, W - 2, H - 2, W, H, 0.05) end local function s_vignette() local rows = {} for y = 0, 15 do local row = {} for x = 0, 15 do local d = ((x - 7.5) / 8) ^ 2 + ((y - 7.5) / 8) ^ 2 row[x + 1] = max(0, min(255, floor(255 * (d - 0.3) / 1.2))) end rows[y + 1] = string.char(table.unpack(row)) end blend_mask(table.concat(rows), 0, 0, 0, 0, 0, W, H, 16, 16, 0.4) end local STEPS = { s_roof_wall, s_windows_machines, s_floor, function() moss_pass(0, 52, 92, 40, 0.9, 1) end, function() moss_pass(0, 52, 92, 40, 0.9, 2) end, function() moss_pass(40, 110, 148, 66, 0.55, 1) end, function() moss_pass(40, 110, 148, 66, 0.55, 2) end, s_light, s_foreground, function() grain_rows(0, 21) end, function() grain_rows(22, 42) end, function() grain_rows(43, 63) end, s_grain, s_vignette, } local STEP, ACC, BG = 1, nil, nil function on_settings() STEP, ACC, BG = 1, nil, nil end local N_DUST = 22 local DX, DY, DP, DS = {}, {}, {}, {} for i = 1, N_DUST do local h = hash32(i * 0x9E3779B1) local s = SHAFTS[1 + i % 3] local y0 = 8 + (h >> 4 & 0x3F) % 48 DY[i] = y0 DX[i] = s[1] - (y0 - 7) * SLOPE + (h >> 10 & 0xF) % (s[2] + 1) DP[i] = (h >> 14 & 0x3FF) / 100 DS[i] = 0.35 + (h >> 20 & 7) / 10 end local function in_shaft(x, y) for i = 1, 3 do local s = SHAFTS[i] local x0 = s[1] - (y - 7) * SLOPE if x >= x0 - 1 and x <= x0 + s[2] + 1 then return true end end return false end local DRIP_X = 46 local function ring(cx, cy, r, a) local ry = r * 0.32 for dx = -r, r, 1 do local dy = ry * sqrt(1 - (dx / r) ^ 2) local iy = floor(dy + 0.5) blend(cx + dx, cy - iy, 206, 226, 234, a) if iy > 0 then blend(cx + dx, cy + iy, 206, 226, 234, a * 0.7) end end if ry >= 1 then blend(cx - r, cy, 206, 226, 234, a); blend(cx + r, cy, 206, 226, 234, a) end end local function dynamic(frame) local nd = (cfg.dust or 3) * 22 // 5 for i = 1, min(nd, N_DUST) do local t = frame * 0.02 * DS[i] local x = DX[i] + 3 * sin(t + DP[i]) - frame * 0.012 * DS[i] local y = 8 + (DY[i] - 8 + frame * 0.03 * DS[i]) % 50 local lit = in_shaft(x, y) local a = (lit and 0.62 or 0.2) * (0.6 + 0.4 * sin(frame * 0.05 + DP[i] * 3)) local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi local cr, cg, cb = 255, 246, 214 blend(xi, yi, cr, cg, cb, a * (1 - fx) * (1 - fy)) blend(xi + 1, yi, cr, cg, cb, a * fx * (1 - fy)) blend(xi, yi + 1, cr, cg, cb, a * (1 - fx) * fy) blend(xi + 1, yi + 1, cr, cg, cb, a * fx * fy) end local P = ({ 70, 44, 28 })[cfg.drip or 2] local p = frame % P local TIP = 37 if p < 10 then local g = p / 10 px(DRIP_X, TIP + 1, 150, 172, 180) if g > 0.5 then px(DRIP_X, TIP + 2, 196, 216, 226) end else local dt = p - 10 local y = TIP + 2 + 0.045 * dt * dt if y < 55 then px(DRIP_X, floor(y + 0.5), 206, 226, 236) blend(DRIP_X, floor(y - 1 + 0.5), 206, 226, 236, 0.5) else local hit = sqrt((55 - TIP - 2) / 0.045) + 10 local age = p - hit for k = 0, 1 do local a = age - k * 7 if a >= 0 and a < 22 then local r = 1 + floor(a * 0.45) ring(DRIP_X, 56, r, 0.55 * (1 - a / 22)) end end end end if p < 22 then local a = p + P - sqrt((55 - TIP - 2) / 0.045) - 10 for k = 0, 1 do local aa = a - k * 7 if aa >= 0 and aa < 22 then ring(DRIP_X, 56, 1 + floor(aa * 0.45), 0.55 * (1 - aa / 22)) end end end for i = 1, 3 do local a = 0.5 * max(0, sin(frame * 0.07 + i * 2.1)) blend(32 + i * 6, 54 + i % 2, 230, 240, 244, a) end end function render() if STEP <= #STEPS then if ACC then blit_rgb(ACC, 0, 0, W, H) end STEPS[STEP]() STEP = STEP + 1 ACC = gfx.snapshot(0, 0, W, H) if STEP > #STEPS then BG = ACC end gfx.ready(false) return end blit_rgb(BG, 0, 0, W, H) dynamic(frame) end