info = { type = "image", title = "Lava slope", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lava runs down a night volcano, smoke rises", api = 33, } local tr = ({ sk = { ["Lava bombs"] = "Lávové bomby", ["Lava colour"] = "Farba lávy", off = "žiadne", rare = "zriedka", often = "často" }, cs = { ["Lava bombs"] = "Lávové bomby", ["Lava colour"] = "Barva lávy", off = "žádné", rare = "zřídka", often = "často" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bombs", label = T("Lava bombs"), type = "int", default = 2, opts = { T("off"), T("rare"), T("often") } }, { key = "lava", label = T("Lava colour"), type = "color", default = "#FF7A1A" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, ceil, sin, sqrt, abs, max, min = math.floor, math.ceil, math.sin, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PX, PY, GROUND = 24, 19, 56 local S = {} for x = 0, W - 1 do local d = x - PX local y if d >= 4 then local e = d - 4; y = PY + 0.45 * e + 0.0095 * e * e elseif d <= -4 then local e = -d - 4; y = PY + 0.95 * e + 0.011 * e * e else y = PY + (abs(d) <= 2 and 2 or 0) end S[x] = floor(y + 0.8 * sin(x * 1.3 + 1) * min(1, abs(d) / 10) + 0.5) end local PATHS = { { 24,22, 27,30, 31,37, 37,45, 41,54 }, { 23,22, 20,29, 16,37, 11,45, 7,55 }, { 25,23, 26,32, 24,41, 27,50 }, { 26,24, 33,32, 39,36, 47,41, 55,46 }, } local LAV = {} for c, p in ipairs(PATHS) do local L = {} for s = 1, #p - 2, 2 do local x0, y0, x1, y1 = p[s], p[s + 1], p[s + 2], p[s + 3] local n = max(abs(x1 - x0), abs(y1 - y0)) for k = 0, n - 1 do local i = #L + 1 L[i] = { floor(x0 + (x1 - x0) * k / n + 0.5 + 0.9 * sin(i * 0.5 + c * 2)), floor(y0 + (y1 - y0) * k / n + 0.5) } end end LAV[c] = L end local PULSE_V = { 0.31, 0.38, 0.27, 0.34 } local LDr, LDg, LDb, LBr, LBg, LBb = 150, 70, 14, 255, 190, 90 local BG, BOMBS_CL = nil, 240 function on_settings() local c = cfg.lava or { r = 255, g = 122, b = 26 } LDr, LDg, LDb = floor(c.r * 0.6), floor(c.g * 0.5), floor(c.b * 0.5) LBr, LBg, LBb = c.r + (255 - c.r) * 0.6, c.g + (255 - c.g) * 0.6, c.b + (255 - c.b) * 0.6 BOMBS_CL = ({ 0, 420, 240 })[(cfg.bombs or 2) + 1] BG = nil end local function build_bg() for y = 0, GROUND - 1 do local t = (y / GROUND) ^ 1.7 rect(0, y, W, 1, floor(10 + 84 * t), floor(6 + 14 * t), floor(20 + 4 * t)) end for x = 0, W - 1 do local top = S[x] rect(x, top, 1, GROUND - top, 56, 26, 28) rect(x, top, 1, min(4, GROUND - top), 70, 34, 32) for k = 1, 3 do local h = hash32(x * 0x9E3779B1 + k * 0x85EBCA6B) local y = top + 2 + umod(h, max(1, GROUND - top - 2)) if y < GROUND then if umod(h >> 9, 2) == 0 then px(x, y, 40, 18, 22) else px(x, y, 76, 36, 34) end end end end for g = 1, 8 do local h = hash32(g * 0xC2B2AE35) local x, y = 3 + umod(h, 56), 0 y = S[x] + 3 for _ = 1, 5 do rect(x, y, 1, 3, 30, 14, 18) y = y + 3; x = x + umod(h >> (_ * 3), 3) - 1 if x < 0 or x >= W or y > GROUND - 3 then break end end end local lr, lg, lb = LBr, LBg * 0.6, LBb * 0.4 for x = 8, 44 do local f = 1 - abs(x - PX) / 22 if f > 0 then blend_rect(x, S[x], 1, 6, lr, lg, lb, 0.4 * f) blend_rect(x, S[x] + 6, 1, 8, lr, lg, lb, 0.16 * f) px(x, S[x], 120 + floor(60 * f), 40, 28) end end rect(22, PY + 1, 5, 2, LBr, LBg, LBb) for c = 1, #LAV do for _, p in ipairs(LAV[c]) do px(p[1], p[2], LDr, LDg, LDb) end end rect(0, GROUND, W, H - GROUND, 26, 12, 16) for _, cx in ipairs({ 6, 20, 34, 50, 58 }) do rect(cx, GROUND + 2 + cx % 3, 4, 1, LDr, LDg, LDb) px(cx + 4, GROUND + 3 + cx % 3, LDr, LDg, LDb) end BG = gfx.snapshot(0, 0, W, H) end local function blob(cx, cy, r, R, G, B, a) for y = floor(cy - r), ceil(cy + r) - 1 do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q) local xa, xb = cx - hw, cx + hw local ia, ib = floor(xa), floor(xb) if ib - ia >= 2 then blend_rect(ia + 1, y, ib - ia - 1, 1, R, G, B, a) blend(ia, y, R, G, B, a * (1 - (xa - ia))) blend(ib, y, R, G, B, a * (xb - ib)) elseif ib == ia then blend(ia, y, R, G, B, a * (xb - xa)) else blend(ia, y, R, G, B, a * (1 - (xa - ia))) blend(ib, y, R, G, B, a * (xb - ib)) end end end end local function dot(x, y, R, G, B, a) local fx, fy = x - 0.5, y - 0.5 local ix, iy = floor(fx), floor(fy) fx, fy = fx - ix, fy - iy blend(ix, iy, R, G, B, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, R, G, B, a * fx * (1 - fy)) blend(ix, iy + 1, R, G, B, a * (1 - fx) * fy) blend(ix + 1, iy + 1, R, G, B, a * fx * fy) end local PUFFS, LIFE = 9, 270 local function smoke() for j = PUFFS - 1, 0, -1 do local u = frame / LIFE + j / PUFFS local n = floor(u) local a = u - n local h = hash32(j * 0x9E3779B1 + n * 0x85EBCA6B) local x = PX + 24 * a ^ 1.3 + (umod(h, 7) - 3) * 0.45 * a local y = PY - 1 - a * 27 local r = 2.8 + 5.4 * a + umod(h >> 8, 5) * 0.12 local al = min(1, a * 8) * (a > 0.75 and (1 - a) * 4 or 1) * 0.7 local k = min(1, a / 0.7) blob(x, y, r, min(255, LDr * 1.5) * (1 - k) + 60 * k, min(255, LDg * 1.5) * (1 - k) + 42 * k, min(255, LDb * 1.5) * (1 - k) + 50 * k, al) if a < 0.4 then blob(x, y + r * 0.35, r * 0.65, LBr, LBg * 0.7, LBb * 0.5, 0.45 * (1 - a / 0.4)) end end end local G_B, SPARKS, SPARK_LIFE = 0.05, 7, 26 local plan_n, B = -1, nil local function plan(n) local h = hash32(n * 0x9E3779B1 + 7) local vx = (umod(h >> 8, 2) == 1 and 1 or -1) * (0.35 + umod(h >> 10, 10) / 40) local vy = -1.35 - umod(h >> 14, 6) / 20 local t = 12 while t < 150 do local x, y = PX + vx * t, PY + 1 + vy * t + 0.5 * G_B * t * t local xi = floor(x + 0.5) if xi < 0 or xi >= W or (t > 20 and y >= S[xi] - 1) then break end t = t + 1 end B = { t0 = 8 + umod(h >> 18, 30), vx = vx, vy = vy, tl = t, lx = PX + vx * t, ly = PY + 1 + vy * t + 0.5 * G_B * t * t, n = n } plan_n = n end local function bombs() local n = frame // BOMBS_CL if n ~= plan_n then plan(n) end local tt = frame % BOMBS_CL - B.t0 if tt < 0 then return end if tt < 8 then blob(PX, PY, 4 - tt * 0.3, LBr, LBg, LBb, 0.5 * (1 - tt / 8)) end if tt < B.tl then for k = 8, 2, -2 do local t2 = tt - k if t2 >= 0 then local x, y = PX + B.vx * t2, PY + 1 + B.vy * t2 + 0.5 * G_B * t2 * t2 blob(x, y, 1.3, 255, 120, 30, 0.5 - k * 0.05) end end local x, y = PX + B.vx * tt, PY + 1 + B.vy * tt + 0.5 * G_B * tt * tt blob(x, y, 1.9, 255, 110, 30, 0.45) dot(x, y, 255, 240, 170, 1) dot(x, y, 255, 240, 170, 0.6) else local ts = tt - B.tl if ts < 4 then blob(B.lx, B.ly, 3.2 - ts * 0.5, 255, 220, 140, 0.6) end if ts < SPARK_LIFE then for s = 1, SPARKS do local hs = hash32(B.n * 977 + s * 0x85EBCA6B) local vx = (umod(hs, 100) / 100 - 0.5) * 1.4 local vy = -(0.4 + umod(hs >> 8, 70) / 100) local a = 1 - ts / SPARK_LIFE dot(B.lx + vx * ts, B.ly + vy * ts + 0.035 * ts * ts, 255, 120 + 100 * a, 40 + 50 * a, a) end end if ts < 70 then blob(B.lx, B.ly, 1.4, 255, 80, 20, 0.55 * (1 - ts / 70)) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) for c = 1, #LAV do local pts = LAV[c] local n = #pts for k = 0, 1 do local p = (frame * PULSE_V[c] + k * n * 0.5 + c * 9) % (n + 10) - 3 for i = max(1, ceil(p - 4)), min(n, floor(p + 4)) do local b = 1 - abs(i - p) / 4 local x, y = pts[i][1], pts[i][2] blend(x, y, LBr, LBg, LBb, b * 0.9) blend(x - 1, y, LBr, LBg * 0.7, LBb * 0.4, b * 0.3) blend(x + 1, y, LBr, LBg * 0.7, LBb * 0.4, b * 0.5) end end end blob(PX, PY + 1, 3.5, LBr, LBg, LBb, 0.22 + 0.08 * sin(frame * 0.13)) smoke() if BOMBS_CL > 0 then bombs() end end