info = { type = "image", title = "Honeycomb", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bees crawl over a honeycomb and seal a cell", api = 44, } local tr = ({ sk = { ["Bees"] = "Včely", ["Bee speed"] = "Rýchlosť včiel", ["Honey"] = "Med", ["Golden"] = "Zlatý", ["Amber"] = "Jantárový", ["Acacia"] = "Agátový" }, cs = { ["Bees"] = "Včely", ["Bee speed"] = "Rychlost včel", ["Honey"] = "Med", ["Golden"] = "Zlatý", ["Amber"] = "Jantarový", ["Acacia"] = "Akátový" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bees", label = T("Bees"), type = "int", min = 0, max = 4, default = 4 }, { key = "speed", label = T("Bee speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "honey", label = T("Honey"), type = "int", default = "golden", opts = { keys = { "golden", "amber", "acacia" }, T("Golden"), T("Amber"), T("Acacia") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, ceil, sin, sqrt, abs, max, min = math.floor, math.ceil, math.sin, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TONES = { golden = { { 226, 140, 24 }, { 250, 178, 50 }, { 252, 190, 30 }, { 214, 124, 12 }, { 255, 232, 120 } }, amber = { { 176, 92, 16 }, { 224, 138, 40 }, { 228, 138, 18 }, { 172, 84, 8 }, { 252, 196, 90 } }, acacia = { { 232, 170, 60 }, { 252, 212, 110 }, { 255, 216, 80 }, { 226, 160, 30 }, { 255, 246, 170 } }, } local WAX_RIM, WAX, WAX_HI = { 200, 160, 92 }, { 234, 202, 138 }, { 252, 232, 180 } local GOLD, GOLD_D, GOLD_HI = { 255, 200, 40 }, { 206, 112, 10 }, { 255, 240, 150 } local TN local function c3(c) return c[1], c[2], c[3] end local CELLS = {} for j = 0, 6 do for i = 0, 5 do local cx = (j % 2 == 0 and 8 or 2) + 12 * i if cx < W + 6 then local h = hash32((j * 7 + i) * 0x9E3779B1 + 3) local k = umod(h >> 8, 10) CELLS[#CELLS + 1] = { cx, 4 + 10 * j, k < 6 and 1 or (k < 8 and 2 or 3) } end end end local SX, SY = 32, 24 local HXR = {} local function hexrows(e) local rows = HXR[e] if not rows then rows = {} local wh = 5 + e local R = wh / 0.866 for dy = -ceil(R), ceil(R) do local hw = floor(min(wh, (R - abs(dy)) * 1.732) + 0.5) if hw > 0 then rows[#rows + 1] = { dy, hw } end end HXR[e] = rows end return rows end local function hex(cx, cy, e, r, g, b) local rows = hexrows(e) for i = 1, #rows do local q = rows[i] rect(cx - q[2], cy + q[1], 2 * q[2], 1, r, g, b) end end local CW = 13 local CELLG = {} local function cell_sprite(st) local g = {} for i = 1, CW * CW do g[i] = 0 end local function mark(cx, cy, e, idx) local rows = hexrows(e) for i = 1, #rows do local q = rows[i] for x = cx - q[2], cx + q[2] - 1 do g[(cy + q[1]) * CW + x + 1] = idx end end end local function box(x, y, w, h, idx) for j = y, y + h - 1 do for i = x, x + w - 1 do g[j * CW + i + 1] = idx end end end mark(6, 6, 1, 1) if st == 1 then mark(7, 7, 0, 2); mark(6, 6, -1, 3) box(3, 4, 3, 1, 4); box(3, 5, 1, 2, 4); box(4, 5, 1, 1, 5) elseif st == 2 then mark(7, 7, 0, 6); mark(8, 8, -1, 7); box(7, 7, 3, 2, 8) else mark(7, 7, 0, 9); mark(6, 6, -1, 10) box(3, 4, 3, 1, 11); box(3, 5, 1, 2, 11); box(6, 7, 2, 1, 9) end return char(unpack(g)) end local PALC, BG, BGS, BST = nil, nil, nil, 1 local function build_bg() if BST == 1 then rect(0, 0, W, H, c3(TN[1])) else blit_rgb(BGS, 0, 0, W, H) end local half = (#CELLS + 1) // 2 for i = (BST - 1) * half + 1, min(#CELLS, BST * half) do local c = CELLS[i] blit_indexed(CELLG[c[3]], PALC, c[1] - 7, c[2] - 7, CW, CW, CW, CW, true) end BGS = snapshot(0, 0, W, H) BST = BST + 1 if BST > 2 then BG = BGS end end function on_settings() TN = TONES[cfg.honey] or TONES.golden PALC = { 0, 0, 0 } for _, c in ipairs({ TN[2], TN[4], TN[3], TN[5], { 255, 252, 226 }, { 168, 96, 12 }, { 92, 44, 6 }, { 70, 32, 4 }, WAX_RIM, WAX, WAX_HI }) do PALC[#PALC + 1], PALC[#PALC + 2], PALC[#PALC + 3] = c[1], c[2], c[3] end if not CELLG[1] then for st = 1, 3 do CELLG[st] = cell_sprite(st) end end BG, BST = nil, 1 end local PAL, NP, PK, SPR = { 0, 0, 0 }, 0, {}, {} local function pc(r, g, b) r, g, b = min(255, floor((r + 4) / 8) * 8), min(255, floor((g + 4) / 8) * 8), min(255, floor((b + 4) / 8) * 8) local k = r * 65536 + g * 256 + b local i = PK[k] if i then return i end if NP >= 250 then return NP end NP = NP + 1 PAL[NP * 3 + 1], PAL[NP * 3 + 2], PAL[NP * 3 + 3] = r, g, b PK[k] = NP return NP end local function sdell(x, y, cx, cy, rx, ry) local u, v = (x - cx) / rx, (y - cy) / ry return (sqrt(u * u + v * v) - 1) * (rx < ry and rx or ry) end local function cover(d) return d <= -0.5 and 1 or (d >= 0.5 and 0 or 0.5 - d) end local function frame_of(h, lx, ly) if h == 1 then return lx, ly elseif h == 2 then return -lx, ly elseif h == 3 then return -ly, lx end return ly, lx end local BR, BG_, BB, BA = 0, 0, 0, 0 local function put(c, cr, cg, cb) if c > 0 then BR, BG_, BB = BR * (1 - c) + cr * c, BG_ * (1 - c) + cg * c, BB * (1 - c) + cb * c BA = BA + (1 - BA) * c end end local BASEB, BQ = {}, 1 local BQUEUE = { 10, 11, 20, 21, 30, 31, 40, 41 } local BPART = {} local function bake_bee(key, y0, y1) local h, pose = key // 10, key % 10 local out = BPART[key] or {} BPART[key] = out for y = y0, y1 do for x = 0, 10 do local lx, ly = x + 0.5 - 5.5, y + 0.5 - 5.5 local u, v = frame_of(h, lx, ly) BR, BG_, BB, BA = 0, 0, 0, 0 local su, sv = frame_of(h, lx - 1.2, ly - 1.2) put(cover(sdell(su, sv, -1, 0, 5, 2.3)) * 0.34, 96, 52, 6) local av = abs(v) local ab = cover(sdell(u, v, -2.3, 0, 3.3, 2.3)) if ab > 0 then local k = floor((-u - 0.8) / 1.5) local shade = 1 - 0.35 * (av / 2.3) ^ 2 if k % 2 == 0 then put(ab, 238 * shade, 168 * shade, 28 * shade) else put(ab, 46, 26, 8) end end put(cover(sdell(u, v, 1.2, 0, 2.0, 2.1)), v < 0 and 238 or 204, v < 0 and 176 or 132, v < 0 and 52 or 26) put(cover(sdell(u, v, 3.7, 0, 1.6, 1.7)), 58, 34, 14) if pose == 1 then local d = sdell(u, av, -0.6, 3.0, 3.2, 1.4) local w1 = cover(d) put(w1, 250, 238, 206) put(cover(d + 1) * 0.35 * (1 - w1), 188, 160, 112) else put(cover(sdell(u, av, -1.8, 1.0, 3.6, 0.95)) * 0.3, 250, 238, 206) end local i = (y * 11 + x) * 4 if BA > 0 then out[i + 1], out[i + 2], out[i + 3], out[i + 4] = floor(BR / BA + 0.5), floor(BG_ / BA + 0.5), floor(BB / BA + 0.5), floor(BA * 255 + 0.5) else out[i + 1], out[i + 2], out[i + 3], out[i + 4] = 0, 0, 0, 0 end end end if y1 == 10 then BASEB[key] = char(unpack(out)); BPART[key] = nil end end local function derive(key, ph) local base = BASEB[key] local vert = key >= 30 local f = ph / 4 local ow, oh = vert and 11 or 12, vert and 12 or 11 local rows = {} for y = 0, oh - 1 do local row = {} for x = 0, ow - 1 do local a0, a1, r0, g0, b0, r1, g1, b1 = 0, 0, 0, 0, 0, 0, 0, 0 if x < 11 and y < 11 then local i = (y * 11 + x) * 4 + 1; r0, g0, b0, a0 = string.byte(base, i, i + 3) end local sx, sy = vert and x or x - 1, vert and y - 1 or y if sx >= 0 and sy >= 0 then local i = (sy * 11 + sx) * 4 + 1; r1, g1, b1, a1 = string.byte(base, i, i + 3) end local w0, w1 = (1 - f) * a0 / 255, f * (a1 or 0) / 255 local A = w0 + w1 if A > 0.12 then local r, g, b = (w0 * r0 + w1 * r1) / A, (w0 * g0 + w1 * g1) / A, (w0 * b0 + w1 * b1) / A row[x + 1] = pc(232 + (r - 232) * A, 150 + (g - 150) * A, 26 + (b - 26) * A) else row[x + 1] = 0 end end rows[y + 1] = char(unpack(row)) end SPR[key * 10 + ph] = concat(rows) end local DERIVED = 0 local function draw_bee(x, y, h, pose) local key = h * 10 + pose if not BASEB[key] then return end local xi, yi = floor(x), floor(y) local ph = floor(((h <= 2 and x - xi or y - yi)) * 4) local s = SPR[key * 10 + ph] if not s then if DERIVED >= 2 then return end DERIVED = DERIVED + 1 derive(key, ph) s = SPR[key * 10 + ph] end local vert = h >= 3 local w, hh = vert and 11 or 12, vert and 12 or 11 blit_indexed(s, PAL, xi - 5, yi - 5, w, hh, w, hh, true) end local function walk(s, v, move, pause) local cyc = move + pause local n, u = floor(s / cyc), s % cyc return v * (n * move + min(u, move)), u < move end local function flutter(frm, period) return (frm // period) % 2 end local function drip(x, s) local u = s % 6.5 if u < 5.0 then local L = 3 + min(u, 4.5) / 4.5 * 11 local Li = floor(L) local thin = u > 4.5 if thin then rect(x, 0, 1, Li + 2, c3(GOLD)); rect(x - 1, 0, 1, 4, c3(GOLD_D)); rect(x + 1, 0, 1, 4, c3(GOLD_D)) else rect(x - 1, 0, 1, Li, c3(GOLD_D)); rect(x + 2, 0, 1, Li, c3(GOLD_D)); rect(x, 0, 2, Li, c3(GOLD)) rect(x, 0, 1, Li, c3(GOLD_HI)); blend_rect(x, Li, 2, 1, GOLD[1], GOLD[2], GOLD[3], L - Li) end local by = Li + (thin and 2 or 0) rect(x - 1, by, 4, 2, c3(GOLD_D)); rect(x, by - 1, 2, 1, c3(GOLD)); rect(x - 1 + 1, by, 3, 2, c3(GOLD)) rect(x, by + 2, 2, 1, c3(GOLD_D)); px(x, by, c3(GOLD_HI)) else rect(x - 1, 0, 1, 4, c3(GOLD_D)); rect(x + 2, 0, 1, 4, c3(GOLD_D)); rect(x, 0, 2, 4, c3(GOLD)) if u < 6.2 then local t = u - 5.0 local y = floor(16 + 50 * t * t) blend_rect(x, y - 5, 2, 4, GOLD[1], GOLD[2], GOLD[3], 0.45) rect(x - 1, y, 4, 2, c3(GOLD_D)); rect(x, y - 1, 2, 1, c3(GOLD)); rect(x, y, 2, 2, c3(GOLD)); px(x, y, c3(GOLD_HI)) rect(x, y + 2, 2, 1, c3(GOLD_D)) end end end local BEES = { { kind = "h", dir = 1, c = 12, v = 8, move = 3, pause = 2, off = 0.5 }, { kind = "h", dir = 2, c = 45, v = 7, move = 4, pause = 1.5, off = 3 }, { kind = "v", dir = 4, c = 58, v = 6, move = 2.5, pause = 1.5, off = 1 }, { kind = "v", dir = 3, c = 7, v = 7, move = 3.5, pause = 2, off = 2.2 }, } function render() if not TN then on_settings() end if not BG then build_bg(); if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local s = frame / FPS DERIVED = 0 if BQ <= 2 * #BQUEUE then bake_bee(BQUEUE[(BQ + 1) // 2], BQ % 2 == 1 and 0 or 6, BQ % 2 == 1 and 5 or 10) BQ = BQ + 1 end local t = s % 24 local p = 0 if t >= 4 and t < 11.5 then p = (t - 4) / 7.5 elseif t >= 11.5 and t < 18 then p = 1 elseif t >= 18 and t < 20 then p = 1 - (t - 18) / 2 end if p > 0 then hex(SX, SY, 0, c3(WAX_RIM)) hex(SX - 1, SY - 1, -1, c3(WAX)) local e = -1 - floor(p * 5 + 0.5) if e > -6 then hex(SX - 1, SY - 1, e, c3(TN[3])) if p < 0.7 then rect(SX - 4, SY - 3, 3, 1, c3(TN[5])) end else rect(SX - 4, SY - 3, 3, 1, c3(WAX_HI)); rect(SX - 4, SY - 2, 1, 2, c3(WAX_HI)); rect(SX - 1, SY, 2, 1, c3(WAX_RIM)) end end drip(20, s) drip(47, s + 3.2) local sp = 0.5 + 0.25 * (cfg.speed or 3) for i = 1, cfg.bees or 4 do local b = BEES[i] local d, moving = walk(s * sp + b.off, b.v, b.move, b.pause) local pose = moving and (frame % 70 < 10 and flutter(frame, 2) or 0) or flutter(frame, 4) local span = W + 20 if b.kind == "h" then local x = d % span - 10 if b.dir == 2 then x = W + 10 - d % span end draw_bee(x, b.c, b.dir, pose) else local y = d % span - 10 if b.dir == 3 then y = H + 10 - d % span end draw_bee(b.c, y, b.dir, pose) end end if t < 16 then local pose = (t >= 4 and t < 12) and flutter(frame, 3) or (frame % 60 < 8 and flutter(frame, 2) or 0) if t < 4 then draw_bee(-8 + 40 * t / 4, 34, 1, pose) elseif t < 12 then draw_bee(SX, 34 - ((frame // 10) % 3 == 0 and 1 or 0), 3, pose) else draw_bee(32 + 40 * (t - 12) / 4, 34, 1, pose) end end for i = 1, 5 do local c = CELLS[1 + umod(hash32(i * 0x2545F491), #CELLS)] local a = sin(s * 1.3 + i * 1.7) if c[3] == 1 and a > 0.4 then blend_rect(c[1] - 3, c[2] - 2, 2, 1, 255, 255, 255, (a - 0.4) * 0.9) end end end