info = { type = "image", title = "Counting sheep", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A child dreams of sheep jumping a fence", api = 44, } local tr = ({ sk = { ["Sheep pace"] = "Tempo oviec", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo", ["Counter"] = "Počítadlo" }, cs = { ["Sheep pace"] = "Tempo ovcí", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle", ["Counter"] = "Počítadlo" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pace", label = T("Sheep pace"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, { key = "num", label = T("Counter"), 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 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 disc(cx, cy, r, cr, cg, cb) for dy = -r, r do local hw = floor(sqrt(r * r + r - dy * dy) + 0.0) rect(cx - hw, cy + dy, 2 * hw + 1, 1, cr, cg, cb) end end local IX, IY, IW, IH = 25, 10, 32, 25 local GROUND = 32 local BX, BY, BW, BH = 44, 11, 13, 11 local function room() for y = 0, 51 do rect(0, y, W, 1, mix({ 16, 26, 62 }, { 22, 34, 74 }, y / 51)) end rect(0, 51, W, 1, 34, 46, 92) rect(0, 52, W, 12, 12, 18, 44) for y = 55, 63, 3 do rect(0, y, W, 1, 16, 24, 54) end rect(3, 5, 13, 20, 30, 40, 86) rect(4, 6, 11, 18, 64, 100, 176) rect(9, 6, 1, 18, 30, 40, 86); rect(4, 14, 11, 1, 30, 40, 86) disc(6, 9, 1, 240, 244, 255) px(12, 8, 220, 228, 255); px(7, 19, 200, 210, 250); px(13, 18, 220, 228, 255) for y = 25, 50 do local x0, x1 = floor(3 + (y - 25) * 0.55), floor(16 + (y - 25) * 1.1) blend_rect(x0, y, x1 - x0, 1, 120, 150, 230, 0.10) end rect(49, 44, 15, 19, 24, 32, 72) rect(49, 44, 15, 1, 70, 92, 160); rect(49, 45, 1, 18, 44, 58, 124) rect(50, 52, 14, 1, 14, 20, 50); rect(50, 58, 14, 1, 14, 20, 50) px(56, 48, 110, 130, 200); px(56, 55, 110, 130, 200) rect(52, 40, 6, 4, 20, 28, 64) end local function bed() rect(7, 51, 36, 10, 58, 86, 176) rect(6, 61, 37, 3, 36, 44, 100) rect(2, 38, 4, 25, 58, 42, 74); rect(2, 38, 4, 1, 96, 78, 120) rect(43, 46, 3, 17, 58, 42, 74) rect(7, 45, 10, 5, 206, 212, 238); rect(7, 49, 10, 1, 176, 184, 220) end local BUMPS = { { 25, 10 }, { 31, 9 }, { 38, 9 }, { 45, 9 }, { 52, 9 }, { 57, 10 }, { 25, 35 }, { 31, 36 }, { 38, 36 }, { 45, 36 }, { 52, 36 }, { 57, 35 }, { 25, 16 }, { 25, 22 }, { 25, 29 }, { 57, 16 }, { 57, 22 }, { 57, 29 }, } local function dream() for _, b in ipairs(BUMPS) do disc(b[1], b[2], 5, 176, 190, 238) end for _, b in ipairs(BUMPS) do disc(b[1], b[2], 4, 238, 242, 255) end disc(15, 42, 1, 176, 190, 238); disc(18, 39, 2, 176, 190, 238) disc(15, 42, 0, 226, 232, 255); disc(18, 39, 1, 226, 232, 255) for y = IY, GROUND - 6 do rect(IX, y, IW, 1, mix({ 255, 204, 190 }, { 255, 240, 212 }, (y - IY) / (GROUND - 6 - IY))) end disc(31, 16, 3, 255, 236, 150) rect(IX, 24, IW, 4, 196, 230, 176) disc(50, 27, 6, 196, 230, 176) for y = 27, IY + IH - 1 do rect(IX, y, IW, 1, mix({ 182, 224, 154 }, { 152, 206, 132 }, (y - 27) / 8)) end for _, x in ipairs({ 36, 40, 44, 48 }) do rect(x, 25, 2, 8, 252, 246, 236); rect(x + 2, 26, 1, 7, 214, 200, 196) end rect(35, 27, 15, 1, 252, 246, 236); rect(35, 30, 15, 1, 252, 246, 236) rect(35, 28, 15, 1, 214, 200, 196); rect(35, 31, 15, 1, 214, 200, 196) for _, f in ipairs({ { 28, 33 }, { 33, 34 }, { 52, 33 }, { 55, 34 }, { 30, 31 }, { 54, 31 } }) do px(f[1], f[2], 255, 170, 190); px(f[1] + 1, f[2] - 1, 255, 232, 150) end end local BG = nil local STAGES = { room, bed, function() dream(); BG = gfx.snapshot(0, 0, W, H) end } local STAGE = 1 local PAL = { 0, 0, 0, 252, 250, 244, 222, 214, 230, 84, 68, 86 } local function sheep_sprite(pose) local c = {} for i = 1, 12 * 8 do c[i] = 0 end local function P(x, y, v) if x >= 0 and x < 12 and y >= 0 and y < 8 then c[y * 12 + x + 1] = v end end local function R(x, y, w, h, v) for j = y, y + h - 1 do for i = x, x + w - 1 do P(i, j, v) end end end local function L(x0, y0, x1, y1, v) local n = max(math.abs(x1 - x0), math.abs(y1 - y0)) for i = 0, n do P(floor(x0 + (x1 - x0) * i / n + 0.5), floor(y0 + (y1 - y0) * i / n + 0.5), v) end end R(2, 0, 6, 1, 1); R(1, 1, 8, 3, 1); R(2, 4, 6, 1, 2) if pose == 3 then R(9, 3, 3, 3, 3) else R(9, 2, 3, 3, 3) end if pose == 0 or pose == 3 then L(2, 5, 2, 7, 3); L(3, 5, 3, 7, 3); L(7, 5, 7, 7, 3); L(8, 5, 8, 7, 3) elseif pose == 1 then L(2, 5, 0, 7, 3); L(3, 5, 3, 7, 3); L(7, 5, 9, 7, 3); L(8, 5, 8, 7, 3) else L(2, 5, 0, 6, 3); L(3, 5, 1, 7, 3); L(7, 5, 11, 6, 3); L(8, 5, 10, 7, 3) end local rows = {} for y = 0, 7 do rows[y + 1] = string.char(table.unpack(c, y * 12 + 1, y * 12 + 12)) end return table.concat(rows) end local SPR = {} for p = 0, 3 do SPR[p] = sheep_sprite(p) end local TAKE, JUMP, RUN_IN, RUN_OUT = 30, 22, 16, 16 local function stall(k) return (k % 4 == 2) and 30 or 0 end local function draw_sheep(k, r) local st = stall(k) local x, lift, pose if r < RUN_IN then x, lift, pose = 14 + r, 0, (r // 3) % 2 elseif r < RUN_IN + st then x, lift, pose = TAKE, 0, ((r // 6) % 2 == 0) and 0 or 3 elseif r < RUN_IN + st + JUMP then local q = (r - RUN_IN - st) / JUMP x, lift, pose = TAKE + (r - RUN_IN - st), floor(32 * q * (1 - q) + 0.5), 2 elseif r < RUN_IN + st + JUMP + RUN_OUT then x, lift, pose = TAKE + JUMP + (r - RUN_IN - st - JUMP), 0, (r // 3) % 2 else return end gfx.blit_indexed(SPR[pose], PAL, x - 5, GROUND - 7 - lift, 12, 8, 12, 8, true) end local PACE = { slow = 120, normal = 90, fast = 66 } local FIRST = RUN_IN + JUMP local NUMFONT = { font = "5x7", w = 11, align = "center" } local NTOP, NBOT = gfx.text_ink("0", NUMFONT) local SKIN, HAIR, BLANKET, HEM = { 232, 184, 150 }, { 96, 64, 52 }, { 58, 86, 176 }, { 110, 134, 220 } local function sleeper(f) local c = f % 2400 local rolled = c >= 1120 and c < 2280 local lift = 0 for _, s in ipairs({ 1100, 2260 }) do if c >= s and c < s + 40 then lift = floor(2.5 * sin(3.1416 * (c - s) / 40) + 0.5) end end local b = sin(f * 0.037) > 0 and 1 or 0 if rolled then disc(12, 46, 3, HAIR[1], HAIR[2], HAIR[3]); px(15, 46, SKIN[1], SKIN[2], SKIN[3]); px(15, 47, SKIN[1], SKIN[2], SKIN[3]) rect(9, 48 - b - lift, 19, 4, BLANKET[1], BLANKET[2], BLANKET[3]) rect(9, 48 - b - lift, 19, 1, HEM[1], HEM[2], HEM[3]) else disc(12, 46, 3, SKIN[1], SKIN[2], SKIN[3]) rect(10, 43, 5, 1, HAIR[1], HAIR[2], HAIR[3]); rect(9, 44, 7, 1, HAIR[1], HAIR[2], HAIR[3]) px(10, 46, 70, 50, 64); px(14, 46, 70, 50, 64); px(12, 48, 190, 120, 120) rect(10, 49 - b - lift, 16, 3, BLANKET[1], BLANKET[2], BLANKET[3]) rect(10, 49 - b - lift, 16, 1, HEM[1], HEM[2], HEM[3]) end end function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) sleeper(frame) if (frame // 45) % 6 == 0 then rect(55, 41, 1, 2, 90, 220, 170) else rect(55, 41, 1, 2, 40, 90, 90) end local twinkle = 0.5 + 0.5 * sin(frame * 0.05) blend(12, 8, 255, 255, 255, 0.6 * twinkle); blend(13, 18, 255, 255, 255, 0.6 * (1 - twinkle)) local P = PACE[cfg.pace or "normal"] or 90 gfx.clip(IX, IY, IW, IH) local k0 = max(0, (frame - 110) // P) for k = k0, frame // P do draw_sheep(k, frame - k * P) end if cfg.num ~= false then rect(BX, BY, BW, BH, 255, 252, 246) rect(BX + 1, BY, BW - 2, 1, 190, 176, 214); rect(BX + 1, BY + BH - 1, BW - 2, 1, 190, 176, 214) rect(BX, BY + 1, 1, BH - 2, 190, 176, 214); rect(BX + BW - 1, BY + 1, 1, BH - 2, 190, 176, 214) local kk = (frame - FIRST) // P local n = 0 if kk >= 0 then n = (frame >= kk * P + FIRST + stall(kk)) and kk + 1 or kk end if n > 0 then gfx.text(BX + 1, BY + 1 + (BH - 2 - (NBOT - NTOP + 1)) // 2 - NTOP, tostring((n - 1) % 99 + 1), 150, 110, 170, NUMFONT) end end gfx.clip() end