info = { type = "image", title = "Rocket rider", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Astronaut rides a rocket past planets, waving", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Rocket colour"] = "Farba rakety", ["Planets"] = "Planéty" }, cs = { ["Speed"] = "Rychlost", ["Rocket colour"] = "Barva rakety", ["Planets"] = "Planety" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "hull", label = T("Rocket colour"), type = "color", default = "#E8384F" }, { key = "planets", label = T("Planets"), type = "bool", default = true }, } local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local blit, bmask = gfx.blit_indexed, gfx.blend_mask local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.283185 local SR, SG, SB = 16, 18, 54 local OR, OG, OB = 30, 26, 62 local function pack(w, h, get) local rows, row = {}, {} for y = 0, h - 1 do for x = 0, w - 1 do row[x + 1] = get(x, y) end rows[y + 1] = string.char(table.unpack(row, 1, w)) end local cells = table.concat(rows) return { cells = cells, mask = (cells:gsub("[\1-\255]", "\255")), w = w, h = h } end local function art(rows, map) local t = {} for i = 1, #rows do t[i] = (rows[i]:gsub(".", map)) end local cells = table.concat(t) return { cells = cells, mask = (cells:gsub("[\1-\255]", "\255")), w = #rows[1], h = #rows } end local function put(s, pal, fx, fy, r, g, b) local ix, iy = floor(fx), floor(fy) local ax, ay = fx - ix, fy - iy if ax > 0.04 then bmask(s.mask, r, g, b, ix + 1, iy, s.w, s.h, s.w, s.h, ax) end if ay > 0.04 then bmask(s.mask, r, g, b, ix, iy + 1, s.w, s.h, s.w, s.h, ay) end blit(s.cells, pal, ix, iy, s.w, s.h, s.w, s.h, true) return ix, iy end local RW, RH, RC = 46, 19, 9 local RX, RY = 12, 30 local IN = {} for x = 0, RW - 1 do local hh = x <= 1 and 3 or (x <= 3 and 2 or (x <= 37 and 5 or floor(5.4 * sqrt((46 - x) / 8.5)))) local e = (x - 4) // 2 local fin = x >= 4 and x <= 11 for y = 0, RH - 1 do IN[y * RW + x] = abs(y - RC) <= hh or (fin and ((y >= e and y <= 3) or (y <= RH - 1 - e and y >= RH - 4))) end end local ROCKET = pack(RW, RH, function(x, y) local i = y * RW + x if not IN[i] then return 0 end if x == 0 or x == RW - 1 or not (IN[i - 1] and IN[i + 1] and IN[i - RW] and IN[i + RW]) then return 1 end local dy = y - RC if x <= 3 then return 8 end if abs(dy) > 5 then return dy < 0 and 4 or 5 end local wx = x - 33 local d2 = wx * wx + dy * dy if d2 <= 2 then return (wx == -1 and dy == -1) and 7 or 6 end if d2 <= 6 then return 8 end if x >= 38 or (x >= 14 and x <= 16) then return dy >= 2 and 5 or 4 end if dy <= -3 then return 9 elseif dy >= 3 then return 3 end return 2 end) local ASTRO = art({ "....ooooo.....", "...owwwwwo....", "..owwwvvvvo...", "..owwvvvhvo...", "..owwvvvvvo...", "..owwwvvvvo...", "...owwwwwo....", "..pooooooo....", ".ppowwwwwwo...", ".ppowwrrwwo...", ".ppowwwwwwo...", ".ppowwwwwso...", "..powwwwwwwwwo", "...osswwwwwwwo", "....oooowwwwo.", "........owwo..", "........owwo..", "........osso..", ".......oooooo.", }, { ["."] = "\0", o = "\1", w = "\10", s = "\11", v = "\12", h = "\13", p = "\14", r = "\4" }) local AX, AY = 16, -10 local SHX, SHY = 10, 9 local PAL = {} local function apply(c) local s = { 0, 0, 0, OR, OG, OB, 226, 228, 240, 150, 156, 190, c.r, c.g, c.b, c.r * 62 // 100, c.g * 62 // 100, c.b * 62 // 100, 64, 150, 232, 214, 242, 255, 112, 116, 140, 255, 255, 255, 242, 242, 250, 166, 172, 204, 244, 178, 52, 255, 238, 170, 122, 128, 158 } for i = 1, #s do PAL[i] = s[i] end end apply({ r = 232, g = 56, b = 79 }) local PALP = { 0, 0, 0, 226, 150, 98, 196, 112, 78, 150, 82, 72, 0, 0, 0, 238, 214, 160, 176, 156, 126, 176, 186, 222, 126, 134, 182 } local PLANET, MOON local function build_planets() PLANET = pack(23, 15, function(x, y) local dx, dy = x - 11, y - 7 local e = dx * dx / 110.25 + dy * dy / 6.76 local r2 = dx * dx + dy * dy if e > 0.72 and e < 1.25 and (dy > 0 or r2 > 40) then return dy >= 0 and 5 or 6 end if r2 <= 40 then if r2 > 26 and dx + dy > 0 then return 3 end if (dy + 8) % 4 == 0 then return 2 end return 1 end return 0 end) MOON = pack(9, 9, function(x, y) local dx, dy = x - 4, y - 4 local r2 = dx * dx + dy * dy if r2 > 13 then return 0 end if (r2 > 6 and dx + dy > 1) or (dx == -1 and dy == -1) or (dx == 1 and dy == 0) then return 8 end return 7 end) end local PX0, MX0 = 67, 26 local NS = 26 local STX, STY, STL = {}, {}, {} for i = 1, NS do local h = hash32(i * 0x9E3779B1) STX[i], STY[i] = umod(h, 72), umod(h >> 8, H) STL[i] = i <= 14 and 1 or (i <= 22 and 2 or 3) end local LV = { 0.16, 0.5, 1.7 } local function stars(t, m) for i = 1, NS do local l = STL[i] local x = (STX[i] - t * LV[l] * m) % 72 - 4 local ix = floor(x) local f = x - ix local y = STY[i] if l == 3 then rect(ix, y, 1, 1, 230, 236, 255) blend_rect(ix + 1, y, 4, 1, 150, 160, 220, 0.5) elseif l == 2 then blend(ix, y, 222, 226, 255, 1 - f); blend(ix + 1, y, 222, 226, 255, f) else blend(ix, y, 130, 138, 200, 1 - f); blend(ix + 1, y, 130, 138, 200, f) end end end local function flame(nx, cy, t, sp) local seed = t // 2 local L0 = 5 + 1.6 * sp for d = -3, 3 do local ad = d < 0 and -d or d local j = umod(hash32(seed * 0x9E3779B1 + d * 0x85EBCA6B), 4) local L = floor(L0 * (1 - ad * 0.21) + j) if L > 0 then local y = cy + d rect(nx - L + 1, y, L, 1, 255, 84, 40) local m = L * 2 // 3 if m > 0 then rect(nx - m + 1, y, m, 1, 255, 178, 48) end local c = L // 3 if ad <= 1 and c > 0 then rect(nx - c + 1, y, c, 1, 255, 246, 210) end end end for k = 0, 2 do local a = (t * 2 + k * 9) % 18 local h = hash32((t + k * 9) // 18 * 0x27D4EB2F + k) blend(floor(nx - L0 - a), cy - 2 + umod(h, 5), 255, 150, 60, 1 - a / 18) end end local REST, UP = 0.75, -1.05 local function smooth(e) return e * e * (3 - 2 * e) end local function arm_angle(t) local p = t % 120 if p < 12 then return REST + (UP - REST) * smooth(p / 12) end if p < 60 then local q = p - 12 return UP + 0.42 * sin(TAU * q / 16) * (1 - q / 80) end if p < 76 then return UP + (REST - UP) * smooth((p - 60) / 16) end return REST end local AXS, AYS = {}, {} local function arm(sx, sy, ang) local c, s = cos(ang), sin(ang) for k = 1, 5 do AXS[k], AYS[k] = floor(sx + c * k * 1.3), floor(sy + s * k * 1.3) end for k = 2, 5 do rect(AXS[k] - 1, AYS[k] - 1, 4, 4, OR, OG, OB) end for k = 1, 4 do rect(AXS[k], AYS[k], 2, 2, 242, 242, 250) end rect(AXS[5], AYS[5], 2, 2, 244, 178, 52) end local PLANETS = true function on_settings() apply(cfg.hull or { r = 232, g = 56, b = 79 }) PLANETS = cfg.planets ~= false end function render() local t = frame * STEP_HZ // FPS local sp = cfg.speed or 3 local m = sp / 3 gfx.clear(SR, SG, SB) stars(t, m) if PLANETS then if not PLANET then build_planets() end put(MOON, PALP, (MX0 - t * 0.16 * m) % (W + 14) - 10, 53, 176, 186, 222) put(PLANET, PALP, (PX0 - t * 0.09 * m) % (W + 30) - 26, 4, 226, 150, 98) end local fy = RY + 1.4 * sin(TAU * t / 96) flame(RX - 1, floor(fy) + RC, t, sp) local ix, iy = put(ROCKET, PAL, RX, fy, OR, OG, OB) put(ASTRO, PAL, RX + AX, fy + AY, OR, OG, OB) arm(ix + AX + SHX, iy + AY + SHY, arm_angle(t)) end