info = { type = "image", title = "Raccoon bin", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A raccoon raids a bin under a night lamp", api = 33, } local tr = ({ sk = { ["Bin colour"] = "Farba koša", ["Green"] = "Zelená", ["Metal"] = "Kovová" }, cs = { ["Bin colour"] = "Barva koše", ["Green"] = "Zelená", ["Metal"] = "Kovová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bin", label = T("Bin colour"), type = "int", default = "green", opts = { keys = { "green", "metal" }, T("Green"), T("Metal") } }, } local rect, px, blend, blend_rect, clip = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.clip 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 function ease(w) w = w < 0 and 0 or (w > 1 and 1 or w) return w * w * (3 - 2 * w) end local SKY0, SKY1 = { 16, 24, 66 }, { 40, 54, 108 } local WALL, WALL_D, EAVE = { 54, 62, 104 }, { 46, 52, 92 }, { 30, 34, 66 } local FENCE, FENCE_L, TREE = { 46, 42, 70 }, { 60, 54, 86 }, { 22, 34, 60 } local GR0, GR1 = { 62, 84, 78 }, { 34, 48, 54 } local LIGHT, GLASS_ON, GLASS_OFF, FRAME = { 255, 226, 150 }, { 255, 216, 124 }, { 26, 36, 74 }, { 186, 182, 200 } local BINS = { green = { body = { 48, 124, 86 }, dark = { 32, 88, 64 }, lite = { 92, 176, 128 }, lid = { 62, 146, 102 } }, metal = { body = { 132, 142, 158 }, dark = { 92, 100, 118 }, lite = { 196, 206, 220 }, lid = { 156, 166, 182 } }, } local RC, RC_D, RC_L = { 128, 132, 150 }, { 76, 78, 98 }, { 196, 200, 214 } local MASK, TAIL_D = { 30, 28, 40 }, { 58, 58, 74 } local APPLE, APPLE_L, STEM, LEAFC = { 224, 52, 56 }, { 255, 156, 140 }, { 96, 64, 44 }, { 84, 176, 84 } local BIN = BINS.green local BG = nil function on_settings() BIN = BINS[cfg.bin] or BINS.green end local LX, LY = 50, 12 local BX0, BX1, BTOP, GY = 33, 47, 43, 55 local function build_bg() for y = 0, 49 do rect(0, y, W, 1, lerp3(SKY0, SKY1, y / 49)) end for i = 1, 10 do local h = hash32(i * 0x9E3779B1) blend(umod(h, W), umod(h >> 8, 22), 220, 228, 255, 0.5 + umod(h >> 16, 4) * 0.1) end oval(40, 28, 12, 7, TREE); oval(33, 24, 8, 6, TREE); oval(49, 22, 7, 6, TREE) rect(0, 6, 27, 44, WALL[1], WALL[2], WALL[3]) for y = 10, 48, 4 do rect(0, y, 27, 1, WALL_D[1], WALL_D[2], WALL_D[3]) end rect(0, 6, 28, 3, EAVE[1], EAVE[2], EAVE[3]) rect(5, 15, 14, 17, FRAME[1], FRAME[2], FRAME[3]) for x = 27, W - 1, 4 do rect(x, 31, 3, 19, FENCE[1], FENCE[2], FENCE[3]) rect(x + 3, 31, 1, 19, FENCE_L[1], FENCE_L[2], FENCE_L[3]) px(x + 1, 30, FENCE[1], FENCE[2], FENCE[3]) end rect(27, 36, W - 27, 1, FENCE_L[1], FENCE_L[2], FENCE_L[3]) rect(27, 45, W - 27, 1, FENCE_L[1], FENCE_L[2], FENCE_L[3]) for y = 50, H - 1 do rect(0, y, W, 1, lerp3(GR0, GR1, (y - 50) / 13)) end for i = 1, 40 do local h = hash32(i * 0x85EBCA6B) blend(umod(h, W), 51 + umod(h >> 8, 12), 20, 30, 30, 0.3) end for y = LY + 2, 50 do local xc, hw = LX - 6 * (y - LY) / 38, 3 + (y - LY) * 0.5 span(xc - hw, xc + hw, y, LIGHT[1], LIGHT[2], LIGHT[3], 0.07) span(xc - hw * 0.55, xc + hw * 0.55, y, LIGHT[1], LIGHT[2], LIGHT[3], 0.06) end oval(44, 57, 22, 6.5, LIGHT, 0.08); oval(44, 57, 15, 4.6, LIGHT, 0.08); oval(44, 57, 8, 2.8, LIGHT, 0.07) rect(57, 16, 2, 40, 58, 62, 90) capsule(58, 15, LX + 1, LY + 1.5, 0.9, { 58, 62, 90 }) oval(LX, LY + 1.2, 4.2, 1.6, { 36, 40, 64 }) oval(LX, LY + 3.2, 3, 2, LIGHT, 0.3) oval(LX, LY + 3.0, 1.6, 1.2, { 255, 246, 200 }) oval(LX, LY + 4, 6.5, 6.5, LIGHT, 0.09) BG = gfx.snapshot(0, 0, W, H) end local function window(lit) if lit then rect(6, 16, 12, 15, GLASS_ON[1], GLASS_ON[2], GLASS_ON[3]) rect(6, 16, 12, 3, 230, 120, 100) oval(12, 24, 14, 12, LIGHT, 0.07) oval(12, 24, 9, 8, LIGHT, 0.07) oval(10, 56, 17, 5.5, LIGHT, 0.12) oval(10, 56, 11, 3.5, LIGHT, 0.1) else rect(6, 16, 12, 15, GLASS_OFF[1], GLASS_OFF[2], GLASS_OFF[3]) end rect(11, 16, 2, 15, FRAME[1], FRAME[2], FRAME[3]) rect(6, 23, 12, 1, FRAME[1], FRAME[2], FRAME[3]) end local function bin_body() local B = BIN for y = BTOP, GY - 1 do local k = (y - BTOP) / (GY - BTOP) local x0, x1 = BX0 + 0.5 * k, BX1 - 0.5 * k rect(floor(x0), y, floor(x1 - x0) + 1, 1, B.body[1], B.body[2], B.body[3]) rect(floor(x0), y, 2, 1, B.dark[1], B.dark[2], B.dark[3]) rect(floor(x1) - 1, y, 2, 1, B.lite[1], B.lite[2], B.lite[3]) end for _, y in ipairs({ 47, 51 }) do rect(BX0 + 1, y, BX1 - BX0 - 1, 1, B.dark[1], B.dark[2], B.dark[3]) end rect(BX0 - 1, BTOP, BX1 - BX0 + 3, 1, B.lite[1], B.lite[2], B.lite[3]) rect(BX0 + 1, GY, BX1 - BX0 - 1, 1, B.dark[1], B.dark[2], B.dark[3]) end local function lid_angle(u) if u < 115 then return 0 end if u < 165 then return 82 * ease((u - 115) / 50) end if u < 300 then return 82 end local tau, a, v = u - 300, 0.506, 0 local phi = 82 local fall = sqrt(2 * phi / a) if tau < fall then return phi - 0.5 * a * tau * tau end tau = tau - fall v = a * fall for _ = 1, 3 do v = v * 0.36 local air = 2 * v / a if tau < air then return v * tau - 0.5 * a * tau * tau end tau = tau - air end return 0 end local function lid(deg) local B = BIN local r = deg * 0.017453 local hx, hy = BX1 + 0.5, BTOP - 1.2 local ex, ey = hx - 15 * cos(r), hy - 15 * sin(r) capsule(hx, hy, ex, ey, 1.2, B.lid) capsule(hx, hy - 0.5, ex, ey - 0.5, 0.45, B.lite) oval(hx, hy, 1.4, 1.4, B.dark) end local CX, CYB, C, S = 0, 0, 1, 0 local function loc(lx, ly) return CX + lx * C + ly * S, CYB - lx * S + ly * C end local function leg(hx, hy, fx, fy, c) local kx, ky = (hx + fx) / 2 - 1.0, (hy + fy) / 2 capsule(hx, hy, kx, ky, 0.95, c) capsule(kx, ky, fx, fy, 0.85, c) oval(fx + 0.6, fy - 0.3, 1.3, 0.8, c) end local TAILP = { { -4.4, 0.2 }, { -7.2, -1.2 }, { -10, -1.8 }, { -12.8, -1.2 }, { -15, 0.2 } } local function raccoon(cx, G, pitch, ws, gait, t, lean, look, front) local bob = 0 if gait == 2 and ws < 1 then bob = -0.9 * math.abs(sin(t * 0.22)) * (1 - ws) end CX, C, S = cx, cos(pitch), sin(pitch) CYB = G - 5.2 - 1.3 * S + bob local tc, ts = cos(pitch * 0.35), sin(pitch * 0.35) local per, A, lift = 24, 3.2, 1.8 if gait == 2 then per, A, lift = 14, 4.6, 2.6 end local ph = { 0, 0.5, 0.5, 0 } local fxs, fys = {}, {} local hips = { { -3, 2 }, { 3, 2 }, { -2, 2.4 }, { 3.6, 2.4 } } for i = 1, 4 do local hx, hy = loc(hips[i][1], hips[i][2]) local q = (t / per + ph[i]) % 1 local rel, lf if q < 0.6 then rel, lf = A * (1 - 2 * q / 0.6), 0 else local u = (q - 0.6) / 0.4; rel, lf = -A + 2 * A * u, lift * sin(3.1416 * u) end local gx, gy = hx + rel, G - lf local sx_, sy_ if i == 1 or i == 3 then sx_, sy_ = cx - 3 + (i == 3 and 3 or 0), G else sx_, sy_ = 33.4 - (i == 2 and 1.6 or 0), 43.6 + (i == 2 and 0.9 or 0) end fxs[i] = gx + (sx_ - gx) * ws fys[i] = gy + (sy_ - gy) * ws end if front then for i = 2, 4, 2 do local hx, hy = loc(hips[i][1], hips[i][2] - 1) if ws > 0.01 then leg(hx, hy, fxs[i], fys[i], i == 2 and RC_D or RC) end end return end local hx, hy = loc(hips[1][1], hips[1][2]); leg(hx, hy, fxs[1], fys[1], RC_D) if ws < 0.5 then hx, hy = loc(hips[2][1], hips[2][2]); leg(hx, hy, fxs[2], fys[2], RC_D) end local px_, py_ = loc(TAILP[1][1], TAILP[1][2]) for k = 2, 5 do local lx, ly = TAILP[k][1], TAILP[k][2] local nx, ny = CX + lx * tc + ly * ts, CYB - lx * ts + ly * tc capsule(px_, py_, nx, ny, 1.9 - 0.12 * k, (k % 2 == 0) and RC or TAIL_D) px_, py_ = nx, ny end local ax, ay = loc(-3, 0); local bx, by = loc(3, 0) capsule(ax, ay, bx, by, 3.0, RC) ax, ay = loc(-2.4, 1.5); bx, by = loc(2.8, 1.5) capsule(ax, ay, bx, by, 1.4, RC_L) ax, ay = loc(-3, -1.9); bx, by = loc(2.4, -1.9) capsule(ax, ay, bx, by, 0.8, RC_L) hx, hy = loc(hips[3][1], hips[3][2]); leg(hx, hy, fxs[3], fys[3], RC) if ws < 0.5 then hx, hy = loc(hips[4][1], hips[4][2]); leg(hx, hy, fxs[4], fys[4], RC) end local nx, ny = loc(4.2, -0.6) local hx0, hy0 = loc(5.2, -1.6) local hX = hx0 + (37.2 - hx0) * lean local hY = hy0 + (45.6 + 0.8 * sin(t * 0.3) - hy0) * lean hX = hX - 0.5 * look capsule(nx, ny, hX, hY, 2.0, RC) oval(hX, hY, 3.0, 2.6, RC) oval(hX + 2.5, hY + 0.7, 1.9, 1.3, RC_L) oval(hX + 1.2, hY - 0.5, 2.2, 1.0, MASK) px(floor(hX + 1.2 - look * 1.0), floor(hY - 0.8), 240, 240, 236) px(floor(hX + 4.1), floor(hY + 0.3), 20, 18, 26) oval(hX - 1.4, hY - 2.8, 1.2, 1.3, RC_D) oval(hX + 0.9, hY - 3.0, 1.1, 1.3, RC_D) return hX, hY end local function apple(x, y) oval(x, y, 1.7, 1.6, APPLE) px(floor(x - 0.6), floor(y - 0.6), APPLE_L[1], APPLE_L[2], APPLE_L[3]) px(floor(x), floor(y - 2), STEM[1], STEM[2], STEM[3]) px(floor(x + 1), floor(y - 2), LEAFC[1], LEAFC[2], LEAFC[3]) end local PER, OFFS = 470, 40 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame local u = (t + OFFS) % PER local lit = u > 255 and u < 430 and not (u > 258 and u < 261) window(lit) for i = 1, 3 do local a = t * (0.07 + 0.02 * i) + i * 2.1 blend(floor(LX + (3 + i) * cos(a)), floor(LY + 3 + (2 + 0.8 * i) * sin(a)), 255, 250, 220, 0.85) end local cx, G, pitch, ws, gait, lean, look = 0, 55, 0, 0, 1, 0, 0 local show = true if u < 85 then cx = -12 + 38 * u / 85 elseif u < 312 then cx, ws = 26, ease((u - 85) / 12) pitch = 1.15 * ease((u - 85) / 30) if u < 165 then lean = 0 elseif u < 185 then lean = ease((u - 165) / 20) elseif u < 235 then lean = 1 elseif u < 250 then lean = 1 - 0.8 * ease((u - 235) / 15) elseif u < 262 then lean = 0.2 * (1 - ease((u - 250) / 12)) end if u >= 262 and u < 300 then look = ease((u - 262) / 8) end if u >= 300 then ws = 1 - ease((u - 300) / 12) pitch = 1.15 * (1 - ease((u - 300) / 24)) gait, look = 2, 1 - ease((u - 300) / 8) end elseif u < 392 then cx = 26 + 1.15 * (u - 312) G = 55 + 4 * ease((u - 312) / 14) gait, pitch = 2, -0.08 if cx > 80 then show = false end else show = false end local front = u >= 312 local hX, hY if show and not front then hX, hY = raccoon(cx, G, pitch, ws, gait, t, lean, look, false) end bin_body() if show and front then hX, hY = raccoon(cx, G, pitch, ws, gait, t, lean, look, false) end lid(lid_angle(u)) if show and not front and ws > 0.01 then raccoon(cx, G, pitch, ws, gait, t, lean, look, true) end if u >= 235 and show then local ax, ay if u < 255 then local k = ease((u - 235) / 20) ax, ay = 39 + (34.2 - 39) * k, 46 + (40.8 - 46) * k if ay > 42.5 then clip(0, 0, W, BTOP) end elseif u < 300 then ax, ay = 34.2, 40.8 else local k = ease((u - 300) / 10) ax, ay = 34.2 + ((hX or cx) + 3.8 - 34.2) * k, 40.8 + ((hY or G - 6) + 1.0 - 40.8) * k end apple(ax, ay) clip() end end