info = { type = "image", title = "Santa UFO", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A UFO lands and a sad alien catches a snowflake", api = 33, } local tr = ({ sk = { ["Snow"] = "Sneh", ["Santa hat"] = "Santova čiapka" }, cs = { ["Snow"] = "Sníh", ["Santa hat"] = "Santova čepice" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snow"), type = "int", min = 0, max = 5, default = 3 }, { key = "hat", label = T("Santa hat"), type = "bool", default = true }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, sin, exp, sqrt, abs = math.floor, math.sin, math.exp, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU, PI = 6.28318, 3.14159 local CYC = 660 local FEET = 56 local UW, UH = 26, 19 local UX, UY = 2, FEET - UH local ATOP = FEET - 18 local function lerp(a, b, u) return a + (b - a) * u end local function c3(c) return c[1], c[2], c[3] end local function frect(x, y, w, h, r, g, b) local x0 = floor(x) local f = x - x0 if f < 0.05 then rect(x0, y, w, h, r, g, b) return end blend_rect(x0, y, 1, h, r, g, b, 1 - f) if w > 1 then rect(x0 + 1, y, w - 1, h, r, g, b) end blend_rect(x0 + w, y, 1, h, r, g, b, f) end local function fdot(x, y, r, g, b, a) local x0, y0 = floor(x), floor(y) local fx, fy = x - x0, y - y0 blend(x0, y0, r, g, b, a * (1 - fx) * (1 - fy)) blend(x0 + 1, y0, r, g, b, a * fx * (1 - fy)) blend(x0, y0 + 1, r, g, b, a * (1 - fx) * fy) blend(x0 + 1, y0 + 1, r, g, b, a * fx * fy) end local BG = nil local function tree(x, yb, h) for k = 0, 2 do local y0 = yb - h + k * (h // 3) local half = 2 + k * 2 rect(x - half, y0 + 2, 2 * half + 1, h // 3 + 1, 14, 52, 56) rect(x - half + 1, y0 + 1, 2 * half - 1, 1, 214, 228, 248) end rect(x, yb - 1, 1, 2, 40, 30, 36) end local function build_bg() rect(0, 0, W, 28, 6, 10, 34) for y = 28, 49 do local t = ((y - 28) / 21) ^ 1.5 rect(0, y, W, 1, floor(lerp(6, 30, t) + 0.5), floor(lerp(10, 48, t) + 0.5), floor(lerp(34, 104, t) + 0.5)) end for i = 1, 16 do local h = hash32(i * 0x9E3779B1 + 11) local b = (i % 4 == 0) and 230 or 130 rect(umod(h, W), umod(h >> 8, 34), 1, 1, b, b, 255) end for x = 0, W - 1 do local h = floor(4 + 2 * sin(x * 0.13) + 1.5 * sin(x * 0.31 + 2) + 0.5) rect(x, 50 - h, 1, h, 26, 42, 88) rect(x, 50 - h, 1, 1, 70, 92, 150) end tree(57, 51, 21); tree(51, 50, 13); tree(61, 50, 14); tree(37, 50, 9) rect(30, 41, 7, 6, 52, 44, 64); rect(29, 40, 9, 2, 218, 230, 248); rect(35, 38, 1, 3, 70, 60, 80) rect(32, 43, 2, 2, 255, 210, 110) for y = 50, H - 1 do local t = (y - 50) / 13 rect(0, y, W, 1, floor(lerp(176, 122, t) + 0.5), floor(lerp(198, 146, t) + 0.5), floor(lerp(240, 206, t) + 0.5)) end rect(2, 52, 14, 1, 214, 226, 248); rect(30, 58, 18, 1, 200, 214, 244); rect(8, 61, 20, 1, 190, 206, 242) rect(4, 53, 14, 1, 150, 174, 224); rect(32, 59, 18, 1, 134, 158, 214) BG = snapshot(0, 0, W, H) end local UP = { 0, 0, 0, 28, 34, 60, 96, 214, 226, 190, 248, 250, 206, 214, 230, 144, 154, 184, 92, 100, 134, 40, 46, 80, 120, 128, 150 } local function saucer(legs) local mask = {} for y = 0, UH - 1 do mask[y] = {} for x = 0, UW - 1 do local dx = x - 12.5 local dome = (dx / 7.5) ^ 2 + ((y - 8.5) / 8.5) ^ 2 local sau = (dx / 13) ^ 2 + ((y - 12.5) / 6) ^ 2 mask[y][x] = (dome <= 1 and y <= 8) and 2 or (sau <= 1 and (y < 18 or legs or (x >= 11 and x <= 14))) and 4 or 0 end end local rows = {} for y = 0, UH - 1 do local row = {} for x = 0, UW - 1 do local v = mask[y][x] if v ~= 0 then local edge = x == 0 or y == 0 or x == UW - 1 or y == UH - 1 or mask[y][x - 1] == 0 or mask[y][x + 1] == 0 or mask[y - 1] and mask[y - 1][x] == 0 or mask[y + 1] and mask[y + 1][x] == 0 if edge then v = 1 elseif v == 2 then v = ((x == 8 or x == 9) and y >= 2 and y <= 4) and 3 or 2 elseif y <= 9 then v = 4 elseif y <= 12 then v = 5 else v = 6 end if v ~= 1 and y == 12 and (x == 4 or x == 8 or x == 13 or x == 17 or x == 21) then v = 7 end if legs and y == 18 and (x == 7 or x == 8 or x == 17 or x == 18) then v = 8 end end row[#row + 1] = string.char(v) end rows[#rows + 1] = table.concat(row) end return table.concat(rows) end local UFO_OUT, UFO_IN local RED, WHITE, GREEN, GREEN2 = { 206, 36, 48 }, { 246, 246, 250 }, { 146, 214, 104 }, { 104, 168, 72 } local EYE, SUIT, BLUE, GOLD = { 16, 22, 28 }, { 236, 242, 252 }, { 64, 112, 208 }, { 250, 216, 80 } local LEG, BOOT, PINK, MOUTH = { 70, 84, 150 }, { 40, 50, 100 }, { 250, 120, 150 }, { 70, 30, 40 } local HAT = true local function draw_alien(cx, d, pose, t) local walk = pose == "walk" local spread = walk and (t // 5) % 2 == 0 local y = ATOP + (spread and 1 or 0) local function part(ox, oy, w, h, c) frect(d == 1 and cx + ox or cx - ox - w, y + oy, w, h, c[1], c[2], c[3]) end local aR, aF = spread and 0 or 1, spread and 1 or 0 part(-4, 10 + aR, 2, 6, SUIT); part(2, 10 + aF, 2, 6, SUIT) part(-4, 15 + aR, 2, 1, GREEN); part(2, 15 + aF, 2, 1, GREEN) if spread then part(-3, 15, 2, 3, LEG); part(1, 15, 2, 3, LEG); part(-3, 17, 2, 1, BOOT); part(1, 17, 2, 1, BOOT) else part(-2, 15, 2, 3, LEG); part(0, 15, 2, 3, LEG); part(-2, 17, 2, 1, BOOT); part(0, 17, 2, 1, BOOT) end part(-3, 10, 6, 5, SUIT); part(-3, 10, 6, 1, BLUE); part(0, 12, 1, 1, GOLD) part(-4, 4, 8, 3, GREEN); part(-3, 7, 6, 2, GREEN); part(-2, 9, 4, 1, GREEN) part(2, 5, 2, 3, GREEN2) if pose == "happy" then part(-3, 6, 2, 1, EYE); part(1, 6, 2, 1, EYE) part(-1, 8, 2, 1, GREEN2); part(-2, 7, 1, 1, GREEN2); part(1, 7, 1, 1, GREEN2) elseif pose == "up" then part(-3, 4, 2, 2, EYE); part(1, 4, 2, 2, EYE); part(-2, 4, 1, 1, WHITE); part(2, 4, 1, 1, WHITE) part(-1, 8, 2, 2, MOUTH); part(1, 8, 5, 1, PINK) else part(-3, 5, 2, 2, EYE); part(1, 5, 2, 2, EYE); part(-2, 5, 1, 1, WHITE); part(2, 5, 1, 1, WHITE) part(-1, 8, 2, 1, GREEN2); part(-2, 9, 1, 1, GREEN2); part(1, 9, 1, 1, GREEN2) end if HAT then part(-3, 1, 6, 2, RED); part(-2, 0, 4, 1, RED); part(-6, 1, 3, 1, RED); part(-7, 2, 2, 2, WHITE) part(-4, 3, 8, 1, WHITE) end end local ARR, SET, OUT, LOOK, CATCH, HAPPY, SAD, BACK, HUM, LIFT = 20, 110, 150, 250, 355, 390, 405, 505, 525, 535 local function ufo_pos(t) if t < ARR then return nil end if t < SET then local s = (t - ARR) / (SET - ARR) return lerp(80, UX, 1 - (1 - s) ^ 3), lerp(-24, UY, 1 - (1 - s) ^ 2) elseif t < OUT then local u = t - SET return UX, UY - floor(2.6 * exp(-u / 9) * abs(sin(u * 0.45)) + 0.5) elseif t < HUM then return UX + ((t >= BACK and t // 2 % 2 == 0) and 1 or 0), UY elseif t < LIFT then return UX, UY + 1 elseif t < 600 then local u = t - LIFT return UX - 0.0115 * u * u, UY + 1 - 0.0155 * u * u end return nil end local function alien_state(t) if t < OUT or t >= BACK then return nil end if t < LOOK then return lerp(17, 46, (t - OUT) / (LOOK - OUT)), 1, "walk" end if t < CATCH then return 46, 1, "up" end if t < HAPPY then return 46, 1, "happy" end if t < SAD then return 46, 1, "idle" end return lerp(46, 14, (t - SAD) / (BACK - SAD)), -1, "walk" end local NF = 18 local function draw_snow(t, far) local n, v, a = far and NF or NF // 2, far and 0.318 or 0.636, far and 0.85 or 1 local per = far and 132 or 165 for i = 1, n do local h = hash32(i * 0x9E3779B1 + (far and 1 or 2)) local y = (umod(h >> 8, 70) + t * v) % 70 - 3 local x = umod(h, W) + floor(1.5 * sin(TAU * (t % per) / per + umod(h >> 16, 628) / 100) + 0.5) local y0 = floor(y) local f = y - y0 blend(x, y0, 255, 255, 255, a * (1 - f)) blend(x, y0 + 1, 255, 255, 255, a * f) end end local function draw_flake(x, y) fdot(x, y, 255, 255, 255, 1) fdot(x - 1, y, 235, 244, 255, 0.7); fdot(x + 1, y, 235, 244, 255, 0.7) fdot(x, y - 1, 235, 244, 255, 0.7); fdot(x, y + 1, 235, 244, 255, 0.7) end function on_settings() NF = (cfg.snow or 3) * 6 HAT = cfg.hat ~= false end function render() if not BG then build_bg() elseif not UFO_OUT then UFO_OUT = saucer(true) elseif not UFO_IN then UFO_IN = saucer(false) end blit_rgb(BG, 0, 0, W, H) local t = frame % CYC for i = 1, 4 do local h = hash32(i * 0x27D4EB2F + 6) local a = sin(TAU * (t % 110) / 110 + i * 1.7) if a > 0 then blend(umod(h, W), umod(h >> 8, 30), 230, 240, 255, a * 0.8) end end if NF > 0 then draw_snow(t, true) end local ux, uy = ufo_pos(t) if ux and uy > UY - 8 then blend_rect(floor(ux) + 3, FEET - 1, 20, 2, 40, 60, 130, 0.4) end local ax, ad, pose = alien_state(t) if ax then blend_rect(floor(ax) - 4, FEET - 1, 9, 1, 40, 60, 130, 0.45) draw_alien(ax, ad, pose, t) end if ux and UFO_IN then local x, y = floor(ux + 0.5), floor(uy + 0.5) local out = t >= ARR + 60 and t < LIFT blit_indexed(out and UFO_OUT or UFO_IN, UP, x, y, UW, UH, UW, UH, true) local k = (t // 4) % 2 local fast = (t >= HUM) and 1 or 0 for i, lx in ipairs({ 4, 8, 13, 17, 21 }) do local on = (i + k + fast * (t % 2)) % 2 == 0 if on then rect(x + lx, y + 12, 1, 1, 255, 226, 100) else rect(x + lx, y + 12, 1, 1, 255, 90, 80) end end if (t < SET - 6 or t >= LIFT) and y + UH < FEET then rect(x + 12, y + UH, 2, 2 + t % 2, 255, 170, 60); rect(x + 12, y + UH, 2, 1, 255, 240, 190) end end if t >= LOOK + 15 and t < CATCH then local s = (CATCH - t) / (CATCH - LOOK - 15) draw_flake(51 + 5 * s * sin(s * 11), lerp(ATOP + 7, -4, s)) end if NF > 0 then draw_snow(t, false) end end