info = { type = "image", title = "Octopus glide", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "An octopus jets across the sea past the seabed", api = 33, } local tr = ({ sk = { ["Colour"] = "Farba", ["Red"] = "Červená", ["Orange"] = "Oranžová", ["Purple"] = "Fialová", ["Pace"] = "Tempo", ["Calm"] = "Pokojné", ["Quick"] = "Rýchle", ["Ink"] = "Atrament" }, cs = { ["Colour"] = "Barva", ["Red"] = "Červená", ["Orange"] = "Oranžová", ["Purple"] = "Fialová", ["Pace"] = "Tempo", ["Calm"] = "Klidné", ["Quick"] = "Rychlé", ["Ink"] = "Inkoust" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "colour", label = T("Colour"), type = "int", default = "red", opts = { keys = { "red", "orange", "purple" }, T("Red"), T("Orange"), T("Purple") } }, { key = "pace", label = T("Pace"), type = "int", default = "calm", opts = { keys = { "calm", "quick" }, T("Calm"), T("Quick") } }, { key = "ink", label = T("Ink"), type = "bool", default = true }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, ceil, sin, sqrt, exp, max, min, abs = math.floor, math.ceil, math.sin, math.sqrt, math.exp, math.max, math.min, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function lerp3(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 span(y, a, b, r, g, bl, al, h) h = h or 1 local fa, fb = floor(a), floor(b) if fa == fb then if b > a then blend_rect(fa, y, 1, h, r, g, bl, (b - a) * al) end return end if a > fa then blend_rect(fa, y, 1, h, r, g, bl, (1 - a + fa) * al); fa = fa + 1 end if fb > fa then blend_rect(fa, y, fb - fa, h, r, g, bl, al) end if b > fb then blend_rect(fb, y, 1, h, r, g, bl, (b - fb) * al) end end local function dot(x, y, r, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, r, g, b, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, r, g, b, a * fx * (1 - fy)) blend(ix, iy + 1, r, g, b, a * (1 - fx) * fy) blend(ix + 1, iy + 1, r, g, b, a * fx * fy) end local GW, GH = 32, 24 local PAL = {} local COLOURS = { red = { 214, 52, 50 }, orange = { 238, 124, 40 }, purple = { 150, 84, 192 } } local function put(g, x, y, c) if x >= 0 and x < GW and y >= 0 and y < GH then g[y * GW + x + 1] = c end end local function box(g, x, y, w, h, c) for j = y, y + h - 1 do for i = x, x + w - 1 do put(g, i, j, c) end end end local function ell(g, cx, cy, rx, ry, c) for j = floor(cy - ry), ceil(cy + ry) do for i = floor(cx - rx), ceil(cx + rx) do local dx, dy = (i - cx) / rx, (j - cy) / ry if dx * dx + dy * dy <= 1 then put(g, i, j, c) end end end end local function seg(g, x0, y0, x1, y1, c, th) local n = max(abs(x1 - x0), abs(y1 - y0)) * 2 for s = 0, n do local u = s / n box(g, floor(x0 + (x1 - x0) * u + 0.5), floor(y0 + (y1 - y0) * u + 0.5), th, th, c) end end local START = { { 25, 15 }, { 23, 16 }, { 21, 16 }, { 15, 13 }, { 22, 16 }, { 20, 16 } } local ENDS = { O = { { 9, 22, 1 }, { 5, 20, 2 }, { 2, 15, 1 }, { 1, 9, -2 }, { 7, 23, 2 }, { 3, 19, 2 } }, H = { { 7, 19, 0 }, { 3, 18, 1 }, { 1, 16, 0 }, { 1, 12, -1 }, { 6, 21, 1 }, { 2, 18, 1 } }, S = { { 2, 15, 0 }, { 1, 16, 0 }, { 1, 15, 0 }, { 1, 14, 0 }, { 2, 17, 0 }, { 1, 16, 0 } }, } local ORDER = { 4, 5, 6, 3, 2, 1 } local function bake(key) local g = {} for i = 1, GW * GH do g[i] = 0 end local E = ENDS[key] for n = 1, 6 do local i = ORDER[n] local s, e = START[i], E[i] local c = n <= 3 and 3 or 2 local x1, y1 = s[1] + (e[1] - s[1]) * 0.4, s[2] + (e[2] - s[2]) * 0.4 + e[3] local x2, y2 = s[1] + (e[1] - s[1]) * 0.75, s[2] + (e[2] - s[2]) * 0.75 + e[3] * 0.6 seg(g, s[1], s[2], x1, y1, c, 2) seg(g, x1, y1, x2, y2, c, 2) seg(g, x2, y2, e[1], e[2], c, 1) end local hx, hrx, hry = 21, 8, 7.2 if key == "S" then hx, hrx, hry = 20.5, 7, 8 end ell(g, hx, 10, hrx, hry, 2) for y = 14, GH - 1 do for x = floor(hx - hrx), ceil(hx + hrx) do local dx, dy = (x - hx) / hrx, (y - 10) / hry if dx * dx + dy * dy <= 1 then put(g, x, y, 3) end end end ell(g, 18, 5.6, 2.6, 1.4, 4) put(g, 17, 9, 7); put(g, 19, 12, 7); put(g, 22, 13, 7); put(g, 15, 11, 7) if key == "S" then ell(g, 25, 9, 2.4, 1.4, 5); put(g, 26, 9, 6) else ell(g, 25, 9, 2.4, 2.8, 5); box(g, 26, 8, 1, 3, 6) end local o = {} for y = 0, GH - 1 do for x = 0, GW - 1 do if g[y * GW + x + 1] == 0 and (x > 0 and g[y * GW + x] ~= 0 or x < GW - 1 and g[y * GW + x + 2] ~= 0 or y > 0 and g[(y - 1) * GW + x + 1] ~= 0 or y < GH - 1 and g[(y + 1) * GW + x + 1] ~= 0) then o[#o + 1] = y * GW + x + 1 end end end for i = 1, #o do g[o[i]] = 1 end local ed = {} for y = 0, GH - 1 do for x = 0, GW - 1 do local v = g[y * GW + x + 1] if v ~= 0 and (x == 0 or g[y * GW + x] == 0 or x == GW - 1 or g[y * GW + x + 2] == 0) then ed[#ed + 1] = x; ed[#ed + 1] = y; ed[#ed + 1] = v end end end for i = 1, #ed, 3 do g[ed[i + 1] * GW + ed[i] + 1] = 0 end local rows = {} for y = 0, GH - 1 do rows[y + 1] = string.char(table.unpack(g, y * GW + 1, y * GW + GW)) end return { core = table.concat(rows), ed = ed } end local POSES = {} local function sprite(key, x, y) local P = POSES[key] if not P then P = bake(key); POSES[key] = P end local ix = floor(x) local f = x - ix blit(P.core, PAL, ix, y, GW, GH, GW, GH, true) local ed, a = P.ed, 1 - f for i = 1, #ed, 3 do local c = ed[i + 2] * 3 local ex, ey, r, g, b = ix + ed[i], y + ed[i + 1], PAL[c + 1], PAL[c + 2], PAL[c + 3] blend(ex, ey, r, g, b, a) if f > 0.02 then blend(ex + 1, ey, r, g, b, f) end end end local WEED = {} local CLUMPS = { { 4, 0 }, { 17, 2 }, { 30, 1 }, { 45, 3 }, { 58, 0 } } do local H0, SW = { 12, 17, 14 }, { -3, 0, 3 } for c = 1, #CLUMPS do WEED[c] = {} for p = 1, 3 do local l = {} for f = 1, 3 do local bx, hh = CLUMPS[c][1] + (f - 2) * 3, H0[f] + (c % 2) local sw = SW[p] + (f - 2) local h = 0 while h <= hh do local x = bx + floor(sw * (h / hh) ^ 1.6 + 0.5) local h1 = h while h1 < hh and bx + floor(sw * ((h1 + 1) / hh) ^ 1.6 + 0.5) == x do h1 = h1 + 1 end local k = (h + h1) / 2 / 18 l[#l + 1] = { x, 58 - h1, h1 - h + 1, floor(28 + 40 * k), floor(98 + 60 * k), floor(70 + 30 * k) } h = h1 + 1 end end WEED[c][p] = l end end end local BG local function build_bg() for y = 0, H - 1 do rect(0, y, W, 1, lerp3({ 44, 156, 180 }, { 10, 52, 92 }, y / (H - 1)) ) end for y = 54, H - 1 do rect(0, y, W, 1, lerp3({ 158, 142, 108 }, { 96, 84, 68 }, (y - 54) / 9)) end rect(0, 54, W, 1, 184, 168, 130) for _, r in ipairs({ { 9, 56, 7, 4 }, { 50, 55, 9, 5 }, { 36, 59, 5, 3 } }) do for j = floor(-r[4]), r[4] do local hw = r[3] * sqrt(1 - (j / (r[4] + 0.5)) ^ 2) local y = r[2] + j local k = (j + r[4]) / (2 * r[4]) local c = k < 0.3 and { 130, 142, 150 } or (k < 0.7 and { 98, 108, 120 } or { 62, 70, 86 }) rect(floor(r[1] - hw + 0.5), y, floor(2 * hw + 0.5), 1, c[1], c[2], c[3]) end end for i = 1, 14 do local k = hash32(i * 0x9E3779B1) px(umod(k, W), 60 + umod(k >> 8, 4), 190, 172, 134) end for _, s in ipairs({ { 24, 60 } }) do local x, y = s[1], s[2] rect(x - 1, y, 3, 1, 230, 120, 70); px(x, y - 1, 230, 120, 70); px(x, y + 1, 230, 120, 70) px(x - 2, y - 1, 230, 120, 70); px(x + 2, y - 1, 230, 120, 70) end BG = gfx.snapshot(0, 0, W, H) end local function shafts(t) for i = 1, 2 do local sx = i == 1 and 14 or 44 for y = 0, 48, 2 do local c = sx + 0.55 * y + 3 * sin(t * 0.012 + i * 2) span(y, c - 3 - y * 0.05, c + 3 + y * 0.05, 200, 244, 255, 0.07 * (1 - y / 50), 2) end end end local function weed(t) for c = 1, #CLUMPS do local p = ({ 2, 3, 2, 1 })[(t // 11 + CLUMPS[c][2]) % 4 + 1] local l = WEED[c][p] for i = 1, #l do local r = l[i] rect(r[1], r[2], 1, r[3], r[4], r[5], r[6]) end end end local L, T0, V0, XS = 30, 10, 2.2, -34 local D, NS, C = 0, 5, 0 function on_settings() local k = COLOURS[cfg.colour] or COLOURS.red local sh = { floor(k[1] * 0.66), floor(k[2] * 0.66), floor(k[3] * 0.7) } local hl = { floor(k[1] + (255 - k[1]) * 0.38), floor(k[2] + (255 - k[2]) * 0.38), floor(k[3] + (255 - k[3]) * 0.38) } local sp = { floor(k[1] + (255 - k[1]) * 0.2), floor(k[2] + (255 - k[2]) * 0.2), floor(k[3] + (255 - k[3]) * 0.2) } PAL = { 0, 0, 0, floor(k[1] * 0.3), floor(k[2] * 0.3), floor(k[3] * 0.35), k[1], k[2], k[3], sh[1], sh[2], sh[3], hl[1], hl[2], hl[3], 240, 244, 248, 20, 14, 24, sp[1], sp[2], sp[3] } if cfg.pace == "quick" then L, T0, V0 = 22, 8, 2.6 else L, T0, V0 = 30, 10, 2.2 end D = V0 * T0 * (1 - exp(-L / T0)) NS = ceil((W - XS) / D) C = NS * L + 66 end local function xpos(t) local k = t // L return XS + k * D + V0 * T0 * (1 - exp(-(t - k * L) / T0)) end local function ypos(t, ph) return 17 + 5 * sin(t * 0.03 + ph) end local INK = { { -6, 0, 5 }, { -2, -3, 6 }, { 3, 2, 5 }, { -9, 3, 4 }, { 0, 5, 4 } } local function ink(t, ph) local t0 = 2 * L + 2 local age = t - t0 if age < 0 or age >= 90 then return end local ox, oy = xpos(t0) + 8, ypos(t0, ph) + 12 local g = 1 - exp(-age / 14) local a = 0.62 * (1 - age / 90) ^ 1.3 for i = 1, #INK do local b = INK[i] local cx, cy, r = ox + b[1] * g * 1.4, oy + b[2] * g - age * 0.03, 2 + (b[3] - 2) * g for j = -floor(r), floor(r) do local hw = sqrt(max(0, r * r - j * j)) span(floor(cy) + j, cx - hw, cx + hw, 22, 14, 48, a) end end end local function bubbles(t, ph) for k = max(0, (t - 64) // L), min(NS - 1, t // L) do for j = 0, 3 do local tj = t - k * L - j * 3 if tj >= 0 and tj < 64 then local rt = k * L + j * 3 local bx = xpos(rt) + 12 + j * 2 + 1.2 * sin(tj * 0.25 + j * 2) local by = ypos(rt, ph) + 9 - tj * 0.42 local a = min(1, (64 - tj) / 14) dot(bx, by, 190, 232, 246, 0.9 * a) if j % 2 == 0 then dot(bx + 1.5, by, 190, 232, 246, 0.35 * a); dot(bx, by + 1.5, 190, 232, 246, 0.35 * a) end end end end end local VENTS = { { 12, 0 }, { 47, 20 } } local function vents(t) for i = 1, #VENTS do for j = 0, 2 do local u = (t * 0.32 + VENTS[i][2] + j * 16) % 48 dot(VENTS[i][1] + 1.3 * sin(u * 0.3 + j), 54 - u, 190, 232, 246, 0.8 * min(1, (48 - u) / 12)) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame % C local n = frame // C local ph = umod(hash32(n * 0x9E3779B1), 628) / 100 shafts(frame) weed(frame) vents(frame) if t < NS * L + 40 then bubbles(t, ph) end if cfg.ink ~= false and hash32(n * 0x85EBCA6B) % 3 == 0 then ink(t, ph) end if t >= NS * L then return end local x = xpos(t) if x >= W then return end local tau = t % L local key = "O" if tau < 3 then key = "S" elseif tau < 8 or tau >= L - 4 then key = "H" end local y = floor(ypos(t, ph) - 3 + 0.1 * tau + 0.5) sprite(key, x, y) end