info = { type = "image", title = "Pearl clam", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A clam slowly opens to show its glowing pearl", api = 33, } local tr = ({ sk = { ["Opening speed"] = "Rýchlosť otvárania", ["Shell colour"] = "Farba mušle", ["Pearl colour"] = "Farba perly" }, cs = { ["Opening speed"] = "Rychlost otevírání", ["Shell colour"] = "Barva mušle", ["Pearl colour"] = "Barva perly" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Opening speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "shell", label = T("Shell colour"), type = "color", default = "#B8508C" }, { key = "pearl", label = T("Pearl colour"), type = "color", default = "#FFDDB0" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local CX, CY = 32, 43 local RX = 22 local PERIOD = { 32, 24, 18, 13, 9 } local function mix(a, b, t) return { floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) } end local function c3(c) return c[1], c[2], c[3] end local WHITE = { 255, 255, 255 } local SAND, SAND_L, SAND_D = { 226, 196, 140 }, { 240, 214, 160 }, { 196, 162, 112 } local WEED, WEED_L = { 40, 166, 110 }, { 84, 200, 130 } local BUB = { 206, 240, 255 } local OUT, RIB, RIM, INN, INN_R, LO, LO_L, INSIDE, PEARL, PEARL_L, PEARL_D, GLOW local BG = nil local COLS = {} for x = 11, 53, 2 do local u = (x + 1 - CX) / RX if u > 0.995 then u = 0.995 elseif u < -0.995 then u = -0.995 end local s = sqrt(1 - u * u) COLS[#COLS + 1] = { x = x, yb = CY - 6 * s, d = 12 * s + 1, r = 26 * s ^ 0.55 * (0.9 + 0.1 * math.abs(sin(3.14159 * (x - 11) / 7))), k = (#COLS) % 3, } end function on_settings() local sc = cfg.shell or { r = 184, g = 80, b = 140 } local pc = cfg.pearl or { r = 255, g = 221, b = 176 } local c = { sc.r, sc.g, sc.b } OUT, RIB = c, mix(c, { 0, 0, 0 }, 0.22) RIM = mix(c, WHITE, 0.5) INN, INN_R = mix(c, WHITE, 0.42), mix(c, WHITE, 0.22) LO = mix(c, { 40, 176, 164 }, 0.8) LO_L = mix(LO, WHITE, 0.35) INSIDE = mix(c, WHITE, 0.8) local p = { pc.r, pc.g, pc.b } PEARL, PEARL_L, PEARL_D = p, mix(p, WHITE, 0.7), mix(p, { 150, 90, 70 }, 0.4) GLOW = mix(p, WHITE, 0.3) BG = nil end local function ray(x0, w, slant) for y0 = 0, 44, 4 do blend_rect(x0 + floor(y0 * slant), y0, w + (y0 >> 4), 4, 255, 255, 255, 0.10) end end local function build_bg() for y = 0, 51 do local t = y / 51 rect(0, y, W, 1, floor(44 - 26 * t + 0.5), floor(138 - 48 * t + 0.5), floor(198 - 46 * t + 0.5)) end ray(10, 5, 0.30); ray(30, 7, 0.28); ray(48, 4, 0.30) for x = 0, W - 1 do local ys = 51 + floor(1.2 * sin(x * 0.2) + 0.5) rect(x, ys, 1, H - ys, c3(SAND)) end for x = CX - RX, CX + RX - 1 do local u = (x + 0.5 - CX) / RX local s = sqrt(1 - u * u) local y1 = CY + 6 * s local y2 = CY + 11 * s rect(x, CY, 1, floor(y2 - CY + 0.5), c3((x % 4 == 0) and LO_L or LO)) local hh = floor(6 * s + 0.5) if hh > 0 then rect(x, CY - hh, 1, 2 * hh, c3(INSIDE)) end rect(x, floor(y1 + 0.5), 1, 1, c3(LO_L)) end for x = 0, W - 1 do local ys = 53 + floor(0.8 * sin(x * 0.35 + 1) + 0.5) rect(x, ys, 1, H - ys, c3(SAND_L)) px(x, ys + 3 + (x * 7) % 5, c3(SAND_D)) end rect(6, 59, 4, 1, c3(SAND_D)); rect(40, 60, 6, 1, c3(SAND_D)); rect(22, 61, 5, 1, c3(SAND_D)) for _, p in ipairs({ { 52, 58 }, { 51, 59 }, { 53, 59 }, { 52, 60 }, { 51, 61 }, { 53, 61 } }) do px(p[1], p[2], 244, 130, 96) end px(52, 59, 255, 190, 140) rect(14, 58, 3, 2, 170, 140, 104); px(14, 58, 214, 184, 140) BG = gfx.snapshot(0, 0, W, H) end local B = {} for i = 1, 8 do local h = hash32(i * 0x9E3779B1) local lane = { 5, 12, 53, 59, 19, 46, 27, 38 } B[i] = { x = lane[i], y0 = i > 6 and 40 or 52, v = (8 + umod(h, 9)) / STEP_HZ, ph = umod(h >> 8, 628) / 100.0, off = umod(h >> 16, 400), sh = i > 6, } end local function bubble(b, t, o) local rise = b.y0 + 4 local cyc = rise / b.v + 20 local u = ((t + b.off * 3) % cyc) * b.v local y = b.y0 - u if y < -3 then return end local al = 0.85 if b.sh then if o < 0.55 then return end al = 0.85 * (o - 0.55) / 0.45 end local x = floor(b.x + 1.6 * sin(t * 0.07 + b.ph) + 0.5) local yi = floor(y) local f = y - yi blend_rect(x, yi, 2, 1, BUB[1], BUB[2], BUB[3], al * (1 - f)) blend_rect(x, yi + 1, 2, 1, BUB[1], BUB[2], BUB[3], al) blend_rect(x, yi + 2, 2, 1, BUB[1], BUB[2], BUB[3], al * f) px(x, yi + 1, 255, 255, 255) end local function weed(x0, hgt, ph, t) for k = 0, hgt - 1 do local sway = floor(1.6 * (k / hgt) * sin(t * 0.06 + ph + k * 0.35) + 0.5) rect(x0 + sway, 52 - k * 3, 2, 3, c3(k % 2 == 0 and WEED or WEED_L)) end end local SP = { { 8, 22, 0.0 }, { 55, 17, 2.1 }, { 21, 9, 4.0 }, { 45, 8, 5.3 } } local function sparkles(t, o) for i = 1, #SP do local s = SP[i] local v = sin(t * 0.1 + s[3]) * o local x, y = s[1], s[2] if v > 0.25 then px(x, y, 255, 255, 255) if v > 0.55 then rect(x - 1, y, 3, 1, 236, 250, 255); rect(x, y - 1, 1, 3, 236, 250, 255) end if v > 0.85 then px(x - 2, y, 190, 230, 255); px(x + 2, y, 190, 230, 255) px(x, y - 2, 190, 230, 255); px(x, y + 2, 190, 230, 255) end end end end local PW = { 1, 2, 3, 3, 3, 2, 1 } local function pearl(t, o) local cx, cy = CX, 41 if o > 0.02 then local pulse = 0.5 + 0.5 * sin(t * 0.09) local a1 = (0.16 + 0.08 * pulse) * o local a2 = (0.10 + 0.06 * pulse) * o for k = -7, 7 do local w = floor(sqrt(64 - k * k) + 0.5) blend_rect(cx - w, cy + k, 2 * w + 1, 1, GLOW[1], GLOW[2], GLOW[3], a2) end for k = -5, 5 do local w = floor(sqrt(30 - k * k) + 0.5) blend_rect(cx - w, cy + k, 2 * w + 1, 1, GLOW[1], GLOW[2], GLOW[3], a1) end end for k = 1, 7 do local w = PW[k] rect(cx - w, cy - 4 + k, 2 * w + 1, 1, c3(PEARL)) end rect(cx - 2, cy + 2, 4, 1, c3(PEARL_D)); rect(cx - 1, cy + 3, 3, 1, c3(PEARL_D)) rect(cx - 2, cy - 2, 2, 2, c3(PEARL_L)) px(cx - 2, cy - 2, 255, 255, 255) end local function lid(a) local ca, sa = cos(a), sin(a) local outer = a < 0.3 for i = 1, #COLS do local c = COLS[i] local yt = c.yb + c.d * ca - c.r * sa local lo, hi = yt, c.yb if yt > c.yb then lo, hi = c.yb, yt end local yi = floor(lo) local f = lo - yi local body, rim if outer then body = (c.k == 0) and RIB or OUT rim = RIM else body = (c.k == 0) and INN_R or INN rim = RIM end local x = c.x blend_rect(x, yi, 2, 1, rim[1], rim[2], rim[3], 1 - f) local hgt = floor(hi - yi + 0.5) - 1 if hgt < 1 then hgt = 1 end rect(x, yi + 1, 2, 1, c3(rim)) rect(x, yi + 2, 2, hgt, c3(body)) if not outer then rect(x, floor(hi + 0.5) - 2, 2, 2, c3(mix(body, OUT, 0.5))) end end end function render() if not BG then build_bg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) local per = (PERIOD[cfg.speed or 3] or 18) * STEP_HZ local p = (t / per + 0.35) % 1 local d = math.abs(p - 0.5) * 2 local o = (0.85 - d) / 0.55 if o < 0 then o = 0 elseif o > 1 then o = 1 end o = o * o * (3 - 2 * o) weed(3, 6, 0.0, t); weed(7, 4, 1.7, t); weed(57, 5, 3.1, t); weed(60, 3, 4.4, t) pearl(t, o) lid(o * 1.83) sparkles(t, 0.5 + 0.5 * o) for i = 1, #B do bubble(B[i], t, o) end end