info = { type = "image", title = "Campfire", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A crackling campfire by a tent at night", api = 33, } local tr = ({ sk = { ["Flame"] = "Plameň", ["Sparks"] = "Iskry", ["Smoke"] = "Dym" }, cs = { ["Flame"] = "Plamen", ["Sparks"] = "Jiskry", ["Smoke"] = "Kouř" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "flame", label = T("Flame"), type = "int", min = 1, max = 5, default = 3 }, { key = "sparks", label = T("Sparks"), type = "int", min = 0, max = 10, default = 6 }, { key = "smoke", label = T("Smoke"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, abs = math.floor, math.sin, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SKY, SKY_LO = { 20, 26, 80 }, { 44, 46, 120 } local STAR, STAR_D = { 230, 230, 255 }, { 120, 130, 200 } local PINE = { 12, 16, 38 } local GROUND = { 56, 40, 60 } local GLOW1, GLOW2 = { 96, 52, 50 }, { 150, 82, 50 } local TENT_L, TENT_D, DOOR = { 220, 132, 70 }, { 96, 60, 104 }, { 36, 20, 30 } local LOG, LOG_D, LOG_END = { 96, 54, 34 }, { 58, 30, 26 }, { 196, 140, 96 } local STONE, STONE_L = { 100, 90, 116 }, { 196, 156, 130 } local OUTER, MID, CORE = { 232, 58, 20 }, { 255, 140, 20 }, { 255, 224, 96 } local EMBER, EMBER_D = { 255, 120, 30 }, { 190, 44, 20 } local SMOKE = { 170, 168, 196 } local HORIZON = H - 28 local CX, FY = W // 2, H - 13 local FW = 8 local function c3(c) return c[1], c[2], c[3] end local BG = nil local function pine(x, base, h) for j = 0, h - 1 do local k, r = j // 4, j % 4 local hw = k + (r + 1) // 2 rect(x - hw, base - h + j, 2 * hw + 1, 1, c3(PINE)) end end local function ellipse(cx, cy, rx, ry, c) for dy = -ry, ry do local k = 1 - (dy * dy) / (ry * ry) local hw = floor(rx * k ^ 0.5 + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end local function tent(ax, ay, h) for j = 0, h - 1 do local hw = floor(j * 0.62 + 0.5) rect(ax - hw, ay + j, hw, 1, c3(TENT_D)) rect(ax, ay + j, hw + 1, 1, c3(TENT_L)) if j >= h // 2 then local d = (j - h // 2) * 2 // 5 rect(ax - d, ay + j, 2 * d + 1, 1, c3(DOOR)) end end px(ax, ay - 1, c3(LOG_END)) end local function stone(x, y) rect(x, y, 3, 2, c3(STONE)) rect(x, y, 2, 1, c3(STONE_L)) end local function build_bg() rect(0, 0, W, H, c3(SKY)) rect(0, HORIZON - 12, W, 12, c3(SKY_LO)) for x = 0, W - 1, 2 do px(x, HORIZON - 13, c3(SKY_LO)) end local P = { { 2, 15 }, { 9, 11 }, { 15, 14 }, { 22, 9 }, { 27, 12 }, { 37, 10 }, { 43, 13 }, { 49, 9 }, { 55, 15 }, { 61, 12 } } for i = 1, #P do pine(P[i][1], HORIZON + 1, P[i][2]) end rect(0, HORIZON, W, H - HORIZON, c3(GROUND)) ellipse(CX, FY + 2, 28, 8, GLOW1) ellipse(CX, FY + 2, 17, 5, GLOW2) tent(11, HORIZON - 6, 19) rect(46, FY + 1, 14, 3, c3(LOG_D)) rect(59, FY + 1, 2, 3, c3(LOG_END)) for x = CX - 10, CX + 8, 4 do stone(x, FY - 1) end for i = 0, 12 do px(CX - 7 + i, FY - 2 + (i * 4) // 12, c3(LOG)) px(CX - 7 + i, FY - 1 + (i * 4) // 12, c3(LOG)) px(CX + 6 - i, FY - 2 + (i * 4) // 12, c3(LOG)) px(CX + 6 - i, FY - 1 + (i * 4) // 12, c3(LOG)) end px(CX - 7, FY - 2, c3(LOG_END)); px(CX + 6, FY - 2, c3(LOG_END)) for x = CX - 12, CX + 10, 4 do stone(x, FY + 3) end BG = gfx.snapshot(0, 0, W, H) end local SX, SY, SPH = {}, {}, {} do local k, i = 0, 0 while k < 16 and i < 200 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, HORIZON - 16) k = k + 1 SX[k], SY[k], SPH[k] = x, y, umod(h >> 16, 628) / 100.0 end end local HMAX, NSPARK, SMOKE_ON = 15, 12, true local PROF = {} function on_settings() HMAX = 9 + 3 * (cfg.flame or 3) NSPARK = 2 * (cfg.sparks or 6) SMOKE_ON = cfg.smoke ~= false for c = -FW, FW - 1 do local d = abs(c + 0.5) / FW PROF[c] = HMAX * (1 - d ^ 1.6) end end local TOPS = {} local function flame(t) local crackle = floor(t * 15) local tops = TOPS for c = -FW, FW - 1 do local n = 0.72 + 0.18 * sin(t * 6.3 + c * 1.7) + 0.12 * sin(t * 9.9 - c * 0.9) n = n + umod(hash32(crackle * 0x9E3779B1 + c * 0x85EBCA6B), 100) / 600.0 local h = floor(PROF[c] * n + 0.5) local x = CX + c if h > 0 then rect(x, FY - h, 1, h, c3(OUTER)) end local ac = abs(c + 0.5) if ac < FW - 2 then local m = floor(h * 0.62 + 0.5) if m > 0 then rect(x, FY - m, 1, m, c3(MID)) end end if ac < FW - 4 then local k = floor(h * 0.32 + 0.5) if k > 0 then rect(x, FY - k, 1, k, c3(CORE)) end end tops[c] = h end return tops end local LICK_P = 0.3 local function licks(t, tops) for i = 1, 5 do local tt = t + i * 0.13 local n, a = floor(tt / LICK_P), tt % LICK_P if a < 0.2 then local h = hash32(n * 0x9E3779B1 + i * 0x27D4EB2F) local c = umod(h, FW) - FW // 2 local y = FY - tops[c] - 2 - floor(a * 30) local x = CX + c + (a >= 0.1 and ((h >> 9) & 1) * 2 - 1 or 0) if a < 0.1 then rect(x, y, 1, 2, c3(MID)) else px(x, y, c3(OUTER)) end end end end local EMBERS = { { -5, 0 }, { -2, 1 }, { 1, 0 }, { 4, 1 }, { -4, 2 }, { 3, 2 } } local function embers(t) local k = floor(t * 10) for i = 1, #EMBERS do local e = EMBERS[i] local on = umod(hash32(k * 0x9E3779B1 + i * 0x85EBCA6B), 3) ~= 0 px(CX + e[1], FY + e[2], c3(on and EMBER or EMBER_D)) end end local SP = {} for i = 1, 20 do local h = hash32(i * 0x85EBCA6B) SP[i] = { life = 1.1 + umod(h, 100) / 100.0, off = umod(h >> 6, 260) / 100.0, v = 13.5 + umod(h >> 12, 35) * 0.3, x0 = umod(h >> 18, 2 * FW - 4) - FW + 2, amp = 1 + umod(h >> 22, 3), w = 1.8 + umod(h >> 25, 6) * 0.3, } end local function sparks(t) for i = 1, NSPARK do local s = SP[i] local a = (t + s.off) % s.life local yf = FY - HMAX * 0.6 - a * s.v local y = floor(yf) local f = yf - y local x = floor(CX + s.x0 + s.amp * sin(a * s.w + i) + a * 1.2 + 0.5) local k = a / s.life local c = k < 0.4 and CORE or (k < 0.75 and MID or OUTER) px(x, y, c[1], c[2], c[3]) blend(x, y - 1, c[1], c[2], c[3], 1 - f) end end local PUFF_LIFE, NPUFF = 5, 6 local function smoke(t) for i = 0, NPUFF - 1 do local a = (t + i * PUFF_LIFE / NPUFF) % PUFF_LIFE local k = a / PUFF_LIFE local yf = FY - HMAX - 3 - a * 6.6 local y = floor(yf) local f = yf - y local s = 1 + floor(k * 3.2) local x = floor(CX - 1 + a * 2.7 + 2 * sin(a * 1.5 + i) + 0.5) local al = 0.14 + 0.44 * (1 - k) local a0, a1 = al * (1 - f), al * f if a0 > 0.06 then blend_rect(x, y, s + 1, 1, SMOKE[1], SMOKE[2], SMOKE[3], a0) end if s > 1 then blend_rect(x, y + 1, s + 1, s - 1, SMOKE[1], SMOKE[2], SMOKE[3], al) end if a1 > 0.06 then blend_rect(x, y + s, s + 1, 1, SMOKE[1], SMOKE[2], SMOKE[3], a1) end end end local function stars(t) local a = t * (6.28318 / 4) for k = 1, #SX do local c = sin(a + SPH[k] * 3) > -0.4 and STAR or STAR_D px(SX[k], SY[k], c[1], c[2], c[3]) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS stars(t) if SMOKE_ON then smoke(t) end local tops = flame(t) licks(t, tops) embers(t) sparks(t) end