info = { type = "image", title = "Ship window", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Spaceship viewport with passing planet, comet", api = 33, } local tr = ({ sk = { ["Star speed"] = "Rýchlosť hviezd", ["Planet"] = "Planéta", ["Lights"] = "Kontrolky", ["Cyan"] = "Azúrová", ["Green"] = "Zelená", ["Amber"] = "Jantárová" }, cs = { ["Star speed"] = "Rychlost hvězd", ["Planet"] = "Planeta", ["Lights"] = "Kontrolky", ["Cyan"] = "Azurová", ["Green"] = "Zelená", ["Amber"] = "Jantarová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Star speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "planet", label = T("Planet"), type = "bool", default = true }, { key = "lights", label = T("Lights"), type = "int", default = "cyan", opts = { keys = { "cyan", "green", "amber" }, T("Cyan"), T("Green"), T("Amber") } }, } local rect, blend_rect, px, blend = gfx.rect, gfx.blend_rect, gfx.px, gfx.blend local floor, ceil, sin, sqrt = math.floor, math.ceil, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local KEY = { 255, 0, 255 } local WX, WY, WW, WH = 6, 6, 52, 34 local PANES = { { 6, 6 }, { 24, 6 }, { 42, 6 }, { 6, 24 }, { 24, 24 }, { 42, 24 } } local WALL, FRAME = { 36, 44, 72 }, { 64, 78, 116 } local LIP_L, LIP_D = { 122, 146, 200 }, { 24, 30, 52 } local CONS, PANEL = { 46, 56, 90 }, { 30, 38, 64 } local VOID = { 3, 4, 11 } local LX = { 5, 9, 13, 17 } local LY = { 49, 53, 57 } local BG = nil local function build_bg() rect(0, 0, W, H, KEY[1], KEY[2], KEY[3]) rect(0, 0, W, H, WALL[1], WALL[2], WALL[3]) for y = 6, 40, 14 do px(1, y, LIP_L[1], LIP_L[2], LIP_L[3]); px(62, y, LIP_L[1], LIP_L[2], LIP_L[3]) end rect(3, 3, 58, 40, FRAME[1], FRAME[2], FRAME[3]) rect(3, 3, 58, 1, 90, 108, 156) rect(3, 42, 58, 1, LIP_D[1], LIP_D[2], LIP_D[3]) for i = 1, 6 do local x, y = PANES[i][1], PANES[i][2] rect(x, y, 16, 16, KEY[1], KEY[2], KEY[3]) rect(x - 1, y - 1, 17, 1, LIP_L[1], LIP_L[2], LIP_L[3]) rect(x - 1, y, 1, 16, LIP_L[1], LIP_L[2], LIP_L[3]) rect(x, y + 16, 17, 1, LIP_D[1], LIP_D[2], LIP_D[3]) rect(x + 16, y, 1, 16, LIP_D[1], LIP_D[2], LIP_D[3]) end rect(0, 43, W, 2, 84, 102, 150) rect(0, 45, W, 19, CONS[1], CONS[2], CONS[3]) rect(2, 47, 21, 15, PANEL[1], PANEL[2], PANEL[3]) rect(41, 47, 21, 15, PANEL[1], PANEL[2], PANEL[3]) rect(26, 47, 12, 12, 18, 24, 44) rect(27, 48, 10, 10, 4, 12, 22) for r = 1, 3 do for c = 1, 4 do rect(LX[c] - 1, LY[r] - 1, 5, 4, 20, 26, 46) end end rect(43, 48, 7, 7, 20, 26, 46) rect(52, 48, 7, 7, 20, 26, 46) rect(43, 57, 16, 3, 20, 26, 46) BG = gfx.snapshot(0, 0, W, H) end local LAYERS = { { 30, 1.4, { 150, 160, 220 }, 0.55, 0 }, { 16, 5, { 200, 210, 245 }, 0.8, 0 }, { 7, 18, { 235, 242, 255 }, 1, 3 } } local STAR = {} for li = 1, 3 do STAR[li] = {} for i = 1, LAYERS[li][1] do local h = hash32((li * 64 + i) * 0x9E3779B1) STAR[li][i] = { x = umod(h, WW), y = WY + umod(h >> 8, WH), ph = umod(h >> 16, 628) / 100 } end end local function stars(tt, t) for li = 1, 3 do local L = LAYERS[li] local c = L[3] for i = 1, L[1] do local s = STAR[li][i] local x = (s.x - tt * L[2]) % WW local a = L[4] if li == 1 then a = a * (0.65 + 0.35 * sin(t * 1.2 + s.ph)) end local ix = floor(x) local f = x - ix if L[5] > 0 then rect(WX + ix, s.y, L[5], 1, 90, 100, 150) px(WX + ix, s.y, c[1], c[2], c[3]) else blend(WX + ix, s.y, c[1], c[2], c[3], a * (1 - f)) blend(WX + (ix + 1) % WW, s.y, c[1], c[2], c[3], a * f) end end end end local R, RX, RY, RXI, RYI = 10, 17, 4, 12, 2.8 local PCY = 22 local TONE = { { 214, 164, 112 }, { 188, 132, 92 }, { 228, 186, 136 }, { 176, 120, 86 } } local PL = {} for b = 1, 4 do local c = TONE[b] PL[b] = { { floor(c[1] * 0.3 + 4), floor(c[2] * 0.3 + 5), floor(c[3] * 0.3 + 12) }, { floor(c[1] * 0.62), floor(c[2] * 0.62), floor(c[3] * 0.64) }, c } end local function planet(cx) for c = floor(cx - RX), ceil(cx + RX) do local dx = c + 0.5 - cx local adx = dx < 0 and -dx or dx if adx < RX then local yo = floor(RY * sqrt(1 - (dx / RX) ^ 2) + 0.5) local yi = adx < RXI and floor(RYI * sqrt(1 - (dx / RXI) ^ 2) + 0.5) or 0 local kb = yi > 0 and yi or 1 local ad = adx < R and floor(sqrt(R * R - dx * dx)) + 1 or 0 if ad > kb then kb = ad end if yo >= kb then rect(c, PCY - yo, 1, yo - kb + 1, 128, 116, 96) end end end for dy = -R, R - 1 do local a = sqrt(R * R - (dy + 0.5) * (dy + 0.5)) local xl, xr = cx - a, cx + a local il, ir = ceil(xl), floor(xr) local y = PCY + dy local tone = PL[(dy + R) // 3 % 4 + 1] local b1, b2 = floor(cx - 0.62 * a + 0.5), floor(cx - 0.2 * a + 0.5) if b1 < il then b1 = il end if b2 > ir then b2 = ir end if b2 < b1 then b2 = b1 end if b1 > il then rect(il, y, b1 - il, 1, tone[1][1], tone[1][2], tone[1][3]) end if b2 > b1 then rect(b1, y, b2 - b1, 1, tone[2][1], tone[2][2], tone[2][3]) end if ir > b2 then rect(b2, y, ir - b2, 1, tone[3][1], tone[3][2], tone[3][3]) end if il - xl > 0.05 then blend(il - 1, y, tone[1][1], tone[1][2], tone[1][3], il - xl) end if xr - ir > 0.05 then blend(ir, y, tone[3][1], tone[3][2], tone[3][3], xr - ir) end end for c = floor(cx - RX), ceil(cx + RX) do local dx = c + 0.5 - cx local adx = dx < 0 and -dx or dx if adx < RX then local yo = floor(RY * sqrt(1 - (dx / RX) ^ 2) + 0.5) local yi = adx < RXI and floor(RYI * sqrt(1 - (dx / RXI) ^ 2) + 0.5) or 0 if yo >= yi then rect(c, PCY + yi, 1, yo - yi + 1, 214, 198, 162) end end end end local function comet(u) local hx, hy = 62 - 66 * u, 4 + 28 * u local ix, iy = floor(hx), floor(hy) local fx, fy = hx - ix, hy - iy for k = 14, 1, -1 do local x, y = hx + 0.92 * k * 1.3, hy - 0.39 * k * 1.3 local a = (1 - k / 15) ^ 1.2 blend_rect(floor(x + 0.5), floor(y + 0.5), 2, 2, 150, 205, 255, 0.7 * a) end blend_rect(ix - 2, iy - 2, 6, 6, 120, 180, 255, 0.2) blend_rect(ix - 1, iy - 1, 4, 4, 160, 215, 255, 0.35) blend(ix, iy, 240, 252, 255, (1 - fx) * (1 - fy)) blend(ix + 1, iy, 240, 252, 255, fx * (1 - fy)) blend(ix, iy + 1, 240, 252, 255, (1 - fx) * fy) blend(ix + 1, iy + 1, 240, 252, 255, fx * fy) blend_rect(ix, iy, 2, 2, 240, 252, 255, 0.6) end local LCOL = { cyan = { 80, 220, 255 }, green = { 110, 255, 150 }, amber = { 255, 190, 70 } } local LC = LCOL.cyan local SF = 1 function on_settings() LC = LCOL[cfg.lights or "cyan"] or LCOL.cyan SF = ({ 0.5, 0.75, 1, 1.5, 2.2 })[cfg.speed or 3] or 1 end local function cabin(t) for r = 1, 3 do for c = 1, 4 do local i = (r - 1) * 4 + c local h = hash32(i * 0x85EBCA6B) local per = i % 4 == 0 and 1000000 or (i % 2 == 0 and 14 + umod(h, 12) or 40 + umod(h, 70)) local on = (frame + umod(h >> 8, 90)) % per < per * 0.55 local x, y = LX[c], LY[r] if on then blend_rect(x - 1, y - 1, 5, 4, LC[1], LC[2], LC[3], 0.22) rect(x, y, 3, 2, LC[1], LC[2], LC[3]) else rect(x, y, 3, 2, LC[1] // 5, LC[2] // 5, LC[3] // 5) end end end for i = 0, 2 do local h = 1 + floor(7 * (0.5 + 0.5 * sin(t * (1.3 + 0.7 * i) + 2 * i))) rect(28 + 3 * i, 58 - h, 2, h, LC[1], LC[2], LC[3]) end rect(44, 49, 5, 5, 62, 74, 110) rect(44, 49, 1, 1, 20, 26, 46); rect(48, 49, 1, 1, 20, 26, 46) rect(44, 53, 1, 1, 20, 26, 46); rect(48, 53, 1, 1, 20, 26, 46) local ang = sin(t * 0.5) * 1.2 px(46 + floor(2 * sin(ang) + 0.5), 51 - floor(2 * math.cos(ang) + 0.5), LC[1], LC[2], LC[3]) local alarm = (t % 1.6) < 0.8 rect(53, 49, 5, 5, alarm and 255 or 90, alarm and 60 or 24, alarm and 50 or 28) if alarm then blend_rect(52, 48, 7, 7, 255, 60, 50, 0.2) end rect(44, 58, 14, 1, 24, 30, 52) rect(44 + floor(11 * (0.5 + 0.5 * sin(t * 0.3))), 57, 3, 3, 150, 170, 210) end function render() if not BG then build_bg() end local t = frame / FPS local tt = t * SF rect(WX, WY, WW, WH, VOID[1], VOID[2], VOID[3]) stars(tt, t) if cfg.planet ~= false then local u = tt % 58 if u < 22 then planet(76 - 4 * u) end end local cu = (tt % 29 - 20) / 3.2 if cu >= 0 and cu < 1 then comet(cu) end gfx.blit_rgb(BG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) cabin(t) end