info = { type = "image", title = "Ship in a bottle", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rolling sea and a rocking ship in a bottle", api = 33, } local tr = ({ sk = { ["Waves"] = "Vlny", ["Candle"] = "Sviečka", ["Sails"] = "Plachty" }, cs = { ["Waves"] = "Vlny", ["Candle"] = "Svíčka", ["Sails"] = "Plachty" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "waves", label = T("Waves"), type = "int", min = 1, max = 5, default = 3 }, { key = "candle", label = T("Candle"), type = "bool", default = true }, { key = "sails", label = T("Sails"), type = "color", default = "#DC3832" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs = math.floor, math.sin, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local WALL, GLOW1, GLOW2 = { 50, 40, 44 }, { 60, 46, 46 }, { 78, 56, 48 } local SHELF, SHELF_F, UNDER = { 156, 106, 62 }, { 112, 72, 44 }, { 34, 28, 32 } local GLASS, TINT = { 150, 198, 212 }, { 120, 172, 192 } local WATER, WATER_D, CREST = { 40, 142, 218 }, { 24, 96, 176 }, { 120, 214, 250 } local WAX, FIRE, FIRE_C = { 228, 210, 172 }, { 255, 150, 40 }, { 255, 236, 140 } local CX, CY = 51, 26 local function c3(c) return c[1], c[2], c[3] end local function glass_y(x) if x < 5 or x > 55 then return nil end if x == 5 then return 28, 45 elseif x == 6 then return 26, 47 elseif x <= 43 then return 25, 48 elseif x == 44 then return 26, 47 elseif x == 45 then return 28, 45 elseif x == 46 then return 30, 43 elseif x == 47 then return 32, 41 end return 33, 40 end local function disc(cx, cy, r, c, a) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3], a) end end local BG, BG_KEY = nil, nil local function build_bg(candle) rect(0, 0, W, H, c3(WALL)) if candle then for dy = -15, 15 do local hw = floor(sqrt(225 - dy * dy) + 0.5) rect(CX - hw, 21 + dy, 2 * hw + 1, 1, c3(GLOW1)) end for dy = -9, 9 do local hw = floor(sqrt(81 - dy * dy) + 0.5) rect(CX - hw, 21 + dy, 2 * hw + 1, 1, c3(GLOW2)) end end rect(0, 54, W, 1, c3(SHELF)); rect(0, 55, W, 4, c3(SHELF_F)); rect(0, 59, W, 5, c3(UNDER)) rect(10, 49, 7, 5, c3(SHELF_F)); rect(34, 49, 7, 5, c3(SHELF_F)) rect(CX - 2, CY + 1, 5, 54 - CY - 1, c3(WAX)) rect(CX - 2, CY + 1, 1, 54 - CY - 1, 190, 170, 134) px(CX, CY, 70, 56, 52) for x = 5, 55 do local t, b = glass_y(x) local inner_t, inner_b = t + 1, b - 2 if x <= 6 then inner_t, inner_b = t + 2, b - 2 end blend_rect(x, t, 1, b - t + 1, TINT[1], TINT[2], TINT[3], 0.3) rect(x, t, 1, 1, c3(GLASS)); rect(x, b - 1, 1, 2, c3(GLASS)) if x <= 6 then rect(x, t, 1, b - t + 1, c3(GLASS)) end end rect(7, 41, 38, 6, c3(WATER)); rect(7, 44, 38, 3, c3(WATER_D)) rect(9, 29, 1, 8, 200, 232, 240); rect(9, 38, 1, 2, 200, 232, 240) rect(55, 31, 3, 12, c3(GLASS)); rect(57, 33, 5, 8, 120, 84, 52); rect(57, 33, 5, 1, 146, 104, 66) px(60, 36, 90, 62, 40); px(58, 38, 90, 62, 40) BG = gfx.snapshot(0, 0, W, H) end local SRC = { ".......YY......", ".......M.......", "......RMr......", ".....RRMrr.....", "....RRRMrrr....", "...RRRRMrrrr...", ".......M.......", "......RMrr.....", ".....RRMrrr....", "....RRRMrrrr...", ".......M.......", ".HHHHHHHHHHHHH.", "..HhhhhhhhhhH..", "...HHHHHHHHH...", } local CODE = { M = 1, R = 2, r = 3, H = 4, h = 5, Y = 6 } local SW, SH, DW, DH = 15, 14, 17, 16 local PIV_X, PIV_Y = 7, 12.5 local ANGLES = { -0.21, -0.14, -0.07, 0, 0.07, 0.14, 0.21 } local POSES, SRCG = {}, nil local function build_pose() if not SRCG then SRCG = {} for y = 1, SH do local row = SRC[y] for x = 1, SW do SRCG[(y - 1) * SW + x] = CODE[row:sub(x, x)] or 0 end end return end local i = #POSES + 1 local c, s = math.cos(ANGLES[i]), math.sin(ANGLES[i]) local cells = {} for y = 0, DH - 1 do for x = 0, DW - 1 do local rx, ry = x - (PIV_X + 1), y - (PIV_Y + 1) local sx = floor(PIV_X + rx * c + ry * s + 0.5) local sy = floor(PIV_Y - rx * s + ry * c + 0.5) local v = 0 if sx >= 0 and sx < SW and sy >= 0 and sy < SH then v = SRCG[sy * SW + sx + 1] end cells[y * DW + x + 1] = v end end POSES[i] = string.char(table.unpack(cells, 1, DW * DH)) end local PAL = { 0, 0, 0, 112, 76, 44, 220, 56, 50, 150, 30, 40, 84, 50, 34, 150, 98, 58, 250, 210, 70 } local SURF = 38 local function surf(x, t, amp) return SURF + amp * (0.55 * sin(0.33 * x - 2.3 * t) + 0.3 * sin(0.17 * x + 1.4 * t)) end local function draw_water(t, amp) local xa = 7 local ya = floor(surf(7, t, amp) + 0.5) for x = 8, 45 do local y = x < 45 and floor(surf(x, t, amp) + 0.5) or -1 if y ~= ya then rect(xa, ya, x - xa, 1, c3(CREST)) if ya < 40 then rect(xa, ya + 1, x - xa, 40 - ya, c3(WATER)) end xa, ya = x, y end end end local SHIP_X = 17 local function draw_ship(t, amp, sail) local cx = SHIP_X + 8 local ys = surf(cx, t, amp) local slope = (surf(cx + 4, t, amp) - surf(cx - 4, t, amp)) / 8 local ang = slope * 0.5 + 0.05 * sin(t * 0.9) local best, bi = 9, 4 for i = 1, #ANGLES do local d = abs(ANGLES[i] - ang) if d < best then best, bi = d, i end end PAL[7], PAL[8], PAL[9] = sail.r, sail.g, sail.b PAL[10], PAL[11], PAL[12] = sail.r * 2 // 3, sail.g * 2 // 3, sail.b * 2 // 3 local oy = floor(ys + 0.5) - 13 gfx.blit_indexed(POSES[bi], PAL, SHIP_X, oy, DW, DH, DW, DH, true) gfx.clip(SHIP_X, oy + 14, DW, 4) draw_water(t, amp) gfx.clip() end local function draw_candle(t) local k = frame // 4 + (frame // 9) * 2 local pose = k % 3 - 1 local tall = (k // 3) % 2 rect(CX - 1, CY - 2, 3, 3, c3(FIRE)) rect(CX + pose, CY - 4 - tall, 1, 2 + tall, c3(FIRE)) rect(CX, CY - 2, 1, 3, c3(FIRE_C)) local flick = 0.07 + 0.03 * sin(t * 13) + 0.02 * sin(t * 5.3 + 1) - 0.015 * pose disc(CX, 21, 11, FIRE, flick) disc(CX, 21, 5, FIRE_C, flick * 0.7) local g1 = 0.35 + 0.25 * sin(t * 11) local g2 = 0.3 + 0.25 * sin(t * 7.3 + 2) blend_rect(44, 26, 2, 1, 255, 224, 160, g1) blend_rect(46, 31, 2, 1, 255, 224, 160, g2) blend_rect(49, 33, 6, 1, 255, 224, 160, g1 * 0.8) blend_rect(30 + floor(3 * sin(t * 0.5) + 0.5), 25, 5, 1, 255, 224, 160, g2 * 0.5) end function render() local candle = cfg.candle ~= false if BG_KEY ~= candle then build_bg(candle); BG_KEY = candle elseif #POSES < #ANGLES then build_pose() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local amp = 0.4 + (cfg.waves or 3) * 0.4 draw_water(t, amp) if #POSES == #ANGLES then draw_ship(t, amp, cfg.sails or { r = 220, g = 56, b = 50 }) end if candle then draw_candle(t) end end