info = { type = "image", title = "Hen walk", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hen struts, pecks grain, chicks follow", api = 33, } local ltr = ({ sk = { ["Hen colour"] = "Farba sliepky", ["Chicks"] = "Kuriatka" }, cs = { ["Hen colour"] = "Barva slepice", ["Chicks"] = "KuĊ™ata" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "hen", label = LT("Hen colour"), type = "color", default = "#F4F0E6" }, { key = "chicks", label = LT("Chicks"), type = "int", min = 0, max = 3, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs, max, min = math.floor, math.sin, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local G = 55 local GC = 60 local function mix(c, d, k) return { floor(c[1] + (d[1] - c[1]) * k + 0.5), floor(c[2] + (d[2] - c[2]) * k + 0.5), floor(c[3] + (d[3] - c[3]) * k + 0.5) } end local function B(s) return s:byte() end local DOT = 46 local function build_shape(rows, mirror) local h, w = #rows, 0 for y = 1, h do if #rows[y] > w then w = #rows[y] end end local Gd = {} for y = 1, h do local r = { rows[y]:byte(1, -1) } for x = #r + 1, w do r[x] = DOT end if mirror then local m = {} for x = 1, w do m[x] = r[w + 1 - x] end r = m end Gd[y] = r end local function at(x, y) local r = Gd[y]; return r and r[x] or DOT end local core, lft, rgt = {}, {}, {} for y = 1, h do local x = 1 while x <= w do local c = at(x, y) if c == DOT then x = x + 1 elseif at(x - 1, y) == DOT then local m = #lft lft[m + 1], lft[m + 2], lft[m + 3], lft[m + 4] = x - 1, y - 1, 1, c x = x + 1 else local n = 1 while x + n <= w and at(x + n, y) == c do n = n + 1 end local m = #core core[m + 1], core[m + 2], core[m + 3], core[m + 4] = x - 1, y - 1, n, c x = x + n end end for x2 = 1, w do local c = at(x2, y) if c ~= DOT and at(x2 + 1, y) == DOT then local m = #rgt rgt[m + 1], rgt[m + 2], rgt[m + 3], rgt[m + 4] = x2, y - 1, 1, c end end end return { core = core, lft = lft, rgt = rgt, w = w, h = h } end local function paint(s, pal) local function flat(src) local d = {} for i = 1, #src, 4 do local c = pal[src[i + 3]] local m = #d d[m + 1], d[m + 2], d[m + 3] = src[i], src[i + 1], src[i + 2] d[m + 4], d[m + 5], d[m + 6] = c[1], c[2], c[3] end return d end return { core = flat(s.core), lft = flat(s.lft), rgt = flat(s.rgt), w = s.w, h = s.h } end local function draw_h(s, x, y) local ix = floor(x) local f = x - ix local c = s.core for k = 1, #c, 6 do rect(ix + c[k], y + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5]) end c = s.lft local a = 1 - f for k = 1, #c, 6 do blend_rect(ix + c[k], y + c[k + 1], 1, 1, c[k + 3], c[k + 4], c[k + 5], a) end if f > 0.03 then c = s.rgt for k = 1, #c, 6 do blend_rect(ix + c[k], y + c[k + 1], 1, 1, c[k + 3], c[k + 4], c[k + 5], f) end end end local function draw_i(s, x, y) local c = s.core for k = 1, #c, 6 do rect(x + c[k], y + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5]) end c = s.lft for k = 1, #c, 6 do rect(x + c[k], y + c[k + 1], 1, 1, c[k + 3], c[k + 4], c[k + 5]) end end local BODY_R = { "t...............", "tt..............", "ttt...ooooo.....", "tttoobbbbbbbo...", "ttobbbbbbbbbbo..", ".tobbwwwwwbbbbo.", "..obbbwwwwwbbbo.", "..obbbbbwwbbbo..", "...obbbbbbbbo...", "....ooooooo.....", } local HEAD_R = { "..rr...", ".rrrr..", ".obbbo.", "obbbkbyy", "obbbbo..", ".rbbo...", } local PECK_R = { ".rr..", "rrrr.", "obbbo", "obbko", "obbbo", ".oyo.", "..y..", } local CHICK_R = { ".yyy..", "yyyky.", "yyyyyo", "yyyyy.", } local SHP = { body = { build_shape(BODY_R, false), build_shape(BODY_R, true) }, head = { build_shape(HEAD_R, false), build_shape(HEAD_R, true) }, peck = { build_shape(PECK_R, false), build_shape(PECK_R, true) }, chick = { build_shape(CHICK_R, false), build_shape(CHICK_R, true) }, } local SPR = {} local HEN_B, HEN_O = { 244, 240, 230 }, { 90, 80, 80 } local LEG = { 236, 150, 50 } local function hen_paint(c) local b = { c.r, c.g, c.b } HEN_B = b HEN_O = mix(b, { 50, 36, 40 }, 0.66) local pal = { [B"b"] = b, [B"o"] = HEN_O, [B"w"] = mix(b, { 120, 100, 90 }, 0.22), [B"t"] = mix(b, { 40, 40, 50 }, 0.45), [B"r"] = { 226, 50, 46 }, [B"y"] = { 246, 190, 60 }, [B"k"] = { 26, 22, 26 }, } local cpal = { [B"y"] = { 255, 222, 80 }, [B"k"] = { 30, 26, 30 }, [B"o"] = { 240, 140, 40 } } for _, n in ipairs({ "body", "head", "peck" }) do SPR[n] = { paint(SHP[n][1], pal), paint(SHP[n][2], pal) } end SPR.chick = { paint(SHP.chick[1], cpal), paint(SHP.chick[2], cpal) } end local BG = nil local function build_bg() for y = 0, 27 do local t = y / 27 rect(0, y, W, 1, floor(118 + 80 * t), floor(184 + 44 * t), floor(238 + 10 * t)) end for y = 4, 12 do for x = 48, 58 do local dx, dy = x - 53, y - 8 local d = sqrt(dx * dx + dy * dy) if d <= 3.6 then px(x, y, 255, 244, 190) elseif d <= 5.2 then px(x, y, 236, 240, 226) end end end rect(0, 28, W, 4, 104, 168, 86) for x = 0, W - 1 do local h = floor(5 + 2.5 * sin(x * 0.08 + 1.1) + 1.5 * sin(x * 0.21) + 0.5) rect(x, 28 - h, 1, h, 132, 190, 106) local h2 = floor(2 + 1.5 * sin(x * 0.13 + 3) + 0.5) rect(x, 31 - h2, 1, h2 + 1, 104, 168, 86) end rect(0, 31, W, H - 31, 112, 178, 72) rect(0, 33, W, 2, 182, 132, 86); rect(0, 38, W, 2, 182, 132, 86) rect(0, 35, W, 1, 140, 98, 62); rect(0, 40, W, 1, 140, 98, 62) for x = 3, W - 1, 11 do rect(x, 30, 3, 13, 196, 146, 98) rect(x + 2, 30, 1, 13, 150, 106, 68) px(x + 1, 29, 196, 146, 98) end for y = 45, H - 1 do local v = (y - 56) / 11 local hw = floor(40 * sqrt(max(0, 1 - v * v)) + 0.5) rect(32 - hw, y, 2 * hw, 1, 206, 170, 118) px(32 - hw, y, 182, 146, 96); px(31 + hw, y, 182, 146, 96) end for k = 1, 22 do local h = hash32(k * 0x9E3779B1) local x, y = umod(h, W), 44 + umod(h >> 8, 20) if k % 2 == 0 then px(x, y, 176, 140, 96) else px(x, y - 1, 84, 150, 60); px(x + 1, y, 84, 150, 60) end end BG = gfx.snapshot(0, 0, W, H) end local V = 0.5 local T = 8 local PECKT = 16 local SEG = { { "walk", 6, 38, 1 }, { "peck", 38, 3, 1 }, { "turn", 38, 8, 1 }, { "walk", 38, 8, -1 }, { "peck", 8, 3, -1 }, { "turn", 8, 8, -1 }, } local SEG_T, CYCLE = {}, 0 for i, s in ipairs(SEG) do local d if s[1] == "walk" then d = floor(abs(s[3] - s[2]) / V + 0.5) elseif s[1] == "peck" then d = s[3] * PECKT else d = s[3] end SEG_T[i] = { CYCLE, d } CYCLE = CYCLE + d end local function state(t) local u = t % CYCLE for i = 1, #SEG do local st = SEG_T[i] if u < st[1] + st[2] then local s = SEG[i] local tau = u - st[1] if s[1] == "walk" then return "walk", s[2] + s[4] * V * tau, s[4], tau end if s[1] == "peck" then return "peck", s[2], s[4], tau end local dir = (tau < s[3] // 2) and s[4] or -s[4] return "turn", s[2], dir, tau end end return "turn", 6, 1, 0 end local STEP_HZ = 30 local OFF = 90 local function leg(hx, hy, fx, fy, dir) local dx, dy = fx - hx, fy - hy local n = max(abs(dx), abs(dy)) for k = 0, n do px(floor(hx + dx * k / n + 0.5), floor(hy + dy * k / n + 0.5), LEG[1], LEG[2], LEG[3]) end rect(dir > 0 and fx or fx - 2, fy, 3, 1, LEG[1], LEG[2], LEG[3]) px(fx - dir, fy, LEG[1], LEG[2], LEG[3]) end local function neck(x0, y0, x1, y1) local n = max(abs(x1 - x0), abs(y1 - y0)) if n == 0 then n = 1 end for k = 0, n do local x = floor(x0 + (x1 - x0) * k / n + 0.5) local y = floor(y0 + (y1 - y0) * k / n + 0.5) rect(x, y, 3, 3, HEN_B[1], HEN_B[2], HEN_B[3]) end end local GRAIN = { { 58, 0 }, { 60, 1 }, { 57, 2 }, { 3, 0 }, { 1, 1 }, { 4, 2 } } local function grains(t) local u = t % CYCLE for k = 1, #GRAIN do local g = GRAIN[k] local seg = (k <= 3) and 2 or 5 local st = SEG_T[seg] local eaten = u >= st[1] + g[2] * PECKT + 6 if not eaten then px(g[1], G + 1 - (k % 2), 236, 196, 70) end end end local function hen(t) local mode, x, dir, tau = state(t) local side = dir > 0 and 1 or 2 local bob = 0 if mode == "walk" then bob = ((tau // T) % 2 == 0) and 0 or -1 end local by = G - 15 + bob local bxi = floor(x + 0.5) local hipx = x + 7 for f = 0, 1 do local fx, fy if mode == "walk" then local p = (tau + f * T) % (2 * T) if p < T then local xa = hipx - V * dir * p - 2 * dir local xb = hipx + V * dir * (T - p) + 2 * dir fx = xa + (xb - xa) * p / T fy = G - floor(2 * sin(3.14159 * p / T) + 0.5) else fx = hipx - V * dir * (p - T) + 2 * dir fy = G end else fx, fy = hipx + (f == 0 and -1 or 2), G end leg(floor(hipx + (f == 0 and -1 or 1) + 0.5), by + 10, floor(fx + 0.5), fy, dir) end draw_h(SPR.body[side], x, by) local nbx = (dir > 0) and (bxi + 11) or (bxi + 2) local nby = by + 2 local hx, hy local pecking = false if mode == "walk" then local k = tau // T local p = tau % T local function target(kk) return x - V * dir * tau + V * dir * (kk + 1) * T end local h1 = target(k) local h0 = target(k - 1) local hxw = (p < 2) and (h0 + (h1 - h0) * (p + 1) / 3) or h1 hx = floor(hxw + 0.5) + (dir > 0 and 12 or -3) hy = by - 5 elseif mode == "peck" then local p = tau % PECKT local d if p < 4 then d = p / 4 elseif p < 8 then d = 1 elseif p < 14 then d = 1 - (p - 8) / 6 else d = 0 end hx = bxi + (dir > 0 and 12 or -3) + floor(d * 4 * dir + 0.5) hy = by - 5 + floor(d * 13 + 0.5) pecking = d > 0.5 else hx = bxi + (dir > 0 and 12 or -3) hy = by - 5 end local s = pecking and SPR.peck[side] or SPR.head[side] local hw = s.w neck(nbx, nby, hx + (dir > 0 and 1 or hw - 4), hy + 3) draw_i(s, hx, hy) end local NCH = 2 local function chicks(t) for i = 1, NCH do local lag = 22 + 18 * (i - 1) local mode, x, dir, tau = state(t - lag) local side = dir > 0 and 1 or 2 local cx = x + 5 - dir * (6 + 3 * i) local hop = 0 if mode == "walk" then hop = ((tau // 4 + i) % 2 == 0) and -1 or 0 end local cy = GC - 4 + hop if mode == "peck" and (tau % PECKT) >= 3 and (tau % PECKT) < 9 then cy = cy + 1 end draw_h(SPR.chick[side], cx, cy) local lx = floor(cx + 0.5) px(lx + 1, GC, 240, 140, 40); px(lx + 3, GC + hop, 240, 140, 40) end end function on_settings() hen_paint(cfg.hen or { r = 244, g = 240, b = 230 }) NCH = cfg.chicks or 2 end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS + OFF grains(t) hen(t) if NCH > 0 then chicks(t) end end