info = { type = "image", title = "Sloth branch", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A sloth creeps along a branch in the rain", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Slow"] = "Pomaly", ["Slower"] = "Ešte pomalšie" }, cs = { ["Speed"] = "Rychlost", ["Slow"] = "Pomalu", ["Slower"] = "Ještě pomaleji" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", default = "slow", opts = { keys = { "slow", "slower" }, T("Slow"), T("Slower") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, ceil, sqrt, sin, cos = math.floor, math.ceil, math.sqrt, math.sin, math.cos local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function span(xa, xb, y, r, g, b, a) if xb <= xa then return end a = a or 1 local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, (xb - xa) * a) return end local s, c = ia + 1, ia + 1 - xa if c > 0.96 and a == 1 then s = ia else blend(ia, y, r, g, b, c * a) end local f = xb - ib if f > 0.04 then blend(ib, y, r, g, b, f * a) end if ib > s then if a < 1 then blend_rect(s, y, ib - s, 1, r, g, b, a) else rect(s, y, ib - s, 1, r, g, b) end end end local function oval(cx, cy, rx, ry, c, a) for y = floor(cy - ry), ceil(cy + ry) - 1 do local d = (y + 0.5 - cy) / ry if d > -1 and d < 1 then local hw = rx * sqrt(1 - d * d) span(cx - hw, cx + hw, y, c[1], c[2], c[3], a) end end end local function lin(a, b, lo, hi) if a > -1e-9 and a < 1e-9 then if b >= lo and b <= hi then return -1e9, 1e9 end return 1e9, -1e9 end local x1, x2 = (lo - b) / a, (hi - b) / a if x1 > x2 then return x2, x1 end return x1, x2 end local function capsule_rgb(x0, y0, x1, y1, rad, r, g, b, a) local dx, dy = x1 - x0, y1 - y0 local L2 = dx * dx + dy * dy local L = sqrt(L2) local ya, yb = y0, y1 if ya > yb then ya, yb = yb, ya end local r2 = rad * rad local ady = dy < 0 and -dy or dy local steep = ady > 0.35 * L local ihw, k = 0, 0 if steep then ihw, k = rad * L / ady, dx / dy end local xlo, xhi = (x0 < x1 and x0 or x1) - rad, (x0 > x1 and x0 or x1) + rad for y = floor(ya - rad), ceil(yb + rad) - 1 do local yc = y + 0.5 local lo, hi if steep and yc >= ya and yc <= yb then local xc = x0 + k * (yc - y0) lo, hi = xc - ihw, xc + ihw if lo < xlo then lo = xlo end if hi > xhi then hi = xhi end else lo, hi = 1e9, -1e9 local e = r2 - (yc - y0) * (yc - y0) if e > 0 then e = sqrt(e); lo, hi = x0 - e, x0 + e end e = r2 - (yc - y1) * (yc - y1) if e > 0 then e = sqrt(e) if x1 - e < lo then lo = x1 - e end if x1 + e > hi then hi = x1 + e end end if not steep and L > 0.01 then local pa, pb = lin(dy, -x0 * dy - (yc - y0) * dx, -rad * L, rad * L) local qa, qb = lin(dx, -x0 * dx + (yc - y0) * dy, 0, L2) if qa > pa then pa = qa end if qb < pb then pb = qb end if pb > pa then if pa < lo then lo = pa end if pb > hi then hi = pb end end end end span(lo, hi, y, r, g, b, a) end end local function capsule(x0, y0, x1, y1, rad, c, a) return capsule_rgb(x0, y0, x1, y1, rad, c[1], c[2], c[3], a) end local function lerp3(a, b, f) return floor(a[1] + (b[1] - a[1]) * f + 0.5), floor(a[2] + (b[2] - a[2]) * f + 0.5), floor(a[3] + (b[3] - a[3]) * f + 0.5) end local TOP, MID, LOW = { 34, 86, 72 }, { 132, 178, 158 }, { 56, 112, 88 } local TRUNK, MIST = { 74, 122, 104 }, { 214, 234, 224 } local LEAF, LEAF_L, VINE = { 22, 70, 52 }, { 52, 118, 80 }, { 40, 96, 66 } local BARK, BARK_L, BARK_D = { 98, 72, 52 }, { 138, 106, 74 }, { 62, 46, 36 } local MOSS = { 78, 140, 72 } local FUR, FUR_D, FUR_L = { 150, 114, 84 }, { 106, 78, 56 }, { 184, 148, 106 } local FACE, MASK, EYE = { 226, 208, 172 }, { 58, 42, 36 }, { 238, 234, 218 } local DROP = { 214, 236, 255 } local WING, WING_D = { 90, 160, 255 }, { 50, 100, 220 } local BY = 22 local CX, CY = 28, 35 local GRIP = BY + 2.6 local R = 4 local PC, V = 330, 0 local BG = nil function on_settings() PC = cfg.speed == "slower" and 560 or 330 V = 2 * R / (0.78 * PC) end local function bake_leaves() local s = 0 capsule(-4, -2, 14 + s, 10, 2.8, LEAF); capsule(-4, -2, 13 + s, 9.5, 0.4, LEAF_L) capsule(-3, 4, 10 + s, 20, 2.3, LEAF); capsule(-3, 4, 9.6 + s, 19, 0.35, LEAF_L) capsule(68, -2, 50 - s, 8, 2.8, LEAF); capsule(68, -2, 51 - s, 7.6, 0.4, LEAF_L) capsule(67, 5, 54 - s, 22, 2.3, LEAF); capsule(67, 5, 54.4 - s, 21, 0.35, LEAF_L) capsule(-4, 67, 14 + s, 53, 2.6, LEAF); capsule(-4, 67, 13 + s, 53.6, 0.4, LEAF_L) capsule(-4, 63, 8 + s, 47, 2.1, LEAF); capsule(-4, 63, 7.6 + s, 47.8, 0.35, LEAF_L) capsule(68, 67, 51 - s, 55, 2.6, LEAF); capsule(68, 67, 52 - s, 55.4, 0.4, LEAF_L) capsule(67, 62, 57 - s, 47, 2.1, LEAF); capsule(67, 62, 56.6 - s, 47.8, 0.35, LEAF_L) end local function build_bg() for y = 0, H - 1 do local f = y / (H - 1) if f < 0.55 then rect(0, y, W, 1, lerp3(TOP, MID, f / 0.55)) else rect(0, y, W, 1, lerp3(MID, LOW, (f - 0.55) / 0.45)) end end for i = 1, 7 do local h = hash32(i * 0x9E3779B1) oval(umod(h, 70) - 3, 30 + umod(h >> 8, 14), 9 + umod(h >> 16, 8), 3 + umod(h >> 20, 3), MIST, 0.16) end rect(-2, BY - 2, W + 4, 1, BARK_L[1], BARK_L[2], BARK_L[3]) rect(-2, BY - 1, W + 4, 2, BARK[1], BARK[2], BARK[3]) rect(-2, BY + 1, W + 4, 1, BARK_D[1], BARK_D[2], BARK_D[3]) rect(-2, BY + 2, W + 4, 1, BARK_D[1] - 8, BARK_D[2] - 6, BARK_D[3] - 4) bake_leaves() BG = gfx.snapshot(0, 0, W, H) end local function sx(wx, k, t, period, margin) return (wx - t * V * k) % period - margin end local function draw_far(t) for i = 1, 4 do local x = sx(i * 23 + 5, 0.25, t, 92, 14) local w = 3 + i % 3 blend_rect(floor(x) + 1, 0, w - 1, H, TRUNK[1], TRUNK[2], TRUNK[3], 0.42) local f = x - floor(x) blend_rect(floor(x), 0, 1, H, TRUNK[1], TRUNK[2], TRUNK[3], 0.42 * (1 - f)) blend_rect(floor(x) + w, 0, 1, H, TRUNK[1], TRUNK[2], TRUNK[3], 0.42 * f) end for i = 1, 3 do local x = (i * 41 - t * 0.02) % 120 - 30 oval(x, 36 + (i * 7) % 11, 22, 3.2, MIST, 0.12) end end local function draw_mid(t) for i = 1, 4 do local x = sx(i * 19 + 3, 0.6, t, 78, 10) local len = 14 + (i * 5) % 9 local ix, f = floor(x), x - floor(x) blend_rect(ix, 0, 1, len, VINE[1], VINE[2], VINE[3], 1 - f) blend_rect(ix + 1, 0, 1, len, VINE[1], VINE[2], VINE[3], f) oval(x + 0.3, len, 2.4, 1.2, LEAF_L) oval(x - 1.4, len * 0.55, 2.0, 1.0, LEAF) end end local function draw_moss(t) for i = 1, 4 do local x = sx(i * 17 + 2, 1, t, 68, 10) oval(x, BY - 2.2, 3.2, 1.3, MOSS) oval(x + 7, BY + 2.9, 1.0, 2.4 + (i % 2), MOSS) end for i = 1, 2 do local x = sx(i * 31 + 9, 1, t, 68, 10) oval(x, BY, 1.7, 2.6, BARK_D) end end local PH = { 0, 0.5, 0.25, 0.75 } local BX = { 7, 3, -5, -9 } local SHX = { 6, 3.5, -4, -7 } local function limb(i, t, bx, by) local q = (t / PC + PH[i]) % 1 local gx, gy if q < 0.78 then gx, gy = BX[i] + R * (1 - 2 * q / 0.78), GRIP else local u = (q - 0.78) / 0.22 local e = u * u * (3 - 2 * u) gx, gy = BX[i] - R + 2 * R * e, GRIP + 3.2 * sin(3.1416 * e) end local ax, ay = bx + SHX[i], by - 2.6 local hx = bx + gx local mx, my = (ax + hx) / 2 - 0.9, (ay + gy) / 2 + 1.0 capsule(ax, ay, mx, my, 1.0, FUR_D) capsule(mx, my, hx, gy, 0.85, FUR_D) oval(hx, gy - 0.2, 1.5, 1.2, FUR) px(floor(hx - 1.2), floor(gy + 0.7), 214, 200, 170) px(floor(hx + 0.2), floor(gy + 0.9), 214, 200, 170) end local function sw(w) return w * w * (3 - 2 * w) end local function sloth(t) local bx = CX local by = CY + 0.35 * sin(t * 0.045) limb(3, t, bx, by); limb(4, t, bx, by) oval(bx, by, 9.5, 4.8, FUR_D) oval(bx, by - 0.6, 9.1, 4.1, FUR) oval(bx - 1.0, by - 2.0, 6.2, 1.5, FUR_L) for k = -3, 3 do px(floor(bx + k * 2.4), floor(by + 4.4), FUR_D[1], FUR_D[2], FUR_D[3]) end local turn = sin(t * 6.2832 / 900) local look = (turn > 0.35 and 1 or (turn < -0.35 and -1 or turn / 0.35)) local fx = 1.5 * look local hx, hy = bx + 11.5, by + 3.0 + 0.2 * sin(t * 0.03) oval(hx, hy, 4.6, 4.3, FUR_D) oval(hx, hy - 0.3, 4.3, 4.0, FUR) oval(hx + fx * 0.6, hy + 0.3, 3.3, 3.0, FACE) oval(hx + fx - 1.6, hy - 0.5, 1.5, 1.1, MASK) oval(hx + fx + 1.6, hy - 0.5, 1.5, 1.1, MASK) local shut = (t + 40) % 130 < 6 if not shut then oval(hx + fx - 1.6, hy - 0.5, 0.55, 0.55, EYE) oval(hx + fx + 1.6, hy - 0.5, 0.55, 0.55, EYE) end oval(hx + fx * 1.1, hy + 1.2, 0.8, 0.55, MASK) span(hx + fx * 1.1 - 1.0, hx + fx * 1.1 + 1.0, floor(hy + 2.3), 130, 88, 70) limb(1, t, bx, by); limb(2, t, bx, by) end local DRIPS = { { 14, 10 }, { 10, 20 }, { 50, 8 }, { 54, 22 } } local function draw_rain(t) for i = 1, 4 do local d = DRIPS[i] local tau = ((t + i * 47) % 150) / 30 local y = d[2] + 2 + 22 * tau * tau if tau > 0.15 and y < H then blend_rect(d[1] + (i % 2 == 0 and 1 or 0) - 1, floor(y), 1, 2, DROP[1], DROP[2], DROP[3], 0.8) end end for i = 1, 12 do local h = hash32(i * 0x85EBCA6B) local x = umod(h, W) local y = (umod(h >> 8, 90) + t * (1.6 + umod(h >> 16, 8) * 0.12)) % 90 - 12 blend_rect(x, floor(y), 1, 3, DROP[1], DROP[2], DROP[3], 0.2) end end local function draw_butterfly(t) local u = (t % 780) / 780 if u > 0.55 then return end local w = u / 0.55 local x = -6 + 76 * w local y = 44 - 10 * sin(w * 3.1416) + 2.5 * sin(w * 19) local flap = 0.35 + 0.65 * (0.5 + 0.5 * sin(t * 1.3)) oval(x - 1.7, y - 0.6, 1.9 * flap + 0.3, 1.6, WING) oval(x + 1.7, y - 0.6, 1.9 * flap + 0.3, 1.6, WING) oval(x - 1.2, y + 1.4, 1.2 * flap + 0.2, 1.1, WING_D) oval(x + 1.2, y + 1.4, 1.2 * flap + 0.2, 1.1, WING_D) px(floor(x), floor(y), 40, 30, 36) end function render() if not BG then build_bg() end if frame % 3 ~= 0 then return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame draw_far(t) draw_mid(t) draw_moss(t) sloth(t) draw_rain(t) draw_butterfly(t) end