info = { type = "image", title = "Alchemy flask", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Glowing elixir bubbling in a glass flask", api = 44, } local tr = ({ sk = { ["Elixir"] = "Elixír", ["Bubbles"] = "Bublinky", ["Sparks"] = "Iskry", ["Purple"] = "Fialový", ["Green"] = "Zelený", ["Orange"] = "Oranžový" }, cs = { ["Elixir"] = "Elixír", ["Bubbles"] = "Bublinky", ["Sparks"] = "Jiskry", ["Purple"] = "Fialový", ["Green"] = "Zelený", ["Orange"] = "Oranžový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "elixir", label = T("Elixir"), type = "int", default = "purple", opts = { keys = { "purple", "green", "orange" }, T("Purple"), T("Green"), T("Orange") } }, { key = "bubbles", label = T("Bubbles"), type = "int", min = 0, max = 9, default = 7 }, { key = "sparks", label = T("Sparks"), type = "int", min = 0, max = 10, default = 8 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, exp, abs, max, min, atan = math.floor, math.sin, math.sqrt, math.exp, math.abs, math.max, math.min, math.atan local hash32, umod = math.hash32, math.umod local char, unpack, concat, byte = string.char, table.unpack, table.concat, string.byte local W, H, FPS = gfx.W, gfx.H, gfx.FPS local ELIX = { purple = { { 50, 6, 92 }, { 118, 28, 188 }, { 214, 138, 255 } }, green = { { 6, 60, 28 }, { 28, 168, 68 }, { 170, 255, 150 } }, orange = { { 104, 26, 4 }, { 232, 104, 10 }, { 255, 208, 96 } }, } local E, ED, EM, EL local R, CX, CY, NW, LEVEL = 13, 32, 37, 4.5, 33 local FX0, FY0, FW, FH = 17, 6, 31, 46 local GLASS = { 190, 212, 242 } local BG, SCENE, FL, STAGE = nil, nil, nil, 1 local function lerp3(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function bg_a() rect(0, 0, W, 58, 54, 42, 60) for row = 0, 7 do local y = row * 7 + 3 rect(0, y, W, 1, 70, 56, 76) for x = (row % 2) * 7, W, 14 do rect(x, y + 1, 1, 6, 70, 56, 76) end end rect(0, 14, W, 2, 104, 68, 44); rect(0, 16, W, 1, 36, 28, 40); rect(0, 14, W, 1, 134, 92, 60) rect(4, 7, 5, 7, 40, 92, 84); rect(5, 5, 3, 2, 40, 92, 84); px(5, 8, 150, 210, 196) rect(52, 5, 7, 9, 104, 44, 60); rect(54, 3, 3, 2, 104, 44, 60); px(53, 6, 216, 150, 168) rect(14, 9, 4, 5, 150, 120, 60); px(14, 9, 226, 196, 120) rect(46, 8, 4, 6, 70, 84, 130); px(46, 8, 150, 170, 220) BG = snapshot(0, 0, W, H) end local function bg_b() blit_rgb(BG, 0, 0, W, H) for ring = 0, 4 do local r = 34 - ring * 3 for y = -r, r, 2 do local hw = floor(sqrt(r * r - y * y)) blend_rect(CX - hw, 30 + y, 2 * hw, 2, EL[1], EL[2], EL[3], 0.032) end end BG = snapshot(0, 0, W, H) end local function bg_c() blit_rgb(BG, 0, 0, W, H) for ring = 5, 8 do local r = 34 - ring * 3 for y = -r, r, 2 do local hw = floor(sqrt(r * r - y * y)) blend_rect(CX - hw, 30 + y, 2 * hw, 2, EL[1], EL[2], EL[3], 0.032) end end BG = snapshot(0, 0, W, H) end local function bg_d() blit_rgb(BG, 0, 0, W, H) rect(17, 45, 30, 1, 92, 90, 104); rect(17, 44, 30, 1, 138, 136, 150) rect(17, 45, 1, 13, 70, 68, 80); rect(46, 45, 1, 13, 70, 68, 80) rect(17, 46, 1, 12, 44, 42, 52); rect(46, 46, 1, 12, 44, 42, 52) rect(0, 58, W, 1, 134, 92, 58) for y = 59, 63 do rect(0, y, W, 1, 96 - (y - 59) * 7, 62 - (y - 59) * 5, 40 - (y - 59) * 4) end for _, x in ipairs({ 11, 27, 43, 58 }) do rect(x, 59, 1, 5, 44, 26, 18) end for y = 0, 2 do blend_rect(CX - 15 + y * 2, 59 + y, 30 - y * 4, 1, EL[1], EL[2], EL[3], 0.16) end rect(48, 55, 15, 3, 110, 30, 44); rect(48, 56, 15, 1, 190, 150, 70); rect(50, 52, 12, 3, 30, 84, 98); rect(50, 53, 12, 1, 150, 190, 200) rect(5, 52, 6, 6, 40, 110, 72); rect(7, 49, 2, 3, 40, 110, 72); rect(7, 48, 2, 1, 150, 110, 60); px(6, 53, 150, 226, 180) rect(25, 56, 14, 2, 52, 84, 98); rect(26, 55, 12, 1, 52, 84, 98); rect(28, 54, 8, 1, 52, 84, 98) rect(26, 57, 12, 1, 150, 84, 22); rect(27, 56, 10, 1, 120, 64, 16); px(26, 56, 150, 200, 214); px(27, 55, 150, 200, 214) rect(30, 53, 4, 1, 180, 132, 56); rect(31, 52, 2, 1, 64, 52, 40) blend_rect(24, 58, 16, 1, 255, 150, 60, 0.16); blend_rect(27, 59, 10, 1, 255, 150, 60, 0.10) BG = snapshot(0, 0, W, H) end local function flask_px(x, y, br, bg, bb) local dx, dy = x - CX, y - CY local ax = abs(dx) local d = sqrt(dx * dx + dy * dy) local cs = d <= R + 0.5 and (d >= R - 0.5 and R + 0.5 - d or 1) or 0 if cs > 0 then local n = d / R if n > 1 then n = 1 end local nz = sqrt(1 - n * n) local f = (1 - nz) ^ 2.2 local r, g, b = br, bg, bb local inner = R - 1.3 local q = dx / (inner - 0.2) local ytop = LEVEL - 2.0 * sqrt(max(0, 1 - q * q)) if d <= inner and y >= ytop then local n2 = d / inner local nz2 = sqrt(1 - n2 * n2) r, g, b = lerp3(ED, EM, nz2 ^ 0.8) local wf = (dy + 2) / 13 wf = wf < 0 and 0 or (wf > 1 and 1 or wf) local k = 0.38 * wf * wf + 0.4 * nz2 ^ 3 r, g, b = r + (EL[1] - r) * k, g + (EL[2] - g) * k, b + (EL[3] - b) * k local e = 0.5 * exp(-((dx - 4.5) ^ 2 + (dy - 8) ^ 2) / 14) r, g, b = r + (EL[1] - r) * e, g + (EL[2] - g) * e, b + (EL[3] - b) * e if y < LEVEL + 0.6 then r, g, b = r + (EL[1] - r) * 0.55, g + (EL[2] - g) * 0.55, b + (EL[3] - b) * 0.55 end if y < ytop + 1.0 then r, g, b = r + (255 - r) * 0.5, g + (255 - g) * 0.5, b + (255 - b) * 0.5 end end local ag = 0.07 + 0.55 * f r, g, b = r + (GLASS[1] - r) * ag, g + (GLASS[2] - g) * ag, b + (GLASS[3] - b) * ag local ang = atan(dy, dx) if n > 0.66 and n < 0.86 and ang > -2.75 and ang < -1.85 then local a = 0.85 * (1 - abs(n - 0.76) / 0.1) * (1 - abs(ang + 2.3) / 0.45) r, g, b = r + (255 - r) * a, g + (255 - g) * a, b + (255 - b) * a end if n > 0.84 and n < 0.97 and ang > 0.2 and ang < 1.5 then local a = 0.5 * (1 - abs(n - 0.905) / 0.065) * (1 - abs(ang - 0.85) / 0.65) r, g, b = r + (255 - r) * a, g + (170 - g) * a, b + (90 - b) * a end return br + (r - br) * cs, bg + (g - bg) * cs, bb + (b - bb) * cs end if y < 26 and y > 8 and ax <= NW then local nn = ax / NW local f = (1 - sqrt(1 - nn * nn)) ^ 2.2 local ag = 0.06 + 0.6 * f local r, g, b = br + (GLASS[1] - br) * ag, bg + (GLASS[2] - bg) * ag, bb + (GLASS[3] - bb) * ag local tp = min((y - 9) / 4, (24 - y) / 4, 1) local a = 0 if dx > -3.1 and dx < -1.9 and tp > 0 then a = 0.55 * tp elseif dx > 2.4 and dx < 3.2 then a = 0.2 end return r + (255 - r) * a, g + (255 - g) * a, b + (255 - b) * a end if y > 6 and y < 8 and ax <= 5.5 then local t = y < 7 and 0.62 or 0.34 return br + (228 - br) * t, bg + (238 - bg) * t, bb + (252 - bb) * t end return br, bg, bb end local FROWS = {} local function flask_rows(y0, y1) for y = y0, y1 do local row = {} local base = ((FY0 + y) * W + FX0) * 3 + 1 for x = 0, FW - 1 do local i = base + x * 3 local br, bg, bb = byte(BG, i, i + 2) local r, g, b = flask_px(FX0 + x + 0.5, FY0 + y + 0.5, br, bg, bb) row[x * 3 + 1], row[x * 3 + 2], row[x * 3 + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end FROWS[#FROWS + 1] = char(unpack(row)) end end local STAGES = { bg_a, bg_b, bg_c, bg_d } for y0 = 0, FH - 1, 8 do STAGES[#STAGES + 1] = function() flask_rows(y0, min(FH - 1, y0 + 7)) end end STAGES[#STAGES + 1] = function() blit_rgb(BG, 0, 0, W, H) blit_rgb(concat(FROWS), FX0, FY0, FW, FH) SCENE = snapshot(0, 0, W, H) end function on_settings() E = ELIX[cfg.elixir] or ELIX.purple ED, EM, EL = E[1], E[2], E[3] BG, SCENE, STAGE, FROWS = nil, nil, 1, {} end local function hf(i, k) return (hash32(i * 0x9E3779B1 + k * 0x85EBCA6B) & 0x7FFF) / 0x7FFF end local function flame(frm) local v = (frm // 3 + (frm // 7) * 2) % 3 blend_rect(26, 48, 12, 6, 255, 150, 60, 0.07 + v * 0.02) blend_rect(28, 49, 8, 4, 255, 160, 70, 0.08) rect(30, 52, 4, 1, 255, 138, 28); rect(30, 51, 4, 1, 255, 138, 28) rect(31, 50, 2, 1, 255, 150, 40) rect(31, 52, 2, 1, 96, 130, 240); rect(31, 51, 2, 1, 255, 226, 110) if v == 0 then px(31, 49, 255, 190, 60); px(32, 50, 255, 226, 110) elseif v == 1 then rect(31, 49, 2, 1, 255, 190, 60); px(32, 48, 255, 150, 40); px(31, 50, 255, 226, 110) else rect(31, 49, 2, 1, 255, 190, 60); px(31, 48, 255, 150, 40); px(32, 50, 255, 226, 110) end end local function blob(x, y, w, r, g, b, a) local yi = floor(y) local f = y - yi blend_rect(x, yi, w, 1, r, g, b, a * (1 - f)) blend_rect(x, yi + 1, w, 1, r, g, b, a) blend_rect(x, yi + 2, w, 1, r, g, b, a * f) end function render() if not EL then on_settings() end if STAGE <= #STAGES then if SCENE then blit_rgb(SCENE, 0, 0, W, H) elseif BG then blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 if STAGE <= #STAGES then gfx.ready(false) end return end blit_rgb(SCENE, 0, 0, W, H) local s = frame / FPS blend_rect(25, 40, 14, 6, EL[1], EL[2], EL[3], 0.03 + 0.025 * sin(s * 1.7)) for i = 1, cfg.bubbles or 7 do local sp = 5 + hf(i, 1) * 5 local p = (s * sp / 15 + hf(i, 2)) % 1 local y = 48.5 - p * 14.5 local dy = y - CY local hw = sqrt(max(0, 10.5 * 10.5 - dy * dy)) - 1.5 local x = floor(CX + (hf(i, 3) * 2 - 1) * hw * 0.8 + 1.2 * sin(s * 2 + i * 1.7)) local a = y < 36 and 0.62 * max(0, (y - 33.6) / 2.4) or 0.62 if i % 3 == 0 then local yi = floor(y) local f = y - yi for k = 0, 1 do local aa = a * 0.9 * (k == 0 and 1 - f or f) blend_rect(x + 1, yi + k, 2, 1, 255, 255, 255, aa); blend_rect(x + 1, yi + k + 3, 2, 1, 255, 255, 255, aa) blend_rect(x, yi + k + 1, 1, 2, 255, 255, 255, aa); blend_rect(x + 3, yi + k + 1, 1, 2, 255, 255, 255, aa) end else blob(x, y, 2, 255, 255, 255, a * 0.7) end end flame(frame) for i = 0, 4 do local p = (s * 0.42 + i / 5) % 1 local y = 5.5 - p * 9 local x = floor(CX - 1 + 2.6 * sin(p * 5 + i * 2) + 0.5) local w = 3 + floor(p * 4) local a = 0.5 * (1 - p) blob(x - w // 2, y, w, 190 + (EL[1] - 190) * 0.3, 190 + (EL[2] - 190) * 0.3, 200 + (EL[3] - 200) * 0.3, a) end for i = 1, cfg.sparks or 8 do local life = 1.3 + hf(i, 4) * 1.2 local u = (s / life + hf(i, 5)) % 1 local k = floor(s / life + hf(i, 5)) local vx = (hf(i + k * 13, 6) - 0.5) * 22 local vy = -(16 + hf(i + k * 13, 7) * 14) local tau = u * life local x, y = 32 + vx * tau + 0.5, 6 + vy * tau + 9 * tau * tau if y > -1 and y < 40 then local a = (1 - u) ^ 0.7 local m = u < 0.3 and 1 or 0 blend_rect(floor(x), floor(y), 1, 1, EL[1] + (255 - EL[1]) * m, EL[2] + (255 - EL[2]) * m, EL[3] + (255 - EL[3]) * m, a) blend_rect(floor(x - vx * 0.03), floor(y - vy * 0.03), 1, 1, EL[1], EL[2], EL[3], a * 0.45) if u < 0.45 then blend_rect(floor(x) - 1, floor(y), 3, 1, EL[1], EL[2], EL[3], a * 0.35) blend_rect(floor(x), floor(y) - 1, 1, 3, EL[1], EL[2], EL[3], a * 0.35) end end end for i = 1, 3 do local a = sin(s * (1.1 + i * 0.4) + i * 2.1) if a > 0.6 then local gx, gy = ({ 24, 38, 31 })[i], ({ 31, 33, 14 })[i] local al = (a - 0.6) * 2.2 blend_rect(gx, gy, 1, 1, 255, 255, 255, al) blend_rect(gx - 1, gy, 3, 1, 255, 255, 255, al * 0.35) blend_rect(gx, gy - 1, 1, 3, 255, 255, 255, al * 0.35) end end end