info = { type = "image", title = "Lunar lander", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lander touches down, astronaut plants a flag", api = 33, } local tr = ({ sk = { ["Earth"] = "Zem", ["Full"] = "Spln", ["Crescent"] = "Kosák" }, cs = { ["Earth"] = "Země", ["Full"] = "Úplněk", ["Crescent"] = "Srpek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "earth", label = T("Earth"), type = "int", default = "full", opts = { keys = { "full", "crescent" }, T("Full"), T("Crescent") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt, exp, min, max = math.floor, math.sin, math.cos, math.sqrt, math.exp, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat, unpack, byte = string.char, table.concat, table.unpack, string.byte local L = 620 local T0, TREST, TD = -30, 28, 170 local X0, GY = 3, 56 local KEY = { 255, 0, 255 } local function clamp(v, a, b) return v < a and a or v > b and b or v end local function lattice(w, h, sd) local t = {} for i = 0, w * h - 1 do t[i] = (hash32(i * 0x9E3779B1 + sd) & 0xFFFF) / 65535 end return t, w end local function vn(t, w, x, y) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy fx, fy = fx * fx * (3 - 2 * fx), fy * fy * (3 - 2 * fy) local a, b = t[iy * w + ix], t[iy * w + ix + 1] local c, d = t[(iy + 1) * w + ix], t[(iy + 1) * w + ix + 1] return a + (b - a) * fx + (c - a) * fy + (a - b - c + d) * fx * fy end local EN1, EN2 local function earth(crescent) local sx, sy, sz = -0.35, -0.35, 0.87 if crescent then sx, sy, sz = -0.8, -0.2, -0.55 end local n1, w1, n2, w2 = EN1, 18, EN2, 18 local rows = {} for y = 0, 16 do local row = {} for x = 0, 16 do local nx, ny = (x - 8) / 8.3, (y - 8) / 8.3 local r2 = nx * nx + ny * ny local r, g, b = KEY[1], KEY[2], KEY[3] if r2 < 1.06 then local nz = sqrt(max(0, 1 - r2)) local l = nx * sx + ny * sy + nz * sz local lum = l > 0 and (0.05 + 0.95 * l ^ 0.7) or 0.05 if crescent then lum = min(1, lum * 2) end local u, v = nx * 2.2 + 3, ny * 2.2 + 3 local land = vn(n1, w1, u, v) + 0.35 * vn(n1, w1, u * 2, v * 2) local cr, cg, cb = 24, 74, 178 if land > 0.98 then local d = clamp((land - 0.98) * 5, 0, 1) cr, cg, cb = 40 + 90 * d, 128 - 10 * d, 70 - 10 * d end local cl = vn(n2, w2, u * 1.6 + 5, v * 1.6) - 0.5 if cl > 0 then local a = clamp(cl * 3.2, 0, 0.9) cr, cg, cb = cr + (244 - cr) * a, cg + (246 - cg) * a, cb + (250 - cb) * a end local rim = clamp((r2 - 0.6) * 1.6, 0, 0.5) cr, cg, cb = cr + (130 - cr) * rim, cg + (190 - cg) * rim, cb + (255 - cb) * rim local cov = clamp((1.06 - r2) * 9, 0, 1) r, g, b = floor(cr * lum * cov + 0.5), floor(cg * lum * cov + 0.5), floor(cb * lum * cov + 0.5) if r == 0 and g == 0 and b == 0 then r, g, b = 0, 0, 1 end end local k = x * 3 row[k + 1], row[k + 2], row[k + 3] = r, g, b end rows[y + 1] = char(unpack(row, 1, 51)) end return concat(rows) end local CRATERS = { { 12, 52, 9, 2.6 }, { 47, 60, 12, 3.2 }, { 30, 48.5, 5, 1.4 }, { 58, 50.5, 6, 1.7 }, { 4, 58, 6, 2 }, { 36, 62, 7, 1.6 } } local GN, GW local function hz(x) return floor(45.5 + 0.9 * sin(x * 0.17 + 0.5) + 0.6 * sin(x * 0.41)) end local GROUND = {} local function ground_rows(ya, yb) for y = ya, yb do local row = {} local t = (y - 44) / 19 local br, bg, bb = 150 - 46 * t, 146 - 46 * t, 138 - 45 * t for x = 0, 63 do local r, g, b = 0, 0, 0 if y >= hz(x) then local f = 0.95 + 0.20 * (vn(GN, GW, x / 5 + 1, (y - 44) / 2.5 + 1) - 0.5) f = f * (1.07 - 0.15 * x / 63) * (1 + 0.03 * ((hash32(x * 977 + y * 131) & 7) - 3.5)) if y - hz(x) < 2 then f = f * 1.05 end for i = 1, #CRATERS do local c = CRATERS[i] local dx, dy = (x - c[1]) / c[3], (y - c[2]) / c[4] if dx < 1.25 and dx > -1.25 and dy < 1.25 and dy > -1.25 then local r2 = dx * dx + dy * dy if r2 < 1 then f = f * (0.9 + 0.38 * dx * sqrt(r2)) elseif r2 < 1.5625 and dx < 0 then f = f * (1 + 0.17 * (1.5625 - r2) / 0.56 * -dx) end end end r, g, b = floor(clamp(br * f, 0, 255) + 0.5), floor(clamp(bg * f, 0, 255) + 0.5), floor(clamp(bb * f, 0, 255) + 0.5) end local k = x * 3 row[k + 1], row[k + 2], row[k + 3] = r, g, b end GROUND[y - 43] = char(unpack(row, 1, 192)) end end local LMC, LMS local LMPAL = { 236, 196, 96, 206, 154, 52, 140, 96, 34, 86, 56, 24, 226, 226, 232, 176, 178, 190, 112, 114, 130, 24, 28, 40, 204, 204, 212, 110, 110, 124, 60, 56, 66, 156, 156, 162, 246, 246, 250, } local function line(x1, y1, x2, y2, v) local dy = y2 - y1 for y = y1, y2 do local x = floor(x1 + (x2 - x1) * (y - y1) / dy + 0.5) LMC[y * 32 + x + 1] = v end end local function build_lm() LMC = {} for i = 1, 32 * 29 do LMC[i] = 0 end local function set(x, y, v) LMC[y * 32 + x + 1] = v end line(7, 17, 1, 27, 9); line(8, 17, 2, 27, 10) line(24, 17, 30, 27, 9); line(23, 17, 29, 27, 10) line(11, 21, 9, 27, 9); line(20, 21, 22, 27, 9) for _, yy in ipairs({ 20, 23, 26 }) do local x = floor(24 + (yy - 17) * 0.6 + 0.5) set(x - 1, yy, 13); set(x, yy, 9); set(x + 1, yy, 9) end for _, fx in ipairs({ 1, 30, 9, 22 }) do for k = -2, 2 do set(fx + k, 28, 12) end if fx == 1 or fx == 30 then set(fx, 27, 12) end end for y = 13, 21 do local a, b = 6, 25 if y == 13 or y == 21 then a, b = 7, 24 end for x = a, b do local v = x < 10 and 2 or x < 22 and 1 or 3 local h = hash32(x * 7919 + y * 104729) & 15 if h < 4 then v = v + 1 elseif h > 13 and v > 1 then v = v - 1 end if x == 15 or x == 16 then v = max(v, 2) end if y == 21 then v = 4 elseif y == 13 and x >= 10 and x <= 21 then v = 3 end set(x, y, min(v, 4)) end end for y = 22, 25 do local a, b = 13, 18 if y >= 24 then a, b = 12, 19 end for x = a, b do set(x, y, x == a and 9 or 11) end end for y = 3, 12 do local hw = y == 3 and 4 or 6 for x = 16 - hw, 15 + hw do local v = (x >= 12 and x <= 19) and 5 or x < 12 and 6 or 7 if y == 3 then v = 5 end set(x, y, v) end end set(13, 6, 8); set(14, 6, 8); set(13, 7, 8); set(17, 6, 8); set(18, 6, 8); set(18, 7, 8) set(13, 6, 13) for x = 10, 21 do set(x, 9, x >= 12 and x <= 19 and 6 or 7); set(x, 12, 7) end for x = 14, 17 do set(x, 2, 5) end set(15, 1, 9); set(16, 0, 9); set(15, 0, 9) set(9, 8, 7); set(22, 8, 7) local rows = {} for y = 0, 28 do rows[y + 1] = char(unpack(LMC, y * 32 + 1, y * 32 + 32)) end LMS = concat(rows) LMC = nil end local RAS, built = {}, false local function shift_y(f) local out, g = {}, 1 - f for y = 0, 29 do local row, k = {}, 0 for x = 0, 31 do local a = (y >= 1 and f > 0) and byte(LMS, (y - 1) * 32 + x + 1) or 0 local b = (y < 29 and g > 0) and byte(LMS, y * 32 + x + 1) or 0 local r, gg, bb = KEY[1], KEY[2], KEY[3] if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = 0, 0, 0, 0, 0, 0 if a ~= 0 then ar, ag, ab = LMPAL[3 * a - 2], LMPAL[3 * a - 1], LMPAL[3 * a] end if b ~= 0 then cr, cg, cb = LMPAL[3 * b - 2], LMPAL[3 * b - 1], LMPAL[3 * b] end r, gg, bb = floor(ar * f + cr * g + 0.5), floor(ag * f + cg * g + 0.5), floor(ab * f + cb * g + 0.5) if r == 255 and gg == 0 and bb == 255 then r = 254 end end row[k + 1], row[k + 2], row[k + 3] = r, gg, bb k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local BG, EARTH, stage = nil, nil, 0 local function build_step() stage = stage + 1 if stage == 1 then GN, GW, GROUND = lattice(16, 12, 3), 16, {} EN1, EN2 = lattice(18, 18, 11), lattice(18, 18, 29) elseif stage == 2 then EARTH = earth(cfg.earth == "crescent"); EN1, EN2 = nil, nil elseif stage == 3 then build_lm() elseif stage <= 13 then ground_rows(44 + (stage - 4) * 2, 45 + (stage - 4) * 2) else gfx.clear(0, 0, 0) for i = 1, 22 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, 64), umod(h >> 8, 40) local dx, dy = x - 50, y - 13 if dx * dx + dy * dy > 150 then local v = 70 + umod(h >> 16, 130) px(x, y, v, v, floor(v * 0.9 + 25)) end end gfx.blit_rgb(EARTH, 42, 5, 17, 17, KEY[1], KEY[2], KEY[3]) gfx.blit_rgb(concat(GROUND), 0, 44, 64, 20) for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) local x, y = 2 + umod(h, 58), 50 + umod(h >> 8, 12) blend_rect(x + 1, y, 3, 1, 0, 0, 0, 0.35) rect(x, y - 1, 2, 2, 168, 164, 156); px(x, y - 1, 200, 196, 188) end BG = gfx.snapshot(0, 0, 64, 64) EARTH, GROUND, GN = nil, nil, nil end end function on_settings() BG, stage, RAS, built = nil, 0, {}, false end local ND, DUST0, G = 26, 77, 0.012 local DP = {} for i = 1, ND do local h = hash32(i * 0xC2B2AE35) DP[i] = { tb = DUST0 + (i - 1) * 100 // ND, dir = (h & 1) * 2 - 1, vx = 0.2 + umod(h >> 4, 100) / 100, vz = 0.12 + umod(h >> 12, 100) / 290, yg = 54 + umod(h >> 20, 7), dx = umod(h >> 24, 7) - 3, big = umod(h >> 27, 4) == 0 } end local function dust(u, cx) for i = 1, ND do local p = DP[i] local age = u - p.tb if age >= 0 then local fl = 2 * p.vz / G local a = age < fl and 0.6 or 0.6 * (1 - (age - fl) / 60) if a > 0.04 then local ta = min(age, fl) local x = cx + p.dx + p.dir * p.vx * (ta + (age > fl and (age - fl) * 0.1 or 0)) * 0.9 local y = p.yg - (age < fl and (p.vz * age - 0.5 * G * age * age) or 0) local ix, iy = floor(x), floor(y) local f = x - ix blend_rect(ix, iy, p.big and 2 or 1, 1, 176, 170, 160, a * (1 - f)) blend_rect(ix + (p.big and 2 or 1), iy, 1, 1, 176, 170, 160, a * f) end end end end local function hr(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if f < 0.05 then rect(ix, y, w, h, r, g, b); return end if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end local function astronaut(fx, fy, legs, arm) hr(fx - 5, fy - 10, 3, 6, 188, 188, 198) local l1, l2 = fx - 3, fx if legs == 1 then l1, l2 = fx - 4, fx + 1 elseif legs == 2 then l1, l2 = fx - 2, fx + 2 end hr(l1, fy - 4, 2, 3, 214, 214, 222); hr(l2, fy - 4, 2, 3, 168, 168, 182) hr(l1 - 0.5, fy - 1, 3, 1, 64, 62, 72); hr(l2 - 0.5, fy - 1, 3, 1, 56, 54, 64) hr(fx - 3, fy - 10, 6, 6, 232, 232, 238); hr(fx + 1, fy - 10, 2, 6, 176, 176, 192) px(floor(fx), fy - 8, 90, 96, 112) hr(fx - 3, fy - 15, 6, 5, 238, 238, 244); hr(fx + 2, fy - 15, 1, 5, 184, 184, 198) hr(fx, fy - 14, 3, 3, 214, 164, 60); hr(fx, fy - 14, 1, 1, 255, 232, 160) if arm == 0 then hr(fx + 2, fy - 10, 2, 5, 222, 222, 230); hr(fx + 2, fy - 6, 2, 1, 70, 66, 76) elseif arm == 1 then hr(fx + 2, fy - 12, 2, 4, 222, 222, 230); hr(fx + 3, fy - 13, 2, 1, 70, 66, 76) else hr(fx + 2, fy - 8, 2, 4, 222, 222, 230); hr(fx + 3, fy - 5, 2, 1, 70, 66, 76) end end local function flag(xp, yb, sway) local top = yb - 17 hr(xp, yb - 8, 1, 8, 206, 206, 214) hr(xp + sway * 0.4, top + 8, 1, 1, 206, 206, 214) local xs = xp + sway hr(xs, top, 1, 8, 220, 220, 228) for i = 0, 5 do local red = i % 2 == 0 hr(xs + 1, top + 1 + i, 8, 1, red and 205 or 238, red and 40 or 238, red and 54 or 244) end hr(xs + 1, top + 1, 4, 3, 40, 58, 150) px(floor(xs + 2), top + 1, 240, 240, 250); px(floor(xs + 4), top + 2, 240, 240, 250) end local CX = X0 + 16 local function pad_y(Tf) return Tf + 28 end function render() if not BG then build_step(); if not BG then gfx.clear(0, 0, 0); return end end gfx.blit_rgb(BG, 0, 0, 64, 64) local u = frame % L local Tf if u < TD then Tf = T0 + (TREST - T0) * (1 - (1 - u / TD) ^ 2.2) else local ut = u - TD Tf = TREST + (ut < 60 and 2.2 * exp(-ut / 9) * cos(0.5 * ut) or 0) end local h = max(0, GY - pad_y(Tf)) local engine = u < TD + 6 and (u < TD and 1 or 1 - (u - TD) / 6) or 0 blend_rect(X0 + 4 + floor(h * 0.4), 55, 28, 3, 0, 0, 0, 0.45 * (1 - h / 80)) if engine > 0 then local g = clamp(1 - h / 36, 0, 1) * engine * 0.3 if g > 0.02 then for k = 0, 3 do local w = k == 0 and 8 or k == 1 and 12 or k == 2 and 9 or 5 blend_rect(CX - w, 54 + k * 2, 2 * w, 2, 255, 196, 120, g) end end end local iy = floor(Tf) local q = floor((Tf - iy) * 4 + 0.5) if q == 4 then iy, q = iy + 1, 0 end if not RAS[0] then RAS[0] = shift_y(0); built = true end if not RAS[q] and not built then RAS[q] = shift_y(q / 4); built = true end local R = RAS[q] if not R then R = RAS[0]; iy = floor(Tf + 0.5) end gfx.blit_rgb(R, X0, iy, 32, 30, KEY[1], KEY[2], KEY[3]) if u >= TD then for _, fx in ipairs({ 1, 30, 9, 22 }) do rect(X0 + fx - 2, GY - 0, 5, 1, 156, 156, 162) end end if u >= 230 then rect(X0 + 15, TREST + 9, 2, 3, 16, 18, 26) end if engine > 0 then local len = floor((14 - 7 * (u / TD) ^ 2) * engine + 0.5) + (hash32(frame * 31) & 1) local fy = iy + 26 for r = 0, len - 1 do local w = floor(1 + r * 0.25) local k = r / len local cr, cg, cb = 255, 252, 230 if r >= 8 then cr, cg, cb = 232, 72, 22 elseif r >= 4 then cr, cg, cb = 255, 146, 40 elseif r >= 2 then cr, cg, cb = 255, 226, 130 end blend_rect(CX - w, fy + r, 2 * w, 1, cr, cg, cb, k < 0.75 and 0.95 or 0.95 * (1 - k) * 4) end end dust(u, CX) local fx, fy, legs, arm, pole, carry = 0, 0, 0, 0, 0, false if u >= 250 and u < 600 then local porchY = TREST + 13 local XF = 46 if u < 270 then local s = (u - 250) / 20 fx, fy = X0 + 15.5 + 10 * s, porchY; legs = (u // 5) % 2 == 0 and 1 or 2 elseif u < 310 then local s = (u - 270) / 40 fy = porchY + (GY - porchY) * s fx = X0 + 25.5 + (fy - porchY) * 0.45 legs = (u // 6) % 2 == 0 and 1 or 2 elseif u < 370 then local s = (u - 310) / 60 local ph = ((u - 310) % 18) / 18 fx, fy = X0 + 31.5 + (XF - X0 - 31.5) * s, GY - floor(4 * ph * (1 - ph) * 2 + 0.5) legs = (ph > 0.15 and ph < 0.85) and 1 or 0 carry = true else fx, fy = XF, GY if u < 400 then carry = true; pole = 1 end if u >= 440 and u < 500 then arm = 1 end if u >= 400 then arm = (u >= 440 and u < 500) and 1 or 2 end end if carry then arm = 1 end if carry then local yb = fy - 4 if pole == 1 then yb = fy - 4 + (u - 370) / 30 * 4 end flag(fx + 4.5, yb, 0) end astronaut(fx, fy, legs, arm) end if u >= 400 and u < 600 then local t = u - 400 flag(46 + 4.5, GY, 1.4 * exp(-t / 22) * sin(0.4 * t)) end local fade = u < 20 and 1 - u / 20 or u >= 560 and (u - 560) / 40 or 0 if fade > 0.02 then blend_rect(0, 0, 64, 64, 0, 0, 0, min(1, fade)) end end