info = { type = "image", title = "BTC berry", category = "btc", author = "BRRRO", version = "1.0", license = "MIT", desc = "A golden bitcoin ripens on a twig", api = 44, } local tr = ({ sk = { Twists = "Otočenia", never = "nikdy", rarely = "zriedka", sometimes = "občas", often = "často", ["Ripening berry"] = "Dozrievajúci plod" }, cs = { Twists = "Otočení", never = "nikdy", rarely = "zřídka", sometimes = "občas", often = "často", ["Ripening berry"] = "Dozrávající plod" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "spin", label = T("Twists"), type = "int", default = 2, opts = { T("never"), T("rarely"), T("sometimes"), T("often") } }, { key = "ripen", label = T("Ripening berry"), type = "bool", default = true }, } local rect, px = gfx.rect, gfx.px local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local CN = 22 local GLY = { "..x.x...", "xxxxxxx.", ".xx...xx", ".xx...xx", ".xx..xx.", ".xxxxxx.", ".xx...xx", ".xx...xx", ".xx...xx", "xxxxxxx.", "..x.x...", } local FRONT, BACK do local f, b = {}, {} for y = 0, CN - 1 do for x = 0, CN - 1 do local dx, dy = x - 10.5, y - 10.5 local d = sqrt(dx * dx + dy * dy) local v = 0 if d <= 10.9 then if d > 9.9 then v = 1 elseif d > 8.4 then v = dx + dy < -6 and 3 or 2 else v = (dx + dy < -8 and d > 6) and 7 or 4 end end f[y * CN + x + 1], b[y * CN + x + 1] = v, v if v == 4 and d > 5.4 and d < 6.5 then b[y * CN + x + 1] = 2 end end end for k = 0, 11 do local a = k * TAU / 12 local x, y = floor(10.5 + 9.15 * cos(a)), floor(10.5 + 9.15 * sin(a)) f[y * CN + x + 1], b[y * CN + x + 1] = 6, 6 end for r = 1, #GLY do local row = GLY[r] for c = 1, #row do if row:sub(c, c) == "x" then f[(r + 4) * CN + (c + 6) + 1] = 5 end end end local rf, rb = {}, {} for y = 0, CN - 1 do rf[y + 1] = string.char(table.unpack(f, y * CN + 1, y * CN + CN)) rb[y + 1] = string.char(table.unpack(b, y * CN + 1, y * CN + CN)) end FRONT, BACK = table.concat(rf), table.concat(rb) end local CPAL = { 0, 0, 0, 118, 66, 8, 226, 150, 24, 255, 214, 90, 250, 190, 40, 146, 82, 8, 176, 104, 14, 255, 230, 140 } local EDGE = { 178, 106, 16 } local function disc(r) local n, t = 2 * r + 1, {} for y = 0, n - 1 do for x = 0, n - 1 do local dx, dy = x - r, y - r local d = sqrt(dx * dx + dy * dy) local v = 0 if d <= r - 0.6 then local hx, hy = dx + r * 0.4, dy + r * 0.4 v = (hx * hx + hy * hy < r * r * 0.12) and 3 or 2 elseif d <= r + 0.4 then v = 1 end t[#t + 1] = v end end return string.char(table.unpack(t)), n end local D3, N3 = disc(3) local D5, N5 = disc(5) local D6, N6 = disc(6) local GREEN = { 0, 0, 0, 40, 92, 30, 110, 190, 60, 172, 228, 112 } local LIME = { 0, 0, 0, 92, 98, 20, 198, 206, 62, 238, 242, 144 } local AMBER = { 0, 0, 0, 132, 80, 10, 240, 168, 40, 255, 226, 124 } local BGC = { 56, 110, 104 } local LEAF, LEAF_L, LEAF_E, VEIN = { 78, 150, 62 }, { 106, 178, 78 }, { 32, 76, 42 }, { 150, 206, 104 } local TWIG, TWIG_L, TWIG_D, STEM = { 118, 76, 40 }, { 160, 110, 62 }, { 74, 44, 24 }, { 100, 66, 34 } local function TY(x) return floor(12 - x * 0.05 + 1.5 * sin(x * 0.12) + 0.5) end local function leaf(x0, y0, x1, y1, hw) local dx, dy = x1 - x0, y1 - y0 local L = sqrt(dx * dx + dy * dy) local ux, uy = dx / L, dy / L local xa, xb = floor(math.min(x0, x1) - hw), floor(math.max(x0, x1) + hw) + 1 local ya, yb = floor(math.min(y0, y1) - hw), floor(math.max(y0, y1) + hw) + 1 for y = ya, yb do local rx, rc = nil, nil for x = xa, xb + 1 do local c = nil if x <= xb then local qx, qy = x + 0.5 - x0, y + 0.5 - y0 local s = (qx * ux + qy * uy) / L local q = qy * ux - qx * uy local aq = q < 0 and -q or q if s > 0 and s < 1 and aq < hw then local wv = hw * sin(3.14159 * s) if aq < wv then if aq < 0.55 then c = VEIN elseif aq > wv - 1.1 then c = LEAF_E elseif q < 0 then c = LEAF_L else c = LEAF end end end end if c ~= rc then if rc then rect(rx, y, x - rx, 1, c3(rc)) end rx, rc = x, c end end end end local LEAVES = { { 6, 13, -1, 34, 5 }, { 24, 11, 15, -3, 4 }, { 28, 12, 22, 34, 4 }, { 46, 11, 59, -2, 4 }, { 52, 10, 66, 31, 5 }, { 36, 11, 44, 1, 3 }, { 8, 59, 22, 66, 4 }, { 18, 56, 32, 63, 4 }, { 2, 60, -2, 46, 4 }, } local BG, PART, STAGE = nil, nil, 1 local MCX, MCY = 38, 26 local FX, FY = 17, 23 local STAGES = { function() rect(0, 0, W, H, c3(BGC)) for i = 1, 3 do local l = LEAVES[i]; leaf(l[1], l[2], l[3], l[4], l[5]) end end, function() for i = 4, 6 do local l = LEAVES[i]; leaf(l[1], l[2], l[3], l[4], l[5]) end end, function() for i = 7, #LEAVES do local l = LEAVES[i]; leaf(l[1], l[2], l[3], l[4], l[5]) end end, function() for x = -1, W do local y = TY(x) rect(x, y, 1, 3, c3(TWIG)); px(x, y, c3(TWIG_L)); px(x, y + 2, c3(TWIG_D)) end for x = -1, 26 do local y = floor(61 - x * 0.28 + 0.5) rect(x, y, 1, 2, c3(TWIG)); px(x, y, c3(TWIG_L)) end rect(MCX, TY(MCX) + 2, 1, MCY - TY(MCX) - 1, c3(STEM)) rect(FX, TY(FX) + 2, 1, FY - TY(FX) - 1, c3(STEM)) rect(54, TY(54) + 2, 1, 3, c3(STEM)) gfx.blit_indexed(D3, GREEN, 51, TY(54) + 5, N3, N3, N3, N3, true) rect(14, 58, 1, 2, c3(STEM)) gfx.blit_indexed(D3, GREEN, 11, 51, N3, N3, N3, N3, true) BG = gfx.snapshot(0, 0, W, H) end, } local SPX, SPY = { 26, 52, 29, 51, 44 }, { 25, 31, 48, 46, 22 } local SIZE = { 0, 1, 2, 1, 0 } local function sparkle(x, y, L) if L == 0 then px(x, y, 255, 244, 200); return end rect(x - L, y, 2 * L + 1, 1, 255, 236, 150) rect(x, y - L, 1, 2 * L + 1, 255, 236, 150) px(x, y, 255, 255, 255) end local PERIODS = { 0, 10, 6.67, 4.33 } local function coin(t) local p = PERIODS[(cfg.spin or 2) + 1] or 200 local th, f, D = 0, 0, 1.8 if p > 0 then f = t % p if f < D then local u = f / D th = TAU * u * u * (3 - 2 * u) end end local cs, sn = cos(th), sin(th) local w = floor(CN * (cs < 0 and -cs or cs) / 2 + 0.5) * 2 if w < 2 then w = 2 end local x0 = MCX - w // 2 local e = floor(2.5 * (sn < 0 and -sn or sn) + 0.5) if e > 0 then if sn > 0 then rect(x0 + w - 1, MCY + 2, e + 1, CN - 4, c3(EDGE)) else rect(x0 - e, MCY + 2, e + 1, CN - 4, c3(EDGE)) end end gfx.blit_indexed(cs >= 0 and FRONT or BACK, CPAL, x0, MCY, w, CN, CN, CN, true) if p > 0 and f >= D and f < D + 0.5 then local k = floor((f - D) * 10) sparkle(MCX - 5, MCY + 5, SIZE[k + 1]) end end local function fruit(t) local u = t % 11 if u < 2.67 then gfx.blit_indexed(D3, GREEN, FX - 3, FY, N3, N3, N3, N3, true) elseif u < 5.33 then gfx.blit_indexed(D5, LIME, FX - 5, FY, N5, N5, N5, N5, true) elseif u < 7.33 then gfx.blit_indexed(D6, AMBER, FX - 6, FY, N6, N6, N6, N6, true) elseif u < 10.67 then local y = FY if u >= 9.67 then local v = u - 9.67; y = FY + floor(108 * v * v) end gfx.blit_indexed(FRONT, CPAL, FX - 7, y, 14, 14, CN, CN, true) if u < 7.83 then local k = floor((u - 7.33) * 10) sparkle(FX + 7, FY + 1, SIZE[k + 1]) end end end function render() if not BG then if STAGE > 1 then gfx.blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if not BG then PART = gfx.snapshot(0, 0, W, H) end gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS if cfg.ripen ~= false then fruit(t) else gfx.blit_indexed(FRONT, CPAL, FX - 7, FY, 14, 14, CN, CN, true) end coin(t) for i = 1, #SPX do local k = (t + i * 0.97) % 2.33 if k < 0.5 then sparkle(SPX[i], SPY[i], SIZE[floor(k * 10) + 1]) end end end