info = { type = "image", title = "Blood moon keep", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bats leave a dark keep under a red moon", api = 33, } local tr = ({ sk = { ["Bats"] = "Netopiere", ["Lightning"] = "Blesk" }, cs = { ["Bats"] = "Netopýři", ["Lightning"] = "Blesk" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bats", label = T("Bats"), type = "int", min = 1, max = 8, default = 7 }, { key = "storm", label = T("Lightning"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local BLK, FAR = { 6, 2, 4 }, { 44, 10, 16 } local MX, MY, MR = 44, 23, 16 local CALM = { { 34, 4, 10 }, { 168, 30, 24 }, { 206, 48, 34 }, { 226, 78, 46 }, { 172, 36, 30 }, { 150, 32, 32 }, { 112, 22, 28 }, { 128, 28, 30 } } local FLASH = { { 120, 36, 44 }, { 238, 130, 112 }, { 255, 168, 134 }, { 255, 204, 170 }, { 226, 120, 104 }, { 236, 118, 106 }, { 214, 100, 94 }, { 255, 150, 130 } } 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 disc(cx, cy, r, c) for y = floor(cy - r), floor(cy + r) do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local function spire(cx, yb, hw, h, rim) for i = 0, h - 1 do local half = floor(hw * ((i + 0.5) / h) ^ 1.35 + 0.5) rect(cx - half, yb - h + i, 2 * half + 1, 1, BLK[1], BLK[2], BLK[3]) px(cx + half, yb - h + i, rim[1], rim[2], rim[3]) end end local function bake(P) for y = 0, 53 do rect(0, y, W, 1, mix(P[1], P[2], y / 53)) end disc(MX, MY, MR + 4.5, P[7]); disc(MX, MY, MR + 2.5, P[6]); disc(MX, MY, MR, P[3]) disc(MX - 3, MY - 3, 10, P[4]) for _, c in ipairs({ { -5, 4 }, { 2, 8 }, { 8, -2 }, { -9, -4 }, { 4, -9 }, { 9, 6 }, { -2, 11 } }) do rect(MX + c[1], MY + c[2], 2, 1, P[5][1], P[5][2], P[5][3]) px(MX + c[1] + 1, MY + c[2] + 1, P[5][1], P[5][2], P[5][3]) end for x = 0, W - 1, 2 do local h = 6 + floor(3 * sin(x * 0.21) + 2 * sin(x * 0.5 + 1)) rect(x, 54 - h, 2, h, FAR[1], FAR[2], FAR[3]) end local rim = P[8] for y = 44, 53 do local x0 = floor(4 - (y - 44) * 0.4 + 2 * sin(y * 1.3)) local x1 = floor(42 + (y - 44) * 0.7 + 2 * sin(y * 0.9)) rect(x0, y, x1 - x0, 1, BLK[1], BLK[2], BLK[3]) px(x1 - 1, y, rim[1], rim[2], rim[3]) end rect(0, 54, W, H - 54, BLK[1], BLK[2], BLK[3]) rect(10, 34, 27, 12, BLK[1], BLK[2], BLK[3]) for x = 10, 34, 3 do rect(x, 32, 2, 2, BLK[1], BLK[2], BLK[3]) end rect(36, 34, 1, 12, rim[1], rim[2], rim[3]) rect(8, 22, 7, 24, BLK[1], BLK[2], BLK[3]); spire(11, 22, 4, 11, rim) rect(21, 14, 8, 28, BLK[1], BLK[2], BLK[3]); spire(25, 14, 5, 14, rim) rect(32, 24, 7, 22, BLK[1], BLK[2], BLK[3]); spire(35, 24, 4, 11, rim) rect(28, 14, 1, 28, rim[1], rim[2], rim[3]); rect(38, 24, 1, 22, rim[1], rim[2], rim[3]) rect(15, 26, 4, 8, BLK[1], BLK[2], BLK[3]); spire(17, 26, 2, 6, rim) for _, p in ipairs({ { 9, 20 }, { 13, 20 }, { 22, 12 }, { 28, 12 }, { 33, 22 }, { 37, 22 } }) do rect(p[1], p[2], 1, 3, BLK[1], BLK[2], BLK[3]) end rect(5, 40, 5, 6, BLK[1], BLK[2], BLK[3]); rect(40, 38, 4, 8, BLK[1], BLK[2], BLK[3]) end local ART = { big = { { "#..#.#..#", "##.###.##", ".#######.", "..#####..", "...#.#..." }, { "...#.#...", ".#######.", "##.###.##", "#..###..#", "....#...." }, { "...#.#...", "..#####..", ".#######.", "##.###.##", "#...#...#" }, }, small = { { "#.#.#", ".###.", "..#.." }, { ".#.#.", "#####", "..#.." }, { "..#..", ".###.", "#.#.#" }, }, } local POSES = {} for size, list in pairs(ART) do POSES[size] = {} for k, rows in ipairs(list) do local runs = {} for dy, row in ipairs(rows) do local x = 1 while x <= #row do local a, b = row:find("#+", x) if not a then break end runs[#runs + 1] = { a - 1 - #row // 2, dy - 1 - #rows // 2, b - a + 1 } x = b + 1 end end POSES[size][k] = runs end end local ANG = { 25, 55, 15, 70, 40, 30, 65, 20 } local SPD = { 1.25, 1.1, 1.5, 1.2, 1.35, 1.15, 1.3, 1.45 } local CY = 130 local FLAP = { 1, 2, 3, 2 } local function bat(i, u) local a = ANG[i] * 0.01745 local s = i % 2 == 0 and 1 or -1 local x = 24 + s * SPD[i] * cos(a) * u local y = 17 - SPD[i] * sin(a) * u + 2.2 * sin(u * 0.21 + i) if x < -10 or x > W + 10 or y < -8 then return end local runs = POSES[i % 3 == 0 and "small" or "big"][FLAP[(u // 3 + i) % 4 + 1]] local ix, iy = floor(x), floor(y) for k = 1, #runs do local r = runs[k] rect(ix + r[1], iy + r[2], r[3], 1, BLK[1], BLK[2], BLK[3]) end end local WIN = { { 24, 19, 1, 3 }, { 24, 26, 1, 3 }, { 24, 33, 1, 3 }, { 11, 31, 1, 3 }, { 35, 31, 1, 3 }, { 17, 31, 1, 2 }, { 14, 38, 1, 2 }, { 31, 38, 1, 2 } } local EMB = {} for i = 1, 14 do local h = hash32(i * 0x9E3779B1) EMB[i] = { h % W, (h >> 8) % 64, 0.1 + ((h >> 16) % 20) / 100, (h >> 4) % 628 / 100 } end local BOLT = { { { 6, 0 }, { 3, 8 }, { 7, 15 }, { 4, 24 }, { 7, 33 } }, { { 54, 0 }, { 58, 9 }, { 53, 17 }, { 57, 27 }, { 52, 38 } }, } local SLOT = 270 local BGN, BGF function render() if not BGN then bake(CALM); BGN = gfx.snapshot(0, 0, W, H) elseif not BGF then bake(FLASH); BGF = gfx.snapshot(0, 0, W, H) end local flash, bolt = false, nil if cfg.storm ~= false then local k = frame // SLOT local lf = frame % SLOT - (90 + hash32(k * 0x85EBCA6B) % 100) if lf >= 0 and lf < 12 then flash = lf < 2 or (lf >= 3 and lf < 6) bolt = flash and BOLT[k % 2 + 1] or nil end end gfx.blit_rgb(flash and BGF or BGN, 0, 0, W, H) if bolt then for i = 1, #bolt - 1 do local a, b = bolt[i], bolt[i + 1] for y = a[2], b[2] - 1 do local x = floor(a[1] + (b[1] - a[1]) * (y - a[2]) / (b[2] - a[2]) + 0.5) rect(x, y, 2, 1, 255, 236, 226) end end end local lit = frame // 5 for i = 1, #WIN do local w = WIN[i] local d = hash32(lit * 31 + i) & 3 == 0 rect(w[1], w[2], w[3], w[4], d and 168 or 232, d and 36 or 64, d and 30 or 38) end rect(22, 40, 3, 6, 232, 64, 38); rect(23, 39, 1, 1, 232, 64, 38) for i = 1, #EMB do local e = EMB[i] local y = (e[2] - frame * e[3]) % 60 local iy = floor(y) local f = y - iy local x = e[1] + floor(1.5 * sin(frame * 0.03 + e[4]) + 0.5) local a = 0.25 + 0.7 * (y / 60) blend(x, iy, 232, 70, 40, a * (1 - f)) blend(x, iy + 1, 232, 70, 40, a * f) end local n = cfg.bats or 7 local t = frame % CY for i = 1, n do local u = t - (i - 1) * 4 if u >= 0 then bat(i, u) end end end