info = { type = "image", title = "Fishbowl astronaut", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Astronaut floats with a fishbowl in space", api = 33, } local tr = ({ sk = { ["Fish colour"] = "Farba rýb", ["Stars"] = "Hviezdy", ["Bubbles"] = "Bubliny" }, cs = { ["Fish colour"] = "Barva ryb", ["Stars"] = "Hvězdy", ["Bubbles"] = "Bubliny" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fish", label = T("Fish colour"), type = "color", default = "#FF8A2E" }, { key = "stars", label = T("Stars"), type = "int", min = 0, max = 30, default = 18 }, { key = "bubbles", label = T("Bubbles"), type = "bool", default = true }, } local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local rect, px, blend = gfx.rect, 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 = 6.283185 local SR, SG, SB = 16, 24, 64 local WR, WG, WB = 46, 118, 196 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 art(rows, map) local t = {} for i = 1, #rows do t[i] = (rows[i]:gsub(".", map)) end local cells = table.concat(t) return { cells = cells, mask = (cells:gsub("[\1-\255]", "\255")), w = #rows[1], h = #rows } 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) end local PAL = { 0, 0, 0, 24, 22, 48, 226, 244, 255, 150, 205, 235, 255, 255, 255, 140, 210, 245, WR, WG, WB, 232, 190, 120, 186, 136, 108, 238, 240, 248, 52, 150, 210, 210, 245, 255, 255, 190, 64, 140, 146, 176, 170, 176, 204, 230, 70, 80 } local BCX, BCY = 12, 13 local BOWL = pack(25, 26, function(x, y) local dx, dy = x - BCX, y - BCY local d = sqrt(dx * dx + dy * dy) if d > 12.4 or dy < -9 then return 0 end if dy == -9 then return 2 end if d > 11.3 then return 3 end if dx < -2 and dy < -1 and d > 8.6 and d < 10.2 then return 4 end if dy < -5 then return 0 end if dy == -5 then return 5 end if dy >= 9 then return umod(hash32(x * 31 + y * 17), 3) == 0 and 8 or 7 end return 6 end) local WATER_TOP, GRAVEL = BCY - 5, BCY + 9 local ASTRO = art({ ".....oooooo.....", "....owwwwwwo....", "...owvvvvwwwo...", "...ovvhvvvwwo...", "...ovvvvvvwwo...", "...ovvvvvwwwo...", "...owvvvwwwwo...", "....owwwwwwo....", "....oaaaaaaoo...", "...owwwwwwwwppo.", "..owwwrrwwwwppo.", ".owwowwwwwwwppo.", "owwo.owwwwwwppo.", "oaao.owwwwwwwpo.", ".oo..owwaawwwo..", ".....owwwwwwwo..", ".....owwwowwwo..", ".....owwo.owwo..", ".....owwo..owwo.", ".....owwo..owwo.", ".....osso..osso.", ".....oaao..oaao.", ".....oooo..oooo.", }, { ["."] = "\0", o = "\1", w = "\9", v = "\10", h = "\11", a = "\12", p = "\13", s = "\14", r = "\15" }) local AX, AY = 23, 1 local FISH_R = { { "...dd...", "d.fffff.", "ddffffkf", "d.fffff.", "...d...." }, { "...dd...", ".dfffff.", "ddffffkf", ".dfffff.", "...d...." }, } local FMAP = { ["."] = "\0", f = "\1", d = "\2", k = "\3" } local FISH = {} for i = 1, 2 do local rr, ll = {}, {} for j = 1, 5 do rr[j] = FISH_R[i][j]; ll[j] = FISH_R[i][j]:reverse() end FISH[i] = art(rr, FMAP) FISH[i + 2] = art(ll, FMAP) end local PALF = { 0, 0, 0, 255, 138, 46, 178, 96, 32, 20, 18, 40 } local NS = 30 local STX, STY, STW, STP = {}, {}, {}, {} for i = 1, NS do local h = hash32(i * 0x9E3779B1 + 0x6A09E667) STX[i], STY[i] = umod(h, W), umod(h >> 8, 50) STW[i] = TAU / (40 + umod(h >> 16, 80)) STP[i] = umod(h >> 22, 628) / 100 end local function stars(n, t) for i = 1, n do local s = sin(t * STW[i] + STP[i]) local x, y = STX[i], STY[i] if s > 0.55 then px(x, y, 255, 250, 224) if s > 0.9 then px(x - 1, y, 150, 150, 200); px(x + 1, y, 150, 150, 200) px(x, y - 1, 150, 150, 200); px(x, y + 1, 150, 150, 200) end elseif s > -0.3 then px(x, y, 176, 178, 226) else px(x, y, 104, 108, 170) end end end local BG = nil local function build_bg() gfx.clear(SR, SG, SB) local cx, cy, r = 4, 84, 32 for y = cy - r, H - 1 do local hw = sqrt(r * r - (y - cy) * (y - cy)) rect(0, y, floor(cx + hw + 0.5) + 1, 1, 120, 220, 190) local d = (r - 1) * (r - 1) - (y - cy) * (y - cy) if d >= 0 then local c1, c2, c3 = 60, 170, 150 if y == cy - r + 6 or y == cy - r + 7 or y == cy - r + 11 then c1, c2, c3 = 40, 132, 122 end rect(0, y, floor(cx + sqrt(d) + 0.5) + 1, 1, c1, c2, c3) end end for y = -4, 4 do local hw = floor(sqrt(18 - y * y) + 0.5) rect(55 - hw, 9 + y, 2 * hw + 1, 1, 200, 190, 232) end rect(53, 8, 2, 2, 158, 148, 202); px(57, 11, 158, 148, 202) BG = gfx.snapshot(0, 0, W, H) end local NSTARS, BUBBLES = 18, true function on_settings() local c = cfg.fish or { r = 255, g = 138, b = 46 } PALF[4], PALF[5], PALF[6] = c.r, c.g, c.b PALF[7], PALF[8], PALF[9] = c.r * 68 // 100, c.g * 68 // 100, c.b * 68 // 100 NSTARS = cfg.stars or 18 BUBBLES = cfg.bubbles ~= false end local function dot_y(x, y, r, g, b, a) local iy = floor(y) local f = y - iy blend(x, iy, r, g, b, a * (1 - f)); blend(x, iy + 1, r, g, b, a * f) end local function dot_x(x, y, r, g, b) local ix = floor(x) local f = x - ix blend(ix, y, r, g, b, 1 - f); blend(ix + 1, y, r, g, b, f) end local WEED = { { 6, 7 }, { 17, 5 } } local BUB = { 4, 15, 20 } function render() if not BG then build_bg() else gfx.blit_rgb(BG, 0, 0, W, H) end local t = frame * STEP_HZ // FPS stars(NSTARS, t) local fx = 12 + 1.2 * sin(TAU * t / 200) local fy = 20 + 2 * sin(TAU * t / 130 + 1) put(BOWL, PAL, fx, fy, 150, 205, 235) local wy = floor(fy) for k = 1, #WEED do local bx, h = WEED[k][1], WEED[k][2] for j = 1, h do local o = 1.3 * sin(TAU * t / 90 - j * 0.7 + k) * j / h dot_x(fx + bx + o, wy + GRAVEL - j, 70, 200, 110) end end local th = TAU * t / 150 local sx, sy = sin(th), cos(th) local wag = (t // 5) % 2 + 1 local fish = sx < 0 and FISH[wag] or FISH[wag + 2] put(fish, PALF, fx + BCX + 5.5 * sy - 4, fy + BCY + 2 + 1.8 * sx - 2, WR, WG, WB) if BUBBLES then for k = 1, #BUB do local a = (t + k * 23) % 70 local y = fy + GRAVEL - 1 - a * 0.17 dot_y(floor(fx + BUB[k] + 0.5), y, 200, 236, 255, a < 64 and 0.9 or 0.4) end end put(ASTRO, PAL, fx + AX, fy + AY, 24, 22, 48) end