info = { type = "image", title = "Parrot falls", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Jungle waterfall with a parrot flying by", api = 33, } local tr = ({ sk = { ["Fall speed"] = "Rýchlosť vodopádu", ["Mist"] = "Hmla", ["Parrot"] = "Papagáj" }, cs = { ["Fall speed"] = "Rychlost vodopádu", ["Mist"] = "Mlha", ["Parrot"] = "Papoušek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Fall speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "mist", label = T("Mist"), type = "int", min = 0, max = 5, default = 3 }, { key = "parrot", label = T("Parrot"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local DEEP, MID = { 12, 38, 34 }, { 22, 66, 50 } local LEAF, LEAFL = { 44, 122, 64 }, { 92, 176, 84 } local TRUNK, TRUNKD = { 110, 70, 38 }, { 62, 40, 26 } local ROCK, ROCKL, ROCKD = { 92, 84, 74 }, { 134, 122, 102 }, { 52, 48, 46 } local WATER, WATERD = { 30, 130, 210 }, { 18, 84, 168 } local WATERL, FOAM = { 128, 204, 248 }, { 236, 248, 255 } local VINE = { 62, 118, 50 } local FX0, FX1 = 44, 51 local LIP, FOOT = 11, 53 local function c3(c) return c[1], c[2], c[3] end local function fseg(x, y, len, c, a) local r, g, b = c[1], c[2], c[3] local x0 = floor(x) local xe = x + len local x1 = floor(xe) if x1 == x0 then blend_rect(x0, y, 1, 1, r, g, b, a * len) return end if x1 - x0 > 1 then blend_rect(x0 + 1, y, x1 - x0 - 1, 1, r, g, b, a) end blend_rect(x0, y, 1, 1, r, g, b, a * (1 - (x - x0))) local f1 = xe - x1 if f1 > 0.02 then blend_rect(x1, y, 1, 1, r, g, b, a * f1) end end local function blob(cx, cy, rx, ry, c) for dy = -ry, ry do local k = dy / (ry + 0.5) local hw = floor(rx * sqrt(1 - k * k) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c3(c)) end end local BG = nil local function build_bg() rect(0, 0, W, H, c3(DEEP)) for _, t in ipairs({ { 5, 3 }, { 16, 3 }, { 29, 4 } }) do rect(t[1], 10, t[2], 44, c3(TRUNKD)) rect(t[1], 10, 1, 44, c3(TRUNK)) end local blobs = { { 6, 6, 10, 7 }, { 20, 4, 11, 6 }, { 34, 7, 9, 7 }, { 12, 15, 8, 5 }, { 27, 17, 8, 5 }, { 2, 18, 5, 5 } } for _, b in ipairs(blobs) do blob(b[1], b[2] + 1, b[3], b[4], MID) end for _, b in ipairs(blobs) do blob(b[1] - 1, b[2] - 1, b[3] - 2, b[4] - 2, LEAF) end for _, b in ipairs(blobs) do blob(b[1] - 3, b[2] - 3, b[3] - 6, 1, LEAFL) end for y = 14, H - 1 do local jag = umod(hash32(y * 0x9E3779B1), 3) rect(35 + jag, y, W - 35 - jag, 1, c3(ROCK)) px(35 + jag, y, c3(ROCKL)) end for y = 24, 52, 7 do rect(46 + umod(hash32(y), 12), y, 2, 1, c3(ROCKD)); rect(54 + umod(hash32(y + 1), 8), y + 3, 3, 1, c3(ROCKD)) end rect(36, 12, 8, 3, c3(LEAF)); rect(52, 12, 12, 3, c3(LEAF)) rect(38, 10, 4, 2, c3(LEAFL)); rect(55, 10, 6, 2, c3(LEAFL)); rect(58, 8, 3, 2, c3(LEAFL)) rect(FX0 - 2, 14, 2, FOOT - 14, c3(ROCKD)); rect(FX1 + 1, 14, 2, FOOT - 14, c3(ROCKD)) rect(FX0, LIP, FX1 - FX0 + 1, FOOT - LIP, c3(WATER)) rect(FX0, LIP, 1, FOOT - LIP, c3(WATERD)); rect(FX1, LIP, 1, FOOT - LIP, c3(WATERD)) rect(0, FOOT, 24, H - FOOT, c3(MID)) rect(0, FOOT, 24, 2, c3(LEAF)) for x = 1, 22, 3 do px(x, FOOT - 1, c3(LEAFL)); px(x + 1, FOOT - 2, c3(LEAF)) end rect(24, FOOT, W - 24, H - FOOT, c3(WATER)) rect(24, 59, W - 24, 5, c3(WATERD)) rect(30, FOOT - 1, 5, 3, c3(ROCKL)); rect(31, FOOT - 2, 3, 1, c3(ROCKL)); rect(30, FOOT + 1, 5, 1, c3(ROCKD)) BG = gfx.snapshot(0, 0, W, H) end local LIANAS = { { 9, 17, 17, 0.13, 3.6, 0.0 }, { 21, 15, 23, 0.10, 4.3, 1.9 }, { 31, 19, 14, 0.15, 3.1, 4.0 }, { 14, 12, 11, 0.16, 2.8, 2.6 }, } local function draw_liana(l, t) local a = l[4] * sin(t * 6.2832 / l[5] + l[6]) local s = sin(a) for k = 0, l[3] do local xf = l[1] + s * k local x0 = floor(xf) local f = xf - x0 local y = l[2] + k blend(x0, y, VINE[1], VINE[2], VINE[3], 1 - f) if f > 0.04 then blend(x0 + 1, y, VINE[1], VINE[2], VINE[3], f) end end local xt = l[1] + s * l[3] local yt = l[2] + l[3] fseg(xt - 1, yt, 3, LEAF, 1) fseg(xt - 1.5, yt + 1, 4, LEAFL, 1) fseg(xt - 1, yt + 2, 3, LEAF, 1) end local function draw_fall(t, spd) for g = 0, 3 do local x = FX0 + g * 2 + 1 - 1 for k = 0, 2 do local h = hash32((g * 3 + k + 1) * 0x9E3779B1) local len = 6 + umod(h, 5) local span = FOOT - LIP + len + 8 local v = (30 + umod(h >> 4, 14)) * spd / 3 local y = (umod(h >> 8, span) + t * v) % span - len local ya, yb = floor(y), floor(y) + len if ya < LIP then ya = LIP end if yb > FOOT then yb = FOOT end if yb > ya then rect(x, ya, 2, yb - ya, c3(WATERL)) if yb - ya > 2 then rect(x, ya, 2, 1, c3(FOAM)) end end end end end local SPRAY = {} for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) SPRAY[i] = { vx = (umod(h, 100) - 50) / 12, vy = 14 + umod(h >> 8, 12), per = 0.7 + umod(h >> 14, 6) / 10, ph = umod(h >> 20, 100) / 100 } end local function draw_foot(t, spd, mist) local pulse = 0.65 + 0.2 * sin(t * 7) blend_rect(FX0 - 3, FOOT, FX1 - FX0 + 7, 1, FOAM[1], FOAM[2], FOAM[3], pulse) blend_rect(FX0 - 1, FOOT + 1, FX1 - FX0 + 3, 1, FOAM[1], FOAM[2], FOAM[3], 0.35) for i = 1, 9 do local p = SPRAY[i] local s = ((t * spd / 3) / p.per + p.ph) % 1 local u = s * p.per local y = FOOT - 1 - (p.vy * u - 14 * u * u) if y < FOOT then local x = 47.5 + p.vx * u * 1.6 blend(floor(x + 0.5), floor(y + 0.5), FOAM[1], FOAM[2], FOAM[3], 0.9 * (1 - s * 0.5)) end end for i = 1, mist * 2 do local h = hash32(i * 0xC2B2AE35) local ph = umod(h, 628) / 100 local cx = 47.5 + 7 * sin(t * 0.6 + ph) + (umod(h >> 10, 9) - 4) local cy = FOOT - 3 - umod(h >> 14, 7) + 1.5 * sin(t * 0.9 + ph * 2) local a = 0.10 + 0.04 * sin(t * 1.3 + ph) local w = 8 + umod(h >> 18, 5) fseg(cx - w / 2, floor(cy), w, FOAM, a) fseg(cx - w / 2 + 2, floor(cy) + 1, w - 4, FOAM, a) fseg(cx - w / 2 + 2, floor(cy) - 1, w - 4, FOAM, a * 0.7) end end local function draw_pool(t) for i = 1, 7 do local h = hash32(i * 0x27D4EB2F) local y = FOOT + 2 + (i - 1) % 6 + (i > 6 and 4 or 0) local len = 4 + umod(h, 4) local v = (3 + umod(h >> 6, 4)) * (i % 2 == 1 and 1 or -1) local x = (umod(h >> 10, W + 12) + t * v) % (W + 12) - 8 if x + len > 24 then local x0 = x < 24 and 24 or x fseg(x0, y, x + len - x0, WATERL, 0.55) end end for i = 0, 2 do local x = 52 + (i * 4 - t * 4) % 12 fseg(x, LIP, 3, WATERL, 0.6) end end local PERIOD, FLIGHT, SPEED = 12, 2.5, 38 local BW = 12 local function draw_parrot(t) local cyc = floor(t / PERIOD) local u = t - cyc * PERIOD if u > FLIGHT then return end local dir = (cyc % 2 == 0) and 1 or -1 local d = (u / FLIGHT) * (W + 2 * BW) - BW local x0 = floor(dir == 1 and d or (W - d - BW) + 0.5) local y0 = floor(18 + 5 * (u / FLIGHT) + 2 * sin(u * 8.5) + 0.5) local function r(dx, dy, w, h, c) local X = dir == 1 and (x0 + dx) or (x0 + BW - dx - w) rect(X, y0 + dy, w, h, c3(c)) end local RED, BLUE, YEL = { 232, 52, 44 }, { 40, 92, 226 }, { 250, 214, 64 } local ph = floor(u * 30 / 4) % 4 r(-3, 4, 4, 1, BLUE); r(-2, 5, 3, 1, YEL) r(1, 3, 5, 3, RED) r(6, 2, 3, 3, RED); r(9, 3, 1, 1, YEL) px(dir == 1 and x0 + 7 or x0 + BW - 7 - 1, y0 + 3, 20, 20, 24) if ph == 0 then r(2, 0, 3, 3, BLUE); r(2, 0, 1, 1, YEL) elseif ph == 1 or ph == 3 then r(1, 2, 5, 1, BLUE); r(1, 1, 1, 1, YEL) else r(2, 6, 3, 2, BLUE); r(2, 7, 1, 1, YEL) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_fall(t, cfg.speed or 3) draw_pool(t) draw_foot(t, cfg.speed or 3, cfg.mist or 3) for i = 1, #LIANAS do draw_liana(LIANAS[i], t) end if cfg.parrot ~= false then draw_parrot(t) end end