info = { type = "image", title = "Sea in a bottle", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A little sailboat rocks inside a bottle of sea", api = 33, } local ltr = ({ sk = { ["Sea colour"] = "Farba mora", ["Waves"] = "Vlny", ["Sailboat"] = "Plachetnica" }, cs = { ["Sea colour"] = "Barva moře", ["Waves"] = "Vlny", ["Sailboat"] = "Plachetnice" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "sea", label = LT("Sea colour"), type = "color", default = "#2EAAC4" }, { key = "waves", label = LT("Waves"), type = "int", min = 1, max = 5, default = 3 }, { key = "boat", label = LT("Sailboat"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local CX = 32 local WL = 31 local XL, XR = 15, 49 local function mix(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 c3(c) return c[1], c[2], c[3] end local WHITE = { 255, 255, 255 } local GLASS, AIR = { 226, 244, 250 }, { 206, 192, 226 } local WALL_T, WALL_B = { 150, 120, 192 }, { 238, 164, 150 } local WOOD, WOOD_L, WOOD_D = { 150, 98, 62 }, { 196, 138, 90 }, { 108, 68, 46 } local CORK, CORK_D = { 200, 150, 98 }, { 156, 108, 70 } local SAND, SAND_D = { 232, 204, 148 }, { 200, 168, 116 } local CORAL, CORAL_L = { 244, 108, 130 }, { 255, 168, 176 } local WEED, WEED_L = { 40, 164, 108 }, { 90, 204, 130 } local SAILW, SAILR, HULL, MAST = { 252, 244, 228 }, { 255, 150, 130 }, { 156, 84, 52 }, { 110, 70, 44 } local SEA, SURF, DEEP local BG = nil local function hw(y) if y < 5 or y > 57 then return 0 end if y <= 13 then return 4 end if y <= 21 then local t = (y - 13) / 9 return floor(4 + 14 * (1 - (1 - t) * (1 - t)) + 0.5) end if y <= 52 then return 18 end local d = y - 53 return floor(14 + sqrt(16 - d * d) + 0.5) end function on_settings() local c = cfg.sea or { r = 46, g = 170, b = 196 } SEA = { c.r, c.g, c.b } SURF = mix(SEA, WHITE, 0.45) DEEP = mix(SEA, { 20, 50, 130 }, 0.55) BG = nil end local function build_bg() for y = 0, 57 do local c = mix(WALL_T, WALL_B, y / 57) rect(0, y, W, 1, c3(c)) end rect(0, 58, W, 1, c3(WOOD_L)) rect(0, 59, W, 5, c3(WOOD)) rect(0, 62, W, 2, c3(WOOD_D)) rect(14, 58, 37, 1, 120, 78, 70) for y = 5, 57 do local h = hw(y) rect(CX - h, y, 2 * h + 1, 1, c3(GLASS)) if h > 1 and y < 57 then rect(CX - h + 1, y, 2 * h - 1, 1, c3(AIR)) end end rect(CX - 5, 5, 11, 1, c3(GLASS)) rect(CX - 3, 1, 7, 5, c3(CORK)) rect(CX - 3, 1, 7, 1, c3(WOOD_L)) px(CX - 1, 3, c3(CORK_D)); px(CX + 2, 2, c3(CORK_D)); px(CX, 4, c3(CORK_D)) for y = WL, 56 do local h = hw(y) local c = mix(SURF, DEEP, (y - WL) / (56 - WL)) rect(CX - h + 1, y, 2 * h - 1, 1, c3(c)) end for y = WL + 1, 50, 3 do blend_rect(40 - (y - WL) // 3, y, 5, 3, 255, 255, 255, 0.11) blend_rect(25 - (y - WL) // 4, y, 3, 3, 255, 255, 255, 0.09) end for x = 18, 46 do local ys = 53 + floor(1.2 * sin(x * 0.3) + 0.5) rect(x, ys, 1, 57 - ys, c3(SAND)) if x % 5 == 2 then px(x, ys + 1, c3(SAND_D)) end end rect(26, 49, 1, 5, c3(CORAL)); rect(24, 50, 1, 2, c3(CORAL)); rect(28, 48, 1, 3, c3(CORAL)) px(24, 49, c3(CORAL_L)); px(28, 47, c3(CORAL_L)); px(26, 48, c3(CORAL_L)); px(25, 52, c3(CORAL)); px(27, 52, c3(CORAL)) rect(39, 53, 3, 2, 250, 214, 190); px(40, 52, 250, 214, 190); px(39, 54, 214, 166, 140) for _, p in ipairs({ { 33, 53 }, { 32, 54 }, { 34, 54 }, { 31, 55 }, { 35, 55 }, { 33, 54 } }) do px(p[1], p[2], 255, 160, 100) end rect(17, 38, 1, 12, c3(GLASS)); px(17, 36, c3(GLASS)) rect(17, 23, 1, 5, 255, 255, 255) rect(47, 40, 1, 6, 236, 250, 252) rect(CX - 3, 8, 1, 5, 255, 255, 255) BG = gfx.snapshot(0, 0, W, H) end local AMP, SPD = 1.4, 1.0 local function surf(x, t) return WL + AMP * (0.6 * sin(0.38 * x - 2.55 * SPD * t) + 0.4 * sin(0.7 * x + 1.8 * SPD * t + 1.3)) end local function surface_strip(t) local s = SURF for x = XL, XR do local ys = surf(x, t) local yi = floor(ys) local f = ys - yi if yi < WL then if yi < WL - 1 then rect(x, yi + 1, 1, WL - yi - 1, c3(s)) end blend_rect(x, yi, 1, 1, s[1], s[2], s[3], 1 - f) else if yi > WL then rect(x, WL, 1, yi - WL, c3(AIR)) end blend_rect(x, yi, 1, 1, AIR[1], AIR[2], AIR[3], f) end end end local function boat(t) local bx = 32 + 7 * sin(t * 0.33) + 2 * sin(t * 0.81 + 1) local ix = floor(bx) local fx = bx - ix local ys = surf(bx, t) - 1 local slope = (surf(bx + 3, t) - surf(bx - 3, t)) / 6 local tilt = slope * 0.9 if tilt > 0.25 then tilt = 0.25 elseif tilt < -0.25 then tilt = -0.25 end local yi = floor(ys) local f = ys - yi local hull = { 9, 9, 7, 5 } for k = 1, 4 do local w = hull[k] local a = (k == 1) and (1 - f) or (k == 4 and f or 1) local sh = floor((k - 2.5) * tilt * -1 + 0.5) local x0 = ix - (w >> 1) + sh if a > 0.04 then blend_rect(x0, yi - 1 + k, w, 1, HULL[1], HULL[2], HULL[3], a) end if fx > 0.04 then blend_rect(x0 + w, yi - 1 + k, 1, 1, HULL[1], HULL[2], HULL[3], fx * a) end end rect(ix - 3, yi, 7, 1, 214, 140, 92) for j = 1, 11 do local sh = floor(j * tilt + 0.5) + ix local y = yi - j px(sh, y, c3(MAST)) if j >= 2 and j <= 10 then local wr = (11 - j) * 5 // 10 + 1 rect(sh + 1, y, wr, 1, c3(SAILW)) if j <= 7 then local wl = (8 - j) * 4 // 10 + 1 rect(sh - wl, y, wl, 1, c3(SAILR)) end end end local fl = floor(sin(t * 9) * 1.2 + 0.5) px(floor(12 * tilt + 0.5) + ix + 1, yi - 12, 255, 90, 90) px(floor(12 * tilt + 0.5) + ix + 2, yi - 12 + fl, 255, 90, 90) end local function fish(t) local L = 26 local u = (t * 3) % (2 * L) local dir = u < L and 1 or -1 local x = 19 + (u < L and u or 2 * L - u) local y = 44 + floor(2.2 * sin(t * 1.2) + 0.5) local xi = floor(x) local f = x - xi local B, T = { 255, 150, 70 }, { 255, 108, 50 } rect(xi + 1, y, 3, 2, c3(B)) blend_rect(xi, y, 1, 2, B[1], B[2], B[3], 1 - f) blend_rect(xi + 4, y, 1, 2, B[1], B[2], B[3], f) local tx = (dir == 1) and xi - 1 or xi + 5 local fl = floor(sin(t * 15) + 0.5) px(tx, y - fl, c3(T)); px(tx, y + 1 + fl, c3(T)) px((dir == 1) and xi + 3 or xi + 1, y, 30, 40, 70) end local BX, BV, BP, BO = { 22, 29, 36, 43, 20 }, {}, {}, {} for i = 1, 5 do local h = hash32(i * 0x9E3779B1) BV[i] = 7 + umod(h, 8) BP[i] = umod(h >> 8, 628) / 100.0 BO[i] = umod(h >> 16, 1000) / 100 end local function bubble(i, t) local run = 24 / BV[i] local cyc = run + 1.33 local k = (t + BO[i]) % cyc if k > run then return end local y = 52 - k * BV[i] local x = floor(BX[i] + 1.2 * sin(t * 1.8 + BP[i]) + 0.5) local yi = floor(y) local f = y - yi blend_rect(x, yi, 2, 1, 220, 250, 255, 0.8 * (1 - f)) blend_rect(x, yi + 1, 2, 1, 220, 250, 255, 0.8) blend_rect(x, yi + 2, 2, 1, 220, 250, 255, 0.8 * f) end local function weed(x0, hgt, ph, t) for k = 0, hgt - 1 do local sway = floor(1.5 * (k / hgt) * sin(t * 2.1 + ph + k * 0.4) + 0.5) rect(x0 + sway, 54 - k * 3, 2, 3, c3(k % 2 == 0 and WEED or WEED_L)) end end local GX, GY, GP = { 20, 27, 34, 44, 38 }, { 34, 36, 33, 37, 35 }, { 0, 1.7, 3.3, 4.6, 2.4 } function render() if not BG then build_bg() end local t = frame / FPS AMP = 0.5 + 0.3 * (cfg.waves or 3) SPD = 0.6 + 0.2 * (cfg.waves or 3) gfx.blit_rgb(BG, 0, 0, W, H) weed(21, 5, 0.0, t); weed(42, 6, 2.2, t); weed(45, 4, 4.0, t) fish(t) for i = 1, 5 do bubble(i, t) end surface_strip(t) if cfg.boat ~= false then boat(t) end for i = 1, 5 do if sin(t * 3.9 + GP[i]) > 0.35 then px(GX[i], GY[i], 230, 255, 255) end end local gy = 24 + floor(t * 15) % 26 px(17, gy, 255, 255, 255) end