info = { type = "image", title = "Lanternfish", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Pink lanternfish glides through the deep dark", api = 33, } local tr = ({ sk = { ["Fish colour"] = "Farba rýb", ["Glow colour"] = "Farba žiary", ["Marine snow"] = "Morský sneh", ["Speed"] = "Rýchlosť" }, cs = { ["Fish colour"] = "Barva ryb", ["Glow colour"] = "Barva záře", ["Marine snow"] = "Mořský sníh", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fish", label = T("Fish colour"), type = "color", default = "#FF6F8E" }, { key = "glow", label = T("Glow colour"), type = "color", default = "#5CF0FF" }, { key = "snow", label = T("Marine snow"), type = "int", min = 0, max = 40, default = 24 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local floor, sin, sqrt, max, abs = math.floor, math.sin, math.sqrt, math.max, math.abs local hash32, umod = math.hash32, math.umod local px, blend = gfx.px, gfx.blend local blit, bmask = gfx.blit_indexed, gfx.blend_mask local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU, PI = 6.283185, 3.141593 local function pack(w, h, get) local rows, row = {}, {} for y = 0, h - 1 do for x = 0, w - 1 do row[x + 1] = get(x, y) end rows[y + 1] = string.char(table.unpack(row, 1, w)) end local cells = table.concat(rows) return { cells = cells, mask = (cells:gsub("[\1-\255]", "\255")), w = w, h = h } end local function put(s, pal, fx, fy, r, g, b) local ix, iy = floor(fx), floor(fy) local ax, ay = fx - ix, fy - iy if ax > 0.04 then bmask(s.mask, r, g, b, ix + 1, iy, s.w, s.h, s.w, s.h, ax) end if ay > 0.04 then bmask(s.mask, r, g, b, ix, iy + 1, s.w, s.h, s.w, s.h, ay) end blit(s.cells, pal, ix, iy, s.w, s.h, s.w, s.h, true) return ix, iy end local FW, FH, FC = 36, 19, 9 local PHASES = {} local function build(ph) local phi = TAU * ph / 8 local top, bot, yc = {}, {}, {} for x = 0, FW - 1 do local c = FC + 1.9 * ((FW - 1 - x) / (FW - 1)) ^ 1.6 * sin(phi + x * 0.32) local hh if x >= 6 then local v = ((x - 6) / 29 - 0.6) / 0.62 hh = 5.6 * sqrt(max(0, 1 - v * v)) local nose = (35.5 - x) * 1.3 + 0.5 if nose < hh then hh = nose end else hh = 1 + (6 - x) * 0.85 end yc[x] = c top[x], bot[x] = floor(c - hh + 0.5), floor(c + hh + 0.5) end local ecy = floor(yc[31] - 0.5) local flap = sin(phi * 2) local pec = {} for j = 0, 3 do pec[floor(yc[26] + 2.5 + j * (0.4 + 0.35 * flap)) * FW + 26 - j] = true end local spr = pack(FW, FH, function(x, y) local t0, b0, c = top[x], bot[x], yc[x] if x < 6 then if y < t0 or y > b0 then return 0 end if x <= 2 and abs(y - c) < (3 - x) * 1.2 then return 0 end return 4 end if y < t0 then if x >= 15 and x <= 24 and y >= t0 - floor(2.6 * sin(PI * (x - 15) / 9) + 0.5) then return 4 end return 0 end if y > b0 then if x >= 10 and x <= 17 and y <= b0 + floor(1.6 * sin(PI * (x - 10) / 7) + 0.5) then return 4 end return 0 end local ex, ey = x - 31, y - ecy local e2 = ex * ex + ey * ey if e2 <= 2 then return (ex == 1 and ey == -1) and 6 or 5 end if e2 <= 5 then return 8 end if pec[y * FW + x] then return 4 end if x == 27 and y > t0 + 1 and y < b0 - 1 then return 7 end if y <= t0 + 1 then return 2 end if y >= b0 - 1 then return 3 end return 1 end) local lx, ly = {}, {} for x = 27, 9, -3 do lx[#lx + 1] = x; ly[#ly + 1] = bot[x] - 1 end for x = 22, 13, -4 do lx[#lx + 1] = x; ly[#ly + 1] = floor(yc[x] + 1.5) end local s = { spr = spr, lx = lx, ly = ly } PHASES[ph] = s return s end local PALF = { 0, 0, 0 } local FR, FG, FB, GR, GG, GB = 255, 111, 142, 92, 240, 255 local function apply(c, g) FR, FG, FB, GR, GG, GB = c.r, c.g, c.b, g.r, g.g, g.b local s = { 0, 0, 0, c.r, c.g, c.b, c.r * 72 // 100, c.g * 72 // 100, c.b * 72 // 100, c.r + (255 - c.r) * 45 // 100, c.g + (255 - c.g) * 45 // 100, c.b + (255 - c.b) * 45 // 100, c.r * 60 // 100, c.g * 60 // 100, c.b * 60 // 100, 14, 10, 24, 255, 255, 255, c.r * 55 // 100, c.g * 55 // 100, c.b * 55 // 100, 220, 220, 236 } for i = 1, #s do PALF[i] = s[i] end end apply({ r = 255, g = 111, b = 142 }, { r = 92, g = 240, b = 255 }) local NSNOW = 40 local SX, SY, SV, SR, SG, SB = {}, {}, {}, {}, {}, {} for i = 1, NSNOW do local h = hash32(i * 0x9E3779B1 + 0x510E527F) SX[i], SY[i] = umod(h, W), umod(h >> 7, 72) SV[i] = (5 + umod(h >> 14, 11)) / 100 local k = (55 + umod(h >> 20, 46)) / 100 SR[i], SG[i], SB[i] = floor(214 * k), floor(186 * k), floor(200 * k) end local NGL = 7 local LX, LY, LP, LW = {}, {}, {}, {} for i = 1, NGL do local h = hash32(i * 0x85EBCA6B + 0x1F83D9AB) LX[i], LY[i] = 2 + umod(h, W - 4), 2 + umod(h >> 8, H - 4) LP[i] = umod(h >> 16, 628) / 100 LW[i] = TAU / (70 + umod(h >> 22, 60)) end local function snow(t, a, b, step) for i = a, b, step do local y = (SY[i] + t * SV[i]) % 72 - 4 local iy = floor(y) local f = y - iy blend(SX[i], iy, SR[i], SG[i], SB[i], 1 - f) blend(SX[i], iy + 1, SR[i], SG[i], SB[i], f) end end local NS, SPEED = 24, 3 function on_settings() apply(cfg.fish or { r = 255, g = 111, b = 142 }, cfg.glow or { r = 92, g = 240, b = 255 }) NS = cfg.snow or 24 SPEED = cfg.speed or 3 end function render() local t = frame * STEP_HZ // FPS gfx.clear(0, 0, 0) for i = 1, NGL do local b = sin(t * LW[i] + LP[i]) * 1.6 - 0.6 if b > 0 then if b > 1 then b = 1 end local x, y = LX[i], LY[i] blend(x, y, GR, GG, GB, b) if b > 0.6 then local a = (b - 0.6) * 0.6 blend(x - 1, y, GR, GG, GB, a); blend(x + 1, y, GR, GG, GB, a) blend(x, y - 1, GR, GG, GB, a); blend(x, y + 1, GR, GG, GB, a) end end end snow(t, 1, NS, 2) local v = 0.1 * SPEED local x = (t * v + 34) % 112 - 40 local y = 22 + 4 * sin(TAU * t / 260) local ph = floor(t * SPEED / 12) % 8 local S = PHASES[ph] or build(ph) local ix, iy = put(S.spr, PALF, x, y, FR, FG, FB) local lx, ly = S.lx, S.ly for k = 1, #lx do local b = 0.62 + 0.38 * sin(TAU * t / 40 - k * 0.8) px(ix + lx[k], iy + ly[k], floor(GR * b), floor(GG * b), floor(GB * b)) end snow(t, 2, NS, 2) end