info = { type = "image", title = "Pollen bot", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A robot bee pollinates garden flowers", api = 33, } local tr = ({ sk = { ["Petals"] = "Lupene", ["Pace"] = "Tempo", ["Slow"] = "Pomalé", ["Normal"] = "Bežné", ["Quick"] = "Rýchle" }, cs = { ["Petals"] = "Lístky", ["Pace"] = "Tempo", ["Slow"] = "Pomalé", ["Normal"] = "Běžné", ["Quick"] = "Rychlé" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "petal", label = T("Petals"), type = "color", default = "#F0609C" }, { key = "pace", label = T("Pace"), type = "int", default = 1, opts = { T("Slow"), T("Normal"), T("Quick") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, exp, abs = math.floor, math.sin, math.cos, math.exp, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local PI = 3.14159 local SKY0, SKY1 = { 124, 192, 240 }, { 212, 236, 246 } local STEM, STEM_L = { 52, 128, 50 }, { 98, 178, 72 } local CENTER, CENTER_L = { 250, 206, 70 }, { 255, 246, 176 } local COL = { { 244, 196, 52 }, { 54, 58, 82 }, { 152, 166, 192 }, { 210, 220, 236 }, { 255, 72, 72 } } local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local FL = { { 12, 12, 34 }, { 32, 32, 26 }, { 52, 52, 36 } } local BASE_Y = 62 local BG local function bake() for y = 0, 49 do rect(0, y, W, 1, mix(SKY0, SKY1, y / 49)) end for x = 0, W - 1, 2 do local h = 6 + floor(3 * sin(x * 0.11) + 2 * sin(x * 0.27 + 1) + 0.5) rect(x, 46 - h, 2, h + 4, 176, 210, 154) end for x = 0, W - 1 do local h = 3 + (hash32(x // 3 * 0x9E3779B1) % 3) rect(x, 50 - h, 1, h + 1, 106, 172, 94) end for y = 50, H - 1 do rect(0, y, W, 1, mix({ 98, 172, 66 }, { 66, 136, 48 }, (y - 50) / 13)) end for i = 1, 40 do local v = hash32(i * 0x85EBCA6B) rect(v % W, 51 + (v >> 8) % 12, 1, 2, 142, 202, 86) end for i = 1, 7 do local v = hash32(i * 0x27D4EB2F) local x, y = v % 58 + 2, 54 + (v >> 8) % 8 px(x, y, 255, 255, 255); px(x - 1, y, 240, 244, 250); px(x + 1, y, 240, 244, 250) px(x, y - 1, 240, 244, 250); px(x, y + 1, 240, 244, 250); px(x, y, 250, 206, 70) end for _, f in ipairs(FL) do local bx = f[1] rect(bx + 2, 53, 4, 1, 76, 156, 60); rect(bx + 5, 52, 2, 1, 76, 156, 60); rect(bx + 2, 54, 2, 1, 52, 128, 50) rect(bx - 5, 57, 4, 1, 76, 156, 60); rect(bx - 6, 56, 2, 1, 76, 156, 60); rect(bx - 4, 58, 2, 1, 52, 128, 50) end BG = gfx.snapshot(0, 0, W, H) end local POSE = { [0] = { { -3, -1, 1 }, { -2, -2, 2 }, { -1, -2, 2 }, { 0, -2, 2 }, { 1, -2, 2 }, { 2, -1, 1 } }, [1] = { { -4, 0, 0 }, { -3, -1, 1 }, { -2, -2, 2 }, { -1, -2, 2 }, { 0, -2, 2 }, { 1, -2, 2 }, { 2, -1, 1 } }, [2] = { { -3, -1, 1 }, { -2, -2, 2 }, { -1, -3, 3 }, { 0, -3, 3 }, { 1, -3, 3 }, { 2, -2, 2 }, { 3, -1, 1 } }, [3] = { { -4, -1, 1 }, { -3, -3, 3 }, { -2, -4, 4 }, { -1, -4, 4 }, { 0, -4, 4 }, { 1, -4, 4 }, { 2, -4, 4 }, { 3, -3, 3 }, { 4, -1, 1 } }, } local PR, PG, PB = 240, 96, 156 local function head(cx, cy, pose, g) local r, gg, b = mix({ PR, PG, PB }, { 255, 214, 232 }, g * 0.6) local rows = POSE[pose] for i = 1, #rows do local q = rows[i] if q[1] >= 2 then rect(cx + q[2], cy + q[1], q[3] - q[2] + 1, 1, floor(r * 0.8), floor(gg * 0.8), floor(b * 0.8)) else rect(cx + q[2], cy + q[1], q[3] - q[2] + 1, 1, r, gg, b) end end if pose >= 2 then local c = g > 0.5 and CENTER_L or CENTER rect(cx - 1, cy - 1, 3, 3, c[1], c[2], c[3]) if pose == 3 then local d = floor(r * 0.8) px(cx - 3, cy - 2, d, floor(gg * 0.8), floor(b * 0.8)); px(cx + 3, cy - 2, d, floor(gg * 0.8), floor(b * 0.8)) px(cx - 3, cy + 2, d, floor(gg * 0.8), floor(b * 0.8)); px(cx + 3, cy + 2, d, floor(gg * 0.8), floor(b * 0.8)) end elseif pose == 1 then px(cx, cy - 2, CENTER[1], CENTER[2], CENTER[3]) else rect(cx - 2, cy + 2, 5, 1, STEM[1], STEM[2], STEM[3]); px(cx, cy + 3, STEM[1], STEM[2], STEM[3]) end end local function stem(bx, hx, hy, ox) local top = hy + 5 local x0, y0 = nil, nil for y = top, BASE_Y do local s = (BASE_Y - y) / (BASE_Y - top) local x = bx + floor(ox * s * s + 0.5) if x ~= x0 then if x0 then rect(x0, y0, 1, y - y0, STEM_L[1], STEM_L[2], STEM_L[3]); rect(x0 + 1, y0, 1, y - y0, STEM[1], STEM[2], STEM[3]) end x0, y0 = x, y end end rect(x0, y0, 1, BASE_Y + 1 - y0, STEM_L[1], STEM_L[2], STEM_L[3]) rect(x0 + 1, y0, 1, BASE_Y + 1 - y0, STEM[1], STEM[2], STEM[3]) end local function cov(x, y, w, h, r, g, b, a) if a > 0.05 and w > 0 and h > 0 then if a > 0.97 then rect(x, y, w, h, r, g, b) else blend_rect(x, y, w, h, r, g, b, a) end end end local function frect(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = floor((x - ix) * 4) / 4, floor((y - iy) * 4) / 4 cov(ix + 1, iy + 1, w - 1, h - 1, r, g, b, 1) cov(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx); cov(ix + w, iy + 1, 1, h - 1, r, g, b, fx) cov(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy); cov(ix + 1, iy + h, w - 1, 1, r, g, b, fy) cov(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)); cov(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) cov(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy); cov(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end local BEE = { { -5, 0, 1, 1, 2 }, { -4, -1, 1, 3, 1 }, { -3, -1, 1, 3, 2 }, { -2, -1, 1, 3, 1 }, { -1, -1, 1, 3, 2 }, { 0, -1, 3, 3, 3 }, { 0, -1, 3, 1, 4 }, { 3, -1, 2, 3, 3 }, { 4, -1, 1, 1, 5 }, { 4, -2, 1, 1, 2 }, { 5, -3, 1, 1, 2 }, { -1, -2, 1, 1, 2 }, { 2, -2, 1, 1, 2 }, } local BLADE = { 9, 5, 7, 3 } local function bee(x, y, dir, landed) for i = 1, #BEE do local q = BEE[i] local c = COL[q[5]] frect(dir > 0 and x + q[1] or x - q[1] - q[3] + 1, y + q[2], q[3], q[4], c[1], c[2], c[3]) end local lh = landed and 2 or 1 local c = COL[2] frect(dir > 0 and x - 1 or x + 1, y + 2, 1, lh, c[1], c[2], c[3]) frect(dir > 0 and x + 1 or x - 1, y + 2, 1, lh, c[1], c[2], c[3]) local ix, iy = floor(x + 0.5), floor(y + 0.5) local ph = landed and frame // 5 or frame for k = 0, 1 do local len = BLADE[(ph + k * 2) % 4 + 1] local mx = k == 0 and 2 or -1 blend_rect((dir > 0 and ix + mx or ix - mx) - len // 2, iy - 3, len, 1, 196, 214, 232, landed and 0.35 or 0.65) end if frame % 30 < 20 then px(dir > 0 and ix + 1 or ix - 1, iy, 120, 244, 255) end end local CYC0 = 420 local function smooth(s) s = s < 0 and 0 or (s > 1 and 1 or s); return s * s * (3 - 2 * s) end local function hover(i, bob) local f = FL[i] local b = bob and 1 or 0 return f[2] + 0.8 * b * sin(frame * 0.13), f[3] - 12 + 1.2 * b * sin(frame * 0.2) end local SPARK = {} for i = 1, 6 do local v = hash32(i * 0x9E3779B1) SPARK[i] = { v % 17 - 8, (v >> 8) % 15 - 8, (v >> 16) % 60 } end local function spark(x, y, a) blend(x, y, 255, 255, 230, a) blend(x - 1, y, 255, 250, 200, a * 0.6); blend(x + 1, y, 255, 250, 200, a * 0.6) blend(x, y - 1, 255, 250, 200, a * 0.6); blend(x, y + 1, 255, 250, 200, a * 0.6) end local function dot(x, y, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 255, 226, 96, a * (1 - fx) * (1 - fy)); blend(ix + 1, iy, 255, 226, 96, a * fx * (1 - fy)) blend(ix, iy + 1, 255, 226, 96, a * (1 - fx) * fy); blend(ix + 1, iy + 1, 255, 226, 96, a * fx * fy) end local function cloud(x, y, a) local ix = floor(x) local f = x - ix blend_rect(ix + 3, y, 8, 1, 255, 255, 255, a); blend_rect(ix + 1, y + 1, 13, 2, 255, 255, 255, a) blend_rect(ix, y + 1, 1, 2, 255, 255, 255, a * (1 - f)); blend_rect(ix + 14, y + 1, 1, 2, 255, 255, 255, a * f) end function render() if not BG then bake() end local c = cfg.petal if c then PR, PG, PB = c.r, c.g, c.b end local CYC = ({ 560, 420, 320 })[(cfg.pace or 1) + 1] local n = (frame // CYC) % 3 local t = (frame % CYC) * CYC0 / CYC gfx.blit_rgb(BG, 0, 0, W, H) cloud((frame * 0.04) % 80 - 16, 6, 0.55); cloud((40 + frame * 0.025) % 80 - 16, 15, 0.4) local w = 0 if t >= 30 and t < 180 then w = 1 - exp(-(t - 30) * 0.14) * cos((t - 30) * 0.4) elseif t >= 180 then w = exp(-(t - 180) * 0.1) * cos((t - 180) * 0.35) end local ox, oy = floor(1.2 * w + 0.5), floor(3 * w + 0.5) local pos = {} for i = 1, 3 do local f = FL[i] local r = (i - 1 - n) % 3 local pose, g, dx, dy = 0, 0, 0, 0 if r == 0 then pose = t < 300 and 3 or (t < 340 and 3 - floor((t - 300) / 10) or 0) g = t < 130 and 0 or (t < 300 and smooth((t - 130) / 20) or smooth(1 - (t - 300) / 25)) dx, dy = ox, oy elseif r == 1 then pose = t < 190 and 0 or (t < 230 and floor((t - 190) / 10) or 3) end stem(f[1], f[2], f[3] + dy, dx) head(f[2] + dx, f[3] + dy, pose, g) pos[i] = { f[2] + dx, f[3] + dy } end local cur = n + 1 local nxt = (n + 1) % 3 + 1 local prv = (n + 2) % 3 + 1 local c0 = pos[cur] local dirp = FL[cur][2] >= FL[prv][2] and 1 or -1 local dirn = FL[nxt][2] >= FL[cur][2] and 1 or -1 local bx, by, dir, landed if t < 30 then local hx, hy = hover(cur, true) local e = smooth(t / 30) bx, by, dir = hx + (c0[1] - hx) * e, hy + (c0[2] - 5 - hy) * e, dirp elseif t < 180 then bx, by, dir, landed = c0[1], c0[2] - 5 + 0.5 * sin(frame * 0.5), dirp, true elseif t < 225 then local hx, hy = hover(cur, true) local e = smooth((t - 180) / 45) bx, by, dir = c0[1] + (hx - c0[1]) * e, c0[2] - 5 + (hy - c0[2] + 5) * e, dirn elseif t < 375 then local s = (t - 225) / 150 local e = smooth(s) local ax, ay = hover(cur, false) local zx, zy = hover(nxt, false) local k = sin(PI * s) bx = ax + (zx - ax) * e + 6 * sin(2 * PI * s) * k * dirn by = ay + (zy - ay) * e - 4 * k - 3 * sin(4 * PI * s) * k dir = dirn else bx, by = hover(nxt, true) dir = dirn end if t >= 45 and t < 175 then for j = 0, 9 do local a = t - 45 - j * 12 if a >= 0 and a < 55 then local v = hash32(j * 0x27D4EB2F + n) local vx = (v % 7 - 3) * 0.09 dot(c0[1] + vx * a + (v >> 8) % 3 - 1, c0[2] - 3 - 0.55 * a + 0.014 * a * a, 1 - a / 55) end end end for i = 1, 6 do local s = SPARK[i] local a = t - 135 - s[3] if a >= 0 and a < 40 then spark(c0[1] + s[1], c0[2] + s[2] - 2, sin(PI * a / 40)) end end if t >= 195 and t < 240 then local p = pos[nxt] for i = 1, 3 do local s = SPARK[i] local a = t - 195 - i * 8 if a >= 0 and a < 18 then spark(p[1] + s[1] // 2, p[2] + s[2] // 2, sin(PI * a / 18)) end end end bee(bx, by, dir, landed) end