info = { type = "image", title = "Deep submarine", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Submarine lights up the deep as it passes", api = 44, } local tr = ({ sk = { ["Submarine"] = "Ponorka", ["Yellow"] = "Žltá", ["Grey"] = "Šedá", ["Orange"] = "Oranžová" }, cs = { ["Submarine"] = "Ponorka", ["Yellow"] = "Žlutá", ["Grey"] = "Šedá", ["Orange"] = "Oranžová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sub", label = T("Submarine"), type = "int", default = "yellow", opts = { keys = { "yellow", "grey", "orange" }, T("Yellow"), T("Grey"), T("Orange") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, abs, min = math.floor, math.sin, math.abs, math.min local hash32, umod = math.hash32, math.umod local byte, char, concat, unpack = string.byte, string.char, table.concat, table.unpack local FPS = gfx.FPS local TAU = 6.28318 local HULL = { yellow = { { 255, 236, 120 }, { 240, 190, 40 }, { 190, 130, 24 }, { 120, 76, 20 } }, grey = { { 206, 220, 230 }, { 150, 168, 184 }, { 104, 120, 140 }, { 60, 72, 90 } }, orange = { { 255, 176, 110 }, { 230, 110, 40 }, { 170, 66, 24 }, { 100, 40, 20 } }, } local GLASS, GLASS_HI, PORT, LAMP = { 70, 160, 190 }, { 150, 232, 240 }, { 255, 236, 150 }, { 255, 255, 232 } local BEAM = { 200, 255, 235 } local function bgcol(y) local a, b, t if y < 40 then a, b, t = { 26, 122, 140 }, { 10, 56, 86 }, y / 40 else a, b, t = { 10, 56, 86 }, { 6, 30, 52 }, (y - 40) / 23 end 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 CW, CH = 36, 21 local CELLS local function build_cells() local rows = {} for y = 0, CH - 1 do local row = {} for x = 0, CW - 1 do local v = 0 local hx, hy = (x + 0.5 - 18) / 15.5, (y + 0.5 - 14) / 5.5 if hx * hx * hx * hx + hy * hy <= 1 then v = hy < -0.55 and 1 or hy < 0.1 and 2 or hy < 0.6 and 3 or 4 end if x >= 13 and x <= 21 and y >= 4 and y <= 9 then v = (y == 4 or x == 13) and 1 or 2 if y == 6 and (x == 15 or x == 18) then v = 5 elseif y == 6 and (x == 16 or x == 19) then v = 6 end end if x == 19 and y >= 0 and y <= 3 then v = 3 end if y == 0 and x >= 19 and x <= 21 then v = 3 end if x >= 2 and x <= 5 and y >= 7 and y <= 14 and y + x >= 15 - 0 and v == 0 then v = 3 end if y == 14 and (x == 8 or x == 9 or x == 25 or x == 26 or x == 29 or x == 30) then v = 7 end if y == 15 and (x == 8 or x == 9 or x == 25 or x == 26 or x == 29 or x == 30) and v ~= 0 then v = 7 end if y >= 14 and y <= 15 and x == 34 then v = 8 end row[x + 1] = char(v) end rows[y + 1] = concat(row) end CELLS = concat(rows) end local HC = {} local RAS, built = {}, false local KR, KG, KB = 255, 0, 255 local Y0 = 20 local function shift_x(f) local out, row, g = {}, {}, 1 - f for y = 0, CH - 1 do local br, bg, bb = bgcol(Y0 + y) local base, k = y * CW, 0 for x = 0, CW do local a = (x >= 1 and f > 0) and byte(CELLS, base + x) or 0 local b = (x < CW and g > 0) and byte(CELLS, base + x + 1) or 0 local r1, g1, b1 = KR, KG, KB if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = br, bg, bb, br, bg, bb if a ~= 0 then ar, ag, ab = HC[3 * a - 2], HC[3 * a - 1], HC[3 * a] end if b ~= 0 then cr, cg, cb = HC[3 * b - 2], HC[3 * b - 1], HC[3 * b] end r1, g1, b1 = floor(ar * f + cr * g + 0.5), floor(ag * f + cg * g + 0.5), floor(ab * f + cb * g + 0.5) end row[k + 1], row[k + 2], row[k + 3] = r1, g1, b1 k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local BL, SLOPE, WIDE = 38, 0.55, 0.26 local BH = 34 local MASK local function build_mask() local rows = {} for j = 0, BH - 1 do local row = {} for i = 0, BL - 1 do local r = abs((j - 3) - SLOPE * i) / (1 + WIDE * i) row[i + 1] = char(r < 1 and floor(255 * (1 - r * r) * (1 - i / BL)) or 0) end rows[j + 1] = concat(row) end MASK = concat(rows) end local function lit(ox, oy, lx, ly) local dx = ox - lx if dx < 1 or dx >= BL then return 0 end local r = abs(oy - ly - SLOPE * dx) / (1 + WIDE * dx) if r >= 1 then return 0 end return min(1, (1 - r * r) * (1 - dx / BL) * 2.2) end local BG local HY, HN = {}, {} for x = 0, 63 do HY[x] = floor(53 + 2.5 * sin(x * 0.13) + 1.5 * sin(x * 0.31 + 1) + 0.5) HN[x] = floor(59 + 1.5 * sin(x * 0.2 + 2) + 0.5) end local function build_bg() for y = 0, 63 do rect(0, y, 64, 1, bgcol(y)) end for k = 1, 2 do local sx = k == 1 and 8 or 38 for y = 0, 27, 3 do blend_rect(floor(sx + y * 0.45), y, 7, 3, 150, 240, 230, 0.07 * (1 - y / 30)) end end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) px(umod(h, 64), umod(h >> 8, 50), 60, 150, 160) end for x = 0, 63 do local t = HY[x] rect(x, t, 1, 64 - t, 3, 16, 28) px(x, t, 12, 60, 82) end for x = 0, 63 do rect(x, HN[x], 1, 64 - HN[x], 2, 10, 18) end BG = gfx.snapshot(0, 0, 64, 64) end local function hrect(x, y, w, h, r, g, b, a) local ix = floor(x) local f = x - ix if f < 0.04 then blend_rect(ix, y, w, h, r, g, b, a); return end if w > 1 then blend_rect(ix + 1, y, w - 1, h, r, g, b, a) end blend_rect(ix, y, 1, h, r, g, b, a * (1 - f)) blend_rect(ix + w, y, 1, h, r, g, b, a * f) end local LX, LY = 0, 0 local FISH = {} for i = 1, 6 do local h = hash32(i * 0x85EBCA6B) FISH[i] = { x0 = umod(h, 80), y = 38 + umod(h >> 8, 14), v = (0.1 + umod(h >> 12, 10) / 60) * (umod(h >> 16, 2) * 2 - 1), c = umod(h >> 20, 2) } end local function fish(f, t) local x = (f.x0 + f.v * t) % 80 - 8 local a = lit(x + 3, f.y + 1, LX, LY) if a < 0.04 then return end local r, g, b = 255, 170, 90 if f.c == 1 then r, g, b = 190, 220, 240 end local d = f.v > 0 and 1 or -1 local y = f.y hrect(x + 1, y, 4, 1, r, g, b, a) hrect(x, y + 1, 6, 1, r, g, b, a) hrect(x + 1, y + 2, 4, 1, r, g, b, a) local tx = d > 0 and x - 2 or x + 6 hrect(tx, y, 2, 1, r, g, b, a * 0.8); hrect(tx, y + 2, 2, 1, r, g, b, a * 0.8) blend_rect(floor(d > 0 and x + 5 or x), y + 1, 1, 1, 20, 40, 60, a) end local function jelly(t, x0, yc, ph) local y = yc + 3 * sin(t * TAU / 210 + ph) local pulse = floor(0.5 + sin(t * TAU / 40 + ph)) local a = lit(x0 + 3, y + 2, LX, LY) if a < 0.04 then return end local iy = floor(y + 0.5) blend_rect(x0 + 1, iy, 4, 1, 255, 150, 220, a) blend_rect(x0, iy + 1, 6, 2 + pulse, 255, 150, 220, a * 0.9) for k = 0, 2 do local sw = floor(0.5 + sin(t * TAU / 50 + k * 2 + ph)) blend_rect(x0 + 1 + k * 2 + sw, iy + 3 + pulse, 1, 4, 230, 120, 200, a * 0.7) end end local WRECK = { 3, 6, 8, 7, 4 } local function wreck() for s = 0, 4 do local x = 38 + s * 4 local h = WRECK[s + 1] local a = lit(x + 2, 58 - h / 2, LX, LY) if a >= 0.04 then blend_rect(x, 58 - h, 4, h, 150, 130, 100, a) blend_rect(x, 58 - h, 4, 1, 210, 190, 150, a) if s == 2 then blend_rect(x + 1, 58 - h - 6, 1, 6, 180, 160, 120, a) end end end end local CYC, X_FROM, SPEED = 500, -40, 0.22 function on_settings() local c = HULL[cfg.sub] or HULL.yellow HC = {} local function add(k) local n = #HC; HC[n + 1], HC[n + 2], HC[n + 3] = k[1], k[2], k[3] end for i = 1, 4 do add(c[i]) end add(GLASS); add(GLASS_HI); add(PORT); add(LAMP) RAS, built = {}, false end local function sub_x(t) return X_FROM + ((t + 170) % CYC) * SPEED end local function bubbles(t) for j = 0, 8 do local age = (t - j * 9) % 81 local tb = t - age local sx = sub_x(tb) if sx > -34 and sx < 64 then local h = hash32(floor((tb + j * 9) / 81) * 31 + j * 0x9E3779B1) local bx = floor(sx + 1 + umod(h, 3)) + floor(0.5 + 0.8 * sin(age * 0.18 + j)) local yf = Y0 + 17 + sin(tb * TAU / 150) - age * 0.28 local iy = floor(yf) local f = yf - iy local a = 0.8 * (1 - age / 81) blend_rect(bx, iy, 1, 1, 170, 230, 235, a * (1 - f)) blend_rect(bx, iy + 1, 1, 1, 170, 230, 235, a * f) end end end function render() if not MASK then if not BG then build_bg() elseif not CELLS then build_cells() else build_mask() end gfx.clear(0, 0, 0) gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, 64, 64) local t = frame local xs = sub_x(t) local by = floor(sin(t * TAU / 150) + 0.5) LX, LY = floor(xs + 34.5), Y0 + by + 14 for i = 1, #FISH do fish(FISH[i], t) end jelly(t, 14, 44, 0); jelly(t, 50, 34, 2) wreck() gfx.blend_mask(MASK, BEAM[1], BEAM[2], BEAM[3], LX, LY - 3, BL, BH, BL, BH, 0.3) bubbles(t) if xs > -40 and xs < 64 then local q = floor(xs * 4 + 0.5) local p = q % 4 if not RAS[0] then RAS[0] = shift_x(0); built = true end if not RAS[p] and not built then RAS[p] = shift_x(p / 4); built = true end local R = RAS[p] if not R then R = RAS[0]; q = floor(xs + 0.5) * 4 end local x0 = q // 4 gfx.blit_rgb(R, x0, Y0 + by, CW + 1, CH, KR, KG, KB) local pf = (t // 2) % 2 rect(x0 + 1, Y0 + by + 14 - pf, 1, 3 - pf, 70, 76, 96) blend_rect(x0 + 34, Y0 + by + 13, 3, 3, 255, 255, 220, 0.25) end end