info = { type = "image", title = "Dusk ridge", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Layered mountain ridges pan past a dusk camp", api = 33, } local tr = ({ sk = { ["Pan speed"] = "Rýchlosť posunu", ["Still"] = "Stojí", ["Slow"] = "Pomalý", ["Normal"] = "Bežný", ["Camp"] = "Tábor", ["Stars"] = "Hviezdy", ["Sky"] = "Obloha", ["Dusk"] = "Súmrak", ["Rose"] = "Ružová" }, cs = { ["Pan speed"] = "Rychlost posunu", ["Still"] = "Stojí", ["Slow"] = "Pomalý", ["Normal"] = "Běžný", ["Camp"] = "Tábor", ["Stars"] = "Hvězdy", ["Sky"] = "Obloha", ["Dusk"] = "Soumrak", ["Rose"] = "Růžová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Pan speed"), type = "int", default = 2, opts = { T("Still"), T("Slow"), T("Normal") } }, { key = "tent", label = T("Camp"), type = "bool", default = true }, { key = "stars", label = T("Stars"), type = "bool", default = true }, { key = "sky", label = T("Sky"), type = "int", default = 0, opts = { T("Dusk"), T("Rose") } }, } local rect, px, blend, blend_rect, blit_rgb, blit_idx = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local hash32 = math.hash32 local W, H = gfx.W, gfx.H local P = 128 local TILE = 16 local WV = 6.2832 / P local NS, NV = 6, 10 local function lerp(a, b, t) return a + (b - a) * t end local function mix(a, b, t) return { lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t) } end local SKYS = { { { 70, 48, 132 }, { 178, 84, 140 }, { 255, 138, 84 }, { 255, 186, 100 } }, { { 52, 40, 120 }, { 150, 70, 150 }, { 240, 110, 130 }, { 255, 170, 150 } }, } local SK = SKYS[1] local YS = { 0, 20, 36, 46 } local LAYERS = { { y0 = 27, hl = 25, base = 40, dm = 24, parts = { { 2, 7, 0.3 }, { 5, 3.5, 1.1 }, { 11, 1.5, 2.0 } }, lit = { 246, 150, 112 }, shade = { 166, 98, 136 }, rim = { 255, 214, 160 } }, { y0 = 34, hl = 26, base = 50, dm = 22, parts = { { 3, 9, 1.7 }, { 7, 4, 0.4 }, { 15, 2, 3.0 } }, lit = { 182, 98, 126 }, shade = { 74, 56, 116 }, rim = { 244, 168, 128 }, bot = { 96, 66, 118 } }, { y0 = 41, hl = 23, base = 58, dm = 18, parts = { { 2, 6, 2.5 }, { 6, 3, 0.2 }, { 17, 1.5, 1.0 } }, lit = { 86, 62, 108 }, shade = { 28, 26, 54 }, rim = { 226, 140, 112 }, bot = { 14, 16, 34 } }, } local LEDGE_Y, TENT_U, FIRE_U, TREE_U = 55, 47, 36, 63 local function ytop(L, u) local ly = LAYERS[L] local h = 0 local parts = ly.parts for i = 1, #parts do local c = parts[i] h = h + c[2] * (1 - abs(sin(c[1] * WV * u + c[3]))) end local t = ly.base - h if L == 3 then local a = min(1, max(0, (u - 28) / 4)); a = a * a * (3 - 2 * a) local b = min(1, max(0, (u - 70) / 4)); b = b * b * (3 - 2 * b) local m = a * (1 - b) t = t * (1 - m) + LEDGE_Y * m end return t end local PALS = {} local function build_pals() for L = 1, 3 do local ly = LAYERS[L] local bot = ly.bot or mix(SK[4], { 120, 70, 110 }, 0.12) local pal = { 0, 0, 0 } for s = 0, NS - 1 do local ts = s / (NS - 1) local top = mix(ly.shade, ly.lit, ts) for v = 0, NV - 1 do local c = mix(top, bot, v / (NV - 1)) pal[#pal + 1], pal[#pal + 2], pal[#pal + 3] = floor(c[1] + 0.5), floor(c[2] + 0.5), floor(c[3] + 0.5) end end for s = 0, NS - 1 do local c = mix(ly.lit, ly.rim, 0.35 + 0.65 * s / (NS - 1)) pal[#pal + 1], pal[#pal + 2], pal[#pal + 3] = floor(c[1]), floor(c[2]), floor(c[3]) end if L == 3 then for _, c in ipairs({ { 214, 96, 76 }, { 60, 40, 80 }, { 255, 178, 86 }, { 14, 34, 40 }, { 36, 72, 62 }, { 46, 36, 56 }, { 92, 70, 84 } }) do pal[#pal + 1], pal[#pal + 2], pal[#pal + 3] = c[1], c[2], c[3] end end PALS[L] = pal end end local STR = {} local NEXT = 1 local RIM0 = NS * NV + 1 local OBJ0 = RIM0 + NS local function camp_cell(uc, y) local dx = uc - TENT_U if y >= 46 and y <= 54 then local hw = (y - 46 + 0.5) / 9 * 6 if abs(dx) < hw then if y >= 52 and abs(dx) < 1.1 then return OBJ0 + 2 end return dx < 0 and OBJ0 or OBJ0 + 1 end end local tx = uc - TREE_U if y >= 42 and y <= 55 then local hw = (y - 42 + 0.5) / 13 * 3.6 if abs(tx) < hw then return tx < -0.3 and OBJ0 + 4 or OBJ0 + 3 end end local fx = uc - FIRE_U if y >= 54 and y <= 55 and abs(fx) < 2.6 and (y == 55 or abs(fx) > 1.2) then return fx < 0 and OBJ0 + 6 or OBJ0 + 5 end return nil end local JOB local function start_job(L, p) local ly = LAYERS[L] local top, base, base2, rim, T = {}, {}, {}, {}, {} for x = -1, P + 2 do T[x] = ytop(L, x - 0.5 + 0.5 * p) end for x = 1, P do top[x] = T[x] local sl = T[x + 2] - T[x - 2] local s = floor(min(1, max(0, 0.5 - 0.17 * sl)) * (NS - 1) + 0.5) base[x], rim[x] = 1 + s * NV, RIM0 + s if L < 3 and (hash32((x - 1) // 3 * 0x9E3779B1 + L) & 7) < 2 then base2[x] = 1 + max(0, s - 2) * NV else base2[x] = base[x] end end local vbt = {} for i = 1, ly.hl do vbt[i] = min(NV - 1, floor(min(i, ly.dm) / ly.dm * (NV - 1) + 0.5)) end JOB = { L = L, p = p, top = top, base = base, base2 = base2, rim = rim, vbt = vbt, r = 1, rows = {}, row = {} } end local function job_rows() local j = JOB local ly = LAYERS[j.L] local top, base, base2, rim, vbt, row = j.top, j.base, j.base2, j.rim, j.vbt, j.row local camp = j.L == 3 and cfg.tent ~= false for r = j.r, min(j.r + 11, ly.hl) do local y = ly.y0 + r - 1 local Y = y + 0.5 for x = 1, P do local d = Y - top[x] if d < 1 then row[x] = d < 0 and 0 or rim[x] elseif d > 5 then row[x] = base2[x] + vbt[d // 1] else row[x] = base[x] + vbt[d // 1] end end if camp and y >= 42 then for _, c0 in ipairs({ FIRE_U - 4, TENT_U - 7, TREE_U - 5 }) do for x = c0, c0 + 13 do local cx = camp_cell(x - 1 + 0.5 * j.p + 0.5, y) if cx then row[x] = cx end end end end j.rows[r] = string.char(table.unpack(row, 1, P)) end j.r = min(j.r + 11, ly.hl) + 1 if j.r > ly.hl then local tiles = {} for t = 0, P // TILE - 1 do local tr = {} for r = 1, ly.hl do tr[r] = j.rows[r]:sub(t * TILE + 1, t * TILE + TILE) end tiles[t] = table.concat(tr) end STR[(j.L - 1) * 2 + j.p + 1] = tiles JOB = nil NEXT = NEXT + 1 end end local BG local function sky_at(y) for i = 1, 3 do if y <= YS[i + 1] then local t = (y - YS[i]) / (YS[i + 1] - YS[i]) local a, b = SK[i], SK[i + 1] 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 end return SK[4][1], SK[4][2], SK[4][3] end local function build_sky() for y = 0, H - 1 do rect(0, y, W, 1, sky_at(y)) end BG = gfx.snapshot(0, 0, W, H) end local CLOUDS = { { 11, 26, 0.03, 8 }, { 19, 34, 0.02, 40 }, { 6, 20, 0.045, 25 } } local function clouds() for i = 1, #CLOUDS do local c = CLOUDS[i] local x = (c[4] + frame * c[3]) % (W + c[2]) - c[2] local i0 = floor(x) local f = x - i0 blend_rect(i0 + 2, c[1], c[2] - 5, 1, 255, 196, 170, 0.24) blend_rect(i0 + 2 + (f > 0.5 and 1 or 0), c[1] + 1, c[2] - 7, 1, 255, 196, 170, 0.16) end end local STARS = {} for i = 1, 40 do local h = hash32(i * 0x9E3779B1) STARS[i] = { (h & 0x7FFF) % W, ((h >> 8) & 0x7FFF) % 24, (((h >> 16) & 0x7FFF) % 1800), ((h >> 4) & 7) / 3 } end local CYCLE = 2700 local function stars() local t = frame % CYCLE local off = 1 if t > 2300 then off = 1 - min(1, (t - 2300) / 380) off = off * off * (3 - 2 * off) end if off <= 0.02 then return end for i = 1, #STARS do local s = STARS[i] local a = min(1, max(0, (t - s[3]) / 60)) if a > 0 then a = a * a * (3 - 2 * a) * off * (0.75 + 0.25 * sin(frame * 0.07 + s[4] * 9)) blend(s[1], s[2], 255, 246, 232, a * (s[4] > 1.6 and 1 or 0.7)) end end end local SPEED = { 0, 0.5, 1 } local LV = { 0.05, 0.12, 0.27 } function on_settings() SK = SKYS[(cfg.sky or 0) + 1] or SKYS[1] BG = nil STR = {} NEXT = 1 JOB = nil end local function flame(sx) local x = floor(sx) if x < -2 or x > W + 1 then return end local n = (hash32(frame // 2) & 3) + 1 blend_rect(x - 3, 55, 7, 2, 255, 120, 40, 0.16) px(x, 54, 255, 98, 28) px(x - 1 + (n & 1), 53, 255, 150, 40) px(x, 53, 255, 170, 50) if n >= 2 then px(x, 52, 255, 214, 96) end if n == 4 then px(x, 51, 255, 240, 160) end end function render() if not BG then build_sky(); build_pals() end if JOB then job_rows() elseif NEXT <= 6 then start_job((NEXT - 1) % 3 + 1, (NEXT - 1) // 3) end blit_rgb(BG, 0, 0, W, H) clouds() if cfg.stars ~= false then stars() end local sp = SPEED[(cfg.speed or 2) + 1] or 1 local cam3 for L = 1, 3 do local c = (frame * LV[L] * sp) % P if L == 3 then cam3 = c end local off = floor(c) local ph = c - off >= 0.5 and 1 or 0 local ly = LAYERS[L] local s = STR[(L - 1) * 2 + ph + 1] or STR[(L - 1) * 2 + 1] if s then local pal, y0, hl = PALS[L], ly.y0, ly.hl for t = 0, P // TILE - 1 do local x = t * TILE - off if x <= -TILE then x = x + P end if x < W then blit_idx(s[t], pal, x, y0, TILE, hl, TILE, hl, true) end end end end if cfg.tent ~= false and STR[5] then local sx = FIRE_U + 0.5 - cam3 if sx < -4 then sx = sx + P end flame(sx) end end