info = { type = "image", title = "Jungle falls", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A jungle waterfall, a stream and butterflies", api = 44, } local tr = ({ sk = { ["Butterflies"] = "Motýle", ["Flow"] = "Prúd", ["Slow"] = "Pomalý", ["Normal"] = "Normálny", ["Fast"] = "Rýchly" }, cs = { ["Butterflies"] = "Motýli", ["Flow"] = "Proud", ["Slow"] = "Pomalý", ["Normal"] = "Normální", ["Fast"] = "Rychlý" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bfly", label = T("Butterflies"), type = "int", min = 0, max = 6, default = 4 }, { key = "flow", label = T("Flow"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, } local rect, blend_rect, blend = gfx.rect, gfx.blend_rect, gfx.blend local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local DEEP, DARK, MID, LIGHT, HI = { 8, 40, 28 }, { 16, 78, 44 }, { 32, 124, 54 }, { 84, 178, 62 }, { 170, 226, 90 } local BACKP, MIDP, FRONTP = { DEEP, DARK, MID }, { DARK, MID, LIGHT }, { MID, LIGHT, HI } local ROCK_D, ROCK, ROCK_L = { 52, 50, 62 }, { 92, 88, 92 }, { 140, 134, 126 } local WAT_D, WAT, WAT_L = { 20, 112, 150 }, { 36, 168, 196 }, { 130, 230, 232 } local function rr(i, s) return (hash32(i * 0x9E3779B1 + s * 0x85EBCA6B) & 0xFFFF) / 65536 end local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, rx, ry, c) for dy = -ry, ry do local half = floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) rect(cx - half, cy + dy, 2 * half + 1, 1, c3(c)) end end local function clump(cx, cy, r, p, hi) ell(cx, cy + 1, r, r - 1, p[1]); ell(cx - 1, cy, r - 1, r - 2, p[2]); ell(cx - 2, cy - 1, r - 2, r - 3, p[3]) if hi then rect(cx - r + 2, cy - r + 3, 2, 1, c3(hi)); rect(cx - 1, cy - r + 2, 1, 1, c3(hi)) end end local function leaf(x, y, len, dir, slope, c, rib) for i = 0, len do local w = floor(2.6 * sin(3.14159 * i / len) + 0.5) + 1 local cx, cy = x + dir * i, y + floor(i * slope) rect(cx, cy - w, 1, 2 * w + 1, c3(c)) rect(cx, cy + w, 1, 1, c3(DARK)) if i % 2 == 0 then rect(cx, cy, 1, 1, c3(rib)) end end end local function stream_y(x) return floor(55 + (36 - x) * 0.03 + 1.2 * sin(x * 0.22)) end local BG, STAGE = nil, 0 local GLINTS = {} local FALL_X, FALL_W, FALL_T, FALL_B = 44, 10, 8, 47 local STAGES = { function() rect(0, 0, 64, 64, c3(DEEP)) for i = 1, 38 do clump(floor(rr(i, 1) * 64), floor(rr(i, 2) * 52), 5 + floor(rr(i, 3) * 3), BACKP) end end, function() for y = 6, 52 do local x0 = 35 + floor(2.5 * sin(y * 0.35) + 1.5 * sin(y * 0.8) + 0.5) rect(x0, y, 64 - x0, 1, c3(ROCK)) rect(x0, y, 2, 1, c3(ROCK_L)) end for _, y in ipairs({ 14, 22, 30, 38, 46 }) do rect(36 + floor(rr(y, 4) * 6), y, 22 + floor(rr(y, 5) * 6), 1, c3(ROCK_D)) end for i = 1, 10 do rect(55 + floor(rr(i, 6) * 8), 8 + floor(rr(i, 7) * 40), 1, 3 + floor(rr(i, 8) * 4), c3(ROCK_D)) end for i = 1, 8 do clump(37 + floor(rr(i, 9) * 26), 12 + floor(rr(i, 10) * 36), 3, MIDP) end rect(FALL_X, FALL_T - 2, FALL_W, FALL_B - FALL_T + 2, c3(WAT)) rect(FALL_X, FALL_T - 2, 1, FALL_B - FALL_T + 2, c3(WAT_D)) rect(FALL_X + FALL_W - 1, FALL_T - 2, 1, FALL_B - FALL_T + 2, c3(WAT_L)) rect(FALL_X - 3, 5, FALL_W + 6, 3, c3(ROCK_D)); rect(FALL_X - 2, 5, FALL_W + 4, 1, c3(ROCK)) ell(46, 52, 12, 5, WAT_D); ell(46, 52, 11, 4, WAT) ell(34, 56, 4, 3, ROCK_D); ell(34, 55, 3, 2, ROCK); ell(59, 55, 4, 3, ROCK_D); ell(59, 54, 3, 2, ROCK) ell(40, 58, 3, 2, ROCK_D); ell(53, 59, 4, 2, ROCK_D); ell(52, 58, 3, 1, ROCK) for i = 1, 8 do clump(35 + floor(rr(i, 11) * 28), floor(rr(i, 12) * 6), 5, FRONTP) end end, function() for i = 0, 5 do rect(8 + i, 0, 1, 53, c3(i < 2 and { 130, 88, 50 } or (i < 4 and { 90, 56, 34 } or { 56, 36, 24 }))) end for i = 1, 14 do rect(8 + floor(rr(i, 21) * 6), floor(rr(i, 22) * 44), 1, 3, 56, 36, 24) end for x = 0, 36 do local y = stream_y(x) rect(x, y - 2, 1, 1, 14, 90, 110); rect(x, y - 1, 1, 2, c3(WAT)); rect(x, y + 1, 1, 2, 60, 200, 214); rect(x, y + 3, 1, 1, 20, 110, 130) end for x = 0, 33 do local y = stream_y(x) + 4 rect(x, y, 1, 64 - y, 34, 112, 48) rect(x, y, 1, 1, c3(LIGHT)) end end, function() for i = 1, 22 do clump(floor(rr(i, 23) * 36), 12 + floor(rr(i, 24) * 40), 4 + floor(rr(i, 25) * 3), MIDP) end for i = 1, 8 do local x, y = floor(rr(i, 26) * 30), 4 + floor(rr(i, 27) * 28) leaf(x, y, 12 + floor(rr(i, 28) * 6), i % 2 == 0 and 1 or -1, 0.5, i % 3 == 0 and LIGHT or MID, HI) end ell(26, 57, 3, 2, ROCK_D); ell(25, 56, 2, 1, ROCK) end, function() for k = 1, 8 do local x0, len = 4 + floor(rr(k, 31) * 34), 14 + floor(rr(k, 32) * 22) for y = 0, len do local x = x0 + floor(1.2 * sin(y * 0.5 + k) + 0.5) rect(x, y, 1, 1, 22, 96, 40) if y > 3 and y % 5 == 0 then rect(x - 2, y, 2, 1, c3(LIGHT)); rect(x + 1, y + 1, 2, 1, c3(MID)) end end end end, function() for i = 1, 9 do clump(floor(rr(i, 33) * 64), 60 + floor(rr(i, 34) * 4), 5, FRONTP, HI) end clump(40, 12, 5, FRONTP, HI); clump(57, 14, 5, FRONTP, HI); clump(38, 30, 4, MIDP); clump(59, 34, 4, MIDP) leaf(60, 22, 9, -1, 0.5, MID, HI); leaf(36, 4, 9, 1, 0.6, LIGHT, HI) local FL = { { 255, 110, 170 }, { 255, 230, 80 }, { 240, 60, 60 }, { 250, 250, 250 } } for i = 1, 14 do local x, y = floor(rr(i, 35) * 62), 26 + floor(rr(i, 36) * 34) local c = FL[i % 4 + 1] rect(x, y, 2, 1, c3(c)); rect(x, y + 1, 1, 1, c3(c)) end for i = 1, 8 do GLINTS[i] = { 36 + floor(rr(i, 37) * 26), 49 + floor(rr(i, 38) * 8), 0.05 + rr(i, 39) * 0.05, rr(i, 40) * 6.28 } end for i = 9, 14 do GLINTS[i] = { floor(rr(i, 37) * 34), stream_y(floor(rr(i, 37) * 34)), 0.05 + rr(i, 39) * 0.05, rr(i, 40) * 6.28 } end end, } local FLOW = 1.0 local NBF = 4 function on_settings() FLOW = ({ slow = 0.7, normal = 1.0, fast = 1.5 })[cfg.flow] or 1.0 NBF = cfg.bfly or 4 end local function span(x, y, w, c, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 1, c[1], c[2], c[3], a * (1 - f)) if w > 1 then blend_rect(ix + 1, y, w - 1, 1, c[1], c[2], c[3], a) end blend_rect(ix + w, y, 1, 1, c[1], c[2], c[3], a * f) end local function bilinear(x, y, c) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, c[1], c[2], c[3], (1 - fx) * (1 - fy)); blend(ix + 1, iy, c[1], c[2], c[3], fx * (1 - fy)) blend(ix, iy + 1, c[1], c[2], c[3], (1 - fx) * fy); blend(ix + 1, iy + 1, c[1], c[2], c[3], fx * fy) end local WING = { { 255, 214, 50 }, { 90, 170, 255 }, { 255, 126, 40 }, { 250, 90, 160 }, { 240, 240, 250 }, { 160, 255, 120 } } local BODY = { 40, 30, 30 } local OPEN = { { -2, -1 }, { -1, -1 }, { 1, -1 }, { 2, -1 }, { -1, 0 }, { 1, 0 } } local HALF = { { -1, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 } } local CLOSED = { { 1, -1 }, { 1, 0 } } local FLAP = { OPEN, HALF, CLOSED, HALF } local FLIT = {} for i = 1, 6 do FLIT[i] = { cx = 8 + rr(i, 51) * 44, cy = 14 + rr(i, 52) * 34, ax = 7 + rr(i, 53) * 8, ay = 4 + rr(i, 54) * 5, wx = 0.018 + rr(i, 55) * 0.02, wy = 0.027 + rr(i, 56) * 0.02, ph = rr(i, 57) * 6.28 } end local WHITE = { 236, 252, 252 } function render() if STAGE < #STAGES then if STAGE > 0 then gfx.blit_rgb(BG, 0, 0, 64, 64) end STAGES[STAGE + 1]() STAGE = STAGE + 1 BG = gfx.snapshot(0, 0, 64, 64) if STAGE < #STAGES then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, 64, 64) local f = frame gfx.clip(FALL_X, FALL_T, FALL_W, FALL_B - FALL_T) for i = 0, FALL_W - 1 do local ph = floor(f * (1 + 0.1 * ((i * 7) % 6)) * FLOW + (i * 5) % 11) % 11 for k = -1, 4 do local y = FALL_T + ph + 11 * k rect(FALL_X + i, y, 1, 4, 190, 245, 245) rect(FALL_X + i, y + 5, 1, 2, 90, 200, 220) end end gfx.clip() for i = 1, 14 do local h = hash32(i * 0x85EBCA6B + (f // 4) * 0x9E3779B1) rect(41 + (h & 15), 45 + ((h >> 8) & 3), 2, 1, c3(WHITE)) end for k = 0, 2 do local a = (f + k * 13) % 40 local r, al = 2 + floor(a * 0.28), 0.9 * (1 - a / 40) local ry = r // 4 + 1 blend_rect(46 - r + 2, 51 - ry, 2 * r - 3, 1, 236, 252, 252, al); blend_rect(46 - r + 2, 51 + ry, 2 * r - 3, 1, 236, 252, 252, al) blend_rect(46 - r, 51 - ry + 1, 1, 2 * ry - 1, 236, 252, 252, al); blend_rect(46 + r, 51 - ry + 1, 1, 2 * ry - 1, 236, 252, 252, al) end blend_rect(40, 40, 14, 8, 236, 252, 252, 0.10 + 0.05 * sin(f * 0.05)) blend_rect(42, 36, 10, 5, 236, 252, 252, 0.06 + 0.04 * sin(f * 0.05 + 1)) for i = 1, 14 do local v = (0.25 + rr(i, 61) * 0.45) * FLOW local x = (rr(i, 62) * 40 - f * v) % 40 - 3 if x < 34 then span(x, stream_y(floor(x)) - 1 + i % 4, 3, WAT_L, 0.8) end end for i = 1, #GLINTS do local g = GLINTS[i] local a = sin(f * g[3] + g[4]) if a > 0.65 then blend(g[1], g[2], 255, 255, 255, (a - 0.65) / 0.35) end end for i = 1, NBF do local b = FLIT[i] local x = b.cx + b.ax * sin(f * b.wx + b.ph) local y = b.cy + b.ay * sin(f * b.wy + b.ph * 2) local wc = WING[i] local pose = FLAP[(f // 3 + i) % 4 + 1] for j = 1, #pose do bilinear(x + pose[j][1], y + pose[j][2], wc) end bilinear(x, y - 1, BODY); bilinear(x, y, BODY) end end