info = { type = "image", title = "Two suns cliff", category = "space", author = "BRRRO", version = "1.0", license = "MIT", desc = "Astronaut watches two suns set over a cliff", api = 33, } local ltr = ({ sk = { ["Dust"] = "Prach", ["Ship"] = "Loď", ["Second sun"] = "Druhé slnko", ["Sunset speed"] = "Rýchlosť západu" }, cs = { ["Dust"] = "Prach", ["Ship"] = "Loď", ["Second sun"] = "Druhé slunce", ["Sunset speed"] = "Rychlost západu" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "dust", label = LT("Dust"), type = "int", min = 0, max = 5, default = 3 }, { key = "ship", label = LT("Ship"), type = "bool", default = true }, { key = "sun2", label = LT("Second sun"), type = "bool", default = true }, { key = "speed", label = LT("Sunset speed"), type = "int", min = 1, max = 5, default = 2 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, blit_indexed = gfx.blit_rgb, gfx.snapshot, gfx.blit_indexed local floor, ceil, sin, cos, sqrt = math.floor, math.ceil, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local KEY = { 255, 0, 255 } local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function frect(x, y, w, h, r, g, b, a) a = a or 1 local x0, xe = floor(x), x + w local x1 = floor(xe) if x1 == x0 then blend_rect(x0, y, 1, h, r, g, b, w * a) return end local f = x - x0 if f > 0.02 then blend_rect(x0, y, 1, h, r, g, b, (1 - f) * a); x0 = x0 + 1 end if x1 > x0 then if a < 1 then blend_rect(x0, y, x1 - x0, h, r, g, b, a) else rect(x0, y, x1 - x0, h, r, g, b) end end if xe - x1 > 0.02 then blend_rect(x1, y, 1, h, r, g, b, (xe - x1) * a) end end local function disc(cx, cy, r, R, G, B) for y = floor(cy - r), floor(cy + r) do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q); frect(cx - hw, y, 2 * hw, 1, R, G, B) end end end local function halo(cx, cy, r, R, G, B, a) cx, cy = floor(cx + 0.5), floor(cy + 0.5) for dy = -r, r do local hw = floor(sqrt(r * r + 0.5 - dy * dy)) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, R, G, B, a) end end local SKY_T, SKY_H = { 92, 46, 56 }, { 246, 176, 110 } local EDGE = {} for y = 0, H - 1 do EDGE[y] = 52 + floor(1.5 * sin(y * 0.7) + 0.5) end local SKY, FG local function build() for y = 0, 41 do rect(0, y, W, 1, mix(SKY_T, SKY_H, (y / 41) ^ 1.2)) end SKY = snapshot(0, 0, W, H) rect(0, 0, W, H, KEY[1], KEY[2], KEY[3]) for x = 0, W - 1 do local top = 36 + floor(1.5 * sin(x * 0.35) + 1.2 * sin(x * 0.8 + 2) + 0.5) rect(x, top, 1, 46 - top, 214, 132, 102) end for _, m in ipairs({ { 0, 9, 38 }, { 13, 20, 40 }, { 36, 44, 39 }, { 57, 63, 37 } }) do rect(m[1], m[3], m[2] - m[1] + 1, 46 - m[3], 188, 110, 90) rect(m[1], m[3], m[2] - m[1] + 1, 1, 224, 146, 112) end for y = 42, H - 1 do rect(EDGE[y], y, W - EDGE[y], 1, mix({ 206, 128, 100 }, { 92, 48, 56 }, (y - 42) / 21)) end rect(57, 49, 7, 15, 150, 84, 76); rect(57, 49, 7, 1, 198, 116, 92) rect(60, 58, 4, 6, 124, 66, 64) for y = 42, H - 1 do rect(0, y, EDGE[y], 1, mix({ 228, 130, 72 }, { 176, 82, 42 }, (y - 42) / 21)) px(EDGE[y] - 1, y, 120, 56, 44) rect(EDGE[y], y, 1, 1, 70, 34, 38) end for k = 1, 14 do local h = hash32(k * 0x9E3779B1) local x, y = umod(h, 44), 45 + umod(h >> 8, 17) px(x, y, 246, 160, 98); px(x + 1, y, 130, 62, 40); px(x + 2, y, 130, 62, 40) end for k = 1, 6 do local h = hash32(k * 0x85EBCA6B) rect(umod(h, 40), 44 + umod(h >> 8, 19), 6 + umod(h >> 16, 6), 1, 206, 108, 58) end FG = snapshot(0, 0, W, H) end local STAND = { "..36666..", ".3666661.", ".3666661.", ".3666661.", "..66661..", "..22221..", ".32444421", ".32444421", ".32444421", ".32444421", ".3222221.", "..22222..", "..22.22..", "..22.22..", ".555.555.", } local function pose(rows, edit) local t = {} for i, r in ipairs(rows) do t[i] = edit[i] and (r:sub(1, 8) .. edit[i]) or r end return (table.concat(t):gsub("%.", "\0"):gsub("%d", function(d) return string.char(tonumber(d)) end)) end local SIT, POINT local APAL = { 0, 0, 0, 124, 96, 102, 196, 170, 166, 255, 226, 180, 140, 112, 118, 84, 56, 60, 232, 210, 202 } local AX, AY = 22, 40 local FX, FY = AX + 4, AY + 14 local DUST = {} for i = 1, 20 do local h = hash32(i * 0xC2B2AE35) DUST[i] = { x = umod(h, 50), y = 46 + umod(h >> 6, 17), vx = 6 + umod(h >> 12, 5), rise = 1.5 + umod(h >> 16, 20) / 10, life = 5 + umod(h >> 20, 5), off = umod(h >> 24, 100) / 10 } end local function bar(x, w, y0, f, a) blend_rect(x, y0, w, 1, 74, 28, 30, a * (1 - f)) blend_rect(x, y0 + 1, w, 2, 74, 28, 30, a) if f > 0.01 then blend_rect(x, y0 + 3, w, 1, 74, 28, 30, a * f) end end local function shadow(len, dir, slope, a) local n, k = ceil(len), 0 while k < n do local key = floor((FY - 1.5 + slope * k) * 4) local e = k while e + 1 < n - 1 and floor((FY - 1.5 + slope * (e + 1)) * 4) == key do e = e + 1 end bar(dir > 0 and FX + k or FX - e, e - k + 1, key // 4, (key % 4) / 4, k == n - 1 and a * (len - k) or a) k = e + 1 end end local function ship(x, y) frect(x, y, 10, 2, 96, 60, 68); frect(x + 2, y - 1, 4, 1, 210, 150, 120) frect(x + 7, y + 2, 3, 1, 70, 40, 52) local fl = (frame // 3) % 2 == 0 and 255 or 220 frect(x + 10, y, 1, 2, fl, 220, 150) end function render() if not SKY then build() SIT = pose(STAND, {}) POINT = pose(STAND, { [2] = "3", [3] = "2", [4] = "2", [5] = "2", [6] = "2", [7] = "2", [8] = ".", [9] = ".", [10] = "." }) end local t = frame / FPS local P = 300 / (cfg.speed or 2) blit_rgb(SKY, 0, 0, W, H) local s1 = 0.5 - 0.5 * cos(TAU * t / P + 1.1) local y1 = 15 + 19 * s1 local x1 = 12 local two = cfg.sun2 ~= false local s2 = 0.5 - 0.5 * cos(TAU * t / (P * 1.4) + 4.2) local y2 = 10 + 12 * s2 halo(x1, y1, 9, 255, 214, 160, 0.2); halo(x1, y1, 7, 255, 220, 170, 0.28) disc(x1, y1, 5, mix({ 255, 248, 222 }, { 255, 176, 104 }, s1)) if two then halo(44, y2, 6, 255, 214, 160, 0.22) disc(44, y2, 3, mix({ 255, 238, 206 }, { 255, 170, 110 }, s2)) end local ship_x if cfg.ship ~= false then local u = (t % 30) * 7 ship_x = 72 - u if ship_x > -12 then ship(ship_x, floor(27 + 2 * sin(t * 0.8))) end end blit_rgb(FG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) gfx.clip(0, 43, 50, 21) shadow(6 + 17 * s1, 1, 0.35, 0.55) if two then shadow(4 + 9 * s2, -1, 0.5, 0.3) end gfx.clip() local pointing = ship_x and ship_x > 6 and ship_x < 58 blit_indexed(pointing and POINT or SIT, APAL, AX, AY, 9, 15, 9, 15, true) for i = 1, 4 * (cfg.dust or 3) do local d = DUST[i] local tau = (t + d.off) % d.life local u = tau / d.life local x = d.x + d.vx * (tau + 0.6 * (1 - cos(0.5 * tau))) local y = floor(d.y - d.rise * tau + 1.2 * sin(tau * 1.7 + d.off) + 0.5) local a = 0.75 * sin(3.1416 * u) local ix = floor(x) local f = x - ix blend(ix, y, 255, 214, 160, a * (1 - f)) blend(ix + 1, y, 255, 214, 160, a * f) end end