info = { type = "image", title = "Sea serpent", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A sea serpent rolls through a stormy sea", api = 33, } local tr = ({ sk = { ["Serpent colour"] = "Farba hada", ["Rain"] = "Dážď", ["Lightning"] = "Blesky", ["Off"] = "Vypnuté", ["Rare"] = "Zriedka", ["Often"] = "Často", ["Wave speed"] = "Rýchlosť vĺn" }, cs = { ["Serpent colour"] = "Barva hada", ["Rain"] = "Déšť", ["Lightning"] = "Blesky", ["Off"] = "Vypnuto", ["Rare"] = "Zřídka", ["Often"] = "Často", ["Wave speed"] = "Rychlost vln" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "serp", label = T("Serpent colour"), type = "color", default = "#4E9A6A" }, { key = "rain", label = T("Rain"), type = "int", min = 0, max = 10, default = 5 }, { key = "bolt", label = T("Lightning"), type = "int", default = 1, opts = { T("Off"), T("Rare"), T("Often") } }, { key = "waves", label = T("Wave speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI, TAU = 3.14159, 6.28318 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 WL = 38 local SKY_T, SKY_B = { 78, 72, 134 }, { 136, 120, 178 } local SEA_T, SEA_B = { 96, 158, 182 }, { 52, 112, 146 } local CLOUD, CLOUD_L = { 62, 58, 114 }, { 100, 90, 150 } local FOAM, RAIN = { 226, 240, 250 }, { 180, 200, 238 } local PINK, EYE = { 246, 128, 160 }, { 255, 90, 64 } local BODY, HI, SHADE local BG = nil function on_settings() local c = cfg.serp or { r = 78, g = 154, b = 106 } BODY = { c.r, c.g, c.b } HI = mix(BODY, WHITE, 0.42) SHADE = mix(BODY, { 20, 50, 70 }, 0.4) end local function blob(cx, cy, rx, ry, c) for dy = -ry, ry do local w = floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) rect(cx - w, cy + dy, 2 * w + 1, 1, c3(c)) end end local function build_bg() for y = 0, WL - 1 do rect(0, y, W, 1, c3(mix(SKY_T, SKY_B, y / (WL - 1)))) end local K = { { 8, 5, 16, 6 }, { 30, 3, 18, 6 }, { 52, 6, 16, 7 }, { 20, 12, 14, 4 }, { 44, 14, 14, 4 }, { 62, 11, 8, 5 } } for i = 1, #K do local k = K[i] blob(k[1], k[2] + 2, k[3], k[4], CLOUD_L) end for i = 1, #K do local k = K[i] blob(k[1], k[2], k[3], k[4], CLOUD) end for y = WL, H - 1 do rect(0, y, W, 1, c3(mix(SEA_T, SEA_B, (y - WL) / (H - 1 - WL)))) end BG = gfx.snapshot(0, 0, W, H) end local function span(xl, y, w, c, a) local xi = floor(xl) local fx = xl - xi if w > 1 then blend_rect(xi + 1, y, w - 1, 1, c[1], c[2], c[3], a) end blend_rect(xi, y, 1, 1, c[1], c[2], c[3], a * (1 - fx)) if fx > 0.02 then blend_rect(xi + w, y, 1, 1, c[1], c[2], c[3], a * fx) end end local FLASH = { 1.0, 0.5, 0.0, 0.85, 1.0, 0.65, 0.4, 0.28, 0.18, 0.12, 0.08, 0.05 } local function bolt_info(t, mode) if mode == 0 then return nil end local lc = (mode == 2) and 120 or 210 local n = t // lc local s if n == 0 then s = (mode == 2) and 90 or 125 else s = 20 + (hash32(n * 0x9E3779B1) & 0x7FFF) % (lc - 60) end local lf = t - n * lc - s if lf < 0 or lf > 40 then return nil end return n, lf end local function lightning(n, lf) local a = FLASH[lf + 1] or 0 if a > 0 then blend_rect(0, 0, W, WL, 255, 250, 232, 0.30 * a) blend_rect(0, WL, W, H - WL, 255, 250, 232, 0.10 * a) local x = 34 + (hash32(n * 0x85EBCA6B) & 0x7FFF) % 24 for y = 8, WL - 1, 4 do local j = ((hash32((n + 1) * 0x9E3779B1 + y) & 0x7FFF) % 5) - 2 blend_rect(x - 1, y, 3, 4, 255, 255, 220, 0.35 * a) rect(x, y, 1, 4, 255, 255, 240) rect(x + (j > 0 and 0 or j), y + 3, (j > 0 and j or -j) + 1, 1, 255, 255, 240) x = x + j end end end local K_W = TAU / 21 local SA, CA, SB, CB, SK, CK = {}, {}, {}, {}, {}, {} for x = 0, W do SA[x], CA[x] = sin(0.33 * x), math.cos(0.33 * x) SB[x], CB[x] = sin(0.8 * x), math.cos(0.8 * x) SK[x], CK[x] = sin(K_W * x), math.cos(K_W * x) end local WY, FS = {}, {} local function water_rows(t, sp) local a1, a2 = 0.13 * sp * t, 0.09 * sp * t local s1, c1, s2, c2 = sin(a1), math.cos(a1), sin(a2), math.cos(a2) for x = 0, W do local f = SA[x] * c1 - CA[x] * s1 FS[x] = f WY[x] = WL + 1.1 * f + 0.5 * (SB[x] * c2 + CB[x] * s2) end end local function serpent(t, sp, roar) local om = 0.075 * sp local co, so = math.cos(om * t), sin(om * t) for x = 22, W - 1 do local a = 12.5 - (x - 22) * 0.14 local s = SK[x] * co - CK[x] * so local yw = WY[x] local ywi = floor(yw) if s > 0 then local h = a * (s ^ 0.55) if h > 0.6 then local yt = WL - h local yi = floor(yt) local f = yt - yi blend_rect(x, yi, 1, 1, HI[1], HI[2], HI[3], 1 - f) local bot = ywi - yi - 1 if bot > 0 then rect(x, yi + 1, 1, 1, c3(HI)) if bot > 1 then rect(x, yi + 2, 1, bot - 1, c3(BODY)) end end if x % 5 == 0 and h > 3 then px(x, yi - 1, c3(SHADE)) end if x % 6 == 3 and h > 5 then px(x, yi + 4, c3(PINK)) end end end end end local function neck(t, sp, roar) local Hn = 19 + 1.5 * sin(t * 0.035) local top_cx local ys = floor(Hn) for j = 0, ys, 2 do local k = j / Hn local w = floor(5 - 2 * k + 0.5) if w < 3 then w = 3 end local cx = 17 - 6 * k ^ 1.6 + 1.3 * k * sin(t * 0.04 - j * 0.12) span(cx - w / 2, WL - j - 1, w, (j % 6 == 4) and HI or BODY, 1) span(cx - w / 2, WL - j, w, BODY, 1) if j % 8 == 4 then px(floor(cx), WL - j - 1, c3(PINK)) end top_cx = cx end local hx = floor(top_cx + 0.5) local hy = WL - ys - 3 local jaw = roar and 2 or 0 rect(hx - 6, hy, 9, 3, c3(BODY)) rect(hx - 8, hy + 1, 3, 2, c3(BODY)) rect(hx - 6, hy, 7, 1, c3(HI)) rect(hx - 7, hy + 3 + jaw, 8, 1, c3(SHADE)) if roar then rect(hx - 7, hy + 3, 7, 2, 230, 90, 100) px(hx - 8, hy + 3, 250, 240, 230); px(hx - 4, hy + 3, 250, 240, 230) end px(hx - 3, hy + 1, c3(EYE)) if roar then blend_rect(hx - 4, hy, 3, 3, EYE[1], EYE[2], EYE[3], 0.35) end px(hx + 1, hy - 1, c3(SHADE)); px(hx + 2, hy, c3(SHADE)) end local function sea(t, sp) for x = 0, W - 1, 2 do local yw = WY[x + 1] local yi = floor(yw) local f = yw - yi local top = mix(SEA_T, WHITE, 0.12) local len = WL + 3 - yi rect(x, yi + 1, 2, len, c3(top)) blend_rect(x, yi, 2, 1, top[1], top[2], top[3], 1 - f) if FS[x + 1] > 0.85 then px(x, yi - 1, c3(FOAM)); px(x + 1, yi, c3(FOAM)) end end for r = 1, 2 do local y = WL + 6 + r * 8 local v = (0.3 + 0.18 * r) * sp for k = 0, 2 do local x = (k * 24 + r * 9 + t * v) % (W + 16) - 8 span(x, y, 4 + r, FOAM, 0.35) end end end local DX, DV, DO = {}, {}, {} for i = 1, 50 do local h = hash32(i * 0x9E3779B1) DX[i] = umod(h, W) DV[i] = 2.2 + umod(h >> 8, 16) / 10.0 DO[i] = umod(h >> 16, 90) end local function rain(t, n) for i = 1, n do local y = (t * DV[i] + DO[i]) % (H + 8) - 4 local x = (DX[i] + floor(y * 0.3)) % W blend_rect(x, floor(y), 1, 3, RAIN[1], RAIN[2], RAIN[3], 0.55) end end local function streaks(t) local x = (t * 0.06) % (W + 30) - 30 span(x, 17, 26, CLOUD, 0.55) span(x + 3, 18, 20, CLOUD, 0.45) local x2 = (W + 20) - (t * 0.04) % (W + 40) span(x2, 24, 20, CLOUD, 0.45) end function render() if not BG then build_bg() end local t = frame * STEP_HZ // FPS local sp = 0.6 + 0.2 * (cfg.waves or 3) gfx.blit_rgb(BG, 0, 0, W, H) streaks(t) local n, lf = bolt_info(t, cfg.bolt or 1) if n then lightning(n, lf) end water_rows(t, sp) serpent(t, sp) neck(t, sp, n and lf < 28) sea(t, sp) rain(t, (cfg.rain or 5) * 5) end