info = { type = "image", title = "Igloo glide", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Penguins by an igloo under a dancing aurora", api = 33, } local tr = ({ sk = { ["Snowflakes"] = "Vločky", ["Aurora colour"] = "Farba žiary" }, cs = { ["Snowflakes"] = "Vločky", ["Aurora colour"] = "Barva záře" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snowflakes"), type = "int", min = 0, max = 40, default = 20 }, { key = "aurora", label = T("Aurora colour"), type = "color", default = "#46F0A0" }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local byte, char, concat, unpack = string.byte, string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local SKY, SKY_LO = { 20, 30, 84 }, { 54, 76, 146 } local MOON, SHINE = { 250, 242, 206 }, { 44, 56, 118 } local HALO1, HALO2 = { 52, 66, 134 }, { 34, 46, 108 } local RIDGE, RIDGE_HI = { 132, 160, 212 }, { 206, 222, 248 } local ICE_HI, ICE = { 176, 198, 232 }, { 214, 228, 246 } local CRACK, SHADOW = { 192, 210, 238 }, { 178, 198, 232 } local IG, IG_S, IG_J = { 242, 248, 255 }, { 202, 218, 244 }, { 168, 188, 224 } local DOOR_IN, DOOR_T = { 250, 172, 84 }, { 222, 128, 64 } local GLOW = { 255, 196, 120 } local FLAKE = { 236, 242, 255 } local STAR, STAR_DIM = { 236, 236, 255 }, { 110, 124, 184 } local MX, MY, MR = 11, 9, 6 local HORIZON = 36 local IX, IY, IR = 50, 49, 12 local function c3(c) return c[1], c[2], c[3] end local PAL = { 0, 0, 0, 22, 26, 48, 244, 248, 255, 250, 160, 50, 150, 160, 184, 200, 208, 224 } local MAP = { ["."] = "\0", K = "\1", W = "\2", O = "\3", G = "\4", g = "\5" } local ADULT = { "...KKK...", "..KKKKK..", ".KKWKWKK.", ".KKKOKKK.", ".KKWWWKK.", "KKWWWWWKK", "KKWWWWWKK", "KKWWWWWKK", ".KWWWWWK.", ".KKWWWKK.", "..KKKKK..", "..OO.OO..", } local BABY = { "..KKK..", ".KKKKK.", ".WKWKW.", ".WWOWW.", "GGWWWGG", "GGgggGG", ".GgggG.", "..O.O..", } local function lean(rows, s, top) local out = {} local n = #rows for r = 1, n do local row = "." .. rows[r] .. "." if r <= top and s ~= 0 then if s < 0 then row = row:sub(2) .. "." else row = "." .. row:sub(1, -2) end end if r == n and s ~= 0 then local w = #row if s < 0 then row = row:sub(1, w // 2) .. row:sub(w // 2 + 1):gsub("O(O?)", ".%1", 1) else row = (row:sub(1, w // 2):reverse():gsub("O(O?)", ".%1", 1)):reverse() .. row:sub(w // 2 + 1) end end out[r] = (row:gsub(".", MAP)) end return concat(out), #rows[1] + 2, n end local AD, BB = {}, {} for s = -1, 1 do AD[s] = lean(ADULT, s, 5); BB[s] = lean(BABY, s, 4) end local AW, AH = #ADULT[1] + 2, #ADULT local BW, BH = #BABY[1] + 2, #BABY local SLIDE = { "...........KKK..", "..KKKKKKKKKKKWK.", "OKKKKKKKKKKKKKOO", "O.WWWWWWWWWWWW..", } local SW, SH = #SLIDE[1], #SLIDE local SLY = 52 local KR, KG, KB = 255, 0, 255 local NP = 4 local function shift_x(cells, w, h, f) local out, row = {}, {} local g = 1 - f local br, bg, bb = c3(ICE) for y = 0, h - 1 do local base, k = y * w, 0 for x = 0, w do local a = (x >= 1 and f > 0) and byte(cells, base + x) or 0 local b = (x < w and g > 0) and byte(cells, base + x + 1) or 0 local r1, g1, b1 = KR, KG, KB if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = br, bg, bb, br, bg, bb if a ~= 0 then ar, ag, ab = PAL[3 * a + 1], PAL[3 * a + 2], PAL[3 * a + 3] end if b ~= 0 then cr, cg, cb = PAL[3 * b + 1], PAL[3 * b + 2], PAL[3 * b + 3] end r1 = floor(ar * f + cr * g + 0.5) g1 = floor(ag * f + cg * g + 0.5) b1 = floor(ab * f + cb * g + 0.5) end row[k + 1], row[k + 2], row[k + 3] = r1, g1, b1 k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local SLIDE_C = {} for r = 1, SH do SLIDE_C[r] = (SLIDE[r]:gsub(".", MAP)) end SLIDE_C = concat(SLIDE_C) local SLIDE_R = {} local SX, SY, SPH = {}, {}, {} do local k, i = 0, 0 while k < 14 and i < 200 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 27) local dx, dy = x - MX, y - MY if dx * dx + dy * dy > (MR + 6) * (MR + 6) then k = k + 1 SX[k], SY[k], SPH[k] = x, y, umod(h >> 16, 628) / 100.0 end end end local FX, FV, FY, FW, FP, FC = {}, {}, {}, {}, {}, {} for i = 1, 40 do local h = hash32(i * 0x85EBCA6B + 7) FX[i] = umod(h, W) FV[i] = 0.18 + umod(h >> 8, 30) / 100.0 FY[i] = umod(h >> 14, 74) FW[i] = TAU / ((2 + umod(h >> 20, 20) / 10.0) * STEP_HZ) FP[i] = umod(h >> 4, 628) / 100.0 FC[i] = 0.55 + (FV[i] - 0.18) * 1.4 end local BG = nil local function build_bg() local G0, G1 = 16, 33 rect(0, 0, W, G0, c3(SKY)) for y = G0, G1 - 1 do local t = (y - G0 + 1) / (G1 - G0 + 1) rect(0, y, W, 1, floor(SKY[1] + (SKY_LO[1] - SKY[1]) * t + 0.5), floor(SKY[2] + (SKY_LO[2] - SKY[2]) * t + 0.5), floor(SKY[3] + (SKY_LO[3] - SKY[3]) * t + 0.5)) end rect(0, G1, W, HORIZON - G1, c3(SKY_LO)) for y = MY - MR - 5, MY + MR + 5 do for x = MX - MR - 5, MX + MR + 5 do local dx, dy = x - MX, y - MY local d = sqrt(dx * dx + dy * dy) if d <= MR + 0.5 then local ex, ey = x - MX - 3, y - MY + 2 if ex * ex + ey * ey <= (MR - 0.5) * (MR - 0.5) then px(x, y, c3(SHINE)) else px(x, y, c3(MOON)) end elseif d <= MR + 2.5 then px(x, y, c3(HALO1)) elseif d <= MR + 4.5 then px(x, y, c3(HALO2)) end end end for x = 0, W - 1 do local top = floor(31.5 + 1.8 * sin(x * 0.19 + 0.5) + 1.3 * sin(x * 0.53) + 0.5) rect(x, top, 1, HORIZON - top, c3(RIDGE)) px(x, top, c3(RIDGE_HI)) end for y = HORIZON, 43 do local t = (y - HORIZON + 1) / 9 rect(0, y, W, 1, floor(ICE_HI[1] + (ICE[1] - ICE_HI[1]) * t + 0.5), floor(ICE_HI[2] + (ICE[2] - ICE_HI[2]) * t + 0.5), floor(ICE_HI[3] + (ICE[3] - ICE_HI[3]) * t + 0.5)) end rect(0, 44, W, H - 44, c3(ICE)) rect(2, 47, 9, 1, c3(CRACK)); rect(9, 48, 5, 1, c3(CRACK)) rect(20, 60, 12, 1, c3(CRACK)); rect(30, 61, 6, 1, c3(CRACK)) rect(46, 59, 8, 1, c3(CRACK)); rect(4, 62, 7, 1, c3(CRACK)) rect(IX - IR - 1, IY + 1, 2 * IR + 3, 1, c3(SHADOW)) for y = IY - IR, IY do local dy = IY - y local hw = floor(sqrt(IR * IR - dy * dy) + 0.5) local sh = floor(hw * 0.35 + 0.5) rect(IX - hw, y, 2 * hw + 1, 1, c3(IG)) rect(IX + sh, y, hw - sh + 1, 1, c3(IG_S)) if dy > 0 and dy % 3 == 0 then rect(IX - hw + 1, y, 2 * hw - 1, 1, c3(IG_J)) end if dy % 3 ~= 0 then local off = (dy // 3) % 2 == 0 and 0 or 2 for x = IX - hw + 2 + off, IX + hw - 2, 4 do if (dy % 3) == 1 or x % 8 < 4 then px(x, y, c3(IG_J)) end end end end rect(39, 43, 9, 7, c3(IG)); rect(40, 42, 7, 1, c3(IG)); rect(41, 41, 5, 1, c3(IG)) rect(46, 43, 2, 7, c3(IG_S)); rect(39, 49, 9, 1, c3(IG_S)) rect(41, 44, 5, 5, c3(DOOR_IN)); rect(42, 43, 3, 1, c3(DOOR_T)); rect(41, 44, 5, 1, c3(DOOR_T)) blend_rect(37, 50, 13, 1, GLOW[1], GLOW[2], GLOW[3], 0.45) blend_rect(39, 51, 9, 1, GLOW[1], GLOW[2], GLOW[3], 0.25) rect(13, 50, 11, 1, c3(SHADOW)); rect(28, 50, 9, 1, c3(SHADOW)) BG = gfx.snapshot(0, 0, W, H) end local AR, AG, AB, NSNOW = 70, 240, 160, 20 function on_settings() local c = cfg.aurora or { r = 70, g = 240, b = 160 } AR, AG, AB = c.r, c.g, c.b NSNOW = cfg.snow or 20 end local function draw_stars(t) local a = t * (TAU / (3 * STEP_HZ)) for k = 1, #SX do local c = sin(a + SPH[k] * 3) > -0.3 and STAR or STAR_DIM px(SX[k], SY[k], c[1], c[2], c[3]) end end local function draw_aurora(t) for x = 14, 62, 2 do local yt = 7 + 3 * sin(x * 0.14 + t * 0.035) + 1.5 * sin(x * 0.37 - t * 0.05) local yb = yt + 10 + 3 * sin(x * 0.08 + t * 0.02) local a = 0.33 + 0.2 * sin(x * 0.21 - t * 0.045) local it, ib = floor(yt), floor(yb) blend_rect(x, it, 2, 1, AR, AG, AB, a * (1 - (yt - it))) blend_rect(x, it + 1, 2, 3, AR, AG, AB, a) local at = a * 0.4 if ib > it + 4 then blend_rect(x, it + 4, 2, ib - it - 4, AR, AG, AB, at) end blend_rect(x, ib, 2, 1, AR, AG, AB, at * (yb - ib)) end end local function draw_penguins(t) local s = sin(t * TAU / (1.8 * STEP_HZ)) local l = s > 0.45 and 1 or (s < -0.45 and -1 or 0) gfx.blit_indexed(AD[l], PAL, 13, 38, AW, AH, AW, AH, true) s = sin(t * TAU / (1.1 * STEP_HZ) + 1.7) l = s > 0.45 and 1 or (s < -0.45 and -1 or 0) gfx.blit_indexed(BB[l], PAL, 28, 42, BW, BH, BW, BH, true) end local CYCLE, START, DUR = 12 * STEP_HZ, 2 * STEP_HZ, 5 * STEP_HZ local function draw_slider(t) local c = t % CYCLE - START if c < 0 or c >= DUR then return end local u = c / DUR local x = -SW - 1 + 100 * (1 - (1 - u) * (1 - u)) if x >= W then return end local v = 200 * (1 - u) / DUR local q = floor(x * NP + 0.5) local ix, p = q // NP, q % NP local xs = q / NP local xl, xr = xs + 2, xs + SW - 2 local il, ir = floor(xl), floor(xr) rect(il + 1, SLY + SH, ir - il - 1, 1, c3(SHADOW)) blend_rect(il, SLY + SH, 1, 1, SHADOW[1], SHADOW[2], SHADOW[3], 1 - (xl - il)) blend_rect(ir, SLY + SH, 1, 1, SHADOW[1], SHADOW[2], SHADOW[3], xr - ir) local R = SLIDE_R[p] if not R then R = shift_x(SLIDE_C, SW, SH, p / NP); SLIDE_R[p] = R end gfx.blit_rgb(R, ix, SLY, SW + 1, SH, KR, KG, KB) if v > 0.25 then local a = v > 1 and 0.9 or v * 0.9 for k = 1, 3 do local h = hash32((t // 3) * 0x9E3779B1 + k) local sx = ix - 1 - k * 2 - umod(h, 2) local sy = SLY + 2 - umod(h >> 4, 3) blend(sx, sy, 255, 255, 255, a * (1 - k * 0.25)) end end end local function draw_snow(t) for i = 1, NSNOW do local y = (FY[i] + t * FV[i]) % 74 - 5 local x = (FX[i] + floor(1.2 * sin(t * FW[i] + FP[i]) + 0.5)) % W local iy = floor(y) local f = y - iy local a = FC[i] blend(x, iy, FLAKE[1], FLAKE[2], FLAKE[3], a * (1 - f)) blend(x, iy + 1, FLAKE[1], FLAKE[2], FLAKE[3], a * f) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS draw_stars(t) draw_aurora(t) draw_penguins(t) draw_slider(t) draw_snow(t) end