info = { type = "image", title = "Jungle rope bridge", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A pilgrim crosses a rope bridge in the jungle", api = 44, } local tr = ({ sk = { ["Mist"] = "Hmla", ["Thin"] = "Riedka", ["Thick"] = "Hustá" }, cs = { ["Mist"] = "Mlha", ["Thin"] = "Řídká", ["Thick"] = "Hustá" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fog", label = T("Mist"), type = "int", default = 0, opts = { T("Thin"), T("Thick") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function lerp(a, b, t) return a + (b - a) * t end local function rnd(v) return floor(v + 0.5) end local function clamp(x, a, b) return x < a and a or (x > b and b or x) end local function hs(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local NZ = {} for i = 1, 1024 do NZ[i] = (hash32(i * 0x9E3779B1) & 0x7FFF) / 32767 end local function vnoise(x, y) local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi fx = fx * fx * (3 - 2 * fx); fy = fy * fy * (3 - 2 * fy) local x0, x1, y0, y1 = xi & 31, (xi + 1) & 31, (yi & 31) * 32, ((yi + 1) & 31) * 32 local a = NZ[y0 + x0 + 1]; local b = NZ[y0 + x1 + 1] local c = NZ[y1 + x0 + 1]; local d = NZ[y1 + x1 + 1] a = a + (b - a) * fx; c = c + (d - c) * fx return a + (c - a) * fy end local function ramp(S, t) t = clamp(t, 0, 1) for i = 2, #S do local b = S[i] if t <= b[1] then local a = S[i - 1] local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end local z = S[#S] return z[2], z[3], z[4] end local FOL = { { 0, 8, 22, 14 }, { 0.28, 22, 62, 28 }, { 0.52, 54, 112, 34 }, { 0.76, 126, 170, 44 }, { 1, 214, 226, 112 } } local RIV = { { 0, 12, 64, 84 }, { 0.4, 22, 122, 132 }, { 0.7, 56, 184, 180 }, { 1, 176, 238, 228 } } local HAZE = { 150, 196, 184 } local YH, AD, DELTA = 8, 54, 0.09 local function xw(t) return -2 + 1.6 * t + 0.6 * t * t - 0.1 * t * t * t end local function xc(d) return 32 + 9 * xw(d - 1) / d end local function yg(d) return YH + AD / d end local DECK_TOP = 22 local DFAR = AD / (DECK_TOP - YH) local P local function pack(r, g, b) r, g, b = rnd(r), rnd(g), rnd(b) r = r < 0 and 0 or (r > 255 and 255 or r) g = g < 0 and 0 or (g > 255 and 255 or g) b = b < 0 and 0 or (b > 255 and 255 or b) return (r << 16) | (g << 8) | b end local function mix(x, y, r, g, b, a) local row = P[y + 1] local c = row[x + 1] local cr, cg, cb = c >> 16, (c >> 8) & 255, c & 255 row[x + 1] = pack(cr + (r - cr) * a, cg + (g - cg) * a, cb + (b - cb) * a) end local function aaline(x0, y0, x1, y1, r, g, b, al) local dx, dy = x1 - x0, y1 - y0 local n = max(1, floor(max(abs(dx), abs(dy)))) for i = 0, n do local u = i / n local x, y = x0 + dx * u, y0 + dy * u local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy for oy = 0, 1 do for ox = 0, 1 do local wgt = (ox == 0 and 1 - fx or fx) * (oy == 0 and 1 - fy or fy) local X, Y = ix + ox, iy + oy if wgt > 0.15 and X >= 0 and X < W and Y >= 0 and Y < H then mix(X, Y, r, g, b, min(1, wgt * 2.6) * al) end end end end end local DSL, DSR, DLW, DK, DGAP, DD, DTONE = {}, {}, {}, {}, {}, {}, {} local INL, INR, ROWC = {}, {}, {} local PK_Y0, PK_Y1 = {}, {} local function deck_rows() for y = DECK_TOP, H - 1 do local d = AD / (y + 0.5 - YH) local dtop, dbot = AD / (y - YH), AD / (y + 1 - YH) local cx, half = xc(d), 9 / d DSL[y], DSR[y] = cx - half, cx + half DLW[y] = max(1, 1.5 / d) DD[y] = d local qa, qb = (dbot - 1) / DELTA, (dtop - 1) / DELTA local k = floor((d - 1) / DELTA) DK[y] = k DGAP[y] = (AD * DELTA / (d * d) >= 2.3) and (floor(qb) > floor(qa)) DTONE[y] = 0.42 + 0.46 * (hs(k + 100, 7) % 100) / 100 INL[y], INR[y] = rnd(DSL[y] + DLW[y]), rnd(DSR[y] - DLW[y]) if k >= 0 and AD * DELTA / (d * d) >= 2.3 then PK_Y0[k] = min(PK_Y0[k] or y, y); PK_Y1[k] = max(PK_Y1[k] or y, y) end end end local FOLL, RIVL, BEAM = {}, {}, {} local function make_luts() for i = 0, 127 do local r, g, b = ramp(FOL, i / 127); FOLL[i + 1] = pack(r, g, b) r, g, b = ramp(RIV, i / 127); RIVL[i + 1] = pack(r, g, b) end for i = 0, 340 do BEAM[i + 1] = 0.5 + 0.5 * sin(i / 3 * 0.3 - 6) end end local SPX = {} local N1A, N1B = {}, {} local function build_row(y) local out = {} P[y + 1] = out local dsl, dsr = DSL[y], DSR[y] local fy, ya = (26 - y) / 9, (y - 56) / 8 local prow, crow = N1A, N1B local pn = 0.5 for x = 0, W - 1 do local f = (18 - x) / 12 local t = (x - 45) / 11 if t > f then f = t end if fy > f then f = fy end t = (x - 44) / 10 + ya if t > f then f = t end if f < 0 then f = 0 end local m = f + 0.7 * (vnoise(x / 6 + 20, y / 6) - 0.5) + 0.06 * (NZ[((y * 13 + x * 7) & 1023) + 1] - 0.5) local bm = BEAM[floor((x + 0.75 * y) * 3) + 1] local c if m > 0.5 then local n1 = vnoise(x / 5, y / 5) crow[x] = n1 local lit = 0.5 + 4 * (n1 - 0.5 * (pn + (prow[x] or n1))) lit = lit < 0 and 0 or (lit > 1 and 1 or lit) pn = n1 local n2 = (vnoise(x / 2.4 + 7, y / 2.4 + 3) - 0.38) * 3.2 n2 = n2 < 0 and 0 or (n2 > 1 and 1 or n2) local v = 0.02 + 0.3 * n1 + 0.3 * n2 + 0.16 * NZ[((y * 29 + x * 11) & 1023) + 1] + 0.28 * lit + 0.3 * bm * (1 - y / 80) v = 0.62 * v - 0.04 if m < 0.64 then v = v - 0.2 * (0.64 - m) / 0.14 end local iv = floor(v * 127 + 0.5) c = FOLL[(iv < 0 and 0 or (iv > 127 and 127 or iv)) + 1] elseif m > 0.42 then local k = 26 + 26 * NZ[((y * 17 + x * 5) & 1023) + 1] c = pack(k * 0.8, k * 1.1, k * 0.85) pn = 0.5 else pn = 0.5 local v = 0.28 + 0.36 * vnoise(x / 10 + 3, y / 2.2) + 0.2 * vnoise(x / 3, y / 2) + 0.14 * bm local sh = (m - 0.1) / 0.3 v = v - 0.3 * (sh < 0 and 0 or (sh > 1 and 1 or sh)) local iv = floor(v * 127 + 0.5) c = RIVL[(iv < 0 and 0 or (iv > 127 and 127 or iv)) + 1] if x > 24 then local rr = clamp((x - 30) / 16, 0, 1) local th = 0.74 - 0.3 * rr local fm = vnoise(x / 2.8 + 17, y / 1.6) if fm > th then local a = (fm - th) * 6 if a > 1 then a = 1 end local cr, cg, cb = c >> 16, (c >> 8) & 255, c & 255 c = pack(cr + (218 - cr) * a, cg + (242 - cg) * a, cb + (238 - cb) * a) end end if #SPX < 16 and y > 28 and hs(x, y) % 41 == 0 and (not dsl or x < dsl - 2 or x > dsr + 2) then SPX[#SPX + 1] = { x, y, hs(x, y) % 100 } end end if dsl and x >= floor(dsl) and x <= floor(dsr) then local d = DD[y] local cov = 1 if x == floor(dsl) then cov = 1 - (dsl - x) end if x == floor(dsr) then cov = min(cov, dsr - x) end local dr, dg, db if x < INL[y] or x > INR[y] then local hi = (x == INR[y] + 1 or x == INL[y] - 1) and 0 or 1 dr, dg, db = 98 + 36 * hi * bm, 68 + 28 * hi * bm, 40 + 18 * hi * bm else local k = DK[y] local tone = DTONE[y] + 0.12 * (vnoise(x / 3 + k * 3.7, k * 1.3) - 0.5) dr, dg, db = lerp(84, 218, tone), lerp(56, 174, tone), lerp(30, 108, tone) if DGAP[y] then dr, dg, db = 62, 40, 26 elseif DGAP[y - 1] then dr, dg, db = dr * 1.1, dg * 1.1, db * 1.1 end end local hz = clamp(0.17 * (d - 1), 0, 0.62) dr, dg, db = lerp(dr, HAZE[1], hz), lerp(dg, HAZE[2], hz), lerp(db, HAZE[3], hz) local cr, cg, cb = c >> 16, (c >> 8) & 255, c & 255 dr, dg, db = lerp(cr, dr, cov), lerp(cg, dg, cov), lerp(cb, db, cov) c = pack(dr, dg, db) if x == floor((dsl + dsr) / 2) then ROWC[y] = { dr, dg, db } end end out[x + 1] = c end N1A, N1B = N1B, N1A end local function rails() for side = -1, 1, 2 do local px0, py0, mx0, my0 for j = 0, 60 do local d = lerp(0.93, DFAR, j / 60) local x = xc(d) + side * 8.3 / d local y = yg(d) local hz = clamp(0.17 * (d - 1), 0, 0.62) local r, g, b = lerp(198, HAZE[1], hz), lerp(164, HAZE[2], hz), lerp(106, HAZE[3], hz) if px0 then aaline(px0, py0, x, y - 8.6 / d, r, g, b, 1) aaline(mx0, my0, x, y - 4.4 / d, r * 0.85, g * 0.85, b * 0.85, 0.9) end px0, py0, mx0, my0 = x, y - 8.6 / d, x, y - 4.4 / d end for j = 0, 7 do local d = 1 + j * 6 * DELTA if d < DFAR then local x, y = xc(d) + side * 8.3 / d, yg(d) local hz = clamp(0.17 * (d - 1), 0, 0.62) aaline(x, y, x, y - 8.8 / d, lerp(84, HAZE[1], hz), lerp(58, HAZE[2], hz), lerp(34, HAZE[3], hz), 1) if d < 2.2 then aaline(x + 1, y, x + 1, y - 8.8 / d, lerp(58, HAZE[1], hz), lerp(40, HAZE[2], hz), lerp(24, HAZE[3], hz), 1) end end end end end local M1S, M2S local function make_mask(w, h) local c = {} for y = 0, h - 1 do for x = 0, w - 1 do local d = sqrt(((x - (w - 1) / 2) / (w / 2)) ^ 2 + ((y - (h - 1) / 2) / (h / 2)) ^ 2) local k = 1 - d c[y * w + x + 1] = d >= 1 and 0 or rnd(255 * k * sqrt(k)) end end return string.char(table.unpack(c, 1, w * h)) end local STAGE, ROW, ROWS, BG = 0, 0, nil, nil local function build_step() if STAGE == 0 then P, ROWS = {}, {} deck_rows() STAGE = 0.5 elseif STAGE == 0.5 then make_luts() STAGE = 1 elseif STAGE == 1 then for y = ROW, min(H - 1, ROW + 1) do build_row(y) end ROW = ROW + 2 if ROW >= H then STAGE, ROW = 2, 0 end elseif STAGE == 2 then rails() STAGE = 3 elseif STAGE == 3 then local row = {} for y = ROW, min(H - 1, ROW + 5) do local k, prow = 0, P[y + 1] for x = 0, W - 1 do local c = prow[x + 1] row[k + 1], row[k + 2], row[k + 3] = c >> 16, (c >> 8) & 255, c & 255 k = k + 3 end ROWS[y + 1] = string.char(table.unpack(row, 1, W * 3)) end ROW = ROW + 6 if ROW >= H then BG = table.concat(ROWS); P, ROWS = nil, nil; STAGE = 4 end elseif STAGE == 4 then M1S = make_mask(20, 20); STAGE = 5 elseif STAGE == 5 then M2S = make_mask(30, 12); STAGE = 6 end end local function frect(x, y, w, h, r, g, b) local x1, y1 = x + w, y + h for j = floor(y), floor(y1 - 0.0001) do local cy = min(j + 1, y1) - max(j, y) for i = floor(x), floor(x1 - 0.0001) do local c = (min(i + 1, x1) - max(i, x)) * cy if c >= 0.97 then px(i, j, r, g, b) elseif c > 0.04 then blend(i, j, r, g, b, c) end end end end local WALK = 34 * FPS local CYCLE = 42 * FPS local function pilgrim(t) local u = t / (WALK / FPS) if u < 0 or u > 1 then return end local s = -0.07 + 0.92 * u local d = 1 + 3.5 * s local fx, fy = xc(d), yg(d) local h = 15 / d local ph = TAU * 1.15 * t local bob = 0.025 * h * abs(sin(ph)) local a = clamp((0.83 - s) * 14, 0, 1) local qp = (d - 1) / DELTA for k = max(0, floor(qp) - 2), floor(qp) + 2 do local y0, y1 = PK_Y0[k], PK_Y1[k] if y0 then local w = max(0, 1 - abs(k + 0.5 - qp) / 2.4) local off = floor(w * 1.5 / d + 0.5) if off > 0 then for y = y0, y1 do local c = ROWC[y] if c then local yy = y + off if INR[yy] then local wdt = INR[yy] - INL[yy] + 1 rect(INL[yy], yy, wdt, 1, rnd(c[1] * 0.96), rnd(c[2] * 0.96), rnd(c[3] * 0.96)) end end end end end end if a < 0.2 then return end local sink = (qp > 0 and qp < 9) and 0.5 or 0 fy = fy + sink blend_rect(rnd(fx - 0.28 * h), rnd(fy - 0.02 * h), max(2, rnd(0.56 * h)), max(1, rnd(0.06 * h)), 18, 14, 8, 0.35) for i = 0, 1 do local lift = 0.06 * h * max(0, sin(ph + i * 3.1416)) frect(fx + (i == 0 and -0.14 or 0.02) * h, fy - 0.42 * h, 0.12 * h, 0.42 * h - lift, 54, 46, 70) end frect(fx - 0.15 * h, fy - 0.79 * h + bob, 0.30 * h, 0.38 * h, 176, 56, 44) frect(fx - 0.15 * h, fy - 0.47 * h + bob, 0.30 * h, 0.06 * h, 120, 36, 34) frect(fx - 0.11 * h, fy - 0.74 * h + bob, 0.22 * h, 0.22 * h, 112, 80, 46) frect(fx - 0.14 * h, fy - 0.79 * h + bob, 0.28 * h, 0.05 * h, 96, 122, 150) frect(fx - 0.065 * h, fy - 0.92 * h + bob, 0.13 * h, 0.13 * h, 62, 42, 32) frect(fx - 0.17 * h, fy - 0.945 * h + bob, 0.34 * h, 0.04 * h, 238, 204, 108) frect(fx - 0.085 * h, fy - 1.0 * h + bob, 0.17 * h, 0.06 * h, 220, 184, 92) frect(fx + 0.19 * h, fy - 0.95 * h, max(0.8, 0.05 * h), 0.95 * h, 150, 112, 70) end local function mist(t, n, amax) for i = 1, n do local life = 13 + (hs(i, 1) % 80) / 10 local tt = t + hs(i, 2) % 1000 / 10 local c, ft = floor(tt / life), (tt % life) / life local x0, y0 = 4 + hs(i, c + 5) % 58, 40 + hs(i, c + 50) % 24 local x, y = x0 + 5 * sin(ft * 3 + i), y0 - 24 * ft local al = amax * min(1, ft * 4, (1 - ft) * 2.2) if i % 2 == 0 then gfx.blend_mask(M1S, 206, 238, 232, rnd(x) - 10, rnd(y) - 10, 20, 20, 20, 20, al) else gfx.blend_mask(M2S, 206, 238, 232, rnd(x) - 15, rnd(y) - 6, 30, 12, 30, 12, al) end end end local BIRD = { { "#.......#", ".#.....#.", "..##.##..", "...###..." }, { ".........", "##.....##", ".#######.", "...###..." }, { ".........", ".........", "##.###.##", ".##.#.##." }, } local BCELLS = {} for p = 1, 3 do local c = {} for y = 1, 4 do for x = 1, 9 do c[(y - 1) * 9 + x] = BIRD[p][y]:sub(x, x) == "#" and 1 or 0 end end BCELLS[p] = c end local function bird(t) local u = (t - 11) / 10 if u < 0 or u > 1 then return end local x = 72 - 84 * u local y = 17 + 6 * sin(u * 5) + 6 * u local pose = ({ 1, 2, 3, 2 })[floor(frame / 3) % 4 + 1] local cells = BCELLS[pose] local ix, iy = floor(x), floor(y) local fx = x - ix for oy = 0, 3 do for ox = 0, 9 do local a = 0 if ox < 9 and cells[oy * 9 + ox + 1] == 1 then a = a + (1 - fx) end if ox > 0 and cells[oy * 9 + ox] == 1 then a = a + fx end if a > 0.1 then blend(ix + ox, iy + oy, 244, 244, 232, min(1, a)) end end end end local FOG_NOW function on_settings() FOG_NOW = nil end function render() if STAGE < 6 then build_step() rect(0, 0, W, H, 18, 40, 28) rect(0, 0, W, 16, 30, 60, 44) gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = (frame % CYCLE) / FPS for i = 1, #SPX do local g = SPX[i] local a = max(0, sin((frame / FPS) * 1.9 + g[3] * 0.3)) if a > 0.6 then blend(g[1], g[2], 236, 252, 246, (a - 0.6) * 1.5) end end pilgrim(t) bird(t) local thick = (cfg.fog or 0) == 1 mist(frame / FPS, thick and 18 or 8, thick and 0.44 or 0.28) end