info = { type = "image", title = "Red squid", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A red squid jets upward through the deep sea", api = 33, } local tr = ({ sk = { ["Squid colour"] = "Farba chobotnice", ["Pulse rate"] = "Rýchlosť pulzu", ["Deep lights"] = "Hlbinné svetlá", ["Marine snow"] = "Morský sneh" }, cs = { ["Squid colour"] = "Barva chobotnice", ["Pulse rate"] = "Rychlost pulzu", ["Deep lights"] = "Hlubinná světla", ["Marine snow"] = "Mořský sníh" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Squid colour"), type = "color", default = "#D2263A" }, { key = "speed", label = T("Pulse rate"), type = "int", min = 1, max = 5, default = 3 }, { key = "lights", label = T("Deep lights"), type = "int", min = 0, max = 8, default = 5 }, { key = "snow", label = T("Marine snow"), type = "bool", default = true }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local GOLD, PUPIL = { 255, 214, 92 }, { 12, 6, 6 } local SPOT = { 255, 176, 96 } local SUCK = { 255, 214, 170 } local function c3(c) return c[1], c[2], c[3] end local function span(xa, xb, y, r, g, b) if xb <= xa then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local TONE, DARK, HI, FIN = { 210, 38, 58 }, { 122, 22, 34 }, { 232, 119, 104 }, { 220, 76, 78 } local P, DD = 45, 15 local NL, SNOW_ON = 5, true local function mix(c, t, k) return { floor(c[1] + (t[1] - c[1]) * k), floor(c[2] + (t[2] - c[2]) * k), floor(c[3] + (t[3] - c[3]) * k) } end function on_settings() local c = cfg.tone or { r = 210, g = 38, b = 58 } TONE = { c.r, c.g, c.b } DARK = { floor(c.r * 0.58), floor(c.g * 0.58), floor(c.b * 0.58) } HI = mix(TONE, { 255, 200, 150 }, 0.5) FIN = mix(TONE, HI, 0.45) P = ({ 66, 54, 45, 38, 32 })[cfg.speed or 3] DD = P * 0.33 NL = cfg.lights or 5 SNOW_ON = cfg.snow ~= false end local MW = { 0.6, 1.4, 2.1, 2.7, 3.2, 3.6, 3.9, 4.1, 4.25, 4.3, 4.3, 4.2, 4.0, 3.7, 3.4, 3.0, 2.7, 2.5, 2.4 } local FW = { 1, 2.2, 3.4, 4.2, 4.6, 4.3, 3.5, 2.4, 1.2, 0.4 } local HW = { 2.6, 3.4, 3.6, 3.2, 2.4 } local SPOTS = { { 5, -0.4 }, { 7, 0.3 }, { 9, -0.1 }, { 11, 0.5 }, { 13, -0.45 }, { 15, 0.1 } } local ARMS = { { -3.2, -8, 13, 0.0 }, { -1.9, -4.5, 15, 1.6 }, { -0.7, -1.5, 24, 2.4, true }, { 0.7, 1.5, 24, 3.2, true }, { 1.9, 4.5, 15, 4.8 }, { 3.2, 8, 13, 5.6 }, } local function draw_arm(a, f, cy, open, tight) local bx, tip, L, ph, tent = a[1], a[2], a[3], a[4], a[5] local n = floor(L * (1 + 0.22 * tight)) local amp = (tent and 2.8 or 2.1) * (1 - 0.75 * tight) local thick = floor(n * 0.4) for j = 1, n do local u = j / n local x = 32 + bx + tip * open * u * sqrt(u) + sin(f * 0.105 - 0.55 * j + ph) * amp * u local y = cy + 3 + j if tent and j > n - 3 then span(x - 1, x + 1, y, c3(HI)) elseif tent then local ix = floor(x - 0.5) local fr = x - 0.5 - ix blend(ix, y, TONE[1], TONE[2], TONE[3], 1 - fr); blend(ix + 1, y, TONE[1], TONE[2], TONE[3], fr) elseif j <= thick then span(x - 1.2, x + 1.2, y, c3(TONE)) else local ix = floor(x - 0.5) local fr = x - 0.5 - ix blend(ix, y, FIN[1], FIN[2], FIN[3], 1 - fr); blend(ix + 1, y, FIN[1], FIN[2], FIN[3], fr) end end end local NS = 22 local SX, SY, SK, SPH = {}, {}, {}, {} for i = 1, NS do local h = hash32(i * 0x9E3779B1) SX[i], SY[i] = (h & 63), ((h >> 6) & 127) % 72 SK[i] = ({ 0.5, 0.8, 1.1, 1.4 })[(h >> 14) % 4 + 1] SPH[i] = ((h >> 16) & 255) / 40 end local SNOWC = { { 96, 112, 150 }, { 120, 138, 178 }, { 150, 168, 206 }, { 184, 202, 232 } } local function draw_snow(f, C) for i = 1, NS do local k = SK[i] local y = (SY[i] + C * k) % 72 - 4 local yi = floor(y) local fy = y - yi local x = SX[i] + floor(1.4 * sin(f * 0.02 + SPH[i]) + 0.5) local c = SNOWC[k == 0.5 and 1 or (k == 0.8 and 2 or (k == 1.1 and 3 or 4))] blend(x, yi, c[1], c[2], c[3], 1 - fy) blend(x, yi + 1, c[1], c[2], c[3], fy) end end local LC = { { 60, 230, 255 }, { 176, 112, 255 }, { 84, 255, 184 } } local LX, LY, LCOL = {}, {}, {} for i = 1, 8 do local h = hash32(i * 0x85EBCA6B) LX[i], LY[i] = 4 + (h & 63) % 56, ((h >> 8) & 127) % 76 LCOL[i] = LC[i % 3 + 1] end local function draw_lights(f, C) for i = 1, NL do local y = (LY[i] + C * 0.3) % 76 - 6 local yi = floor(y) local fy = y - yi local x = LX[i] local p = 0.55 + 0.45 * sin(f * (0.045 + i * 0.006) + i * 1.7) local c = LCOL[i] local a0, a1 = p * (1 - fy), p * fy blend(x, yi, c[1], c[2], c[3], a0); blend(x, yi + 1, c[1], c[2], c[3], a1) blend(x - 1, yi, c[1], c[2], c[3], a0 * 0.35); blend(x - 1, yi + 1, c[1], c[2], c[3], a1 * 0.35) blend(x + 1, yi, c[1], c[2], c[3], a0 * 0.35); blend(x + 1, yi + 1, c[1], c[2], c[3], a1 * 0.35) blend(x, yi - 1, c[1], c[2], c[3], a0 * 0.35); blend(x, yi + 2, c[1], c[2], c[3], a1 * 0.35) end end local function draw_squid(f, cy, x, tight) local wf = 1 - 0.3 * (x < 0.28 and sin(3.1416 * x / 0.28) or 0) local open = 0.35 + 0.65 * (1 - tight) for k = 1, 6 do draw_arm(ARMS[k], f, cy, open, tight) end local flap = 0.8 + 0.2 * sin(f * 0.22) for r = 0, 9 do local hw = MW[r + 1] * wf local fe = FW[r + 1] * flap local y = cy - 20 + r span(32 - hw - fe, 32 - hw, y, c3(FIN)); span(32 + hw, 32 + hw + fe, y, c3(FIN)) end for r = 0, 18 do local hw = MW[r + 1] * wf local y = cy - 20 + r span(32 - hw, 32 + hw, y, c3(TONE)) local dw = floor(hw * 0.4 + 0.5) if dw > 0 then rect(floor(32 - hw) + 1, y, dw, 1, c3(DARK)) end if r >= 2 and r <= 16 then rect(floor(32 + hw * 0.25 + 0.5), y, floor(hw * 0.3 + 1.0), 1, c3(HI)) end end for k = 1, 6 do local s = SPOTS[k] local a = 0.5 + 0.5 * sin(f * 0.1 + k * 1.9) a = a * a local hw = MW[s[1] + 1] * wf px(floor(32 + s[2] * hw), cy - 20 + s[1], floor(TONE[1] + (SPOT[1] - TONE[1]) * a), floor(TONE[2] + (SPOT[2] - TONE[2]) * a), floor(TONE[3] + (SPOT[3] - TONE[3]) * a)) end for r = 0, 4 do span(32 - HW[r + 1], 32 + HW[r + 1], cy - 1 + r, c3(r >= 3 and DARK or TONE)) end if f % 150 < 3 then rect(27, cy + 1, 3, 1, c3(DARK)); rect(34, cy + 1, 3, 1, c3(DARK)) else rect(27, cy, 3, 3, c3(GOLD)); px(28, cy + 1, c3(PUPIL)) rect(34, cy, 3, 3, c3(GOLD)); px(35, cy + 1, c3(PUPIL)) px(27, cy, c3(DARK)); px(29, cy, c3(DARK)); px(27, cy + 2, c3(DARK)); px(29, cy + 2, c3(DARK)) px(34, cy, c3(DARK)); px(36, cy, c3(DARK)); px(34, cy + 2, c3(DARK)); px(36, cy + 2, c3(DARK)) end if tight > 0.05 then for k = 1, 3 do local d = (k * 3) * (0.5 + x * 2) blend(32 + (k - 2) * 2, floor(cy + 5 + d), 170, 214, 255, 0.7 * tight) end end end function render() local f = frame * STEP_HZ // FPS gfx.clear(0, 0, 0) local n = (f // P) % 1000 local x = (f % P) / P local dn = 12 * (x * x / 2 - 2 * x * x * x / 3 + x * x * x * x / 4) local C = DD * (n + x) local cy = floor(24 - DD * (dn - x) + 0.5) local tight = x < 0.5 and sin(3.1416 * x / 0.5) or 0 if SNOW_ON then draw_snow(f, C) end draw_lights(f, C) draw_squid(f, cy, x, tight) end