info = { type = "image", title = "Ant farm", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Ants carry crumbs through an underground nest", api = 33, } local tr = ({ sk = { ["Ants"] = "Mravce" }, cs = { ["Ants"] = "Mravenci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "ants", label = T("Ants"), type = "int", min = 4, max = 16, default = 10 }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, sqrt, hash32, umod = math.floor, math.sin, math.sqrt, math.hash32, math.umod local W, H = gfx.W, gfx.H local TUN = { 78, 50, 34 } local CRUMB = { 240, 214, 140 } local PAL = { Q = { 214, 120, 54 }, q = { 150, 72, 36 }, K = { 14, 10, 10 }, W = { 244, 236, 204 }, w = { 206, 188, 146 }, } local function parse(art) local runs, w = {}, #art[1] for y = 1, #art do local row, x = art[y], 1 while x <= w do local ch, x2 = row:sub(x, x), x while row:sub(x2 + 1, x2 + 1) == ch do x2 = x2 + 1 end local c = PAL[ch] if c then runs[#runs + 1] = { y - 1, x - 1, x2 - x + 1, c[1], c[2], c[3], x == 1 or not PAL[row:sub(x - 1, x - 1)] } end x = x2 + 1 end end return runs end local function draw(runs, x, y) local ix = floor(x) local f = x - ix for i = 1, #runs do local r = runs[i] local a0, yy, n = ix + r[2], y + r[1], r[3] if r[7] then blend(a0, yy, r[4], r[5], r[6], 1 - f) if n > 1 then rect(a0 + 1, yy, n - 1, 1, r[4], r[5], r[6]) end else rect(a0, yy, n, 1, r[4], r[5], r[6]) end blend(a0 + n, yy, r[4], r[5], r[6], f) end end local QUEEN = parse({ "..qQQQQQq.......", ".qQQQQQQQq.QQ.q.", "qQQQQQQQQQ.QQQKq", "qQQQQQQQQQq.QQQ.", ".qQQQQQQQq..q.q.", "..qqqqqqq.q.q.q.", }) local GRUB = parse({ ".WWw.", "WWWWw" }) local function dot(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, r, g, b, (1 - fx) * (1 - fy)) blend(ix + 1, iy, r, g, b, fx * (1 - fy)) blend(ix, iy + 1, r, g, b, (1 - fx) * fy) blend(ix + 1, iy + 1, r, g, b, fx * fy) end local BG local function ellipse(cx, cy, a, b, c) for dy = -b, b do local hw = floor(a * sqrt(1 - (dy / (b + 0.5)) ^ 2) + 0.5) rect(floor(cx - hw), floor(cy + dy), 2 * hw, 1, c[1], c[2], c[3]) end end local function vtun(cx, y0, y1) rect(floor(cx) - 1, y0, 4, y1 - y0 + 1, TUN[1], TUN[2], TUN[3]) end local function htun(x0, x1, cy) rect(x0, floor(cy) - 1, x1 - x0 + 1, 4, TUN[1], TUN[2], TUN[3]) end local function build_bg() for y = 0, 9 do local t = y / 9 rect(0, y, W, 1, floor(120 + 86 * t), floor(176 + 54 * t), floor(232 + 16 * t)) end rect(0, 10, W, 6, 74, 134, 50) for x = 0, W - 1 do local h = hash32(x * 0x9E3779B1) local th = 1 + umod(h, 4) local c = umod(h >> 6, 2) == 0 and { 98, 168, 62 } or { 60, 116, 44 } rect(x, 13 - th, 1, th + 1, c[1], c[2], c[3]) end for y = 16, H - 1 do local t = (y - 16) / 47 rect(0, y, W, 1, floor(150 - 46 * t), floor(98 - 32 * t), floor(54 - 14 * t)) end rect(0, 16, W, 1, 96, 64, 36) for i = 1, 150 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), 17 + umod(h >> 7, 47) local t = (y - 16) / 47 local d = umod(h >> 14, 2) == 0 and -18 or 16 rect(x, y, 1 + umod(h >> 16, 2), 1, floor(150 - 46 * t) + d, floor(98 - 32 * t) + d, floor(54 - 14 * t) + d) end for _, s in ipairs({ { 4, 29 }, { 26, 21 }, { 56, 28 }, { 8, 50 }, { 52, 50 } }) do rect(s[1], s[2], 3, 2, 150, 148, 142) rect(s[1] + 1, s[2] - 1, 1, 1, 150, 148, 142) rect(s[1], s[2] + 1, 3, 1, 118, 116, 112) end for _, rx in ipairs({ 24, 30, 52 }) do rect(rx, 17, 1, 5, 196, 160, 110) rect(rx + (rx % 2 == 0 and 1 or -1), 20, 1, 3, 196, 160, 110) end vtun(14.5, 15, 38); vtun(46.5, 15, 36); htun(13, 47, 24.5); vtun(30.5, 24, 54) vtun(14.5, 44, 58); htun(13, 22, 57.5); vtun(46.5, 41, 58); htun(40, 47, 57.5) ellipse(14.5, 40.5, 8, 5, TUN); ellipse(46.5, 38.5, 8, 5, TUN); ellipse(31.5, 57.5, 12, 4, TUN) for k = 0, 3 do rect(floor(11 - 4 + k), 45 - k, 9 - 2 * k, 1, 236, 208, 132) end for _, p in ipairs({ { 8, 44 }, { 11, 43 }, { 13, 45 }, { 10, 42 } }) do rect(p[1], p[2], 1, 1, 196, 156, 80) end for _, x in ipairs({ 6, 20, 38, 57 }) do rect(x - 1, 15, 3, 1, CRUMB[1], CRUMB[2], CRUMB[3]) rect(x, 14, 1, 1, CRUMB[1], CRUMB[2], CRUMB[3]) end for _, e in ipairs({ { 21, 60 }, { 23, 59 }, { 42, 60 }, { 40, 61 } }) do rect(e[1], e[2], 1, 1, 244, 236, 204) end BG = gfx.snapshot(0, 0, W, H) end local function route(pts, carry, lane, oneway) local cum, L = { 0 }, 0 for i = 2, #pts do L = L + sqrt((pts[i][1] - pts[i - 1][1]) ^ 2 + (pts[i][2] - pts[i - 1][2]) ^ 2) cum[i] = L end return { p = pts, cum = cum, L = L, carry = carry, lane = lane, oneway = oneway } end local WORK = { route({ { 6, 14 }, { 14.5, 14 }, { 14.5, 38 }, { 11.5, 42.5 } }, true, true), route({ { 57, 14 }, { 46.5, 14 }, { 46.5, 36 }, { 49.5, 39 } }, true, true), route({ { 20, 14 }, { 14.5, 14 }, { 14.5, 24.5 }, { 30.5, 24.5 }, { 30.5, 53 }, { 41, 56.5 } }, true, true), route({ { 38, 14 }, { 46.5, 14 }, { 46.5, 24.5 }, { 14.5, 24.5 }, { 14.5, 38 }, { 12.5, 43 } }, true, true), route({ { 6, 14 }, { 14.5, 14 }, { 14.5, 57.5 }, { 24, 57.5 } }, true, true), route({ { 57, 14 }, { 46.5, 14 }, { 46.5, 57.5 }, { 36, 58.5 } }, true, true), } local NURSE = { route({ { 22, 59.5 }, { 41, 59.5 } }, false, false), route({ { 41, 41.5 }, { 52, 41.5 } }, false, false), } local WALK = route({ { -4, 14 }, { 68, 14 } }, false, false, true) local function at(rt, s) local cum, p = rt.cum, rt.p if s < 0 then s = 0 elseif s > rt.L then s = rt.L end local i = 2 while i < #p and cum[i] < s do i = i + 1 end local len = cum[i] - cum[i - 1] local ux, uy = (p[i][1] - p[i - 1][1]) / len, (p[i][2] - p[i - 1][2]) / len local d = s - cum[i - 1] return p[i - 1][1] + ux * d, p[i - 1][2] + uy * d, ux, uy, i end local DWELL = 36 local function ant(rt, v, off, frame) local run = rt.L / v local T = rt.oneway and run + 60 or 2 * (run + DWELL) local u = (frame + off) % T local s, side, carry if rt.oneway then if u > run then return end s, side = u * v, 1 elseif u < DWELL then s, side, carry = 0, 1, u > DWELL * 0.5 elseif u < DWELL + run then s, side, carry = (u - DWELL) * v, 1, true elseif u < 2 * DWELL + run then s, side, carry = rt.L, -1, u < DWELL + run + DWELL * 0.5 else s, side, carry = rt.L - (u - 2 * DWELL - run) * v, -1, false end carry = carry and rt.carry local hx, hy, ux, uy, seg = at(rt, s) local ox, oy = 0, 0 if rt.lane and seg > 2 then ox, oy = -uy * side * 0.5, ux * side * 0.5 end for k = 0, 2 do local x, y, bx, by, sg = at(rt, s - side * k) local o = (rt.lane and sg > 2) and 0.5 * side or 0 dot(x - by * o, y + bx * o, 22, 12, 10) end if carry then dot(hx + ox + ux * side * 1.5, hy + oy + uy * side * 1.5 - 0.6, CRUMB[1], CRUMB[2], CRUMB[3]) end end local AV, AO = {}, {} for i = 1, 16 do local h = hash32(i * 0xC2B2AE35) AV[i] = 0.2 + umod(h, 14) / 100 AO[i] = umod(h >> 8, 900) end local ORDER = { 1, 2, 3, 4, 5, 6 } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local cx = (f * 0.04) % (W + 20) - 14 blend_rect(floor(cx) + 2, 3, 10, 1, 255, 255, 255, 0.7) blend_rect(floor(cx), 4, 14, 2, 255, 255, 255, 0.7) blend_rect(floor(cx) + 3, 6, 8, 1, 255, 255, 255, 0.5) draw(QUEEN, 27 + 0.9 * sin(f * 0.025), 55) local pulse = 0.5 + 0.5 * sin(f * 0.04) blend(26, 60, 244, 236, 204, 0.3 + 0.7 * pulse) local grubs = { { 41, 40 }, { 45, 42 }, { 51, 40 }, { 48, 42 } } for i = 1, 4 do local g = grubs[i] draw(GRUB, g[1] + 0.7 * sin(f * 0.06 + i * 1.7), g[2]) end for i = 1, 2 do ant(NURSE[i], 0.12, i * 211, f) end ant(WALK, 0.2, 0, f) for i = 1, cfg.ants or 10 do ant(WORK[ORDER[(i - 1) % 6 + 1]], AV[i], AO[i] * 1.0, f) end end