info = { type = "image", title = "Picnic ants", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Ants carry crumbs from a picnic blanket", api = 44, } local tr = ({ sk = { ["Ants"] = "Mravce", ["Butterfly"] = "Motýľ" }, cs = { ["Ants"] = "Mravenci", ["Butterfly"] = "Motýl" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "ants", label = T("Ants"), type = "int", min = 2, max = 8, default = 6 }, { key = "fly", label = T("Butterfly"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt, max = math.floor, math.sin, math.sqrt, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function lerp(a, b, t) return a + (b - a) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function mix(a, b, t) return rgb(lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end local function wall() for y = 0, 13 do rect(0, y, 18, 1, mix({ 138, 198, 232 }, { 196, 228, 240 }, y / 13)) end rect(0, 14, 18, 8, 152, 206, 236) rect(18, 0, W - 18, 22, 214, 178, 150) local tone = { { 176, 86, 62 }, { 164, 78, 56 }, { 186, 98, 72 } } for r = 0, 5 do local y = r * 4 for bx = (r % 2 == 0) and 18 or 14, W - 1, 8 do local c = tone[1 + umod(hash32((r * 16 + bx) * 0x9E3779B1), 3)] local x0 = max(bx, 18) rect(x0, y, bx + 7 - x0, 3, c[1], c[2], c[3]) end end rect(18, 0, 1, 22, 120, 60, 46) end local function grass() for y = 22, H - 1 do rect(0, y, W, 1, mix({ 62, 128, 56 }, { 148, 208, 78 }, (y - 22) / 41)) end rect(18, 22, W - 18, 2, 40, 96, 44) for i = 1, 110 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), 26 + umod(h >> 8, 36) local bh = 2 + (y - 26) // 12 + ((h >> 16) & 1) if (h >> 18) & 1 == 0 then rect(x, y - bh, 1, bh, 176, 228, 100) else rect(x, y - bh, 1, bh, 46, 104, 46) end end end local function xl(y) return 27 - (y - 34) * 0.6 end local function blanket_items() for y = 34, 52 do local x0 = floor(xl(y)) local band = (y - 34) // 4 local c = 0 local x = x0 while x < W do local red = (c + band) % 2 == 0 if red then rect(x, y, 5, 1, 200, 44, 52) else rect(x, y, 5, 1, 242, 238, 232) end x = x + 5 c = c + 1 end end blend_rect(floor(xl(53)), 53, W, 1, 30, 70, 30, 0.4) rect(27, 30, 12, 9, 186, 126, 58) for y = 31, 38, 2 do for x = 27 + (y % 4 == 1 and 0 or 1), 38, 2 do px(x, y, 130, 84, 36) end end rect(27, 30, 12, 1, 150, 98, 44) rect(29, 28, 4, 2, 200, 44, 52); rect(33, 28, 3, 2, 242, 238, 232) rect(28, 25, 10, 1, 120, 76, 34); rect(28, 26, 1, 4, 120, 76, 34); rect(37, 26, 1, 4, 120, 76, 34) for y = 32, 38 do local dy = (y - 35) / 3.5 local hw = floor(5.5 * sqrt(max(0, 1 - dy * dy)) + 0.5) rect(46 - hw, y, 2 * hw + 1, 1, y < 34 and 236 or 214, y < 34 and 184 or 150, y < 34 and 104 or 76) end px(43, 34, 170, 106, 46); px(46, 34, 170, 106, 46); px(49, 34, 170, 106, 46) for r = 0, 4 do rect(54, 35 + r, 9 - r, 1, 250, 208, 70) end rect(54, 40, 9, 1, 230, 176, 40) px(57, 37, 210, 150, 30); px(56, 38, 210, 150, 30); px(59, 36, 210, 150, 30) for dy = 0, 7 do local y = 42 + dy local ho, hm, hf = floor(sqrt(max(0, 49 - dy * dy)) + 0.5), floor(sqrt(max(0, 38 - dy * dy)) + 0.5), floor(sqrt(max(0, 27 - dy * dy)) + 0.5) rect(30 - ho, y, 2 * ho + 1, 1, 36, 120, 60) if dy <= 6 then rect(30 - hm, y, 2 * hm + 1, 1, 200, 236, 180) end if dy <= 5 then rect(30 - hf, y, 2 * hf + 1, 1, 226, 60, 72) end end px(28, 44, 30, 20, 24); px(31, 45, 30, 20, 24); px(33, 43, 30, 20, 24); px(27, 46, 30, 20, 24) rect(49, 45, 3, 3, 200, 44, 48); px(49, 45, 236, 120, 120); px(50, 44, 46, 120, 50) px(40, 48, 232, 190, 120); px(41, 48, 232, 190, 120); px(43, 49, 232, 190, 120) px(41, 46, 232, 190, 120); px(39, 49, 232, 190, 120); px(42, 47, 232, 190, 120) end local function foreground_blades() for i = 1, 36 do local h = hash32(i * 0xC2B2AE35) local x, y = umod(h, W), 55 + umod(h >> 8, 9) local bh = 3 + ((h >> 12) & 1) if (h >> 16) & 1 == 0 then rect(x, y - bh, 1, bh, 176, 228, 100) else rect(x, y - bh, 1, bh, 52, 120, 50) end end end local BG = nil local STAGES = { wall, grass, blanket_items, function() foreground_blades(); BG = gfx.snapshot(0, 0, W, H) end } local STAGE = 1 local PTS = { { -4, 57 }, { 6, 58 }, { 16, 56.5 }, { 26, 54 }, { 34, 51 }, { 39, 49.5 } } local SEG, LEN = {}, 0 for i = 1, #PTS - 1 do local dx, dy = PTS[i + 1][1] - PTS[i][1], PTS[i + 1][2] - PTS[i][2] local l = sqrt(dx * dx + dy * dy) SEG[i] = { PTS[i][1], PTS[i][2], dx / l, dy / l, LEN, LEN + l } LEN = LEN + l end local function pos(s) local n = #SEG for i = 1, n do local g = SEG[i] if s <= g[6] or i == n then local d = s - g[5] return g[1] + g[3] * d, g[2] + g[4] * d, g[3], g[4] end end end local V, PAUSE, GAP = 0.36, 26, 40 local OUT = floor(LEN / V) local CYC = 2 * OUT + PAUSE + 70 local function dot(x, y, r, g, b, a) local ix, iy = floor(x), floor(y + 0.5) local f = x - ix local wl, wr = a * math.min(1, 2 * (1 - f)), a * math.min(1, 2 * f) if wl > 0.1 then blend(ix, iy, r, g, b, wl) end if wr > 0.1 then blend(ix + 1, iy, r, g, b, wr) end end local function ant(s, dir, carry) local x, y, ux, uy = pos(s) local bx, by = -ux * dir, -uy * dir dot(x, y, 18, 10, 8, 1) dot(x + bx * 1.4, y + by * 1.4, 18, 10, 8, 1) dot(x + bx * 2.8, y + by * 2.8, 18, 10, 8, 0.95) if carry then local cx, cy = x + bx * 1.4, y + by * 1.4 - 2.2 dot(cx, cy, 244, 220, 158, 1); dot(cx + 1, cy, 244, 220, 158, 1) end end local function ant_state(p) if p < OUT then return V * p, 1, false end if p < OUT + PAUSE then return LEN, 1, p >= OUT + 12 end if p < 2 * OUT + PAUSE then return LEN - V * (p - OUT - PAUSE), -1, true end end local BLADES = { { 9, 61 }, { 26, 62 }, { 47, 61 }, { 58, 60 } } local FLY = { { -2, -1 }, { -1, -1 }, { 1, -1 }, { 2, -1 }, { -1, 0 }, { 1, 0 } } local FLY2 = { { -1, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 } } function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local n = cfg.ants or 6 for i = 1, n do if not (n >= 4 and i == 4) then local p = (frame - i * GAP) % CYC local s, dir, carry = ant_state(p) if s then if n >= 4 and i == 3 then ant(s, dir, false) ant(s - 5 * dir, dir, false) if carry then local x, y = pos(s - 2.5 * dir) for k = -1, 1 do dot(x + k, y - 3, 250, 232, 170, 1) end dot(x - 1, y - 2, 250, 232, 170, 1); dot(x + 1, y - 2, 250, 232, 170, 1) end else ant(s, dir, carry) end end end end for k = 1, #BLADES do local b = BLADES[k] local o = floor(1.5 * sin(frame * 0.045 + k * 1.7) + 0.5) rect(b[1] + o, b[2] - 7, 1, 7, 150, 214, 84) rect(b[1] - 1, b[2] - 1, 3, 2, 56, 124, 52) end if cfg.fly ~= false then local ph = frame % 720 if ph < 120 then local x, y = 68 - ph, 14 + floor(5 * sin(ph * 0.09) + 0.5) local w = (ph // 3) % 2 == 0 and FLY or FLY2 for k = 1, #w do px(x + w[k][1], y + w[k][2], 250, 190, 50) end px(x, y, 70, 40, 30); px(x, y - 1, 70, 40, 30) end end end