info = { type = "image", title = "Tent door at night", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lake, fireflies and stars seen from a tent", api = 44, } local tr = ({ sk = { ["Moon"] = "Mesiac" }, cs = { ["Moon"] = "Měsíc" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "moon", label = T("Moon"), type = "bool", default = true }, } local rect, px, blend_rect, blit_rgb, snapshot = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, max, min = math.floor, math.sin, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local KR, KG, KB = 255, 0, 255 local HZ = 35 local MX, MY = 38, 16 local MOON local function lerp(a, b, t) return a + (b - a) * t end local function hw(y) if y < 2 then return 0 end return floor(27 * ((y - 2) / 46) ^ 0.6) end local BG, FAB, BAG, stage = nil, nil, nil, 0 local STARS = {} local function ridge(x, k) if k == 1 then return 6 + 3 * sin(x * 0.17 + 1) + 2 * sin(x * 0.41) + 1.5 * sin(x * 0.09 + 3) end return 2 + 1.5 * sin(x * 0.23 + 2) + sin(x * 0.57) end local function bake_bg() local sky = MOON and { { 8, 10, 36 }, { 18, 26, 64 }, { 40, 62, 108 } } or { { 5, 7, 28 }, { 14, 20, 54 }, { 32, 52, 92 } } for y = 0, HZ - 1 do local t = y / (HZ - 1) local a, b, f if t < 0.55 then a, b, f = sky[1], sky[2], t / 0.55 else a, b, f = sky[2], sky[3], (t - 0.55) / 0.45 end rect(0, y, 64, 1, floor(lerp(a[1], b[1], f) + 0.5), floor(lerp(a[2], b[2], f) + 0.5), floor(lerp(a[3], b[3], f) + 0.5)) end local ns = MOON and 36 or 70 local k = 0 STARS = {} for i = 1, 200 do if k >= ns then break end local h = hash32(i * 0x9E3779B1) local x, y = umod(h, 64), umod(h >> 8, 30) local dx, dy = x - MX, y - MY if not MOON or dx * dx + dy * dy > 36 then k = k + 1 local b = 90 + umod(h >> 16, 120) if k <= 12 then STARS[#STARS + 1] = { x, y, b, umod(h >> 20, 628) / 100 } else px(x, y, b * 0.85 // 1, b * 0.9 // 1, b) end end end if not MOON then for i = 1, 40 do local h = hash32(i * 0x85EBCA6B) local u = umod(h, 100) / 100 px(floor(10 + u * 50 + umod(h >> 8, 7) - 3), floor(30 - u * 26 + umod(h >> 12, 7) - 3), 70, 82, 130) end end if MOON then rect(MX - 2, MY - 3, 5, 7, MOON[1], MOON[2], MOON[3]); rect(MX - 3, MY - 2, 7, 5, MOON[1], MOON[2], MOON[3]) rect(MX - 1, MY - 4, 3, 9, MOON[1], MOON[2], MOON[3]) px(MX - 1, MY - 1, 214, 208, 182); px(MX + 1, MY + 1, 214, 208, 182) end local lake = MOON and { { 34, 62, 110 }, { 14, 26, 62 } } or { { 20, 36, 76 }, { 7, 12, 34 } } for y = HZ, 48 do local t = (y - HZ) / (48 - HZ) rect(0, y, 64, 1, floor(lerp(lake[1][1], lake[2][1], t) + 0.5), floor(lerp(lake[1][2], lake[2][2], t) + 0.5), floor(lerp(lake[1][3], lake[2][3], t) + 0.5)) end for x = 0, 63 do local h1 = floor(ridge(x, 1) + 0.5) local h2 = floor(ridge(x + 17, 2) + 0.5) rect(x, HZ - h1 - 1, 1, h1 + 1, 12, 20, 46) rect(x, HZ - h2, 1, h2, 6, 10, 26) rect(x, HZ, 1, floor(h1 * 0.5), 12, 24, 54) if MOON and h1 >= 8 then px(x, HZ - h1 - 1, 40, 62, 104) end end for _, x in ipairs({ 14, 17, 21, 43, 46, 50 }) do local h = hash32(x * 77) rect(x, 41 + umod(h, 4), 1, 8, 5, 12, 16) rect(x + 1, 43 + umod(h >> 4, 4), 1, 6, 5, 12, 16) end BG = snapshot(0, 0, 64, 64) end local function bake_fabric() rect(0, 0, 64, 64, KR, KG, KB) for y = 0, 63 do local k = hw(y) local top = y < 49 if top then local e = 32 - k rect(0, y, e, 1, 24, 44, 52); rect(32 + k, y, e, 1, 24, 44, 52) if k > 0 then rect(e - 4, y, 4, 1, 38, 66, 76); rect(32 + k, y, 4, 1, 38, 66, 76) rect(e - 9, y, 5, 1, 31, 55, 64); rect(32 + k + 4, y, 5, 1, 31, 55, 64) local rc = MOON and { 96, 140, 150 } or { 56, 86, 96 } px(e - 1, y, rc[1], rc[2], rc[3]); px(32 + k, y, rc[1], rc[2], rc[3]) if y % 3 == 0 then px(e - 3, y, 10, 20, 26); px(32 + k + 2, y, 10, 20, 26) end else rect(0, y, 64, 1, 24, 44, 52) end rect(14, y, 1, 1, 20, 36, 44); rect(50, y, 1, 1, 20, 36, 44) else local t = (y - 49) / 14 rect(0, y, 64, 1, floor(lerp(32, 16, t)), floor(lerp(50, 28, t)), floor(lerp(58, 36, t))) end end rect(0, 49, 64, 1, 12, 24, 30) for _, y in ipairs({ 31, 32 }) do local e = 32 - hw(y) rect(e - 8, y, 9, 1, 56, 40, 32); rect(32 + hw(y) - 1, y, 9, 1, 56, 40, 32) end FAB = snapshot(0, 0, 64, 64) end local function bake_bag() rect(10, 44, 45, 20, KR, KG, KB) for y = 46, 63 do local t = (y - 46) / 17 local c1, c2, c3 = floor(lerp(138, 82, t)), floor(lerp(66, 36, t)), floor(lerp(48, 30, t)) local xl = floor(20 - max(0, y - 48) * 0.7) local xr = floor(44 + max(0, y - 46) * 0.7) if y < 52 then local lt = (y == 48 or y == 49) and 1 or 0 rect(xl + (y == 48 and 2 or 0), y, 12 - (y == 48 and 2 or 0), 1, c1, c2, c3) if y >= 47 then rect(33, y, xr - 32 - (y == 46 and 2 or 0), 1, c1, c2, c3) end if y == 47 or y == 49 then px(xl + 2, y, 176, 98, 70); px(34, y, 176, 98, 70) end else rect(xl, y, xr - xl + 1, 1, c1, c2, c3) end if y == 52 or y == 57 or y == 61 then rect(max(xl, 10), y, xr - xl + 1, 1, floor(c1 * 0.7), floor(c2 * 0.7), floor(c3 * 0.7)) end end px(26, 48, 176, 98, 70); px(38, 46, 176, 98, 70) BAG = snapshot(10, 44, 45, 20) end function on_settings() MOON = cfg.moon and { 246, 240, 214 } or nil stage = 0 end local FF = {} for i = 1, 8 do local h = hash32(i * 0x2545F491) FF[i] = { 18 + umod(h, 29), 30 + umod(h >> 8, 14), 3 + umod(h >> 12, 5), 2 + umod(h >> 16, 4), 0.011 + umod(h >> 20, 8) / 1500, 0.013 + umod(h >> 24, 8) / 1500, umod(h >> 4, 628) / 100, 0.03 + umod(h >> 10, 6) / 400 } end local function firefly(f) local x = f[1] + f[3] * sin(frame * f[5] + f[7]) local y = f[2] + f[4] * sin(frame * f[6] + f[7] * 2) local a = sin(frame * f[8] + f[7]) if a < 0.1 then return end a = a * a local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix - 1, iy - 1, 3, 3, 150, 230, 80, a * 0.14) blend_rect(ix, iy, 1, 1, 200, 255, 110, a * (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, 200, 255, 110, a * fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, 200, 255, 110, a * (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, 200, 255, 110, a * fx * fy) end function render() if stage < 2 then if stage == 0 then bake_bg() else bake_fabric(); bake_bag() end stage = stage + 1 gfx.clear(0, 0, 0) gfx.ready(false) return end blit_rgb(BG, 0, 0, 64, 64) for i = 1, #STARS do local s = STARS[i] local a = 0.55 + 0.45 * sin(frame * 0.05 + s[4]) local b = s[3] * a px(s[1], s[2], b * 0.85 // 1, b * 0.9 // 1, b // 1) end if MOON then for r = 0, 5 do local w = 2 + r local x = MX - w / 2 + 1.5 * sin(frame * 0.06 + r * 1.7) local ix = floor(x) local f = x - ix local a = 0.5 + 0.3 * sin(frame * 0.09 + r * 2.3) local y = HZ + 1 + r * 2 blend_rect(ix + 1, y, w - 1, 1, 236, 232, 206, a) blend_rect(ix, y, 1, 1, 236, 232, 206, a * (1 - f)) blend_rect(ix + w, y, 1, 1, 236, 232, 206, a * f) end else for i = 1, 5 do local a = 0.5 + 0.5 * sin(frame * 0.07 + i * 2.1) local s = STARS[i] blend_rect(s[1], HZ + 2 + (HZ - s[2]) // 2, 1, 1, 150, 170, 230, a * 0.6) end end local e, n = frame % 300, frame // 300 if e < 14 then local h = hash32(n * 0x9E3779B1 + 5) local x0, y0 = 16 + umod(h, 22), 4 + umod(h >> 8, 7) for k = 0, 5 do local d = e - k if d >= 0 then blend_rect(floor(x0 + d * 2.2), floor(y0 + d), 1, 1, 235, 242, 255, 1 - k / 6) end end end for i = 1, #FF do firefly(FF[i]) end blit_rgb(FAB, 0, 0, 64, 64, KR, KG, KB) blit_rgb(BAG, 10, 44 + ((frame % 150) < 45 and 1 or 0), 45, 20, KR, KG, KB) end