info = { type = "image", title = "Alien march", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Aliens march over pyramids; a UFO beams down", api = 33, } local tr = ({ sk = { ["Alien colour"] = "Farba votrelcov", ["Aliens per row"] = "Votrelcov v rade", ["Rows"] = "Rady" }, cs = { ["Alien colour"] = "Barva vetřelců", ["Aliens per row"] = "Vetřelců v řadě", ["Rows"] = "Řady" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "col", label = T("Alien colour"), type = "color", default = "#6CF06C" }, { key = "count", label = T("Aliens per row"), type = "int", min = 3, max = 4, default = 4 }, { key = "rows", label = T("Rows"), type = "int", min = 1, max = 2, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, 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 function c3(c) return c[1], c[2], c[3] end local function lerp3(a, b, u) return floor(a[1] + (b[1] - a[1]) * u + 0.5), floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5) end local function runs(rows, pal) local S = {} for r = 1, #rows do local s, out, k, n = rows[r], {}, 1, #rows[r] while k <= n do local ch, e = s:sub(k, k), k while e < n and s:sub(e + 1, e + 1) == ch do e = e + 1 end if ch ~= "." then out[#out + 1] = { k - 1, e - k + 1, pal[ch] } end k = e + 1 end S[r] = out end return S end local function sprite(S, x, y) local ix = floor(x) local f = x - ix for r = 1, #S do local row, yy, m = S[r], y + r - 1, #S[r] for k = 1, m do local e = row[k] local a, w, c = ix + e[1], e[2], e[3] if k == 1 then blend_rect(a, yy, 1, 1, c[1], c[2], c[3], 1 - f) if w > 1 then rect(a + 1, yy, w - 1, 1, c[1], c[2], c[3]) end else rect(a, yy, w, 1, c[1], c[2], c[3]) end if k == m and f > 0.03 then blend_rect(a + w, yy, 1, 1, c[1], c[2], c[3], f) end end end end local SKY0, SKY1 = { 26, 30, 92 }, { 92, 66, 142 } local SAND0, SAND1 = { 176, 132, 104 }, { 128, 90, 86 } local LIT, LIT_L = { 210, 164, 112 }, { 186, 142, 98 } local SHD, SHD_L = { 124, 88, 86 }, { 104, 72, 76 } local MOON = { 244, 236, 196 } local STAR, STAR_D = { 236, 236, 255 }, { 120, 124, 196 } local RIPPLE = { 150, 108, 92 } local BEAM = { 170, 255, 190 } local MX, MY = 56, 25 local PYR = { { 46, 33, 1.1 }, { 4, 42, 1.0 }, { 22, 27, 1.0 } } local function dune(x) return 50 + floor(1.3 * sin(x * 0.13 + 0.7) + 0.8 * sin(x * 0.37) + 0.5) end local function sky_ok(x, y) if y > 46 then return false end for i = 1, #PYR do local p = PYR[i] if y >= p[2] + math.abs(x - p[1]) / p[3] - 1 then return false end end local dx, dy = x - MX, y - MY return dx * dx + dy * dy > 64 end local BG = nil local function build_bg() for y = 0, 53 do rect(0, y, W, 1, lerp3(SKY0, SKY1, y / 53)) end for y = MY - 5, MY + 5 do for x = MX - 5, MX + 5 do local dx, dy = x - MX, y - MY local ex, ey = dx + 2.5, dy + 1.5 if dx * dx + dy * dy <= 27 and ex * ex + ey * ey > 20 then px(x, y, c3(MOON)) end end end for i = 1, #PYR do local p = PYR[i] local ax, ay, k = p[1], p[2], p[3] for y = ay, 54 do local hw = floor((y - ay) * k + 0.5) local stripe = (y - ay) % 3 == 2 if hw > 0 then rect(ax - hw, y, hw, 1, c3(stripe and SHD_L or SHD)) end rect(ax, y, hw + 1, 1, c3(stripe and LIT_L or LIT)) end end for x = 0, W - 1 do local d = dune(x) rect(x, d, 1, 54 - d, c3(SAND0)) end for y = 54, H - 1 do rect(0, y, W, 1, lerp3(SAND0, SAND1, (y - 54) / (H - 55))) end for k = 1, 14 do local h = hash32(k * 0x27D4EB2F) rect((h & 0x7FFF) % W, 54 + ((h >> 15) & 0x7FFF) % 9, 3 + (h >> 24 & 3), 1, c3(RIPPLE)) end BG = gfx.snapshot(0, 0, W, H) end local STARS = {} do local k, i = 0, 0 while k < 26 and i < 400 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = (h & 0x7FFF) % W, ((h >> 15) & 0x7FFF) % 47 if sky_ok(x, y) then k = k + 1 STARS[k] = { x, y, ((h >> 7) & 0xFF) / 40.0 } end end end local ART = { { { "..4.....4..", "...1...1...", "..1111111..", ".112212211.", ".111111111.", "..1112111..", ".1.1...1.1.", "1.1.....1.1" }, { "..4.....4..", "...1...1...", "..1111111..", ".112212211.", "11111111111", "..1112111..", "..1.1.1.1..", ".1...1...1." } }, { { ".33.....33.", ".32.....23.", "..1.....1..", "..1111111..", ".111111111.", "11.11111.11", "1..1...1..1", "..1.....1.." }, { ".33.....33.", ".23.....32.", "..1.....1..", "..1111111..", ".111111111.", "11.11111.11", ".1.1...1.1.", "1.........1" } }, { { "....111....", "..1111111..", ".111333111.", "11112331111", "11113331111", ".111111111.", ".1.1.1.1.1.", "..........." }, { "....111....", "..1111111..", ".111333111.", "11113321111", "11113331111", ".111111111.", "..1.1.1.1..", "..........." } }, } local MAP = { ["."] = "\0", ["1"] = "\1", ["2"] = "\2", ["3"] = "\3", ["4"] = "\4" } local CELLS = {} for t = 1, 3 do CELLS[t] = {} for f = 1, 2 do CELLS[t][f] = (table.concat(ART[t][f]):gsub(".", MAP)) end end local STEP = 12 local PAL = {} local N, ROWS, GAP, FW = 4, 2, 3, 53 local AC = { 108, 240, 108 } local UFO_ART = { ".....ddd.....", "....ddddd....", "..lllllllll..", "hhhhhhhhhhhhh", "..kkkkkkkkk.." } local UFO = runs(UFO_ART, { d = { 130, 230, 220 }, l = { 230, 236, 246 }, h = { 176, 184, 206 }, k = { 106, 112, 142 } }) local LIGHTS = { { 255, 220, 80 }, { 255, 90, 90 }, { 90, 230, 255 } } local READY = false local function apply() READY = true local C = cfg or {} local c = C.col or { r = 108, g = 240, b = 108 } AC = { c.r, c.g, c.b } local d = { floor(c.r * 0.18), floor(c.g * 0.18), floor(c.b * 0.18) + 20 } PAL[1] = { 0, 0, 0, c.r, c.g, c.b, d[1], d[2], d[3], 244, 250, 240, 255, 110, 200 } PAL[2] = { 0, 0, 0, c.r, c.g, c.b, d[1], d[2], d[3], 244, 250, 240, 255, 230, 90 } N = C.count or 4 if N < 3 then N = 3 elseif N > 4 then N = 4 end ROWS = C.rows or 2 if ROWS < 1 then ROWS = 1 elseif ROWS > 2 then ROWS = 2 end GAP = N == 4 and 3 or 5 FW = N * 11 + (N - 1) * GAP end function on_settings() apply() end local ROWY = { { 8 }, { 3, 14 } } local UP, UX, UY = 330, 34, 26 local function ufo_x(p) if p < 60 then local u = p / 60; return -14 + (UX + 14) * (1 - (1 - u) * (1 - u)) end if p < 150 then return UX + 1.2 * sin((p - 60) * 0.07) end if p < 210 then local u = (p - 150) / 60; return UX + (W + 2 - UX) * u * u end return nil end local function draw_beam(p, cx) if p < 70 or p >= 140 then return end local env = 1 if p < 78 then env = (p - 70) / 8 elseif p >= 132 then env = (140 - p) / 8 end local y0 = UY + 5 local len = floor((p - 70) * 2) if len > 58 - y0 then len = 58 - y0 end local r, g, b = c3(BEAM) for y = y0, y0 + len do local hw = floor(1 + (y - y0) * 0.22 + 0.5) blend_rect(floor(cx) - hw, y, 2 * hw + 2, 1, r, g, b, 0.30 * env) end if y0 + len >= 58 then blend_rect(floor(cx) - 7, 57, 16, 1, r, g, b, 0.35 * env) blend_rect(floor(cx) - 9, 58, 20, 1, r, g, b, 0.45 * env) blend_rect(floor(cx) - 7, 59, 16, 1, r, g, b, 0.35 * env) for k = 0, 5 do local yy = 57 - floor((p * 0.6 + k * 4.7) % 24) local xx = floor(cx) + ((k * 5) % 7) - 3 px(xx, yy, 230, 255, 236) end end end function render() if not READY then apply() end if not BG then build_bg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) for k = 1, #STARS do local s = STARS[k] if sin(t * 0.07 + s[3] * 3) > -0.25 then px(s[1], s[2], c3(STAR)) if k <= 3 then px(s[1] - 1, s[2], c3(STAR_D)); px(s[1] + 1, s[2], c3(STAR_D)) px(s[1], s[2] - 1, c3(STAR_D)); px(s[1], s[2] + 1, c3(STAR_D)) end else px(s[1], s[2], c3(STAR_D)) end end local p = (t + 285) % UP local ux = ufo_x(p) if ux then draw_beam(p, ux + 6.5) sprite(UFO, ux, UY) local ix = floor(ux + 0.5) px(ix + 6, UY + 1, c3(AC)) local ph = t // 4 for i, o in ipairs({ 1, 4, 8, 11 }) do px(ix + o, UY + 3, c3(LIGHTS[(ph + i) % 3 + 1])) end end local s = t // STEP local fr = s % 2 + 1 local R = W - 2 - FW local m = s % (2 * R) local off = m <= R and m or 2 * R - m local ys = ROWY[ROWS] for row = 1, ROWS do for k = 1, N do local ty = (k + row * 2 - 3) % 3 + 1 gfx.blit_indexed(CELLS[ty][fr], PAL[fr], 1 + off + (k - 1) * (11 + GAP), ys[row], 11, 8, 11, 8, true) end end end