info = { type = "image", title = "Ruin city", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Overgrown ruins, small fire and drifting fog", api = 44, } local tr = ({ sk = { ["Fog"] = "Hmla", ["Ash"] = "Popol", ["Window lights"] = "Svetlá v oknách", ["Smoke and sparks"] = "Dym a iskry" }, cs = { ["Fog"] = "Mlha", ["Ash"] = "Popel", ["Window lights"] = "Světla v oknech", ["Smoke and sparks"] = "Kouř a jiskry" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fog", label = T("Fog"), type = "bool", default = true }, { key = "ash", label = T("Ash"), type = "int", min = 0, max = 16, default = 8 }, { key = "lights", label = T("Window lights"), type = "bool", default = true }, { key = "embers", label = T("Smoke and sparks"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local FX, FY = 40, 57 local function R(i, m) return umod(hash32(i * 0x9E3779B1), m) end local function C(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function span(xl, xr, y, r, g, b, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend(il, y, r, g, b, a * (1 - (xl - il))) if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, r, g, b, a) end blend(ir, y, r, g, b, a * (xr - ir)) end local LIT = {} local function moss(x, y, w, seed) rect(x, y, w, 2, 124, 152, 66) rect(x + 1, y + 2, w - 2, 1, 88, 116, 50) for k = 0, w - 1 do local r = R(seed * 31 + k, 10) if r < 3 then rect(x + k, y + 3, 1, 2 + r, 88, 116, 50) end if r == 9 then px(x + k, y - 1, 124, 152, 66) end end end local function building(x, y, w, h, wall, slits, seed) rect(x, y, w, h, wall[1], wall[2], wall[3]) rect(x, y, 1, h, C(wall[1] + 14, wall[2] + 14, wall[3] + 14)) rect(x + w - 3, y, 3, h, C(wall[1] - 26, wall[2] - 26, wall[3] - 24)) rect(x, y, w, 1, C(wall[1] + 22, wall[2] + 22, wall[3] + 22)) for wy = y + 4, y + h - 6, 4 do if slits then rect(x + 3, wy, w - 9, 2, 40, 42, 46) if R(seed + wy, 5) == 0 then LIT[#LIT + 1] = { x + 3 + R(seed + wy * 7, w - 11), wy } end else for wx = x + 2, x + w - 6, 4 do local r = R(seed * 131 + wy * 17 + wx, 10) if r == 0 then rect(wx, wy, 2, 2, 22, 24, 28) else rect(wx, wy, 2, 2, 52, 54, 58) end if r == 1 or r == 2 then LIT[#LIT + 1] = { wx, wy } end end end end end local function st_far() for y = 0, 55 do rect(0, y, W, 1, C(44 + y * 0.66, 46 + y * 0.66, 52 + y * 0.6)) end local x = 0 local i = 0 while x < W do i = i + 1 local w, h = 5 + R(i, 7), 24 + R(i + 50, 16) rect(x, 56 - h, w, h, 66, 68, 72) rect(x, 56 - h, w, 1, 78, 80, 84) x = x + w end end local function st_near() building(0, 14, 16, 44, { 112, 114, 116 }, false, 1) building(17, 32, 13, 26, { 100, 102, 100 }, false, 2) building(31, 10, 17, 48, { 96, 98, 100 }, true, 3) building(49, 26, 15, 32, { 108, 110, 110 }, false, 4) rect(40, 10, 8, 3, 66, 68, 72); rect(44, 13, 4, 2, 66, 68, 72) moss(0, 14, 13, 1); moss(17, 32, 11, 2); moss(31, 10, 8, 3); moss(49, 26, 12, 4) for y = 14, 57 do blend_rect(0, y, W, 1, 30, 32, 36, (y - 14) * 0.0055) end end local function st_street() for y = 58, H - 1 do rect(0, y, W, 1, C(54 - (y - 58) * 3, 54 - (y - 58) * 3, 56 - (y - 58) * 3)) end rect(0, 58, W, 1, 70, 72, 74) for i = 1, 9 do local x = R(i, 58) rect(x, 59 + R(i + 9, 4), 3 + R(i + 20, 3), 2, 78, 80, 82) px(x + 1, 59 + R(i + 9, 4), 120, 148, 66) end rect(FX - 3, FY - 1, 8, 5, 74, 52, 40) rect(FX - 3, FY - 1, 8, 1, 104, 76, 56); rect(FX - 3, FY + 1, 8, 1, 50, 34, 28) rect(FX + 3, FY - 1, 2, 5, 52, 36, 30) end local STAGES = { st_far, st_near, st_street } local stage, BG = 0, nil local function flame_row(cx, y, hw, r, g, b) rect(cx - hw, y, 2 * hw + 1, 1, r, g, b) end local function draw_fire(t) local n = frame // 3 for k = -1, 1 do local h = 6 + R(n * 3 + k + 7, 3) + (k == 0 and 2 or 0) local cx = FX + k * 2 + (R(n + k * 5, 3) - 1) // 1 for r = 0, h - 1 do local hw = floor((h - r) * 0.34 + 0.5) if r >= h - 2 then hw = 0 end local y = FY - 1 - r local c = r < h * 0.35 and 0 or (r < h * 0.7 and 1 or 2) if c == 0 then flame_row(cx, y, hw + 1, 252, 172, 44) else flame_row(cx, y, hw, 230, 96, 22) end end end rect(FX - 1, FY - 3, 3, 3, 255, 236, 150) rect(FX, FY - 5, 1, 2, 255, 236, 150) end local function draw_glow() local f = 0.8 + 0.2 * sin(frame * 0.9) * sin(frame * 0.37) for y = 36, 57 do local a = 0.13 * (1 - (57 - y) / 22) * f blend_rect(31, y, 17, 1, 255, 140, 40, a) end blend_rect(30, 58, 22, 2, 255, 140, 40, 0.14 * f) end local function draw_smoke() for i = 0, 7 do local life = 90 local age = (frame + i * 11) % life local c = (frame + i * 11) // life local x = FX + (R(i * 7 + c, 5) - 2) + age * 0.07 + 1.6 * sin(age * 0.09 + i) local y = FY - 8 - age * 0.34 local a = 0.42 * (1 - age / life) local w = age > 28 and 3 or 2 local ix, iy = floor(x), floor(y) local f = y - iy blend_rect(ix, iy, w, w, 62, 62, 66, a * (1 - f)) blend_rect(ix, iy + 1, w, w, 62, 62, 66, a * f) end end local function draw_sparks() for i = 0, 5 do local life = 26 + i * 3 local c, age = (frame + i * 13) // life, (frame + i * 13) % life local vx = (R(c * 11 + i, 100) - 50) / 70 local vy = 0.9 + R(c * 5 + i * 3, 60) / 100 local x = FX + vx * age local y = FY - 8 - vy * age + 0.035 * age * age local a = 1 - age / life blend(floor(x + 0.5), floor(y + 0.5), 255, 196 - 90 * (1 - a), 60, 0.3 + 0.7 * a) end end local function draw_ash(n, t) for i = 1, n do local v = 4 + R(i, 50) / 10 local y = (R(i + 100, H) + v * t) % (H + 4) - 2 local x = floor(R(i + 200, W) + 2 * sin(t * 0.4 + i) + 0.5) local iy = floor(y) local f = y - iy blend(x, iy, 206, 204, 198, 0.55 * (1 - f)) blend(x, iy + 1, 206, 204, 198, 0.55 * f) end end local FOGY, FOGL, FOGV, FOGX = { 22, 36, 47 }, { 44, 52, 40 }, { 2.4, 3.6, 1.8 }, { 4, 30, 50 } local function draw_fog(t) for i = 1, 3 do local len = FOGL[i] local xl = (FOGX[i] + FOGV[i] * t) % (W + len) - len local y = FOGY[i] span(xl + 4, xl + len - 4, y, 150, 154, 158, 0.10) span(xl, xl + len, y + 1, 150, 154, 158, 0.2) span(xl + 3, xl + len - 3, y + 2, 150, 154, 158, 0.14) span(xl + 8, xl + len - 8, y + 3, 150, 154, 158, 0.08) end end local function draw_lights() for i = 1, #LIT do local per = 160 + R(i, 240) local ph = (frame + R(i + 40, 400)) % per local len = 18 + R(i + 80, 40) if ph < len and not (ph < 6 and ph % 3 == 1) then local w = LIT[i] rect(w[1], w[2], 2, 2, 226, 156, 66) end end end function render() if stage < #STAGES then if BG then gfx.blit_rgb(BG, 0, 0, W, H) else gfx.clear(60, 62, 66) end stage = stage + 1 STAGES[stage]() BG = gfx.snapshot(0, 0, W, H) if stage < #STAGES then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS if cfg.lights ~= false then draw_lights() end draw_glow() if cfg.fog ~= false then draw_fog(t) end draw_fire(t) if cfg.embers ~= false then draw_smoke(); draw_sparks() end draw_ash(cfg.ash or 8, t) end