info = { type = "image", title = "Airplane window", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Wing and drifting clouds seen through a window", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["day"] = "deň", ["sunset"] = "západ", ["night"] = "noc" }, cs = { ["Time of day"] = "Denní doba", ["day"] = "den", ["sunset"] = "západ", ["night"] = "noc" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time of day"), type = "int", default = "sunset", opts = { keys = { "day", "sunset", "night" }, T("day"), T("sunset"), T("night") } }, } local rect, px, blend_rect, blit_rgb, snapshot = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, abs, min = math.floor, math.sin, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local HZ = 34 local KR, KG, KB = 255, 0, 255 local TIMES = { day = { wall = { 214, 208, 198 }, tint = { 1, 1, 1 }, sky = { { 76, 134, 214 }, { 136, 184, 236 }, { 204, 228, 246 } }, deck = { { 206, 226, 244 }, { 160, 192, 230 } }, cloud = { far = { { 238, 246, 254 }, { 190, 210, 238 } }, mid = { { 252, 254, 255 }, { 172, 194, 228 } }, near = { { 255, 255, 255 }, { 150, 172, 214 } } }, wing = { { 206, 214, 228 }, { 104, 116, 146 } }, shadow = { 70, 90, 140 }, sun = { 255, 250, 226, 40, 22, 3 }, }, sunset = { wall = { 214, 208, 198 }, tint = { 1, 0.88, 0.78 }, sky = { { 70, 62, 140 }, { 206, 108, 140 }, { 255, 184, 112 } }, deck = { { 252, 178, 122 }, { 122, 84, 136 } }, cloud = { far = { { 255, 206, 160 }, { 226, 130, 130 } }, mid = { { 255, 214, 176 }, { 190, 106, 138 } }, near = { { 255, 206, 180 }, { 120, 74, 132 } } }, wing = { { 246, 200, 184 }, { 104, 70, 112 } }, shadow = { 90, 50, 110 }, sun = { 255, 236, 170, 26, 34, 5 }, }, night = { wall = { 214, 208, 198 }, tint = { 0.4, 0.42, 0.52 }, sky = { { 6, 8, 32 }, { 16, 18, 56 }, { 46, 40, 88 } }, deck = { { 18, 16, 40 }, { 8, 8, 24 } }, cloud = { far = { { 54, 58, 104 }, { 30, 32, 68 } }, mid = { { 68, 74, 118 }, { 36, 38, 76 } }, near = { { 84, 88, 134 }, { 104, 66, 84 } } }, wing = { { 100, 108, 148 }, { 30, 34, 64 } }, shadow = { 6, 6, 20 }, city = true, }, } local P, TP local function lerp(a, b, t) return a + (b - a) * t end local function mix(a, b, t) return floor(lerp(a[1], b[1], t) + 0.5), floor(lerp(a[2], b[2], t) + 0.5), floor(lerp(a[3], b[3], t) + 0.5) end local function grad(stops, t) local n = #stops - 1 local k = min(n - 1, floor(t * n)) return mix(stops[k + 1], stops[k + 2], t * n - k) end local function disc(cx, cy, r, c, a) for dy = -r, r do local hw = floor(sqrt(r * r + 0.6 - dy * dy) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3], a) end end local function ext(rx, ry, y) local dy = abs(y + 0.5 - 32) / ry if dy >= 1 then return 0 end return floor(rx * (1 - dy * dy * dy) ^ 0.33333 + 0.5) end local SKY, WING, FRAME local function bake_sky() for y = 9, 55 do if y < HZ then rect(15, y, 35, 1, grad(P.sky, (y - 9) / (HZ - 10))) else rect(15, y, 35, 1, grad(P.deck, (y - HZ) / (55 - HZ))) end end local s = P.sun if s then local c = { s[1], s[2], s[3] } disc(s[4], s[5], s[6] + 5, c, 0.12) disc(s[4], s[5], s[6] + 2, c, 0.25) disc(s[4], s[5], s[6], c, 1) for y = HZ, 40 do rect(15, y, 35, 1, grad(P.deck, (y - HZ) / (55 - HZ))) end end if P.city then for i = 1, 26 do local h = hash32(i * 0x9E3779B1) local b = 120 + umod(h >> 12, 130) px(15 + umod(h, 35), 11 + umod(h >> 6, 21), b * 0.8 // 1, b * 0.85 // 1, b) end for i = 1, 150 do local h = hash32(i * 0x85EBCA6B) local y = 36 + floor(umod(h >> 8, 1000) ^ 0.5 * 0.6) local k = umod(h >> 20, 4) local c = k == 0 and { 255, 214, 120 } or k == 1 and { 255, 236, 180 } or k == 2 and { 255, 170, 90 } or { 170, 200, 255 } px(15 + umod(h, 35), y, c[1], c[2], c[3]) end end SKY = snapshot(15, 9, 35, 47) end local function bake_wing() rect(16, 34, 35, 25, KR, KG, KB) local top, bot = P.wing[1], P.wing[2] for y = 40, 56 do local s = min(1, (y - 40) / 4, (56 - y) / 10) local x1 = floor(49 - 30 * s + 0.5) rect(x1, y, 50 - x1, 1, mix(top, bot, (y - 40) / 16)) end for x = 19, 49 do local s = (49 - x) / 30 px(x, floor(40 + 4 * s), top[1], top[2], top[3]) local yb = floor(53 - 10 * s) local c = P.wing px(x, yb, mix(c[1], c[2], 0.75)) end rect(18, 38, 2, 8, mix(top, bot, 0.3)) px(18, 37, mix(top, bot, 0.1)) WING = snapshot(16, 34, 35, 25) end local function bake_frame() local w, tn = P.wall, P.tint local function c(r, g, b) return floor(r * tn[1]), floor(g * tn[2]), floor(b * tn[3]) end for y = 0, 63 do local k = y / 63 rect(0, y, 64, 1, c(lerp(216, 186, k), lerp(210, 180, k), lerp(200, 172, k))) local e0 = ext(27, 32, y) if e0 > 0 then rect(32 - e0, y, 2 * e0, 1, c(120, 114, 108)) end local e1 = ext(26, 31, y) if e1 > 0 then rect(32 - e1, y, 2 * e1, 1, c(246, 242, 236)) end local e2 = ext(25, 30, y) local kb = (y - 2) / 60 if e2 > 0 then rect(32 - e2, y, 2 * e2, 1, c(lerp(232, 168, kb), lerp(228, 162, kb), lerp(220, 156, kb))) end local e3 = ext(21, 27, y) if e3 > 0 then local kv = (y - 5) / 54 rect(32 - e3, y, 2 * e3, 1, c(lerp(124, 206, kv), lerp(120, 202, kv), lerp(116, 196, kv))) end local e4 = ext(18, 24, y) if e4 > 0 then rect(32 - e4, y, 2 * e4, 1, c(68, 70, 80)) end end for x = 0, 63 do blend_rect(x, 0, 1, 64, 30, 26, 24, (x / 63) * 0.3 - 0.06 > 0 and (x / 63) * 0.3 - 0.06 or 0) end for y = 0, 63 do local e5 = ext(17, 23, y) if e5 > 0 then if y <= 13 then rect(32 - e5, y, 2 * e5, 1, c(238, 234, 226)) elseif y == 14 then rect(32 - e5, y, 2 * e5, 1, c(150, 148, 150)) else rect(32 - e5, y, 2 * e5, 1, KR, KG, KB) end end end rect(28, 15, 8, 2, c(214, 210, 204)) FRAME = snapshot(0, 0, 64, 64) end local TPL = { { { 4, 5 }, { 1, 11 }, { 0, 12 } }, { { 5, 6 }, { 2, 12 }, { 0, 16 }, { 1, 14 } }, { { 7, 8 }, { 3, 16 }, { 1, 20 }, { 0, 22 }, { 2, 18 } }, } local SPEED = { 0.12, 0.3, 0.6 } local CL = { { 1, 6, 34 }, { 1, 31, 35 }, { 1, 52, 33 }, { 2, 14, 38 }, { 2, 38, 41 }, { 2, 60, 44 }, { 3, 4, 48 }, { 3, 30, 46 }, { 3, 52, 49 }, } local RAMP = {} local function build_ramps() local names = { "far", "mid", "near" } for k = 1, 3 do RAMP[k] = {} local c, n = P.cloud[names[k]], #TPL[k] for i = 1, n do local r, g, b = mix(c[1], c[2], ((i - 1) / (n - 1)) ^ 1.2) RAMP[k][i] = { r, g, b } end end end local function draw_cloud(k, x, y) local T, R = TPL[k], RAMP[k] for i = 1, #T do local fx = x + T[i][1] local ix = floor(fx) local f = fx - ix local w, c, yy = T[i][2], R[i], y + i - 1 rect(ix + 1, yy, w - 1, 1, c[1], c[2], c[3]) blend_rect(ix, yy, 1, 1, c[1], c[2], c[3], 1 - f) blend_rect(ix + w, yy, 1, 1, c[1], c[2], c[3], f) end end local stage = 0 function on_settings() TP = cfg.time or "sunset" P = TIMES[TP] or TIMES.sunset stage = 0 build_ramps() end function render() if stage < 3 then if stage == 0 then bake_sky() elseif stage == 1 then bake_wing() else bake_frame() end stage = stage + 1 gfx.clear(0, 0, 0) gfx.ready(false) return end blit_rgb(SKY, 15, 9, 35, 47) if P.city then for i = 1, 8 do local h = hash32(i * 0x2545F491) local a = sin(frame * 0.11 + i * 1.7) if a > 0.3 then px(17 + umod(h, 31), 44 + umod(h >> 8, 10), 255, 236, 170) end end for i = 1, 6 do local h = hash32(i * 0x9E3779B1 + 77) if sin(frame * 0.09 + i * 2.9) > 0.4 then px(15 + umod(h, 35), 12 + umod(h >> 8, 20), 230, 236, 255) end end end for i = 1, #CL do local c = CL[i] draw_cloud(c[1], (c[2] - SPEED[c[1]] * frame) % 70 - 8, c[3]) end local sw = floor(1.5 * sin(frame * 0.035) + 0.5) local sh = P.shadow for r = 0, 3 do blend_rect(22 + r * 2, 51 + r, 20 - r * 3, 1, sh[1], sh[2], sh[3], 0.22) end blit_rgb(WING, 16, 34 + sw, 35, 25, KR, KG, KB) local lx, ly = 18, 37 + sw if TP ~= "day" then blend_rect(lx - 2, ly - 1, 5, 3, 255, 40, 40, 0.28) end px(lx, ly, 255, 60, 60) local fl = frame % 42 if fl < 2 or (fl >= 4 and fl < 6) then blend_rect(lx - 3, ly - 3, 7, 7, 255, 255, 255, TP == "day" and 0.25 or 0.4) rect(lx - 1, ly - 1, 3, 3, 255, 255, 255) end for i = 0, 11 do blend_rect(41 - i, 16 + i * 3, 2, 3, 255, 255, 255, 0.07) end blit_rgb(FRAME, 0, 0, 64, 64, KR, KG, KB) end