info = { type = "image", title = "Sheepdog and flock", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sheepdog herds the flock into the pen", api = 33, } local tr = ({ sk = { ["Sheep"] = "Ovce" }, cs = { ["Sheep"] = "Ovce" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Sheep"), type = "int", min = 3, max = 8, default = 5 }, } local rect, px, blend_rect, blit_rgb = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb local floor, sin, cos, sqrt, max = math.floor, math.sin, math.cos, math.sqrt, math.max local hash32, umod = math.hash32, math.umod local PI = 3.14159 local CYCLE = 1700 local WALK = 0.55 local WOOL, WOOL_HI, WOOL_SH, HEAD = { 240, 236, 226 }, { 255, 252, 244 }, { 190, 184, 180 }, { 76, 64, 62 } local DOG, DOG_W = { 30, 28, 36 }, { 238, 238, 238 } local RAIL, POST = { 176, 130, 80 }, { 118, 84, 52 } local GRAZE = { { 28, 44 }, { 16, 45 }, { 22, 50 }, { 9, 50 }, { 32, 52 }, { 14, 56 }, { 26, 56 }, { 6, 44 } } local PEN = { { 57, 43 }, { 45, 44 }, { 51, 48 }, { 58, 50 }, { 45, 51 }, { 52, 54 }, { 58, 55 }, { 44, 56 } } local BG local N = 0 local SHEEP, DOGP = {}, nil local GATE_L, GATE_R = { 0, 0 }, { 0, 0 } local function newpath(t, x, y, f) return { { t, x, y, f or 1, false } } end local function go(p, x, y, spd, dur) local a = p[#p] local dx, dy = x - a[2], y - a[3] local dt = dur or max(1, floor(sqrt(dx * dx + dy * dy) / spd + 0.5)) p[#p + 1] = { a[1] + dt, x, y, dx > 0.01 and 1 or (dx < -0.01 and -1 or a[4]), false } return a[1] + dt end local function wait_until(p, t) local a = p[#p] if t > a[1] then p[#p + 1] = { t, a[2], a[3], a[4], a[5] } end end local function orbit(p, t0, t1, a0, a1, cx, cy, rx, ry) local steps = (t1 - t0) // 6 for s = 1, steps do local a = a0 + (a1 - a0) * s / steps go(p, cx + rx * cos(a), cy + ry * sin(a), nil, (t1 - t0) / steps) end end local function at(p, t) local n = #p if t <= p[1][1] then return p[1][2], p[1][3], false, p[1][4], p[1][5] end for k = 1, n - 1 do local a, b = p[k], p[k + 1] if t < b[1] then local u = (t - a[1]) / (b[1] - a[1]) local dx, dy = b[2] - a[2], b[3] - a[3] return a[2] + dx * u, a[3] + dy * u, dx ~= 0 or dy ~= 0, b[4], a[5] end end local l = p[n] return l[2], l[3], false, l[4], l[5] end local function build_paths() local order = {} for i = 1, N do order[i] = i end table.sort(order, function(a, b) return GRAZE[a][1] > GRAZE[b][1] end) local exit_order = {} for i = 1, N do exit_order[i] = i end table.sort(exit_order, function(a, b) if PEN[a][1] ~= PEN[b][1] then return PEN[a][1] > PEN[b][1] end return PEN[a][2] < PEN[b][2] end) local exrank = {} for r, i in ipairs(exit_order) do exrank[i] = r - 1 end local stray = N >= 4 and 3 or 2 local gate_in, gate_out, exit_end = 1e9, 0, 0 local last_in = 0 SHEEP = {} for rk = 0, N - 1 do local gx, gy = GRAZE[order[rk + 1]][1], GRAZE[order[rk + 1]][2] local px_, py_ = PEN[rk + 1][1], PEN[rk + 1][2] local p = newpath(8 + 16 * rk, -12, gy, 1) go(p, gx, gy, WALK) if rk + 1 == stray then wait_until(p, 230) go(p, 11, 36, WALK) wait_until(p, 372) go(p, gx, gy, WALK) end local td = 690 + 36 * rk wait_until(p, td) go(p, 34, 50, WALK) local tg = go(p, 40, 50, WALK) if tg < gate_in then gate_in = tg end last_in = math.max(last_in, go(p, 44, 50, WALK)) go(p, px_, py_, WALK) local ex = 1330 + 26 * exrank[rk + 1] wait_until(p, ex) go(p, 60, 50, WALK) go(p, 74, 50, WALK) exit_end = math.max(exit_end, p[#p][1]) gate_out = (gate_out == 0 or ex < gate_out) and ex or gate_out SHEEP[rk + 1] = { p = p, seed = hash32(rk * 0x9E3779B1) } end GATE_L = { 690 - 15, last_in + 10 } GATE_R = { gate_out - 30, exit_end - 20 } local d = newpath(60, -14, 50, 1) go(d, 6, 49, nil, 30) orbit(d, 90, 200, PI, 3 * PI, 21, 49, 15, 8) wait_until(d, 330) go(d, 6, 41, nil, 20) go(d, 4, 35, nil, 14) wait_until(d, 372) go(d, 8, 42, nil, 36) go(d, 6, 49, nil, 25) orbit(d, 450, 560, 3 * PI, PI, 21, 49, 15, 8) wait_until(d, 700) for k = 0, 13 do go(d, 6 + k * 2.2, (k % 2 == 0) and 56 or 44, nil, 25) end go(d, 35, 52, nil, 30) d[#d][5] = true d[#d][4] = 1 wait_until(d, 1330) d[#d + 1] = { 1334, d[#d][2], d[#d][3], 1, false } go(d, -16, 52, 0.9) DOGP = d end local function lerp(a, b, t) return a + (b - a) * t end local function build_bg() for y = 0, 27 do local t = y / 27 rect(0, y, 64, 1, floor(lerp(118, 208, t) + 0.5), floor(lerp(186, 232, t) + 0.5), floor(lerp(240, 246, t) + 0.5)) end for x = 0, 63 do local h1 = floor(5 + 3 * sin(x * 0.09 + 1) + 2 * sin(x * 0.23)) rect(x, 27 - h1, 1, h1, 150, 196, 190) local h2 = floor(2 + 2 * sin(x * 0.13 + 2.2) + sin(x * 0.31)) rect(x, 27 - h2, 1, h2 + 1, 112, 168, 118) end for y = 28, 63 do local t = (y - 28) / 35 rect(0, y, 64, 1, floor(lerp(128, 80, t) + 0.5), floor(lerp(188, 142, t) + 0.5), floor(lerp(88, 58, t) + 0.5)) end for i = 1, 70 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, 64), 30 + umod(h >> 8, 33) if umod(h >> 16, 3) == 0 then px(x, y, 150, 206, 106) else rect(x, y, 2, 1, 70, 124, 52) end if umod(h >> 20, 9) == 0 then px(x + 1, y - 1, 250, 232, 120) end end rect(38, 34, 26, 1, RAIL[1], RAIL[2], RAIL[3]) rect(38, 36, 26, 1, RAIL[1], RAIL[2], RAIL[3]) for x = 38, 62, 6 do rect(x, 33, 1, 5, POST[1], POST[2], POST[3]) end BG = gfx.snapshot(0, 0, 64, 64) end local function front_fence(t) rect(38, 56, 26, 1, RAIL[1], RAIL[2], RAIL[3]) rect(38, 58, 26, 1, RAIL[1], RAIL[2], RAIL[3]) for x = 38, 62, 6 do rect(x, 54, 1, 6, POST[1], POST[2], POST[3]) end for _, gx in ipairs({ 38, 62 }) do rect(gx, 37, 1, 9, POST[1], POST[2], POST[3]) rect(gx, 55, 1, 2, POST[1], POST[2], POST[3]) end local lo = t >= GATE_L[1] and t < GATE_L[2] local ro = t >= GATE_R[1] and t < GATE_R[2] if lo then rect(38, 45, 5, 1, RAIL[1], RAIL[2], RAIL[3]) else rect(38, 46, 1, 9, RAIL[1], RAIL[2], RAIL[3]); px(39, 50, RAIL[1], RAIL[2], RAIL[3]) end if ro then rect(58, 45, 5, 1, RAIL[1], RAIL[2], RAIL[3]) else rect(62, 46, 1, 9, RAIL[1], RAIL[2], RAIL[3]); px(61, 50, RAIL[1], RAIL[2], RAIL[3]) end end local SX, SY, SF = 0, 0, 1 local function put(dx, dy, w, h, c) rect(SF > 0 and SX + dx or SX - dx - w + 1, SY + dy, w, h, c[1], c[2], c[3]) end local function draw_sheep(x, y, face, mv, seed) SX, SY, SF = floor(x + 0.5), floor(y + 0.5), face local ph = mv and ((frame // 4 + seed) % 2) or 0 local graze = not mv and umod((frame // 45) + seed, 3) ~= 0 for k = 0, 3 do local up = mv and ((k + ph) % 2 == 0) and 1 or 0 put(-3 + k * 2, -2 + up, 1, 2 - up, HEAD) end SY = SY - ph put(-3, -6, 7, 3, WOOL) put(-2, -7, 5, 1, WOOL_HI) put(-3, -3, 7, 1, WOOL_SH) put(-4, -5, 1, 2, WOOL) if graze then put(4, -4, 3, 3, HEAD) else put(4, -6, 3, 3, HEAD); put(3, -6, 1, 1, WOOL_SH) end end local function draw_dog(x, y, face, mv, sit) SX, SY, SF = floor(x + 0.5), floor(y + 0.5), face if sit then put(-2, -6, 4, 6, DOG) put(1, -5, 1, 4, DOG_W) put(-1, -10, 4, 4, DOG) put(3, -8, 1, 1, DOG_W) put(-1, -11, 1, 1, DOG) put(-5, -1, 3, 1, DOG) put(-6, -1, 1, 1, DOG_W) return end local g = mv and (frame // 3) % 2 or 0 if mv then if g == 0 then put(4, -2, 1, 2, DOG); put(-4, -2, 1, 2, DOG) else put(-1, -2, 1, 2, DOG); put(2, -2, 1, 2, DOG) end SY = SY - (g == 0 and 0 or 1) else put(-2, -2, 1, 2, DOG); put(2, -2, 1, 2, DOG) end put(-3, -5, 7, 3, DOG) put(3, -4, 2, 2, DOG_W) put(4, -7, 3, 3, DOG) put(7, -5, 1, 1, DOG_W) put(3, -8, 1, 1, DOG) put(-5, mv and -6 or -4, 2, 1, DOG) put(-6, mv and -7 or -3, 1, 1, DOG_W) end function on_settings() N = cfg.n or 5 build_paths() end local function soft_bar(x, y, w, h, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, w - 1, h, 255, 255, 255, a) blend_rect(ix, y, 1, h, 255, 255, 255, a * (1 - f)) blend_rect(ix + w, y, 1, h, 255, 255, 255, a * f) end local ACT, NACT = {}, 0 for i = 1, 9 do ACT[i] = {} end function render() if not BG then build_bg() end blit_rgb(BG, 0, 0, 64, 64) for k = 0, 1 do local x = (frame * (0.05 + 0.025 * k) + 30 * k) % 96 - 24 local y = 4 + 8 * k soft_bar(x + 3, y, 9, 1, 0.8) soft_bar(x, y + 1, 15, 1, 0.8) soft_bar(x + 4, y + 2, 9, 1, 0.4) end local t = (frame + 100) % CYCLE NACT = 0 for i = 1, N do local x, y, mv, f = at(SHEEP[i].p, t) if x > -8 and x < 72 then NACT = NACT + 1 local a = ACT[NACT] a[1], a[2], a[3], a[4], a[5], a[6], a[7] = y, x, y, mv, f, SHEEP[i].seed, false end end local dx, dy, dmv, df, dsit = at(DOGP, t) if dx > -9 then NACT = NACT + 1 local a = ACT[NACT] a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8] = dy, dx, dy, dmv, df, 0, true, dsit end for i = 2, NACT do local a, j = ACT[i], i - 1 while j >= 1 and ACT[j][1] > a[1] do ACT[j + 1] = ACT[j]; j = j - 1 end ACT[j + 1] = a end for i = 1, NACT do local a = ACT[i] if a[7] then draw_dog(a[2], a[3], a[5], a[4], a[8]) else draw_sheep(a[2], a[3], a[5], a[4], a[6]) end end front_fence(t) end