info = { type = "image", title = "Fish shoal", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A shoal scatters when a big fish dashes by", api = 33, } local tr = ({ sk = { ["Fish"] = "Rybky", ["Colour"] = "Farba", ["Big fish"] = "Veľká ryba", ["Silver"] = "Strieborná", ["Gold"] = "Zlatá", ["Coral"] = "Koralová" }, cs = { ["Fish"] = "Rybky", ["Colour"] = "Barva", ["Big fish"] = "Velká ryba", ["Silver"] = "Stříbrná", ["Gold"] = "Zlatá", ["Coral"] = "Korálová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Fish"), type = "int", min = 4, max = 24, default = 12 }, { key = "tone", label = T("Colour"), type = "int", default = "silver", opts = { keys = { "silver", "gold", "coral" }, T("Silver"), T("Gold"), T("Coral") } }, { key = "big", label = T("Big fish"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local KELP = { { 5, 9 }, { 12, 6 }, { 19, 8 }, { 45, 7 }, { 52, 9 }, { 59, 6 }, } local SEG = 4 local BG local function build_bg() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(36 - 30 * t + 0.5), floor(132 - 92 * t + 0.5), floor(176 - 88 * t + 0.5)) end for _, bx in ipairs({ 4, 26, 47 }) do for y = 0, 48 do blend_rect(floor(bx + y * 0.45), y, 4 + y // 10, 1, 190, 240, 255, 0.07) end end for x = 0, W - 1 do local top = 56 + floor(1.2 * sin(x * 0.19) + 0.8 * sin(x * 0.07 + 2) + 0.5) rect(x, top, 1, H - top, 150, 138, 104) rect(x, top + 3, 1, H - top - 3, 124, 112, 86) rect(x, top + 6, 1, H - top - 6, 100, 90, 70) px(x, top, 184, 170, 128) end for _, r in ipairs({ { 30, 60, 3 }, { 36, 61, 2 }, { 41, 59, 2 }, { 24, 62, 1 } }) do rect(r[1], r[2], r[3] + 1, 2, 70, 80, 90) px(r[1], r[2], 126, 138, 150) end rect(0, 53, 4, 11, 52, 66, 84); rect(1, 51, 3, 3, 52, 66, 84); rect(0, 53, 2, 2, 92, 110, 128) rect(62, 54, 2, 10, 52, 66, 84); rect(61, 57, 1, 6, 52, 66, 84) for _, k in ipairs(KELP) do for s = 0, 1 do rect(k[1], 56 - 1 - s * SEG, 2, SEG, 30, 130 + 6 * s, 84) end end BG = gfx.snapshot(0, 0, W, H) end local PAL_BIG = { 0, 0, 0, 6, 38, 60, 92, 200, 140, 44, 160, 116, 30, 118, 102, 176, 220, 196, 240, 128, 32, 176, 72, 24, 240, 248, 250, 6, 20, 30 } local BW, BH = 26, 13 local function body_top(x) local u = (x - 14) / 9.5 if u * u >= 1 then return nil end return 7.5 - 5.3 * sqrt(1 - u * u) end local function big_cell(x, y, tt) local xc, yc = x + 0.5, y + 0.5 local u, w = (xc - 14) / 9.5, (yc - 7.5) / 5.3 local r2 = u * u + w * w if r2 <= 1 then if x == 19 and (y == 5 or y == 6) then return 8 end if x == 20 and (y == 5 or y == 6) then return 9 end if r2 > 0.7 then return 1 end if x == 17 and y >= 4 and y <= 9 then return 1 end if w < -0.55 then return 2 end if w < 0.2 then return (x > 7 and x < 16 and (x + y) % 2 == 0) and 4 or 3 end return w < 0.6 and 4 or 5 end if xc <= 5.5 then local c = 5.5 - xc local cy = 7.5 + tt * 0.8 * c / 5 local hh = 1.3 + c * 0.75 if abs(yc - cy) <= hh and not (c > 4 and abs(yc - cy) < 1.0) then return abs(yc - cy) > hh - 1 and 7 or 6 end end local bt = body_top(xc) if bt and xc > 9 and xc < 17 then local hf = 4.2 * (1 - abs((xc - 12.5) / 4.5)) if hf > 0 and yc < bt + 0.8 and yc > bt - hf then return yc < bt - hf + 1.2 and 7 or 6 end end if bt and xc > 10.5 and xc < 15.5 then local hf = 2.6 * (1 - abs((xc - 13) / 2.5)) local bb = 15 - bt if hf > 0 and yc > bb - 0.8 and yc < bb + hf then return 6 end end return 0 end local BIGS = {} local TAILS = { -1, 0, 1, 0 } local function big_sprite(pose, left) local k = pose * 2 + (left and 1 or 0) local s = BIGS[k] if s then return s end local rows = {} for y = 0, BH - 1 do local row = {} for x = 0, BW - 1 do row[x + 1] = big_cell(x, y, TAILS[pose + 1]) end if left then local rev = {} for i = 1, BW do rev[i] = row[BW + 1 - i] end row = rev end rows[y + 1] = char(unpack(row)) end s = concat(rows) BIGS[k] = s return s end local TONES = { silver = { { 176, 206, 228 }, { 226, 240, 250 }, { 140, 184, 214 } }, gold = { { 250, 198, 70 }, { 255, 236, 150 }, { 240, 150, 50 } }, coral = { { 244, 118, 100 }, { 255, 190, 170 }, { 224, 96, 104 } }, } local FISH = {} for i = 1, 24 do local h = hash32(i * 0x9E3779B1) local a, rad = i * 2.39996, sqrt((i - 0.5) / 24) FISH[i] = { ox = 17 * rad * cos(a), oy = 9 * rad * sin(a), lag = (i % 5) * 6, ph = umod(h, 628) / 100, v = 0.04 + umod(h >> 10, 4) / 100 } end local function school(ts) return 32 + 14 * sin(ts * TAU / 12), 27 + 6 * sin(ts * TAU / 9 + 1.3), cos(ts * TAU / 12) end local BUB = {} for i = 1, 8 do local h = hash32(i * 0x85EBCA6B) BUB[i] = { x = 6 + umod(h, 52), y0 = umod(h >> 6, 70), v = 7 + umod(h >> 12, 8), ph = umod(h >> 18, 628) / 100 } end local CYCLE = 16 * FPS local DASH = 3.3 local DASH_AT = 4.5 local BSPEED = (W + 2 * BW) / DASH function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame for i = 1, #BUB do local b = BUB[i] local y = 66 - (b.y0 + b.v * t / FPS) % 70 if y > 0 and y < 56 then blend(b.x + floor(1.5 * sin(t * 0.05 + b.ph) + 0.5), floor(y), 220, 244, 255, 0.5) end end for _, k in ipairs(KELP) do local ph = k[1] * 0.7 for s = 2, k[2] do local off = floor(2.2 * (s / k[2]) * sin(t * 0.035 + ph) + 0.5) local y = 56 - 1 - (s - 1) * SEG rect(k[1] + off, y, 2, SEG, 30, 130 + 6 * (s % 2), 84) if s % 3 == 0 then px(k[1] + off + (s % 2 == 0 and 2 or -1), y + 1, 56, 168, 104) end end end local idx, ts = t // CYCLE, (t % CYCLE) / FPS - DASH_AT local bx, by, bdir if cfg.big ~= false and ts >= 0 and ts < DASH then bdir = idx % 2 == 0 and 1 or -1 bx = bdir == 1 and (-BW + BSPEED * ts) or (W - BSPEED * ts) by = 17 + (hash32(idx * 0x9E3779B1) & 15) bx = floor(bx) end local tn = TONES[cfg.tone] or TONES.silver local c1, c2, c3 = tn[1], tn[2], tn[3] for i = 1, cfg.n or 14 do local f = FISH[i] local tt = (t - f.lag) / FPS local cx, cy, hd = school(tt) local x = cx + f.ox + 2 * sin(t * f.v + f.ph) local y = cy + f.oy + 1.5 * sin(t * f.v * 0.8 + f.ph * 2) local dir = hd >= 0 and 1 or -1 if bx then local dx, dy = x - (bx + BW / 2), y - (by + BH / 2) local d = sqrt(dx * dx * 0.35 + dy * dy) if d < 24 then local k = 1 - d / 24 k = k * k * (3 - 2 * k) local sg = (abs(dy) > 2 and dy or (i % 2 == 0 and 1 or -1)) >= 0 and 1 or -1 local nx, ny = dx * 0.25, dy + sg * 3 local nn = sqrt(nx * nx + ny * ny) x, y = x + 28 * k * nx / nn, y + 24 * k * ny / nn if k > 0.25 then dir = nx >= 0 and 1 or -1 end end end local ix = floor(x) local fr = x - ix local iy = floor(y + 0.5) local fa, ha = 1 - fr, fr if dir == 1 then blend_rect(ix, iy, 1, 3, c3[1], c3[2], c3[3], fa) blend_rect(ix + 5, iy, 1, 3, c1[1], c1[2], c1[3], ha) px(ix + 4, iy, 20, 40, 60) else blend_rect(ix, iy, 1, 3, c1[1], c1[2], c1[3], fa) blend_rect(ix + 5, iy, 1, 3, c3[1], c3[2], c3[3], ha) px(ix + 1, iy, 20, 40, 60) end rect(ix + 1, iy, 4, 2, c1[1], c1[2], c1[3]) rect(ix + 1, iy + 2, 4, 1, c2[1], c2[2], c2[3]) if dir == 1 then px(ix + 4, iy, 20, 40, 60) else px(ix + 1, iy, 20, 40, 60) end end if bx then local pose = (t // 5) % 4 gfx.blit_indexed(big_sprite(pose, bdir == -1), PAL_BIG, bx, by, BW, BH, BW, BH, true) end end