info = { type = "image", title = "Misty wood", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Fog drifts through a dark wood, fireflies glow", api = 33, } local tr = ({ sk = { ["Fireflies"] = "Svetlušky", ["Mist"] = "Hmla", ["none"] = "žiadne", ["thin"] = "tenké", ["thick"] = "hrubé", ["Glow"] = "Žiara" }, cs = { ["Fireflies"] = "Světlušky", ["Mist"] = "Mlha", ["none"] = "žádné", ["thin"] = "tenké", ["thick"] = "tlusté", ["Glow"] = "Záře" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "flies", label = T("Fireflies"), type = "int", min = 0, max = 16, default = 9 }, { key = "mist", label = T("Mist"), type = "int", default = 1, opts = { T("none"), T("thin"), T("thick") } }, { key = "glow", label = T("Glow"), type = "color", default = "#D2FF78" }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.2831853 local SKY, HALO2, HALO1, MOON = { 46, 82, 128 }, { 58, 98, 144 }, { 76, 120, 166 }, { 228, 240, 250 } local FAR, MID, NEAR, RIM = { 32, 58, 98 }, { 20, 38, 68 }, { 9, 19, 36 }, { 44, 82, 112 } local MISTC = { 178, 210, 228 } local GLOW = { 210, 255, 120 } local function disc(cx, cy, r, c) for y = cy - r, cy + r do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = floor(sqrt(q) + 0.5) rect(cx - hw, y, 2 * hw, 1, c[1], c[2], c[3]) end end end local function pine(L, R, cx, yb, h, wmax, tiers) local ytop = yb - h local run_y, run_hw = ytop, -1 local function flush(yend) if run_hw >= 0 and yend > run_y then L[#L + 1] = cx - run_hw; L[#L + 1] = run_y; L[#L + 1] = 2 * run_hw + 1; L[#L + 1] = yend - run_y if R then R[#R + 1] = cx + run_hw; R[#R + 1] = run_y; R[#R + 1] = 1; R[#R + 1] = yend - run_y end end end for i = 0, h - 1 do local u = i * tiers / h local t = floor(u) local hw = floor(wmax * (t + 0.45 + 0.55 * (u - t)) / tiers + 0.5) if hw ~= run_hw then flush(ytop + i); run_y, run_hw = ytop + i, hw end end flush(yb) local tw = wmax // 5 + 1 L[#L + 1] = cx - tw // 2; L[#L + 1] = yb; L[#L + 1] = tw; L[#L + 1] = 6 end local FARL, MIDL, NEARL, NEARR, GROUNDL = {}, {}, {}, {}, {} do local k for i = 0, 7 do local h = hash32((i + 1) * 0x9E3779B1) pine(FARL, nil, 4 + i * 9 + umod(h, 5), 52, 22 + umod(h >> 6, 9), 7 + umod(h >> 10, 3), 4) end for i = 0, 5 do local h = hash32((i + 11) * 0x85EBCA6B) pine(MIDL, nil, 3 + i * 11 + umod(h, 6), 56, 24 + umod(h >> 6, 9), 9 + umod(h >> 10, 4), 4) end MIDL[#MIDL + 1] = 0; MIDL[#MIDL + 1] = 54; MIDL[#MIDL + 1] = W; MIDL[#MIDL + 1] = 10 pine(NEARL, NEARR, 3, 62, 54, 11, 7) pine(NEARL, NEARR, 61, 62, 46, 10, 6) for x = 0, W - 1, 2 do local h = hash32((x + 7) * 0xC2B2AE35) local top = 58 - umod(h, 3) GROUNDL[#GROUNDL + 1] = x; GROUNDL[#GROUNDL + 1] = top; GROUNDL[#GROUNDL + 1] = 2; GROUNDL[#GROUNDL + 1] = H - top end end local function draw_list(L, c) local r, g, b = c[1], c[2], c[3] for i = 1, #L, 4 do rect(L[i], L[i + 1], L[i + 2], L[i + 3], r, g, b) end end local BG = nil local function build_bg() rect(0, 0, W, H, SKY[1], SKY[2], SKY[3]) disc(46, 12, 17, HALO2) disc(46, 12, 10, HALO1) disc(46, 12, 4, MOON) draw_list(FARL, FAR) rect(0, 50, W, 14, FAR[1], FAR[2], FAR[3]) BG = gfx.snapshot(0, 0, W, H) end local MH = 16 local MASK do local n = {} for x = 0, 63 do local a = x / 64 * TAU local v = 0.5 + 0.5 * (0.55 * sin(a + 0.7) + 0.3 * sin(3 * a + 2.1) + 0.15 * sin(5 * a + 4.0)) v = (v - 0.12) / 0.55 n[x] = v < 0 and 0 or (v > 1 and 1 or v) end local rows = {} for y = 0, MH - 1 do local e = sin(3.14159265 * (y + 0.5) / MH) local row = {} for x = 0, 63 do row[x + 1] = floor(255 * n[x] * e + 0.5) end rows[y + 1] = string.char(table.unpack(row, 1, 64)) end MASK = table.concat(rows) end local BANDS = { { 23, 3.2, 0, 0.55 }, { 36, -4.5, 21, 0.5 }, { 47, 6.0, 40, 0.42 } } local NB = 2 local BSCALE = 1 local function draw_band(b, ts) local off = (b[3] + b[2] * ts) % 64 local xi = floor(off) local s = b[4] * BSCALE local mb = gfx.blend_mask mb(MASK, MISTC[1], MISTC[2], MISTC[3], -xi, b[1], 64, MH, 64, MH, s) if xi > 0 then mb(MASK, MISTC[1], MISTC[2], MISTC[3], 64 - xi, b[1], 64, MH, 64, MH, s) end end local NFLY = 16 local BX, BY, PH1, PH2, PH3, PER, PHB = {}, {}, {}, {}, {}, {}, {} for i = 1, NFLY do local h = hash32(i * 0x27D4EB2F) BX[i] = 6 + umod(h, 52) BY[i] = 28 + umod(h >> 6, 26) PH1[i] = umod(h >> 12, 628) / 100.0 PH2[i] = umod(h >> 16, 628) / 100.0 PH3[i] = umod(h >> 20, 628) / 100.0 PER[i] = 3.2 + umod(h >> 24, 28) / 10.0 PHB[i] = umod(h >> 8, 628) / 100.0 end local NF = 9 function on_settings() NF = cfg.flies or 9 local m = cfg.mist or 1 NB = m == 0 and 0 or (m == 1 and 2 or 3) BSCALE = m == 2 and 1.25 or 0.9 local c = cfg.glow or { r = 210, g = 255, b = 120 } GLOW = { c.r, c.g, c.b } end local function draw_fly(i, ts) local on = sin(ts * TAU / PER[i] + PHB[i]) if on <= 0.05 then return end local a = on * on * on local x = BX[i] + 5 * sin(0.37 * ts + PH1[i]) + 2.5 * sin(0.91 * ts + PH2[i]) local y = BY[i] + 3 * sin(0.53 * ts + PH3[i]) + 1.5 * sin(1.13 * ts + PH1[i]) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, b = GLOW[1], GLOW[2], GLOW[3] brect(ix - 1, iy - 1, 3, 3, r, g, b, a * 0.16) brect(ix, iy - 2, 1, 5, r, g, b, a * 0.12) brect(ix - 2, iy, 5, 1, r, g, b, a * 0.12) blend(ix, iy, r, g, b, (1 - fx) * (1 - fy) * a) blend(ix + 1, iy, r, g, b, fx * (1 - fy) * a) blend(ix, iy + 1, r, g, b, (1 - fx) * fy * a) blend(ix + 1, iy + 1, r, g, b, fx * fy * a) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local ts = frame / FPS if NB >= 1 then draw_band(BANDS[1], ts) end draw_list(MIDL, MID) if NB >= 2 then draw_band(BANDS[2], ts) end draw_list(NEARL, NEAR) draw_list(NEARR, RIM) draw_list(GROUNDL, NEAR) if NB >= 3 then draw_band(BANDS[3], ts) end for i = 1, NF do draw_fly(i, ts) end end