info = { type = "image", title = "Whale spout", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A whale swims and blows a fountain at sea", api = 33, } local ltr = ({ sk = { ["Whale colour"] = "Farba veľryby", ["Fountain"] = "Fontána", ["off"] = "vypnuté", ["small"] = "malé", ["tall"] = "vysoké", ["Speed"] = "Rýchlosť", ["Clouds"] = "Oblaky" }, cs = { ["Whale colour"] = "Barva velryby", ["Fountain"] = "Fontána", ["off"] = "vypnuto", ["small"] = "malé", ["tall"] = "vysoké", ["Speed"] = "Rychlost", ["Clouds"] = "Mraky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tone", label = LT("Whale colour"), type = "color", default = "#1E3C8C" }, { key = "spout", label = LT("Fountain"), type = "int", default = 2, opts = { LT("off"), LT("small"), LT("tall") } }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "clouds", label = LT("Clouds"), type = "bool", default = true }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local SURF = 28 local XL, N, AX0 = 10, 36, 32.4 local SAND, SAND_L, SAND_D = { 224, 190, 124 }, { 242, 214, 152 }, { 196, 160, 96 } local BELLY, BELLY_D = { 208, 228, 246 }, { 170, 200, 232 } local FOAM = { 255, 255, 255 } local DROP = { 236, 250, 255 } local WEED, WEED_D = { 36, 172, 108 }, { 24, 128, 88 } local function c3(c) return c[1], c[2], c[3] end local function lerp(a, b, t) return a + (b - a) * t end local function span(xa, xb, y, r, g, b, a) if xb <= xa then return end a = a or 1 local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, (xb - xa) * a) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, (ia + 1 - xa) * a); s0 = ia + 1 end if ib > s0 then if a < 1 then brect(s0, y, ib - s0, 1, r, g, b, a) else rect(s0, y, ib - s0, 1, r, g, b) end end if xb > ib then blend(ib, y, r, g, b, (xb - ib) * a) end end local function vspan(x, ya, yb, r, g, b) if yb <= ya then return end local ia, ib = floor(ya), floor(yb) if ia == ib then blend(x, ia, r, g, b, yb - ya) return end local s0 = ia if ya > ia then blend(x, ia, r, g, b, ia + 1 - ya); s0 = ia + 1 end if ib > s0 then rect(x, s0, 1, ib - s0, r, g, b) end if yb > ib then blend(x, ib, r, g, b, yb - ib) end end local DR, DG, DB = 30, 60, 140 local HR, HG, HB = 64, 104, 190 local SPD, SPS = 1.0, 1.0 local CLOUDS_ON = true function on_settings() local c = cfg.tone or { r = 30, g = 60, b = 140 } DR, DG, DB = c.r, c.g, c.b HR, HG, HB = floor(c.r + (255 - c.r) * 0.28), floor(c.g + (255 - c.g) * 0.28), floor(c.b + (255 - c.b) * 0.28) SPD = (cfg.speed or 3) / 3 local s = cfg.spout or 2 SPS = s == 0 and 0 or (s == 1 and 0.62 or 1.0) CLOUDS_ON = cfg.clouds ~= false end local SUNX, SUNY = 54, 8 local BG = nil local function build_bg() for y = 0, SURF - 1 do local t = y / (SURF - 1) rect(0, y, W, 1, floor(lerp(92, 190, t) + 0.5), floor(lerp(180, 228, t) + 0.5), floor(lerp(240, 250, t) + 0.5)) end for dy = -4, 4 do local hw = floor(sqrt(20.5 - dy * dy) + 0.5) rect(SUNX - hw, SUNY + dy, 2 * hw + 1, 1, 255, 244, 176) end for y = SURF, 59 do local t = (y - SURF) / (59 - SURF) rect(0, y, W, 1, 34, floor(lerp(162, 78, t) + 0.5), floor(lerp(222, 152, t) + 0.5)) end for x = 0, W - 1 do local top = 56 + floor(1.4 * sin(x * 0.17 + 1) + 0.9 * sin(x * 0.43) + 1.3) rect(x, top, 1, 2, c3(SAND_L)) rect(x, top + 2, 1, H - top - 2, c3(SAND)) end for k = 1, 14 do local h = hash32(k * 0x9E3779B1) px(h & 63, 60 + ((h >> 8) % 4), c3(SAND_D)) end local P, PL = { 234, 72, 122 }, { 255, 150, 178 } rect(51, 49, 2, 10, c3(P)) rect(47, 46, 2, 7, c3(P)); rect(47, 52, 5, 1, c3(P)) rect(55, 43, 2, 9, c3(P)); rect(52, 51, 4, 1, c3(P)) rect(51, 43, 2, 6, c3(P)) px(47, 46, c3(PL)); px(55, 43, c3(PL)); px(51, 43, c3(PL)); px(48, 47, c3(PL)); px(56, 45, c3(PL)) local O, OD = { 250, 150, 50 }, { 150, 74, 24 } rect(58, 51, 3, 8, c3(O)); rect(61, 54, 3, 5, c3(O)) rect(59, 51, 1, 1, c3(OD)); px(62, 54, c3(OD)) local V, VL = { 150, 92, 204 }, { 196, 148, 236 } local dome = { 4, 8, 10, 12, 12 } for r = 1, 5 do rect(11 - dome[r] // 2, 53 + r - 1, dome[r], 1, c3(V)) end px(8, 55, c3(VL)); px(11, 54, c3(VL)); px(13, 56, c3(VL)); px(10, 57, c3(VL)); px(15, 57, c3(VL)) rect(27, 60, 3, 1, 255, 138, 72); rect(28, 59, 1, 3, 255, 138, 72) BG = gfx.snapshot(0, 0, W, H) end local CLD = { { 5, 22, 0.9, 6 }, { 15, 15, 0.55, 40 }, { 10, 11, 1.3, 20 } } local function draw_clouds(T) for i = 1, #CLD do local c = CLD[i] local y, L = c[1], c[2] local x = (c[4] + T * c[3] / STEP_HZ) % (W + L) - L span(x + 3, x + L - 3, y, 255, 255, 255, 0.92) span(x + 1, x + L - 1, y + 1, 255, 255, 255, 0.92) span(x, x + L, y + 2, 255, 255, 255, 0.9) span(x + 2, x + L - 2, y + 3, 206, 226, 246, 0.8) end end local WV = {} for layer = 1, 3 do local n = ({ 7, 5, 4 })[layer] for k = 1, n do local h = hash32((layer * 31 + k) * 0x85EBCA6B) WV[#WV + 1] = { y = SURF + ({ 0, 2, 5 })[layer], L = 5 + (h & 7) + layer * 2, base = (k - 1) * 75 / n + ((h >> 8) & 7), v = ({ 3.6, 5.6, 8 })[layer], a = ({ 0.9, 0.5, 0.28 })[layer], } end end local function draw_waves(T) for i = 1, #WV do local w = WV[i] local x = (w.base - T * w.v / STEP_HZ) % (W + w.L) - w.L span(x, x + w.L, w.y, 255, 255, 255, w.a) end end local WEEDS = { { 3, 10 }, { 20, 8 }, { 61, 10 } } local function draw_weeds(T) for k = 1, #WEEDS do local x0, n = WEEDS[k][1], WEEDS[k][2] for j = 0, n - 1 do local sx = sin(T * 0.07 + j * 0.45 + x0) * (j / n) * 2.2 local xi = floor(x0 + sx) local f = x0 + sx - xi local c = j % 3 == 2 and WEED_D or WEED blend(xi, 58 - j, c[1], c[2], c[3], 1 - f) blend(xi + 1, 58 - j, c[1], c[2], c[3], f) end end end local VENT = { 24, 33, 57 } local function draw_bubbles(T) for k = 1, #VENT do local y = 58 - (T * (0.28 + k * 0.05) + k * 11) % 31 local x = VENT[k] + floor(sin(T * 0.09 + k * 2) * 1.2 + 0.5) local yi = floor(y) local f = y - yi blend(x, yi, 220, 244, 255, 0.85 * (1 - f)) blend(x, yi + 1, 220, 244, 255, 0.85 * f) end end local HH = { 5, 4.5, 4, 3.2, 2.2, 1.6, 1.4, 1.4, 1.6, 2, 2.6, 3.3, 4, 4.6, 5.2, 5.7, 6.1, 6.4, 6.6, 6.8, 6.9, 7, 7, 6.9, 6.8, 6.7, 6.5, 6.3, 6.1, 5.9, 5.6, 5.3, 4.9, 4.4, 3.7, 2.6 } local CY = {} local SS, S15 = {}, {} for i = 0, N - 1 do local s = (N - 1 - i) / (N - 1) SS[i], S15[i] = s, 3.0 * s * sqrt(s) end local XB = XL + 26.5 local function draw_whale(f, T) local bob = sin(T * TAU / 150) local ph = T * TAU / 72 for i = 0, N - 1 do local s = SS[i] local c = AX0 + bob + S15[i] * sin(ph - 2.4 * s) CY[i] = c local x, h = XL + i, HH[i + 1] * 1.18 local top, bot if i < 6 then top, bot = c - h, c + h else top, bot = c - 0.85 * h, c + 1.1 * h end if i >= 7 then local mid = floor(c + 0.3 * h + 0.5) vspan(x, top, mid, DR, DG, DB) vspan(x, mid, bot, c3(BELLY)) if i >= 9 and i <= 33 then blend(x, floor(top) + 1, HR, HG, HB, 0.75) end if i >= 15 and i <= 33 and i % 3 == 0 then px(x, mid + 2, c3(BELLY_D)); px(x, mid + 4, c3(BELLY_D)) end else vspan(x, top, bot, DR, DG, DB) end end local fy = floor(CY[22] + 3) local sh = floor(1.3 * sin(T * TAU / 90) + 0.5) local FW = { 3, 3, 3, 2, 2, 1 } for r = 0, 5 do rect(XL + 22 - r + (r > 2 and sh or 0), fy + r, FW[r + 1], 1, HR, HG, HB) end local ey = floor(CY[31] + 0.5) for i = 29, 35 do px(XL + i, floor(CY[i] + 2.9 - (i - 29) * 0.12), 14, 30, 84) end local shut = f % 120 < 4 if shut then rect(XL + 30, ey, 2, 1, 14, 30, 84) else rect(XL + 30, ey - 1, 2, 2, 250, 252, 255) px(XL + 31, ey, 10, 20, 60) end blend(XL + 28, ey + 2, 255, 140, 170, 0.8) blend(XL + 29, ey + 2, 255, 140, 170, 0.8) end local SP_T0, SP_P, ND, GRAV = 45, 100, 20, 0.12 local DVX, DVY, DDT = {}, {}, {} for j = 1, ND do local h = hash32(j * 0x9E3779B1) DVX[j] = ((h & 0xFF) / 255 - 0.4) * 0.9 DVY[j] = -(1.45 + ((h >> 8) & 0xFF) / 255 * 0.55) DDT[j] = (j - 1) * 1.1 end local function draw_spout(f) if SPS == 0 or f < SP_T0 then return end local ph = (f - SP_T0) % SP_P if ph > 72 then return end local y0 = SURF - 1.5 for j = 1, ND do local tau = ph - DDT[j] if tau >= 0 then local vy, vx = DVY[j] * SPS, DVX[j] * (0.55 + 0.45 * SPS) local tl = (-vy + sqrt(vy * vy + 3 * GRAV)) / GRAV if tau < tl then local x = XB + vx * tau local y = y0 + vy * tau + 0.5 * GRAV * tau * tau px(floor(x + 0.5), floor(y + 0.5), c3(DROP)) local t2 = tau - 1.3 if t2 > 0 then blend(floor(XB + vx * t2 + 0.5), floor(y0 + vy * t2 + 0.5 * GRAV * t2 * t2 + 0.5), 236, 250, 255, 0.5) end else local ts = tau - tl if ts < 4 then local a = 1 - ts / 4 local xl = floor(XB + vx * tl + 0.5) blend(xl, SURF - 1, 255, 255, 255, a) if ts >= 1 then blend(xl - 1, SURF - 1, 255, 255, 255, a * 0.7) blend(xl + 1, SURF - 1, 255, 255, 255, a * 0.7) end end end end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // FPS local T = f * SPD if CLOUDS_ON then draw_clouds(T) end draw_weeds(T) draw_bubbles(T) draw_whale(f, T) draw_waves(T) draw_spout(f) end