info = { type = "image", title = "Busy bees", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bees circle a big flower and land on it", api = 33, } local tr = ({ sk = { ["Bees"] = "Včely" }, cs = { ["Bees"] = "Včely" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Bees"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local SKY1, SKY2 = { 108, 184, 232 }, { 150, 212, 242 } local CLOUD = { 244, 250, 255 } local HILL1, HILL1L = { 116, 192, 98 }, { 152, 214, 122 } local HILL2, GRASS = { 70, 152, 70 }, { 44, 118, 52 } local STEMC, STEMD = { 62, 148, 64 }, { 40, 112, 46 } local LEAFC = { { 62, 150, 66 }, { 108, 196, 92 }, { 36, 108, 46 } } local DOT = { 255, 232, 120 } local WING = { 255, 255, 255 } local FX, FY, FR = 32, 28, 12 local FN = 2 * FR + 1 local FLOWER local FPAL = { 0, 0, 0, 252, 202, 56, 216, 132, 28, 150, 84, 24, 248, 112, 152, 255, 184, 210, 176, 50, 98 } do local g = {} for j = -FR, FR do for i = -FR, FR do local r = sqrt(i * i + j * j) local a = math.atan(j, i) local lobe = abs(cos(4.5 * a)) local v = 0 if r <= 4.3 then if r > 3.2 then v = 2 elseif (hash32((i + 20) * 0x9E3779B1 + (j + 20) * 0x85EBCA6B) & 7) == 0 then v = 3 else v = 1 end elseif r <= 5.6 + 4.6 * lobe then if lobe < 0.13 and r > 5.2 then v = 6 elseif r > 7.6 then v = 5 else v = 4 end end g[(j + FR) * FN + i + FR + 1] = v end end local row, rows = {}, {} for j = 0, FN - 1 do for i = 0, FN - 1 do local k = j * FN + i + 1 local v = g[k] if v == 0 and ((i > 0 and g[k - 1] > 0) or (i < FN - 1 and g[k + 1] > 0) or (j > 0 and g[k - FN] > 0) or (j < FN - 1 and g[k + FN] > 0)) then v = 6 end row[i + 1] = v end rows[j + 1] = string.char(table.unpack(row, 1, FN)) end FLOWER = table.concat(rows) end local SW, SH = 16, 12 local AXR, AXL, AY = 7, 9, 6 local B_PAL = { 0, 0, 0, 28, 20, 14, 252, 208, 44, 226, 146, 24, 34, 26, 20, 74, 52, 34, 255, 255, 255 } local BEE_R, BEE_L do local c = {} for j = 0, SH - 1 do for i = 0, SW - 1 do local x, y = i + 0.5, j + 0.5 local v = 0 local dx, dy = (x - 7.0) / 4.6, (y - 6.5) / 3.3 if dx * dx + dy * dy <= 1 then if i == 5 or i == 8 then v = 4 elseif dy > 0.42 then v = 3 else v = 2 end end local hx, hy = x - 11.8, y - 5.6 if hx * hx + hy * hy <= 6.25 then v = 5 end c[j * SW + i + 1] = v end end local o = {} for j = 0, SH - 1 do for i = 0, SW - 1 do local k = j * SW + i + 1 local v = c[k] if v == 0 and ((i > 0 and c[k - 1] > 0) or (i < SW - 1 and c[k + 1] > 0) or (j > 0 and c[k - SW] > 0) or (j < SH - 1 and c[k + SW] > 0)) then v = 1 end o[k] = v end end local function set(i, j, v) o[j * SW + i + 1] = v end set(0, 6, 1) set(12, 4, 6) set(13, 1, 1); set(14, 0, 1) set(6, 11, 1); set(8, 11, 1) local rr, rl, row = {}, {}, {} for j = 0, SH - 1 do for i = 0, SW - 1 do row[i + 1] = o[j * SW + i + 1] end rr[j + 1] = string.char(table.unpack(row, 1, SW)) for i = 0, SW - 1 do row[i + 1] = o[j * SW + (SW - 1 - i) + 1] end rl[j + 1] = string.char(table.unpack(row, 1, SW)) end BEE_R, BEE_L = table.concat(rr), table.concat(rl) end local WING_UP = { { 4, 0, 3 }, { 3, 1, 5 }, { 4, 2, 3 }, { 7, -1, 3 }, { 6, 0, 5 }, { 7, 1, 4 } } local WING_DN = { { 1, 3, 4 }, { 2, 4, 2 }, { 4, 2, 5 }, { 5, 3, 3 } } local CYC = 15 local SPX, SPY = 32, 20 local BEES = { { cx = 32, cy = 27, ax = 23, ay = 10, per = 3.6, ph = 0.0, dir = 1, off = 3.73, land = 1 }, { cx = 32, cy = 30, ax = 19, ay = 13, per = 4.2, ph = 2.2, dir = -1, off = 8.73, land = -1 }, { cx = 32, cy = 24, ax = 25, ay = 8, per = 3.2, ph = 4.1, dir = 1, off = 13.73, land = 1 }, } for i = 1, #BEES do BEES[i].w = TAU / BEES[i].per end local function smooth(u) return u * u * (3 - 2 * u) end local function bee_pos(b, t) local s = b.w * t + b.ph local x = b.cx + b.dir * b.ax * sin(s) local y = b.cy + b.ay * sin(2 * s) local tau = (t - b.off) % CYC local m = 0 if tau < 1 then m = smooth(tau) elseif tau < 3.5 then m = 1 elseif tau < 4.5 then m = 1 - smooth(tau - 3.5) end if m > 0 then x = x + (SPX - x) * m; y = y + (SPY - y) * m end return x, y, m end local function cloud(x, y, w) rect(x + 2, y, w - 5, 1, c3(CLOUD)) rect(x, y + 1, w, 2, c3(CLOUD)) rect(x + 1, y + 3, w - 3, 1, c3(CLOUD)) end local function leaf(bx, by, ang, L, Wd) local dx, dy = cos(ang), sin(ang) local litpos = 0.6 * dy - 0.8 * dx > 0 local r = floor(L) + 2 for y = floor(by) - r, floor(by) + r do for x = floor(bx) - r, floor(bx) + r do local qx, qy = x + 0.5 - bx, y + 0.5 - by local u = qx * dx + qy * dy if u > 0 and u < L then local v = -qx * dy + qy * dx local w = Wd * sin(3.14159 * u / L) if v <= w and v >= -w then local c = (v > -0.5 and v < 0.5 and u < L - 2) and 3 or (((v > 0) == litpos) and 2 or 1) px(x, y, c3(LEAFC[c])) end end end end end local function minifl(x, y, r, g, b) px(x, y, 252, 206, 60) px(x - 1, y, r, g, b); px(x + 1, y, r, g, b); px(x, y - 1, r, g, b); px(x, y + 1, r, g, b) end local BG = nil local function build_bg() rect(0, 0, W, H, c3(SKY1)) rect(0, 34, W, H - 34, c3(SKY2)) for x = 0, W - 1, 2 do px(x, 33, c3(SKY2)) end cloud(3, 7, 15); cloud(46, 12, 14) for x = 0, W - 1 do local top = floor(46.5 + 3 * sin(x * 0.08 + 0.6)) rect(x, top, 1, H - top, c3(HILL1)) px(x, top, c3(HILL1L)) end for y = 31, H - 1 do local x = floor(FX + 0.5 + 1.2 * sin((y - 31) * 0.11)) rect(x - 1, y, 2, 1, c3(STEMC)) px(x, y, c3(STEMD)) end leaf(31, 45, 3.55, 10, 3.0); leaf(33, 51, -0.45, 9, 2.6) for x = 0, W - 1 do local top = floor(55.5 + 1.6 * sin(x * 0.17 + 2.0)) rect(x, top, 1, H - top, c3(HILL2)) if (hash32(x * 0x9E3779B1) & 3) == 0 then px(x, top - 1, c3(HILL2)) end if (hash32(x * 0x85EBCA6B) & 3) == 0 then px(x, top + 2 + x % 3, c3(GRASS)) end end minifl(7, 58, 255, 184, 210); minifl(19, 61, 255, 255, 255) minifl(47, 60, 255, 184, 210); minifl(57, 57, 255, 255, 255) blit(FLOWER, FPAL, FX - FR, FY - FR, FN, FN, FN, FN, true) BG = gfx.snapshot(0, 0, W, H) end local NB = 2 function on_settings() NB = cfg.n or 2 if NB < 1 then NB = 1 elseif NB > 3 then NB = 3 end end local function wings(rows, sx, sy, right, a) for k = 1, #rows do local r = rows[k] local x = right and r[1] or (SW - r[1] - r[3]) blend_rect(sx + x, sy + r[2], r[3], 1, WING[1], WING[2], WING[3], a) end end local function draw_bee(b, t) local base = floor(t * 10) / 10 local x, y, m = bee_pos(b, t) for k = 1, 9 do local dx, dy, dm = bee_pos(b, base - 0.1 * k) if dm < 0.5 then local ex, ey = dx - x, dy - y if ex * ex + ey * ey > 9 then blend(floor(dx + 0.5), floor(dy + 0.5), DOT[1], DOT[2], DOT[3], 1.0 - k * 0.08) end end end local right if m >= 0.5 then right = b.land > 0 else local x1 = bee_pos(b, t + 1 / FPS) local x0 = bee_pos(b, t - 1 / FPS) right = x1 >= x0 end local ix, iy = floor(x + 0.5), floor(y + 0.5) local sx, sy = ix - (right and AXR or AXL), iy - AY blit(right and BEE_R or BEE_L, B_PAL, sx, sy, SW, SH, SW, SH, true) if m >= 0.99 then local fl = t % 0.8 < 0.133 and floor(t * 30) % 2 == 0 wings(fl and WING_UP or WING_DN, sx, sy, right, 0.6) local h = hash32(floor(t * 6) * 0x9E3779B1 + floor(b.off * 30)) px(SPX - 5 + (h & 7) + ((h >> 3) & 3), SPY + 3 + ((h >> 6) & 1), 255, 226, 80) else wings((floor(t * 30) % 2 == 0) and WING_UP or WING_DN, sx, sy, right, 0.8) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS for i = 1, NB do draw_bee(BEES[i], t) end end