info = { type = "image", title = "Rocket countdown", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Countdown, liftoff and a camera that follows", api = 33, } local tr = ({ sk = { ["Countdown from"] = "Odpočet od", ["Smoke"] = "Dym", ["Camera follows"] = "Kamera sleduje" }, cs = { ["Countdown from"] = "Odpočet od", ["Smoke"] = "Kouř", ["Camera follows"] = "Kamera sleduje" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "from", label = T("Countdown from"), type = "int", min = 3, max = 9, default = 5 }, { key = "smoke", label = T("Smoke"), type = "int", min = 1, max = 5, default = 3 }, { key = "follow", label = T("Camera follows"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, exp, max, min = math.floor, math.sin, math.sqrt, math.exp, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GY, PAD_Y = 56, 52 local ACC = 30 local ARR, HOLD, TF, EXIT, RET, REST = 3.0, 1.6, 3.8, 1.2, 3.2, 1.2 local KEY = { 255, 0, 255 } local RX = 26 local FIN, FIN_D = { 214, 56, 48 }, { 150, 36, 38 } local BODY, BODY_S = { 238, 240, 246 }, { 168, 176, 200 } local FLAME, FLAME_C = { 255, 132, 30 }, { 255, 226, 96 } local SMK = { 214, 210, 214 } local function c3(c) return c[1], c[2], c[3] end local function smooth(s) s = max(0, min(1, s)); return s * s * (3 - 2 * s) end local KP = { { 0, 236, 170, 128 }, { 22, 150, 140, 184 }, { 70, 40, 64, 146 }, { 170, 12, 18, 66 }, { 330, 4, 4, 18 } } local function sky(a) if a <= 0 then return 236, 170, 128 end for i = 2, #KP do local q = KP[i] if a <= q[1] then local p = KP[i - 1] local t = (a - p[1]) / (q[1] - p[1]) return floor(p[2] + (q[2] - p[2]) * t + 0.5), floor(p[3] + (q[3] - p[3]) * t + 0.5), floor(p[4] + (q[4] - p[4]) * t + 0.5) end end return 4, 4, 18 end local STAR_X, STAR_A = {}, {} for i = 1, 44 do local h = hash32(i * 0x9E3779B1) STAR_X[i] = umod(h, W) STAR_A[i] = 110 + umod(h >> 8, 330) end local function draw_sky(cam) local y0, pr, pg, pb = 0, sky(56 + cam) for y = 1, H - 1 do local r, g, b = sky(56 - (y - cam)) if r ~= pr or g ~= pg or b ~= pb then rect(0, y0, W, y - y0, pr, pg, pb) y0, pr, pg, pb = y, r, g, b end end rect(0, y0, W, H - y0, pr, pg, pb) for i = 1, 44 do local a = STAR_A[i] local y = GY - a + cam if y >= 0 and y < H then local k = min(1, (a - 90 + 0) / 130) local v = floor(70 + 170 * k * (i % 3 == 0 and 1 or 0.7)) px(STAR_X[i], y, v, v, min(255, v + 15)) end end end local BASE = nil local function build_base() gfx.clear(c3(KEY)) for x = 0, W - 1, 2 do local h = 4 + floor(2.5 * sin(x * 0.13) + 2 * sin(x * 0.31 + 1) + 0.5) rect(x, GY - h, 2, h, 36, 52, 84) end rect(0, GY, W, H - GY, 46, 60, 46) rect(0, GY, W, 1, 72, 94, 64) for x = 2, W - 1, 7 do px(x, GY + 2 + umod(hash32(x), 4), 64, 84, 58) end rect(12, 53, 34, 3, 118, 118, 126); rect(12, 53, 34, 1, 154, 154, 164) rect(24, 54, 12, 2, 62, 62, 72) rect(46, 18, 1, 36, 164, 74, 52); rect(52, 18, 1, 36, 164, 74, 52) for y = 20, 48, 4 do rect(47, y, 5, 1, 130, 66, 50) for j = 0, 3 do px((y // 4) % 2 == 0 and 47 + j or 50 - j, y + 1 + j, 112, 58, 46) end end rect(45, 16, 9, 2, 120, 124, 136) rect(44, 54, 11, 2, 90, 92, 104) rect(4, 47, 2, 9, 70, 60, 52); rect(15, 47, 2, 9, 70, 60, 52) rect(1, 36, 20, 11, 96, 100, 116) rect(2, 37, 18, 9, 10, 10, 14) BASE = gfx.snapshot(0, 0, W, H) end local function puff(cx, cy, r, a) local x, y = floor(cx + 0.5), floor(cy + 0.5) blend_rect(x - r, y - r + 2, 2 * r + 1, 2 * r - 3, SMK[1], SMK[2], SMK[3], a) blend_rect(x - r + 2, y - r, 2 * r - 3, 2, SMK[1], SMK[2], SMK[3], a) blend_rect(x - r + 2, y + r - 1, 2 * r - 3, 2, SMK[1], SMK[2], SMK[3], a) end local GP = {} for i = 1, 24 do local h = hash32(i * 0x85EBCA6B) GP[i] = { dir = i % 2 == 1 and 1 or -1, v = 26 + umod(h, 22), dy = umod(h >> 8, 3) - 1 } end local function ground_smoke(td, n, cam) for i = 1, n do local age = td - (i - 1) * 0.07 if age > 0 and age < 11 then local p = GP[i] local k = 1 - exp(-0.45 * age) local r = floor(3 + 3 * k + age * 0.15) puff(RX + 3.5 + p.dir * (5 + p.v * k), 50 + p.dy - 3 * (1 - exp(-0.4 * age)) - age * 0.3 + cam, r, 0.5 * (1 - age / 11)) end end end local function trail(tau, cam, alpha) local lo = floor((PAD_Y + cam - H - 6) / 4) local hi = floor((PAD_Y + cam + 8) / 4) for n = max(1, lo), hi do local tn = sqrt(2 * n * 4 / ACC) local age = tau - tn if age > 0 and age < 4.5 then local dir = n % 2 == 1 and 1 or -1 local x = RX + 3.5 + dir * (age * 2.4) + (umod(hash32(n * 0x27D4EB2F), 3) - 1) puff(x, PAD_Y + 4 - 4 * n + cam, floor(3 + age * 1.1), alpha * (1 - age / 4.5)) end end end local function rocket(x, yb) rect(x - 2, yb - 7, 2, 7, c3(FIN)); rect(x - 2, yb - 7, 1, 7, c3(FIN_D)) rect(x + 8, yb - 7, 2, 7, c3(FIN)); rect(x + 9, yb - 7, 1, 7, c3(FIN_D)) rect(x, yb - 17, 8, 17, c3(BODY)); rect(x + 6, yb - 17, 2, 17, c3(BODY_S)) rect(x, yb - 9, 8, 2, c3(FIN)) rect(x + 3, yb - 14, 2, 3, 70, 150, 230); px(x + 3, yb - 14, 190, 230, 255) rect(x + 1, yb - 18, 6, 1, c3(FIN)); rect(x + 2, yb - 19, 4, 1, c3(FIN)); rect(x + 3, yb - 21, 2, 2, c3(FIN)) rect(x + 2, yb, 4, 1, 60, 62, 74) end local function flame(yb, len) rect(RX + 2, yb + 1, 4, len, c3(FLAME)) rect(RX + 1, yb + 1, 6, max(1, len // 3), c3(FLAME)) rect(RX + 3, yb + 1, 2, max(1, len * 2 // 3), c3(FLAME_C)) end local OPT = { font = "3x5", w = 18, align = "center" } local TY = {} local function board(s, r, g, b, cam) local ty = TY[s] if not ty then local top = gfx.text_ink(s, OPT) ty = 39 - top TY[s] = ty end gfx.text(2, ty + cam, s, r, g, b, OPT) end function render() if not BASE then build_base() end local N = cfg.from or 5 local ti = ARR + N local tl = ti + HOLD local te = tl + TF + EXIT local cycle = te + RET + REST local t = (frame / FPS) % cycle local tau = t - tl local follow = cfg.follow ~= false local cam = 0 if follow and t >= tl then local ht = 0.5 * ACC * min(tau, TF) ^ 2 local u = ht - 20 local c_f = max(0, 0.5 * (u + sqrt(u * u + 36)) - 0.44) cam = t < te and c_f or c_f * (1 - smooth((t - te) / RET)) end local ci = floor(cam + 0.5) draw_sky(ci) gfx.blit_rgb(BASE, 0, ci, W, H, KEY[1], KEY[2], KEY[3]) if t < tl then local e = smooth((t - (ARR - 0.8)) / 0.8) * (1 - smooth((t - ti) / 0.6)) local len = 2 + floor(8 * e + 0.5) rect(46 - len, 34 + ci, len, 2, 142, 146, 158) end if floor(t * 1.5) % 2 == 0 then rect(49, 14 + ci, 2, 2, 255, 70, 56) end if t < ARR then board("--", 110, 36, 30, ci) elseif t < ti then board("T-" .. (N - floor(t - ARR)), 255, 70, 56, ci) elseif t < tl + 1 then board("FIRE", 255, 214, 70, ci) elseif t < te then board("UP", 255, 214, 70, ci) else board("--", 110, 36, 30, ci) end local base, dx = PAD_Y, 0 if t < ARR then dx = -floor(64 * (1 - t / ARR) ^ 2 + 0.5) end if t >= tl then base = PAD_Y - floor(0.5 * ACC * tau * tau + 0.5) end local yb = base + ci local shake = (t >= ti and t < tl and frame % 2 == 0) and 1 or 0 local show = t < te if show and yb - 22 < H and yb + 16 > 0 then if dx ~= 0 then rect(RX + dx - 4, PAD_Y + 1 + ci, 16, 2, 70, 72, 84) rect(RX + dx - 2, PAD_Y + 3 + ci, 2, 1, 30, 30, 36); rect(RX + dx + 8, PAD_Y + 3 + ci, 2, 1, 30, 30, 36) end if t >= ti then local len = t < tl and 2 + floor(2 * min(1, (t - ti) / 0.6)) or 6 + floor(8 * min(1, tau / 1.2)) flame(yb, len + ((frame // 2) % 3) * (t < tl and 0 or 2)) end rocket(RX + dx + shake, yb) end if t >= ti and t < tl + 1.5 then local k = 1 - smooth((t - ti) / (HOLD + 1.5)) blend_rect(16, 54 + ci, 28, 2, 255, 150, 60, 0.45 * k) end local smoke = cfg.smoke or 3 if t >= tl and tau < TF + EXIT + 0.5 + 2 then trail(tau, ci, 0.22 + 0.07 * smoke) end if t >= ti then ground_smoke(t - ti, 6 + 3 * smoke, ci) end end