info = { type = "image", title = "Gas giant", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "A banded planet turns as its moon slips by", api = 33, } local ltr = ({ sk = { ["Planet colour"] = "Farba planéty", ["Speed"] = "Rýchlosť" }, cs = { ["Planet colour"] = "Barva planety", ["Speed"] = "Rychlost" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tone", label = LT("Planet colour"), type = "color", default = "#3CBEC8" }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local px, blend, rect = gfx.px, gfx.blend, gfx.rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI, TAU = 3.14159265, 6.28318531 local SPACE = { 8, 8, 30 } local NEB = { { 66, 30, 108 }, { 116, 50, 158 }, { 176, 98, 214 } } local STARC = { { 92, 92, 142 }, { 172, 178, 224 }, { 255, 255, 255 } } local MOONC = { { 136, 64, 46 }, { 224, 114, 58 }, { 252, 170, 88 } } local STORM = { 246, 128, 112 } local PCX, PCY, R = 34.5, 33.5, 15.5 local YC = floor(PCY) local LX, LY, LZ = -0.62, -0.50, 0.60 local byte = string.byte local WROW = {} for y = YC - 16, YC + 16 do local dy = y + 0.5 - PCY local w2 = R * R - dy * dy WROW[y] = w2 > 0 and sqrt(w2) or 0 end local RUNS = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 30, 31, 32, 33, 34, 35, 36, 38, 40, 42, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 64, 65, 195, 279, 315, 573, 833, 919, 1116 }, { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 21 }, { 2, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 18 }, { 3, 1, 2, 5, 8, 9, 10, 11, 12, 16, 18, 20 }, { 4, 1, 2, 6, 9, 10, 12, 13, 14 } } local UNZ = { ["\n"] = "" } do local c = 33 for _, r in ipairs(RUNS) do local v = string.char(r[1]) for k = 2, #r do if c == 93 then c = 94 end UNZ[string.char(c)] = v:rep(r[k]) c = c + 1 end end end local function unz(s) return (s:gsub(".", UNZ)) end local ZLS = unz([[ Nqb9wrd6xmxxod4xlywocO3xlzoaQ2xl{obQ1~ocQ0~ocQ/~wocR.wl|pdQ-wl}pdR,wl}pdR,m|qcS,m{wqdT+m{rdT+mzreT+m yxsdU+mlyseU+veV+veV+uleV,ueW,tgW-l`sgW.argX/bnlhX0kY1jZ2i[3`Pe\4_6^9[M]]) local function band(ny) if ny < -0.72 then return 3 elseif ny < -0.5 then return 1 elseif ny < -0.3 then return 2 elseif ny < -0.1 then return 1 elseif ny < 0.12 then return 3 elseif ny < 0.3 then return 2 elseif ny < 0.52 then return 1 elseif ny < 0.72 then return 2 else return 3 end end local FEAT = { { -12, 0.6, 0.50, 6 }, { -12, 3.9, 0.40, 7 }, { -9, 2.0, 0.55, 7 }, { -9, 5.1, 0.45, 6 }, { -6, 0.3, 0.70, 6 }, { -6, 3.4, 0.35, 7 }, { -3, 1.6, 0.45, 7 }, { -3, 4.6, 0.60, 6 }, { 0, 0.9, 0.40, 6 }, { 0, 3.8, 0.50, 7 }, { 3, 2.4, 0.22, 4 }, { 4, 2.4, 0.36, 4 }, { 5, 2.4, 0.36, 4 }, { 6, 2.4, 0.22, 4 }, { 4, 2.36, 0.13, 5 }, { 5, 2.36, 0.13, 5 }, { 4, 5.4, 0.45, 6 }, { 8, 0.9, 0.55, 7 }, { 8, 4.3, 0.40, 6 }, { 11, 2.9, 0.60, 6 }, { 13, 5.6, 0.50, 7 }, } local NEBGRID = unz([[ "OGPKQDOEUCS"OAQ`Q!O@P`O`Q@OcO`P?PcPAPaQBO`S!OAO!R!O#O;P"Q`T>S`OaS#O:PaQ!T!O8P`T!OaR9`PbP`O`O`R8S`V` R6PcO`TaP!O7T`P`R`O9R!Ua!O!P6O!O!Z!O:O#U!P=SaT=QdT;QaO`R!O;OfRQ%P@P$Q(O5O)O'O!Q=Q#PaO=R !PbQ:P"O!P`ObO>Qg;P`OfS7O!Ran`O`O!O8Ral`maO`Q8O"aO`nd:OaOblaO`O`;ScOaO> 8, H) local dx, dy = x + 0.5 - PCX, y + 0.5 - PCY if dx * dx + dy * dy > (R + 3) * (R + 3) then k = k + 1 STX[k], STY[k] = x, y STP[k], STR[k] = umod(h >> 16, 628) / 100.0, 0.7 + umod(h >> 4, 9) / 10.0 end end end local CROSS = { { 54, 8, 150, 125 }, { 9, 54, 190, 170 } } local FC = {} local ROT_W, MOON_W = 0, 0 local function mixw(c, a) return { floor(c[1] + (255 - c[1]) * a + 0.5), floor(c[2] + (255 - c[2]) * a + 0.5), floor(c[3] + (255 - c[3]) * a + 0.5) } end local function mul(c, a) return { floor(c[1] * a + 0.5), floor(c[2] * a + 0.5), floor(c[3] * a + 0.5) } end local function lit(c) return { [0] = mul(c, 0.36), mul(c, 0.64), c, mixw(c, 0.38) } end local BG = nil local function apply(t, sp) local c = { t.r, t.g, t.b } FC[1] = lit(c); FC[2] = lit(mixw(c, 0.30)); FC[3] = lit(mul(c, 0.80)) FC[4] = lit(STORM); FC[5] = lit(mixw(STORM, 0.45)) FC[6] = lit(mixw(c, 0.55)); FC[7] = lit(mul(c, 0.60)) ROT_W = TAU / (({ 90, 64, 46, 32, 22 })[sp] * FPS) MOON_W = TAU / (({ 30, 22, 16, 12, 9 })[sp] * FPS) BG = nil end apply({ r = 60, g = 190, b = 200 }, 3) function on_settings() local sp = cfg.speed or 3 if sp < 1 then sp = 1 elseif sp > 5 then sp = 5 end apply(cfg.tone or { r = 60, g = 190, b = 200 }, sp) end local function build_bg() rect(0, 0, W, H, SPACE[1], SPACE[2], SPACE[3]) gfx.blit_indexed(NEBGRID, NEBPAL, 0, 0, W, H, W, H, true) for y = YC - 16, YC + 16 do local dy = y + 0.5 - PCY local cols = FC[band(dy / R)] for x = floor(PCX - R) - 1, floor(PCX + R) + 1 do local lv = byte(ZLS, y * W + x + 1) - 1 if lv >= 0 then local dx = x + 0.5 - PCX local cov = R + 0.5 - sqrt(dx * dx + dy * dy) local c = cols[lv] if cov >= 1 then px(x, y, c[1], c[2], c[3]) elseif cov > 0.05 then blend(x, y, c[1], c[2], c[3], cov) end end end end BG = gfx.snapshot(0, 0, W, H) end local function feature(f, rot) local y = YC + f[1] local w = WROW[y] if not w or w < 1 then return end local d = (f[2] + rot) % TAU if d > PI then d = d - TAU end local a, b = d - f[3], d + f[3] if b <= -PI / 2 or a >= PI / 2 then return end if a < -PI / 2 then a = -PI / 2 end if b > PI / 2 then b = PI / 2 end local xa, xb = PCX + w * sin(a), PCX + w * sin(b) local base, cols = y * W + 1, FC[f[4]] for x = floor(xa), floor(xb) do local lv = byte(ZLS, base + x) - 1 if lv > 0 then local lo, hi = x, x + 1 if xa > lo then lo = xa end if xb < hi then hi = xb end local cov = hi - lo local c = cols[lv] if cov >= 0.97 then px(x, y, c[1], c[2], c[3]) elseif cov > 0.03 then blend(x, y, c[1], c[2], c[3], cov) end end end end local MA, MB, MCT, MST = 25.5, 6.5, cos(-0.32), sin(-0.32) local function moon(phi, front) local ox, oy = MA * cos(phi), MB * sin(phi) local mx = PCX + ox * MCT - oy * MST local my = PCY + ox * MST + oy * MCT local r = 2.5 + 0.35 * sin(phi) local r2 = (R + 0.3) * (R + 0.3) for y = floor(my - r - 1), floor(my + r + 1) do for x = floor(mx - r - 1), floor(mx + r + 1) do local dx, dy = x + 0.5 - mx, y + 0.5 - my local cov = r + 0.5 - sqrt(dx * dx + dy * dy) if cov > 0.05 then local ex, ey = x + 0.5 - PCX, y + 0.5 - PCY if front or ex * ex + ey * ey > r2 then local s = (LX * dx + LY * dy) / r local c = s > 0.3 and MOONC[3] or (s > -0.35 and MOONC[2] or MOONC[1]) if cov >= 1 then px(x, y, c[1], c[2], c[3]) else blend(x, y, c[1], c[2], c[3], cov) end end end end end end local STEP_HZ = 30 local function cross(c, fs) local age = (fs - c[4]) % c[3] if age >= 24 then return end local st = age // 4 local arm = (st == 2 or st == 3) and 2 or ((st == 1 or st == 4) and 1 or 0) local x, y = c[1], c[2] local m = arm > 0 and STARC[3] or STARC[2] px(x, y, m[1], m[2], m[3]) for a = 1, arm do local k = (a == 1 and arm == 2) and STARC[2] or STARC[1] if arm == 1 then k = STARC[2] end px(x - a, y, k[1], k[2], k[3]); px(x + a, y, k[1], k[2], k[3]) px(x, y - a, k[1], k[2], k[3]); px(x, y + a, k[1], k[2], k[3]) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS for k = 1, #STX do local v = sin(t * STR[k] + STP[k]) local c = v > 0.55 and STARC[3] or (v > -0.35 and STARC[2] or STARC[1]) px(STX[k], STY[k], c[1], c[2], c[3]) end for k = 1, #SHX do local c = sin(t * SHR[k] + SHP[k]) > 0.35 and NEB[3] or NEB[2] px(SHX[k], SHY[k], c[1], c[2], c[3]) end local fs = frame * STEP_HZ // FPS cross(CROSS[1], fs); cross(CROSS[2], fs) local phi = frame * MOON_W + 2.33 local front = sin(phi) > 0 if not front then moon(phi, false) end local rot = frame * ROT_W for k = 1, #FEAT do feature(FEAT[k], rot) end if front then moon(phi, true) end end