info = { type = "image", title = "Moon coin", category = "btc", author = "BRRRO", version = "1.0", license = "MIT", desc = "A gold coin glows above drifting night clouds", api = 44, } local tr = ({ sk = { ["Cloud speed"] = "Rýchlosť oblakov", ["Glow"] = "Žiara" }, cs = { ["Cloud speed"] = "Rychlost mraků", ["Glow"] = "Záře" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Cloud speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "glow", label = T("Glow"), type = "int", min = 0, max = 5, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CX, CY, R = 32, 21, 14 local GR = R + 9 local KEY = { 255, 0, 255 } local function lerp(a, b, t) return a + (b - a) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise2(x, y, seed) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy fx, fy = fx * fx * (3 - 2 * fx), fy * fy * (3 - 2 * fy) local a, b = vn(ix + iy * 131, seed), vn(ix + 1 + iy * 131, seed) local c, d = vn(ix + (iy + 1) * 131, seed), vn(ix + 1 + (iy + 1) * 131, seed) return (a + (b - a) * fx) * (1 - fy) + (c + (d - c) * fx) * fy end local LAYERS = { { y = 36, h = 6, w0 = 16, w1 = 22, v = 1.4, top = { 52, 66, 116 }, bot = { 30, 42, 92 }, a = 0.8 }, { y = 43, h = 8, w0 = 20, w1 = 28, v = 3.2, top = { 100, 116, 164 }, bot = { 52, 66, 120 }, a = 0.9 }, { y = 53, h = 11, w0 = 26, w1 = 36, v = 6.5, top = { 186, 194, 222 }, bot = { 80, 94, 146 }, a = 0.95 }, } local NC = 3 local CL = {} for li, L in ipairs(LAYERS) do local span = W + 2 * L.w1 for k = 1, NC do local seed = li * 977 + k * 61 local hh = hash32(seed) local w = L.w0 + umod(hh, L.w1 - L.w0 + 1) local puffs = {} for p = 1, 3 do local ph = hash32(seed * 31 + p) puffs[p] = { cx = (p - 2) * w * 0.28 + umod(ph, 5) - 2, hw = w * (0.22 + umod(ph >> 4, 16) / 100), ph = p == 2 and L.h or L.h * (0.5 + umod(ph >> 9, 40) / 100) } end local c = { x0 = (k - 1) * span / NC + umod(hh >> 6, 12), dy = umod(hh >> 10, 5) - 2, sp = 0.85 + umod(hh >> 14, 30) / 100, lo = {}, hi = {}, col = {}, al = {}, span = span, hw = L.w1 } for j = 0, L.h - 1 do local lo, hi = 99, -99 for _, p in ipairs(puffs) do local dist = (L.h - j - 0.5) / p.ph if dist < 1 then local e = p.hw * sqrt(1 - dist * dist) lo, hi = min(lo, p.cx - e), max(hi, p.cx + e) end end local u = j / (L.h - 1) c.lo[j + 1], c.hi[j + 1] = lo, hi c.col[j + 1] = { lerp(L.top[1], L.bot[1], u), lerp(L.top[2], L.bot[2], u), lerp(L.top[3], L.bot[3], u) } c.al[j + 1] = L.a * (0.55 + 0.45 * min(1, j / 2)) end CL[#CL + 1] = { L, c } end end local function cloud_row(xl, xr, y, a, col) local il, ir = floor(xl), floor(xr) if ir <= il then return end local r, g, b = col[1], col[2], col[3] if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, r, g, b, a) end blend_rect(il, y, 1, 1, r, g, b, a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, r, g, b, a * (xr - ir)) end local SX, SY, SPH, SW, NS = {}, {}, {}, {}, 0 do local i = 0 while NS < 14 and i < 300 do i = i + 1 local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), umod(h >> 8, 40) if sqrt((x - CX) ^ 2 + (y - CY) ^ 2) > GR + 2 then NS = NS + 1 SX[NS], SY[NS], SPH[NS], SW[NS] = x, y, umod(h >> 16, 628) / 100, 1.5 + umod(h >> 22, 30) / 10 end end end local function sky() rect(0, 0, W, H, 13, 20, 59) local i = 0 for k = 1, 30 do local h = hash32(k * 0xC2B2AE35) local x, y = umod(h, W), umod(h >> 8, 44) if sqrt((x - CX) ^ 2 + (y - CY) ^ 2) > GR + 2 then px(x, y, 70, 78, 130) end end end local RAMP = { { 70, 44, 14 }, { 214, 160, 60 }, { 255, 240, 180 } } local function shade(v) v = min(1, max(0, v)) local a, b, t = RAMP[1], RAMP[2], v * 2 if v >= 0.5 then a, b, t = RAMP[2], RAMP[3], (v - 0.5) * 2 end return rgb(lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end local STAGE, BG, PARTC, COIN, MASK = 1, nil, nil, nil, nil local RX0, RY0, RS = CX - R - 1, CY - R - 1, 2 * R + 2 local function coin(ya, yb, first) if first then rect(RX0, RY0, RS, RS, KEY[1], KEY[2], KEY[3]) else blit_rgb(PARTC, RX0, RY0, RS, RS) end for y = ya, yb do local dy = y + 0.5 - CY for x = CX - R, CX + R - 1 do local dx = x + 0.5 - CX local d = sqrt(dx * dx + dy * dy) / R if d <= 1 then local lit = -(dx * 0.5 + dy * 0.86) / R local v if d > 0.9 then v = 0.7 + 0.3 * lit - (d > 0.97 and 0.1 or 0) elseif d > 0.82 then v = 0.4 + 0.1 * lit else v = 0.6 + 0.16 * lit + 0.1 * (1 - d) - 0.16 * noise2(x / 3.2, y / 3.2, 5) end px(x, y, shade(v)) end end end PARTC = snapshot(RX0, RY0, RS, RS) end local BTC = { "..#.#...", "######..", ".##..##.", ".##..##.", ".#####..", ".##...##", ".##...##", "#######.", "..#.#..." } local function btc_sign(x0, y0, s, r, g, b) for i = 1, 9 do local c = 1 while true do local a, e = BTC[i]:find("#+", c) if not a then break end rect(x0 + (a - 1) * s, y0 + (i - 1) * s, (e - a + 1) * s, s, r, g, b) c = e + 1 end end end local function symbol() blit_rgb(PARTC, RX0, RY0, RS, RS) btc_sign(CX - 7, CY - 8, 2, 255, 238, 176) btc_sign(CX - 8, CY - 9, 2, 120, 76, 24) COIN = snapshot(RX0, RY0, RS, RS) end local GS = 2 * GR local function glow_mask() local half, rows = {}, {} for y = 0, GR - 1 do for x = 0, GR - 1 do local d = sqrt((x - GR + 0.5) ^ 2 + (y - GR + 0.5) ^ 2) half[x + 1] = floor(255 * max(0, 1 - max(0, d - R + 2) / (GR - R + 2))) end local s = string.char(table.unpack(half, 1, GR)) rows[y + 1] = s .. s:reverse() end for y = GR + 1, GS do rows[y] = rows[GS + 1 - y] end MASK = table.concat(rows) end local STAGES = { function() sky(); BG = snapshot(0, 0, W, H) end, function() coin(CY - R, CY - R + 9, true) end, function() coin(CY - R + 10, CY - R + 19) end, function() coin(CY - R + 20, CY + R - 1) end, symbol, glow_mask, } function render() if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE]() if BG then blit_rgb(BG, 0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end local t = frame / FPS blit_rgb(BG, 0, 0, W, H) local gl = cfg.glow or 3 if gl > 0 then blend_mask(MASK, 255, 206, 120, CX - GR, CY - GR, GS, GS, GS, GS, 0.15 * gl * (0.85 + 0.15 * sin(t * 0.9))) end blit_rgb(COIN, RX0, RY0, RS, RS, KEY[1], KEY[2], KEY[3]) for i = 1, NS do local v = sin(t * SW[i] + SPH[i]) if v > 0.2 then blend(SX[i], SY[i], 200, 214, 255, (v - 0.2) * 1.1) end end local gp = (t % 6) / 0.9 if gp < 1 then local a = sin(gp * 3.1416) blend(CX - 9, CY - 9, 255, 250, 236, a) blend_rect(CX - 9, CY - 12, 1, 3, 255, 250, 236, a * 0.5) blend_rect(CX - 9, CY - 8, 1, 3, 255, 250, 236, a * 0.5) blend_rect(CX - 12, CY - 9, 3, 1, 255, 250, 236, a * 0.5) blend_rect(CX - 8, CY - 9, 3, 1, 255, 250, 236, a * 0.5) end local sp = ({ 0.4, 0.7, 1, 1.5, 2.2 })[cfg.speed or 3] for i = 1, #CL do local L, c = CL[i][1], CL[i][2] local x = (c.x0 - t * L.v * sp * c.sp) % c.span - c.hw if x < W + c.hw then local y0 = L.y + c.dy for j = 1, L.h do cloud_row(x + c.lo[j], x + c.hi[j], y0 + j - 1, c.al[j], c.col[j]) end end end end