Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(pets): eleven new built-in pets (#1122)
Ruben Lütke2ea8f4653b96
diff --git a/docs/pets.md b/docs/pets.mdindex 4e375e47ba..cb0533c76d 100644--- a/docs/pets.md+++ b/docs/pets.md@@ -293,6 +293,17 @@ committed PNGs match the script). User-created pets live in| `ember` | Ember | A baby dragon: smoke rings while thinking, a fire breath on success, a sooty cough on errors, naps in its eggshell || `cocoa` | Cocoa | A chocolate Labrador puppy: tilts its head to listen, digs while it works, sniffs along a scent trail while searching, does zoomies on success, droops its ears on errors and naps in its dog bed || `dodaios` | Dodaios | The hooded figure of the logo in black and white: the hood's tip perks up to listen and droops asleep, three dots hop beside it while it thinks, the cape trails a hop on success, it types on a laptop at its feet, and hangs by the hood with dangling feet when it is carried |+| `pip` | Pip | A penguin chick in grey fluff: taps its beak to think, shivers on errors, belly-slides and tucks its beak in to sleep |+| `hazel` | Hazel | A red squirrel: tufted ears perk to listen, holds an acorn while thinking, flicks one up on success, naps inside her tail |+| `otto` | Otto | A sea otter: cracks a clam on its belly while thinking, does a backflip on success, sleeps floating on its back |+| `nox` | Nox | A little owl: turns its head to listen, wears reading glasses to work, spreads its wings on success, ruffles on errors |+| `kiko` | Kiko | An axolotl: gill fronds fan out to listen and glow while it thinks, droop on errors, floats asleep among bubbles |+| `inky` | Inky | An inkwell with a quill for a tuft: the quill taps while thinking and scribbles while working, ink blots on errors |+| `toasty` | Toasty | A retro toaster: the slots glow while thinking, the toast pops on success, burns and smokes on errors |+| `cassie` | Cassie | An 80s cassette: the reels are its eyes and spin while thinking, music notes while talking, tape spaghetti on errors |+| `sprout` | Sprout | A plant spirit: a leaf unfurls while thinking, a flower blooms on success, the leaves wilt on errors and fold to sleep |+| `flick` | Flick | An original house elf: huge ears shoot up to listen and droop on errors, scrubs while working, sleeps under its ears |+| `aurelia` | Aurelia | An original princess in an emerald gown: tilts her head to listen, writes letters with a quill, twirls on success |## Adding a petdiff --git a/dodaios/society/companion.py b/dodaios/society/companion.pyindex a2ece9b04e..5c07e58b25 100644--- a/dodaios/society/companion.py+++ b/dodaios/society/companion.py@@ -102,6 +102,7 @@ class CompanionAppearance(BaseModel):#: ``frontend/src/components/society/companion/appearance.ts``.DEFAULT_PETS: tuple[str, ...] = ("ember", "miso", "bolt", "brew", "mochi", "shelly", "cocoa", "dodaios",+ "pip", "hazel", "otto", "nox", "kiko", "inky", "toasty", "cassie", "sprout", "flick", "aurelia",)diff --git a/dodaios/ui/pets/builtin/aurelia/acts.png b/dodaios/ui/pets/builtin/aurelia/acts.pngnew file mode 100644index 0000000000..cf4b47412cBinary files /dev/null and b/dodaios/ui/pets/builtin/aurelia/acts.png differdiff --git a/dodaios/ui/pets/builtin/aurelia/pet.json b/dodaios/ui/pets/builtin/aurelia/pet.jsonnew file mode 100644index 0000000000..a1826f563f--- /dev/null+++ b/dodaios/ui/pets/builtin/aurelia/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "aurelia",+ "name": "Aurelia",+ "description": "A gentle princess in an emerald gown who tilts her head to listen, writes letters with a quill and twirls when something goes right.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "curtsy": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "twirl": {+ "row": 1,+ "frames": 8,+ "fps": 9,+ "loop": false+ },+ "hair_flip": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "fan_flutter": {+ "row": 3,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "tiara_shine": {+ "row": 4,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "wave": {+ "row": 5,+ "frames": 8,+ "fps": 7,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/aurelia/sheet.png b/dodaios/ui/pets/builtin/aurelia/sheet.pngnew file mode 100644index 0000000000..0cd85c8653Binary files /dev/null and b/dodaios/ui/pets/builtin/aurelia/sheet.png differdiff --git a/dodaios/ui/pets/builtin/cassie/acts.png b/dodaios/ui/pets/builtin/cassie/acts.pngnew file mode 100644index 0000000000..626bccd4b5Binary files /dev/null and b/dodaios/ui/pets/builtin/cassie/acts.png differdiff --git a/dodaios/ui/pets/builtin/cassie/pet.json b/dodaios/ui/pets/builtin/cassie/pet.jsonnew file mode 100644index 0000000000..f08a868cb6--- /dev/null+++ b/dodaios/ui/pets/builtin/cassie/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "cassie",+ "name": "Cassie",+ "description": "A groovy 80s cassette whose reels spin while it thinks and hum along to music.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "rewind": {+ "row": 0,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "pencil_wind": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "music_notes": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "tape_tangle": {+ "row": 3,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "flip_side": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "dance_bop": {+ "row": 5,+ "frames": 8,+ "fps": 8,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/cassie/sheet.png b/dodaios/ui/pets/builtin/cassie/sheet.pngnew file mode 100644index 0000000000..c7d6ac2bd6Binary files /dev/null and b/dodaios/ui/pets/builtin/cassie/sheet.png differdiff --git a/dodaios/ui/pets/builtin/flick/acts.png b/dodaios/ui/pets/builtin/flick/acts.pngnew file mode 100644index 0000000000..134d1e507fBinary files /dev/null and b/dodaios/ui/pets/builtin/flick/acts.png differdiff --git a/dodaios/ui/pets/builtin/flick/pet.json b/dodaios/ui/pets/builtin/flick/pet.jsonnew file mode 100644index 0000000000..922d8a3ff2--- /dev/null+++ b/dodaios/ui/pets/builtin/flick/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "flick",+ "name": "Flick",+ "description": "A tidy house elf: its huge ears shoot up to listen and droop when things go wrong.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "bow": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "feather_duster": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "ear_wiggle": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "polish_shine": {+ "row": 3,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "tool_juggle": {+ "row": 4,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "cap_tip": {+ "row": 5,+ "frames": 8,+ "fps": 6,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/flick/sheet.png b/dodaios/ui/pets/builtin/flick/sheet.pngnew file mode 100644index 0000000000..613407bc26Binary files /dev/null and b/dodaios/ui/pets/builtin/flick/sheet.png differdiff --git a/dodaios/ui/pets/builtin/hazel/acts.png b/dodaios/ui/pets/builtin/hazel/acts.pngnew file mode 100644index 0000000000..643a6fd307Binary files /dev/null and b/dodaios/ui/pets/builtin/hazel/acts.png differdiff --git a/dodaios/ui/pets/builtin/hazel/pet.json b/dodaios/ui/pets/builtin/hazel/pet.jsonnew file mode 100644index 0000000000..801146ad5e--- /dev/null+++ b/dodaios/ui/pets/builtin/hazel/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "hazel",+ "name": "Hazel",+ "description": "A red squirrel who perks her tufted ears to listen and naps inside her tail.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "acorn_toss": {+ "row": 0,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "tail_groom": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "bury_nut": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "cheek_stuff": {+ "row": 3,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "nibble": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "ear_scratch": {+ "row": 5,+ "frames": 8,+ "fps": 8,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/hazel/sheet.png b/dodaios/ui/pets/builtin/hazel/sheet.pngnew file mode 100644index 0000000000..c5fe34d7f5Binary files /dev/null and b/dodaios/ui/pets/builtin/hazel/sheet.png differdiff --git a/dodaios/ui/pets/builtin/inky/acts.png b/dodaios/ui/pets/builtin/inky/acts.pngnew file mode 100644index 0000000000..0ef3fc3687Binary files /dev/null and b/dodaios/ui/pets/builtin/inky/acts.png differdiff --git a/dodaios/ui/pets/builtin/inky/pet.json b/dodaios/ui/pets/builtin/inky/pet.jsonnew file mode 100644index 0000000000..8d68d2ddbb--- /dev/null+++ b/dodaios/ui/pets/builtin/inky/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "inky",+ "name": "Inky",+ "description": "A little inkwell with a quill for a tuft that writes down every idea.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "quill_flourish": {+ "row": 0,+ "frames": 8,+ "fps": 9,+ "loop": false+ },+ "ink_slosh": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "blot_oops": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "signature": {+ "row": 3,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "feather_preen": {+ "row": 4,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "bubble_pop": {+ "row": 5,+ "frames": 8,+ "fps": 8,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/inky/sheet.png b/dodaios/ui/pets/builtin/inky/sheet.pngnew file mode 100644index 0000000000..78f5b4dfa4Binary files /dev/null and b/dodaios/ui/pets/builtin/inky/sheet.png differdiff --git a/dodaios/ui/pets/builtin/kiko/acts.png b/dodaios/ui/pets/builtin/kiko/acts.pngnew file mode 100644index 0000000000..cc3bb934e1Binary files /dev/null and b/dodaios/ui/pets/builtin/kiko/acts.png differdiff --git a/dodaios/ui/pets/builtin/kiko/pet.json b/dodaios/ui/pets/builtin/kiko/pet.jsonnew file mode 100644index 0000000000..c436e13d52--- /dev/null+++ b/dodaios/ui/pets/builtin/kiko/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "kiko",+ "name": "Kiko",+ "description": "A dreamy pink axolotl whose gill fronds fan out to listen and glow as it thinks.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "bubble_blow": {+ "row": 0,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "frond_wave": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "tail_swish": {+ "row": 2,+ "frames": 8,+ "fps": 9,+ "loop": false+ },+ "smile_wiggle": {+ "row": 3,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "regenerate_sparkle": {+ "row": 4,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "float_spin": {+ "row": 5,+ "frames": 8,+ "fps": 9,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/kiko/sheet.png b/dodaios/ui/pets/builtin/kiko/sheet.pngnew file mode 100644index 0000000000..dd6b0f5e23Binary files /dev/null and b/dodaios/ui/pets/builtin/kiko/sheet.png differdiff --git a/dodaios/ui/pets/builtin/nox/acts.png b/dodaios/ui/pets/builtin/nox/acts.pngnew file mode 100644index 0000000000..0ebb65cfdbBinary files /dev/null and b/dodaios/ui/pets/builtin/nox/acts.png differdiff --git a/dodaios/ui/pets/builtin/nox/pet.json b/dodaios/ui/pets/builtin/nox/pet.jsonnew file mode 100644index 0000000000..d95841735f--- /dev/null+++ b/dodaios/ui/pets/builtin/nox/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "nox",+ "name": "Nox",+ "description": "A little owl who turns its head to listen, puts on reading glasses to work and spreads its wings when something goes right.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "head_spin": {+ "row": 0,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "wing_stretch": {+ "row": 1,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "slow_blink": {+ "row": 2,+ "frames": 8,+ "fps": 5,+ "loop": false+ },+ "read_book": {+ "row": 3,+ "frames": 8,+ "fps": 5,+ "loop": false+ },+ "feather_ruffle": {+ "row": 4,+ "frames": 8,+ "fps": 9,+ "loop": false+ },+ "hoot": {+ "row": 5,+ "frames": 8,+ "fps": 6,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/nox/sheet.png b/dodaios/ui/pets/builtin/nox/sheet.pngnew file mode 100644index 0000000000..ab60213dd4Binary files /dev/null and b/dodaios/ui/pets/builtin/nox/sheet.png differdiff --git a/dodaios/ui/pets/builtin/otto/acts.png b/dodaios/ui/pets/builtin/otto/acts.pngnew file mode 100644index 0000000000..65110532aeBinary files /dev/null and b/dodaios/ui/pets/builtin/otto/acts.png differdiff --git a/dodaios/ui/pets/builtin/otto/pet.json b/dodaios/ui/pets/builtin/otto/pet.jsonnew file mode 100644index 0000000000..ac9e9db981--- /dev/null+++ b/dodaios/ui/pets/builtin/otto/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "otto",+ "name": "Otto",+ "description": "A sea otter who cracks clams on its belly while it thinks and naps floating on its back.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "clam_crack": {+ "row": 0,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "roll_over": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "juggle_pebble": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "whisker_wash": {+ "row": 3,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "belly_float": {+ "row": 4,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "splash": {+ "row": 5,+ "frames": 8,+ "fps": 8,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/otto/sheet.png b/dodaios/ui/pets/builtin/otto/sheet.pngnew file mode 100644index 0000000000..4e9875c43cBinary files /dev/null and b/dodaios/ui/pets/builtin/otto/sheet.png differdiff --git a/dodaios/ui/pets/builtin/pip/acts.png b/dodaios/ui/pets/builtin/pip/acts.pngnew file mode 100644index 0000000000..e5fae573b8Binary files /dev/null and b/dodaios/ui/pets/builtin/pip/acts.png differdiff --git a/dodaios/ui/pets/builtin/pip/pet.json b/dodaios/ui/pets/builtin/pip/pet.jsonnew file mode 100644index 0000000000..e9d90fa924--- /dev/null+++ b/dodaios/ui/pets/builtin/pip/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "pip",+ "name": "Pip",+ "description": "A penguin chick in grey fluff who waddles to listen and belly-slides when something goes right.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "belly_slide": {+ "row": 0,+ "frames": 8,+ "fps": 9,+ "loop": false+ },+ "snowball": {+ "row": 1,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "fish_snack": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "flipper_flap": {+ "row": 3,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "tumble": {+ "row": 4,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "preen": {+ "row": 5,+ "frames": 8,+ "fps": 6,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/pip/sheet.png b/dodaios/ui/pets/builtin/pip/sheet.pngnew file mode 100644index 0000000000..f4ebe06fb6Binary files /dev/null and b/dodaios/ui/pets/builtin/pip/sheet.png differdiff --git a/dodaios/ui/pets/builtin/sprout/acts.png b/dodaios/ui/pets/builtin/sprout/acts.pngnew file mode 100644index 0000000000..44fe55d61bBinary files /dev/null and b/dodaios/ui/pets/builtin/sprout/acts.png differdiff --git a/dodaios/ui/pets/builtin/sprout/pet.json b/dodaios/ui/pets/builtin/sprout/pet.jsonnew file mode 100644index 0000000000..fcb373059e--- /dev/null+++ b/dodaios/ui/pets/builtin/sprout/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "sprout",+ "name": "Sprout",+ "description": "A little forest plant spirit whose leaves perk up to listen and who bursts into bloom when something goes right.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "bloom": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "leaf_wave": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "water_drop": {+ "row": 2,+ "frames": 8,+ "fps": 7,+ "loop": false+ },+ "sun_stretch": {+ "row": 3,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "butterfly_visit": {+ "row": 4,+ "frames": 8,+ "fps": 6,+ "loop": false+ },+ "seed_puff": {+ "row": 5,+ "frames": 8,+ "fps": 7,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/sprout/sheet.png b/dodaios/ui/pets/builtin/sprout/sheet.pngnew file mode 100644index 0000000000..c8b104d7eaBinary files /dev/null and b/dodaios/ui/pets/builtin/sprout/sheet.png differdiff --git a/dodaios/ui/pets/builtin/toasty/acts.png b/dodaios/ui/pets/builtin/toasty/acts.pngnew file mode 100644index 0000000000..4c8414cbdaBinary files /dev/null and b/dodaios/ui/pets/builtin/toasty/acts.png differdiff --git a/dodaios/ui/pets/builtin/toasty/pet.json b/dodaios/ui/pets/builtin/toasty/pet.jsonnew file mode 100644index 0000000000..b3b56d712f--- /dev/null+++ b/dodaios/ui/pets/builtin/toasty/pet.json@@ -0,0 +1,111 @@+{+ "format": "jarvis-pet/1",+ "id": "toasty",+ "name": "Toasty",+ "description": "A cheerful toaster that glows while it thinks and pops up when it's done.",+ "frame_size": 48,+ "sheet": "sheet.png",+ "animations": {+ "idle": {+ "row": 0,+ "frames": 8,+ "fps": 6,+ "loop": true,+ "accent_frames": 1,+ "accent_every": 3+ },+ "listening": {+ "row": 1,+ "frames": 6,+ "fps": 8,+ "loop": true+ },+ "thinking": {+ "row": 2,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "talking": {+ "row": 3,+ "frames": 4,+ "fps": 10,+ "loop": true+ },+ "success": {+ "row": 4,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "error": {+ "row": 5,+ "frames": 6,+ "fps": 9,+ "loop": false+ },+ "sleeping": {+ "row": 6,+ "frames": 6,+ "fps": 3,+ "loop": true+ },+ "working": {+ "row": 7,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "searching": {+ "row": 8,+ "frames": 8,+ "fps": 8,+ "loop": true+ },+ "held": {+ "row": 9,+ "frames": 6,+ "fps": 10,+ "loop": true+ }+ },+ "acts_sheet": "acts.png",+ "acts": {+ "toast_pop": {+ "row": 0,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "toast_juggle": {+ "row": 1,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "crumb_shake": {+ "row": 2,+ "frames": 8,+ "fps": 10,+ "loop": false+ },+ "lever_bounce": {+ "row": 3,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "butter_shine": {+ "row": 4,+ "frames": 8,+ "fps": 8,+ "loop": false+ },+ "heat_shimmer": {+ "row": 5,+ "frames": 8,+ "fps": 8,+ "loop": false+ }+ }+}diff --git a/dodaios/ui/pets/builtin/toasty/sheet.png b/dodaios/ui/pets/builtin/toasty/sheet.pngnew file mode 100644index 0000000000..0b79569a89Binary files /dev/null and b/dodaios/ui/pets/builtin/toasty/sheet.png differdiff --git a/dodaios/ui/web/frontend/src/components/society/companion/AgentAppearanceDialog.test.tsx b/dodaios/ui/web/frontend/src/components/society/companion/AgentAppearanceDialog.test.tsxindex b77834db4f..9a14242529 100644--- a/dodaios/ui/web/frontend/src/components/society/companion/AgentAppearanceDialog.test.tsx+++ b/dodaios/ui/web/frontend/src/components/society/companion/AgentAppearanceDialog.test.tsx@@ -3,14 +3,14 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { afterEach, expect, it, vi } from "vitest";import { AgentAppearanceDialog } from "./AgentAppearanceDialog";import { SAMPLE_ROSTER } from "../mockRoster";-import { defaultCompanion } from "./appearance";+import { DEFAULT_PETS, defaultCompanion } from "./appearance";vi.mock("@/i18n", () => ({ useT: () => (key: string) => key }));vi.mock("./CompanionPreview", () => ({ CompanionPreview: () => <div data-testid="preview" /> }));vi.mock("../figures/AgentFigureViewer", () => ({ AgentFigureViewer: () => <div data-testid="character-preview" /> }));afterEach(() => { cleanup(); vi.unstubAllGlobals(); });-const PETS = { active: "gigi", scale: 1, bubble: true, strip_always: false, visible: true, pets: ["gigi", "ember", "miso", "bolt", "brew", "mochi", "shelly", "cocoa", "dodaios"].map(id => ({+const PETS = { active: "gigi", scale: 1, bubble: true, strip_always: false, visible: true, pets: ["gigi", ...DEFAULT_PETS].map(id => ({id, name: id, description: "", builtin: true, frame_size: 48, animations: { idle: { row: 0, frames: 1, fps: 1, loop: true } }, sheet_url: `/api/pets/${id}/sheet.png`,})) };diff --git a/dodaios/ui/web/frontend/src/components/society/companion/appearance.ts b/dodaios/ui/web/frontend/src/components/society/companion/appearance.tsindex 22446a6746..dbe1f0e827 100644--- a/dodaios/ui/web/frontend/src/components/society/companion/appearance.ts+++ b/dodaios/ui/web/frontend/src/components/society/companion/appearance.ts@@ -21,7 +21,10 @@ export const PET_ID = /^[a-z0-9][a-z0-9-]{0,31}$/;* The built-in pets an agent wears when nobody picked one (`DEFAULT_PETS` in* `dodaios/society/companion.py`). Gigi stays the lead's own face.*/-export const DEFAULT_PETS = ["ember", "miso", "bolt", "brew", "mochi", "shelly", "cocoa", "dodaios"] as const;+export const DEFAULT_PETS = [+ "ember", "miso", "bolt", "brew", "mochi", "shelly", "cocoa", "dodaios",+ "pip", "hazel", "otto", "nox", "kiko", "inky", "toasty", "cassie", "sprout", "flick", "aurelia",+] as const;export const companionSchema = z.object({shape: z.enum(COMPANION_SHAPES), color: z.string().regex(/^#[0-9a-fA-F]{6}$/),eyes: z.enum(["dots", "lines"]).default("lines"), enabled: z.boolean().default(true),diff --git a/scripts/pets/build_pets.py b/scripts/pets/build_pets.pyindex 7304901905..bf48c2731f 100644--- a/scripts/pets/build_pets.py+++ b/scripts/pets/build_pets.py@@ -9029,7 +9029,44 @@ DODAIOS = PetDesign()-PETS: tuple[PetDesign, ...] = (GIGI, MISO, BREW, BOLT, MOCHI, SHELLY, EMBER, COCOA, DODAIOS)+CORE_PETS: tuple[PetDesign, ...] = (GIGI, MISO, BREW, BOLT, MOCHI, SHELLY, EMBER, COCOA, DODAIOS)++#: Pets drawn in their own module, ``scripts/pets/designs/<id>.py``, in the+#: order they ship. Each module defines ``design(bp) -> PetDesign`` and gets+#: this builder module as ``bp``, so it uses the same helpers, palette and+#: poses without a circular import.+DESIGN_MODULES: tuple[str, ...] = (+ "pip", "hazel", "otto", "nox", "kiko", "inky", "toasty", "cassie", "sprout", "flick", "aurelia",+)+DESIGNS_DIR = Path(__file__).resolve().parent / "designs"+#: This builder, handed to every design module. Captured while the module runs,+#: because a loader (like the test) may unregister it from ``sys.modules`` later.+_THIS_MODULE = sys.modules[__name__]+++def load_design(pet_id: str) -> PetDesign:+ """Load ``designs/<pet_id>.py`` and build its pet with this module's helpers."""+ import importlib.util++ path = DESIGNS_DIR / f"{pet_id}.py"+ spec = importlib.util.spec_from_file_location(f"pet_design_{pet_id}", path)+ if spec is None or spec.loader is None:+ raise FileNotFoundError(path)+ module = importlib.util.module_from_spec(spec)+ # Registered while it runs, so dataclasses in a design module resolve their module.+ sys.modules[spec.name] = module+ try:+ spec.loader.exec_module(module)+ finally:+ sys.modules.pop(spec.name, None)+ pet = module.design(_THIS_MODULE)+ if pet.id != pet_id:+ raise ValueError(f"{path.name} builds {pet.id!r}, expected {pet_id!r}")+ return pet+++def all_pets() -> tuple[PetDesign, ...]:+ return CORE_PETS + tuple(load_design(pet_id) for pet_id in DESIGN_MODULES)# ---------------------------------------------------------------------------@@ -9192,10 +9229,15 @@ def save_json(data: dict, path: Path) -> None:path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8", newline="\n")-def build_all(out_dir: Path) -> list[Path]:- """Write ``out_dir/builtin/<id>/`` for every pet and ``out_dir/template/``."""+def build_all(out_dir: Path, only: Sequence[str] | None = None) -> list[Path]:+ """Write ``out_dir/builtin/<id>/`` for every pet (or ``only`` these) and ``template/``."""written: list[Path] = []- for pet in PETS:+ if only:+ core = {pet.id: pet for pet in CORE_PETS}+ pets: tuple[PetDesign, ...] = tuple(core[i] if i in core else load_design(i) for i in only)+ else:+ pets = all_pets()+ for pet in pets:folder = out_dir / "builtin" / pet.idfolder.mkdir(parents=True, exist_ok=True)save_png(build_sheet(pet), folder / "sheet.png")@@ -9221,8 +9263,14 @@ def main(argv: Sequence[str] | None = None) -> int:default=REPO_ROOT / "dodaios" / "ui" / "pets",help="folder that receives builtin/ and template/ (default: the package)",)+ parser.add_argument(+ "--only",+ default="",+ help="comma-separated pet ids to build (a preview; the default builds every pet)",+ )args = parser.parse_args(argv)- for path in build_all(args.out):+ only = [pet_id for pet_id in args.only.split(",") if pet_id]+ for path in build_all(args.out, only or None):print(path)return 0diff --git a/scripts/pets/contact_sheet.py b/scripts/pets/contact_sheet.pyindex 671c51284a..bf73e53951 100644--- a/scripts/pets/contact_sheet.py+++ b/scripts/pets/contact_sheet.py@@ -89,14 +89,23 @@ def main(argv: Sequence[str] | None = None) -> int:parser.add_argument("--scale", type=int, default=4, help="integer zoom (default 4)")parser.add_argument("--pets", default="", help="comma-separated ids (default: all)")parser.add_argument("--split", action="store_true", help="also write one image per pet")+ parser.add_argument(+ "--root",+ type=Path,+ default=None,+ help="a builtin/ folder written by `build_pets.py --out` (preview; needs --pets)",+ )args = parser.parse_args(argv)wanted = {p for p in args.pets.split(",") if p}- packs = [- load_pet(builtin_root() / m.id, builtin=True)- for m in list_pets()- if m.builtin and (not wanted or m.id in wanted)- ]+ if args.root is not None:+ packs = [load_pet(args.root / pet_id, builtin=True) for pet_id in sorted(wanted)]+ else:+ packs = [+ load_pet(builtin_root() / m.id, builtin=True)+ for m in list_pets()+ if m.builtin and (not wanted or m.id in wanted)+ ]if not packs:print("no matching built-in pets", file=sys.stderr)return 1diff --git a/scripts/pets/designs/aurelia.py b/scripts/pets/designs/aurelia.pynew file mode 100644index 0000000000..a2fefca4ed--- /dev/null+++ b/scripts/pets/designs/aurelia.py@@ -0,0 +1,926 @@+"""Aurelia: a kind, poised princess in an emerald-and-ivory ball gown.++A chibi figure that uses the full cell height: a big head with long, softly+waving chestnut-to-copper hair, a delicate gold tiara with a small sapphire,+big blue-violet eyes, and a bell skirt with gold trim that sways and flares.+The face, the hair and the gown are drawn as left halves mirrored across the+cell's centre line, so she stays perfectly symmetric; each frame's ``_Look``+moves the arms, tilts the head and swings the hair and the hem.+"""++import math+from dataclasses import dataclass, replace++Px = tuple[int, int]+++@dataclass(frozen=True)+class _Look:+ """How one frame poses the figure beyond the shared ``Pose``."""++ #: Head offset from the body (a tilt or a bow).+ head: Px = (0, 0)+ #: Arm paths from each shoulder: an optional elbow, then the hand.+ left: tuple[Px, ...] = ((16, 35),)+ right: tuple[Px, ...] = ((31, 35),)+ #: Which arm is drawn in front of the head and hair (left, right).+ front: tuple[bool, bool] = (False, False)+ #: Hair swing (+ = the tips swing right).+ sway: int = 0+ #: Skirt hem swing (+ = right) and flare (+ = wider and lifted).+ hem: int = 0+ flare: int = 0+ brows: str = ""+ view: str = "front"+ sit: bool = False+ feet: bool = False+ prop: str = ""+ prop_i: int = 0+ #: Narrowing of the whole figure for a twirl: 1.0 = none.+ squash: float = 1.0+ #: Where the gleam runs along the tiara (-1 = none).+ shine: int = -1+++def design(bp):+ hexc = bp.hexc+ ellipse, rect, line, polygon = bp.ellipse, bp.rect, bp.line, bp.polygon+ mirrored, thick, outer_ring = bp.mirrored, bp.thick, bp.outer_ring+ Pose = bp.Pose+ cx = bp.CX++ # -- Palette -------------------------------------------------------------+ skin = hexc("#ffe2d0")+ skin_light = hexc("#fff2e8")+ skin_shade = hexc("#f2bea6")+ skin_deep = hexc("#dd9c88")+ blush = hexc("#ff9cb0")+ blush_soft = hexc("#ffc6cf")+ lip = hexc("#e0607c")+ lip_dark = hexc("#a83450")++ hair_deep = hexc("#4a1c16")+ hair_dark = hexc("#73301f")+ hair = hexc("#9c4428")+ hair_mid = hexc("#bc5d34")+ hair_light = hexc("#da8148")+ hair_shine = hexc("#f6bb84")++ iris_dark = hexc("#33238a")+ iris = hexc("#4a3bb2")+ iris_light = hexc("#7f76e6")+ iris_glow = hexc("#b9b6ff")+ pupil = hexc("#150c30")+ glint = hexc("#ffffff")+ lash = hexc("#2a1420")++ emerald_deep = hexc("#0b3b2d")+ emerald_dark = hexc("#125e48")+ emerald = hexc("#1b8761")+ emerald_light = hexc("#35ab7b")+ emerald_shine = hexc("#7ad8a7")++ ivory = hexc("#fcf5e2")+ ivory_shade = hexc("#e7d7b2")++ gold = hexc("#f1c04b")+ gold_light = hexc("#fff1a8")+ gold_dark = hexc("#b4812a")+ sapphire = hexc("#3263e3")+ sapphire_light = hexc("#b2cdff")+ sapphire_dark = hexc("#1b3592")++ ink = hexc("#2b2440")+ paper = hexc("#fdf8ec")+ paper_shade = hexc("#e6d8bb")+ seal = hexc("#d2384e")+ feather = hexc("#f8f6ff")+ feather_shade = hexc("#c6c0e2")+ heart = hexc("#ff7aa0")+ heart_light = hexc("#ffd0dd")++ def sym(mask):+ """A left-half mask completed by its mirror image."""+ return set(mask) | mirrored(mask)++ def ramp(mask, light, base, dark, deep=None):+ """Light on the top/left inner edge, dark on the bottom/right band, a deep base rim."""+ out = {}+ for x, y in mask:+ color = base+ if deep is not None and (x, y + 1) not in mask:+ color = deep+ elif (x, y - 1) not in mask or (x - 1, y) not in mask:+ color = light+ elif (x, y + 2) not in mask or (x + 1, y) not in mask:+ color = dark+ out[(x, y)] = color+ return out++ # -- Hair ----------------------------------------------------------------++ #: Strand clumps: dark valley, base, mid, a lit ridge, mid.+ clump = (hair_dark, hair, hair_mid, hair_light, hair_light, hair_mid, hair)++ def crown_tone(mask, origin):+ """Hair radiating from the parting at ``origin``: clumps, a halo, a dark rim."""+ ox, oy = origin+ out = {}+ for x, y in mask:+ dx, dy = x + 0.5 - ox, y + 0.5 - oy+ r = math.hypot(dx, dy)+ # Mirror the angle so both sides of the parting match.+ theta = math.atan2(abs(dx), dy)+ k = theta * 2.6 + 0.1+ color = clump[int((k - math.floor(k)) * len(clump))]+ if 5.5 <= r <= 7.2 and color in (hair_mid, hair_light):+ color = hair_shine+ elif 5.0 <= r <= 7.8 and color == hair:+ color = hair_light+ if (x, y + 1) not in mask:+ color = hair_deep if color in (hair_dark, hair) else hair_dark+ out[(x, y)] = color+ return out++ def flow_tone(mask, phase=0.0):+ """Long hair falling in soft waves: vertical clumps that ripple with depth."""+ out = {}+ for x, y in mask:+ wave = 1.3 * math.sin((y + phase) / 2.7)+ rel = abs(x + 0.5 - cx)+ k = (rel + wave) / 3.4+ color = clump[int((k - math.floor(k)) * len(clump))]+ if (x, y + 1) not in mask:+ color = hair_deep+ elif (x, y + 2) not in mask and color != hair_light:+ color = hair_dark+ out[(x, y)] = color+ return out++ face = frozenset(sym(ellipse(cx, 17.8, 7.6, 7.2) | ellipse(cx, 20.6, 6.0, 5.4)))+ dome = frozenset(sym(ellipse(cx, 14.6, 10.4, 10.2)))++ #: The lower edge of the curtain bangs by distance from the parting.+ #: Uneven by a pixel here and there, so the bangs end in pointed strands.+ bangs_edge = (9.8, 10.8, 12.4, 11.6, 12.6, 13.8, 13.0, 16.4)++ def bangs_mask():+ out = set()+ for x, y in dome:+ d = int(abs(x + 0.5 - cx))+ if y < bangs_edge[min(d, len(bangs_edge) - 1)]:+ out.add((x, y))+ return out++ bangs = frozenset(bangs_mask())++ def side_lock(sway):+ """The lock falling in front of the shoulder (left side), curling at its tip."""+ s = sway * 0.5+ return polygon(+ [+ (13.4, 12),+ (17.0, 14.0),+ (16.8, 18),+ (15.6, 22),+ (16.4 + s * 0.4, 26),+ (16.2 + s * 0.8, 29.6),+ (14.6 + s, 31.8),+ (13.0 + s, 31.0),+ (14.2 + s * 0.8, 29.4),+ (13.4 + s * 0.5, 26),+ (12.4 + s * 0.2, 22),+ (12.4, 17),+ ]+ )++ def back_hair(sway, full=False):+ """The long hair behind the body, waving down past the shoulders.++ ``full`` is the hair seen from behind: one wavy curtain down the back.+ """+ left = [+ (cx, 5.0),+ (16.0, 6.6),+ (12.0, 10.5),+ (10.6, 15),+ (11.0, 19),+ (10.0, 23),+ (10.6, 27),+ (9.4, 31),+ (9.8, 34.6),+ (8.6, 37.4),+ (10.6, 38.6),+ (12.2, 37.0),+ (13.6, 38.4),+ (15.4, 36.4),+ (cx, 34.0),+ ]+ if full:+ left[-2:] = [(16.0, 37.0), (18.6, 39.0), (21.4, 37.4), (cx, 39.0)]+ right = [(2 * cx - x, y) for x, y in reversed(left[1:-1])]++ def swing(p):+ x, y = p+ return (x + sway * max(0.0, y - 18) / 14.0, y)++ return polygon([swing(p) for p in [*left, *right]])++ def draw_tiara(f, shine):+ rows_l = (+ "...g.",+ ".g.gG",+ "gGgGs",+ "dGGGs",+ ".dddd",+ )+ palette = {"g": gold_light, "G": gold, "d": gold_dark, "s": sapphire}+ x0, y0 = cx - 5, 3+ cells = {}+ for r, text in enumerate(rows_l):+ for c, mark in enumerate(text):+ if mark in palette:+ for px in ((x0 + c, y0 + r), (2 * cx - 1 - (x0 + c), y0 + r)):+ cells[px] = palette[mark]+ f.paint(outer_ring(set(cells)), bp.OUTLINE)+ f.paint_map(cells)+ # The sapphire: a lit facet top left, a deep one bottom right.+ f.paint({(cx - 1, y0 + 2)}, sapphire_light)+ f.paint({(cx, y0 + 3)}, sapphire_dark)+ if shine >= 0:+ gx = x0 + shine+ f.paint({(gx, y0 + 3), (gx + 1, y0 + 2)}, glint)++ # -- Face ------------------------------------------------------------------++ #: Left eyes, five columns from x=17; the right eye is the mirror image.+ eye_rows = {+ "open": (+ "LLLLL",+ "DWWDD",+ "DWWPD",+ "DIPPD",+ "DlliD",+ ".DDD.",+ ),+ "wide": (+ "LLLLL",+ "DWWDD",+ "DWWPD",+ "DIPPD",+ "DIPPD",+ "DlliD",+ ".DDD.",+ ),+ "up": (+ "LLLLL",+ "DWPPD",+ "DWPPD",+ "DIIID",+ "DlliD",+ ".DDD.",+ ),+ "down": (+ ".....",+ ".....",+ "LLLLL",+ "DIPPD",+ "DlPPD",+ ".DDD.",+ ),+ "closed": (+ ".....",+ ".....",+ ".....",+ "LLLLL",+ ".L..L",+ ".....",+ ),+ "sleep": (+ ".....",+ ".....",+ ".....",+ "L....",+ ".LLLL",+ ".....",+ ),+ "happy": (+ ".....",+ ".....",+ "..LL.",+ ".L..L",+ "L....",+ ".....",+ ),+ "worry": (+ ".....",+ "LLLL.",+ "DWWDL",+ "DWWPD",+ "DlliD",+ ".DDD.",+ ),+ }+ eye_palette = {+ "L": lash,+ "W": glint,+ "w": iris_glow,+ "I": iris,+ "D": iris_dark,+ "P": pupil,+ "l": iris_light,+ "i": iris_glow,+ }+ eye_names = {+ "open": "open",+ "wide": "wide",+ "up_left": "up",+ "up_right": "up",+ "look_left": "open",+ "look_right": "open",+ "down": "down",+ "closed": "closed",+ "sleep": "sleep",+ "happy": "happy",+ "x": "worry",+ "worry": "worry",+ }++ def draw_eyes(f, style):+ name = eye_names.get(style, "open")+ look = 0+ if style in ("up_left", "look_left"):+ look = -1+ elif style in ("up_right", "look_right"):+ look = 1+ x, y = 17, 14+ for r, text in enumerate(eye_rows[name]):+ for c, mark in enumerate(text):+ if mark not in eye_palette:+ continue+ shift = look if mark in "PW" else 0+ lx = x + c + shift+ rx = 2 * cx - 1 - (x + c) + shift+ f.paint({(lx, y + r), (rx, y + r)}, eye_palette[mark])++ def draw_mouth(f, style):+ y = 22+ a, b = cx - 1, cx+ if style in ("rest", "closed"):+ f.paint({(a, y), (b, y)}, lip)+ elif style == "smile":+ f.paint({(a - 1, y - 1), (b + 1, y - 1)}, lip)+ f.paint({(a, y), (b, y)}, lip_dark)+ elif style == "frown":+ f.paint({(a - 1, y + 1), (b + 1, y + 1)}, lip)+ f.paint({(a, y), (b, y)}, lip_dark)+ elif style == "half":+ f.paint({(a, y), (b, y)}, lip_dark)+ f.paint({(a, y + 1), (b, y + 1)}, lip)+ elif style == "open":+ f.paint({(a - 1, y), (b + 1, y)}, lip)+ f.paint({(a, y), (b, y)}, bp.MOUTH_DARK)+ f.paint({(a, y + 1), (b, y + 1)}, bp.TONGUE)+ elif style == "wide":+ f.paint({(a - 1, y), (b + 1, y), (a - 1, y + 1), (b + 1, y + 1)}, lip)+ f.paint({(a, y), (b, y), (a, y + 1), (b, y + 1)}, bp.MOUTH_DARK)+ f.paint({(a, y + 2), (b, y + 2)}, bp.TONGUE)+ elif style == "o":+ f.paint({(a, y), (b, y), (a, y + 1), (b, y + 1)}, bp.MOUTH_DARK)++ def draw_face(f):+ colors = {}+ for x, y in face:+ color = skin+ if (x, y + 1) not in face or ((x + 1, y) not in face and x > cx):+ color = skin_shade+ elif (x - 1, y) not in face and x < cx:+ color = skin_light+ colors[(x, y)] = color+ f.part(set(face), colors)+ f.paint(sym({(16, 21), (18, 21)}), blush_soft)+ f.paint(sym({(17, 21)}), blush)+ f.paint({(cx - 1, 25), (cx, 25)}, skin_deep)++ def draw_features(f, pose, look):+ draw_eyes(f, pose.eyes)+ if look.brows == "worry":+ f.paint({(18, 12), (19, 11), (28, 11), (29, 12)}, hair_dark)+ draw_mouth(f, pose.mouth)++ def draw_head(f, pose, look):+ if look.view == "back":+ head = set(dome) | sym(side_lock(0))+ f.part(head, crown_tone(head, (cx, 1)))+ draw_tiara(f, look.shine)+ return+ draw_face(f)+ top = set(bangs)+ f.part(top, crown_tone(top, (cx, 3)))+ # Where the bangs meet the forehead: a soft shadow, not an ink line.+ f.paint(outer_ring(top) & face, skin_shade)+ draw_features(f, pose, look)+ lock_l = side_lock(look.sway)+ lock_r = mirrored(side_lock(-look.sway))+ for lock in (lock_l, lock_r):+ f.part(lock, flow_tone(lock, 1.0))+ draw_tiara(f, look.shine)++ # -- Body ------------------------------------------------------------------++ bodice = frozenset(sym(polygon([(19.4, 25.6), (cx, 25.6), (cx, 33.0), (20.6, 33.0)])))+ sleeve = frozenset(sym(ellipse(19.0, 28.0, 3.0, 2.6)))++ def skirt_mask(hem, flare, sit):+ if sit:+ return sym(ellipse(cx, 41.6, 16.0, 5.4)) | sym(+ polygon([(20.4, 32.0), (cx, 32.0), (cx, 40.0), (14.0, 40.0)])+ )+ g = flare+ left = [+ (20.4, 32.4),+ (17.0 - g * 0.3, 35.5),+ (13.2 - g * 0.8, 39.6 - g * 0.2),+ (9.4 - g * 1.4, 44.0 - g * 0.6),+ (8.8 - g * 1.5, 45.9 - g * 0.6),+ ]+ right = [(2 * cx - x, y) for x, y in reversed(left)]+ out = polygon([*left, *right])+ if hem:+ out = {(x + round(hem * max(0, y - 35) / 10.0), y) for x, y in out}+ return out++ def draw_skirt(f, look):+ mask = skirt_mask(look.hem, look.flare, look.sit)+ colors = ramp(mask, emerald_light, emerald, emerald_dark, emerald_deep)+ bottom = max(y for _, y in mask)+ top = min(y for _, y in mask)+ for x, y in mask:+ lean = look.hem * max(0, y - 35) / 10.0+ rel = x + 0.5 - cx - lean+ spread = 1.0 + (y - top) * 0.17+ band = abs(rel) / spread+ # Folds fanning out from the waist: a lit ridge, a shadowed valley.+ if colors[(x, y)] == emerald:+ if abs(band - 4.0) < 0.5:+ colors[(x, y)] = emerald_light+ elif abs(band - 4.0) < 0.9 and y > top + 5:+ colors[(x, y)] = emerald_shine if rel < 0 else emerald_light+ elif abs(band - 6.3) < 0.5:+ colors[(x, y)] = emerald_dark+ # The ivory front panel with gold edges.+ half = 0.5 + (y - top) * 0.3+ if y >= top + 2 and abs(rel) < half and not look.sit and look.view != "back":+ if abs(rel) >= half - 1:+ colors[(x, y)] = gold+ elif rel > half - 2.2:+ colors[(x, y)] = ivory_shade+ else:+ colors[(x, y)] = ivory+ for x, y in mask:+ if y == bottom - 1 and colors[(x, y)] not in (ivory, ivory_shade, gold):+ colors[(x, y)] = gold+ if y == bottom:+ colors[(x, y)] = gold_dark+ f.part(mask, colors)+ if look.sit:+ return+ # An ivory ruffle peeking under the hem.+ xs = sorted(x for x, y in mask if y == bottom)+ ruffle = {(x, bottom + 1) for x in xs[1:-1] if (x - look.hem) % 3 != 1}+ f.part(ruffle, {p: ivory_shade if p[0] % 3 else ivory for p in ruffle})+ if look.feet:+ # Dangling: ivory stockings and emerald slippers below the ruffle.+ for sx in (cx - 4, cx + 2):+ lx = sx + 1 - look.hem // 2+ leg = rect(lx, bottom + 1, lx, bottom + 2)+ shoe = rect(sx - look.hem, bottom + 3, sx + 1 - look.hem, bottom + 3)+ foot = leg | shoe+ f.part(foot, {p: emerald_dark if p in shoe else ivory_shade for p in foot})++ def draw_bodice(f):+ mask = set(bodice)+ colors = ramp(mask, emerald_light, emerald, emerald_dark, emerald_deep)+ for x, y in mask:+ if x in (cx - 1, cx) and y >= 27:+ colors[(x, y)] = ivory if y % 2 else ivory_shade+ if y == 26:+ colors[(x, y)] = gold if x not in (cx - 1, cx) else gold_light+ f.part(mask, colors)+ sash = {(x, 33) for x in range(20, 2 * cx - 20)}+ f.part(sash, {p: (gold_light if p[0] in (cx - 1, cx) else gold) for p in sash})+ f.paint({(cx - 2, 24), (cx - 1, 24), (cx, 24), (cx + 1, 24)}, skin_shade)+ f.paint({(cx - 1, 25), (cx, 25)}, skin_shade)++ def draw_sleeves(f):+ mask = set(sleeve)+ f.part(mask, ramp(mask, emerald_shine, emerald_light, emerald, emerald_dark))+ f.paint({(x, y) for x, y in mask if (x, y + 1) not in mask}, gold)++ def draw_arm(f, start, path):+ pts = [start, *path]+ stroke = set()+ for a, b in zip(pts, pts[1:], strict=False):+ stroke |= thick(line(*a, *b))+ hx, hy = path[-1]+ glove = rect(hx - 1, hy - 1, hx + 1, hy + 1) - {(hx + 1, hy + 1)}+ mask = stroke | glove+ colors = {}+ for p in mask:+ near = abs(p[0] - hx) <= 2 and abs(p[1] - hy) <= 2+ colors[p] = ivory_shade if near else skin+ for p in glove:+ colors[p] = ivory_shade if p[1] == hy + 1 or p[0] == hx + 1 else ivory+ f.part(mask, colors)++ def draw_letter(f, i):+ sheet = rect(16, 34, 31, 41)+ f.part(sheet, {p: (paper_shade if p[1] == 41 or p[0] == 31 else paper) for p in sheet})+ for row in range(min(i, 7)):+ y = 35 + (row // 2) * 2+ x0 = 18 if row % 2 == 0 else 24+ if y <= 39:+ f.paint({(x, y) for x in range(x0, x0 + 5)}, ink)+ f.paint({(29, 39), (30, 39), (29, 40), (30, 40)}, seal)++ def draw_quill(f, hand, k):+ """A white feather quill held in the right glove, its tip on the letter."""+ hx, hy = hand+ vane = polygon(+ [(hx + 1, hy - 2), (hx + 4, hy - 8 - k), (hx + 8, hy - 11 - k), (hx + 6, hy - 5 - k)]+ )+ f.part(vane, {p: (feather if p[0] + p[1] < hx + hy - 6 else feather_shade) for p in vane})+ f.paint(line(hx + 1, hy - 1, hx + 6, hy - 9 - k), feather_shade)+ f.paint({(hx - 1, hy + 1), (hx - 2, hy + 2)}, ink)++ def draw_fan(f, i):+ """A folding fan open above the right glove: ivory pleats, a gold rim."""+ tilt = (0.0, 0.25, 0.0, -0.2)[i % 4]+ px, py = 34, 27+ lo, hi = -math.pi * 0.92 + tilt, -math.pi * 0.30 + tilt+ fan = {}+ for y in range(py - 9, py + 1):+ for x in range(px - 9, px + 9):+ dx, dy = x + 0.5 - px, y + 0.5 - py+ r = math.hypot(dx, dy)+ a = math.atan2(dy, dx)+ if 2.0 <= r <= 8.2 and lo <= a <= hi:+ pleat = int((a - lo) / (hi - lo) * 7) % 2+ color = ivory if pleat else ivory_shade+ if r > 7.0:+ color = gold if pleat else gold_dark+ fan[(x, y)] = color+ f.part(set(fan), fan)+ f.paint({(px - 1, py - 1), (px, py - 1)}, gold_dark)++ def draw_figure(f, pose, look):+ lift = 4 if look.sit else 0+ with f.offset(look.head[0], look.head[1] + lift):+ mane = back_hair(look.sway)+ f.part(mane, flow_tone(mane))+ draw_skirt(f, look)+ with f.offset(0, lift):+ draw_bodice(f)+ draw_sleeves(f)+ if not look.front[0]:+ draw_arm(f, (18, 29), look.left)+ if not look.front[1]:+ draw_arm(f, (29, 29), look.right)+ if look.view == "back":+ # Seen from behind, the long hair falls over the back of the gown.+ with f.offset(*look.head):+ curtain = back_hair(look.sway, full=True)+ f.part(curtain, flow_tone(curtain))+ if look.prop == "letter":+ draw_letter(f, look.prop_i)+ with f.offset(*look.head):+ draw_head(f, pose, look)+ if look.prop == "fan":+ draw_fan(f, look.prop_i)+ if look.front[0]:+ draw_arm(f, (18, 29), look.left)+ if look.front[1]:+ draw_arm(f, (29, 29), look.right)+ if look.prop == "letter":+ draw_quill(f, look.right[-1], look.prop_i % 2)++ # -- Frame tables ----------------------------------------------------------++ Look = _Look+ rest = Look()+ looks = {}++ looks["idle"] = tuple(+ Look(sway=s, hem=h)+ for s, h in zip((0, 0, 1, 1, 1, 0, 0, 0), (0, 0, 0, 1, 0, 0, 0, 0), strict=True)+ )++ ear = ((33, 26), (33, 18))+ looks["listening"] = tuple(+ Look(head=(1, 0), right=ear, front=(False, True), sway=s) for s in (1, 2, 2, 1, 0, 1)+ )++ chin = ((31, 31), (26, 25))+ looks["thinking"] = tuple(Look(right=chin, front=(False, True)) for _ in range(8))++ looks["talking"] = (+ rest,+ rest,+ Look(right=((32, 32), (34, 29))),+ Look(right=((32, 31), (35, 27)), sway=1),+ )++ spin = (1.0, 0.7, 0.45, 0.7, 1.0, 0.7, 0.45, 1.0)+ views = ("front", "front", "back", "back", "back", "back", "front", "front")+ looks["success"] = tuple(+ Look(+ view=views[i],+ squash=spin[i],+ flare=(1, 2, 3, 3, 3, 3, 2, 1)[i],+ sway=(0, -1, -2, -2, -1, 1, 2, 1)[i],+ left=((13, 31),) if 1 <= i <= 6 else ((14, 33),),+ right=((34, 31),) if 1 <= i <= 6 else ((33, 33),),+ )+ for i in range(8)+ )++ forehead = ((12, 23), (17, 14))+ looks["error"] = tuple(+ Look(left=forehead, front=(True, False), brows="worry") for _ in range(6)+ )++ looks["sleeping"] = tuple(+ Look(+ head=(2, 2),+ sit=True,+ left=((22, 33), (28, 28)),+ right=((34, 32), (31, 28)),+ front=(True, True),+ sway=(1, 1, 2, 2, 1, 1)[i],+ )+ for i in range(6)+ )++ pen = ((27, 36), (24, 37), (28, 37), (25, 36), (29, 37), (26, 37), (22, 36), (27, 37))+ looks["working"] = tuple(+ Look(+ left=((15, 37),),+ right=((32, 33), pen[i]),+ front=(False, True),+ prop="letter",+ prop_i=i + 1,+ )+ for i in range(8)+ )++ looks["searching"] = tuple(Look() for _ in range(8))++ looks["held"] = tuple(+ Look(+ left=((14, 34), (12, 37)),+ right=((33, 34), (35, 37)),+ sway=-s,+ hem=-s,+ flare=1 + (i % 2),+ feet=True,+ )+ for i, s in enumerate(bp.HELD_SWAY)+ )++ # -- Idle acts -------------------------------------------------------------++ curtsy_dip = (0, 1, 2, 3, 3, 2, 1, 0)+ flip_hand = (+ rest.right,+ ((33, 27), (33, 23)),+ ((34, 25), (36, 20)),+ ((35, 24), (38, 19)),+ ((34, 25), (36, 21)),+ ((33, 28), (33, 25)),+ rest.right,+ rest.right,+ )+ act_looks = {+ "curtsy": tuple(+ Look(+ head=(0, (0, 0, 1, 1, 1, 1, 0, 0)[i]),+ left=((12, 38),) if 1 <= i <= 6 else rest.left,+ right=((35, 38),) if 1 <= i <= 6 else rest.right,+ flare=(0, 1, 2, 3, 3, 2, 1, 0)[i],+ )+ for i in range(8)+ ),+ "twirl": tuple(+ Look(+ view=views[i],+ squash=spin[i],+ flare=(0, 2, 3, 3, 3, 3, 2, 0)[i],+ sway=(0, -1, -2, -2, -1, 1, 2, 0)[i],+ left=((13, 31),) if 1 <= i <= 6 else rest.left,+ right=((34, 31),) if 1 <= i <= 6 else rest.right,+ )+ for i in range(8)+ ),+ "hair_flip": tuple(+ Look(+ right=flip_hand[i],+ front=(False, 1 <= i <= 5),+ sway=(0, 1, 3, 4, 3, 1, -1, 0)[i],+ )+ for i in range(8)+ ),+ "fan_flutter": tuple(+ Look(+ right=((33, 31), (34, 28)) if 1 <= i <= 6 else rest.right,+ front=(False, 1 <= i <= 6),+ prop="fan" if 1 <= i <= 6 else "",+ prop_i=i,+ sway=(0, 0, 1, 1, 0, 0, 1, 0)[i],+ )+ for i in range(8)+ ),+ "tiara_shine": tuple(+ Look(+ head=(0, -1) if 2 <= i <= 5 else (0, 0),+ left=((22, 31),) if 1 <= i <= 6 else rest.left,+ right=((25, 31),) if 1 <= i <= 6 else rest.right,+ shine=(-1, -1, 0, 2, 4, 6, 8, -1)[i],+ )+ for i in range(8)+ ),+ "wave": tuple(+ Look(+ right=rest.right if i in (0, 7) else ((34, 26), ((36, 20), (38, 19))[i % 2]),+ front=(False, i not in (0, 7)),+ )+ for i in range(8)+ ),+ }+ act_eyes = {+ "curtsy": ("open", "open", "closed", "closed", "closed", "closed", "happy", "open"),+ "twirl": ("open", "happy", "happy", "happy", "happy", "happy", "happy", "open"),+ "hair_flip": ("open", "open", "closed", "closed", "happy", "happy", "open", "open"),+ "fan_flutter": ("open", "happy", "happy", "open", "happy", "happy", "open", "open"),+ "tiara_shine": (+ "open",+ "up_right",+ "up_right",+ "up_right",+ "happy",+ "happy",+ "open",+ "open",+ ),+ "wave": ("open", "happy", "happy", "open", "open", "happy", "happy", "open"),+ }+ act_mouth = {+ "curtsy": "smile",+ "twirl": "smile",+ "hair_flip": "smile",+ "fan_flutter": "rest",+ "tiara_shine": "smile",+ "wave": "smile",+ }++ def look_for(pose):+ rows = act_looks[pose.act] if pose.act else looks[pose.state]+ return rows[pose.i % len(rows)]++ # -- Poses -----------------------------------------------------------------++ def pose_for(state, i):+ p = bp.base_pose(state, i)+ if state == "idle":+ return replace(p, mouth="rest")+ if state == "listening":+ return replace(p, dy=0, stretch=(0, 1, 1, 1, 0, 0)[i], eyes="open", mouth="rest")+ if state == "thinking":+ return replace(p, eyes="up_right", mouth="rest")+ if state == "talking":+ # She lifts a pixel while she speaks, so talking never reads as idle.+ return replace(p, dy=-1)+ if state == "success":+ dy = (0, -1, -2, -2, -2, -2, -1, 0)[i]+ return Pose(state, i, dy=dy, eyes="happy", mouth="smile")+ if state == "error":+ return Pose(state, i, dx=(-1, 1, -1, 1, 0, 0)[i], eyes="worry", mouth="frown")+ if state == "sleeping":+ return replace(p, dy=0, mouth="rest")+ if state == "working":+ return replace(p, dy=0, mouth="rest")+ if state == "searching":+ x = bp.SEARCH_SWEEP[i]+ return replace(p, eyes="look_left" if x < -2 else "look_right" if x > 2 else "down")+ if state == "held":+ return replace(p, mouth="o", stretch=0)+ return p++ def draw(f, pose):+ with f.offset(pose.dx, pose.dy):+ draw_figure(f, pose, look_for(pose))++ def post(img, pose):+ look = look_for(pose)+ if look.squash < 1.0:+ return bp.squash_x(img, look.squash)+ return img++ # -- Effects ---------------------------------------------------------------++ sparkles = ((7, 14, 1), (41, 10, 2), (5, 34, 3), (42, 33, 4), (12, 3, 5))+ heart_rows = ("#.#", "#o#", ".#.")+ heart_palette = {"#": heart, "o": heart_light}++ def fx_thinking(f, pose):+ bp.fx_dots(f, pose.i, 31, 3)+ if pose.i in (2, 3, 4):+ bp.fx_sparkles(f, pose.i - 2, ((38, 13, 0),))++ def fx_success(f, pose):+ bp.fx_sparkles(f, pose.i, sparkles)++ def fx_working(f, pose):+ if pose.i % 4 == 2:+ bp.fx_sparkles(f, 0, ((38, 24, 0),))++ def fx_searching(f, pose):+ """The magnifier sweeps across the skirt, a gloved hand on its handle."""+ i = pose.i+ gx = cx + bp.SEARCH_SWEEP[i]+ bp.fx_magnifier(f, i, cx, 30)+ hand = rect(gx + 5, 35, gx + 7, 37)+ f.part(hand, {p: ivory_shade if p[1] == 37 else ivory for p in hand})++ def fx_held(f, pose):+ moving = bp.HELD_SWAY[pose.i] - bp.HELD_SWAY[pose.i - 1]+ if moving:+ x, step = (5, -2) if moving > 0 else (42, 2)+ f.paint(line(x, 30, x, 36) | line(x + step, 31, x + step, 35), bp.ARC_FADE)++ def fx_tiara(f, pose):+ if pose.i in (3, 4, 5):+ bp.fx_sparkles(f, pose.i - 3, ((35, 4, 0),))++ def fx_twirl(f, pose):+ bp.fx_sparkles(f, pose.i, ((6, 30, 2), (41, 28, 3), (9, 12, 4)))++ def fx_curtsy(f, pose):+ if pose.i in (3, 4, 5):+ for x, y in ((6, 20), (39, 20)):+ f.glyph(x, y - (pose.i - 3), heart_rows, heart_palette)++ def fx_wave(f, pose):+ if 1 <= pose.i <= 6:+ hx = 40 if pose.i % 2 else 42+ f.paint(line(hx, 16, hx + 2, 14) | line(hx + 1, 19, hx + 3, 19), bp.ARC)++ def act_pose(name):+ eyes, mouth = act_eyes[name], act_mouth[name]++ def make(i):+ dip = curtsy_dip[i] if name == "curtsy" else 0+ return Pose("idle", i, eyes=eyes[i], mouth=mouth, stretch=-1 if dip >= 2 else 0)++ return make++ acts = (+ bp.IdleAct("curtsy", 8, 6, act_pose("curtsy"), fx_curtsy),+ bp.IdleAct("twirl", 8, 9, act_pose("twirl"), fx_twirl),+ bp.IdleAct("hair_flip", 8, 8, act_pose("hair_flip")),+ bp.IdleAct("fan_flutter", 8, 8, act_pose("fan_flutter")),+ bp.IdleAct("tiara_shine", 8, 7, act_pose("tiara_shine"), fx_tiara),+ bp.IdleAct("wave", 8, 7, act_pose("wave"), fx_wave),+ )++ anchors = bp.Anchors(+ arcs_left=(8, 17),+ arcs_right=(39, 17),+ dots=(31, 3),+ zzz=(36, 15),+ bang=(40, 3),+ sweat=(34, 9),+ sparkles=sparkles,+ magnifier=(cx, 30),+ )++ return bp.PetDesign(+ id="aurelia",+ name="Aurelia",+ description=(+ "A gentle princess in an emerald gown who tilts her head to listen, writes letters"+ " with a quill and twirls when something goes right."+ ),+ draw=draw,+ pose=pose_for,+ anchors=anchors,+ waist=33,+ post=post,+ fx={+ "thinking": fx_thinking,+ "success": fx_success,+ "working": fx_working,+ "searching": fx_searching,+ "held": fx_held,+ },+ acts=acts,+ )diff --git a/scripts/pets/designs/cassie.py b/scripts/pets/designs/cassie.pynew file mode 100644index 0000000000..a90b2d546f--- /dev/null+++ b/scripts/pets/designs/cassie.py@@ -0,0 +1,529 @@+"""Cassie: a living 1980s audio cassette.++A chunky coral cassette standing on two little feet. A cream label with neon+stripes carries the face: the two reels in the dark tape window are Cassie's+eyes (their hubs are the pupils and turn while the tape plays), the mouth sits+on the label below them and the trapezoid tape guide with its screw closes the+shell at the bottom. Cassie's moods live in the reels: they turn slowly to+listen, whirr while thinking and working, run backwards to rewind, stop dead+asleep, and when something goes wrong the tape spills out as brown spaghetti.+"""++from __future__ import annotations++import math+++def design(bp): # noqa: C901, PLR0915 - one closure per pet keeps the module self-contained+ hexc = bp.hexc+ rect = bp.rect+ line = bp.line+ ellipse = bp.ellipse+ polygon = bp.polygon+ polyline = bp.polyline+ rounded_rect = bp.rounded_rect+ shade = bp.shade+ Pose = bp.Pose++ # -- Palette ---------------------------------------------------------------++ shell_day = (hexc("#f07a6a"), hexc("#ffb09f"), hexc("#c4524f"))+ #: Asleep the shell cools down: the same cassette, dimmed.+ shell_night = (hexc("#c4645e"), hexc("#dc8a80"), hexc("#984646"))+ guide_color = hexc("#d9665c")+ label_color = hexc("#fbefd2")+ label_dark = hexc("#e6d2a6")+ neon_pink = hexc("#ff3fa4")+ neon_cyan = hexc("#22d8d0")+ neon_yellow = hexc("#ffd23f")+ window_color = hexc("#2b2236")+ window_shine = hexc("#4a3d5c")+ reel_color = hexc("#f6f3ec")+ reel_shadow = hexc("#c9c2b6")+ tape = hexc("#7a4a2e")+ tape_light = hexc("#b0703f")+ metal = hexc("#d7dbe4")+ metal_dark = hexc("#8d95a6")+ foot_color = hexc("#463a52")+ blush_color = hexc("#ff8fa3")+ pencil = hexc("#ffc93c")+ pencil_dark = hexc("#d79a1c")+ pencil_wood = hexc("#f2d2a0")+ eraser = hexc("#ff8fb0")+ ink = bp.OUTLINE+ flat_eyes = bp.EyeStyle(w=6, h=5, iris=reel_color, glint=None, lid=reel_color)++ note_palettes = (+ {"#": neon_pink},+ {"#": neon_cyan},+ {"#": neon_yellow},+ )+ note_single = ("..##", "..#.", "..#.", "###.", "##..")+ note_double = (".####", ".#..#", ".#..#", "##.##", "##.##")++ # -- Geometry (cell coordinates, before the pose offset) ------------------++ #: The reel centres: the eyes.+ reel_left = (17.0, 21.0)+ reel_right = (31.0, 21.0)+ shell = rounded_rect(5, 9, 42, 40, 3)+ label = rounded_rect(8, 12, 39, 33, 2)+ window = rounded_rect(11, 16, 36, 25, 2)+ guide = polygon([(12.5, 41.0), (16.5, 35.0), (31.5, 35.0), (35.5, 41.0)]) & rect(0, 0, 47, 40)+ screws = ((7, 11), (40, 11), (7, 38), (40, 38))++ # -- Poses -----------------------------------------------------------------++ #: Degrees the reels turn per frame in each state (0: they stand still).+ spin_step = {+ "idle": 17,+ "listening": 20,+ "thinking": 50,+ "talking": 30,+ "success": 30,+ "working": 40,+ "held": 0,+ }+ #: Searching: rewinding left, fast-forwarding right (sign of the sweep).+ sweep_dir = tuple((x > 2) - (x < -2) for x in bp.SEARCH_SWEEP)+ #: Error: the reels jam and twitch.+ jam = (0, 25, -10, 30, 0, 15)++ def pose(state: str, i: int) -> Pose:+ if state == "working":+ return Pose(state, i, dy=bp.WORK_BOB[i], eyes="down", mouth="closed")+ if state == "held":+ mouth = ("half", "open")[i % 2]+ return Pose(state, i, dx=bp.HELD_SWAY[i], dy=-2, eyes="wide", mouth=mouth, stretch=1)+ return bp.base_pose(state, i)++ def reel_angle(p: Pose) -> float:+ if p.act:+ return act_spin[p.act][p.i]+ if p.state == "error":+ return jam[p.i]+ if p.state == "searching":+ total = 0+ for k in range(p.i + 1):+ total += sweep_dir[k] * 35+ return total+ return spin_step.get(p.state, 0) * p.i++ # -- Drawing ---------------------------------------------------------------++ def hub_offset(style: str) -> tuple[int, int]:+ return {+ "look_left": (-1, 0),+ "look_right": (1, 0),+ "up_left": (-1, -1),+ "up_right": (1, -1),+ "down": (0, 1),+ }.get(style, (0, 0))++ def draw_reel(f, cx: float, cy: float, style: str, angle: float) -> None:+ """One reel eye: a white hub disc whose dark centre and three teeth+ turn by ``angle`` and shift towards where Cassie looks."""+ radius = 4.1 if style == "wide" else 3.6+ disc = ellipse(cx, cy, radius, radius)+ f.paint_map(shade(disc, reel_color, dark=reel_shadow, dark_depth=1))+ ox, oy = hub_offset(style)+ hx, hy = cx + ox, cy + oy+ f.paint(rect(int(hx) - 1, int(hy) - 1, int(hx), int(hy)), ink)+ for k in range(3):+ a = math.radians(angle + 120 * k - 90)+ tx = math.floor(round(hx + 2.3 * math.cos(a), 6))+ ty = math.floor(round(hy + 2.3 * math.sin(a), 6))+ f.paint({(tx, ty)}, ink)+ if style == "down":+ # A lowered lid: eyes on the keys.+ f.paint({p for p in disc if p[1] <= int(cy - radius) + 1}, window_color)++ def draw_eyes(f, p: Pose) -> None:+ style = p.eyes+ if style in ("open", "wide", "look_left", "look_right", "up_left", "up_right", "down"):+ angle = reel_angle(p)+ draw_reel(f, *reel_left, style, angle)+ draw_reel(f, *reel_right, style, angle)+ return+ for (cx, cy), right in ((reel_left, False), (reel_right, True)):+ bp.draw_eye(f, style, int(cx) - 3, int(cy) - 2, flat_eyes, right=right)++ def draw_feet(f, p: Pose) -> None:+ foot = rounded_rect(11, 40, 16, 43, 1)+ if p.state == "held":+ kick = 2 if p.i % 2 == 0 else 0+ feet = bp.shifted(foot, -1, kick) | bp.shifted(bp.mirrored(foot), 1, 2 - kick)+ elif p.act == "dance_bop":+ lift = act_feet[p.i]+ feet = bp.shifted(foot, 0, -max(0, -lift)) | bp.shifted(+ bp.mirrored(foot), 0, -max(0, lift)+ )+ else:+ feet = foot | bp.mirrored(foot)+ f.part(feet, {q: foot_color for q in feet})++ def side_b(p: Pose) -> bool:+ return p.act == "flip_side" and 3 <= p.i <= 4++ def draw(f, p: Pose) -> None:+ base, light, dark = shell_night if p.state == "sleeping" else shell_day+ with f.offset(p.dx, p.dy):+ draw_feet(f, p)+ f.part(shell, shade(shell, base, light=light, dark=dark, dark_depth=2))+ # The tape guide: a raised trapezoid with two capstan holes and the+ # tape running along its bottom edge.+ f.part(guide, shade(guide, guide_color, light=light, dark=dark, dark_depth=1))+ f.paint(rect(17, 37, 18, 38) | rect(29, 37, 30, 38), window_color)+ f.paint(rect(15, 40, 32, 40), tape)+ f.paint({(23, 37), (24, 37)}, metal_dark)+ f.paint({(23, 36), (24, 36)}, metal)+ for x, y in screws:+ f.paint({(x, y)}, metal)+ f.paint({(x + 1, y)}, metal_dark)+ # The label with its neon stripes (swapped on side B).+ top, bottom = (neon_cyan, neon_pink) if side_b(p) else (neon_pink, neon_cyan)+ f.part(label, shade(label, label_color, dark=label_dark, dark_depth=1))+ f.paint(rect(9, 13, 38, 13), top)+ f.paint(rect(9, 14, 38, 14), neon_yellow)+ f.paint(rect(10, 31, 37, 31), bottom)+ # The tape window: tape packed between the reels, a shine top left.+ f.part(window, window_color)+ f.paint({(12, 17), (13, 17), (12, 18)}, window_shine)+ draw_eyes(f, p)+ blush = rect(9, 27, 10, 27) | rect(37, 27, 38, 27)+ if p.state in ("success", "held") or p.i in act_blush.get(p.act, ()):+ blush |= {(11, 27), (36, 27)}+ if p.state != "sleeping":+ f.paint(blush, blush_color)+ if p.mouth == "o":+ f.paint({(23, 28), (24, 28)}, bp.MOUTH_DARK)+ f.paint({(23, 27), (24, 27), (22, 28), (25, 28), (23, 29), (24, 29)}, ink)+ else:+ bp.draw_mouth(f, p.mouth, 22, 27, 4)++ # -- Shared effect bits ------------------------------------------------------++ def note(f, x: int, y: int, k: int) -> None:+ glyph = note_double if k % 2 else note_single+ f.glyph(x, y, glyph, note_palettes[k % len(note_palettes)])++ def spaghetti(f, length: int, sway: int = 0) -> None:+ """Brown tape spilling out of the capstan holes in loose loops;+ ``length`` 0-6 is how much has come out."""+ loops = (+ [(18, 38), (16, 42), (19, 45), (22, 43)],+ [(29, 38), (31, 42), (28, 45), (25, 43)],+ [(22, 43), (21, 40), (24, 39), (25, 43)],+ [(19, 45), (13, 44), (9, 41), (5, 43), (7, 46)],+ [(28, 45), (34, 44), (38, 41), (42, 43), (40, 46)],+ [(5, 43), (2, 38), (4, 34)],+ )+ pts: set = set()+ for k in range(min(length, len(loops))):+ bent = [(x + sway * (y - 38) // 6, y) for x, y in loops[k]]+ pts |= polyline(bent)+ pts = {q for q in pts if 1 <= q[0] < 47 and 1 <= q[1] < 47}+ # A thin 1 px ribbon: tan with darker twists, readable on dark and light.+ f.paint(pts, tape_light)+ f.paint({q for q in pts if (q[0] + 2 * q[1]) % 5 == 0}, tape)++ def arrows(f, x: int, y: int, direction: int, color) -> None:+ """Two little triangles: fast-forward (1) or rewind (-1)."""+ tri = ("#..", "##.", "###", "##.", "#..")+ if direction < 0:+ tri = tuple(row[::-1] for row in tri)+ f.glyph(x, y, tri, {"#": color})+ f.glyph(x + 3, y, tri, {"#": color})++ z_small = ("####", "..#.", ".#..", "####")+ z_big = ("#####", "...#.", "..#..", ".#...", "#####")+ #: (x, y, glyph) of each z, lowest first; which show per sleeping frame.+ z_marks = ((36, 4, z_small), (41, 1, z_big), (30, 1, z_small))+ z_phases = ((0,), (0,), (0, 1), (0, 1), (1, 2), (2,))+ z_drift = (1, 0, 1, 0, 0, 0)++ # -- State effects -------------------------------------------------------------++ def fx_listening(f, p: Pose) -> None:+ with f.offset(p.dx, p.dy):+ bp.fx_arcs(f, p.i, anchors.arcs_left, anchors.arcs_right)+ if p.i >= 1:+ age = p.i - 1+ note(f, 37 + (0, 1, 0, -1, 0)[age], 6 - age, 0)++ def fx_thinking(f, p: Pose) -> None:+ with f.offset(p.dx, p.dy):+ bp.fx_dots(f, p.i, 19, 4)+ # Whirr ticks beside the window while the reels race.+ if p.i % 2 == 0:+ f.paint({(9, 19), (9, 22), (38, 19), (38, 22)}, bp.ARC)++ def fx_talking(f, p: Pose) -> None:+ with f.offset(p.dx, p.dy):+ if p.i >= 2:+ note(f, 39, 3, 0)+ if p.i == 3:+ note(f, 3, 2, 1)++ def fx_success(f, p: Pose) -> None:+ bp.fx_sparkles(f, p.i, anchors.sparkles)+ for start, x, k in ((1, 2, 0), (3, 39, 1), (5, 8, 2)):+ age = p.i - start+ if 0 <= age <= 3:+ note(f, x + (0, 1, 0, -1)[age], 8 - 2 * age, k)++ def fx_error(f, p: Pose) -> None:+ bp.fx_bang(f, *anchors.bang)+ with f.offset(p.dx, p.dy):+ spaghetti(f, 1 + p.i, sway=(p.i % 2) * 2 - 1)++ def fx_sleeping(f, p: Pose) -> None:+ """Z marks drifting up off the right corner, in the strip above the shell."""+ for k in z_phases[p.i]:+ x, y, rows = z_marks[k]+ f.glyph(x, y + (z_drift[p.i] if k == 0 else 0), rows, {"#": bp.Z_BLUE})++ def fx_working(f, p: Pose) -> None:+ bp.fx_laptop(f, p.i, *anchors.laptop)+ with f.offset(p.dx, p.dy):+ if p.i % 2 == 0:+ f.paint({(9, 19), (9, 22), (38, 19), (38, 22)}, bp.ARC)++ def fx_searching(f, p: Pose) -> None:+ bp.fx_magnifier(f, p.i, *anchors.magnifier)+ direction = sweep_dir[p.i]+ if direction:+ arrows(f, 21, 2, direction, bp.ARC if p.i % 2 else neon_cyan)++ def fx_held(f, p: Pose) -> None:+ with f.offset(p.dx, p.dy):+ bp.fx_held(f, p.i, (3, 16), (44, 16))+ sway = (-1, 0, 1, 1, 0, -1)[p.i]+ loop = polyline([(22, 41), (21 + sway, 44), (23 + 2 * sway, 46), (26 + sway, 44)])+ f.part(loop, {q: tape_light if q[1] == 44 else tape for q in loop})++ # -- Idle acts ---------------------------------------------------------------------++ #: Reel angle per act frame.+ act_spin = {+ "rewind": (0, -30, -70, -120, -170, -220, -250, -260),+ "pencil_wind": (0, -40, -80, -120, -160, -200, -240, -240),+ "music_notes": (0, 20, 40, 60, 80, 100, 120, 120),+ "tape_tangle": (0, 0, 0, 0, -50, -100, -150, -150),+ "flip_side": (0,) * 8,+ "dance_bop": (0, 30, 60, 90, 120, 150, 180, 180),+ }+ act_blush = {"music_notes": (3, 4, 5), "tape_tangle": (7,), "dance_bop": (2, 3, 4, 5, 6)}+ #: Dance: which foot lifts per frame (-1 left, 1 right).+ act_feet = (0, -2, 0, 2, 0, -2, 0, 2)+ flip_widths = (1.0, 0.6, 0.2, 0.6, 1.0, 0.6, 0.2, 1.0)++ def act_pose(name: str, **columns):+ def make(i: int) -> Pose:+ return Pose("idle", i, act=name, **{k: v[i] for k, v in columns.items()})++ return make++ def fx_rewind(f, p: Pose) -> None:+ i = p.i+ if 1 <= i <= 6:+ arrows(f, 19, 2, -1, neon_cyan if i % 2 else bp.ARC)+ # Speed lines streaming off to the right.+ for y, length in ((15, 3), (21, 4), (27, 3)):+ x = 43 - (i % 2)+ f.paint(line(x, y, min(47, x + length - 1), y), bp.ARC_FADE)+ if i == 7:+ bp.fx_sparkles(f, 0, ((40, 8, 0),))++ #: Pencil wind: the pencil's angle per frame (degrees, 0 = east; None = gone).+ pencil_angles = (None, -95, -125, -155, -125, -95, -125, None)++ def fx_pencil(f, p: Pose) -> None:+ """A pencil pokes into the left reel and cranks the tape back by hand."""+ a = pencil_angles[p.i]+ if a is None:+ return+ cx, cy = reel_left+ ca, sa = math.cos(math.radians(a)), math.sin(math.radians(a))++ def at(r: float) -> tuple[int, int]:+ return round(cx - 0.5 + r * ca), round(cy - 0.5 + r * sa)++ stroke = polyline([at(4), at(16)])+ shade_side = bp.shifted(stroke, 1, 0) - stroke+ body = stroke | shade_side+ colors = {q: pencil for q in stroke} | {q: pencil_dark for q in shade_side}+ f.part(body, colors)+ f.paint(polyline([at(1), at(3)]), pencil_wood)+ f.paint({at(1)}, ink)+ end = at(17)+ cap = rect(end[0], end[1], end[0] + 1, end[1] + 1)+ f.part(cap, eraser)+ if p.i in (3, 5):+ f.paint(line(4, 12, 2, 10) | line(4, 30, 2, 32), bp.ARC)++ def fx_music(f, p: Pose) -> None:+ for start, x, k in ((1, 8, 0), (2, 33, 1), (4, 19, 2), (5, 37, 0)):+ age = p.i - start+ if 0 <= age <= 3:+ note(f, x + (0, 1, 0, -1)[age], 7 - 2 * age, k)++ def fx_tangle(f, p: Pose) -> None:+ length = (0, 1, 2, 4, 3, 2, 1, 0)[p.i]+ with f.offset(p.dx, p.dy):+ spaghetti(f, length, sway=(0, 0, 1, -1, 1, 0, 0, 0)[p.i])+ if p.i == 3:+ bp.fx_bang(f, 41, 1)+ if p.i == 7:+ bp.fx_sparkles(f, 0, ((41, 8, 0),))++ def fx_flip(f, p: Pose) -> None:+ if p.i in (2, 6):+ f.paint(line(4, 22, 1, 22) | line(43, 22, 46, 22), bp.ARC_FADE)+ if p.i == 4:+ bp.fx_sparkles(f, 0, ((42, 8, 0),))++ def fx_dance(f, p: Pose) -> None:+ for start, x, k in ((1, 3, 1), (3, 38, 0), (5, 6, 2)):+ age = p.i - start+ if 0 <= age <= 2:+ note(f, x, 6 - 2 * age, k)+ if p.i in (2, 6):+ f.paint(line(2, 30, 2, 33) | line(45, 30, 45, 33), bp.ARC_FADE)++ acts = (+ bp.IdleAct(+ "rewind",+ 8,+ 10,+ act_pose(+ "rewind",+ dx=(0, -1, -1, -1, -1, -1, -1, 0),+ eyes=(+ "open",+ "look_left",+ "look_left",+ "look_left",+ "look_left",+ "look_left",+ "look_left",+ "happy",+ ),+ mouth=("rest", "o", "o", "o", "o", "o", "closed", "smile"),+ ),+ fx_rewind,+ ),+ bp.IdleAct(+ "pencil_wind",+ 8,+ 8,+ act_pose(+ "pencil_wind",+ eyes=(+ "open",+ "look_left",+ "look_left",+ "look_left",+ "look_left",+ "look_left",+ "happy",+ "open",+ ),+ mouth=("rest", "half", "closed", "closed", "closed", "closed", "smile", "rest"),+ ),+ fx_pencil,+ ),+ bp.IdleAct(+ "music_notes",+ 8,+ 8,+ act_pose(+ "music_notes",+ dx=(0, -1, 0, 1, 0, -1, 0, 0),+ stretch=(0, 1, 0, 1, 0, 1, 0, 0),+ eyes=("open", "happy", "happy", "happy", "happy", "happy", "happy", "open"),+ mouth=("rest", "o", "smile", "o", "smile", "o", "smile", "rest"),+ ),+ fx_music,+ ),+ bp.IdleAct(+ "tape_tangle",+ 8,+ 8,+ act_pose(+ "tape_tangle",+ dy=(0, 0, 0, -2, 0, 0, 0, 0),+ eyes=("open", "down", "down", "wide", "look_left", "look_right", "down", "happy"),+ mouth=("rest", "closed", "half", "wide", "closed", "closed", "closed", "smile"),+ ),+ fx_tangle,+ ),+ bp.IdleAct(+ "flip_side",+ 8,+ 10,+ act_pose(+ "flip_side",+ dy=(0, -2, -4, -5, -4, -2, 0, 0),+ eyes=("open", "wide", "closed", "happy", "happy", "closed", "wide", "open"),+ mouth=("rest", "half", "closed", "smile", "smile", "closed", "half", "rest"),+ ),+ fx_flip,+ ),+ bp.IdleAct(+ "dance_bop",+ 8,+ 8,+ act_pose(+ "dance_bop",+ dx=(0, -2, 0, 2, 0, -2, 0, 0),+ dy=(0, -1, 0, -1, 0, -1, 0, 0),+ stretch=(0, 0, -1, 0, -1, 0, -1, 0),+ eyes=("open", "happy", "happy", "happy", "happy", "happy", "happy", "open"),+ mouth=("rest", "smile", "open", "smile", "open", "smile", "smile", "rest"),+ ),+ fx_dance,+ ),+ )++ def post(img, p: Pose):+ if p.act == "flip_side":+ return bp.squash_x(img, flip_widths[p.i])+ return img++ anchors = bp.Anchors(+ arcs_left=(9, 5),+ arcs_right=(38, 5),+ dots=(19, 4),+ zzz=(38, 6),+ bang=(43, 1),+ sweat=(36, 6),+ sparkles=((4, 8, 1), (43, 14, 2), (3, 34, 3), (44, 32, 3)),+ laptop=(17, 35),+ magnifier=(bp.CX, 21),+ )++ return bp.PetDesign(+ id="cassie",+ name="Cassie",+ description=(+ "A groovy 80s cassette whose reels spin while it thinks and hum along to music."+ ),+ draw=draw,+ pose=pose,+ anchors=anchors,+ fx={+ "listening": fx_listening,+ "thinking": fx_thinking,+ "talking": fx_talking,+ "success": fx_success,+ "error": fx_error,+ "sleeping": fx_sleeping,+ "working": fx_working,+ "searching": fx_searching,+ "held": fx_held,+ },+ acts=acts,+ post=post,+ waist=39,+ )diff --git a/scripts/pets/designs/flick.py b/scripts/pets/designs/flick.pynew file mode 100644index 0000000000..c1a54f2a19--- /dev/null+++ b/scripts/pets/designs/flick.py@@ -0,0 +1,953 @@+"""Flick: a little house elf helper.++A slim caramel-skinned elf in a cream shirt and a patched dark-teal work+apron, a wrench poking out of its pocket and a knitted mustard cap tipped+over one ear. Its huge pointed ears carry the mood: they shoot up and fan out+to listen, twitch while it thinks, flap on a happy jump, droop flat when+something goes wrong and fold down over it like a blanket while it sleeps.+Eager and tidy, it scrubs the floor while it works and dusts, polishes and+bows while nothing happens.++Built by ``scripts/pets/build_pets.py``: ``design(bp)`` gets that module as+``bp`` and uses its shapes, shading, poses and shared effects.+"""++import math+from dataclasses import dataclass, replace++#: The builder module, set by ``design``.+bp = None+++def _hex(value: str) -> tuple[int, int, int, int]:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++SKIN = _hex("#d99a5b")+SKIN_LIGHT = _hex("#f2c48c")+SKIN_DARK = _hex("#a96a37")+SKIN_DEEP = _hex("#7c4824")+EAR_INNER = _hex("#e98d72")+EAR_INNER_DARK = _hex("#c06650")+NOSE = _hex("#b8723e")+NOSE_SHINE = _hex("#f7d2a2")+BLUSH = _hex("#f08a7e")+IRIS = _hex("#f0a531")+IRIS_LIGHT = _hex("#ffd877")+IRIS_DARK = _hex("#b8691c")+PUPIL = _hex("#2b1608")+SHIRT = _hex("#ecdcb8")+SHIRT_LIGHT = _hex("#f8eed6")+SHIRT_DARK = _hex("#c4ab80")+APRON = _hex("#21716b")+APRON_LIGHT = _hex("#36978e")+APRON_DARK = _hex("#14504c")+PATCH = _hex("#c8653a")+PATCH_LIGHT = _hex("#ea9363")+STITCH = _hex("#f4e8cc")+CAP = _hex("#e6b42e")+CAP_LIGHT = _hex("#f8d873")+CAP_DARK = _hex("#b07f14")+POMPOM = _hex("#fbf1d6")+BOOT = _hex("#6a4330")+BOOT_LIGHT = _hex("#93644a")+BOOT_DARK = _hex("#45291c")+STEEL = _hex("#a9b1c2")+STEEL_LIGHT = _hex("#e1e6ef")+WOOD = _hex("#9a6238")+WOOD_LIGHT = _hex("#c88b55")+FEATHER = _hex("#d0738f")+FEATHER_LIGHT = _hex("#f4a9c0")+FEATHER_DARK = _hex("#9c4766")+BRISTLE = _hex("#f1d58c")+SUDS = _hex("#e9f4ff")+SUDS_SHADE = _hex("#a9cdee")+WATER = _hex("#7ec4f0")+BUCKET = _hex("#7f8aa0")+BUCKET_LIGHT = _hex("#b7c0d2")+BUCKET_DARK = _hex("#545d72")+GOLD = _hex("#ffcf45")+GOLD_LIGHT = _hex("#fff1b0")+GOLD_DARK = _hex("#c9952a")+DUST = _hex("#c9bfb3")++# ---------------------------------------------------------------------------+# Geometry (cell coordinates of the standing elf, before any offset).+# ---------------------------------------------------------------------------++#: The ear's root on the left side of the head (top and bottom corner); the+#: right ear is the mirror image.+_EAR_ROOT = ((18.0, 13.0), (18.0, 21.5))+#: Left-ear shapes: (tip, bulge). The bulge pushes the upper edge out, so a+#: bigger one is a fanned-out ear.+_EARS: dict[str, tuple[tuple[float, float], float]] = {+ "rest": ((2.5, 10.5), 2.0),+ "lift": ((3.0, 6.0), 2.2),+ "twitch": ((2.0, 8.0), 2.0),+ "perk": ((6.5, 1.5), 3.6),+ "fan": ((5.0, 2.5), 4.2),+ "low": ((3.0, 16.5), 1.8),+ "droop": ((4.0, 27.0), 1.6),+ "flap_up": ((3.5, 5.5), 2.4),+ "flap_down": ((3.5, 21.0), 2.0),+ "forward": ((10.0, 24.0), 1.6),+}+_EYE_AT = ((17, 16), (26, 16))+#: Where the 3 x 4 pupil sits in the 5 x 6 eye box per look.+_PUPIL = {+ "open": (1, 1),+ "wide": (1, 2),+ "look_left": (0, 1),+ "look_right": (2, 1),+ "up_left": (0, 0),+ "up_right": (2, 0),+ "down": (1, 2),+}+#: Closed-eye shapes on the 5 x 6 box (the right eye is mirrored).+_LIDS = {+ "closed": (".....", ".....", ".....", "#...#", ".###.", "....."),+ "sleep": (".....", ".....", ".....", "#...#", ".###.", "....."),+ "happy": (".....", ".###.", "#...#", ".....", ".....", "....."),+}+_SHOULDERS = ((18.5, 29.0), (29.5, 29.0))+_REST_HANDS = ((15.5, 37.0), (32.5, 37.0))+_GROUND = 45+++@dataclass(frozen=True)+class _Look:+ """How the elf holds itself beyond the pose's eyes and mouth."""++ ears: tuple[str, str] = ("rest", "rest")+ #: The head's offset from the body.+ head: tuple[int, int] = (0, 0)+ #: Each hand's centre and an optional elbow; None hangs it at the side.+ hands: tuple[tuple | None, tuple | None] = (None, None)+ elbows: tuple[tuple | None, tuple | None] = (None, None)+ #: The cap's offset from the head and its extra tilt (radians).+ cap: tuple[int, int] = (0, 0)+ cap_tilt: float = 0.0+ brows: str = "rest"+ blush: bool = True+ #: The wrench stays in the apron pocket.+ wrench: bool = True+ #: How far the boots hang below the ground (held) and spread apart.+ feet_drop: int = 0+ feet_spread: int = 0+ #: Torso and head pushed down (a crouch).+ crouch: int = 0+ #: Which hands are pressed over the eyes as open palms: (left, right).+ cover: tuple[bool, bool] = (False, False)+ #: A prop in the right hand: "", "duster", "rag", "cup".+ prop: str = ""+ prop_at: tuple[float, float] = (0.0, 0.0)+ prop_angle: float = 0.0+++# ---------------------------------------------------------------------------+# Parts.+# ---------------------------------------------------------------------------+++def _fill(f, mask, base, light, dark, depth=1):+ f.part(mask, bp._shade(mask, base, light, dark, depth))+++def _edge_point(x, y, ex, ey, nx, ny, bulge, t):+ """A point ``t`` of the way along an edge, pushed ``bulge`` px along its normal."""+ lift = bulge * math.sin(math.pi * t)+ return (x + ex * t + nx * lift, y + ey * t + ny * lift)+++def _ear_mask(name, *, right=False):+ """A big bat-like ear: a long point with a fanned upper edge and a+ slightly hollow lower edge."""+ (tx, ty), bulge = _EARS[name]+ (r0x, r0y), (r1x, r1y) = _EAR_ROOT+ # Outward normal of the upper edge (away from the lower root corner).+ ex, ey = tx - r0x, ty - r0y+ length = math.hypot(ex, ey) or 1.0+ nx, ny = -ey / length, ex / length+ if nx * (r1x - r0x) + ny * (r1y - r0y) > 0:+ nx, ny = -nx, -ny+ top = [_edge_point(r0x, r0y, ex, ey, nx, ny, bulge, t) for t in (0.0, 0.25, 0.5, 0.75)]+ bx, by = tx - r1x, ty - r1y+ blen = math.hypot(bx, by) or 1.0+ mx, my = -by / blen, bx / blen+ if mx * (r0x - r1x) + my * (r0y - r1y) < 0:+ mx, my = -mx, -my+ bottom = [_edge_point(r1x, r1y, bx, by, mx, my, 0.9, t) for t in (0.7, 0.4, 0.0)]+ mask = bp.polygon([*top, (tx, ty), *bottom])+ return bp.mirrored(mask) if right else mask+++def _draw_ear(f, name, *, right=False):+ ear = _ear_mask(name, right=right)+ _fill(f, ear, SKIN, SKIN_LIGHT, SKIN_DARK, 1)+ inner = bp.eroded(ear, 2)+ # Keep the inner ear away from the root, where the head covers it.+ inner = {(x, y) for x, y in inner if (x >= 30 if right else x <= 17)}+ if inner:+ f.paint_map(bp._shade(inner, EAR_INNER, EAR_INNER, EAR_INNER_DARK, 1))+++def _head_mask():+ return bp.ellipse(24, 18.8, 9.6, 7.8) | bp.ellipse(24, 22.3, 7.4, 5.4)+++def _eyes(f, style):+ for side, (x, y) in enumerate(_EYE_AT):+ if style == "hidden":+ continue+ if style == "wink" and side == 1:+ rows = tuple(r[::-1] for r in _LIDS["happy"])+ f.paint(bp.from_rows(x, y, rows), bp.OUTLINE)+ continue+ if style in _LIDS:+ rows = _LIDS[style]+ if side:+ rows = tuple(r[::-1] for r in rows)+ f.paint(bp.from_rows(x, y, rows), bp.OUTLINE)+ continue+ look = "open" if style == "wink" else style+ top, h = (y - 1, 7) if look == "wide" else (y, 6)+ eye = bp._oval(x, top, 5, h)+ # A lash line over the top lifts the amber off the caramel skin.+ f.paint({p for p in bp.outer_ring(eye) if p[1] < top + 2}, bp.OUTLINE)+ f.paint(eye, IRIS)+ f.paint({p for p in eye if p[1] >= top + h - 2}, IRIS_LIGHT)+ f.paint({p for p in eye if p[1] == top}, IRIS_DARK)+ px, py = _PUPIL[look]+ px, py = x + px, top + py + (1 if look == "wide" else 0)+ f.paint(bp.rect(px, py, px + 2, py + 3), PUPIL)+ f.paint(bp.rect(px + 1, py, px + 2, py + 1), bp.FX_WHITE)+ f.paint({(px, py + 3)}, IRIS_LIGHT)+++#: Left brow pixels per mood (the right one is mirrored).+_BROWS = {+ "up": ((18, 13), (19, 12), (20, 12)),+ "worried": ((19, 14), (20, 13), (21, 13)),+ "focus": ((18, 13), (19, 14), (20, 14)),+}+++def _face(f, eyes, mouth, brows="rest", blush=True):+ if blush:+ f.paint({(15, 23), (16, 23), (31, 23), (32, 23)}, BLUSH)+ _eyes(f, eyes)+ if brows in _BROWS:+ left = set(_BROWS[brows])+ f.paint(left | bp.mirrored(left), SKIN_DEEP)+ # A small round nose with a shine.+ f.paint({(23, 21), (24, 21), (22, 22), (23, 22), (24, 22), (25, 22)}, NOSE)+ f.paint({(23, 21)}, NOSE_SHINE)+ _mouth(f, mouth)+++def _mouth(f, style):+ x, y = 22, 24+ if style == "hidden":+ return+ if style == "rest":+ f.paint({(x + 1, y + 1), (x + 2, y + 1), (x, y), (x + 3, y)}, SKIN_DEEP)+ elif style == "grin":+ f.paint(bp.rect(x - 1, y, x + 4, y + 1) - {(x - 1, y + 1), (x + 4, y + 1)}, bp.MOUTH_DARK)+ f.paint(bp.rect(x, y, x + 3, y), bp.FX_WHITE)+ f.paint({(x + 1, y + 2), (x + 2, y + 2)}, bp.MOUTH_DARK)+ f.paint({(x + 1, y + 1), (x + 2, y + 1)}, bp.TONGUE)+ elif style == "wobble":+ f.paint({(x, y + 1), (x + 1, y), (x + 2, y + 1), (x + 3, y)}, SKIN_DEEP)+ elif style == "tongue":+ f.paint({(x, y), (x + 1, y + 1), (x + 2, y + 1), (x + 3, y)}, SKIN_DEEP)+ f.paint({(x + 3, y + 1), (x + 3, y + 2)}, bp.TONGUE)+ elif style == "o":+ f.paint(bp.rect(x + 1, y, x + 2, y + 1), bp.MOUTH_DARK)+ else:+ bp.draw_mouth(f, style, x, y, 4, SKIN_DEEP)+++def _cap_parts(dx, dy, tilt):+ """The knitted cap tipped over the right ear: (cap, band, pompom, frame).++ Its brim runs down across the crown to the right ear; below the brim it+ hugs the skull, above it a low dome rises. ``frame`` is (cx, cy, d) for+ the ribbing along the brim."""+ theta = 0.36 + tilt+ cx, cy = 27.6 + dx, 12.6 + dy+ d = (math.cos(theta), math.sin(theta))+ n = (math.sin(theta), -math.cos(theta))+ a, b = 7.4, 6.0+ pts = []+ for k in range(13):+ t = math.pi * k / 12+ u, v = a * math.cos(t), b * math.sin(t)+ pts.append((cx + u * d[0] + v * n[0], cy + u * d[1] + v * n[1]))+ dome = bp.polygon(pts)+ band = set()+ for x in range(math.floor(cx - a - 2), math.ceil(cx + a + 2)):+ for y in range(math.floor(cy - a), math.ceil(cy + a)):+ u = (x + 0.5 - cx) * d[0] + (y + 0.5 - cy) * d[1]+ v = (x + 0.5 - cx) * n[0] + (y + 0.5 - cy) * n[1]+ if abs(u) <= a + 0.3 and -1.7 <= v <= 1.0:+ band.add((x, y))+ cap = dome | band+ apex = (cx + (b + 0.6) * n[0] + 1.6 * d[0], cy + (b + 0.6) * n[1] + 1.6 * d[1])+ pompom = bp.ellipse(apex[0], apex[1], 2.1, 2.1)+ return cap, band, pompom, (cx, cy, d)+++def _draw_cap(f, dx, dy, tilt):+ cap, band, pompom, (cx, cy, d) = _cap_parts(dx, dy, tilt)+ colors = bp._shade(cap, CAP, CAP_LIGHT, CAP_DARK, 1)+ for x, y in cap:+ u = (x + 0.5 - cx) * d[0] + (y + 0.5 - cy) * d[1]+ v = (x + 0.5 - cx) * d[1] - (y + 0.5 - cy) * d[0]+ if (x, y) in band:+ colors[(x, y)] = CAP_DARK if math.floor(u) % 2 == 0 else CAP+ elif colors[(x, y)] == CAP and 3.0 <= v < 4.0 and math.floor(u) % 2 == 0:+ colors[(x, y)] = CAP_DARK+ f.part(cap, colors)+ _fill(f, pompom, POMPOM, bp.FX_WHITE, SHIRT_DARK, 1)+++def _draw_head(f, pose, look):+ with f.offset(*look.head):+ left, right = look.ears+ _draw_ear(f, left)+ _draw_ear(f, right, right=True)+ head = _head_mask()+ _fill(f, head, SKIN, SKIN_LIGHT, SKIN_DARK, 2)+ _face(f, pose.eyes, pose.mouth, look.brows, look.blush)+ _draw_cap(f, *look.cap, look.cap_tilt)+++def _limb(a, b):+ ax, ay, bx, by = (math.floor(v) for v in (*a, *b))+ return bp.thick(bp.line(ax, ay, bx, by))+++def _draw_arm(f, side, hand, elbow, cover=False):+ """A thin arm: a cream sleeve to the elbow, then the bare forearm."""+ shoulder = _SHOULDERS[side]+ hand = hand or _REST_HANDS[side]+ if elbow is None:+ elbow = (+ shoulder[0] + (hand[0] - shoulder[0]) * 0.5,+ shoulder[1] + (hand[1] - shoulder[1]) * 0.5,+ )+ forearm = _limb(elbow, hand)+ _fill(f, forearm, SKIN, SKIN_LIGHT, SKIN_DARK, 1)+ sleeve = _limb(shoulder, elbow) | bp.ellipse(shoulder[0], shoulder[1] + 0.5, 1.8, 1.6)+ sleeve |= bp.ellipse(elbow[0] + 0.5, elbow[1] + 0.5, 1.5, 1.5)+ _fill(f, sleeve, SHIRT, SHIRT_LIGHT, SHIRT_DARK, 1)+ if cover:+ # A palm over the eye, fingers up.+ hx, hy = math.floor(hand[0]), math.floor(hand[1])+ palm = bp.rounded_rect(hx - 2, hy - 1, hx + 2, hy + 2, 1)+ palm |= {(hx - 2, hy - 2), (hx, hy - 2), (hx + 2, hy - 2)}+ _fill(f, palm, SKIN_LIGHT, SKIN_LIGHT, SKIN, 1)+ f.paint({(hx - 1, hy - 1), (hx + 1, hy - 1)}, SKIN_DARK)+ else:+ _fill(f, bp.ellipse(hand[0], hand[1], 1.8, 1.7), SKIN, SKIN_LIGHT, SKIN_DARK, 1)+++_WRENCH = ("#.#", "###", ".#.", ".#.")+++def _draw_wrench(f, x, y):+ """A tiny wrench standing in the pocket: an open jaw on a short handle."""+ f.glyph(x, y, _WRENCH, {"#": STEEL_LIGHT})+ f.paint({(x + 1, y + 2), (x + 1, y + 3)}, STEEL)+++def _draw_body(f, look):+ c = look.crouch+ # Legs and boots stay planted unless dangling.+ for side in (0, 1):+ lx = 20 if side == 0 else 27+ spread = -look.feet_spread if side == 0 else look.feet_spread+ foot_y = _GROUND - 1.5 + look.feet_drop+ leg = bp.rect(lx + spread // 2, 38 + c, lx + 1 + spread // 2, math.floor(foot_y))+ _fill(f, leg, SKIN, SKIN_LIGHT, SKIN_DARK, 1)+ bx = lx + 0.5 + spread + (-1.0 if side == 0 else 1.0)+ boot = bp.ellipse(bx, foot_y + 0.3, 3.2, 2.1)+ boot |= bp.rect(lx + spread, math.floor(foot_y) - 2, lx + 1 + spread, math.floor(foot_y))+ _fill(f, boot, BOOT, BOOT_LIGHT, BOOT_DARK, 1)+ with f.offset(0, c):+ torso = {p for p in bp.ellipse(24, 33.6, 7.0, 7.0) if p[1] <= 39}+ _fill(f, torso, SHIRT, SHIRT_LIGHT, SHIRT_DARK, 1)+ skirt = bp.polygon([(19.5, 31), (28.5, 31), (30.6, 41), (17.4, 41)])+ apron = skirt | bp.rect(21, 28, 26, 31)+ colors = bp._shade(apron, APRON, APRON_LIGHT, APRON_DARK, 1)+ f.part(apron, colors)+ # Straps up over the shoulders.+ f.paint({(20, 27), (19, 26), (27, 27), (28, 26)}, APRON_DARK)+ # A patch with cross stitches, low on the left.+ patch = bp.rect(19, 36, 21, 38)+ f.part(patch, PATCH)+ f.paint({(19, 36), (21, 38)}, PATCH_LIGHT)+ f.paint({(20, 37)}, STITCH)+ # The tool pocket on the right with its stitched rim.+ if look.wrench:+ _draw_wrench(f, 24, 30)+ brush = bp.rect(27, 31, 28, 33)+ f.part(brush, {p: (BRISTLE if p[1] == 31 else WOOD) for p in brush})+ pocket = bp.rect(23, 34, 28, 37)+ f.part(pocket, bp._shade(pocket, APRON_LIGHT, APRON_LIGHT, APRON, 1))+ f.paint(bp.rect(23, 34, 28, 34), APRON_DARK)+ f.paint({(24, 36), (26, 36)}, APRON)+++_GOBLET = ("#####", "#o###", ".###.", "..#..", ".###.")+++def _draw_prop(f, look):+ x, y = look.prop_at+ if look.prop == "duster":+ a = look.prop_angle+ tip = (x + 6 * math.cos(a), y + 6 * math.sin(a))+ f.part(_limb((x, y), tip), WOOD)+ cx, cy = tip[0] + 2.5 * math.cos(a), tip[1] + 2.5 * math.sin(a)+ tuft = bp.ellipse(cx, cy, 3.6, 3.6)+ colors = bp._shade(tuft, FEATHER, FEATHER_LIGHT, FEATHER_DARK, 1)+ # Feather quills fanning out from the handle.+ for k in (-1, 0, 1):+ ang = a + 0.6 * k+ for r in (1.0, 2.0, 3.0):+ q = (+ math.floor(tip[0] + (r + 0.5) * math.cos(ang)),+ math.floor(tip[1] + (r + 0.5) * math.sin(ang)),+ )+ if q in colors:+ colors[q] = FEATHER_DARK+ f.part(tuft, colors)+ elif look.prop == "cup":+ f.glyph(math.floor(x) - 2, math.floor(y) - 4, _GOBLET, {"#": GOLD, "o": GOLD_LIGHT})+ f.paint(+ {(math.floor(x) + 2, math.floor(y) - 3), (math.floor(x) + 1, math.floor(y) - 2)},+ GOLD_DARK,+ )+ elif look.prop == "rag":+ rag = bp.polygon([(x - 3, y - 1), (x + 2, y - 2.5), (x + 3, y + 1), (x - 1.5, y + 2)])+ f.part(rag, bp._shade(rag, SHIRT, SHIRT_LIGHT, SHIRT_DARK, 1))+++def _draw_standing(f, pose, look):+ with f.offset(pose.dx, pose.dy):+ _draw_body(f, look)+ c = look.crouch+ head_look = replace(look, head=(look.head[0], look.head[1] + c))+ _draw_head(f, pose, head_look)+ for side in (0, 1):+ hand = look.hands[side]+ elbow = look.elbows[side]+ if hand is None and c:+ hand = (_REST_HANDS[side][0], _REST_HANDS[side][1] + c)+ with f.offset(0, c):+ _draw_arm(+ f,+ side,+ None if hand is None else (hand[0], hand[1] - c),+ None if elbow is None else (elbow[0], elbow[1] - c),+ look.cover[side],+ )+ if look.prop:+ _draw_prop(f, look)+++# ---------------------------------------------------------------------------+# Poses.+# ---------------------------------------------------------------------------++_IDLE_EARS = (+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "twitch"),+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "rest"),+)+_LISTEN_EARS = ("lift", "perk", "fan", "perk", "fan", "perk")+_THINK_EAR = ("rest", "twitch", "rest", "rest", "rest", "twitch", "rest", "twitch")+_TALK_EARS = ("rest", "rest", "lift", "lift")+_HOP = (1, -1, -3, -4, -3, -1, 1, 0)+_HOP_STRETCH = (-1, 1, 1, 0, 0, 1, -1, 0)+_HOP_EARS = ("low", "flap_down", "flap_up", "flap_down", "flap_up", "flap_down", "low", "rest")+_SCRUB = (-4, -2, 1, 4, 4, 2, -1, -4)+#: Searching: the ear on the side Flick peers towards lifts, the other sinks.+_SEARCH_EARS = {+ "look_left": ("lift", "low"),+ "look_right": ("low", "lift"),+ "open": ("rest", "rest"),+}+_HELD_EARS = ("flap_up", "flap_down", "flap_up", "flap_down", "flap_up", "flap_down")+_HELD_SWAY = (-1, 0, 1, 1, 0, -1)+++def flick_pose(state, i):+ pose = bp.base_pose(state, i)+ if state == "idle":+ return replace(pose, mouth="rest")+ if state == "listening":+ return replace(pose, mouth="o" if i in (2, 3) else "closed")+ if state == "thinking":+ return replace(pose, mouth="tongue" if 2 <= i <= 5 else "closed")+ if state == "success":+ return bp.Pose(state, i, dy=_HOP[i], eyes="happy", mouth="grin", stretch=_HOP_STRETCH[i])+ if state == "error":+ return bp.Pose(state, i, dx=(-1, 1, -1, 1, 0, 0)[i], eyes="hidden", mouth="wobble")+ if state == "sleeping":+ return bp.Pose(state, i, eyes="sleep", mouth="o", stretch=bp.SLEEP_BREATH[i])+ if state == "working":+ return bp.Pose(state, i, eyes="down", mouth="tongue")+ if state == "searching":+ return replace(pose, mouth="closed")+ if state == "held":+ return bp.Pose(state, i, dx=_HELD_SWAY[i], dy=-2, eyes="wide", mouth="o")+ return pose+++def _look(pose):+ s, i = pose.state, pose.i+ if s == "idle":+ ears = _IDLE_EARS[i] if i < len(_IDLE_EARS) else ("rest", "rest")+ return _Look(ears=ears)+ if s == "listening":+ e = _LISTEN_EARS[i]+ return _Look(ears=(e, e), brows="up", cap=(0, -1) if e != "lift" else (0, 0))+ if s == "thinking":+ return _Look(+ ears=("rest", _THINK_EAR[i]),+ hands=((30.5, 35.0), (25.5, 27.5)),+ elbows=((20.0, 36.0), (30.5, 34.0)),+ brows="focus",+ )+ if s == "talking":+ e = _TALK_EARS[i]+ return _Look(ears=(e, e))+ if s == "success":+ e = _HOP_EARS[i]+ up = 1 <= i <= 5+ hands = ((12.5, 22.0), (35.5, 22.0)) if up else (None, None)+ return _Look(ears=(e, e), hands=hands)+ if s == "error":+ peek = i in (3, 4)+ left = (19.5, 20.0) if not peek else (17.5, 27.0)+ return _Look(+ ears=("droop", "droop"),+ hands=(left, (28.5, 20.0)),+ elbows=((15.0, 31.0), (33.0, 31.0)),+ cover=(not peek, True),+ blush=False,+ )+ if s == "working":+ x = 24 + _SCRUB[i]+ e = "low" if i % 2 else "rest"+ return _Look(+ ears=(e, e),+ crouch=3,+ hands=((x - 2.5, 41.0), (x + 2.5, 41.0)),+ feet_spread=2,+ brows="focus",+ )+ if s == "searching":+ gx = 24 + bp.SEARCH_SWEEP[i]+ return _Look(+ ears=_SEARCH_EARS[pose.eyes],+ hands=(None, (gx + 7.5, 37.5)),+ elbows=(None, (33.0, 36.0) if gx > 22 else None),+ )+ if s == "held":+ e = _HELD_EARS[i]+ flail = i % 2 == 0+ return _Look(+ ears=(e, e),+ hands=((12.5, 29.0 if flail else 33.0), (35.5, 33.0 if flail else 29.0)),+ feet_drop=3,+ brows="up",+ )+ return _Look()+++def draw_flick(f, pose):+ if pose.act:+ _ACT_DRAW[pose.act](f, pose)+ elif pose.state == "sleeping":+ _draw_sleeping(f, pose)+ else:+ _draw_standing(f, pose, _look(pose))+++# ---------------------------------------------------------------------------+# Sleeping: curled up, the big ears folded down over it like a blanket.+# ---------------------------------------------------------------------------+++def _blanket_ear(right=False):+ pts = [+ (18.0, 23.0),+ (11.0, 26.0),+ (7.5, 32.0),+ (8.5, 38.5),+ (14.0, 43.0),+ (21.0, 45.0),+ (29.5, 45.5),+ (22.0, 42.0),+ (18.5, 37.0),+ (18.0, 31.0),+ ]+ mask = bp.polygon(pts)+ return bp.mirrored(mask) if right else mask+++def _draw_sleeping(f, pose):+ with f.offset(0, pose.dy):+ # Boots poke out at the bottom, toes up.+ for bx in (16.5, 31.5):+ boot = bp.ellipse(bx, 44.0, 3.0, 2.0)+ _fill(f, boot, BOOT, BOOT_LIGHT, BOOT_DARK, 1)+ body = bp.ellipse(24, 39.0, 9.0, 5.5)+ _fill(f, body, APRON, APRON_LIGHT, APRON_DARK, 1)+ for right in (False, True):+ ear = _blanket_ear(right)+ _fill(f, ear, SKIN, SKIN_LIGHT, SKIN_DARK, 1)+ inner = {(x, y) for x, y in bp.eroded(ear, 2) if y < 42}+ inner = bp.eroded(inner, 1) if right else inner+ f.paint_map(bp._shade(inner, EAR_INNER, EAR_INNER, EAR_INNER_DARK, 1))+ with f.offset(0, 9):+ head = _head_mask()+ _fill(f, head, SKIN, SKIN_LIGHT, SKIN_DARK, 2)+ _face(f, "sleep", "o" if pose.i in (2, 3) else "rest")+ _draw_cap(f, 1, 1, 0.25)+++# ---------------------------------------------------------------------------+# Effects.+# ---------------------------------------------------------------------------+++def _suds(f, x, y, r):+ bubble = bp.ellipse(x, y, r, r)+ f.part(bubble, bp._shade(bubble, SUDS, bp.FX_WHITE, SUDS_SHADE, 1))+++_BUBBLES = ((0, -1, 1.2), (3, -3, 1.0), (-3, -2, 0.9))+++def _working_fx(f, pose):+ """A bucket of suds beside the elf and bubbles rising off the brush."""+ i = pose.i+ x = 24 + _SCRUB[i]+ # The scrub brush under both hands.+ brush = bp.rect(x - 4, 42, x + 4, 43)+ f.part(brush, {p: (WOOD_LIGHT if p[1] == 42 else WOOD) for p in brush})+ f.paint(bp.rect(x - 4, 44, x + 4, 44) - {(x - 3 + 2 * k, 44) for k in range(4)}, BRISTLE)+ # Suds on the floor behind the brush.+ trail = -1 if _SCRUB[i] > _SCRUB[i - 1] else 1+ _suds(f, x + 6 * trail, 44.0, 1.6)+ _suds(f, x + 9 * trail, 44.5, 1.0)+ # The bucket on the left.+ bucket = bp.polygon([(2.5, 36), (11.5, 36), (10.5, 45.5), (3.5, 45.5)])+ f.part(bucket, bp._shade(bucket, BUCKET, BUCKET_LIGHT, BUCKET_DARK, 1))+ f.paint(bp.rect(3, 38, 10, 38), BUCKET_DARK)+ f.paint(bp.rect(3, 36, 10, 36), WATER)+ for k, (dx, dy, r) in enumerate(_BUBBLES):+ age = (i + 3 * k) % 8+ if age < 6:+ _suds(f, 7 + dx, 34 + dy - age, r)+++def _search_fx(f, pose):+ i = pose.i+ bp.fx_magnifier(f, i, 24, 30)+++def _success_fx(f, pose):+ bp.fx_sparkles(f, pose.i, FLICK_SPARKLES)+++def _held_fx(f, pose):+ moving = _HELD_SWAY[pose.i] - _HELD_SWAY[pose.i - 1]+ if not moving:+ return+ x, step = (5, -2) if moving > 0 else (42, 2)+ f.paint(bp.line(x, 30, x, 35) | bp.line(x + step, 31, x + step, 34), bp.ARC_FADE)+++FLICK_SPARKLES = ((6, 9, 1), (42, 8, 2), (5, 30, 3), (42, 30, 3))+++# ---------------------------------------------------------------------------+# Idle acts.+# ---------------------------------------------------------------------------++_BOW_HEAD = (0, 1, 3, 5, 5, 4, 1, 0)+_BOW_CROUCH = (0, 0, 1, 2, 2, 1, 0, 0)+_BOW_EYES = ("open", "happy", "closed", "hidden", "hidden", "closed", "happy", "open")+++def _bow_pose(i):+ return bp.Pose("idle", i, eyes=_BOW_EYES[i], mouth="smile" if i in (1, 6) else "rest")+++def _draw_bow(f, pose):+ """A deep, polite bow: a hand on the heart, the other swept out, the+ head dipped so far the cap points at you and the ears fall forward."""+ i = pose.i+ down = _BOW_HEAD[i]+ ears = "forward" if down >= 5 else "low" if down >= 3 else "rest"+ sweep = 1 <= i <= 6+ look = _Look(+ ears=(ears, ears),+ head=(0, down - _BOW_CROUCH[i]),+ crouch=_BOW_CROUCH[i],+ hands=((10.5, 35.0) if sweep else None, (23.5, 31.5) if sweep else None),+ elbows=((14.0, 32.0) if sweep else None, (29.0, 34.0) if sweep else None),+ cap=(0, 1 if down >= 5 else 0),+ cap_tilt=0.1 if down >= 5 else 0.0,+ )+ if down >= 5:+ pose = replace(pose, mouth="hidden")+ _draw_standing(f, pose, look)+++_DUST_ANGLES = (-1.3, -1.3, -1.6, -1.1, -0.8, -1.3, -1.6, -1.4)+_DUST_HAND = ((34, 31), (36, 24), (36, 22), (38, 23), (38, 25), (37, 23), (36, 22), (35, 30))+_DUST_EYES = ("open", "up_right", "up_right", "up_right", "up_right", "up_right", "up_left", "open")+++def _duster_pose(i):+ return bp.Pose("idle", i, eyes=_DUST_EYES[i], mouth="smile" if 2 <= i <= 5 else "rest")+++def _draw_duster(f, pose):+ i = pose.i+ hand = _DUST_HAND[i]+ look = _Look(+ hands=(None, (hand[0] + 0.5, hand[1] + 0.5)),+ elbows=(None, (33.0, 30.0) if hand[1] < 28 else None),+ prop="duster",+ prop_at=(hand[0], hand[1]),+ prop_angle=_DUST_ANGLES[i],+ ears=("rest", "low" if i in (2, 3, 6) else "rest"),+ )+ _draw_standing(f, pose, look)+++#: Dust motes shaken off the duster: (x, y, first frame).+_MOTES = ((40, 9, 2), (44, 13, 3), (37, 5, 3), (42, 6, 5), (45, 10, 6), (39, 3, 6))+++def _duster_fx(f, pose):+ for x, y, start in _MOTES:+ age = pose.i - start+ if 0 <= age <= 1:+ f.part({(x, y + age)}, DUST)+++_WIGGLE = (+ ("lift", "low"),+ ("low", "lift"),+ ("lift", "low"),+ ("low", "lift"),+ ("flap_up", "flap_up"),+ ("fan", "fan"),+ ("lift", "lift"),+ ("rest", "rest"),+)+++def _wiggle_pose(i):+ mouth = "grin" if i in (4, 5) else "smile" if 1 <= i <= 6 else "rest"+ return bp.Pose(+ "idle",+ i,+ eyes="happy" if 1 <= i <= 6 else "open",+ mouth=mouth,+ stretch=1 if i in (4, 5) else 0,+ )+++def _draw_wiggle(f, pose):+ _draw_standing(f, pose, _Look(ears=_WIGGLE[pose.i]))+++#: The rag circles over the goblet, then the goblet goes up to shine.+_POLISH_RAG = ((26, 30), (23, 29), (25, 28), (27, 30), (24, 31), (23, 29))+++def _polish_pose(i):+ eyes = ("open", "down", "down", "down", "down", "down", "happy", "open")[i]+ mouth = "tongue" if 1 <= i <= 5 else "grin" if i == 6 else "smile"+ return bp.Pose("idle", i, eyes=eyes, mouth=mouth, stretch=1 if i == 6 else 0)+++def _draw_polish(f, pose):+ i = pose.i+ if i >= 6:+ hand = (34.5, 22.0) if i == 6 else (33.5, 30.0)+ look = _Look(+ hands=(None, hand),+ elbows=(None, (33.0, 29.0) if i == 6 else None),+ prop="cup",+ prop_at=(hand[0], hand[1] - 1.5),+ ears=("lift", "lift") if i == 6 else ("rest", "rest"),+ )+ _draw_standing(f, pose, look)+ return+ rag = _POLISH_RAG[i]+ look = _Look(+ hands=((20.5, 34.0), (rag[0] + 0.5, rag[1] + 0.5)),+ elbows=(None, (31.0, 34.0)),+ prop="cup",+ prop_at=(22.5, 33.0),+ )+ _draw_standing(f, pose, look)+ with f.offset(pose.dx, pose.dy):+ _draw_prop(f, replace(look, prop="rag", prop_at=(rag[0], rag[1])))+++def _polish_fx(f, pose):+ if pose.i == 6:+ bp.fx_sparkles(f, 1, ((39, 15, 0), (30, 13, 1)))+ elif pose.i == 7:+ bp.fx_sparkles(f, 3, ((38, 24, 0),))+ elif pose.i in (2, 4):+ bp.fx_sparkles(f, 0, ((19, 28, 0),))+++#: The juggled tools loop in front of the apron, hand to hand under the chin.+_JUGGLE_PATH = tuple(+ (24 + 9.5 * math.cos(math.pi * (1 + k / 8)), 35 + 8.5 * math.sin(math.pi * (1 + k / 8)))+ for k in range(9)+)+_JUGGLE_HANDS = (+ ((15.5, 36.0), (32.5, 36.0)),+ ((15.5, 34.0), (32.5, 36.0)),+ ((15.5, 36.0), (32.5, 34.0)),+ ((15.5, 34.0), (32.5, 36.0)),+ ((15.5, 36.0), (32.5, 34.0)),+ ((15.5, 34.0), (32.5, 36.0)),+ ((15.5, 36.0), (32.5, 34.0)),+ (None, None),+)+++def _juggle_pose(i):+ eyes = ("open", "down", "look_left", "down", "look_right", "down", "happy", "open")[i]+ return bp.Pose("idle", i, eyes=eyes, mouth="grin" if i == 6 else "o" if i in (3, 4) else "rest")+++def _draw_juggle(f, pose):+ i = pose.i+ look = _Look(+ hands=_JUGGLE_HANDS[i],+ wrench=i == 7,+ ears=("lift", "lift") if i == 6 else ("rest", "rest"),+ )+ _draw_standing(f, pose, look)+++_TOOLS = (+ (("#.#", "###", ".#.", ".#.", ".#."), {"#": STEEL_LIGHT}),+ (("ooo", "ooo", "###", ".#.", ".#."), {"#": WOOD_LIGHT, "o": BRISTLE}),+ ((".##.", "#oo#", "#oo#", ".##."), {"#": GOLD, "o": GOLD_LIGHT}),+)+++def _juggle_fx(f, pose):+ i = pose.i+ if i == 7:+ return+ for k, (rows, palette) in enumerate(_TOOLS):+ step = (i * 2 + k * 3) % 9+ if i == 6 and step not in (0, 8):+ # Caught: everything lands back in the hands.+ step = 0 if k == 0 else 8+ x, y = _JUGGLE_PATH[step]+ f.glyph(math.floor(x) - 1, math.floor(y) - 3, rows, palette)+++_TIP_HAND = (+ None,+ (32.5, 16.0),+ (32.5, 12.0),+ (32.5, 9.0),+ (32.5, 9.0),+ (32.5, 12.0),+ (33.5, 22.0),+ None,+)+_TIP_CAP = ((0, 0), (0, 0), (0, -2), (0, -4), (0, -4), (0, -1), (0, 0), (0, 0))+_TIP_TILT = (0.0, 0.0, 0.1, 0.25, 0.25, 0.05, 0.0, 0.0)+++def _tip_pose(i):+ eyes = ("open", "open", "happy", "wink", "wink", "happy", "open", "open")[i]+ return bp.Pose("idle", i, eyes=eyes, mouth="smile" if 2 <= i <= 5 else "rest")+++def _draw_tip(f, pose):+ i = pose.i+ hand = _TIP_HAND[i]+ look = _Look(+ hands=(None, hand),+ elbows=(None, (36.0, 24.0) if hand else None),+ cap=_TIP_CAP[i],+ cap_tilt=_TIP_TILT[i],+ ears=("rest", "low" if 1 <= i <= 6 else "rest"),+ )+ _draw_standing(f, pose, look)+++_ACT_DRAW = {+ "bow": _draw_bow,+ "feather_duster": _draw_duster,+ "ear_wiggle": _draw_wiggle,+ "polish_shine": _draw_polish,+ "tool_juggle": _draw_juggle,+ "cap_tip": _draw_tip,+}+++def design(module):+ global bp+ bp = module+ acts = (+ bp.IdleAct("bow", 8, 6, _bow_pose),+ bp.IdleAct("feather_duster", 8, 8, _duster_pose, _duster_fx),+ bp.IdleAct("ear_wiggle", 8, 8, _wiggle_pose),+ bp.IdleAct("polish_shine", 8, 7, _polish_pose, _polish_fx),+ bp.IdleAct("tool_juggle", 8, 8, _juggle_pose, _juggle_fx),+ bp.IdleAct("cap_tip", 8, 6, _tip_pose),+ )+ anchors = bp.Anchors(+ arcs_left=(8, 18),+ arcs_right=(39, 18),+ dots=(8, 3),+ zzz=(37, 16),+ bang=(41, 3),+ sweat=(6, 12),+ sparkles=FLICK_SPARKLES,+ )+ return bp.PetDesign(+ id="flick",+ name="Flick",+ description=(+ "A tidy house elf: its huge ears shoot up to listen and droop when things go wrong."+ ),+ draw=draw_flick,+ anchors=anchors,+ pose=flick_pose,+ fx={+ "success": _success_fx,+ "working": _working_fx,+ "searching": _search_fx,+ "held": _held_fx,+ },+ acts=acts,+ waist=38,+ )diff --git a/scripts/pets/designs/hazel.py b/scripts/pets/designs/hazel.pynew file mode 100644index 0000000000..32f624f904--- /dev/null+++ b/scripts/pets/designs/hazel.py@@ -0,0 +1,960 @@+"""Hazel: the red squirrel.++A russet squirrel that sits upright with a cream belly and muzzle, tufted+pointy ears and a huge bushy tail that rises behind her back and curls out at+the top. She is busy, quick and a hoarder, and her tail does most of the+talking: it sways while she idles, stands straight up while she listens,+flicks while she ponders an acorn held to her chin and bottle-brushes out when+something goes wrong. She types next to her stash of acorns, sniffs along+behind the magnifier, tosses an acorn and catches it on a happy hop, sleeps+curled inside her own tail and dangles with the tail swinging when picked up.++Built by ``scripts/pets/build_pets.py``, which passes itself in as ``bp``.+"""++from __future__ import annotations++import math+from collections.abc import Callable, Sequence+from types import ModuleType+from typing import Any++#: The builder module, set by ``design()`` before anything is drawn.+bp: Any = None++Px = tuple[int, int]+RGBA = tuple[int, int, int, int]+Mask = set[Px]+Frame = Any+Pose = Any+++def _hex(value: str) -> RGBA:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++# -- Palette ------------------------------------------------------------------++FUR = _hex("#c9572a")+FUR_LIGHT = _hex("#ec8a4c")+FUR_DARK = _hex("#93371a")+TAIL = _hex("#d4652f")+TAIL_LIGHT = _hex("#f6a463")+TAIL_DARK = _hex("#9c3d1b")+#: The darker streak down the middle of the tail: it gives the brush volume.+TAIL_SPINE = _hex("#b24a22")+TUFT = _hex("#6e2812")+TUFT_LIGHT = _hex("#a2441e")+CREAM = _hex("#fbe8cb")+CREAM_DARK = _hex("#e9c79c")+EAR_INNER = _hex("#f0a37e")+NOSE = _hex("#4a2420")+BLUSH = _hex("#ef7f73")+IRIS = _hex("#24161a")+#: A second, dimmer glint low in each eye.+IRIS_GLINT = _hex("#6b5a66")+TOOTH = _hex("#ffffff")+ACORN_CAP = _hex("#7f4f2a")+ACORN_CAP_LIGHT = _hex("#b07a46")+ACORN_NUT = _hex("#d7963f")+ACORN_NUT_LIGHT = _hex("#f6c879")+ACORN_STEM = _hex("#4f2c16")+DIRT = _hex("#9a7452")+DIRT_LIGHT = _hex("#c49c74")+DIRT_DARK = _hex("#6a4c34")+CRUMB = _hex("#e3b06a")++#: Hazel sits a little left of the cell's centre to leave room for the tail;+#: everything symmetric mirrors across the line between columns 21 and 22.+AXIS = 22++#: Eye boxes (top-left) of the left and right eye.+EYE_L: Px = (15, 18)+EYE_R: Px = (26, 18)++_ACORN = ("..s..", ".cCc.", "ccccc", ".lnn.", ".lnn.", "..n..")+_ACORN_S = (".s.", "cCc", "lnn", ".n.")+_ACORN_PALETTE = {+ "s": ACORN_STEM,+ "c": ACORN_CAP,+ "C": ACORN_CAP_LIGHT,+ "n": ACORN_NUT,+ "l": ACORN_NUT_LIGHT,+}+_HEART = ("#.#", "###", ".#.")+++# -- Shapes -------------------------------------------------------------------+++def _mirror(mask: Mask) -> Mask:+ return bp.mirrored(mask, AXIS)+++def _fur(f: Frame, mask: Mask, depth: int = 2) -> None:+ f.part(mask, bp.shade(mask, FUR, light=FUR_LIGHT, dark=FUR_DARK, dark_depth=depth))+++def _paw(f: Frame, cx: float, cy: float, rx: float = 1.8, ry: float = 1.6) -> None:+ """A small front paw, a shade lighter than the fur."""+ paw = bp.ellipse(cx, cy, rx, ry)+ f.part(paw, bp.shade(paw, FUR_LIGHT, dark=FUR, dark_depth=1))+++def _arm(f: Frame, start: Px, paw: tuple[float, float]) -> None:+ """A thin front leg from the shoulder ``start`` to a paw centred on ``paw``."""+ _fur(f, bp.thick(bp.line(*start, math.floor(paw[0]), math.floor(paw[1]))), 1)+ _paw(f, *paw)+++def _acorn(f: Frame, x: int, y: int, *, small: bool = False) -> None:+ """An acorn glyph with its top-left at ``(x, y)``."""+ f.glyph(x, y, _ACORN_S if small else _ACORN, _ACORN_PALETTE)+++def _clip(f: Frame) -> None:+ """Drop whatever a prop painted outside the cell."""+ for p in [p for p in f.px if not (0 <= p[0] < bp.CELL and 0 <= p[1] < bp.CELL)]:+ del f.px[p]+++# -- The tail -----------------------------------------------------------------++Spine = Sequence[tuple[float, float, float]]+++def _brush(spine: Spine) -> tuple[Mask, list[tuple[float, float]]]:+ """A bushy tail: round puffs along ``spine`` (x, y, radius) sampled a+ pixel apart, with locks of fur bulging out of alternate sides every few+ pixels. Also returns the centre line the dark streak follows."""+ mask: Mask = set()+ centre: list[tuple[float, float]] = []+ for (x0, y0, r0), (x1, y1, r1) in zip(spine, spine[1:], strict=False):+ length = math.hypot(x1 - x0, y1 - y0)+ steps = max(1, math.ceil(length))+ nx, ny = (y0 - y1) / (length or 1), (x1 - x0) / (length or 1)+ for k in range(steps + 1):+ t = k / steps+ x, y, r = x0 + (x1 - x0) * t, y0 + (y1 - y0) * t, r0 + (r1 - r0) * t+ mask |= bp.ellipse(x, y, r, r)+ centre.append((x, y))+ if len(centre) % 4 == 2 and r >= 3:+ # Round locks of fur bulge out of the edge, alternating sides,+ # so the outline billows like a cloud.+ side = 1 if len(centre) % 8 == 2 else -1+ lx, ly = x + side * nx * (r - 0.2), y + side * ny * (r - 0.2)+ mask |= bp.ellipse(lx, ly, 2.0, 2.0)+ return mask, centre+++def _tail(f: Frame, spine: Spine, *, puff: float = 0.0) -> Mask:+ """Paint a tail along ``spine``; ``puff`` widens every puff (a bottle-brush)."""+ if puff:+ spine = [(x, y, r + puff) for x, y, r in spine]+ mask, centre = _brush(spine)+ colors = bp.shade(mask, TAIL, light=TAIL_LIGHT, dark=TAIL_DARK, dark_depth=1)+ # A second light row along the top-left edge: soft, fluffy fur.+ for x, y in bp.eroded(mask):+ if (x - 1, y - 1) not in mask or (x, y - 2) not in mask:+ colors[(x, y)] = TAIL_LIGHT+ # The darker streak down the middle.+ inner = bp.eroded(mask, 2)+ streak = {(math.floor(x + 0.5), math.floor(y)) for x, y in centre[2:-3]}+ for p in streak & inner:+ colors[p] = TAIL_SPINE+ f.part(mask, colors)+ return mask+++#: Tail spines, base first. The base tucks in behind the right hip.+_TAIL_REST: Spine = (+ (29, 41, 2.6),+ (35, 39.5, 4.0),+ (38.5, 33, 5.4),+ (39, 25, 5.8),+ (37.5, 17, 5.4),+ (34.5, 11.5, 4.4),+ (30.5, 9.5, 3.0),+)+#: Straight up: alert.+_TAIL_UP: Spine = (+ (30, 41, 3.0),+ (35, 38, 4.0),+ (37, 31, 4.4),+ (37, 23, 4.8),+ (37, 15, 4.6),+ (37.5, 10, 3.8),+ (38, 7, 2.6),+)+++def _tail_swayed(sway: float, curl: float = 0.0) -> Spine:+ """The resting tail with its upper half swayed ``sway`` px sideways and the+ tip curled ``curl`` px further down."""+ out = []+ for k, (x, y, r) in enumerate(_TAIL_REST):+ weight = max(0.0, (k - 2) / 4)+ tip = curl if k == len(_TAIL_REST) - 1 else curl / 2 if k == len(_TAIL_REST) - 2 else 0+ out.append((x + sway * weight, y + tip, r))+ return out+++def _bristle(f: Frame, i: int, x_min: int) -> None:+ """Puffed-up fur: tufts poke out of the outline right of ``x_min`` and+ tremble from frame to frame."""+ filled = dict(f.px)+ for (x, y), color in filled.items():+ if color != bp.OUTLINE or x < x_min or (2 * x + 3 * y + i) % 5 != 0:+ continue+ for dx, dy in ((1, 0), (0, -1), (-1, 0), (0, 1)):+ tip = (x + dx, y + dy)+ far = (x + 2 * dx, y + 2 * dy)+ if tip in filled or not (0 <= far[0] < bp.CELL and 0 <= far[1] < bp.CELL):+ continue+ if far in filled or filled.get((x - dx, y - dy)) in (None, bp.OUTLINE):+ continue+ fur = TAIL_LIGHT if dy < 0 or dx < 0 else TAIL+ f.px[(x, y)] = fur+ f.px[tip] = fur+ f.px[far] = bp.OUTLINE+ for side in ((dy, dx), (-dy, -dx)):+ beside = (tip[0] + side[0], tip[1] + side[1])+ if beside not in filled:+ f.px[beside] = bp.OUTLINE+ break+++# -- The head -----------------------------------------------------------------++#: Left ear tip per look; the right ear uses the same table, mirrored.+_EAR_TIPS: dict[str, tuple[float, float]] = {+ "rest": (13.0, 6.0),+ "perk": (14.0, 5.0),+ "flick": (10.5, 6.5),+ "back": (9.5, 9.5),+ "flat": (7.5, 13.5),+}+++def _ear(f: Frame, look: str, *, tuft: int = 3) -> Mask:+ """One left ear: a pointed triangle with a dark tuft growing off its tip."""+ tx, ty = _EAR_TIPS[look]+ ear = bp.polygon([(11.5, 16.5), (tx, ty), (19.5, 12.0)])+ _fur(f, ear, 1)+ f.paint(bp.eroded(ear, 2), EAR_INNER)+ # The tuft: a little flame of dark fur that leans away from the head.+ x, y = math.floor(tx), math.floor(ty)+ lean = -1 if tx < 12 else 0+ tuft_mask = bp.line(x, y + 1, x + lean, y - tuft + 1) | {(x - 1 + lean, y - tuft + 2)}+ if tuft >= 3:+ tuft_mask |= {(x + 1, y), (x - 1, y - 1)}+ f.part(tuft_mask, {p: TUFT_LIGHT if p[1] == y + 1 else TUFT for p in tuft_mask})+ return ear+++def _head(+ f: Frame,+ pose: Pose,+ *,+ cheeks: float = 0.0,+ nose_dy: int = 0,+) -> None:+ """Head and face of the sitting squirrel; ``cheeks`` puffs them out and+ ``nose_dy`` twitches the nose."""+ head = bp.ellipse(AXIS, 20.5, 10.5, 8.5) | bp.ellipse(AXIS, 24, 11 + cheeks, 5 + cheeks / 2)+ _fur(f, head)+ # Cream cheeks and muzzle.+ cheek = bp.ellipse(18.0 - cheeks / 2, 26, 4.2 + cheeks, 3.2 + cheeks / 2)+ muzzle = cheek | _mirror(cheek)+ muzzle = {p for p in muzzle if p in bp.eroded(head)}+ f.paint(muzzle, CREAM)+ f.paint({p for p in muzzle if p[1] >= 29 and (p[0], p[1] + 1) not in muzzle}, CREAM_DARK)+ # A pale rim on the outer side of each eye.+ ring = {(14, 19), (14, 20), (14, 21)}+ f.paint(ring | _mirror(ring), FUR_LIGHT)+ f.paint({(13, 24), (14, 24), (29, 24), (30, 24)}, BLUSH)+ _eyes(f, pose.eyes)+ f.paint(bp.rect(21, 24 + nose_dy, 22, 24 + nose_dy) | {(21, 23 + nose_dy)}, NOSE)+ _mouth(f, pose.mouth)+++def _ears(f: Frame, ears: tuple[str, str], tuft: int = 3) -> None:+ """Both ears: the left one as drawn, the right one mirrored from its own look."""+ for side, look in enumerate(ears):+ if side == 0:+ _ear(f, look, tuft=tuft)+ else:+ sub = bp.Frame()+ _ear(sub, look, tuft=tuft)+ f.paint_map({(2 * AXIS - 1 - x, y): c for (x, y), c in sub.px.items()})+++#: Big, dark, glossy eyes (an ``EyeStyle``, built in ``design()``).+_EYES: Any = None+++def _eyes(f: Frame, style: str) -> None:+ bp.draw_eyes(f, style, EYE_L, EYE_R, _EYES)+ if style in ("open", "wide"):+ # The second glint: bright, wet eyes.+ f.paint({(15, 21), (26, 21)}, IRIS_GLINT)+++def _mouth(f: Frame, style: str) -> None:+ """The squirrel mouth: a small split lip under the nose, with buck teeth+ showing whenever it opens."""+ x, y = 20, 26+ if style == "rest":+ f.paint({(20, 26), (21, 25), (22, 25), (23, 26)}, bp.OUTLINE)+ f.paint({(21, 26), (22, 26)}, TOOTH)+ elif style in ("half", "open", "wide"):+ bp.draw_mouth(f, style, x, y, 4)+ f.paint({(21, y + 1), (22, y + 1)}, TOOTH)+ elif style == "nibble":+ f.paint(bp.rect(20, 26, 23, 27), bp.MOUTH_DARK)+ f.paint(bp.rect(20, 26, 23, 26), bp.OUTLINE)+ f.paint({(21, 27), (22, 27)}, TOOTH)+ elif style == "lick":+ bp.draw_mouth(f, "smile", x, y, 4)+ f.paint({(21, 28), (22, 28), (22, 29)}, bp.TONGUE)+ elif style == "puff":+ f.paint(bp.rect(21, 27, 22, 27), bp.OUTLINE)+ else:+ bp.draw_mouth(f, style, x, y, 4)+++# -- Poses --------------------------------------------------------------------++#: Idle: the tail's slow sway over the seven breathing cells.+_IDLE_SWAY = (0, 0, 1, 2, 2, 1, 0)+#: Listening: the ears take turns twitching forward.+_LISTEN_EARS = (+ ("perk", "perk"),+ ("perk", "rest"),+ ("perk", "perk"),+ ("rest", "perk"),+ ("perk", "perk"),+ ("perk", "perk"),+)+#: Thinking: the tail tip flicks (sway, curl) while she ponders.+_THINK_FLICK = ((0, 0), (2, -1), (0, 0), (-1, 1), (0, 0), (2, -1), (0, 0), (-1, 1))+#: Success: where the tossed acorn's top-left is per frame (None while in the paws).+_TOSS = (None, (7, 17), (4, 6), (5, 2), (8, 9), None, None, None)+#: Working: which paw presses a key (left, right), then a pause.+_TYPE = (+ (True, False),+ (False, True),+ (True, False),+ (False, False),+ (False, True),+ (True, False),+ (False, True),+ (False, False),+)+#: Held: the swing from the cursor.+_SWAY = (-1, 0, 1, 1, 0, -1)+_SHAKE = (-2, 2, -2, 2, -1, 0)+++def hazel_pose(state: str, i: int) -> Pose:+ pose = bp.base_pose(state, i)+ if state == "idle" and i == 5:+ return bp.replace(pose, mouth="rest")+ if state == "thinking":+ return bp.replace(pose, mouth="closed")+ if state == "error":+ return bp.Pose(state, i, dx=_SHAKE[i], eyes="wide", mouth="half")+ if state == "held":+ return bp.Pose(state, i, dx=_SWAY[i % len(_SWAY)], eyes="wide", mouth="half")+ return pose+++# -- The sitting squirrel -----------------------------------------------------++#: The shoulders front legs grow from (left, right).+_SHOULDERS: tuple[Px, Px] = ((17, 33), (26, 33))+++def _sit(+ f: Frame,+ pose: Pose,+ *,+ ears: tuple[str, str] = ("rest", "rest"),+ tail: Spine | None = None,+ puff: float = 0.0,+ paws: str = "chest",+ tuft: int = 3,+ cheeks: float = 0.0,+ head_dx: int = 0,+ nose_dy: int = 0,+) -> None:+ """The sitting squirrel every upright state draws: tail, body, hind feet,+ the head and the front paws (``"chest"`` held together under the chin,+ ``"none"`` when the caller draws them)."""+ _tail(f, _TAIL_REST if tail is None else tail, puff=puff)+ body = bp.ellipse(AXIS, 37, 8.5, 7)+ _fur(f, body)+ f.paint(bp.ellipse(AXIS, 38.5, 4.8, 4.6), CREAM)+ for cx in (15.5, 28.5):+ foot = bp.ellipse(cx, 43.2, 3.4, 1.8)+ f.part(foot, bp.shade(foot, FUR, light=FUR_LIGHT, dark=FUR_DARK, dark_depth=1))+ with f.offset(head_dx, 0):+ _ears(f, ears, tuft)+ _head(f, pose, cheeks=cheeks, nose_dy=nose_dy)+ if paws == "chest":+ _paw(f, 19.5, 32.5)+ _paw(f, 24.5, 32.5)+++def draw_hazel(f: Frame, pose: Pose) -> None:+ """The pet's ``draw``: the body for the pose, kept inside the cell."""+ _draw_body(f, pose)+ _seal(f)+++def _seal(f: Frame) -> None:+ """Keep a hop or a puffed tail inside the cell: drop whatever pokes out and+ close the silhouette with the outline along the cell's edge."""+ last = bp.CELL - 1+ for p in [p for p in f.px if not (0 <= p[0] <= last and 0 <= p[1] <= last)]:+ del f.px[p]+ for (x, y), color in list(f.px.items()):+ if color != bp.OUTLINE and (x in (0, last) or y in (0, last)):+ f.px[(x, y)] = bp.OUTLINE+++def _draw_body(f: Frame, pose: Pose) -> None:+ if pose.act:+ _ACT_BODIES[pose.act](f, pose)+ elif pose.state == "sleeping":+ _draw_curled(f, pose)+ elif pose.state == "held":+ _draw_dangling(f, pose)+ else:+ with f.offset(pose.dx, pose.dy):+ _draw_sitting(f, pose)+ if pose.state == "error":+ _bristle(f, pose.i, 30 + pose.dx)+++def _draw_sitting(f: Frame, pose: Pose) -> None:+ state, i = pose.state, pose.i+ if state == "idle":+ if i >= len(_IDLE_SWAY):+ _sit(f, pose, ears=("flick", "rest"), tail=_tail_swayed(-1, 1))+ else:+ _sit(f, pose, tail=_tail_swayed(_IDLE_SWAY[i]), nose_dy=-1 if i == 5 else 0)+ elif state == "listening":+ lift = (0, 1, 1, 0, 1, 0)[i % 6]+ tail = [(x, y - (lift if k > 2 else 0), r) for k, (x, y, r) in enumerate(_TAIL_UP)]+ _sit(f, pose, ears=_LISTEN_EARS[i % 6], tail=tail, tuft=4)+ elif state == "thinking":+ sway, curl = _THINK_FLICK[i % 8]+ _sit(f, pose, tail=_tail_swayed(sway, curl), paws="none")+ # Both paws hold an acorn to the chin; it taps on every other beat.+ tap = 1 if i % 4 in (1, 2) else 0+ _paw(f, 17.5, 31.5 + tap)+ _paw(f, 26.5, 31.5 + tap)+ _acorn(f, 20, 28 + tap)+ elif state == "talking":+ ears = ("rest", "rest") if i < 3 else ("perk", "perk")+ _sit(f, pose, ears=ears, tail=_tail_swayed((0, 1, 1, 2)[i % 4]))+ elif state == "success":+ up = _TOSS[i] is not None+ ears = ("perk", "perk") if pose.dy >= -1 else ("back", "back")+ _sit(+ f,+ pose,+ ears=ears,+ tail=_tail_swayed(-1 if up else 1, -1),+ paws="none" if up else "chest",+ )+ if up:+ # Hooray: both paws up after the acorn.+ _arm(f, _SHOULDERS[0], (11.5, 27))+ _arm(f, _SHOULDERS[1], (32.5, 27))+ else:+ # Caught: back in both paws.+ _paw(f, 17.5, 31.5)+ _paw(f, 26.5, 31.5)+ _acorn(f, 20, 28)+ elif state == "error":+ _sit(f, pose, ears=("flat", "flat"), tail=_tail_swayed(-3, -1), puff=1.0)+ elif state == "working":+ sway = (0, 1, 1, 0, -1, -1, 0, 0)[i % 8]+ glow = any(_TYPE[i % 8])+ _sit(+ f,+ pose,+ tail=_tail_swayed(sway),+ paws="none",+ ears=("rest", "flick") if i in (3, 4) else ("rest", "rest"),+ )+ if glow:+ f.paint({(19, 29), (20, 29), (21, 29), (22, 29), (23, 29), (24, 29)}, bp.SCREEN_GLOW)+ elif state == "searching":+ look = bp.SEARCH_SWEEP[i % 8]+ lean = (look > 2) - (look < -2)+ _sit(+ f, pose, ears=("perk", "perk"), tail=_tail_swayed(-lean), head_dx=lean, nose_dy=-(i % 2)+ )+ else:+ _sit(f, pose)+++# -- Sleeping and held --------------------------------------------------------+++def _draw_curled(f: Frame, pose: Pose) -> None:+ """Asleep: curled in a ball with the head on the paws, the big tail wrapped+ up over the back like a blanket, its tip twitching in a dream."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ body = bp.ellipse(26, 38.5, 11, 6)+ _fur(f, body)+ twitch = (0, 0, 1, 1, 0, 0)[i % 6]+ _tail(+ f,+ (+ (35, 42.5, 3.0),+ (39.5, 37, 4.4),+ (38, 30.5, 5.2),+ (31, 27, 5.2),+ (24, 27.5, 4.4),+ (20.5 - twitch, 30.5, 3.0),+ ),+ )+ for ear in (+ bp.polygon([(9.5, 34.5), (10.5, 27.5), (15.5, 31.5)]),+ bp.polygon([(17.5, 31.5), (21.5, 27.5), (22.0, 34.0)]),+ ):+ _fur(f, ear, 1)+ f.paint(bp.eroded(ear, 2), EAR_INNER)+ head = bp.ellipse(16, 38, 8, 6) | bp.ellipse(16, 40.5, 8.5, 3.5)+ _fur(f, head)+ cheek = bp.ellipse(12.5, 41.5, 3, 2) | bp.ellipse(19.5, 41.5, 3, 2)+ f.paint(cheek & bp.eroded(head), CREAM)+ bp.draw_eye(f, "sleep", 10, 37, _EYES, right=False)+ bp.draw_eye(f, "sleep", 19, 37, _EYES, right=True)+ f.paint({(15, 40), (16, 40), (15, 39)}, NOSE)+ f.paint({(8, 40), (23, 40)}, BLUSH)+ _paw(f, 12.0, 44.5, 2.0, 1.2)+ _paw(f, 19.0, 44.5, 2.0, 1.2)+++def _draw_dangling(f: Frame, pose: Pose) -> None:+ """Held: hanging by the scruff, legs dangling and the tail swinging behind."""+ lag = -pose.dx+ with f.offset(pose.dx, 0):+ _tail(+ f,+ (+ (26, 38, 2.8),+ (30 + lag, 41.5, 3.6),+ (35 + 2 * lag, 40, 4.0),+ (37 + 3 * lag, 34, 4.0),+ (36 + 3 * lag, 28, 3.4),+ (34 + 3 * lag, 24, 2.6),+ ),+ )+ for x in (18, 25):+ _fur(f, bp.thick(bp.line(x, 38, x + lag, 42)), 1)+ foot = bp.ellipse(x + 1 + lag, 44, 2.2, 1.6)+ f.part(foot, bp.shade(foot, FUR, light=FUR_LIGHT, dark=FUR_DARK, dark_depth=1))+ body = bp.ellipse(AXIS, 32, 6.5, 8)+ _fur(f, body)+ f.paint(bp.ellipse(AXIS, 33.5, 3.6, 5), CREAM)+ for x, end in ((17, 14), (26, 29)):+ _fur(f, bp.thick(bp.line(x, 28, end + lag, 33)), 1)+ _paw(f, end + 1 + lag, 34.5)+ with f.offset(0, -3):+ tuft = bp.polygon([(19.5, 13.5), (22.0, 8.5), (24.5, 13.5)])+ _fur(f, tuft, 1)+ _ears(f, ("back", "back"), 2)+ _head(f, pose)+++# -- Idle acts: bodies --------------------------------------------------------+++def _draw_toss(f: Frame, pose: Pose) -> None:+ """Acorn toss: flicked up past the left ear, eyes on it, caught again."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ _sit(+ f,+ pose,+ tail=_tail_swayed((0, 0, -1, -1, 0, 1, 1, 0)[i]),+ paws="none",+ ears=("perk", "perk") if 1 <= i <= 5 else ("rest", "rest"),+ )+ if 2 <= i <= 4:+ # The left paw follows the acorn up; the right stays at the chest.+ _arm(f, _SHOULDERS[0], (11.5, (27, 25, 28)[i - 2]))+ _paw(f, 24.5, 32.5)+ else:+ lift = 2 if i == 1 else 0+ _paw(f, 17.5, 31.5 - lift)+ _paw(f, 26.5, 31.5 - lift)+ if i != 1:+ _acorn(f, 20, 28)+ else:+ _acorn(f, 18, 25)+++#: Tail grooming: how far round the front the tail has come (0 behind, 1 halfway, 2 in front).+_GROOM = (0, 1, 2, 2, 2, 2, 1, 0)+++def _draw_groom(f: Frame, pose: Pose) -> None:+ """Tail grooming: the tail hugged round the front and combed with both paws."""+ i = pose.i+ reach = _GROOM[i]+ with f.offset(pose.dx, pose.dy):+ if reach == 0:+ _sit(f, pose)+ return+ if reach == 1:+ _sit(f, pose, tail=_tail_swayed(-4, 2))+ return+ _sit(f, pose, tail=((30, 41, 3.0), (34, 40, 3.4)), paws="none", head_dx=-1)+ comb = (0, 0, 0, 1, 0, 1, 0, 0)[i]+ _tail(+ f,+ (+ (31, 42, 2.8),+ (25, 43, 3.4),+ (18, 41.5, 3.8),+ (13.5, 37.5, 3.8),+ (12, 33 + comb, 3.2),+ (13.5, 29.5 + comb, 2.4),+ ),+ )+ _paw(f, 16.5, 34.5 + comb)+ _paw(f, 20.5, 38.0 - comb)+++#: Burying a nut: how far the whole squirrel leans to the left per frame.+_BURY_LEAN = (0, -1, -3, -3, -3, -3, -1, 0)+#: The digging paws' heights per frame (None: paws at the chest).+_BURY_DIG: tuple[tuple[float, float] | None, ...] = (+ None,+ None,+ (42.5, 40.5),+ (40.5, 42.5),+ (42.5, 42.5),+ (41.5, 43.0),+ (43.0, 41.5),+ None,+)+++def _draw_bury(f: Frame, pose: Pose) -> None:+ """Burying a nut: lean over, dig, drop the acorn in, pat the earth down."""+ i = pose.i+ dig = _BURY_DIG[i]+ with f.offset(pose.dx + _BURY_LEAN[i], pose.dy):+ _sit(+ f,+ pose,+ tail=_tail_swayed((0, 1, 2, 2, 1, 2, 1, 0)[i]),+ paws="none",+ nose_dy=-1 if i == 1 else 0,+ )+ if dig is None:+ _paw(f, 17.5, 31.5)+ _paw(f, 26.5, 31.5)+ if i in (0, 1):+ _acorn(f, 20, 28)+ else:+ _paw(f, 16.5, dig[0], 2.2, 1.5)+ _paw(f, 21.5, dig[1], 2.2, 1.5)+++#: Cheek stuffing: how far the cheeks are puffed out per frame.+_CHEEKS = (0.0, 0.0, 0.0, 0.8, 1.4, 1.8, 1.8, 1.0)+_CHEEK_PAWS = (31.5, 30.0, 28.5, 31.0, 31.5, 31.5, 31.5, 31.5)+++def _draw_cheek_stuff(f: Frame, pose: Pose) -> None:+ """Cheek stuffing: one more acorn in, and the cheeks balloon out."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ _sit(+ f,+ pose,+ cheeks=_CHEEKS[i],+ paws="none",+ ears=("perk", "perk") if i >= 5 else ("rest", "rest"),+ )+ _paw(f, 17.5, _CHEEK_PAWS[i])+ _paw(f, 26.5, _CHEEK_PAWS[i])+ if i <= 2:+ _acorn(f, 20, (28, 26, 25)[i])+++#: Nibbling: the pixels bitten out of the acorn per frame.+_BITES: tuple[frozenset[Px], ...] = (+ frozenset(),+ frozenset(),+ frozenset({(23, 31)}),+ frozenset({(23, 31)}),+ frozenset({(23, 31), (23, 30)}),+ frozenset({(23, 31), (23, 30), (21, 31)}),+)+++def _draw_nibble(f: Frame, pose: Pose) -> None:+ """Nibbling: the acorn turned in both paws at the mouth, bitten smaller."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ _sit(f, pose, paws="none")+ if i <= 5:+ jig = (0, 0, 1, 0, 1, 0)[i]+ _paw(f, 17.5, 31.0 + jig)+ _paw(f, 26.5, 31.0 + jig)+ _acorn(f, 20, 27 + jig)+ # Bites out of the nut: it gets smaller as she nibbles.+ f.paint({(x, y + jig) for x, y in _BITES[i]}, bp.OUTLINE)+ else:+ _paw(f, 19.5, 32.5)+ _paw(f, 24.5, 32.5)+++#: Ear scratch: the paw's height at the ear per frame (None: paws at the chest).+_SCRATCH = (None, 21.0, 15.0, 13.0, 15.0, 13.0, 21.0, None)+++def _draw_ear_scratch(f: Frame, pose: Pose) -> None:+ """A quick scratch behind the left ear, the arm reaching round the cheek."""+ i = pose.i+ height = _SCRATCH[i]+ ears = ("flick", "rest") if i in (3, 5) else ("rest", "rest")+ with f.offset(pose.dx, pose.dy):+ _sit(+ f,+ pose,+ ears=ears,+ paws="none" if height else "chest",+ tail=_tail_swayed((0, 1, 2, 1, 2, 1, 1, 0)[i]),+ head_dx=1 if height and height < 20 else 0,+ )+ if height:+ arm = bp.thick(bp.polyline([(15, 33), (10, 27), (10, math.floor(height) + 2)]))+ _fur(f, arm, 1)+ _paw(f, 11.5, height)+ _paw(f, 24.5, 32.5)+++# -- Effects ------------------------------------------------------------------+++def _success_fx(f: Frame, pose: Pose) -> None:+ """The acorn flies up off the paws, spins at the top and is caught again,+ sparkles pop around the catch."""+ spot = _TOSS[pose.i]+ if spot is not None:+ x, y = spot+ if pose.i in (2, 3):+ # Spinning at the top: the acorn upside down.+ f.glyph(x, y, tuple(reversed(_ACORN)), _ACORN_PALETTE)+ else:+ _acorn(f, x, y)+ bp.fx_sparkles(f, pose.i, ((41, 5, 4), (5, 27, 5), (8, 9, 6), (42, 24, 6)))+++def _working_fx(f: Frame, pose: Pose) -> None:+ """A laptop beside her stash of acorns; paws hammer the keys."""+ i = pose.i+ x, y = 15, 34+ bp.fx_laptop(f, i, x, y)+ pressed = _TYPE[i % 8]+ for down, cx, side in ((pressed[0], 13.0, -1), (pressed[1], 30.0, 1)):+ if down:+ _paw(f, cx, 41.0, 2.4, 1.3)+ tx = math.floor(cx) + side * 3+ f.paint({(tx, 38), (tx + side, 37)}, bp.ARC)+ else:+ _paw(f, cx, 37.5 + pose.dy)+ # The stash: a little pile of acorns on the left, the top one wobbling.+ _acorn(f, 1, 40, small=True)+ _acorn(f, 5, 41, small=True)+ _acorn(f, 3 + (1 if i % 4 == 2 else 0), 37, small=True)+++def _searching_fx(f: Frame, pose: Pose) -> None:+ """The magnifier sweeps across, and sniff puffs come off the nose."""+ bp.fx_magnifier(f, pose.i, *ANCHORS.magnifier)+ if pose.i % 2:+ f.paint({(17, 24), (16, 23), (27, 24), (28, 23)}, bp.ARC_FADE)+++def _toss_fx(f: Frame, pose: Pose) -> None:+ """The acorn's flight past the left ear, flipping over at the top."""+ spot = (None, None, (7, 15), (5, 3), (8, 13), None, None, None)[pose.i]+ if spot is not None:+ f.glyph(*spot, _ACORN if pose.i != 3 else tuple(reversed(_ACORN)), _ACORN_PALETTE)+ if pose.i == 5:+ bp.fx_sparkles(f, 1, ((33, 24, 0),))+++def _groom_fx(f: Frame, pose: Pose) -> None:+ """Sparkles off the freshly combed tail tip."""+ if pose.i in (3, 5):+ bp.fx_sparkles(f, 0, ((8, 24, 0),))+ elif pose.i == 4:+ bp.fx_sparkles(f, 1, ((7, 22, 0),))+++#: The dirt mound per frame (0 none, 1 a little, 2 a pile) and flying dirt.+_MOUND = (0, 0, 1, 2, 2, 2, 1, 1)+++def _bury_fx(f: Frame, pose: Pose) -> None:+ """The dirt mound, flying earth, the acorn going in and a pleased heart."""+ i = pose.i+ size = _MOUND[i]+ if size:+ mound = bp.ellipse(7, 45.5, 3 + 1.2 * size, 1.0 + 1.2 * size)+ mound = {p for p in mound if p[1] <= 45}+ f.part(mound, bp.shade(mound, DIRT, light=DIRT_LIGHT, dark=DIRT_DARK, dark_depth=1))+ if i in (2, 3):+ # Dirt flies up behind the digging paw.+ for p in (((4, 38), (2, 35), (7, 36)), ((3, 34), (6, 32), (1, 38)))[i - 2]:+ f.part({p}, DIRT)+ if i == 4:+ _acorn(f, 6, 36, small=True) # into the hole it goes+ if i == 7:+ f.glyph(4, 37, _HEART, {"#": BLUSH})+ _clip(f)+++def _cheek_fx(f: Frame, pose: Pose) -> None:+ """A heart floats up from the full cheeks."""+ if pose.i == 6:+ f.glyph(33, 14, _HEART, {"#": BLUSH})+ elif pose.i == 7:+ f.glyph(34, 11, _HEART, {"#": BLUSH})+++def _nibble_fx(f: Frame, pose: Pose) -> None:+ """Crumbs and bits of shell fall from the busy teeth."""+ i = pose.i+ if 1 <= i <= 6:+ falls = (+ ((18, 33),),+ ((25, 34), (17, 36)),+ ((19, 35), (26, 38)),+ ((16, 37), (24, 36)),+ ((27, 40), (18, 39)),+ ((16, 42),),+ )[i - 1]+ f.part(set(falls), CRUMB)+ if i == 7:+ f.paint({(25, 44), (17, 45), (27, 45)}, CRUMB)+++def _scratch_fx(f: Frame, pose: Pose) -> None:+ """Scratch marks twitching beside the ear."""+ if pose.i in (2, 3, 4, 5):+ f.paint({(8, 9 + pose.i % 2), (7, 12 + pose.i % 2)}, bp.ARC_FADE)+++#: Effect anchors (an ``Anchors``, built in ``design()``).+ANCHORS: Any = None+++def _act(+ name: str, eyes: Sequence[str], mouth: Sequence[str], fx: Callable | None, fps: int = 8+) -> Any:+ """An eight-frame idle act; its body is drawn by ``_ACT_BODIES[name]``."""+ return bp.IdleAct(+ name,+ 8,+ fps,+ lambda i: bp.Pose("idle", i, eyes=eyes[i], mouth=mouth[i]),+ fx,+ )+++_ACT_BODIES: dict[str, Callable[[Frame, Pose], None]] = {+ "acorn_toss": _draw_toss,+ "tail_groom": _draw_groom,+ "bury_nut": _draw_bury,+ "cheek_stuff": _draw_cheek_stuff,+ "nibble": _draw_nibble,+ "ear_scratch": _draw_ear_scratch,+}+++def design(module: ModuleType) -> Any:+ """Build Hazel with the builder's helpers (``module`` is ``build_pets``)."""+ global bp, _EYES, ANCHORS+ bp = module+ _EYES = bp.EyeStyle(w=3, h=5, iris=IRIS, glint=bp.FX_WHITE, lid=bp.OUTLINE)+ ANCHORS = bp.Anchors(+ arcs_left=(9, 14),+ arcs_right=(34, 14),+ dots=(16, 3),+ zzz=(36, 18),+ bang=(4, 2),+ sweat=(7, 15),+ sparkles=((5, 12, 1), (40, 6, 2), (5, 32, 3), (42, 30, 3)),+ laptop=(15, 34),+ magnifier=(22, 27),+ )+ acts = (+ _act(+ "acorn_toss",+ ("open", "up_left", "up_left", "up_left", "up_left", "open", "happy", "open"),+ ("rest", "rest", "half", "open", "half", "rest", "smile", "rest"),+ _toss_fx,+ ),+ _act(+ "tail_groom",+ ("open", "look_left", "down", "closed", "closed", "closed", "happy", "open"),+ ("rest", "rest", "closed", "lick", "closed", "lick", "smile", "rest"),+ _groom_fx,+ ),+ _act(+ "bury_nut",+ ("open", "look_left", "down", "down", "down", "down", "happy", "happy"),+ ("rest", "rest", "closed", "closed", "half", "closed", "smile", "smile"),+ _bury_fx,+ ),+ _act(+ "cheek_stuff",+ ("open", "open", "happy", "happy", "wide", "happy", "happy", "open"),+ ("rest", "half", "nibble", "puff", "puff", "puff", "puff", "rest"),+ _cheek_fx,+ ),+ _act(+ "nibble",+ ("open", "down", "down", "down", "down", "down", "happy", "open"),+ ("rest", "nibble", "closed", "nibble", "closed", "nibble", "smile", "rest"),+ _nibble_fx,+ fps=10,+ ),+ _act(+ "ear_scratch",+ ("open", "closed", "happy", "happy", "happy", "happy", "closed", "open"),+ ("rest", "rest", "smile", "smile", "smile", "smile", "rest", "rest"),+ _scratch_fx,+ ),+ )+ return bp.PetDesign(+ id="hazel",+ name="Hazel",+ description="A red squirrel who perks her tufted ears to listen and naps inside her tail.",+ draw=draw_hazel,+ pose=hazel_pose,+ anchors=ANCHORS,+ waist=36,+ fx={+ "success": _success_fx,+ "working": _working_fx,+ "searching": _searching_fx,+ },+ acts=acts,+ )diff --git a/scripts/pets/designs/inky.py b/scripts/pets/designs/inky.pynew file mode 100644index 0000000000..3d51841967--- /dev/null+++ b/scripts/pets/designs/inky.py@@ -0,0 +1,741 @@+"""Inky: the living inkwell.++A squat glass ink bottle with a cork-coloured rim, a brass neck ring and deep+blue ink sloshing behind its face. A cream quill feather sticks out of the+neck and does the talking with its body: it perks up to listen, taps while+thinking, waves on success, droops on an error and flops over the face to+sleep. When there is writing to do the quill hops out and scribbles on a+sheet of paper beside the bottle. Literary, neat and a little dramatic.+"""++from __future__ import annotations++import math+from collections.abc import Callable, Sequence++#: The builder module (``scripts/pets/build_pets.py``), set by ``design``.+B = None+++def _hex(value: str) -> tuple[int, int, int, int]:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++# -- Palette -------------------------------------------------------------------++GLASS = _hex("#c6e2ec")+GLASS_LIGHT = _hex("#eef9fc")+GLASS_DARK = _hex("#8fb8c8")+#: Asleep the glass dims a little, like a room with the lamp turned down.+GLASS_COOL = (_hex("#a9c8d6"), _hex("#d3e8f0"), _hex("#7699ab"))+GLASS_SHINE = _hex("#ffffff")+INK = _hex("#2c3fa0")+INK_DEEP = _hex("#1b276b")+INK_TOP = _hex("#5d7ff0")+INK_SHINE = _hex("#93a9f6")+#: A 1 px ink stroke in the air: a mid blue that holds on dark AND light.+INK_STROKE = _hex("#4a6ff0")+INK_STROKE_FADE = _hex("#8ea4f0")+CORK = _hex("#b98552")+CORK_LIGHT = _hex("#deaa76")+CORK_DARK = _hex("#86562f")+GOLD = _hex("#ffcf45")+GOLD_LIGHT = _hex("#fff1a8")+GOLD_DARK = _hex("#c9952a")+QUILL = _hex("#f7eedb")+QUILL_LIGHT = _hex("#fffaf0")+QUILL_SHADE = _hex("#e2cfa6")+QUILL_SHAFT = _hex("#c4a26a")+#: The bare quill below the vane: translucent, almost white.+QUILL_BARREL = _hex("#fdf8ec")+NIB = _hex("#3a3f52")+NIB_LIGHT = _hex("#8d95a6")+FACE = _hex("#1b1f3a")+PAPER = _hex("#fbf6e9")+PAPER_DARK = _hex("#d8cdb4")+PAPER_RULE = _hex("#c9d6ee")++# -- Geometry (cell coordinates, before the pose offset) -----------------------++#: Where the quill leaves the bottle's mouth.+QUILL_BASE = (24, 15)+#: The resting ink level (its surface row).+INK_LEVEL = 35+EYE_L = (17, 26)+EYE_R = (28, 26)+MOUTH = (22, 31)+#: The sheet of paper Inky writes on while working (top-left).+PAPER_AT = (34, 30)+++def _body() -> set:+ """The glass bottle: rounded shoulders, a flatter bottom."""+ top = B.rounded_rect(11, 23, 36, 34, 5)+ bottom = B.rounded_rect(11, 28, 36, 41, 2)+ return top | bottom+++# -- Quill ---------------------------------------------------------------------+++def _quill_path(bx: float, by: float, angle: float, curl: float, length: float) -> list:+ """Centre line of the quill as (x, y, angle_radians, t) samples, base first.++ ``angle`` is degrees clockwise from straight up; ``curl`` is how many+ more degrees the tip bends.+ """+ steps = max(2, int(length * 2))+ x, y = bx + 0.5, by + 0.5+ out = []+ for k in range(steps + 1):+ t = k / steps+ a = math.radians(angle + curl * t)+ out.append((x, y, a, t))+ x += math.sin(a) * 0.5+ y -= math.cos(a) * 0.5+ return out+++def _vane_width(t: float, side: float, ruffle: bool, k: int) -> float:+ if t < 0.2:+ return 0.0+ u = min(1.0, (t - 0.2) / 0.84)+ w = side * math.sin(math.pi * u) ** 0.6+ if ruffle and k % 3 == 0:+ w += 1.2+ return w+++def quill_tip(bx: float, by: float, angle: float, curl: float, length: float) -> tuple:+ x, y, _a, _t = _quill_path(bx, by, angle, curl, length)[-1]+ return math.floor(x), math.floor(y)+++def quill_length(angle: float, curl: float, lift: int) -> int:+ """13 px, or shorter where standing tall would take the tip out of the cell."""+ bx, by = QUILL_BASE+ length = 15+ while quill_tip(bx, by, angle, curl, length)[1] + lift < 3:+ length -= 1+ return length+++def draw_quill(+ f,+ bx: int,+ by: int,+ angle: float,+ curl: float,+ length: float = 14,+ *,+ nib: bool = False,+ ruffle: bool = False,+ shine: bool = False,+) -> None:+ """The cream quill feather from its base ``(bx, by)``.++ The left vane is lit, the right one shaded, a tan shaft runs down the+ middle; with ``nib`` the steel writing nib shows below the base.+ """+ path = _quill_path(bx, by, angle, curl, length)+ right_edge, left_edge, spine = [], [], []+ for k, (x, y, a, t) in enumerate(path):+ nx, ny = math.cos(a), math.sin(a)+ wr = _vane_width(t, 3.3, ruffle, k)+ wl = _vane_width(t, 2.5, ruffle, k + 1)+ if t >= 0.18:+ spine.append((x, y))+ right_edge.append((x + nx * wr, y + ny * wr))+ left_edge.append((x - nx * wl, y - ny * wl))+ right = B.polygon(spine + right_edge[::-1])+ left = B.polygon(spine + left_edge[::-1])+ shaft = B.polyline([(math.floor(x), math.floor(y)) for x, y, _a, t in path if t <= 0.9])+ mask = right | left | shaft+ nib_px: set = set()+ if nib:+ a = path[0][2]+ nib_px = B.line(+ bx, by, round(bx - math.sin(a) * 3), round(by + math.cos(a) * 3)+ ) - shaft+ mask |= nib_px+ fill = {}+ for p in mask:+ fill[p] = QUILL_SHADE if p in right and p not in left else QUILL+ for p in left - right:+ fill[p] = QUILL_LIGHT if shine else QUILL+ barrel = {(math.floor(x), math.floor(y)) for x, y, _a, t in path if t < 0.2}+ for p in shaft:+ fill[p] = QUILL_BARREL if p in barrel else QUILL_SHAFT+ for p in nib_px:+ fill[p] = NIB+ f.part(mask, fill)+ if nib_px:+ tip = max(nib_px, key=lambda p: p[1])+ f.paint({tip}, INK_TOP)+++# -- Per-pose tables ---------------------------------------------------------------++#: (angle, curl) of the quill per state frame; length stays 14.+_QUILL: dict[str, tuple[tuple[float, float], ...]] = {+ "idle": ((20, 22), (20, 22), (18, 22), (15, 22), (15, 22), (18, 22), (20, 22), (20, 22)),+ "listening": ((0, 4), (-3, 6), (0, 4), (3, 4), (0, 4), (-2, 6)),+ "thinking": ((20, 22), (45, 30), (75, 40), (45, 30), (20, 22), (45, 30), (75, 40), (45, 30)),+ "talking": ((22, 24), (16, 20), (10, 16), (4, 12)),+ "success": ((20, 22), (30, 28), (38, 32), (30, 28), (20, 22), (12, 16), (20, 22), (20, 22)),+ "error": ((40, 30), (70, 45), (95, 50), (110, 50), (115, 50), (115, 50)),+ "sleeping": ((-118, -45), (-118, -45), (-122, -45), (-122, -45), (-118, -45), (-118, -45)),+ "searching": (+ (-12, -10), (-8, -8), (0, 6), (10, 12), (16, 16), (10, 12), (0, 6), (-8, -8),+ ),+ "held": ((35, 30), (20, 22), (5, 10), (20, 22), (35, 30), (20, 22)),+}++#: Held: the ink surface's tilt per frame (rows of drop per column), lagging the swing.+_HELD_TILT = (0.25, 0.0, -0.25, 0.0, 0.25, 0.0)+_WORK_DX = -4+++def inky_pose(state: str, i: int):+ if state == "working":+ return B.Pose(state, i, dx=_WORK_DX, dy=B.WORK_BOB[i], eyes="look_right", mouth="closed")+ if state == "held":+ mouth = ("half", "open")[i % 2]+ return B.Pose(+ state, i, dx=B.HELD_SWAY[i], dy=-2, eyes="wide", mouth=mouth, stretch=1+ )+ return B.base_pose(state, i)+++def _quill_for(pose) -> tuple[float, float] | None:+ """The quill's (angle, curl) in this pose, or None when it is out writing."""+ if pose.act:+ return _ACT_QUILL[pose.act][pose.i]+ if pose.state == "working":+ return None+ return _QUILL[pose.state][pose.i]+++def _ink_shape(pose) -> tuple[int, float, int]:+ """(level, tilt, wave phase) of the ink surface; phase -1 = calm."""+ if pose.act:+ level, tilt = _ACT_INK.get(pose.act, ((INK_LEVEL, 0.0),) * 8)[pose.i]+ return level, tilt, -1+ if pose.state == "held":+ return INK_LEVEL, _HELD_TILT[pose.i], -1+ if pose.state == "error":+ return INK_LEVEL + (1 if pose.i >= 2 else 0), 0.0, pose.i if pose.i < 3 else -1+ if pose.state == "talking":+ return INK_LEVEL, 0.0, pose.i if pose.i == 3 else -1+ if pose.state == "success":+ return INK_LEVEL, 0.0, pose.i if 2 <= pose.i <= 5 else -1+ return INK_LEVEL, 0.0, -1+++# -- The body ---------------------------------------------------------------------+++def _paint_ink(f, body: set, pose) -> set:+ level, tilt, phase = _ink_shape(pose)+ inner = B.eroded(body, 1)+ ink: dict = {}+ for x, y in inner:+ surface = level + math.floor(round((x - 23.5) * tilt + 0.5, 6))+ if phase >= 0:+ surface += math.floor(math.sin((x + 2 * phase) * math.pi / 4) + 0.5)+ if y < surface:+ continue+ if y == surface:+ ink[(x, y)] = INK_TOP+ elif (x + 1, y) not in inner or (x, y + 1) not in inner:+ ink[(x, y)] = INK_DEEP+ else:+ ink[(x, y)] = INK+ f.paint_map(ink)+ return set(ink)+++def _swirl(i: int) -> set:+ """Thinking: three dashes circling inside the ink."""+ out = set()+ for k in range(3):+ a = 2 * math.pi * (i / 8 + k / 3)+ x = math.floor(23.5 + math.cos(a) * 6)+ y = math.floor(38.5 + math.sin(a) * 1.6)+ out |= {(x, y), (x + (1 if math.sin(a) > 0 else -1), y)}+ return out+++def draw_inky(f, pose) -> None:+ sleeping = pose.state == "sleeping"+ base, light, dark = GLASS_COOL if sleeping else (GLASS, GLASS_LIGHT, GLASS_DARK)+ with f.offset(pose.dx, pose.dy):+ # Two tiny cork feet peeking out under the bottle.+ for x0 in (14, 28):+ foot = B.rounded_rect(x0, 41, x0 + 5, 44, 1)+ f.part(foot, B.shade(foot, CORK, light=CORK_LIGHT, dark=CORK_DARK, dark_depth=1))+ # The neck, the brass ring and the cork-coloured lip.+ neck = B.rect(19, 16, 28, 23)+ f.part(neck, B.shade(neck, base, light=light, dark=dark, dark_depth=1))+ body = _body()+ f.part(body, B.shade(body, base, light=light, dark=dark, dark_depth=2))+ ink = _paint_ink(f, body, pose)+ if pose.state == "thinking":+ f.paint(_swirl(pose.i) & ink, INK_SHINE)+ _bubbles(f, pose, ink)+ # The glass shine: a tall streak on the left and a dot on the right.+ streak = B.rect(14, 26, 14, 38) | B.rect(15, 25, 15, 26)+ f.paint(streak - ink, GLASS_SHINE)+ f.paint(streak & ink, INK_SHINE)+ f.paint({(33, 27), (33, 28)}, light)+ ring = B.rect(18, 20, 29, 21)+ f.part(ring, {p: GOLD_LIGHT if p[1] == 20 else GOLD_DARK for p in ring})+ f.paint({(19, 20), (20, 20)}, GLASS_SHINE)+ lip = B.rounded_rect(18, 14, 29, 16, 1)+ f.part(lip, {p: CORK_LIGHT if p[1] == 14 else CORK_DARK if p[1] == 16 else CORK+ for p in lip})+ quill = _quill_for(pose)+ if quill is None:+ f.paint(B.rect(21, 14, 26, 14), INK_DEEP) # the empty mouth of the bottle+ f.paint({(20, 17), (20, 18)}, GLASS_SHINE)+ # The face on the glass.+ eyes = pose.eyes+ B.draw_eyes(f, eyes, EYE_L, EYE_R, INKY_EYES)+ blush = B.rect(14, 31, 15, 31) | B.rect(32, 31, 33, 31)+ if pose.state in ("success", "held") or pose.i in _ACT_BLUSH.get(pose.act, ()):+ blush |= {(13, 31), (34, 31)}+ f.paint(blush, B.BLUSH)+ mx, my = MOUTH+ if pose.mouth == "o":+ f.paint({(mx + 1, my + 1), (mx + 2, my + 1)}, B.MOUTH_DARK)+ f.paint({(mx + 1, my), (mx + 2, my), (mx, my + 1), (mx + 3, my + 1),+ (mx + 1, my + 2), (mx + 2, my + 2)}, FACE)+ else:+ B.draw_mouth(f, pose.mouth, mx, my, 4, color=FACE)+ if quill is not None:+ angle, curl = quill+ if sleeping:+ angle += (0, 0, -3, -3, 0, 0)[pose.i]+ bx, by = QUILL_BASE+ length = quill_length(angle, curl, pose.dy - max(0, pose.stretch))+ draw_quill(+ f, bx, by, angle, curl, length,+ ruffle=pose.act == "feather_preen" and 2 <= pose.i <= 4,+ shine=pose.act == "feather_preen" and pose.i == 6,+ )+++#: Bubbles rising through the ink: per act/state, (x, y) per frame or None.+_IDLE_BUBBLE = (None, (30, 40), (30, 39), (29, 38), (30, 37), None, None, None)+_POP_BUBBLE = ((25, 40), (24, 38), (25, 36), None, None, None, None, None)+++def _bubbles(f, pose, ink: set) -> None:+ spot = None+ if pose.act == "bubble_pop":+ spot = _POP_BUBBLE[pose.i]+ elif pose.state == "idle" and not pose.act:+ spot = _IDLE_BUBBLE[pose.i]+ if spot is None:+ return+ x, y = spot+ big = pose.act == "bubble_pop"+ bubble = B.rect(x, y, x + 1, y + 1) if big else {(x, y)}+ f.paint(bubble & ink, INK_SHINE)+ f.paint({(x, y)} & ink, GLASS_SHINE)+++# -- Effects -------------------------------------------------------------------------++_QUOTE = ("##.##", "##.##", ".#..#")+++def inky_talking(f, pose) -> None:+ """Quotation marks pop up beside the quill as the voice gets louder."""+ with f.offset(pose.dx, pose.dy):+ if pose.i >= 2:+ f.glyph(8, 15 - (pose.i - 2), _QUOTE, {"#": INK_STROKE})+ if pose.i == 3:+ f.glyph(35, 12, _QUOTE, {"#": INK_STROKE})+++_INK_SPARK = {"#": INK_STROKE, "o": INK_SHINE}+++def _ribbon(f, path: Sequence, shown: int, fade: int) -> None:+ """The first ``shown`` points of a flourish as a 1 px ink ribbon; its+ first ``fade`` segments already drying (lighter)."""+ pts = path[:shown]+ if len(pts) < 2:+ return+ old = B.polyline(pts[: fade + 1]) if fade else set()+ f.paint(B.polyline(pts) - old, INK_STROKE)+ f.paint(old, INK_STROKE_FADE)+++#: Success: a cursive swash with a loop, written in the air left of the head.+_SWASH = (+ (19, 11), (17, 7), (14, 4), (10, 3), (7, 5), (7, 8), (10, 9), (12, 7), (11, 4),+ (8, 2), (5, 2), (3, 4),+)+#: How many swash points show per success frame, and how many have dried.+_SWASH_SHOWN = (0, 3, 6, 9, 12, 12, 12, 0)+_SWASH_FADE = (0, 0, 0, 0, 0, 5, 9, 0)+++def inky_success(f, pose) -> None:+ """A happy hop while a swash of ink writes itself in the air, loop and+ all; gold and ink sparkles go off around it."""+ _ribbon(f, _SWASH, _SWASH_SHOWN[pose.i], _SWASH_FADE[pose.i])+ B.fx_sparkles(f, pose.i, ((41, 22, 2), (5, 36, 3), (40, 40, 4)))+ B.fx_sparkles(f, pose.i, ((38, 10, 1), (4, 14, 5)), _INK_SPARK)+++_BLOT = (".##.", "####", "####", ".##.")+_DROP = (".#.", "###", "###")+#: Error: ink drops thrown out of the mouth, (x, y) per frame, mirrored.+_SPLASH = (+ ((21, 9),),+ ((18, 6), (22, 3)),+ ((14, 6), (21, 1)),+ ((11, 10),),+ ((9, 16),),+ (),+)+++def inky_error(f, pose) -> None:+ """An ink blot bursts out of the bottle: drops fly up and out and land+ as blots on the floor; the red "!" overhead."""+ B.fx_bang(f, 41, 3)+ pal = {"#": INK_STROKE, "o": INK_SHINE}+ with f.offset(pose.dx, 0):+ for x, y in _SPLASH[pose.i]:+ f.glyph(x, y, _DROP, pal)+ f.glyph(47 - x - 3, y + 1, _DROP, pal)+ if pose.i == 0:+ f.paint(B.rect(22, 11, 25, 12), INK_STROKE)+ if pose.i >= 4:+ f.glyph(4, 42, _BLOT, pal)+ f.glyph(40, 43, ("###", ".#."), pal)+ if pose.i >= 5:+ f.glyph(39, 42, _BLOT, pal)+++def inky_sleeping(f, pose) -> None:+ B.fx_zzz(f, pose.i, 33, 15)+++def _paper(f, lines: int, partial: int) -> None:+ """The sheet of paper with ``lines`` full ink lines and one ``partial`` px long."""+ x, y = PAPER_AT+ sheet = B.rect(x, y, x + 11, y + 13) - {(x + 11, y), (x + 10, y), (x + 11, y + 1)}+ fill = {p: PAPER for p in sheet}+ for p in sheet:+ if p[0] == x + 11 or p[1] == y + 13:+ fill[p] = PAPER_DARK+ fill[(x + 10, y + 1)] = PAPER_DARK+ for r in range(4):+ for cx in range(x + 2, x + 10):+ fill[(cx, y + 3 + 3 * r)] = PAPER_RULE+ f.part(sheet, fill)+ scribble = (0, 1, 0, 0, 1, 1, 0, 1)+ for r in range(4):+ n = 7 if r < lines else partial if r == lines else 0+ for k in range(n):+ f.paint({(x + 2 + k, y + 2 + 3 * r + scribble[(k + r) % 8])}, INK_DEEP)+++#: Working: (lines done, partial length) per frame: one line takes four frames.+_WRITE = ((0, 1), (0, 3), (0, 5), (0, 7), (1, 1), (1, 3), (1, 5), (1, 7))+++def _writing_quill(f, lines: int, partial: int, i: int) -> None:+ x, y = PAPER_AT+ nx = x + 2 + max(0, partial - 1)+ ny = y + 2 + 3 * min(lines, 3) + (i % 2)+ draw_quill(f, nx, ny - 1, -12 + 6 * (i % 2), 8, 13, nib=True)+++def inky_working(f, pose) -> None:+ """The quill hops out of the bottle and scribbles line after line on a+ sheet of paper; little ticks fly off the nib."""+ lines, partial = _WRITE[pose.i]+ _paper(f, lines, partial)+ _writing_quill(f, lines, partial, pose.i)+ if pose.i % 2 == 0:+ x, y = PAPER_AT+ tx = x + 2 + partial+ f.paint({(tx + 1, y + 1 + 3 * lines)}, INK_STROKE)+++def inky_held(f, pose) -> None:+ """Dangling: ink drops fling out of the mouth as it swings."""+ B.fx_held(f, pose.i, (9, 25), (38, 25))+ with f.offset(pose.dx, pose.dy):+ drops = ((15, 10), (), (32, 9), (34, 5), (), (13, 6))[pose.i]+ if drops:+ f.glyph(drops[0], drops[1], ("##", "#o"), {"#": INK_STROKE, "o": INK_SHINE})+++# -- Idle acts -------------------------------------------------------------------++_ACT_QUILL: dict[str, tuple[tuple[float, float] | None, ...]] = {+ "quill_flourish": (+ (20, 22), (32, 26), (42, 30), (50, 34), (55, 36), (45, 30), (30, 26), (20, 22),+ ),+ "ink_slosh": (+ (20, 22), (35, 25), (20, 22), (5, 15), (20, 22), (30, 25), (20, 22), (20, 22),+ ),+ "blot_oops": (+ (20, 22), (5, 10), (45, 35), (30, 25), (25, 25), (70, 70), (75, 75), (20, 22),+ ),+ "signature": ((20, 22), None, None, None, None, None, None, (20, 22)),+ "feather_preen": (+ (20, 22), (-40, -40), (-70, -60), (-55, -50), (-70, -60), (-20, -10), (15, 18), (20, 22),+ ),+ "bubble_pop": (+ (20, 22), (20, 22), (30, 28), (40, 32), (45, 34), (45, 34), (30, 26), (20, 22),+ ),+}+_ACT_INK: dict[str, tuple[tuple[int, float], ...]] = {+ "ink_slosh": (+ (35, 0.0), (35, 0.3), (35, 0.0), (35, -0.3), (35, 0.0), (35, 0.2), (35, -0.1), (35, 0.0),+ ),+}+_ACT_BLUSH: dict[str, tuple[int, ...]] = {+ "blot_oops": (5, 6),+ "signature": (7,),+ "feather_preen": (6, 7),+ "bubble_pop": (7,),+}+++def _act_pose(name: str, **columns: Sequence) -> Callable:+ def pose(i: int):+ fields = {key: values[i] for key, values in columns.items()}+ return B.Pose("idle", i, act=name, **fields)++ return pose+++#: Quill flourish: the loop the quill's tip writes to the right of the head.+_LOOP = (+ (33, 6), (36, 3), (40, 2), (43, 4), (43, 7), (40, 9), (37, 8), (37, 5), (40, 3),+ (44, 2), (46, 4),+)+_LOOP_SHOWN = (0, 2, 4, 6, 8, 11, 11, 0)+_LOOP_FADE = (0, 0, 0, 0, 0, 3, 7, 0)+++def _flourish_fx(f, pose) -> None:+ """The quill leans out and writes a loop in the air, a ribbon of ink+ that dries from its tail; a sparkle crowns the finish."""+ _ribbon(f, _LOOP, _LOOP_SHOWN[pose.i], _LOOP_FADE[pose.i])+ if pose.i >= 5:+ B.fx_sparkles(f, pose.i - 5, ((42, 12, 0),))+++def _slosh_fx(f, pose) -> None:+ """Ticks on the side the ink sloshes against."""+ tilt = _ACT_INK["ink_slosh"][pose.i][1]+ with f.offset(pose.dx, 0):+ if tilt > 0.2:+ f.paint(B.line(39, 33, 41, 31) | B.line(39, 36, 42, 36), B.ARC)+ elif tilt < -0.2:+ f.paint(B.line(8, 33, 6, 31) | B.line(8, 36, 5, 36), B.ARC)+++#: Blot oops: the flying drop (x, y) per frame, then the blot on the floor.+_OOPS_DROP = (None, None, (36, 3), (40, 6), (42, 14), None, None, None)+++def _oops_fx(f, pose) -> None:+ """A flick of the quill sends a drop flying; it lands as a blot on the+ floor. Oops. A sheepish blush."""+ pal = {"#": INK_STROKE, "o": INK_SHINE}+ spot = _OOPS_DROP[pose.i]+ if spot is not None:+ f.glyph(spot[0], spot[1], (".#.", "###", "#o#", ".#."), pal)+ if pose.i == 4:+ f.paint(B.line(43, 20, 43, 30), INK_STROKE_FADE)+ if pose.i == 5:+ f.glyph(37, 41, ("#..#..#", ".#####.", "###o###", ".#.#.#."), pal)+ if pose.i >= 6:+ f.glyph(37, 43, (".####..", "###o###", ".#####."), pal)+ if pose.i in (5, 6):+ B.fx_sweat(f, 0, 35, 16)+++#: Signature: how many points of the name show per frame (None = no paper).+_SIGN_SHOWN = (None, 1, 4, 7, 10, 10, 10, None)+++def _signature_fx(f, pose) -> None:+ """Inky pulls the quill out and signs its name on a sheet with a loop,+ a dot and a proud underline."""+ i = pose.i+ shown = _SIGN_SHOWN[i]+ if shown is None:+ if i == 7:+ B.fx_sparkles(f, 1, ((41, 34, 0),))+ return+ x, y = PAPER_AT+ _paper(f, 0, 0)+ name = [(x + 1, y + 9), (x + 2, y + 6), (x + 3, y + 5), (x + 3, y + 8), (x + 4, y + 9),+ (x + 5, y + 7), (x + 6, y + 9), (x + 7, y + 7), (x + 8, y + 9), (x + 9, y + 6),+ (x + 10, y + 8)]+ pts = name[:shown]+ f.paint(B.polyline(pts) if len(pts) > 1 else set(pts), INK_DEEP)+ nx, ny = pts[-1]+ if i >= 5:+ f.paint({(x + 5, y + 4)}, INK_DEEP) # the dot+ f.paint(B.line(x + 1, y + 11, x + 3 + 2 * (i - 4), y + 11), INK_STROKE)+ nx, ny = (x + 5, y + 4) if i == 5 else (x + 3 + 2 * (i - 4), y + 11)+ draw_quill(f, nx, ny - 1, -12 + 6 * (i % 2), 8, 13, nib=True)+++def _preen_fx(f, pose) -> None:+ """The quill bends down and ruffles; a shake sends fluff flying, then it+ lies smooth and gleams."""+ if pose.i in (3, 4, 5):+ k = pose.i - 3+ for x, y in ((7 - k, 19 + 2 * k), (10 - k, 12 - 2 * k), (4 - k, 14 - k)):+ f.glyph(x, y, ("##",), {"#": QUILL})+ if pose.i == 6:+ bx, by = QUILL_BASE+ tx, ty = quill_tip(bx, by, 15, 18, 14)+ B.fx_sparkles(f, 1, ((tx + 3, ty + 2, 0),))+++#: Bubble pop: the bubble above the bottle, (x, y, size) per frame.+_POP_AIR = (None, None, None, (20, 10, 1), (17, 6, 2), (14, 2, 3), None, None)+++def _bubble_fx(f, pose) -> None:+ """An ink bubble rises through the ink, squeezes out of the mouth, floats+ up and pops."""+ spot = _POP_AIR[pose.i]+ pal = {"#": INK_TOP, "o": INK_SHINE, "w": GLASS_SHINE}+ if spot is not None:+ x, y, size = spot+ rows = {+ 1: ("##", "##"),+ 2: (".##.", "#wo#", "#oo#", ".##."),+ 3: (".###.", "#woo#", "#ooo#", "#ooo#", ".###."),+ }[size]+ f.glyph(x, y, rows, pal)+ if pose.i == 6:+ x, y = 14, 2+ f.paint({(x, y), (x + 4, y), (x, y + 4), (x + 4, y + 4)}, INK_STROKE)+ f.paint({(x + 2, y - 1), (x - 1, y + 2), (x + 5, y + 2), (x + 2, y + 5)}, INK_STROKE_FADE)+ B.fx_sparkles(f, 0, ((x + 7, y + 3, 0),), _INK_SPARK)+++def _acts():+ return (+ B.IdleAct(+ "quill_flourish", 8, 9,+ _act_pose(+ "quill_flourish",+ eyes=("open", "up_right", "up_right", "up_right", "up_right", "happy", "happy",+ "open"),+ mouth=("rest", "rest", "half", "open", "open", "smile", "smile", "rest"),+ stretch=(0, 1, 1, 1, 0, 0, 0, 0),+ ),+ _flourish_fx,+ ),+ B.IdleAct(+ "ink_slosh", 8, 8,+ _act_pose(+ "ink_slosh",+ dx=(0, -1, 0, 1, 0, -1, 0, 0),+ eyes=("open", "look_right", "open", "look_left", "open", "look_right", "happy",+ "open"),+ mouth=("rest", "half", "closed", "half", "closed", "half", "smile", "rest"),+ ),+ _slosh_fx,+ ),+ B.IdleAct(+ "blot_oops", 8, 8,+ _act_pose(+ "blot_oops",+ dy=(0, 0, -1, 0, 0, 0, 0, 0),+ eyes=("open", "up_right", "wide", "look_right", "look_right", "wide", "happy",+ "open"),+ mouth=("rest", "closed", "half", "o", "o", "frown", "closed", "rest"),+ ),+ _oops_fx,+ ),+ B.IdleAct(+ "signature", 8, 7,+ _act_pose(+ "signature",+ dx=(0, -2, -4, -4, -4, -4, -2, 0),+ eyes=("open", "look_right", "down", "down", "down", "look_right", "happy",+ "happy"),+ mouth=("rest", "rest", "closed", "closed", "closed", "smile", "smile", "smile"),+ stretch=(0, 0, 0, 0, 0, 1, 1, 0),+ ),+ _signature_fx,+ ),+ B.IdleAct(+ "feather_preen", 8, 8,+ _act_pose(+ "feather_preen",+ dx=(0, 0, 0, -1, 1, 0, 0, 0),+ eyes=("open", "look_left", "closed", "closed", "closed", "open", "happy",+ "open"),+ mouth=("rest", "closed", "smile", "closed", "closed", "rest", "smile", "rest"),+ ),+ _preen_fx,+ ),+ B.IdleAct(+ "bubble_pop", 8, 8,+ _act_pose(+ "bubble_pop",+ eyes=("down", "down", "open", "up_left", "up_left", "up_left", "wide", "happy"),+ mouth=("rest", "closed", "closed", "o", "o", "o", "open", "smile"),+ dy=(0, 0, 0, 0, 0, 0, -1, 0),+ ),+ _bubble_fx,+ ),+ )+++INKY_EYES = None+++def design(bp):+ global B, INKY_EYES+ B = bp+ INKY_EYES = bp.EyeStyle(w=3, h=4, iris=FACE, glint=bp.FX_WHITE, lid=FACE)+ return bp.PetDesign(+ id="inky",+ name="Inky",+ description="A little inkwell with a quill for a tuft that writes down every idea.",+ draw=draw_inky,+ pose=inky_pose,+ anchors=bp.Anchors(+ arcs_left=(9, 25),+ arcs_right=(38, 25),+ dots=(4, 8),+ zzz=(33, 15),+ bang=(41, 3),+ sweat=(35, 16),+ sparkles=((6, 14, 1), (41, 22, 2), (5, 36, 3)),+ laptop=(17, 34),+ magnifier=(24, 28),+ ),+ fx={+ "talking": inky_talking,+ "success": inky_success,+ "error": inky_error,+ "sleeping": inky_sleeping,+ "working": inky_working,+ "held": inky_held,+ },+ acts=_acts(),+ waist=38,+ )diff --git a/scripts/pets/designs/kiko.py b/scripts/pets/designs/kiko.pynew file mode 100644index 0000000000..df2d08e63a--- /dev/null+++ b/scripts/pets/designs/kiko.py@@ -0,0 +1,769 @@+"""Kiko: the axolotl.++A soft pink axolotl seen from the front: a wide flat head with a gentle,+ever-present smile, small dark eyes set far apart, three feathery gill fronds+on each side in a deeper magenta, stubby four-fingered limbs and a finned tail+curling out to the right. The fronds carry most of the acting: they fan out+to listen, a light pulse runs along them while Kiko thinks, they flare on a+happy hop and droop when something goes wrong.+"""++import math+from dataclasses import dataclass, replace+from typing import Any++#: The build_pets module, handed in by ``design``.+bp: Any = None+++def _hex(value: str) -> tuple[int, int, int, int]:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++SKIN = _hex("#ffb3cf")+SKIN_LIGHT = _hex("#ffdbe9")+SKIN_DARK = _hex("#e384aa")+BELLY = _hex("#ffe6f0")+GILL = _hex("#e0508f")+GILL_LIGHT = _hex("#ff8cc0")+GILL_DARK = _hex("#a83270")+#: The light pulse running along the fronds while Kiko thinks.+GILL_GLOW = _hex("#ffe3f2")+FIN = _hex("#ffd0e2")+INK = _hex("#2e1a2a")+CHEEK = _hex("#ff6f9f")+#: Pale cheeks: the blush drains when something goes wrong.+CHEEK_PALE = _hex("#f4e6ec")+BUBBLE = _hex("#9fd8ff")+BUBBLE_SHINE = _hex("#f2fbff")+HEART = _hex("#ff5c8a")++CX = 24+#: Left fronds' roots on the head (upper, middle, lower) and their resting+#: directions in degrees (screen coordinates, 180 = straight left).+_ROOTS = ((13, 19), (11, 23), (12, 28))+_ANGLES = (218.0, 186.0, 156.0)+_FROND_LEN = 7+++@dataclass(frozen=True)+class Look:+ """Everything a frame can change on Kiko besides the pose."""++ #: Fronds fan out (+1) or fold in (-1).+ spread: float = 0.0+ #: Fronds rotate towards the ground (1 = a full droop).+ droop: float = 0.0+ #: Per-frond extra angle in degrees (upper, middle, lower), each side;+ #: the right side's angles are mirrored, so +1 lifts a frond on both sides.+ wave_l: tuple[float, float, float] = (0.0, 0.0, 0.0)+ wave_r: tuple[float, float, float] = (0.0, 0.0, 0.0)+ #: Frond lengths, each side.+ len_l: tuple[int, int, int] = (_FROND_LEN,) * 3+ len_r: tuple[int, int, int] = (_FROND_LEN,) * 3+ #: Where the light pulse sits along the fronds (None = no glow).+ glow: int | None = None+ cheek: str = "pink"+ #: Tail tip lift in pixels (+ up).+ tail: int = 0+ #: Arm pose of each side: rest, up, type_up, type_down, dangle, hug.+ arm_l: str = "rest"+ arm_r: str = "rest"+ #: Head offset against the body (a tilt-like wiggle).+ head_dx: int = 0+ #: Turned away (the back of the head, no face).+ back: bool = False+ held: bool = False+++# ---------------------------------------------------------------------------+# Shapes.+# ---------------------------------------------------------------------------+++def _frond(root: tuple[int, int], angle: float, length: int) -> tuple[set, set, list]:+ """One feathery frond for the left side: (stalk, fringe, centre line).++ A plump stalk that tapers and curls up a little towards its tip, with+ short barbs on both sides slanting towards the tip, like a feather.+ """+ stalk: set = set()+ fringe: set = set()+ centre: list = []+ x, y = root[0] + 0.5, root[1] + 0.5+ a = math.radians(angle)+ curl = math.radians(2.5)+ for t in range(length + 1):+ c, s = round(math.cos(a), 6), round(math.sin(a), 6)+ p = (math.floor(x), math.floor(y))+ centre.append(p)+ r = 1.0 if t < length else 0.6+ stalk |= bp.ellipse(x, y, r, r)+ if t % 2 == 1:+ # A pair of barbs slanting towards the tip: a feathery plume.+ px, py = -s, c+ fringe.add((math.floor(x + px * 1.5 + c * 0.5), math.floor(y + py * 1.5 + s * 0.5)))+ fringe.add((math.floor(x - px * 1.5 + c * 0.5), math.floor(y - py * 1.5 + s * 0.5)))+ x, y = x + c, y + s+ # Curl the tip upwards: the left fronds turn clockwise, towards the sky.+ a += curl if math.sin(a) <= 0.3 else -curl+ return stalk, fringe - stalk, centre+++def _frond_angles(look: Look, wave: tuple[float, float, float]) -> list[float]:+ out = []+ for k, base in enumerate(_ANGLES):+ fan = (20.0, 0.0, -20.0)[k] * look.spread+ out.append(base + fan - 30.0 * look.droop + wave[k])+ return out+++def _draw_fronds(f, look: Look, hx: int) -> None:+ """Six fronds behind the head; the right side mirrors the left."""+ for side in (0, 1):+ wave = look.wave_l if side == 0 else look.wave_r+ lengths = look.len_l if side == 0 else look.len_r+ for k, angle in enumerate(_frond_angles(look, wave)):+ if lengths[k] <= 0:+ continue+ stalk, fringe, centre = _frond(_ROOTS[k], angle, lengths[k])+ # The pulse runs down the fronds one after the other.+ glow = None if look.glow is None else look.glow - 2 * k+ colors = {p: GILL for p in stalk}+ for t, p in enumerate(centre):+ if t <= 1:+ colors[p] = GILL_DARK+ for p in fringe:+ colors[p] = GILL_LIGHT+ if glow is not None:+ # A light pulse travelling from the root to the tip.+ for t, p in enumerate(centre):+ d = glow - t+ if 0 <= d <= 2:+ # A bright head with a fading trail behind it.+ tone = (GILL_GLOW, GILL_GLOW, GILL_LIGHT)[d]+ for q in bp.rect(p[0] - 1, p[1] - 1, p[0] + 1, p[1] + 1) & stalk:+ colors[q] = tone+ for p in fringe:+ if _near(p, centre, glow):+ colors[p] = GILL_GLOW+ if side == 1:+ colors = {(2 * CX - 1 - x, y): c for (x, y), c in colors.items()}+ colors = {(x + hx, y): c for (x, y), c in colors.items()}+ f.part(set(colors), colors)+++def _near(p, centre, glow: int) -> bool:+ if glow < 0 or glow >= len(centre):+ return False+ cx, cy = centre[glow]+ return abs(p[0] - cx) <= 1 and abs(p[1] - cy) <= 2 or abs(p[0] - cx) <= 2 and p[1] == cy+++def _head() -> set:+ """Wide and flat: a broad oval with a flattened crown."""+ return bp.ellipse(CX, 24.5, 13.5, 9.0) | bp.ellipse(CX, 22.5, 11.5, 7.5)+++def _body() -> set:+ return bp.ellipse(CX, 38.5, 9.0, 6.5)+++def _tail(lift: float) -> tuple[set, set]:+ """The tail curling out to the right, tip ``lift`` px up: (tail, fin) masks."""+ p0, p1, p2 = (29.0, 41.0), (38.0, 44.5), (43.0, 38.5 - lift)+ tail: set = set()+ fin: set = set()+ for n in range(25):+ t = n / 24+ x = (1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t * t * p2[0]+ y = (1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t * t * p2[1]+ r = 2.4 * (1 - t) + 0.7+ tail |= bp.ellipse(x, y, r, r)+ if 0.25 < t < 0.97:+ # A soft fin along the top of the tail, tallest in the middle.+ h = 1.0 + 1.2 * math.sin(math.pi * (t - 0.25) / 0.72)+ top = math.floor(y - r - h + 0.5)+ fin |= {(math.floor(x), yy) for yy in range(top, math.floor(y) + 1)}+ return tail, fin - tail+++_ARM_TIPS = {+ "rest": ((16, 37), (13, 41)),+ "up": ((16, 36), (11, 31)),+ "type_up": ((15, 37), (14, 40)),+ "type_down": ((15, 37), (16, 43)),+ "dangle": ((16, 37), (15, 43)),+ "hug": ((16, 37), (19, 40)),+}+++def _arm(mode: str) -> tuple[set, set]:+ """A left front limb: (arm, fingers). Four tiny fingers splay from the hand."""+ (sx, sy), (hx, hy) = _ARM_TIPS[mode]+ arm: set = set()+ for x, y in bp.line(sx, sy, hx, hy):+ arm |= bp.ellipse(x + 0.5, y + 0.5, 1.3, 1.3)+ hand = bp.ellipse(hx + 0.5, hy + 0.5, 1.6, 1.6)+ dx, dy = hx - sx, hy - sy+ norm = math.hypot(dx, dy) or 1.0+ ux, uy = dx / norm, dy / norm+ fingers: set = set()+ for spread in (-0.9, -0.3, 0.3, 0.9):+ ang = math.atan2(uy, ux) + spread+ fingers.add(+ (math.floor(hx + 0.5 + math.cos(ang) * 2.0), math.floor(hy + 0.5 + math.sin(ang) * 2.0))+ )+ return arm | hand, fingers - arm - hand+++def _leg() -> tuple[set, set]:+ """A left hind leg, a stub with its toes on the ground."""+ leg = bp.ellipse(17.0, 43.5, 2.2, 1.8)+ toes = {(15, 45), (16, 46)}+ return leg, toes - leg+++def _paint_limb(f, mask: set, fingers: set, mirror: bool, dx: int = 0) -> None:+ if mirror:+ mask, fingers = bp.mirrored(mask), bp.mirrored(fingers)+ mask, fingers = bp.shifted(mask, dx, 0), bp.shifted(fingers, dx, 0)+ whole = mask | fingers+ colors = bp.shade(mask, SKIN, light=SKIN_LIGHT, dark=SKIN_DARK, dark_depth=1)+ colors.update({p: SKIN_DARK for p in fingers})+ f.part(whole, colors)+++# ---------------------------------------------------------------------------+# Drawing.+# ---------------------------------------------------------------------------+++def _paint_kiko(f, pose, look: Look) -> None:+ hx = look.head_dx+ # Back to front: the fronds, the tail and hind legs, the body, the head.+ _draw_fronds(f, look, hx)+ tail, fin = _tail(look.tail)+ colors = bp.shade(tail, SKIN, light=SKIN_LIGHT, dark=SKIN_DARK, dark_depth=1)+ colors.update({p: FIN for p in fin})+ f.part(tail | fin, colors)+ if not look.held:+ for mirror in (False, True):+ leg, toes = _leg()+ _paint_limb(f, leg, toes, mirror)+ else:+ # Dangling: the hind feet hang down, toes pointing at the floor.+ for mirror in (False, True):+ leg = bp.ellipse(18.5, 45.5, 1.7, 2.4)+ _paint_limb(f, leg, {(17, 48), (19, 48)} - leg, mirror)+ body = _body()+ colors = bp.shade(body, SKIN, light=SKIN_LIGHT, dark=SKIN_DARK, dark_depth=2)+ belly = bp.ellipse(CX, 40.0, 5.0, 3.6) & bp.eroded(body, 1)+ colors.update({p: BELLY for p in belly})+ f.part(body, colors)+ head = bp.shifted(_head(), hx, 0)+ colors = bp.shade(head, SKIN, light=SKIN_LIGHT, dark=SKIN_DARK, dark_depth=2)+ f.part(head, colors)+ inner = bp.eroded(head, 1)+ shine = {(15 + hx, 18), (16 + hx, 17), (17 + hx, 17), (18 + hx, 17)}+ f.paint(shine & inner, bp.FX_WHITE)+ if look.back:+ # The back of the head: a darker crown stripe instead of a face.+ f.paint(bp.rect(19 + hx, 18, 28 + hx, 18) & inner, SKIN_DARK)+ else:+ _face(f, pose, look, hx)+ # Front limbs over the body (at work they reach round the laptop instead).+ if pose.state != "working":+ _paint_limb(f, *_arm(look.arm_l), False)+ _paint_limb(f, *_arm(look.arm_r), True)+++EYES = None # set in design (needs bp.EyeStyle)+++def _face(f, pose, look: Look, hx: int) -> None:+ bp.draw_eyes(f, pose.eyes, (15 + hx, 21), (30 + hx, 21), EYES)+ cheek = {+ "pink": CHEEK,+ "pale": CHEEK_PALE,+ }[look.cheek]+ cheeks = bp.rect(12 + hx, 26, 14 + hx, 26) | bp.rect(33 + hx, 26, 35 + hx, 26)+ f.paint(cheeks, cheek)+ _mouth(f, pose.mouth, hx)+++#: The wide axolotl grin, 10 px across, centred under the eyes.+_SMILE = ("#........#", ".##....##.", "...####...")+_SMILE_BIG = ("#........#", "#........#", ".##....##.", "...####...")+++def _mouth(f, style: str, hx: int) -> None:+ x = 19 + hx+ if style == "rest":+ f.paint(bp.from_rows(x, 26, _SMILE), INK)+ elif style == "smile":+ f.paint(bp.from_rows(x, 26, _SMILE), INK)+ f.paint(bp.rect(x + 3, 27, x + 6, 27), bp.TONGUE)+ elif style == "pucker":+ f.paint(bp.from_rows(x + 3, 26, (".##.", "#..#", ".##.")), INK)+ elif style == "closed":+ f.paint(bp.from_rows(x, 27, ("#........#", ".########.")), INK)+ elif style == "frown":+ f.paint(bp.from_rows(x + 2, 27, ("..##..", ".#..#.", "#....#")), INK)+ else:+ bp.draw_mouth(f, style, x + 2, 27, 6, color=INK)+ f.paint({(x, 26), (x + 9, 26), (x + 1, 27), (x + 8, 27)}, INK)+++def draw_kiko(f, pose) -> None:+ look = _look(pose)+ with f.offset(pose.dx, pose.dy):+ _paint_kiko(f, pose, look)+ if pose.state == "working":+ _laptop(f, pose)+++# ---------------------------------------------------------------------------+# Per-state looks and poses.+# ---------------------------------------------------------------------------++#: Idle: a slow ripple through the fronds and a lazy tail.+_IDLE_WAVE = (0, 3, 6, 6, 3, 0, -3)+_IDLE_TAIL = (0, 0, 1, 1, 1, 0, 0)+#: Working: which hand is down on the keys per frame.+_TAPS = "LRL.RLR."+_SLEEP_FLOAT = (0, 0, -1, -1, 0, 0)+#: Held: the tail wiggles up and down while Kiko dangles.+_HELD_TAIL = (5, 1, -2, 1, 5, 2)+++def kiko_pose(state: str, i: int):+ pose = bp.base_pose(state, i)+ if state == "idle" and pose.eyes != "closed":+ return replace(pose, mouth="rest")+ if state == "thinking":+ return replace(pose, mouth="rest")+ if state == "success":+ return replace(pose, mouth="smile" if i not in (2, 3, 4) else "open")+ if state == "sleeping":+ return replace(pose, dy=_SLEEP_FLOAT[i], mouth="rest")+ if state == "searching":+ return replace(pose, mouth="rest")+ if state == "held":+ return replace(pose, dy=-3, mouth="pucker" if i % 3 == 2 else "half")+ if state == "working":+ # No nod: the feet and the laptop stay on the ground.+ return replace(pose, dy=0, mouth="rest")+ return pose+++def _look(pose) -> Look:+ if pose.act:+ return _ACT_LOOKS[pose.act](pose.i)+ s, i = pose.state, pose.i+ if s == "idle":+ if i >= len(_IDLE_WAVE):+ return Look()+ w = _IDLE_WAVE[i]+ return Look(wave_l=(w, w * 0.6, w * 0.3), wave_r=(w, w * 0.6, w * 0.3), tail=_IDLE_TAIL[i])+ if s == "listening":+ fan = (0.6, 1.0, 1.3, 1.3, 1.0, 0.8)[i % 6]+ return Look(spread=fan, wave_l=(4, 0, 0), wave_r=(4, 0, 0), tail=1)+ if s == "thinking":+ return Look(glow=i + 1, spread=0.2, tail=i % 4 // 2)+ if s == "talking":+ return Look(spread=(0.0, 0.3, 0.6, 1.0)[i], tail=i // 2)+ if s == "success":+ up = i in (1, 2, 3, 4, 5)+ return Look(+ spread=1.6 if up else 0.8,+ wave_l=(8, 6, 4) if up else (0, 0, 0),+ wave_r=(8, 6, 4) if up else (0, 0, 0),+ arm_l="up" if up else "rest",+ arm_r="up" if up else "rest",+ tail=3 if up else 1,+ )+ if s == "error":+ return Look(droop=(0.7, 0.9, 1.0, 1.0, 1.1, 1.1)[i], spread=-0.5, cheek="pale", tail=-2)+ if s == "sleeping":+ return Look(droop=0.5, spread=-0.3, tail=-1, wave_l=(0, 0, _SLEEP_FLOAT[i] * 4))+ if s == "working":+ tap = _TAPS[i % len(_TAPS)]+ return Look(+ arm_l="type_down" if tap == "L" else "type_up",+ arm_r="type_down" if tap == "R" else "type_up",+ tail=i % 2,+ )+ if s == "searching":+ return Look(spread=0.5, arm_r="hug")+ if s == "held":+ return Look(+ held=True,+ droop=-0.4,+ spread=1.0,+ arm_l="dangle",+ arm_r="dangle",+ wave_l=(bp.HELD_SWAY[i] * 6,) * 3,+ wave_r=(-bp.HELD_SWAY[i] * 6,) * 3,+ tail=_HELD_TAIL[i % len(_HELD_TAIL)],+ )+ return Look()+++def _laptop(f, pose) -> None:+ """A laptop in front: the lid hides the belly, the little hands reach round+ its sides and tap the keyboard, one key lighting up under each tap."""+ bp.fx_laptop(f, pose.i, 17, 36)+ tap = _TAPS[pose.i % len(_TAPS)]+ for side, mirror in (("L", False), ("R", True)):+ down = tap == side+ arm, fingers = _arm("type_down" if down else "type_up")+ _paint_limb(f, arm, fingers, mirror)+ if down:+ f.paint({(19, 44) if side == "L" else (28, 44)}, bp.FX_WHITE)+++# ---------------------------------------------------------------------------+# Effects.+# ---------------------------------------------------------------------------+++def _bubble(f, cx: int, cy: int, size: int) -> None:+ """A see-through bubble: a dot, a pair, or a ring with a shine."""+ if size <= 0:+ f.part({(cx, cy)}, BUBBLE)+ return+ if size == 1:+ dot = bp.rect(cx, cy, cx + 1, cy + 1)+ f.part(dot, BUBBLE)+ f.paint({(cx, cy)}, BUBBLE_SHINE)+ return+ disc = bp.ellipse(cx + 0.5, cy + 0.5, size, size)+ ring = disc - bp.eroded(disc, 1)+ f.paint(bp.outer_ring(disc), bp.OUTLINE)+ f.paint(ring, BUBBLE)+ top = min(y for _, y in ring)+ f.paint({p for p in ring if p[1] <= top + 1 and p[0] < cx}, BUBBLE_SHINE)+++def kiko_success(f, pose) -> None:+ """Bubbles stream up from both sides of the hop, sparkles at the top."""+ i = pose.i+ for x, start, size in ((6, 0, 2), (40, 1, 1), (9, 3, 1), (38, 4, 2)):+ age = i - start+ if 0 <= age <= 4:+ _bubble(f, x + (age % 2), 40 - 6 * age, size if age else 0)+ bp.fx_sparkles(f, i, ((CX, 5, 2), (5, 20, 3), (42, 18, 4)))+++def kiko_sleeping(f, pose) -> None:+ """Slow bubbles rising from the smile, one at a time, and a sleepy z."""+ i, dy = pose.i, pose.dy+ for start in (0, 3):+ age = (i - start) % 6+ if age <= 4:+ _bubble(f, 31 + (age % 2), 24 + dy - 4 * age, min(2, age))+ bp.fx_zzz(f, i, 36, 15)+++def kiko_working(f, pose) -> None:+ """The laptop is part of the body drawing (the hands sit on its keys)."""+++def kiko_held(f, pose) -> None:+ """Swing streaks on the trailing side and two startled marks."""+ sway = bp.HELD_SWAY[pose.i % len(bp.HELD_SWAY)]+ x = pose.dx+ if sway < 0:+ f.paint(bp.line(41 + x, 34, 41 + x, 39) | bp.line(43 + x, 35, 43 + x, 38), bp.ARC_FADE)+ elif sway > 0:+ f.paint(bp.line(6 + x, 34, 6 + x, 39) | bp.line(4 + x, 35, 4 + x, 38), bp.ARC_FADE)+ if pose.i % 3 != 2:+ f.paint(bp.line(17 + x, 10, 15 + x, 8) | bp.line(30 + x, 10, 32 + x, 8), bp.ARC)+++# ---------------------------------------------------------------------------+# Idle acts.+# ---------------------------------------------------------------------------+++def _act_pose(name: str, rows):+ """An act's pose function from (dx, dy, eyes, mouth) rows."""++ def pose(i: int):+ dx, dy, eyes, mouth = rows[i]+ return bp.Pose("idle", i, dx=dx, dy=dy, eyes=eyes, mouth=mouth, act=name)++ return pose+++# bubble_blow: a breath, a pucker, a bubble swells in front of Kiko's chest,+# floats up over the head and pops; Kiko watches it all the way.+_BLOW = (+ (0, 0, "open", "rest"),+ (0, 0, "closed", "closed"),+ (0, 0, "down", "pucker"),+ (0, 0, "down", "pucker"),+ (0, 0, "down", "pucker"),+ (0, 0, "up_right", "rest"),+ (0, 0, "wide", "open"),+ (0, 0, "happy", "smile"),+)+#: The bubble per frame: (centre x, centre y, radius); radius 0 = no bubble.+_BLOW_BUBBLE = (+ (0.0, 0.0, 0.0),+ (0.0, 0.0, 0.0),+ (24.0, 31.5, 1.5),+ (24.0, 33.0, 2.6),+ (24.0, 34.0, 3.6),+ (26.0, 9.0, 3.6),+ (0.0, 0.0, 0.0),+ (0.0, 0.0, 0.0),+)+_POP = (26.0, 8.0)+++def _blow_look(i: int) -> Look:+ return Look(spread=0.8 if i == 6 else 0.0, wave_l=(4, 2, 0) if i == 7 else (0, 0, 0))+++def act_bubble_blow(f, pose) -> None:+ i = pose.i+ cx, cy, r = _BLOW_BUBBLE[i]+ if r:+ disc = bp.ellipse(cx, cy, r, r)+ ring = disc - bp.eroded(disc, 1)+ f.paint(bp.outer_ring(disc), bp.OUTLINE)+ f.paint(ring, BUBBLE)+ top = min(y for _, y in ring)+ f.paint({p for p in ring if p[1] <= top + 1 and p[0] < cx - 1}, BUBBLE_SHINE)+ return+ if i == 6:+ px, py = _POP+ for k in range(8):+ ang = 2.0 * math.pi * k / 8+ c, s = round(math.cos(ang), 6), round(math.sin(ang), 6)+ a = (math.floor(px + c * 3.5), math.floor(py + s * 3.5))+ b = (math.floor(px + c * 5.5), math.floor(py + s * 5.5))+ f.paint(bp.line(*a, *b), bp.ARC if k % 2 else bp.ARC_FADE)+ if i == 7:+ for x, y, size in ((31, 6, 1), (19, 4, 0), (36, 10, 0)):+ _bubble(f, x, y, size)+++# frond_wave: a ripple runs down the left fronds, then the right ones.+_Z3 = (0, 0, 0)+_WAVE_L = (_Z3, (20, 6, 0), (10, 20, 6), (0, 10, 20), (0, 0, 10), _Z3, _Z3, _Z3)+_WAVE_R = (_Z3, _Z3, _Z3, _Z3, (20, 6, 0), (10, 20, 6), (0, 10, 20), _Z3)+_WAVE_FACE = (+ (0, 0, "open", "rest"),+ (0, 0, "look_left", "rest"),+ (0, 0, "look_left", "smile"),+ (0, 0, "look_left", "smile"),+ (0, 0, "look_right", "smile"),+ (0, 0, "look_right", "smile"),+ (0, 0, "look_right", "rest"),+ (0, 0, "happy", "smile"),+)+++def _wave_look(i: int) -> Look:+ return Look(wave_l=_WAVE_L[i], wave_r=_WAVE_R[i])+++def act_frond_wave(f, pose) -> None:+ i = pose.i+ if i in (2, 3):+ f.paint({(2, 14 + i), (3, 13 + i)}, bp.ARC_FADE)+ if i in (5, 6):+ f.paint({(45, 14 + i - 3), (44, 13 + i - 3)}, bp.ARC_FADE)+ if i == 7:+ bp.fx_sparkles(f, 1, ((CX, 8, 0),))+++# tail_swish: the tail flicks up and down; little water swirls follow it.+_SWISH_TAIL = (0, 4, 8, 3, -3, 6, 2, 0)+_SWISH_FACE = (+ (0, 0, "open", "rest"),+ (0, 0, "look_right", "rest"),+ (0, 0, "look_right", "smile"),+ (0, 0, "look_right", "smile"),+ (0, 0, "happy", "smile"),+ (0, 0, "look_right", "smile"),+ (0, 0, "happy", "smile"),+ (0, 0, "open", "rest"),+)+++def _swish_look(i: int) -> Look:+ return Look(tail=_SWISH_TAIL[i], wave_r=(0, 2 * (i % 2), 4 * (i % 2)))+++def act_tail_swish(f, pose) -> None:+ i = pose.i+ tip_y = 38 - _SWISH_TAIL[i]+ prev = _SWISH_TAIL[i - 1] if i else 0+ if _SWISH_TAIL[i] != prev:+ direction = 1 if _SWISH_TAIL[i] < prev else -1+ f.paint(bp.line(45, tip_y - 2 * direction, 45, tip_y - 4 * direction), bp.ARC_FADE)+ if i in (3, 4, 5):+ _bubble(f, 44, 30 - 3 * (i - 3), 0 if i == 3 else 1)+++# smile_wiggle: the head bobs side to side with a widening grin, hearts float up.+_WIGGLE_DX = (0, -1, 0, 1, 0, -1, 1, 0)+_WIGGLE_FACE = (+ (0, 0, "open", "rest"),+ (0, 0, "happy", "smile"),+ (0, -1, "happy", "smile"),+ (0, 0, "happy", "smile"),+ (0, -1, "happy", "smile"),+ (0, 0, "happy", "smile"),+ (0, -1, "happy", "smile"),+ (0, 0, "open", "smile"),+)+++def _wiggle_look(i: int) -> Look:+ d = _WIGGLE_DX[i]+ return Look(+ head_dx=d, wave_l=(-4 * d, -3 * d, -2 * d), wave_r=(4 * d, 3 * d, 2 * d), tail=i % 2+ )+++_HEART = ("#.#", "###", ".#.")+++def act_smile_wiggle(f, pose) -> None:+ hearts = {2: ((40, 14),), 3: ((41, 10), (5, 16)), 4: ((42, 6), (4, 12)), 5: ((3, 8),)}+ for x, y in hearts.get(pose.i, ()):+ f.glyph(x, y, _HEART, {"#": HEART})+++# regenerate_sparkle: a short frond sparkles and grows back to full length.+_REGEN_LEN = (3, 3, 4, 5, 6, 7, 7, 7)+_REGEN_FACE = (+ (0, 0, "up_left", "rest"),+ (0, 0, "up_left", "closed"),+ (0, 0, "closed", "closed"),+ (0, 0, "closed", "closed"),+ (0, 0, "up_left", "rest"),+ (0, 0, "wide", "open"),+ (0, 0, "happy", "smile"),+ (0, 0, "open", "rest"),+)+++def _regen_look(i: int) -> Look:+ n = _REGEN_LEN[i]+ return Look(len_l=(n, _FROND_LEN, _FROND_LEN), wave_l=(6, 0, 0) if i in (5, 6) else (0, 0, 0))+++def act_regenerate_sparkle(f, pose) -> None:+ i = pose.i+ a = math.radians(_ANGLES[0])+ n = _REGEN_LEN[i]+ tip = (+ math.floor(_ROOTS[0][0] + 0.5 + math.cos(a) * (n + 2)),+ math.floor(_ROOTS[0][1] + 0.5 + math.sin(a) * (n + 2)),+ )+ if 1 <= i <= 5:+ bp.fx_sparkles(f, (i - 1) % 3 + 1, ((tip[0], tip[1], 0),))+ if i == 6:+ bp.fx_sparkles(f, 1, ((6, 26, 0), (CX, 6, 0)))+ if i == 7:+ bp.fx_sparkles(f, 3, ((6, 26, 0), (CX, 6, 0)))+++# float_spin: Kiko floats up, turns a full circle and drifts back down.+_SPIN = (1.0, 1.0, 0.6, 0.2, 0.6, 1.0, 1.0, 1.0)+_SPIN_FACE = (+ (0, 0, "open", "rest"),+ (0, -2, "happy", "smile"),+ (0, -4, "happy", "smile"),+ (0, -5, "happy", "smile"),+ (0, -5, "happy", "smile"),+ (0, -4, "happy", "smile"),+ (0, -2, "open", "smile"),+ (0, 0, "happy", "rest"),+)+++def _spin_look(i: int) -> Look:+ return Look(back=i == 4, spread=0.6 if 1 <= i <= 6 else 0.0, tail=2 if 1 <= i <= 6 else 0)+++def act_float_spin(f, pose) -> None:+ i = pose.i+ if 2 <= i <= 4:+ f.paint(bp.line(8, 30, 12, 30) | bp.line(35, 30, 39, 30), bp.ARC_FADE)+ for x, start in ((10, 1), (36, 2), (22, 4)):+ age = i - start+ if 0 <= age <= 3:+ _bubble(f, x, 44 - 5 * age, min(2, age))+++def kiko_post(img, pose):+ if pose.act == "float_spin":+ return bp.squash_x(img, _SPIN[pose.i])+ return img+++_ACT_LOOKS = {+ "bubble_blow": _blow_look,+ "frond_wave": _wave_look,+ "tail_swish": _swish_look,+ "smile_wiggle": _wiggle_look,+ "regenerate_sparkle": _regen_look,+ "float_spin": _spin_look,+}+++def design(bp_module):+ global bp, EYES+ bp = bp_module+ EYES = bp.EyeStyle(w=3, h=4, iris=INK, glint=bp.FX_WHITE, lid=INK)+ acts = (+ bp.IdleAct("bubble_blow", 8, 8, _act_pose("bubble_blow", _BLOW), act_bubble_blow),+ bp.IdleAct("frond_wave", 8, 8, _act_pose("frond_wave", _WAVE_FACE), act_frond_wave),+ bp.IdleAct("tail_swish", 8, 9, _act_pose("tail_swish", _SWISH_FACE), act_tail_swish),+ bp.IdleAct("smile_wiggle", 8, 8, _act_pose("smile_wiggle", _WIGGLE_FACE), act_smile_wiggle),+ bp.IdleAct(+ "regenerate_sparkle",+ 8,+ 7,+ _act_pose("regenerate_sparkle", _REGEN_FACE),+ act_regenerate_sparkle,+ ),+ bp.IdleAct("float_spin", 8, 9, _act_pose("float_spin", _SPIN_FACE), act_float_spin),+ )+ return bp.PetDesign(+ id="kiko",+ name="Kiko",+ description=(+ "A dreamy pink axolotl whose gill fronds fan out to listen and glow as it thinks."+ ),+ draw=draw_kiko,+ anchors=bp.Anchors(+ arcs_left=(8, 11),+ arcs_right=(39, 11),+ dots=(19, 6),+ zzz=(36, 15),+ bang=(40, 3),+ sweat=(36, 13),+ sparkles=((CX, 5, 2), (5, 20, 3), (42, 18, 4)),+ laptop=(17, 36),+ magnifier=(CX, 38),+ ),+ pose=kiko_pose,+ waist=44,+ post=kiko_post,+ fx={+ "success": kiko_success,+ "sleeping": kiko_sleeping,+ "working": kiko_working,+ "held": kiko_held,+ },+ acts=acts,+ )diff --git a/scripts/pets/designs/nox.py b/scripts/pets/designs/nox.pynew file mode 100644index 0000000000..75387a4f8d--- /dev/null+++ b/scripts/pets/designs/nox.py@@ -0,0 +1,728 @@+"""Nox: the little owl.++A round, plump owl in soft brown-grey: one egg-shaped body with the head+on top, small ear tufts, a pale heart-shaped facial disc with two BIG amber+eyes, a tiny hooked beak, a speckled chest, folded wings and small talons.+Wise, calm and a bookworm: it turns and tilts its head to listen, puts a wing+to its chin while it thinks, puts on round reading glasses to work, sweeps a+magnifier while it searches and sinks into its feathers to sleep.++The head (disc, eyes, beak, tufts) is painted into its own frame and laid+over the body, sheared column by column for a tilt and moved for a sink, so+the face stays crisp. A head turn slides the face inside the head's round,+which also gives the famous owl head spin.+"""++import math+from dataclasses import dataclass, replace+++def design(bp): # noqa: C901, PLR0915 - one closure per pet keeps the module self-contained+ hexc = bp.hexc+ ellipse, rect, line, polygon = bp.ellipse, bp.rect, bp.line, bp.polygon+ mirrored, shifted, from_rows = bp.mirrored, bp.shifted, bp.from_rows+ outer_ring, eroded, shade = bp.outer_ring, bp.eroded, bp.shade+ Frame, Pose, base_pose = bp.Frame, bp.Pose, bp.base_pose+ OUTLINE, FX_WHITE, FX_DIM, CX, CELL = bp.OUTLINE, bp.FX_WHITE, bp.FX_DIM, bp.CX, bp.CELL++ # -- Palette ---------------------------------------------------------------++ base = hexc("#8c7d70")+ light = hexc("#b3a597")+ shine = hexc("#cfc3b6")+ dark = hexc("#66584c")+ wing = hexc("#796a5d")+ wing_light = hexc("#a39282")+ wing_dark = hexc("#54483d")+ wing_bar = hexc("#c9b9a6")+ disc = hexc("#f1e7d7")+ disc_rim = hexc("#c8b399")+ lid = hexc("#d9c9b1")+ chest = hexc("#e3d7c4")+ chest_shade = hexc("#cdbfa9")+ speck = hexc("#7d6a59")+ amber = hexc("#f7a928")+ amber_light = hexc("#ffd66e")+ amber_dark = hexc("#cf7019")+ pupil = hexc("#1a1216")+ beak = hexc("#e9b54f")+ beak_light = hexc("#ffe39a")+ beak_dark = hexc("#a8742e")+ talon = hexc("#e2a84a")+ talon_dark = hexc("#9a6a2a")+ blush = hexc("#f2a0a8")+ glasses = hexc("#3a3146")+ glasses_shine = hexc("#cfe6ff")+ book = hexc("#4f7fd1")+ book_light = hexc("#7fa6ea")+ book_dark = hexc("#33589e")+ page = hexc("#f7f1e3")+ page_line = hexc("#b9ae9b")+ ribbon = hexc("#e5383b")+ note = bp.ARC+ note_fade = bp.ARC_FADE++ # -- Small helpers ---------------------------------------------------------++ def inside(mask, margin=1):+ """``mask`` clipped so it AND its outline stay in the cell."""+ return {+ (x, y) for x, y in mask if margin <= x < CELL - margin and margin <= y < CELL - margin+ }++ def feathers(f, mask, depth=2):+ f.part(mask, shade(mask, base, light=light, dark=dark, dark_depth=depth))++ def wing_fill(mask):+ colors = shade(mask, wing, light=wing_light, dark=wing_dark, dark_depth=1)+ return colors++ # -- Geometry --------------------------------------------------------------++ head_c = (CX, 17.5)+ head_r = (12.5, 10.0)+ body_mask = ellipse(CX, 33.5, 12.5, 11.0)++ #: Ear tufts (left; the right one is mirrored): rest, perked, flattened.+ tufts = {+ "rest": [(12.0, 13.5), (12.0, 4.5), (14.5, 6.5), (19.0, 9.5)],+ "perk": [(12.0, 13.5), (11.5, 3.0), (14.5, 5.5), (19.0, 9.5)],+ "flat": [(12.0, 14.0), (7.5, 9.5), (11.0, 9.0), (18.0, 10.0)],+ "droop": [(12.5, 13.5), (10.5, 7.5), (14.0, 8.0), (18.5, 10.0)],+ "flick": [(12.0, 13.5), (9.5, 4.0), (13.0, 6.0), (19.0, 9.5)],+ }++ #: Top-left of each eye box at the neutral face.+ eye_at = ((16, 13), (25, 13))++ @dataclass(frozen=True)+ class Look:+ """How Nox holds itself beyond the pose's eyes and mouth."""++ #: The face slides this far sideways inside the head (a head turn).+ turn: int = 0+ #: The head seen from behind (mid head spin).+ back: bool = False+ #: Column shear of the head (positive drops the right side).+ tilt: float = 0.0+ #: Head offset from the body (a sink, a lift).+ head: tuple[int, int] = (0, 0)+ tufts: tuple[str, str] = ("rest", "rest")+ #: Wing styles, left and right (the right one is mirrored).+ wings: tuple[str, str] = ("fold", "fold")+ #: Round reading glasses on the beak.+ glasses: bool = False+ #: How ruffled the feathers are (0 = smooth).+ ruffle: int = 0+ #: How puffed up the whole body is (0 = normal).+ puff: float = 0.0+ blush: bool = True+ #: Dangling talons (held up by the mouse).+ dangle: bool = False++ # -- Eyes ------------------------------------------------------------------++ eye_disc = ellipse(3.5, 3.5, 3.6, 3.6)++ spiral = ("#####", "....#", "###.#", "#...#", "#####")++ def spin_rows(k):+ rows = [list(r) for r in spiral]+ for _ in range(k % 4):+ rows = [list(r) for r in zip(*rows[::-1], strict=True)]+ return tuple("".join(r) for r in rows)++ #: Closed-eye strokes on the 7 x 7 box (left eye; the right is mirrored).+ eye_lines = {+ "closed": (".......", ".......", ".......", "#.....#", ".#...#.", "..###..", "......."),+ "sleep": (".......", ".......", ".......", "#.....#", ".##.##.", "..###..", "......."),+ "happy": (".......", "..###..", ".#...#.", "#.....#", ".......", ".......", "......."),+ "x": (".......", ".#...#.", "..#.#..", "...#...", "..#.#..", ".#...#.", "......."),+ }++ def draw_eye(f, style, x, y, right, frame_i=0):+ mask = shifted(eye_disc, x, y)+ if style in eye_lines:+ rows = eye_lines[style]+ if right:+ rows = tuple(r[::-1] for r in rows)+ if style == "x":+ f.paint(mask, lid)+ f.paint(from_rows(x, y, rows), OUTLINE)+ return+ f.paint(outer_ring(mask), OUTLINE)+ rim = mask - eroded(mask)+ colors = {p: amber for p in mask}+ for p in rim:+ if p[1] >= y + 4 or (p[0] >= x + 5 and p[1] >= y + 2):+ colors[p] = amber_dark+ for p in mask:+ if p not in rim and p[1] == y + 1 and p[0] <= x + 4:+ colors[p] = amber_light+ f.paint_map(colors)+ if style == "spin":+ f.paint(from_rows(x + 1, y + 1, spin_rows(frame_i)), pupil)+ return+ big = style in ("wide", "heavy_wide")+ offs = {+ "open": (0, 0),+ "wide": (0, 0),+ "look_left": (-1, 0),+ "look_right": (1, 0),+ "up_left": (-1, -1),+ "up_right": (1, -1),+ "down": (0, 1),+ "read": (0, 1),+ "half": (0, 1),+ "heavy": (0, 1),+ "squint": (0, 1),+ }+ ox, oy = offs.get(style, (0, 0))+ if big:+ pup = rect(x + 1, y + 1, x + 5, y + 5) - {+ (x + 1, y + 1), (x + 5, y + 1), (x + 1, y + 5), (x + 5, y + 5)+ }+ else:+ pup = rect(x + 2, y + 2, x + 4, y + 4)+ pup = shifted(pup, ox, oy)+ f.paint(pup & mask, pupil)+ gx, gy = (x + 4 + ox, y + 2 + oy)+ f.paint({(gx, gy)} & mask, FX_WHITE)+ if big:+ f.paint({(gx, gy - 1), (gx - 1, gy - 1), (x + 2, y + 4)} & mask, FX_WHITE)+ # Lids: a feathered lid comes down over the top of the eye.+ if style == "squint":+ # Squeezed nearly shut: a feathered lid with a thin amber slit.+ f.paint(mask, lid)+ f.paint(line(x, y + 3, x + 6, y + 3), OUTLINE)+ f.paint(line(x + 2, y + 4, x + 4, y + 4), amber)+ f.paint({(x + 1, y + 2), (x + 5, y + 2)} if not right else {(x + 5, y + 2)}, OUTLINE)+ return+ cover = {"down": 1, "heavy": 2, "half": 3}.get(style, 0)+ if cover:+ lid_px = {p for p in mask if p[1] < y + cover}+ f.paint(lid_px, lid)+ edge = {(px, py) for px, py in mask if py == y + cover} | {+ (px, py) for px, py in outer_ring(mask) if py == y + cover+ }+ f.paint(edge, OUTLINE)++ def draw_eyes(f, style, turn, frame_i=0):+ left, _, right = style.partition("/")+ right = right or left+ for side, (x, y) in enumerate(eye_at):+ name = right if side else left+ if name == "spin" and side:+ draw_eye(f, name, x + turn, y, True, frame_i + 2)+ else:+ draw_eye(f, name, x + turn, y, bool(side), frame_i)++ # -- Beak ------------------------------------------------------------------++ def draw_beak(f, mouth, tx):+ """The hooked beak; talking drops the lower half below it."""+ x = 22 + tx+ upper = from_rows(x, 19, (".##.", "####", ".##."))+ gap = {"half": 1, "open": 2, "wide": 3, "smile": 1, "hoot": 2}.get(mouth, 0)+ if gap == 0:+ tip = {(x + 1, 22), (x + 2, 22)}+ f.part(upper | tip, beak)+ f.paint({(x + 1, 19)}, beak_light)+ f.paint({(x, 20)}, beak_light)+ f.paint({(x + 2, 22), (x + 3, 20)}, beak_dark)+ return+ inner = rect(x + 1, 22, x + 2, 21 + gap)+ lower = {(x + 1, 22 + gap), (x + 2, 22 + gap)}+ if mouth == "wide":+ inner |= {(x, 23), (x + 3, 23)}+ f.part(upper | inner | lower, beak)+ f.paint({(x + 1, 19), (x, 20)}, beak_light)+ f.paint({(x + 3, 20)}, beak_dark)+ f.paint(inner, bp.MOUTH_DARK)+ if gap >= 3:+ f.paint({(x + 1, 21 + gap), (x + 2, 21 + gap)}, bp.TONGUE)+ f.paint(lower, beak_dark)++ # -- Head ------------------------------------------------------------------++ def head_mask(look):+ head = ellipse(*head_c, *head_r)+ left = polygon(tufts[look.tufts[0]])+ right = mirrored(polygon(tufts[look.tufts[1]]))+ return head, left, right++ def disc_mask(tx):+ lobe = ellipse(19.5 + tx, 17.5, 6.4, 6.6)+ lobe_r = ellipse(28.5 + tx, 17.5, 6.4, 6.6)+ chin = polygon([(16.0 + tx, 20.0), (32.0 + tx, 20.0), (CX + tx, 27.5)])+ return lobe | lobe_r | chin++ def draw_head(f, pose, look):+ """The head at the neutral spot, onto ``f`` (a sub-frame)."""+ head, tuft_l, tuft_r = head_mask(look)+ whole = head | tuft_l | tuft_r+ colors = shade(whole, base, light=light, dark=dark, dark_depth=1)+ for p in tuft_l | tuft_r:+ if p not in head:+ colors[p] = dark if colors[p] == base else colors[p]+ # The tuft tips catch the light.+ for tuft in (tuft_l, tuft_r):+ top = min(y for _x, y in tuft)+ for p in tuft:+ if p[1] <= top + 1 and p not in head:+ colors[p] = light+ f.paint(outer_ring(whole), OUTLINE)+ f.paint_map(colors)+ f.paint({(20, 8), (21, 8), (22, 8), (19, 9), (26, 8), (27, 8)} & head, shine)+ face = eroded(head)+ if look.back:+ # The back of the head: speckled feathers and a darker nape.+ for k, (x, y) in enumerate(((16, 12), (22, 11), (28, 12), (19, 15), (25, 15),+ (31, 16), (16, 19), (22, 19), (28, 19), (19, 23),+ (25, 23))):+ f.paint({(x, y), (x + 1, y + 1), (x + 2, y)} & face, light if k % 2 else dark)+ f.paint({(x, y) for x, y in face if y >= 26}, dark)+ return+ tx = look.turn+ dm = disc_mask(tx) & face+ dcol = {p: disc for p in dm}+ for p in dm - eroded(dm):+ dcol[p] = disc_rim+ f.paint_map(dcol)+ if look.blush and abs(tx) <= 2:+ f.paint({(15 + tx, 22), (16 + tx, 22), (31 + tx, 22), (32 + tx, 22)} & dm, blush)+ sub = Frame()+ draw_eyes(sub, pose.eyes, tx, pose.i)+ if look.glasses:+ draw_glasses(sub, tx)+ draw_beak(sub, pose.mouth, tx)+ f.paint_map({p: c for p, c in sub.px.items() if p in head})++ def draw_glasses(f, tx):+ """Small round reading glasses perched on the beak."""+ for x, y in eye_at:+ x += tx+ lens = ellipse(x + 3.5, y + 3.5, 4.4, 4.4)+ rim = {(px, py) for px, py in outer_ring(lens) if py >= y + 4}+ f.paint(rim | line(x - 1, y + 4, x + 7, y + 4), glasses)+ # The lens: the lower half of the eye a shade paler, with a glint.+ f.paint({(x + 1, y + 6), (x + 2, y + 6)}, glasses_shine)++ def lay(f, sub, k, offset=(0, 0)):+ """Lay ``sub`` onto ``f`` sheared by ``k`` around the centre."""+ ox, oy = offset+ out = {}+ for (x, y), color in sub.px.items():+ ny = y + oy + math.floor((x + 0.5 - CX) * k + 0.5)+ out[(x + ox, ny)] = color+ f.paint_map(out)++ # -- Wings -----------------------------------------------------------------++ def bezier(root, ctrl, tip, r0, r1, steps=8):+ mask = set()+ for k in range(steps + 1):+ t = k / steps+ x = (1 - t) ** 2 * root[0] + 2 * (1 - t) * t * ctrl[0] + t * t * tip[0]+ y = (1 - t) ** 2 * root[1] + 2 * (1 - t) * t * ctrl[1] + t * t * tip[1]+ r = r0 + (r1 - r0) * t+ mask |= ellipse(x, y, r, r)+ return mask++ #: Wing shapes for the LEFT wing: (root, ctrl, tip, root radius, tip radius),+ #: and whether it sits behind the body.+ wing_shapes = {+ "fold": (((14.0, 27.5), (11.0, 33.0), (15.0, 41.5), 4.0, 1.6), False),+ "half": (((15.0, 28.0), (9.0, 27.0), (5.0, 33.0), 3.8, 1.6), True),+ "spread": (((15.0, 28.0), (8.0, 22.0), (3.5, 18.5), 3.8, 1.8), True),+ "up": (((15.0, 27.0), (10.0, 18.0), (7.0, 10.5), 3.8, 1.8), True),+ "down": (((15.0, 28.5), (10.0, 33.0), (7.5, 40.0), 3.8, 1.6), True),+ "chin": (((14.0, 29.0), (16.0, 33.5), (21.5, 26.0), 3.6, 2.2), False),+ "type_up": (((14.0, 28.0), (12.5, 35.0), (18.0, 38.5), 3.8, 2.0), False),+ "type_down": (((14.0, 28.0), (12.5, 36.0), (18.0, 40.5), 3.8, 2.0), False),+ "hold": (((14.0, 28.0), (12.0, 35.0), (17.0, 37.5), 3.8, 2.2), False),+ "stretch": (((15.0, 28.0), (7.0, 26.0), (2.5, 29.5), 3.8, 1.6), True),+ }++ def wing_mask(style):+ (root, ctrl, tip, r0, r1), _behind = wing_shapes[style]+ mask = bezier(root, ctrl, tip, r0, r1)+ if style in ("spread", "up", "half", "stretch", "down"):+ # Fingered primaries at the open wing's end.+ tx, ty = tip+ ang = math.atan2(ty - ctrl[1], tx - ctrl[0])+ for k, da in enumerate((-0.7, -0.15, 0.4)):+ a = ang + da + math.pi / 2 * 0.5+ ex = tx + math.cos(a) * (2.5 + k % 2)+ ey = ty + math.sin(a) * (2.5 + k % 2)+ mask |= line(math.floor(tx), math.floor(ty), math.floor(ex), math.floor(ey))+ return inside(mask)++ def draw_wing(f, style, right):+ mask = wing_mask(style)+ if right:+ mask = mirrored(mask)+ colors = wing_fill(mask)+ (root, ctrl, tip, _r0, _r1), _ = wing_shapes[style]+ # Pale feather bars across the wing, towards its end.+ for t in (0.55, 0.75):+ x = (1 - t) ** 2 * root[0] + 2 * (1 - t) * t * ctrl[0] + t * t * tip[0]+ y = (1 - t) ** 2 * root[1] + 2 * (1 - t) * t * ctrl[1] + t * t * tip[1]+ bx, by = math.floor(x), math.floor(y)+ if right:+ bx = 2 * CX - 1 - bx+ for p in ((bx - 1, by), (bx, by), (bx + 1, by)):+ if p in mask and colors.get(p) == wing:+ colors[p] = wing_bar+ f.part(mask, colors)++ # -- Body ------------------------------------------------------------------++ def ruffled(mask, amount, phase):+ """Spiky tufts sticking out of the outline: ruffled feathers."""+ if amount <= 0:+ return mask+ out = set(mask)+ cx, cy = 24.0, 31.0+ for k in range(14):+ a = 2 * math.pi * (k / 14 + phase * 0.03)+ for r in range(10, 22):+ x = math.floor(cx + math.cos(a) * r)+ y = math.floor(cy + math.sin(a) * r * 0.95)+ if (x, y) not in mask:+ for s in range(amount):+ out.add((math.floor(cx + math.cos(a) * (r + s)),+ math.floor(cy + math.sin(a) * (r + s) * 0.95)))+ break+ return inside(out)++ specks = ((20, 30), (26, 30), (17, 34), (23, 33), (29, 34), (20, 37), (26, 37), (23, 40))++ def draw_body(f, look, i):+ body = body_mask+ if look.puff:+ body = ellipse(CX, 33.5 - look.puff / 2, 12.5 + look.puff, 11.0 + look.puff / 2)+ body = ruffled(body, look.ruffle, i)+ feathers(f, inside(body))+ belly = ellipse(CX, 35.0, 8.0, 7.5) & eroded(body)+ f.paint_map({p: chest_shade if p[1] >= 41 else chest for p in belly})+ for x, y in specks:+ v = {(x, y), (x + 1, y + 1), (x + 2, y)} & belly+ f.paint(v, speck)++ def draw_feet(f, look):+ if look.dangle:+ for x in (19, 26):+ leg = rect(x, 43, x + 2, 44)+ toes = {(x - 1, 45), (x + 1, 46), (x + 3, 45)}+ f.part(leg | toes, talon)+ f.paint({(x - 1, 45), (x + 1, 46), (x + 3, 45)}, talon_dark)+ return+ for x in (18, 26):+ foot = rect(x, 44, x + 3, 44) | {(x, 45), (x + 1, 45), (x + 3, 45)}+ f.part(foot, talon)+ f.paint({(x, 45), (x + 3, 45)}, talon_dark)++ # -- Looks per state ---------------------------------------------------------++ listen_tilt = (0.0, 0.1, 0.17, 0.17, 0.1, 0.0)+ listen_turn = (0, 1, 1, 1, 1, 0)+ idle_turn = (0, 0, 0, 1, 1, 0, 0)+ flap = ("up", "half", "down", "half", "up", "half")+ hop_wings = ("half", "spread", "up", "up", "up", "spread", "half", "fold")++ def look_for(pose):+ state, i = pose.state, pose.i+ if state == "idle":+ turn = idle_turn[i] if i < len(idle_turn) else 0+ return Look(turn=turn)+ if state == "listening":+ return Look(turn=listen_turn[i], tilt=listen_tilt[i], tufts=("perk", "perk"))+ if state == "thinking":+ tap = i % 4 == 2+ return Look(tilt=-0.06, wings=("fold", "chin"), head=(0, 1 if tap else 0))+ if state == "talking":+ # The tufts lift from the first frame, so talking never reads as idle.+ t = ("flick", "flick", "perk", "perk")[i]+ return Look(tufts=(t, t))+ if state == "success":+ return Look(wings=(hop_wings[i], hop_wings[i]), tufts=("perk", "perk"))+ if state == "error":+ return Look(ruffle=(1, 2, 2, 2, 1, 1)[i], tufts=("flick", "flat"), blush=False)+ if state == "sleeping":+ return Look(head=(0, 3), tufts=("droop", "droop"), blush=False)+ if state == "working":+ up = i % 2 == 0+ return Look(+ glasses=True,+ wings=("type_up" if up else "type_down", "type_down" if up else "type_up"),+ )+ if state == "searching":+ sweep = bp.SEARCH_SWEEP[i % 8]+ turn = (sweep > 2) - (sweep < -2)+ return Look(turn=2 * turn, wings=("fold", "fold"))+ if state == "held":+ w = flap[i % len(flap)]+ return Look(wings=(w, w), dangle=True, tufts=("perk", "perk"))+ return Look()++ # -- Poses -----------------------------------------------------------------++ idle_eyes = ("open", "open", "open", "look_right", "look_right", "open", "open")+ error_eyes = ("x", "x", "spin", "spin", "spin", "spin")+ #: A lower hop than the shared one: the ear tufts must stay in the cell.+ hop = (0, -1, -2, -3, -3, -2, 0, 0)++ def nox_pose(state, i):+ pose = base_pose(state, i)+ if state == "idle" and i < len(idle_eyes):+ return replace(pose, eyes=idle_eyes[i])+ if state == "thinking":+ return replace(pose, eyes=("up_left" if i < 4 else "up_right") + "/squint")+ if state == "success":+ return replace(pose, dy=hop[i], mouth="smile")+ if state == "error":+ return replace(pose, eyes=error_eyes[i], mouth="rest")+ if state == "sleeping":+ return replace(pose, eyes="sleep", mouth="rest")+ if state == "working":+ return replace(pose, eyes="read", mouth="rest")+ if state == "searching":+ return replace(pose, mouth="rest")+ if state == "held":+ return replace(pose, mouth="half")+ if state == "listening":+ return replace(pose, mouth="rest")+ return pose++ # -- Drawing ---------------------------------------------------------------++ def draw_owl(f, pose, look):+ planted = (pose.dx, min(pose.dy, 0))+ with f.offset(pose.dx, pose.dy):+ for side, style in enumerate(look.wings):+ if wing_shapes[style][1]:+ draw_wing(f, style, bool(side))+ draw_body(f, look, pose.i)+ with f.offset(*planted):+ draw_feet(f, look)+ with f.offset(pose.dx, pose.dy):+ sub = Frame()+ draw_head(sub, pose, look)+ lay(f, sub, look.tilt, look.head)+ for side, style in enumerate(look.wings):+ if not wing_shapes[style][1]:+ draw_wing(f, style, bool(side))++ def draw(f, pose):+ if pose.act:+ look, extra = act_look(pose)+ draw_owl(f, pose, look)+ if extra is not None:+ extra(f, pose)+ return+ draw_owl(f, pose, look_for(pose))++ # -- Effects ---------------------------------------------------------------++ feather_rows = ("..##", ".##.", "##..", "#...")++ def loose_feather(f, x, y, flip=False):+ rows = feather_rows if not flip else tuple(r[::-1] for r in feather_rows)+ cells = from_rows(x, y, rows)+ cells = inside(cells)+ f.part(cells, wing_light)+ f.paint({(x, y + 3) if not flip else (x + 3, y + 3)} & cells, wing_dark)++ def error_fx(f, pose):+ i = pose.i+ bp.fx_bang(f, 42, 4)+ with f.offset(pose.dx, 0):+ bp.fx_sweat(f, i, 36, 11)+ if i >= 1:+ loose_feather(f, 6, 26 + 2 * i, flip=i % 2 == 0)+ loose_feather(f, 39, 30 + i, flip=i % 2 == 1)++ question = ("##.", "..#", ".#.", "...", ".#.")+ bulb = (".#.", "#o#", "#o#", ".#.", ".b.")++ def thinking_fx(f, pose):+ """A thought bubble puffs up beside the tufts: a question, then an idea."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ f.part({(36, 13)}, FX_WHITE)+ if i >= 1:+ f.part(rect(38, 10, 39, 11), FX_WHITE)+ if i < 2:+ return+ cloud = inside(+ ellipse(42.0, 5.5, 4.6, 3.6) | ellipse(39.5, 7.0, 2.4, 2.2)+ | ellipse(44.5, 7.0, 2.4, 2.2)+ )+ f.part(cloud, {p: FX_WHITE if p[1] < 8 else FX_DIM for p in cloud})+ if i < 6:+ f.paint(from_rows(41, 3 + (i % 2), question), note)+ else:+ f.glyph(41, 3, bulb, {"#": bp.SPARK, "o": bp.SPARK_CORE, "b": FX_DIM})+ if i == 7:+ bp.fx_sparkles(f, 1, ((37, 3, 0),))++ def working_fx(f, pose):+ bp.fx_laptop(f, pose.i, 17, 34)++ def searching_fx(f, pose):+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ bp.fx_magnifier(f, i, CX, 32)++ # -- Idle acts ---------------------------------------------------------------++ spin_turn = (0, 2, 4, 9, 9, -4, -2, 0)+ stretch_wings = (+ ("fold", "fold"),+ ("half", "fold"),+ ("stretch", "fold"),+ ("stretch", "fold"),+ ("fold", "half"),+ ("fold", "stretch"),+ ("fold", "stretch"),+ ("fold", "fold"),+ )+ blink_eyes = ("open", "heavy", "half", "closed", "closed", "half", "heavy", "open")+ ruffle_amount = (0, 1, 2, 3, 3, 2, 1, 0)+ ruffle_puff = (0.0, 0.5, 1.0, 1.5, 1.5, 1.0, 0.5, 0.0)+ hoot_mouth = ("rest", "half", "hoot", "hoot", "rest", "half", "hoot", "rest")+ hoot_dy = (0, -1, -2, -2, 0, -1, -2, 0)++ def act_pose(name, i):+ if name == "head_spin":+ eyes = "open" if abs(spin_turn[i]) < 4 else "look_left"+ return Pose("idle", i, eyes=eyes if spin_turn[i] >= 0 else "look_right")+ if name == "wing_stretch":+ eyes = "happy" if 2 <= i <= 6 else "open"+ return Pose("idle", i, eyes=eyes, stretch=1 if 2 <= i <= 5 else 0)+ if name == "slow_blink":+ return Pose("idle", i, eyes=blink_eyes[i], stretch=1 if i in (3, 4) else 0)+ if name == "read_book":+ eyes = "look_left/look_left" if i < 3 else "look_right/look_right"+ if i == 7:+ eyes = "happy"+ return Pose("idle", i, eyes=eyes, mouth="rest")+ if name == "feather_ruffle":+ eyes = "closed" if 2 <= i <= 5 else "open"+ return Pose("idle", i, eyes=eyes, dx=(0, 0, -1, 1, -1, 1, 0, 0)[i])+ if name == "hoot":+ eyes = "closed" if hoot_mouth[i] == "hoot" else "open"+ return Pose("idle", i, eyes=eyes, mouth=hoot_mouth[i], stretch=-hoot_dy[i] // 2)+ raise KeyError(name)++ def act_look(pose):+ name, i = pose.act, pose.i+ if name == "head_spin":+ t = spin_turn[i]+ return Look(turn=t, back=abs(t) >= 9, blush=abs(t) < 2), None+ if name == "wing_stretch":+ return Look(wings=stretch_wings[i], tufts=("perk", "perk") if 2 <= i <= 6+ else ("rest", "rest")), None+ if name == "slow_blink":+ return Look(tilt=(0, 0, 0.04, 0.06, 0.06, 0.04, 0, 0)[i]), None+ if name == "read_book":+ return Look(glasses=i < 7, wings=("hold", "hold"), head=(0, 1)), draw_book+ if name == "feather_ruffle":+ return Look(ruffle=ruffle_amount[i], puff=ruffle_puff[i],+ tufts=("flick", "flick") if 2 <= i <= 5 else ("rest", "rest")), None+ if name == "hoot":+ return Look(head=(0, hoot_dy[i]), tufts=("perk", "perk")+ if hoot_mouth[i] == "hoot" else ("rest", "rest")), None+ raise KeyError(name)++ def draw_book(f, pose):+ """An open book held up in front of the chest; a page turns at frame 4-5."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ cover = rect(13, 33, 34, 42)+ f.part(cover, shade(cover, book, light=book_light, dark=book_dark, dark_depth=1))+ left = rect(14, 33, 23, 40)+ right = rect(24, 33, 33, 40)+ f.paint(left | right, page)+ f.paint(line(CX - 1, 33, CX - 1, 41) | line(CX, 33, CX, 41), page_line)+ for y in (35, 37, 39):+ f.paint(line(15, y, 21, y) | line(26, y, 32, y), page_line)+ if i in (4, 5):+ # The page lifting over the spine.+ flip = rect(18, 31, 23, 38) if i == 4 else rect(24, 31, 28, 38)+ f.part(flip, page)+ f.paint(line(min(x for x, _ in flip) + 1, 33, max(x for x, _ in flip) - 1, 33),+ page_line)+ f.paint({(30, 41), (30, 42), (30, 43), (31, 43)}, ribbon)+ # The wing tips holding it at the sides.+ for x in (13, 34):+ tip = ellipse(x + 0.5, 37.5, 2.0, 2.5)+ f.part(tip, wing_fill(tip))++ def wing_stretch_fx(f, pose):+ if pose.i in (3, 6):+ x = 4 if pose.i == 3 else 43+ bp.fx_sparkles(f, 1, ((x, 22, 0),))++ def ruffle_fx(f, pose):+ i = pose.i+ if 3 <= i <= 6:+ age = i - 3+ loose_feather(f, 6 - age, 18 + 3 * age)+ loose_feather(f, 39 + age, 16 + 3 * age, flip=True)+ f.paint({(10, 12 + age), (37, 11 + age)}, FX_DIM)++ def hoot_fx(f, pose):+ i = pose.i+ for start in (2, 6):+ age = i - start+ if 0 <= age <= 1:+ f.paint({(37 + age, 14 - age), (38 + age, 13 - age), (39 + age, 14 - age)},+ note if age == 0 else note_fade)+ bp.fx_arcs(f, 2 + age * 2, (11, 18), (36, 18))++ def slow_blink_fx(f, pose):+ if pose.i in (3, 4):+ f.paint({(38, 12), (39, 11), (40, 12)}, FX_DIM)++ def act(name, fps, fx=None):+ return bp.IdleAct(name, 8, fps, lambda i: act_pose(name, i), fx)++ acts = (+ act("head_spin", 8),+ act("wing_stretch", 7, wing_stretch_fx),+ act("slow_blink", 5, slow_blink_fx),+ act("read_book", 5),+ act("feather_ruffle", 9, ruffle_fx),+ act("hoot", 6, hoot_fx),+ )++ return bp.PetDesign(+ id="nox",+ name="Nox",+ description=(+ "A little owl who turns its head to listen, puts on reading glasses to work"+ " and spreads its wings when something goes right."+ ),+ draw=draw,+ pose=nox_pose,+ waist=40,+ anchors=bp.Anchors(+ arcs_left=(9, 16),+ arcs_right=(38, 16),+ dots=(31, 3),+ zzz=(35, 15),+ bang=(42, 4),+ sparkles=((6, 8, 1), (41, 6, 2), (10, 4, 4), (38, 3, 5)),+ laptop=(17, 34),+ magnifier=(CX, 34),+ ),+ fx={+ "thinking": thinking_fx,+ "error": error_fx,+ "working": working_fx,+ "searching": searching_fx,+ },+ acts=acts,+ )diff --git a/scripts/pets/designs/otto.py b/scripts/pets/designs/otto.pynew file mode 100644index 0000000000..cc15e03c84--- /dev/null+++ b/scripts/pets/designs/otto.py@@ -0,0 +1,1110 @@+"""Otto: the sea otter.++A round little sea otter sitting upright: warm brown fur, a pale cream face+with whiskers and a dark button nose, small round ears, short arms with+webbed paws held together on the chest, big webbed hind feet and a thick+flat tail. Relaxed and playful. It cracks a clam on its belly with a pebble+while it thinks, does a backflip with a shell when something works, drops+the shell in fright on an error, and naps floating on its back with its+paws over its eyes. The idle acts are otter habits: cracking a clam,+rolling over, juggling a pebble, washing its whiskers, floating on its back+and slapping the water with its tail.++Built by ``scripts/pets/build_pets.py``: ``design(bp)`` gets that module and+everything here draws with its helpers, so Otto shares the outline, the+shading pass and the effect conventions of the other pets.+"""++from __future__ import annotations++import math+from dataclasses import replace+from typing import Any, NamedTuple++#: The builder module (``scripts/pets/build_pets.py``), set by ``design``.+bp: Any = None++Px = tuple[int, int]+RGBA = tuple[int, int, int, int]+FPx = tuple[float, float]+++def _rgb(value: str) -> RGBA:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++CX = 24++OTTO_FUR = _rgb("#9a6440")+OTTO_LIGHT = _rgb("#c08455")+OTTO_SHINE = _rgb("#d49a68")+OTTO_DARK = _rgb("#6b4029")+OTTO_DEEP = _rgb("#4a2a1a")+OTTO_BELLY = _rgb("#b98457")+OTTO_CREAM = _rgb("#f3e3c6")+OTTO_CREAM_DARK = _rgb("#dcc29c")+OTTO_PAD = _rgb("#fbf2e2")+OTTO_EAR_IN = _rgb("#5a3220")+OTTO_NOSE = _rgb("#2a1a16")+OTTO_NOSE_SHINE = _rgb("#8f7a74")+OTTO_EYE = _rgb("#21161a")+OTTO_BLUSH = _rgb("#ec9a94")+OTTO_WHISKER = _rgb("#8a6a52")+OTTO_WHISKER_TIP = _rgb("#c9b8a2")+#: A clam: a cool lavender-blue that holds on dark AND light desktops.+OTTO_CLAM = _rgb("#9fb0dc")+OTTO_CLAM_LIGHT = _rgb("#dbe3f7")+OTTO_CLAM_DARK = _rgb("#6a78b0")+OTTO_CLAM_MEAT = _rgb("#ffb7a1")+OTTO_PEBBLE = _rgb("#8e8b98")+OTTO_PEBBLE_LIGHT = _rgb("#c4c1cc")+OTTO_WATER = _rgb("#3f9ad8")+OTTO_WATER_LIGHT = _rgb("#8fd0f4")+OTTO_WATER_DARK = _rgb("#2a6aa8")++_CLAM = (+ "..lll..",+ ".l#l#l.",+ "l#d#d#l",+ "#d#d#d#",+ ".##d##.",+ "..ddd..",+)+_CLAM_OPEN = (+ "..lll..",+ ".l#l#l.",+ "#d#d#d#",+ ".mmmmm.",+ "#d#d#d#",+ ".##d##.",+)+_PEBBLE = (".l#.", "l###", ".##.")+_BUBBLE = (".#.", "#o#", ".#.")+_DRIP = (".#", "##")+++def _clam_palette() -> dict[str, RGBA]:+ return {+ "#": OTTO_CLAM,+ "l": OTTO_CLAM_LIGHT,+ "d": OTTO_CLAM_DARK,+ "m": OTTO_CLAM_MEAT,+ }+++def _pebble_palette() -> dict[str, RGBA]:+ return {"#": OTTO_PEBBLE, "l": OTTO_PEBBLE_LIGHT}+++def _water_palette() -> dict[str, RGBA]:+ return {"#": OTTO_WATER_LIGHT, "o": bp.FX_WHITE}+++# ---------------------------------------------------------------------------+# Small helpers.+# ---------------------------------------------------------------------------+++def _in_cell(mask: set[Px], margin: int = 1) -> set[Px]:+ """``mask`` clipped so that it AND its outline stay inside the cell."""+ hi = bp.CELL - margin+ return {(x, y) for x, y in mask if margin <= x < hi and margin <= y < hi}+++def _fur(f: Any, mask: set[Px], depth: int = 2) -> None:+ f.part(mask, bp.shade(mask, OTTO_FUR, light=OTTO_LIGHT, dark=OTTO_DARK, dark_depth=depth))+++def _limb(start: Px, end: Px) -> set[Px]:+ """A short otter arm or leg, three pixels wide."""+ return bp.thick(bp.thick(bp.line(*start, *end)))+++def _paw(+ f: Any, cx: float, cy: float, rx: float = 2.4, ry: float = 2.0, *, toes_down: bool = False+) -> None:+ """A small webbed front paw with two toe creases, along its top edge (a+ raised paw) or its bottom edge (a paw resting on the belly)."""+ paw = bp.ellipse(cx, cy, rx, ry)+ _fur(f, paw, 1)+ edge = max(y for _x, y in paw) if toes_down else min(y for _x, y in paw) + 1+ x = math.floor(cx)+ f.paint({(x - 1, edge), (x + 1, edge)} & paw, OTTO_DARK)+++def _clam(f: Any, x: int, y: int, *, open_: bool = False) -> None:+ f.glyph(x, y, _CLAM_OPEN if open_ else _CLAM, _clam_palette())+++def _pebble(f: Any, x: int, y: int) -> None:+ f.glyph(x, y, _PEBBLE, _pebble_palette())+++def _tail_mask(root: FPx, ctrl: FPx, tip: FPx, r0: float = 3.0, r1: float = 1.6) -> set[Px]:+ """The thick flat otter tail: round blobs along a curve, tapering to the tip."""+ mask: set[Px] = set()+ steps = 8+ for k in range(steps + 1):+ t = k / steps+ x = (1 - t) ** 2 * root[0] + 2 * (1 - t) * t * ctrl[0] + t * t * tip[0]+ y = (1 - t) ** 2 * root[1] + 2 * (1 - t) * t * ctrl[1] + t * t * tip[1]+ r = r0 + (r1 - r0) * t+ mask |= bp.ellipse(x, y, r, r * 0.85)+ return _in_cell(mask)+++def _tail(f: Any, root: FPx, ctrl: FPx, tip: FPx, r0: float = 3.0) -> None:+ tail = _tail_mask(root, ctrl, tip, r0)+ _fur(f, tail, 1)+ # A darker stripe along the middle: the tail is flat and thick.+ mid = {+ (+ math.floor(root[0] + (tip[0] - root[0]) * t),+ math.floor(ctrl[1] - 1 + (tip[1] - ctrl[1]) * t),+ )+ for t in (0.55, 0.7)+ }+ f.paint(mid & tail, OTTO_DARK)+++def _water(f: Any, mask: set[Px]) -> None:+ """Water with a lit surface row and a darker bottom edge."""+ mask = _in_cell(mask)+ f.part(mask, bp.shade(mask, OTTO_WATER, light=OTTO_WATER_LIGHT, dark=OTTO_WATER_DARK))+++def _transform(f: Any, sub: Any, fn: Any, offset: Px = (0, 0)) -> None:+ """Lay ``sub`` onto ``f`` with every pixel moved by ``fn`` and ``offset``,+ clipped to the cell."""+ out: dict[Px, RGBA] = {}+ for (x, y), color in sub.px.items():+ nx, ny = fn(x, y)+ nx, ny = nx + offset[0], ny + offset[1]+ if 0 <= nx < bp.CELL and 0 <= ny < bp.CELL:+ out[(nx, ny)] = color+ f.paint_map(out)+++def _rot90(k: int, ox: int, oy: int) -> Any:+ """Turn a pixel ``k`` quarter turns clockwise about the corner point ``(ox, oy)``."""++ def fn(x: int, y: int) -> Px:+ for _ in range(k % 4):+ x, y = ox + oy - y - 1, oy - ox + x+ return x, y++ return fn+++def _squash(k: float, oy: int) -> Any:+ """Squash rows towards the line ``oy`` by ``k``."""++ def fn(x: int, y: int) -> Px:+ return x, oy + math.floor((y + 0.5 - oy) * k)++ return fn+++# ---------------------------------------------------------------------------+# The face.+# ---------------------------------------------------------------------------++#: The top-left of each 4 x 5 eye box.+_EYE_AT: tuple[Px, Px] = ((16, 14), (28, 14))+_LOOK: dict[str, Px] = {+ "open": (0, 0),+ "wide": (0, 0),+ "look_left": (-1, 0),+ "look_right": (1, 0),+ "up_left": (-1, -1),+ "up_right": (1, -1),+ "down": (0, 1),+}+#: Closed-eye shapes on the 4 x 5 eye box (the right eye is mirrored).+_LIDS: dict[str, tuple[str, ...]] = {+ "closed": ("....", "....", "#..#", ".##.", "...."),+ "sleep": ("....", "....", "#..#", ".##.", "...."),+ "happy": ("....", ".##.", "#..#", "....", "...."),+ "squint": ("#...", ".##.", "...#", ".##.", "#..."),+ "x": ("#..#", ".##.", ".##.", "#..#", "...."),+ "dizzy": (".##.", "#..#", "#.##", "#...", ".###"),+}+_W = {(20, 22), (21, 23), (22, 23), (23, 22), (24, 22), (25, 23), (26, 23), (27, 22)}+++def _eyes(f: Any, style: str) -> None:+ for side, (x, y) in enumerate(_EYE_AT):+ if style in _LIDS:+ rows = _LIDS[style]+ if side:+ rows = tuple(r[::-1] for r in rows)+ f.paint(bp.from_rows(x, y, rows), bp.OUTLINE)+ continue+ lx, ly = _LOOK[style]+ x, y = x + lx, y + ly+ top, h = (y - 1, 6) if style == "wide" else (y, 5)+ f.paint(bp._oval(x, top, 4, h), OTTO_EYE)+ f.paint({(x + 2, top + 1), (x + 2, top + 2)}, bp.FX_WHITE)+ f.paint({(x + 1, top + h - 2)}, OTTO_NOSE_SHINE)+ if style == "down":+ f.paint(bp.rect(x, top, x + 3, top), bp.OUTLINE)+++def _mouth(f: Any, mouth: str) -> None:+ if mouth in ("rest", "closed"):+ f.paint(_W, bp.OUTLINE)+ elif mouth == "smile":+ f.paint(_W | {(19, 21), (28, 21)}, bp.OUTLINE)+ elif mouth == "frown":+ f.paint(+ {(23, 22), (24, 22), (21, 24), (22, 23), (25, 23), (26, 24)},+ bp.OUTLINE,+ )+ elif mouth == "o":+ f.paint({(23, 22), (24, 22)}, bp.OUTLINE)+ f.part(bp.rect(23, 24, 24, 25), bp.MOUTH_DARK)+ elif mouth == "munch":+ f.paint(_W, bp.OUTLINE)+ f.paint({(23, 23), (24, 23)}, bp.MOUTH_DARK)+ elif mouth in ("half", "open", "wide"):+ depth = {"half": 1, "open": 2, "wide": 3}[mouth]+ inside = bp.rect(21, 23, 26, 23 + depth) - {(21, 23 + depth), (26, 23 + depth)}+ f.paint(inside, bp.MOUTH_DARK)+ f.paint(bp.rect(20, 22, 27, 22) | {(19, 21), (28, 21)}, bp.OUTLINE)+ if depth >= 2:+ f.paint(bp.rect(22, 22 + depth, 25, 23 + depth) & inside, bp.TONGUE)+ f.paint({(22, 23), (25, 23)}, bp.FX_WHITE) # two little teeth+ else:+ raise KeyError(mouth)+++#: Ear centres (left ear; the right one is mirrored) and radii per mood.+_EARS: dict[str, tuple[float, float, float, float]] = {+ "rest": (13.0, 12.0, 2.4, 2.4),+ "up": (13.5, 10.5, 2.4, 2.6),+ "flat": (11.5, 14.5, 2.8, 1.8),+ "flick": (12.5, 11.0, 2.2, 2.2),+}+++def _ears(f: Any, ears: tuple[str, str]) -> None:+ for side, name in enumerate(ears):+ x, y, rx, ry = _EARS[name]+ ear = bp.ellipse(x, y, rx, ry)+ inner = {(math.floor(x), math.floor(y)), (math.floor(x), math.floor(y) + 1)}+ if side:+ ear, inner = bp.mirrored(ear), bp.mirrored(inner)+ _fur(f, ear, 1)+ f.paint(inner & ear, OTTO_EAR_IN)+++def _head(f: Any, pose: Any, look: _Look) -> None:+ """Ears, the round head, the cream face, whiskers and the features+ (head centred on (CX, 18))."""+ with f.offset(*look.head):+ _ears(f, look.ears)+ head = bp.ellipse(CX, 18.5, 12.5, 9.0)+ _fur(f, head)+ f.paint({(19, 10), (20, 10), (21, 10), (22, 10), (17, 11)} & head, OTTO_SHINE)+ # A sea otter's pale face reaches up over the brow; the brown stays a cap.+ brow = bp.ellipse(18.0, 16.5, 4.5, 4.5)+ face = bp.ellipse(CX, 21.0, 10.0, 6.5) | brow | bp.mirrored(brow)+ face |= bp.ellipse(CX, 15.5, 5.0, 3.5)+ face &= bp.eroded(head, 1) | {p for p in head if p[1] >= 24}+ f.paint_map(bp.shade(face, OTTO_CREAM, dark=OTTO_CREAM_DARK, dark_depth=1))+ # Puffy whisker pads either side of the nose, freckled with whisker roots.+ pad = bp.ellipse(21.0, 21.0, 3.4, 2.6)+ f.paint(pad | bp.mirrored(pad), OTTO_PAD)+ f.paint({(19, 20), (20, 21), (18, 21), (28, 20), (27, 21), (29, 21)}, OTTO_WHISKER)+ if look.blush:+ f.paint(bp.rect(14, 22, 16, 22) | bp.rect(31, 22, 33, 22), OTTO_BLUSH)+ _eyes(f, pose.eyes)+ lift = -1 if look.sniff else 0+ nose = bp.rect(21, 17 + lift, 26, 18 + lift) - {(21, 18 + lift), (26, 18 + lift)}+ nose |= {(23, 19 + lift), (24, 19 + lift)}+ f.paint(nose, OTTO_NOSE)+ f.paint({(22, 17 + lift), (23, 17 + lift)}, OTTO_NOSE_SHINE)+ _mouth(f, pose.mouth)+ _whiskers(f, look.whiskers)+++def _whiskers(f: Any, spread: int) -> None:+ """Two whiskers each side, rooted on the cheek; ``spread`` fans them out."""+ left: dict[Px, RGBA] = {}+ for y0, y1 in ((20, 19 - spread), (22, 22 + spread)):+ for x, y in bp.line(16, y0, 8, y1):+ left[(x, y)] = OTTO_WHISKER if x >= 12 else OTTO_WHISKER_TIP+ right = {(2 * CX - 1 - x, y): c for (x, y), c in left.items()}+ f.paint_map(left | right)+++# ---------------------------------------------------------------------------+# The sitting otter: one ``_Look`` per frame says how it holds itself.+# ---------------------------------------------------------------------------++_SHOULDERS: tuple[Px, Px] = ((16, 29), (31, 29))+#: Both paws resting together on the round belly.+_REST_PAWS: tuple[FPx, FPx] = ((21.0, 36.5), (27.0, 36.5))+#: Paws at or below this row lie tucked on the belly, without a visible arm.+_TUCKED = 34.0+_CHIN_PAWS: tuple[FPx, FPx] = ((21.5, 27.5), (26.5, 27.5))+++class _Look(NamedTuple):+ """How the sitting otter holds itself, beyond the pose's eyes and mouth."""++ ears: tuple[str, str] = ("rest", "rest")+ #: The tail's tip: (sway right, lift up) in pixels.+ tail: Px = (0, 0)+ #: Paw centres (left, right); ``None`` hides that arm (drawn elsewhere).+ paws: tuple[FPx | None, FPx | None] = _REST_PAWS+ head: Px = (0, 0)+ whiskers: int = 0+ blush: bool = True+ sniff: bool = False+ #: Hind feet lifted off the floor (left, right).+ feet: Px = (0, 0)+ #: Items in the paws: (kind, x, y) top-left; kinds "clam", "clam_open", "pebble".+ items: tuple[tuple[str, int, int], ...] = ()+ #: Items drawn in front of the paws.+ front: tuple[tuple[str, int, int], ...] = ()+++def _items(f: Any, items: tuple[tuple[str, int, int], ...]) -> None:+ for kind, x, y in items:+ if kind == "pebble":+ _pebble(f, x, y)+ else:+ _clam(f, x, y, open_=kind == "clam_open")+++def _feet(f: Any, lift: Px = (0, 0)) -> None:+ """Big webbed hind feet, toes pointing out, three toe creases each."""+ for side, up in enumerate(lift):+ foot = bp.ellipse(16.0, 45.0 - up, 4.4, 2.0)+ toes = {(13, 46 - up), (15, 46 - up), (17, 46 - up)}+ if side:+ foot, toes = bp.mirrored(foot), bp.mirrored(toes)+ _fur(f, foot, 1)+ f.paint(toes & foot, OTTO_DEEP)+++def _sit_body() -> set[Px]:+ return bp.ellipse(CX, 35.5, 10.5, 9.5) | bp.ellipse(CX, 41.0, 12.0, 5.0)+++def _arm(f: Any, shoulder: Px, end: Px, body: set[Px]) -> None:+ """An arm from ``shoulder`` to ``end``: outlined where it stands free,+ only a soft fur crease where it lies against the body."""+ arm = _limb(shoulder, end)+ ring = bp.outer_ring(arm)+ f.paint(ring - body, bp.OUTLINE)+ f.paint(ring & body, OTTO_DARK)+ f.paint_map(bp.shade(arm, OTTO_FUR, light=OTTO_LIGHT, dark=OTTO_DARK, dark_depth=1))+++def _sit(f: Any, pose: Any, look: _Look) -> None:+ """The otter sitting upright, facing out."""+ with f.offset(pose.dx, min(pose.dy, 0)):+ _feet(f, look.feet)+ with f.offset(pose.dx, pose.dy):+ sway, lift = look.tail+ _tail(f, (32.0, 42.5), (41.0, 45.0), (43.0 + sway, 39.0 - lift))+ body = _sit_body()+ _fur(f, body)+ belly = bp.ellipse(CX, 37.5, 6.5, 6.5)+ f.paint(belly, OTTO_BELLY)+ f.paint({(22, 32), (23, 32), (21, 33)}, OTTO_SHINE)+ f.paint(bp.line(15, 41, 17, 44) | bp.line(32, 41, 30, 44), OTTO_DARK)+ for shoulder, paw in zip(_SHOULDERS, look.paws, strict=True):+ # A paw low on the belly needs no arm: it is tucked against the body.+ if paw is not None and paw[1] < _TUCKED:+ _arm(f, shoulder, (math.floor(paw[0]) - 1, math.floor(paw[1]) - 1), body)+ _items(f, look.items)+ _head(f, pose, look)+ for paw in look.paws:+ if paw is not None:+ if paw[1] >= _TUCKED:+ _paw(f, *paw, 3.0, 2.1, toes_down=True)+ else:+ _paw(f, *paw)+ _items(f, look.front)+++# ---------------------------------------------------------------------------+# Floating on its back (sleeping, the belly float).+# ---------------------------------------------------------------------------+++#: Where the sitting head goes when the otter lies afloat (its face to the+#: viewer, chin just above the water).+_FLOAT_HEAD: Px = (-9, 11)+++def _waves(f: Any, phase: int, top: int = 42) -> None:+ """A band of water across the bottom of the cell with a rolling crest."""+ water: set[Px] = set()+ crest: set[Px] = set()+ for x in range(2, 46):+ y0 = top + round(1.2 * math.sin((x + phase) * 0.7))+ # Rounded ends, so the band reads as a patch of sea, not a slab.+ y0 = max(y0, top + {2: 4, 3: 2, 4: 1, 43: 1, 44: 2, 45: 4}.get(x, -9))+ water |= {(x, y) for y in range(y0, 47)}+ if math.sin((x + phase) * 0.7) < -0.5:+ crest.add((x, y0))+ _water(f, water)+ f.paint(crest & water, bp.FX_WHITE)+ f.paint({(x, 44) for x in range(6 + phase % 4, 42, 7)} & water, OTTO_WATER_LIGHT)+++def _float(f: Any, pose: Any, *, mode: str, rise: int = 0, bob: int = 0) -> None:+ """Afloat on its back: the head to the left with its face to the viewer,+ the belly up, the webbed feet poking out of the water on the right.+ ``mode`` is "sleep" (paws over the eyes) or "clam" (a clam on the belly)."""+ hx, hy = _FLOAT_HEAD+ with f.offset(0, bob):+ _tail(f, (40.0, 39.0), (44.0, 40.0), (45.5, 36.5), 2.4)+ for cx, cy in ((37.5, 29.5), (42.5, 30.5)):+ _fur(f, _limb((math.floor(cx) - 2, 35), (math.floor(cx) - 1, math.floor(cy) + 1)), 1)+ foot = bp.ellipse(cx, cy, 2.2, 3.0)+ _fur(f, foot, 1)+ top = min(y for _x, y in foot)+ f.paint({(math.floor(cx) - 1, top), (math.floor(cx) + 1, top)} & foot, OTTO_DEEP)+ body = bp.ellipse(31.0, 37.0 - rise * 0.5, 12.0, 5.5 + rise * 0.5)+ _fur(f, body)+ f.paint(bp.ellipse(32.0, 34.0 - rise, 8.0, 2.0) & body, OTTO_BELLY)+ f.paint({(29, 33 - rise), (30, 33 - rise)}, OTTO_SHINE)+ # Drawn alone and clipped: the far whisker tips leave the cell.+ sub = bp.Frame()+ _head(sub, pose, _Look(paws=(None, None), head=(hx, hy)))+ _transform(f, sub, lambda x, y: (x, y))+ if mode == "sleep":+ # Both paws over the eyes, the slim arms rising from the water.+ for px, shoulder in ((8.5, (10, 40)), (20.5, (18, 40))):+ _fur(f, bp.thick(bp.line(*shoulder, math.floor(px) - 1, 29)), 1)+ _paw(f, px, 27.5, 2.8, 2.3)+ else:+ _clam(f, 29, 28 - rise)+ for px in (27.5, 37.5):+ _fur(f, _limb((math.floor(px) - 1, 35), (math.floor(px) - 1, 31)), 1)+ _paw(f, px, 31.0 - rise, 2.3, 2.0)+ _waves(f, pose.i + bob)+++# ---------------------------------------------------------------------------+# Held, the backflip and the roll.+# ---------------------------------------------------------------------------+++def _dangle(f: Any, pose: Any) -> None:+ """Held: scooped up, body hanging long, paws paddling at the air and the+ hind feet kicking as if swimming."""+ i = pose.i+ lag = -pose.dx+ paddle = i % 2+ with f.offset(pose.dx, 0):+ sway = (-2, 0, 2, 2, 0, -2)[i % 6]+ _tail(f, (27.0, 39.0), (31.0, 44.0), (33.0 + sway, 44.5), 2.6)+ for side, (hip, out) in enumerate((((19, 38), -1), ((28, 38), 1))):+ up = (paddle + side) % 2+ foot_at = (hip[0] + out * 2 + lag, 43 - up)+ _fur(f, _limb(hip, foot_at), 1)+ foot = bp.ellipse(foot_at[0] + 0.5 + out, foot_at[1] + 1.5, 3.4, 1.8)+ _fur(f, foot, 1)+ f.paint(+ {(foot_at[0] - 1 + out, foot_at[1] + 2), (foot_at[0] + 1 + out, foot_at[1] + 2)}+ & foot,+ OTTO_DEEP,+ )+ body = bp.ellipse(CX, 32.0, 9.5, 9.0)+ _fur(f, body)+ f.paint(bp.ellipse(CX, 33.5, 5.5, 5.5), OTTO_BELLY)+ look = _Look(ears=("up", "up"), head=(0, -4), paws=(None, None), whiskers=1)+ _head(f, pose, look)+ # A doggy paddle: one paw reaches up and forward while the other pulls+ # down and back, then they swap.+ for side in (0, 1):+ up = (paddle + side) % 2+ x = 18.5 if up else 15.0+ paw = (x, 28.5 if up else 35.0)+ if side:+ paw = (2 * CX - paw[0], paw[1])+ shoulder = _SHOULDERS[side][0], 27+ _arm(f, shoulder, (math.floor(paw[0]) - 1, math.floor(paw[1]) - 1), body)+ _paw(f, paw[0] + 0.5 * lag, paw[1], 2.6, 2.1)+++def _ball(f: Any, pose: Any) -> None:+ """Curled up into a ball: the head tucked over the belly, feet and tail+ wrapped round the front. Turned about its centre for the roll."""+ look = _Look(paws=(None, None), head=(0, 3), ears=("flat", "flat"), blush=True)+ body = bp.ellipse(CX, 34.0, 11.0, 10.0)+ _fur(f, body)+ _tail(f, (33.0, 39.0), (30.0, 46.0), (17.0, 43.5), 2.6)+ _head(f, pose, look)+ for x in (18.5, 29.5):+ _paw(f, x, 33.0, 2.6, 2.2)+++def _crouch(f: Any, pose: Any, look: _Look) -> None:+ """The sitting otter squashed down onto its feet: winding up for a jump,+ or the landing."""+ sub = bp.Frame()+ _sit(sub, replace(pose, dx=0, dy=0), look)+ _transform(f, sub, _squash(0.9, 47), (pose.dx, 0))+++def _rolled(f: Any, pose: Any, turns: int) -> None:+ """The ball turned ``turns`` quarter turns clockwise (3 = one back)."""+ sub = bp.Frame()+ _ball(sub, replace(pose, dx=0, dy=0))+ _transform(f, sub, _rot90(turns, CX, 30), (pose.dx, pose.dy))+++# ---------------------------------------------------------------------------+# Poses.+# ---------------------------------------------------------------------------++_IDLE_EYES = ("open", "open", "open", "look_left", "look_left", "open", "open")+_IDLE_TAIL = ((0, 0), (0, 0), (1, 1), (2, 2), (2, 2), (1, 1), (0, 0))+#: Thinking: the pebble paw per frame: raised, striking the clam, or mid-way.+_THINK_PAW = ("up", "hit", "mid", "up", "up", "hit", "mid", "up")+#: Success: the backflip. Vertical squash and flip per frame, then the shell+#: is held up in triumph.+_FLIP_DY = (0, -4, -8, -9, -7, -2, 0, 0)+#: The tucked ball's quarter turns while airborne (None: drawn sitting).+_FLIP_TURNS: tuple[int | None, ...] = (None, None, 3, 2, 1, None, None, None)+_ERROR_EYES = ("wide", "wide", "wide", "x", "x", "squint")+_ERROR_MOUTH = ("o", "o", "o", "frown", "frown", "frown")+_ERROR_DY = (-2, -1, 0, 0, 0, 0)+_ERROR_SHAKE = (0, 0, 0, -1, 1, 0)+_SWAY = (-1, 0, 1, 1, 0, -1)+++def otto_pose(state: str, i: int) -> Any:+ pose = bp.base_pose(state, i)+ if state == "idle":+ if i < len(_IDLE_EYES):+ return replace(pose, eyes=_IDLE_EYES[i], mouth="rest")+ return replace(pose, mouth="rest")+ if state == "listening":+ return replace(pose, mouth="closed")+ if state == "thinking":+ return replace(pose, eyes="down" if _THINK_PAW[i] == "hit" else "up_right", mouth="closed")+ if state == "talking":+ return pose+ if state == "success":+ eyes = "happy"+ mouth = "smile" if i < 6 else "open"+ return bp.Pose(state, i, dy=_FLIP_DY[i], eyes=eyes, mouth=mouth)+ if state == "error":+ return bp.Pose(+ state,+ i,+ dx=_ERROR_SHAKE[i],+ dy=_ERROR_DY[i],+ eyes=_ERROR_EYES[i],+ mouth=_ERROR_MOUTH[i],+ )+ if state == "sleeping":+ return bp.Pose(state, i, eyes="sleep", mouth="closed")+ if state == "working":+ return replace(pose, mouth="closed")+ if state == "searching":+ return pose+ if state == "held":+ return bp.Pose(state, i, dx=_SWAY[i % len(_SWAY)], eyes="wide", mouth="o")+ return pose+++def _look(pose: Any) -> _Look:+ """The sitting otter's body language per state."""+ state, i = pose.state, pose.i+ if state == "idle":+ if i >= len(_IDLE_TAIL):+ return _Look(ears=("flick", "rest")) # the accent: a blink and an ear flick+ return _Look(tail=_IDLE_TAIL[i], whiskers=1 if i == 5 else 0, sniff=i == 5)+ if state == "listening":+ return _Look(ears=("up", "up"), paws=_CHIN_PAWS, tail=(1, 2))+ if state == "thinking":+ phase = _THINK_PAW[i]+ paw = {"up": (31.5, 25.5), "mid": (29.5, 28.0), "hit": (27.0, 32.5)}[phase]+ return _Look(+ paws=((18.5, 37.5), paw),+ items=(("clam", 20, 33),),+ front=(("pebble", math.floor(paw[0]) - 2, math.floor(paw[1]) - 3),),+ tail=(0, i % 2),+ )+ if state == "talking":+ lift = (0, 1, 2, 4)[i]+ paws = ((21.0 - lift, 31.5 - lift / 2), (27.0 + lift, 31.5 - lift / 2))+ return _Look(paws=paws, ears=("flick", "flick") if i == 3 else ("rest", "rest"))+ if state == "error":+ if i < 1:+ return _Look(ears=("up", "up"), items=(("clam", 21, 30),), whiskers=1)+ flung = ((14.5, 25.0), (33.5, 25.0)) if i < 3 else ((19.5, 33.0), (28.5, 33.0))+ return _Look(ears=("flat", "flat"), paws=flung, tail=(-1, -2), whiskers=1 if i < 3 else -1)+ if state == "working":+ return _Look(paws=(None, None))+ if state == "searching":+ return _Look(paws=(_REST_PAWS[0], None))+ return _Look()+++# ---------------------------------------------------------------------------+# Drawing.+# ---------------------------------------------------------------------------+++def draw_otto(f: Any, pose: Any) -> None:+ state, i = pose.state, pose.i+ if pose.act:+ _draw_act(f, pose)+ elif state == "sleeping":+ _float(f, pose, mode="sleep", rise=bp.SLEEP_BREATH[i % 6], bob=0)+ elif state == "held":+ _dangle(f, pose)+ elif state == "success":+ _draw_success(f, pose)+ elif state == "working":+ _draw_working(f, pose)+ elif state == "searching":+ _draw_searching(f, pose)+ else:+ _sit(f, pose, _look(pose))+++def _draw_success(f: Any, pose: Any) -> None:+ """A backflip: crouch, leap, tuck into a ball that turns over backwards,+ land, and hold the shell up high."""+ i = pose.i+ turns = _FLIP_TURNS[i]+ if turns is not None:+ _rolled(f, pose, turns)+ return+ if i < 7:+ look = _Look(+ ears=("up", "up"),+ items=(("clam", 21, 32),),+ paws=((20.0, 35.0), (28.0, 35.0)),+ tail=(1, 3) if 0 < i < 6 else (0, 0),+ feet=(1, 1) if 1 <= i <= 5 else (0, 0),+ )+ if i in (0, 6):+ _crouch(f, pose, look)+ else:+ _sit(f, pose, look)+ return+ # Landed: the shell goes up in one paw, the other on the belly.+ look = _Look(+ ears=("up", "up"),+ paws=((20.5, 33.0), (36.5, 13.5 + (i - 6))),+ front=(("clam", 34, 5 + (i - 6)),),+ tail=(2, 4 - i % 2 * 2),+ )+ _sit(f, pose, look)+++def _draw_working(f: Any, pose: Any) -> None:+ """Typing on the laptop: the arms reach down behind the lid; the paws+ that tap the keys are drawn with the effect, in front."""+ _sit(f, pose, _Look(paws=(None, None)))+ with f.offset(pose.dx, pose.dy):+ for shoulder, end in ((_SHOULDERS[0], (16, 38)), (_SHOULDERS[1], (31, 38))):+ _arm(f, shoulder, end, _sit_body())+++def _draw_searching(f: Any, pose: Any) -> None:+ """One paw holds the magnifier's handle and sweeps it along: the left+ paw while it is on the left, the right one after; the other paw rests."""+ sweep = bp.SEARCH_SWEEP[pose.i % len(bp.SEARCH_SWEEP)]+ side = 0 if sweep < 0 else 1+ paws = (None, _REST_PAWS[1]) if side == 0 else (_REST_PAWS[0], None)+ _sit(f, pose, _Look(paws=paws, whiskers=1))+ hx, hy = _MAG[0] + sweep + 6, _MAG[1] + 7+ with f.offset(pose.dx, pose.dy):+ _arm(f, _SHOULDERS[side], (hx - 1, hy - 1), _sit_body())+++# ---------------------------------------------------------------------------+# Effects.+# ---------------------------------------------------------------------------++_MAG: Px = (CX, 32)+++def _listening_fx(f: Any, pose: Any) -> None:+ with f.offset(pose.dx, pose.dy):+ bp.fx_arcs(f, pose.i, (8, 11), (39, 11))+++def _tink(f: Any, x: int, y: int) -> None:+ """The click of pebble on shell: a little white burst."""+ f.paint({(x - 3, y - 1), (x - 4, y - 2), (x + 3, y - 1), (x + 4, y - 2)}, bp.FX_WHITE)+ f.paint({(x, y - 4), (x, y - 5), (x - 2, y - 3), (x + 2, y - 3)}, bp.SPARK)+++def _thinking_fx(f: Any, pose: Any) -> None:+ """Thought dots orbit overhead; each strike of the pebble goes *tink*."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ bp.fx_dots(f, i, 19, 3)+ if _THINK_PAW[i] == "hit":+ _tink(f, 23, 33)+++def _talking_fx(f: Any, pose: Any) -> None:+ """Little bubbles pop up beside the head, more the louder it talks."""+ i = pose.i+ spots = ((10, 24), (37, 24), (8, 17), (39, 17), (11, 10), (36, 10))+ with f.offset(pose.dx, pose.dy):+ for x, y in spots[: 2 * i]:+ f.glyph(x - 1, y - 1, _BUBBLE, _water_palette())+++#: Success: speed lines on the way up and down, then sparkles round the shell.+def _success_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if 1 <= i <= 4:+ for x in (8, 39):+ y0 = 22 + pose.dy+ f.paint(bp.line(x, y0 + 4, x, y0 + 10) & bp.rect(0, 0, 47, 47), bp.ARC_FADE)+ if i in (0, 6):+ for x in (6, 9, 38, 41):+ f.paint({(x, 46), (x + 1, 45)}, bp.ARC_FADE)+ bp.fx_sparkles(f, i, ((31, 4, 5), (44, 9, 6), (6, 16, 6), (42, 22, 7)))+++#: Error: where the dropped clam is per frame (glyph top-left), and whether+#: it lies upside down.+_DROP_PATH: tuple[tuple[int, int, bool] | None, ...] = (+ None,+ (22, 35, False),+ (25, 41, True),+ (30, 37, True),+ (34, 41, False),+ (35, 41, False),+)+++def _error_fx(f: Any, pose: Any) -> None:+ """The clam slips from the paws, bounces off the floor and rolls away; a+ red "!" and a sweat drop."""+ i = pose.i+ spot = _DROP_PATH[i]+ if spot is not None:+ x, y, upside = spot+ rows = tuple(reversed(_CLAM)) if upside else _CLAM+ f.glyph(x, y, rows, _clam_palette())+ if i == 2:+ f.paint({(23, 46), (22, 45), (33, 46), (34, 45)}, bp.ARC_FADE)+ bp.fx_bang(f, 40, 3)+ if i >= 1:+ with f.offset(pose.dx, 0):+ bp.fx_sweat(f, i - 1, 34, 9)+++def _sleeping_fx(f: Any, pose: Any) -> None:+ bp.fx_zzz(f, pose.i, 27, 21)+++#: Working: which paw is down on a key per frame (0 left, 1 right, None both up).+_TYPE = (0, 1, 0, None, 1, 0, 1, None)+++def _working_fx(f: Any, pose: Any) -> None:+ i = pose.i+ with f.offset(0, pose.dy):+ bp.fx_laptop(f, i, 17, 34)+ down = _TYPE[i]+ for side, x in enumerate((16.0, 32.0)):+ up = 0 if down == side else 1+ _paw(f, x, 40.5 - up, 2.4, 1.9)+++def _searching_fx(f: Any, pose: Any) -> None:+ i = pose.i+ sweep = bp.SEARCH_SWEEP[i % len(bp.SEARCH_SWEEP)]+ with f.offset(pose.dx, pose.dy):+ bp.fx_magnifier(f, i, *_MAG)+ _paw(f, _MAG[0] + sweep + 6.5, _MAG[1] + 6.5, 2.4, 2.1)+++def _held_fx(f: Any, pose: Any) -> None:+ """Swing streaks and water dripping off the kicking feet."""+ i = pose.i+ with f.offset(pose.dx, 0):+ bp.fx_held(f, i, (9, 22), (38, 22))+ for x, start in ((15, 0), (33, 3)):+ age = (i - start) % 6+ if age < 3:+ f.glyph(x, 41 + 2 * age, _DRIP, {"#": OTTO_WATER_LIGHT})+++# ---------------------------------------------------------------------------+# Idle acts.+# ---------------------------------------------------------------------------+++def _act_pose(name: str, i: int) -> Any:+ eyes, mouth, dx, dy = "open", "rest", 0, 0+ if name == "clam_crack":+ eyes = ("down", "up_right", "down", "up_right", "down", "wide", "happy", "happy")[i]+ mouth = ("rest", "rest", "closed", "rest", "closed", "o", "munch", "smile")[i]+ elif name == "roll_over":+ eyes = ("open", "closed", "happy", "happy", "happy", "closed", "dizzy", "open")[i]+ mouth = ("rest", "closed", "smile", "smile", "smile", "closed", "smile", "rest")[i]+ dy = (0, 1, 0, 0, 0, 1, 0, 0)[i]+ elif name == "juggle_pebble":+ eyes = ("down", "up_left", "up_left", "up_right", "down", "up_right", "up_left", "open")[i]+ mouth = ("rest", "rest", "o", "rest", "smile", "rest", "o", "smile")[i]+ elif name == "whisker_wash":+ eyes = ("open", "closed", "closed", "closed", "closed", "closed", "happy", "open")[i]+ mouth = ("rest", "closed", "closed", "closed", "closed", "smile", "smile", "rest")[i]+ elif name == "belly_float":+ eyes = ("open", "squint", "happy", "open", "open", "happy", "squint", "open")[i]+ mouth = ("rest", "closed", "smile", "rest", "munch", "smile", "closed", "smile")[i]+ elif name == "splash":+ eyes = ("open", "open", "squint", "happy", "closed", "closed", "happy", "open")[i]+ mouth = ("rest", "smile", "open", "smile", "closed", "closed", "smile", "rest")[i]+ dx = (0, 0, 0, 0, -1, 1, 0, 0)[i]+ return bp.Pose("idle", i, dx=dx, dy=dy, eyes=eyes, mouth=mouth, act=name)+++#: Juggling: the pebble's top-left per frame and which paw is up.+_JUGGLE_PEBBLE: tuple[Px, ...] = (+ (18, 29),+ (17, 19),+ (22, 3),+ (28, 15),+ (27, 29),+ (28, 18),+ (23, 4),+ (18, 29),+)+_JUGGLE_PAWS: tuple[tuple[FPx, FPx], ...] = (+ ((19.5, 31.5), (27.0, 31.5)),+ ((18.0, 26.0), (27.0, 31.5)),+ ((19.5, 31.5), (27.0, 31.5)),+ ((19.5, 31.5), (29.0, 30.5)),+ ((19.5, 31.5), (28.5, 31.5)),+ ((19.5, 31.5), (30.0, 26.0)),+ ((19.5, 31.5), (27.0, 31.5)),+ ((19.5, 31.5), (27.0, 31.5)),+)+#: Whisker wash: both paws rub round the cheeks.+_WASH_PAWS: tuple[tuple[FPx, FPx], ...] = (+ _REST_PAWS,+ ((16.5, 24.5), (31.5, 24.5)),+ ((15.5, 21.5), (32.5, 23.5)),+ ((16.5, 19.5), (31.5, 21.5)),+ ((15.5, 22.5), (32.5, 19.5)),+ ((16.5, 24.5), (31.5, 22.5)),+ _CHIN_PAWS,+ _REST_PAWS,+)+_CRACK_PAW: tuple[FPx, ...] = (+ (28.0, 31.0),+ (31.5, 25.5),+ (27.0, 32.5),+ (31.5, 25.5),+ (27.0, 32.5),+ (29.0, 33.0),+ (27.5, 26.0),+ (27.0, 31.5),+)+++def _act_look(pose: Any) -> _Look | None:+ """The sitting look of an act frame, or None when the act draws it otherwise."""+ name, i = pose.act, pose.i+ if name == "clam_crack":+ if i < 5:+ paw = _CRACK_PAW[i]+ return _Look(+ paws=((18.5, 37.5), paw),+ items=(("clam", 20, 33),),+ front=(("pebble", math.floor(paw[0]) - 2, math.floor(paw[1]) - 3),),+ )+ if i < 7:+ return _Look(+ paws=((18.5, 37.5), _CRACK_PAW[i]),+ items=(("clam_open", 20, 32),),+ whiskers=1 if i == 6 else 0,+ )+ return _Look(whiskers=1)+ if name == "juggle_pebble":+ x, y = _JUGGLE_PEBBLE[i]+ return _Look(paws=_JUGGLE_PAWS[i], front=(("pebble", x, y),), tail=(i % 3 - 1, 1))+ if name == "whisker_wash":+ return _Look(paws=_WASH_PAWS[i], whiskers=(0, -1, 1, -1, 1, -1, 1, 0)[i])+ if name == "splash":+ tail = ((0, 0), (1, 7), (3, -2), (2, 0), (1, 1), (1, 1), (0, 1), (0, 0))[i]+ ears = ("flat", "flat") if i in (4, 5) else ("rest", "rest")+ paws = _CHIN_PAWS if i in (2, 3) else _REST_PAWS+ return _Look(tail=tail, ears=ears, paws=paws, whiskers=(0, 0, 1, 1, -1, 1, 0, 0)[i])+ if name == "roll_over":+ if i in (0, 7):+ return _Look()+ if i == 6:+ return _Look(ears=("flick", "rest"), whiskers=1)+ return None+ if name == "belly_float" and i in (0, 7):+ return _Look(feet=(0, 0), whiskers=1 if i == 7 else 0)+ return None+++_ROLL_TURNS = (0, 0, 1, 2, 3, 0, 0, 0)+++def _draw_act(f: Any, pose: Any) -> None:+ look = _act_look(pose)+ if look is not None:+ _sit(f, pose, look)+ return+ if pose.act == "roll_over":+ _rolled(f, pose, _ROLL_TURNS[pose.i])+ elif pose.act == "belly_float":+ i = pose.i+ rise = (0, 0, 1, 1, 0, 0, 0, 0)[i]+ _float(f, pose, mode="clam", rise=rise, bob=(1, 0, 0, 1, 1, 0, 1, 1)[i])+++def _clam_crack_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if i in (2, 4):+ _tink(f, 23, 33)+ if i == 4:+ f.paint({(22, 35), (23, 36), (24, 35)}, bp.OUTLINE)+ if i == 5:+ bp.fx_sparkles(f, 1, ((14, 26, 0),))+ if i == 7:+ # The empty halves dropped beside it.+ f.glyph(36, 41, ("lll", "#d#"), _clam_palette())+ f.glyph(5, 42, ("#d#", "lll"), _clam_palette())+ bp.fx_sparkles(f, 0, ((13, 26, 0),))+++def _roll_over_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if 2 <= i <= 4:+ # Motion arcs round the rolling ball.+ f.paint({(6, 26 + i), (7, 24 + i), (40, 38 - i), (41, 36 - i)}, bp.ARC_FADE)+ f.paint({(10, 46), (11, 46), (36, 46), (37, 46)}, bp.ARC_FADE)+ if i == 6:+ bp.fx_dots(f, 2, 19, 4)+++def _juggle_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if i in (2, 6):+ x, y = _JUGGLE_PEBBLE[i]+ f.paint({(x - 2, y + 1), (x + 5, y + 1)}, bp.ARC_FADE)+ if i == 7:+ bp.fx_sparkles(f, 1, ((36, 20, 0),))+++def _wash_fx(f: Any, pose: Any) -> None:+ i = pose.i+ for x, y, start in ((9, 15, 1), (38, 13, 2), (11, 7, 3), (37, 6, 4)):+ age = i - start+ if 0 <= age <= 2:+ f.glyph(x - 1, y - 1 - 2 * age, _BUBBLE, _water_palette())+ if i == 6:+ bp.fx_sparkles(f, 1, ((10, 18, 0), (38, 17, 0)))+++def _float_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if i in (1, 6):+ _splash(f, CX, 44, big=True)+ if 2 <= i <= 5:+ bp.fx_sparkles(f, i, ((32, 20, 4),))+ if i == 7:+ for x, y in ((14, 9), (33, 9)):+ f.glyph(x, y, _DRIP, {"#": OTTO_WATER_LIGHT})+++def _splash(f: Any, x: int, y: int, *, big: bool) -> None:+ """A crown of water drops thrown up from ``(x, y)``."""+ spread = ((-10, -6), (-6, -11), (0, -14), (6, -11), (10, -6))+ if not big:+ spread = ((-4, -4), (0, -7), (4, -4))+ for dx, dy in spread:+ f.glyph(x + dx - 1, y + dy - 1, _DRIP, {"#": OTTO_WATER_LIGHT})+++def _splash_fx(f: Any, pose: Any) -> None:+ """Tail slap: a puddle behind it, a crown of drops where the tail lands,+ then a shake that throws the drops off the fur."""+ i = pose.i+ if 1 <= i <= 6:+ _water(f, bp.ellipse(40.0, 45.5, 6.5, 1.8))+ if i == 2:+ _splash(f, 41, 44, big=False)+ f.paint({(34, 43), (47 - 1, 43)}, bp.FX_WHITE)+ if i == 3:+ for x, y in ((35, 30), (40, 27), (45, 32), (31, 26)):+ f.glyph(x, y, _DRIP, {"#": OTTO_WATER_LIGHT})+ if i in (4, 5):+ side = -1 if i == 4 else 1+ for x, y in ((8, 22), (6, 32), (39, 20), (41, 30)):+ f.glyph(x + side, y + (i - 4) * 2, _DRIP, {"#": OTTO_WATER_LIGHT})+ f.paint({(11, 18), (10, 19), (36, 18), (37, 19)}, bp.ARC_FADE)+ if i == 6:+ bp.fx_sparkles(f, 1, ((40, 36, 0),))+++# ---------------------------------------------------------------------------+# The design.+# ---------------------------------------------------------------------------+++def design(module: Any) -> Any:+ global bp+ bp = module+ acts = (+ ("clam_crack", 7, _clam_crack_fx),+ ("roll_over", 8, _roll_over_fx),+ ("juggle_pebble", 8, _juggle_fx),+ ("whisker_wash", 7, _wash_fx),+ ("belly_float", 6, _float_fx),+ ("splash", 8, _splash_fx),+ )+ return bp.PetDesign(+ id="otto",+ name="Otto",+ description=(+ "A sea otter who cracks clams on its belly while it thinks"+ " and naps floating on its back."+ ),+ draw=draw_otto,+ pose=otto_pose,+ waist=41,+ anchors=bp.Anchors(+ arcs_left=(8, 11),+ arcs_right=(39, 11),+ dots=(19, 3),+ zzz=(27, 21),+ bang=(40, 3),+ sparkles=((31, 4, 5), (44, 9, 6), (6, 16, 6), (42, 22, 7)),+ laptop=(17, 34),+ magnifier=_MAG,+ ),+ fx={+ "listening": _listening_fx,+ "thinking": _thinking_fx,+ "talking": _talking_fx,+ "success": _success_fx,+ "error": _error_fx,+ "sleeping": _sleeping_fx,+ "working": _working_fx,+ "searching": _searching_fx,+ "held": _held_fx,+ },+ acts=tuple(+ bp.IdleAct(name, 8, fps, (lambda n: lambda i: _act_pose(n, i))(name), fx)+ for name, fps, fx in acts+ ),+ )diff --git a/scripts/pets/designs/pip.py b/scripts/pets/designs/pip.pynew file mode 100644index 0000000000..cc7ffbde89--- /dev/null+++ b/scripts/pets/designs/pip.py@@ -0,0 +1,909 @@+"""Pip: the penguin chick (a design module for ``scripts/pets/build_pets.py``).++A round little emperor chick in fluffy grey down: a dark cap with a fluffy+cowlick, a white face mask, big round black eyes with glints, a tiny orange+beak, short flippers and orange feet. Pip is curious and a bit clumsy: it+stands tall with its flippers out to listen, taps its beak while it thinks,+hops with its flippers up when something works, shivers with its down all+ruffled when something fails, pecks at a laptop while it works, peers through+a magnifier while it searches and naps sitting down with its beak tucked into+its fluff. Its idle acts are little scenes: a belly slide, a snowball that+lands on its own head, a fish snack, a flight attempt, a tumble and a preen.++``design(bp)`` gets the builder module as ``bp`` and every helper comes from+there; the module keeps a reference in ``bp`` for the drawing functions.+"""++import math+from collections.abc import Callable, Iterable+from dataclasses import dataclass+from typing import Any++#: The builder module (``scripts/pets/build_pets.py``), set by ``design``.+bp: Any = None++Px = tuple[int, int]+Pt = tuple[float, float]+RGBA = tuple[int, int, int, int]+Mask = set[Px]+++def _hex(value: str) -> RGBA:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++# -- Palette ------------------------------------------------------------------++DOWN = _hex("#9aa4b3")+DOWN_LIGHT = _hex("#c4ccd8")+DOWN_DARK = _hex("#6c7686")+#: The little V-shaped down marks on the body.+DOWN_TICK = _hex("#838d9d")+BELLY = _hex("#d2d8e2")+BELLY_DARK = _hex("#b2bbc8")+CAP = _hex("#3e4555")+CAP_LIGHT = _hex("#5f687b")+CAP_DARK = _hex("#2b303c")+MASK = _hex("#f7f8fb")+MASK_SHADE = _hex("#d5dbe6")+EYE = _hex("#17131d")+BEAK = _hex("#ffa63d")+BEAK_LIGHT = _hex("#ffd48a")+BEAK_DARK = _hex("#d9692b")+FOOT = _hex("#ff9b3d")+FOOT_LIGHT = _hex("#ffc27a")+FOOT_DARK = _hex("#cc6127")+BLUSH = _hex("#ffb0bf")+FLIP = _hex("#7f8999")+FLIP_LIGHT = _hex("#a8b2c0")+FLIP_DARK = _hex("#5c6576")+SNOW = _hex("#f4f8ff")+SNOW_SHADE = _hex("#c3d4ec")+ICE = _hex("#8fd0ff")+ICE_CORE = _hex("#e3f4ff")+FISH = _hex("#5aa6e6")+FISH_LIGHT = _hex("#a8dcff")+FISH_FIN = _hex("#2f6eb0")+HEART = _hex("#ff6f91")+HEART_SHINE = _hex("#ffd3de")+BUBBLE = _hex("#bfe3ff")++# -- Geometry -----------------------------------------------------------------++CX = 24+HEAD = (24.0, 18.5, 12.0, 10.0)+BODY = (24.0, 33.5, 13.0, 10.5)+#: The sitting body (sleeping, working and the tumble's plop): lower and wider.+SIT_BODY = (24.0, 36.0, 13.5, 9.0)+#: Top-left of each 5 x 6 eye box.+EYE_AT: tuple[Px, Px] = ((16, 16), (27, 16))+BEAK_AT: Px = (22, 23)+#: The left flipper's shoulder; the right one is mirrored.+SHOULDER: Pt = (14.5, 27.5)+#: Left flipper tips per name (the right flipper uses the mirrored tip).+FLIPPER_TIPS: dict[str, Pt] = {+ "rest": (10.5, 37.5),+ "half": (7.5, 33.5),+ "out": (4.5, 29.5),+ "up": (6.5, 18.5),+ "flap_up": (3.5, 21.5),+ "flap_down": (4.5, 37.5),+ "hug": (18.5, 34.5),+ "hug2": (18.5, 33.5),+ "front": (19.5, 36.5),+ "tap": (21.5, 25.5),+ "tap2": (21.5, 26.5),+ "preen": (9.5, 25.5),+ "key": (18.5, 37.5),+ "key2": (19.5, 36.5),+}+#: Where each eye style moves the whole eye.+LOOKS: dict[str, Px] = {+ "open": (0, 0),+ "wide": (0, 0),+ "look_left": (-1, 0),+ "look_right": (1, 0),+ "up_left": (-1, -1),+ "up_right": (1, -1),+ "up": (0, -1),+ "down": (0, 1),+}+#: Closed-eye shapes on the 5 x 6 eye box (the right eye is mirrored).+LIDS: dict[str, tuple[str, ...]] = {+ "closed": (".....", ".....", ".....", "#...#", ".###.", "....."),+ "sleep": (".....", ".....", ".....", "#...#", ".###.", "....."),+ "happy": (".....", ".###.", "#...#", ".....", ".....", "....."),+ "squint": (".....", "##...", "..##.", "....#", "..##.", "##..."),+ "x": (".....", "#...#", ".#.#.", "..#..", ".#.#.", "#...#"),+ "dizzy": (".###.", "#...#", "#.#.#", "#.##.", "#....", ".####"),+}+#: The fluffy cowlick on top of the head.+TUFT = ("...#.#", "..#.#.", "..##..")+++# -- One frame's drawing ------------------------------------------------------+++@dataclass(frozen=True)+class Cell:+ """One frame: the pose fields plus the parts only Pip moves."""++ dx: int = 0+ dy: int = 0+ eyes: str = "open"+ beak: str = "rest"+ stretch: int = 0+ #: Left and right flipper: a tip name, or a tip point in left-flipper+ #: coordinates (the right flipper mirrors it).+ flips: tuple[str | Pt, str | Pt] = ("rest", "rest")+ #: The head's offset from the body.+ head: Px = (0, 0)+ #: How far each foot is lifted.+ feet: Px = (0, 0)+ #: 0 smooth down, 1 ruffled (spiky tufts), 2 puffed up round.+ fluff: int = 0+ #: "stand", "sit", "plop" (sitting, feet out front), "dangle" or "prone".+ view: str = "stand"+ #: The body's centre x while lying on the belly (``view == "prone"``).+ slide: int = 24+ snow: bool = False+ #: The beak tucked into the fluff (asleep).+ tuck: bool = False+++_C = Cell+_UP = ("up", "up")+_OUT = ("out", "out")+_HALF = ("half", "half")+++def _in_cell(mask: Iterable[Px], margin: int = 1) -> Mask:+ """``mask`` clipped so that it and its outline stay inside the cell."""+ hi = bp.CELL - 1 - margin+ return {(x, y) for x, y in mask if margin <= x <= hi and margin <= y <= hi}+++def _down(f: Any, mask: Mask, depth: int = 2) -> None:+ f.part(mask, bp.shade(mask, DOWN, light=DOWN_LIGHT, dark=DOWN_DARK, dark_depth=depth))+++def _tufts(cx: float, cy: float, rx: float, ry: float, angles: Iterable[int], size: float) -> Mask:+ """Little spiky tufts of down poking out of an ellipse at ``angles`` (degrees)."""+ out: Mask = set()+ for deg in angles:+ a = math.radians(deg)+ b1, b2 = math.radians(deg - 9), math.radians(deg + 9)+ tip = (cx + math.cos(a) * (rx + size), cy + math.sin(a) * (ry + size))+ out |= bp.polygon(+ [+ (cx + math.cos(b1) * (rx - 1), cy + math.sin(b1) * (ry - 1)),+ tip,+ (cx + math.cos(b2) * (rx - 1), cy + math.sin(b2) * (ry - 1)),+ ]+ )+ return out+++def _body_mask(cx: float, cy: float, rx: float, ry: float, fluff: int) -> Mask:+ """The round body with a scalloped, fluffy rim along its sides and bottom."""+ if fluff == 2:+ rx, ry = rx + 1.0, ry + 0.5+ body = bp.ellipse(cx, cy, rx, ry)+ for deg in range(-10, 191, 25):+ a = math.radians(deg)+ body |= bp.ellipse(cx + math.cos(a) * rx * 0.97, cy + math.sin(a) * ry * 0.95, 1.7, 1.5)+ if fluff == 1:+ body |= _tufts(cx, cy, rx, ry, (-150, -125, -55, -30, 10, 60, 120, 170), 2.4)+ return body+++def _down_ticks(f: Any, body: Mask, cx: float, cy: float) -> None:+ """A few soft V-marks of down on the body."""+ marks: Mask = set()+ for mx, my in ((-8, -2), (7, -3), (-6, 5), (9, 4), (-10, 3)):+ x, y = int(cx) + mx, int(cy) + my+ marks |= {(x - 1, y), (x, y + 1), (x + 1, y)}+ f.paint(marks & bp.eroded(body, 2), DOWN_TICK)+++def _belly(f: Any, cx: float, cy: float, rx: float, ry: float, body: Mask) -> None:+ belly = bp.ellipse(cx, cy, rx, ry) & bp.eroded(body, 1)+ f.paint_map(bp.shade(belly, BELLY, dark=BELLY_DARK, dark_depth=1))+++def _flipper(f: Any, tip: str | Pt, *, right: bool, root: Pt = SHOULDER) -> None:+ """A short tapered flipper from ``root`` to ``tip``, outlined everywhere but+ where it grows out of the body."""+ tx, ty = FLIPPER_TIPS[tip] if isinstance(tip, str) else tip+ rx, ry = root+ ex, ey = tx - rx, ty - ry+ length = math.hypot(ex, ey) or 1.0+ nx, ny = -ey / length, ex / length+ mask = bp.polygon(+ [+ (rx + nx * 2.4, ry + ny * 2.4),+ (tx + nx * 1.3, ty + ny * 1.3),+ (tx - nx * 1.3, ty - ny * 1.3),+ (rx - nx * 2.4, ry - ny * 2.4),+ ]+ ) | bp.ellipse(tx, ty, 1.5, 1.5)+ joint = bp.ellipse(rx, ry, 2.6, 2.6)+ if right:+ mask, joint = bp.mirrored(mask), bp.mirrored(joint)+ f.paint(bp.outer_ring(mask) - joint, bp.OUTLINE)+ f.paint_map(bp.shade(mask, FLIP, light=FLIP_LIGHT, dark=FLIP_DARK, dark_depth=1))+++def _flippers(f: Any, c: Cell, root: Pt = SHOULDER) -> None:+ _flipper(f, c.flips[0], right=False, root=root)+ _flipper(f, c.flips[1], right=True, root=root)+++def _feet(f: Any, lift: Px = (0, 0), spread: int = 0) -> None:+ """Two orange feet in front of the fluff, toes split by darker lines."""+ for side, up in enumerate(lift):+ foot = bp.ellipse(18.5 - spread, 44.5 - up, 3.8, 2.1)+ y = 45 - up+ toes = {(17 - spread, y), (19 - spread, y), (16 - spread, y + 1), (20 - spread, y + 1)}+ light = {(16 - spread, 43 - up), (17 - spread, 43 - up), (18 - spread, 43 - up)}+ if side:+ foot, toes, light = bp.mirrored(foot), bp.mirrored(toes), bp.mirrored(light)+ f.part(foot, FOOT)+ f.paint(light & foot, FOOT_LIGHT)+ f.paint((toes & foot) - light, FOOT_DARK)+++def _soles(f: Any) -> None:+ """Both feet stuck out in front, soles to the viewer (sitting with a plop)."""+ for side in (0, 1):+ sole = bp.ellipse(17.5, 42.5, 2.6, 3.2)+ toes = {(16, 40), (18, 40)}+ if side:+ sole, toes = bp.mirrored(sole), bp.mirrored(toes)+ f.part(sole, bp.shade(sole, FOOT, light=FOOT_LIGHT, dark=FOOT_DARK, dark_depth=1))+ f.paint(toes, FOOT_DARK)+++def _eyes(f: Any, style: str) -> None:+ for side, (x, y) in enumerate(EYE_AT):+ if style in LIDS:+ rows = LIDS[style]+ if side:+ rows = tuple(r[::-1] for r in rows)+ f.paint(bp.from_rows(x, y, rows), bp.OUTLINE)+ continue+ lx, ly = LOOKS[style]+ x, y = x + lx, y + ly+ top, h = (y - 1, 7) if style == "wide" else (y, 6)+ oval = bp.rect(x, top, x + 4, top + h - 1)+ oval -= {(x, top), (x + 4, top), (x, top + h - 1), (x + 4, top + h - 1)}+ f.paint(oval, EYE)+ f.paint(bp.rect(x + 2, top + 1, x + 3, top + 2), bp.FX_WHITE)+ f.paint({(x + 1, top + h - 2)}, bp.FX_WHITE)+++def _beak(f: Any, style: str) -> None:+ """The tiny beak; talking opens it from closed to widest."""+ x, y = BEAK_AT+ dark = bp.MOUTH_DARK+ if style == "tuck":+ f.paint(bp.rect(x + 1, y, x + 2, y), BEAK)+ f.paint({(x + 1, y)}, BEAK_LIGHT)+ return+ if style in ("rest", "closed", "smile", "fish"):+ f.paint(bp.rect(x, y, x + 3, y), BEAK)+ f.paint({(x, y)}, BEAK_LIGHT)+ f.paint({(x + 1, y + 1)}, BEAK)+ f.paint({(x + 2, y + 1)}, BEAK_DARK)+ return+ if style == "half":+ f.paint(bp.rect(x, y, x + 3, y), BEAK)+ f.paint({(x, y)}, BEAK_LIGHT)+ f.paint(bp.rect(x, y + 1, x + 3, y + 1), dark)+ f.paint(bp.rect(x + 1, y + 2, x + 2, y + 2), BEAK_DARK)+ return+ if style == "open":+ f.paint(bp.rect(x - 1, y, x + 4, y), BEAK)+ f.paint({(x - 1, y)}, BEAK_LIGHT)+ f.paint(bp.rect(x, y + 1, x + 3, y + 2), dark)+ f.paint(bp.rect(x + 1, y + 2, x + 2, y + 2), bp.TONGUE)+ f.paint(bp.rect(x, y + 3, x + 3, y + 3), BEAK)+ f.paint({(x + 3, y + 3)}, BEAK_DARK)+ return+ if style == "wide":+ f.paint(bp.rect(x - 1, y, x + 4, y), BEAK)+ f.paint({(x - 1, y)}, BEAK_LIGHT)+ f.paint(bp.rect(x - 1, y + 1, x + 4, y + 3) - {(x - 1, y + 3), (x + 4, y + 3)}, dark)+ f.paint(bp.rect(x, y + 3, x + 3, y + 3), bp.TONGUE)+ f.paint(bp.rect(x, y + 4, x + 3, y + 4), BEAK)+ f.paint({(x + 3, y + 4)}, BEAK_DARK)+ return+ raise KeyError(style)+++def _head_mask(fluff: int) -> Mask:+ head = bp.ellipse(*HEAD) | bp.from_rows(19, 6, TUFT)+ if fluff == 1:+ head |= _tufts(HEAD[0], HEAD[1], HEAD[2], HEAD[3], (-160, -125, -55, -20, 160, 20), 2.2)+ return head+++def _face_mask(head: Mask) -> Mask:+ eye = bp.ellipse(18.5, 19.0, 5.2, 5.0)+ face = eye | bp.mirrored(eye) | bp.ellipse(CX, 23.5, 9.0, 5.0)+ return face & bp.eroded(head, 1) | (face & head & bp.rect(0, 25, 47, 47))+++def _head(f: Any, c: Cell, eyes: str, beak: str) -> None:+ """The dark cap with its cowlick, the white face mask and the face."""+ head = _head_mask(c.fluff)+ f.part(head, bp.shade(head, CAP, light=CAP_LIGHT, dark=CAP_DARK, dark_depth=1))+ face = _face_mask(bp.ellipse(*HEAD))+ f.paint_map(bp.shade(face, MASK, dark=MASK_SHADE, dark_depth=1))+ cheek = bp.rect(15, 23, 17, 23)+ f.paint(cheek | bp.mirrored(cheek), BLUSH)+ _eyes(f, eyes)+ _beak(f, "tuck" if c.tuck else beak)+ if c.snow:+ _snow_cap(f)+++def _snow_cap(f: Any) -> None:+ """A dollop of snow sitting on the head after the snowball came down."""+ pile = bp.ellipse(CX, 9.5, 7.5, 3.2) | bp.ellipse(18.5, 11.5, 2.2, 1.6)+ pile |= bp.ellipse(30.0, 12.0, 1.6, 1.8)+ f.part(pile, bp.shade(pile, SNOW, dark=SNOW_SHADE, dark_depth=1))+++def _collar(f: Any, dy: int) -> None:+ """Puffs of chest fluff pulled up over the chin: the beak tucked in."""+ collar: Mask = set()+ for x in (17.0, 20.5, 24.0, 27.5, 31.0):+ collar |= bp.ellipse(x, 26.5 + dy + (0.5 if x in (17.0, 31.0) else 0.0), 2.6, 2.0)+ f.part(collar, bp.shade(collar, DOWN_LIGHT, dark=DOWN, dark_depth=1))+++# -- Views --------------------------------------------------------------------+++def _draw_stand(f: Any, pose: Any, c: Cell) -> None:+ with f.offset(pose.dx, pose.dy):+ body = _body_mask(*BODY, c.fluff)+ _down(f, body)+ _belly(f, CX, 36.5, 8.0, 6.5, body)+ _down_ticks(f, body, BODY[0], BODY[1])+ with f.offset(*c.head):+ _head(f, c, pose.eyes, pose.mouth)+ _flippers(f, c)+ if c.view == "dangle":+ _dangling_feet(f, pose.i)+ if c.view != "dangle":+ with f.offset(pose.dx, min(pose.dy, 0)):+ _feet(f, c.feet)+++def _dangling_feet(f: Any, i: int) -> None:+ """Held: the feet hang down and kick in turn."""+ for side in (0, 1):+ kick = (i + side) % 2+ foot = bp.ellipse(19.0 - kick, 44.5 - kick, 2.8, 2.4)+ if side:+ foot = bp.mirrored(foot)+ f.part(foot, bp.shade(foot, FOOT, light=FOOT_LIGHT, dark=FOOT_DARK, dark_depth=1))+++def _draw_sit(f: Any, pose: Any, c: Cell) -> None:+ """Sitting down: a round ball of fluff, the head sunk a little lower."""+ with f.offset(pose.dx, pose.dy):+ if c.view == "plop":+ _soles(f)+ body = _body_mask(*SIT_BODY, c.fluff)+ _down(f, body)+ _belly(f, CX, 38.5, 8.5, 5.5, body)+ _down_ticks(f, body, SIT_BODY[0], SIT_BODY[1])+ if c.view == "plop":+ _soles(f)+ with f.offset(c.head[0], c.head[1]):+ _head(f, c, pose.eyes, pose.mouth)+ if c.tuck:+ _collar(f, c.head[1])+ _flippers(f, c, root=(14.0, 32.5))+++def _draw_prone(f: Any, pose: Any, c: Cell) -> None:+ """Lying on the belly and sliding along, facing right."""+ s = c.slide+ for fx, fy in ((s - 13.0, 40.5), (s - 12.0, 43.0)):+ foot = bp.ellipse(fx, fy, 2.6, 1.3)+ f.part(foot, bp.shade(foot, FOOT, light=FOOT_LIGHT, dark=FOOT_DARK, dark_depth=1))+ body = bp.ellipse(s, 38.0, 12.5, 7.0)+ for k in range(5):+ body |= bp.ellipse(s - 9.0 + 4.5 * k, 31.8 + (0.6 if k % 2 else 0.0), 1.8, 1.5)+ _down(f, body)+ under = {(x, y) for x, y in bp.eroded(body, 1) if y >= 41}+ f.paint(under, BELLY)+ head = bp.ellipse(s + 10.0, 33.5, 8.0, 7.5) | bp.from_rows(s + 7, 24, ("#.#", ".##"))+ f.part(head, bp.shade(head, CAP, light=CAP_LIGHT, dark=CAP_DARK, dark_depth=1))+ face = bp.ellipse(s + 12.5, 35.0, 5.5, 4.5) & bp.eroded(head, 1)+ f.paint_map(bp.shade(face, MASK, dark=MASK_SHADE, dark_depth=1))+ f.paint({(s + 10, 37), (s + 11, 37)}, BLUSH)+ ex, ey = s + 12, 31+ if pose.eyes == "happy":+ f.paint({(ex, ey + 2), (ex + 1, ey + 1), (ex + 2, ey + 1), (ex + 3, ey + 2)}, bp.OUTLINE)+ else:+ f.paint(bp.rect(ex, ey, ex + 2, ey + 3) - {(ex, ey), (ex, ey + 3)}, EYE)+ f.paint({(ex + 1, ey + 1)}, bp.FX_WHITE)+ bx, by = s + 18, 35+ if pose.mouth in ("open", "wide"):+ f.paint(bp.rect(bx, by - 1, bx + 2, by - 1), BEAK)+ f.paint(bp.rect(bx, by, bx + 1, by), bp.MOUTH_DARK)+ f.paint(bp.rect(bx, by + 1, bx + 1, by + 1), BEAK_DARK)+ else:+ f.paint(bp.rect(bx, by, bx + 2, by) | {(bx, by + 1), (bx + 1, by + 1)}, BEAK)+ f.paint({(bx + 1, by + 1)}, BEAK_DARK)+ flip = bp.polygon([(s + 3.5, 33.5), (s - 9.5, 31.5), (s - 9.0, 33.5), (s + 3.5, 36.5)])+ f.paint(bp.outer_ring(flip) - bp.ellipse(s + 3.5, 35.5, 2.0, 2.0), bp.OUTLINE)+ f.paint_map(bp.shade(flip, FLIP, light=FLIP_LIGHT, dark=FLIP_DARK, dark_depth=1))+++def draw_pip(f: Any, pose: Any) -> None:+ c = _cell(pose)+ if c.view == "prone":+ _draw_prone(f, pose, c)+ elif c.view in ("sit", "plop"):+ _draw_sit(f, pose, c)+ else:+ _draw_stand(f, pose, c)+++# -- States -------------------------------------------------------------------++_IDLE_EYES = ("open", "open", "open", "look_left", "look_left", "open", "look_right")+_IDLE_FLIPS: tuple[tuple[str, str], ...] = (+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "half"),+ ("rest", "rest"),+ ("rest", "rest"),+ ("rest", "rest"),+)+#: A tiny waddle: the weight shifts from foot to foot.+_IDLE_FEET: tuple[Px, ...] = ((0, 0), (0, 0), (1, 0), (0, 0), (0, 0), (0, 1), (0, 0))+_LISTEN_TILT = (0, 1, 1, 1, 1, 0)+_HOP = (0, -1, -3, -4, -3, -1, 0, 0)+_HOP_STRETCH = (-1, 1, 1, 0, 0, 1, -1, 0)+_SHIVER = (-1, 1, -1, 1, -1, 1)+_ERROR_EYES = ("wide", "squint", "squint", "x", "x", "squint")+_ERROR_BEAK = ("half", "closed", "half", "closed", "half", "closed")+_PECK = (0, 1, 2, 0, 0, 1, 2, 0)+_HELD_FLAP = (("flap_up", "flap_down"), ("flap_down", "flap_up")) * 3+++def _search_tip(sweep: int) -> Pt:+ """Left-flipper coordinates of the right flipper holding the magnifier."""+ hx = CX + sweep + 6+ return (2 * CX - 1 - hx + 0.5, 27.5)+++def _state_cell(state: str, i: int) -> Cell:+ if state == "idle":+ if i >= len(_IDLE_EYES):+ return _C(eyes="closed") # the blink (the accent cell)+ return _C(+ eyes=_IDLE_EYES[i],+ stretch=bp.IDLE_BREATH[i],+ flips=_IDLE_FLIPS[i],+ feet=_IDLE_FEET[i],+ )+ if state == "listening":+ return _C(dy=-1, eyes="wide", stretch=1, flips=_OUT, head=(_LISTEN_TILT[i], 0))+ if state == "thinking":+ tap = "tap2" if i % 4 in (1, 2) else "tap"+ return _C(eyes="up_left" if i < 4 else "up_right", beak="closed", flips=("rest", tap))+ if state == "talking":+ # A flipper lifts to gesture from the first frame, so talking never reads as idle.+ flips = (("rest", "half"), ("rest", "half"), ("half", "rest"), _HALF)[i]+ return _C(dy=0 if i < 2 else -1, beak=bp.TALK_MOUTHS[i], flips=flips)+ if state == "success":+ air = _HOP[i] <= -1+ return _C(+ dy=_HOP[i],+ eyes="happy",+ beak="open" if 1 <= i <= 5 else "smile",+ stretch=_HOP_STRETCH[i],+ flips=_UP if air else _HALF,+ feet=(1, 1) if _HOP[i] <= -3 else (0, 0),+ )+ if state == "error":+ return _C(+ dx=_SHIVER[i],+ eyes=_ERROR_EYES[i],+ beak=_ERROR_BEAK[i],+ flips=("hug", "hug2") if i % 2 else ("hug2", "hug"),+ fluff=1,+ )+ if state == "sleeping":+ return _C(+ dy=1,+ eyes="sleep",+ beak="closed",+ stretch=bp.SLEEP_BREATH[i],+ flips=("front", "front"),+ head=(0, 4),+ view="sit",+ tuck=True,+ )+ if state == "working":+ key = ("key", "key2") if i % 2 else ("key2", "key")+ return _C(eyes="down", beak="closed", flips=key, head=(0, 3 + _PECK[i]), view="sit")+ if state == "searching":+ sweep = bp.SEARCH_SWEEP[i]+ lean = (sweep > 2) - (sweep < -2)+ flips = ("rest", _search_tip(sweep))+ return _C(dx=lean, eyes=bp.SEARCH_EYES[i], beak="closed", flips=flips)+ if state == "held":+ return _C(+ dx=bp.HELD_SWAY[i],+ dy=-2,+ eyes="wide",+ beak="half",+ stretch=1,+ flips=_HELD_FLAP[i],+ view="dangle",+ )+ raise KeyError(state)+++# -- Idle acts ----------------------------------------------------------------++ACTS: dict[str, tuple[Cell, ...]] = {+ #: A run-up, a hop, a belly flop and a slide across the floor: wheee.+ "belly_slide": (+ _C(eyes="look_right"),+ _C(dy=1, eyes="look_right", stretch=-1, flips=_HALF, feet=(0, 0)),+ _C(dx=1, dy=-2, eyes="happy", beak="open", stretch=1, flips=_UP, feet=(1, 1)),+ _C(eyes="happy", beak="open", view="prone", slide=17),+ _C(eyes="happy", beak="wide", view="prone", slide=20),+ _C(eyes="happy", beak="open", view="prone", slide=23),+ _C(eyes="open", beak="closed", view="prone", slide=25),+ _C(dx=1, eyes="happy", beak="smile", flips=_HALF),+ ),+ #: A snowball tossed up high that comes straight back down on the head.+ "snowball": (+ _C(eyes="down"),+ _C(eyes="down", beak="closed", flips=("front", "front")),+ _C(dy=-1, eyes="up", beak="closed", stretch=1, flips=_UP),+ _C(eyes="up", beak="half", flips=_HALF),+ _C(eyes="wide", beak="open", flips=_HALF),+ _C(dy=1, eyes="squint", beak="closed", stretch=-1, flips=_OUT, snow=True),+ _C(dx=-1, eyes="closed", beak="half", flips=_HALF, snow=True, fluff=2),+ _C(dx=1, eyes="happy", beak="smile"),+ ),+ #: A fish comes flying in, Pip catches it and gulps it down.+ "fish_snack": (+ _C(eyes="look_left"),+ _C(eyes="up_left", beak="closed", flips=("half", "rest")),+ _C(eyes="up_left", beak="open", stretch=1, flips=_HALF),+ _C(dy=-1, eyes="up", beak="wide", stretch=1, flips=_OUT),+ _C(eyes="happy", beak="fish", flips=_HALF),+ _C(eyes="happy", beak="closed", stretch=1, fluff=2),+ _C(dy=-2, eyes="happy", beak="open", flips=_UP, feet=(1, 1)),+ _C(eyes="happy", beak="smile"),+ ),+ #: Flap, flap, flap: an attempt at flying that ends in a bump.+ "flipper_flap": (+ _C(eyes="up"),+ _C(eyes="wide", beak="closed", flips=_UP),+ _C(dy=-1, eyes="wide", beak="closed", stretch=1, flips=("flap_down", "flap_down")),+ _C(dy=-3, eyes="happy", beak="open", flips=("flap_up", "flap_up"), feet=(1, 1)),+ _C(dy=-4, eyes="happy", beak="open", flips=("flap_down", "flap_down"), feet=(2, 2)),+ _C(dy=-2, eyes="wide", beak="half", flips=_UP, feet=(1, 1)),+ _C(dy=1, eyes="squint", beak="closed", stretch=-1, flips=_OUT),+ _C(eyes="look_right", beak="closed", flips=_HALF),+ ),+ #: A clumsy waddle, a trip and a plop onto its bottom, a little dizzy.+ "tumble": (+ _C(),+ _C(dx=-1, flips=("half", "rest"), head=(-1, 0), feet=(0, 1)),+ _C(dx=1, flips=("rest", "half"), head=(1, 0), feet=(1, 0)),+ _C(dx=2, eyes="wide", beak="half", flips=("up", "out"), head=(2, 0), feet=(0, 2)),+ _C(dy=1, eyes="squint", beak="half", stretch=-1, flips=_OUT, head=(0, 3), view="plop"),+ _C(dy=1, eyes="dizzy", beak="closed", flips=_HALF, head=(0, 3), view="plop"),+ _C(dy=1, eyes="look_left", beak="closed", flips=_HALF, head=(0, 3), view="plop"),+ _C(eyes="happy", beak="smile", flips=_HALF),+ ),+ #: A careful preen of the shoulder fluff, then a big fluffy shake.+ "preen": (+ _C(eyes="look_left"),+ _C(eyes="look_left", beak="closed", flips=("half", "rest"), head=(-1, 1)),+ _C(eyes="closed", beak="closed", flips=("preen", "rest"), head=(-3, 2)),+ _C(eyes="closed", beak="half", flips=("preen", "rest"), head=(-3, 3)),+ _C(eyes="closed", beak="closed", flips=("preen", "rest"), head=(-3, 2)),+ _C(dx=-1, eyes="closed", beak="closed", flips=_HALF, fluff=2),+ _C(dx=1, eyes="happy", beak="smile", flips=_HALF, fluff=2),+ _C(eyes="happy", beak="smile"),+ ),+}+++def _cell(pose: Any) -> Cell:+ if pose.act:+ return ACTS[pose.act][pose.i]+ return _state_cell(pose.state, pose.i)+++def _pose_of(state: str, i: int, c: Cell, act: str = "") -> Any:+ return bp.Pose(+ state, i, dx=c.dx, dy=c.dy, eyes=c.eyes, mouth=c.beak, stretch=c.stretch, act=act+ )+++def pip_pose(state: str, i: int) -> Any:+ return _pose_of(state, i, _state_cell(state, i))+++# -- Effects ------------------------------------------------------------------++_SNOWFLAKE = ("#.#", ".o.", "#.#")+_SNOWFLAKE_PALETTE = {"#": ICE, "o": ICE_CORE}+_HEART = ("#.#", "###", ".#.")+_HEART_L = ("##.##", "#o###", "#####", ".###.", "..#..")+_HEART_PALETTE = {"#": HEART, "o": HEART_SHINE}+_FISH = ("#..###.", "##.##e#", ".######", "##.###.", "#..##..")+_FISH_PALETTE = {"#": FISH, "e": EYE}+_STAR = (".#.", "#o#", ".#.")+++def _flake(f: Any, x: int, y: int) -> None:+ f.glyph(x - 1, y - 1, _SNOWFLAKE, _SNOWFLAKE_PALETTE)+++def _fluff_bit(f: Any, x: int, y: int) -> None:+ if 1 <= x < bp.CELL - 2 and 1 <= y < bp.CELL - 1:+ f.part({(x, y), (x + 1, y)}, DOWN_LIGHT)+++def _snow_puff(f: Any, x: float, y: float, r: float) -> None:+ puff = _in_cell(bp.ellipse(x, y, r, r * 0.8))+ if puff:+ f.part(puff, bp.shade(puff, SNOW, dark=SNOW_SHADE, dark_depth=1))+++def _talking_fx(f: Any, pose: Any) -> None:+ """Loud cheeps: little lines spring from the beak."""+ if pose.i < 2:+ return+ with f.offset(pose.dx, pose.dy):+ near = {(10, 22), (9, 23), (10, 25), (9, 25)}+ f.paint(near | bp.mirrored(near), bp.ARC)+ if pose.i == 3:+ far = {(7, 20), (6, 21), (7, 26), (6, 27)}+ f.paint(far | bp.mirrored(far), bp.ARC_FADE)+++def _success_fx(f: Any, pose: Any) -> None:+ """Sparkles and a flurry of snowflakes around the happy hop."""+ bp.fx_sparkles(f, pose.i, ((6, 12, 1), (41, 7, 3), (5, 27, 4)))+ for x, y, start in ((40, 24, 0), (9, 5, 2), (42, 32, 4)):+ age = pose.i - start+ if 0 <= age <= 3:+ _flake(f, x, y + age)+++def _error_fx(f: Any, pose: Any) -> None:+ """A red "!", shiver lines on both sides and a cold little breath cloud."""+ i = pose.i+ bp.fx_bang(f, 41, 3)+ side = (6, 32) if i % 2 else (4, 30)+ lines = bp.line(side[0], side[1], side[0], side[1] + 4)+ lines |= bp.line(side[0] - 2, side[1] + 1, side[0] - 2, side[1] + 3)+ f.paint(lines | bp.mirrored(lines), bp.ARC_FADE)+ if i in (1, 3, 5):+ _snow_puff(f, 38.0 + pose.dx, 25.0, 1.3)+ elif i in (2, 4):+ _snow_puff(f, 40.0 + pose.dx, 23.0, 1.9)+ if i >= 2:+ _flake(f, 7, 12 + i)+++def _sleeping_fx(f: Any, pose: Any) -> None:+ """Rising z marks and a sleepy bubble that swells and pops."""+ bp.fx_zzz(f, pose.i, 34, 15)+ i = pose.i+ size = (1.0, 1.6, 2.2, 2.8, 0.0, 0.0)[i]+ if size:+ ring = bp.ellipse(29.5, 28.0, size, size)+ f.paint(ring - bp.eroded(ring), BUBBLE)+ f.paint({(29, 27)} if size > 1.5 else set(), bp.FX_WHITE)+ elif i == 4:+ f.paint({(27, 25), (32, 25), (29, 23), (31, 30)}, BUBBLE)+++def _working_fx(f: Any, pose: Any) -> None:+ bp.fx_laptop(f, pose.i, 17, 35)+++def _searching_fx(f: Any, pose: Any) -> None:+ """The magnifier held up in front of the eyes, sweeping left and right."""+ with f.offset(pose.dx, 0):+ bp.fx_magnifier(f, pose.i, CX, 19)+++def _held_fx(f: Any, pose: Any) -> None:+ with f.offset(pose.dx, pose.dy):+ bp.fx_held(f, pose.i, (8, 21), (39, 21))+ if pose.i % 2 == 0:+ _fluff_bit(f, 3, 30)+ _fluff_bit(f, 43, 27)+++def _belly_slide_fx(f: Any, pose: Any) -> None:+ """Speed lines behind the slide and a spray of snow at the front."""+ i = pose.i+ if i == 2:+ f.paint(bp.line(4, 30, 8, 30) | bp.line(5, 33, 8, 33), bp.ARC_FADE)+ if 3 <= i <= 5:+ s = (17, 20, 23)[i - 3]+ tail = s - 16+ for y, n in ((34, 4), (37, 6), (40, 4)):+ if tail - n >= 1:+ f.paint(bp.line(tail - n, y, tail - 1, y), bp.ARC_FADE)+ else:+ f.paint(bp.line(1, y, max(1, tail - 1), y), bp.ARC_FADE)+ f.paint(bp.line(s - 12, 45, s + 16, 45), SNOW_SHADE)+ if i in (5, 6):+ x = 44 if i == 5 else 43+ _snow_puff(f, x, 42.0, 1.8 if i == 5 else 2.4)+ _snow_puff(f, x - 2, 38.0 + (i - 5), 1.2)+ _flake(f, 42, 31 - (i - 5) * 2)+ if i == 7:+ bp.fx_sparkles(f, 1, ((40, 12, 0),))+++def _snowball_fx(f: Any, pose: Any) -> None:+ """The snowball: held, tossed up, falling and bursting on the head."""+ i = pose.i+ ball = {1: (24.0, 37.0, 3.2), 2: (24.0, 6.0, 3.0), 3: (24.0, 3.5, 2.4), 4: (24.0, 6.5, 3.0)}+ if i in ball:+ x, y, r = ball[i]+ mask = _in_cell(bp.ellipse(x, y + (pose.dy if i == 1 else 0), r, r))+ f.part(mask, bp.shade(mask, SNOW, dark=SNOW_SHADE, dark_depth=1))+ if i == 3:+ f.paint(bp.line(22, 8, 22, 10) | bp.line(26, 8, 26, 10), bp.ARC_FADE)+ if i == 5:+ for x, y in ((13, 6), (35, 5), (10, 12), (38, 11)):+ _snow_puff(f, x, y, 1.3)+ bp.fx_sparkles(f, 0, ((40, 3, 0),), _SNOWFLAKE_PALETTE)+ if i == 6:+ for x, y in ((8, 10), (40, 9), (6, 18), (42, 17)):+ _snow_puff(f, x, y, 1.0)+ f.paint(bp.line(5, 24, 5, 28) | bp.line(42, 24, 42, 28), bp.ARC_FADE)+ if i == 7:+ _flake(f, 9, 20)+ _flake(f, 39, 16)+++def _fish_snack_fx(f: Any, pose: Any) -> None:+ """The fish arcs in from the left; its tail wiggles out of the beak; a heart."""+ i = pose.i+ path = {1: (2, 12), 2: (9, 4), 3: (20, 1)}+ if i in path:+ x, y = path[i]+ f.glyph(x, y, _FISH, _FISH_PALETTE)+ if i == 1:+ f.paint(bp.line(2, 19, 4, 18), bp.ARC_FADE)+ if i == 4:+ f.glyph(26, 23, (".#", "#.", ".#"), {"#": FISH})+ if i == 5:+ f.paint({(17, 27), (31, 27)}, bp.ARC_FADE)+ if i == 6:+ f.glyph(36, 4, _HEART_L, _HEART_PALETTE)+ f.glyph(8, 9, _HEART, _HEART_PALETTE)+ if i == 7:+ f.glyph(38, 2, _HEART, _HEART_PALETTE)+++def _flipper_flap_fx(f: Any, pose: Any) -> None:+ """Whoosh lines under the flapping flippers, loose down, a bump on landing."""+ i = pose.i+ if i in (2, 3, 4, 5):+ with f.offset(0, pose.dy):+ f.paint(bp.line(1, 40, 4, 40) | bp.line(2, 42, 4, 42), bp.ARC_FADE)+ f.paint(bp.line(43, 40, 46, 40) | bp.line(43, 42, 45, 42), bp.ARC_FADE)+ if i in (3, 5):+ _fluff_bit(f, 4, 14 + i)+ if i == 4:+ _fluff_bit(f, 41, 15)+ if i == 6:+ for x in (9, 38):+ _snow_puff(f, x, 44.5, 1.6)+ f.paint({(6, 40), (41, 40)}, bp.ARC)+ if i == 7:+ _fluff_bit(f, 6, 30)+++def _tumble_fx(f: Any, pose: Any) -> None:+ """A wobble mark, a puff of snow on the plop and stars round the head."""+ i = pose.i+ if i == 3:+ f.paint(bp.line(43, 22, 43, 26) | bp.line(45, 23, 45, 25), bp.ARC)+ if i == 4:+ for x in (7, 40):+ _snow_puff(f, x, 44.0, 2.0)+ if i in (4, 5, 6):+ spots = (((9, 9), (38, 6)), ((14, 5), (34, 9)), ((8, 12), (40, 10)))[i - 4]+ for x, y in spots:+ f.glyph(x - 1, y - 1, _STAR, {"#": bp.SPARK, "o": bp.SPARK_CORE})+++def _preen_fx(f: Any, pose: Any) -> None:+ """Loose bits of down float off the shoulder and drift away."""+ i = pose.i+ bits = {+ 3: ((10, 22),),+ 4: ((8, 19), (12, 24)),+ 5: ((6, 15), (11, 21), (38, 22)),+ 6: ((5, 12), (40, 18), (9, 27)),+ 7: ((4, 10),),+ }+ for x, y in bits.get(i, ()):+ _fluff_bit(f, x, y)+ if i in (5, 6):+ f.paint(bp.line(4, 30, 4, 35) | bp.line(43, 30, 43, 35), bp.ARC_FADE)+++ACT_FX: dict[str, tuple[int, Callable[[Any, Any], None]]] = {+ "belly_slide": (9, _belly_slide_fx),+ "snowball": (7, _snowball_fx),+ "fish_snack": (8, _fish_snack_fx),+ "flipper_flap": (10, _flipper_flap_fx),+ "tumble": (8, _tumble_fx),+ "preen": (6, _preen_fx),+}+++def _act(name: str) -> Any:+ fps, fx = ACT_FX[name]+ cells = ACTS[name]+ return bp.IdleAct(name, len(cells), fps, lambda i: _pose_of("idle", i, cells[i], name), fx)+++# -- The pet ------------------------------------------------------------------+++def design(builder: Any) -> Any:+ global bp+ bp = builder+ return bp.PetDesign(+ id="pip",+ name="Pip",+ description=(+ "A penguin chick in grey fluff who waddles to listen and belly-slides "+ "when something goes right."+ ),+ draw=draw_pip,+ pose=pip_pose,+ waist=40,+ anchors=bp.Anchors(+ arcs_left=(8, 19),+ arcs_right=(39, 19),+ dots=(19, 3),+ zzz=(34, 15),+ bang=(41, 3),+ sparkles=((6, 12, 1), (41, 7, 3), (5, 27, 4)),+ laptop=(17, 35),+ magnifier=(CX, 19),+ ),+ fx={+ "talking": _talking_fx,+ "success": _success_fx,+ "error": _error_fx,+ "sleeping": _sleeping_fx,+ "working": _working_fx,+ "searching": _searching_fx,+ "held": _held_fx,+ },+ acts=tuple(_act(name) for name in ACTS),+ )diff --git a/scripts/pets/designs/sprout.py b/scripts/pets/designs/sprout.pynew file mode 100644index 0000000000..5af44061bc--- /dev/null+++ b/scripts/pets/designs/sprout.py@@ -0,0 +1,722 @@+"""Sprout: a little forest plant spirit.++A plump, leaf-green seedling bulb with a lighter belly, two bright leaves on+a short stem on top of its head, stubby root feet and tiny leaf arms. The head+leaves carry its mood: they perk up and open to listen, wilt and drop a leaf+when something goes wrong and fold down over its face to sleep. While it+thinks a new little leaf unfurls on the stem, and on success a flower blooms.++Built by ``scripts/pets/build_pets.py`` (``design(bp)`` gets that module).+"""++import math+from collections.abc import Callable+from dataclasses import dataclass, replace+from types import ModuleType+from typing import Any++#: The builder module, set by ``design``; every helper reaches it through here.+bp: Any = None++Px = tuple[int, int]+Mask = set[Px]+++def _c(value: str) -> tuple[int, int, int, int]:+ value = value.lstrip("#")+ return (int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16), 255)+++BODY = _c("#9bd46f")+BODY_LIGHT = _c("#c8f09a")+BODY_DARK = _c("#67a64e")+BELLY = _c("#e6f7c6")+BELLY_DARK = _c("#cbe9a4")+LEAF = _c("#3cbf5c")+LEAF_LIGHT = _c("#8ef08c")+LEAF_DARK = _c("#22883f")+STEM = _c("#4fa94a")+SHOOT = _c("#b4ec6a")+SHOOT_LIGHT = _c("#e4ffb0")+INK = _c("#2a2433")+ROOT = _c("#c99a63")+ROOT_LIGHT = _c("#ecc590")+ROOT_DARK = _c("#94643a")+PETAL = _c("#ff8fbf")+PETAL_LIGHT = _c("#ffd2e6")+PETAL_DARK = _c("#e0609a")+POLLEN = _c("#ffd23f")+POLLEN_DARK = _c("#e0a020")+WATER = _c("#7cc8ff")+WATER_LIGHT = _c("#e3f4ff")+WING = _c("#b48cff")+WING_LIGHT = _c("#e2d2ff")+SUN = _c("#ffd23f")+SUN_CORE = _c("#fff3b0")+SUN_RAY = _c("#ffb53f")+HEART = _c("#ff6f91")+HEART_LIGHT = _c("#ffd3de")+FLUFF = _c("#f7f7fb")+FLUFF_DIM = _c("#c9cfdb")++#: The top-left of each 3 x 4 eye box, and the mouth's left end and top row.+EYE_L: Px = (18, 28)+EYE_R: Px = (27, 28)+MOUTH: Px = (22, 33)+#: Where the head leaves join the stem (left, right) and the stem's top.+LEAF_ROOT_L = (23.0, 18.0)+LEAF_ROOT_R = (25.0, 18.0)+STEM_TOP = 16+#: Where the leaf arms leave the body.+ARM_ROOT_L = (14.5, 33.0)+ARM_ROOT_R = (33.5, 33.0)+ARM_LEN = 7.5+++@dataclass(frozen=True)+class Look:+ """How Sprout holds itself, beyond the pose's eyes and mouth."""++ #: Head leaves: elevation in degrees above the horizontal, pointing out.+ leaves: tuple[float, float] = (35.0, 35.0)+ leaf_len: float = 11.5+ #: How far each leaf tip sags under its own weight (pixels, down).+ sag: tuple[float, float] = (0.0, 0.0)+ #: Leaves folded down in front of the face (asleep).+ front: bool = False+ #: Leaf arms: elevation in degrees, pointing out (negative hangs down).+ arms: tuple[float, float] = (-45.0, -45.0)+ #: 0 none, 1 bud, 2 opening, 3 full bloom.+ flower: int = 0+ #: 0 none .. 4 an open new leaf on the stem (thinking).+ sprout: int = 0+ #: Each root foot's drop (dangling) and how far apart they kick.+ feet: tuple[int, int] = (0, 0)+ spread: int = 0+ #: Little root tendrils under the feet (only with room below).+ roots: bool = False+ blush: bool = True+++# ---------------------------------------------------------------------------+# Shapes.+# ---------------------------------------------------------------------------+++def _leaf(+ root: tuple[float, float],+ elev: float,+ length: float,+ width: float,+ *,+ left: bool,+ sag: float = 0.0,+) -> tuple[Mask, Mask]:+ """A pointed leaf from ``root``: its mask and the pixels of its midrib."""+ a = math.radians(elev)+ ux = -math.cos(a) if left else math.cos(a)+ uy = -math.sin(a)+ out: Mask = set()+ rib: Mask = set()+ steps = max(6, int(length * 2))+ for k in range(steps + 1):+ t = k / steps+ x = root[0] + ux * length * t+ y = root[1] + uy * length * t + sag * t * t+ r = width * math.sin(math.pi * (0.18 + 0.82 * t) ** 1.1)+ if r < 0.7:+ out.add((math.floor(x), math.floor(y)))+ else:+ out |= bp.ellipse(x, y, r, r * 0.9)+ if 0.2 <= t <= 0.7:+ rib.add((math.floor(x), math.floor(y)))+ return out, rib+++def _body() -> Mask:+ """The plump bulb: round below, a little narrower towards the stem."""+ return bp.ellipse(24, 32, 12, 9.5) | bp.ellipse(24, 27.5, 8.5, 7)+++def _paint_leaves(f: Any, leaves: list[tuple[Mask, Mask]], extra: Mask = frozenset()) -> None:+ """Outline the leaves (and ``extra``, e.g. the stem) as one shape, then fill."""+ whole: Mask = set(extra)+ for mask, _rib in leaves:+ whole |= mask+ f.paint(bp.outer_ring(whole), bp.OUTLINE)+ for mask, rib in leaves:+ f.paint_map(bp.shade(mask, LEAF, light=LEAF_LIGHT, dark=LEAF_DARK, dark_depth=1))+ f.paint(rib & bp.eroded(mask, 1), LEAF_DARK)+++def _head_leaves(look: Look) -> list[tuple[Mask, Mask]]:+ return [+ _leaf(LEAF_ROOT_L, look.leaves[0], look.leaf_len, 3.4, left=True, sag=look.sag[0]),+ _leaf(LEAF_ROOT_R, look.leaves[1], look.leaf_len, 3.4, left=False, sag=look.sag[1]),+ ]+++def _stem() -> Mask:+ return bp.rect(23, STEM_TOP, 24, 21)+++def _draw_sprout(f: Any, stage: int) -> None:+ """Thinking: a new leaf curls up from the top of the stem and unfurls."""+ if stage <= 0:+ return+ stalk = bp.rect(23, STEM_TOP - 1 - min(stage, 3), 24, STEM_TOP)+ if stage == 1:+ f.part(stalk, SHOOT)+ f.paint({(23, STEM_TOP - 2)}, SHOOT_LIGHT)+ return+ top = STEM_TOP - 1 - min(stage, 3)+ if stage == 2:+ curl = bp.from_rows(23, top - 4, (".##.", "#..#", "#.##", ".#.."))+ f.part(stalk | curl, SHOOT)+ f.paint({(24, top - 4)}, SHOOT_LIGHT)+ return+ length, width = (5.5, 2.0) if stage == 3 else (7.0, 2.5)+ elev = 70.0 if stage == 3 else 55.0+ leaf, rib = _leaf((24.5, top + 0.5), elev, length, width, left=False)+ f.part(stalk | leaf, SHOOT)+ f.paint_map(bp.shade(leaf, SHOOT, light=SHOOT_LIGHT, dark=LEAF, dark_depth=1))+ f.paint(rib & bp.eroded(leaf, 1), LEAF)+++#: Petal centres around the flower's heart, as (dx, dy).+_PETALS = tuple(+ (2.6 * math.cos(math.radians(90 + 72 * k)), -2.6 * math.sin(math.radians(90 + 72 * k)))+ for k in range(5)+)+++def _draw_flower(f: Any, stage: int, cx: float = 24.0, cy: float = 13.0) -> None:+ """A pink blossom on top of the stem: a bud, opening, in full bloom."""+ if stage <= 0:+ return+ if stage == 1:+ bud = bp.ellipse(cx, cy + 1.5, 1.6, 2.2)+ f.part(bud, bp.shade(bud, PETAL, light=PETAL_LIGHT, dark=PETAL_DARK, dark_depth=1))+ f.paint({(23, int(cy) + 3), (24, int(cy) + 3)}, LEAF)+ return+ r = 1.4 if stage == 2 else 1.9+ dist = 0.75 if stage == 2 else 1.0+ petals: Mask = set()+ for dx, dy in _PETALS:+ petals |= bp.ellipse(cx + dx * dist, cy + dy * dist, r, r)+ f.part(petals, bp.shade(petals, PETAL, light=PETAL_LIGHT, dark=PETAL_DARK, dark_depth=1))+ heart = bp.rect(23, int(cy) - 1, 24, int(cy))+ f.paint(heart, POLLEN)+ f.paint({(24, int(cy))}, POLLEN_DARK)+++def _feet(look: Look) -> Mask:+ out: Mask = set()+ for side, cx in ((0, 19.0 - look.spread), (1, 29.0 + look.spread)):+ dy = look.feet[side]+ foot = bp.ellipse(cx, 42.0 + dy, 3.3, 2.2)+ toe = (math.floor(cx) - 3, 43 + dy) if side == 0 else (math.floor(cx) + 3, 43 + dy)+ out |= foot | {toe}+ return out+++def _draw_feet(f: Any, look: Look) -> None:+ feet = _feet(look)+ if look.roots:+ for side, cx in ((0, 19 - look.spread), (1, 29 + look.spread)):+ dy = look.feet[side]+ wiggle = 1 if (dy + side) % 2 else -1+ feet |= {(cx - 1, 45 + dy), (cx - 1 + wiggle, 46 + dy)}+ f.part(feet, bp.shade(feet, ROOT, light=ROOT_LIGHT, dark=ROOT_DARK, dark_depth=1))+++#: Arms raised above this elevation are drawn in front of the body.+ARM_FRONT = 10.0+++def _draw_arms(f: Any, look: Look, *, front: bool) -> None:+ arms = []+ for root, elev, left in ((ARM_ROOT_L, look.arms[0], True), (ARM_ROOT_R, look.arms[1], False)):+ if (elev > ARM_FRONT) == front:+ mask, _rib = _leaf(root, elev, ARM_LEN, 2.3, left=left)+ arms.append((mask, set()))+ if arms:+ _paint_leaves(f, arms)+++def _mouth(f: Any, style: str) -> None:+ x, y = MOUTH+ if style == "o":+ f.paint(bp.rect(23, y, 24, y + 1), bp.MOUTH_DARK)+ f.paint(bp.outer_ring(bp.rect(23, y, 24, y + 1)), INK)+ elif style == "puff":+ f.paint({(23, y + 1), (24, y + 1)}, INK)+ else:+ bp.draw_mouth(f, style, x, y, 4, INK)+++def _draw_face(f: Any, pose: Any, look: Look) -> None:+ eyes = bp.EyeStyle(w=3, h=4, iris=INK, glint=bp.FX_WHITE, lid=INK)+ bp.draw_eyes(f, pose.eyes, EYE_L, EYE_R, eyes)+ if look.blush:+ cheek = bp.BLUSH if pose.mouth != "puff" else PETAL+ f.paint(bp.rect(14, 33, 16, 33) | bp.rect(31, 33, 33, 33), cheek)+ if pose.mouth == "puff":+ f.paint(bp.rect(15, 32, 16, 34) | bp.rect(31, 32, 32, 34), cheek)+ _mouth(f, pose.mouth)+++def draw_sprout(f: Any, pose: Any) -> None:+ look = _look(pose)+ with f.offset(pose.dx, pose.dy):+ leaves = _head_leaves(look)+ if not look.front:+ _paint_leaves(f, leaves, _stem())+ f.paint(_stem(), STEM)+ f.paint({(23, STEM_TOP)}, LEAF_LIGHT)+ _draw_sprout(f, look.sprout)+ _draw_flower(f, look.flower)+ _draw_feet(f, look)+ _draw_arms(f, look, front=False)+ body = _body()+ f.part(body, bp.shade(body, BODY, light=BODY_LIGHT, dark=BODY_DARK, dark_depth=2))+ inner = bp.eroded(body, 2)+ belly = bp.ellipse(24, 38.5, 6.5, 3.6) & inner+ f.paint_map(bp.shade(belly, BELLY, dark=BELLY_DARK, dark_depth=1))+ # A dewy shine on the bulb's shoulder.+ f.paint({(16, 25), (17, 24), (18, 24), (15, 26)} & bp.eroded(body, 1), bp.FX_WHITE)+ _draw_face(f, pose, look)+ _draw_arms(f, look, front=True)+ if look.front:+ _paint_leaves(f, leaves, _stem())+ f.paint(_stem() - set().union(*(m for m, _r in leaves)), STEM)+++# ---------------------------------------------------------------------------+# Poses and looks per state.+# ---------------------------------------------------------------------------++#: Idle: a gentle sway of the head leaves over the seven breathing cells.+_IDLE_SWAY = (0, 1, 2, 2, 1, 0, -1)+#: Listening: the leaves perk up and open towards the sound.+_LISTEN_LEAVES = (50.0, 60.0, 66.0, 68.0, 66.0, 62.0)+#: Thinking: the new leaf's growth stage per frame.+_THINK_SPROUT = (1, 1, 2, 2, 3, 3, 4, 4)+#: Talking: the leaves bounce higher the louder it speaks.+_TALK_LEAVES = (35.0, 39.0, 44.0, 50.0)+#: Success: the flower opens on the way up and stays in bloom.+_HOP_FLOWER = (1, 2, 3, 3, 3, 3, 3, 3)+#: Error: the leaves wilt a little more every frame.+_WILT = ((30.0, 1.0), (18.0, 3.0), (8.0, 5.0), (0.0, 8.0), (-4.0, 10.0), (-4.0, 10.0))+#: Held: the roots kick in turn.+_HELD_FEET = ((1, 2), (2, 1), (1, 2), (2, 1), (1, 2), (2, 1))+++def sprout_pose(state: str, i: int) -> Any:+ pose = bp.base_pose(state, i)+ if state == "success" and 2 <= i <= 4:+ return replace(pose, mouth="open")+ if state == "error":+ return replace(pose, dy=1)+ if state == "thinking":+ return replace(pose, mouth="rest" if i >= 6 else "closed")+ return pose+++def _look(pose: Any) -> Look:+ if pose.act:+ return _ACT_LOOKS[pose.act](pose.i)+ state, i = pose.state, pose.i+ if state == "idle":+ sway = _IDLE_SWAY[i] if i < len(_IDLE_SWAY) else 0+ return Look(leaves=(35.0 + 4 * sway, 35.0 - 4 * sway))+ if state == "listening":+ e = _LISTEN_LEAVES[i]+ return Look(leaves=(e, e - 2), leaf_len=11.0, arms=(-20.0, -20.0))+ if state == "thinking":+ tilt = 3.0 if i % 4 < 2 else -3.0+ return Look(leaves=(38.0 + tilt, 38.0 - tilt), sprout=_THINK_SPROUT[i])+ if state == "talking":+ e = _TALK_LEAVES[i]+ return Look(leaves=(e, e))+ if state == "success":+ air = 1 <= i <= 5+ e = 58.0 if air else 30.0+ arms = (40.0, 40.0) if air else (-25.0, -25.0)+ return Look(leaves=(e, e), flower=_HOP_FLOWER[i], arms=arms)+ if state == "error":+ e, sag = _WILT[i]+ # Limp leaves flop down over the sides of the head like sad ears; the+ # right one holds on a little longer than the left.+ return Look(+ leaves=(e, e + 6),+ sag=(sag, sag - 2),+ leaf_len=12.0,+ front=i >= 2,+ arms=(-75.0, -75.0),+ blush=False,+ )+ if state == "sleeping":+ return Look(leaves=(-62.0, -62.0), leaf_len=12.5, front=True, arms=(-60.0, -60.0))+ if state == "working":+ bob = 3.0 if i % 2 else 0.0+ return Look(leaves=(35.0 + bob, 35.0 + bob), arms=(-70.0, -70.0))+ if state == "searching":+ lean = bp.SEARCH_SWEEP[i] * 1.6+ return Look(leaves=(38.0 + lean, 38.0 - lean), arms=(-40.0, -40.0))+ if state == "held":+ return Look(+ leaves=(72.0, 72.0),+ leaf_len=11.0,+ arms=(45.0, 45.0),+ feet=_HELD_FEET[i],+ spread=i % 2,+ roots=True,+ )+ return Look()+++# ---------------------------------------------------------------------------+# State effects.+# ---------------------------------------------------------------------------++_ARC_S = ((0, -2), (1, -1), (1, 0), (0, 1))+_ARC_M = ((0, -3), (1, -2), (2, -1), (2, 0), (1, 1), (0, 2))+++def _arc(f: Any, x: int, y: int, arc: tuple[Px, ...], color: Any, *, left: bool) -> None:+ f.paint({(x - ax if left else x + ax, y + ay) for ax, ay in arc}, color)+++def _talking_fx(f: Any, pose: Any) -> None:+ """Little ripples beside the cheeks on the louder frames."""+ i = pose.i+ with f.offset(pose.dx, pose.dy):+ if i >= 2:+ _arc(f, 9, 32, _ARC_S, bp.ARC_FADE if i == 2 else bp.ARC, left=True)+ _arc(f, 38, 32, _ARC_S, bp.ARC_FADE if i == 2 else bp.ARC, left=False)+ if i == 3:+ _arc(f, 6, 32, _ARC_M, bp.ARC_FADE, left=True)+ _arc(f, 41, 32, _ARC_M, bp.ARC_FADE, left=False)+++_PETAL_FALL = ((8, 0, 1), (39, 0, 2), (12, 0, 3), (36, 0, 4))+_PETAL_GLYPHS = (("##",), ("#", "#"))+++def _success_fx(f: Any, pose: Any) -> None:+ """Sparkles and pink petals fluttering down around the hop."""+ bp.fx_sparkles(f, pose.i, ((10, 12, 2), (38, 8, 3), (42, 22, 4)))+ for x, _dy, start in _PETAL_FALL:+ age = pose.i - start+ if age < 0:+ continue+ y = 18 + 4 * age+ if y > 42:+ continue+ rows = _PETAL_GLYPHS[age % 2]+ f.glyph(x + (age % 2), y, rows, {"#": PETAL})+++#: A wilted leaf drifting to the ground, swaying as it falls.+_FALL = ((11, 14), (7, 19), (9, 24), (5, 29), (8, 34), (5, 40))+_FALLEN = (("..#", ".##", "##."), ("#..", "##.", ".##"))+++def _error_fx(f: Any, pose: Any) -> None:+ i = pose.i+ x, y = _FALL[i]+ f.glyph(x, y, _FALLEN[i % 2], {"#": LEAF})+ with f.offset(pose.dx, pose.dy):+ bp.fx_sweat(f, i, 37, 22)+++#: A vine curling up beside Sprout as it works: its path, one step per pixel.+_VINE = (+ (41, 45), (41, 44), (41, 43), (42, 42), (42, 41), (42, 40), (41, 39), (41, 38),+ (41, 37), (42, 36), (43, 35), (43, 34), (42, 33), (41, 33), (40, 34), (40, 35), (41, 35),+)+#: Little leaves along the vine: (index on the path, x offset side).+_VINE_LEAVES = ((4, 1), (9, -1))+++def _working_fx(f: Any, pose: Any) -> None:+ i = pose.i+ bp.fx_laptop(f, i, 17, 34)+ # Two leaf hands tapping the keys in turn.+ for side, x in ((0, 18), (1, 28)):+ up = (i + side) % 2+ hand = bp.rect(x, 40 - up, x + 1, 41 - up)+ f.part(hand, LEAF)+ f.paint({(x, 40 - up)}, LEAF_LIGHT)+ shown = _VINE[: min(len(_VINE), 4 + 2 * i)]+ f.part(set(shown), STEM)+ for index, side in _VINE_LEAVES:+ if index < len(shown):+ vx, vy = _VINE[index]+ leaf = {(vx + side, vy), (vx + 2 * side, vy - 1), (vx + side, vy - 1)}+ f.part(leaf, LEAF)+++def _sleeping_fx(f: Any, pose: Any) -> None:+ bp.fx_zzz(f, pose.i, 34, 18)+++# ---------------------------------------------------------------------------+# Idle acts.+# ---------------------------------------------------------------------------+++def _pose(i: int, eyes: str = "open", mouth: str = "rest", dy: int = 0, stretch: int = 0) -> Any:+ return bp.Pose("idle", i, dy=dy, eyes=eyes, mouth=mouth, stretch=stretch)+++# -- bloom: a flower slowly opens on its head, then closes again.+_BLOOM = (0, 1, 2, 3, 3, 3, 2, 1)+_BLOOM_EYES = ("open", "up_left", "up_left", "happy", "happy", "happy", "open", "open")+++def _bloom_pose(i: int) -> Any:+ return _pose(i, _BLOOM_EYES[i], "smile" if 3 <= i <= 5 else "rest", stretch=1 if i == 4 else 0)+++def _bloom_look(i: int) -> Look:+ e = 35.0 + 6 * min(_BLOOM[i], 2)+ return Look(leaves=(e, e), flower=_BLOOM[i])+++def _bloom_fx(f: Any, pose: Any) -> None:+ bp.fx_sparkles(f, pose.i, ((14, 8, 2), (34, 7, 3), (37, 17, 4)))+++# -- leaf_wave: a friendly wave with the right leaf arm.+_WAVE_ARM = (-45.0, 15.0, 60.0, 30.0, 60.0, 30.0, 60.0, -20.0)+++def _wave_pose(i: int) -> Any:+ eyes = "happy" if 2 <= i <= 6 else "open"+ return _pose(i, eyes, "open" if i in (2, 4) else "smile" if 1 <= i <= 6 else "rest")+++def _wave_look(i: int) -> Look:+ tilt = 4.0 if 2 <= i <= 6 else 0.0+ return Look(leaves=(35.0 - tilt, 35.0 + tilt), arms=(-45.0, _WAVE_ARM[i]))+++def _wave_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if 2 <= i <= 6:+ color = bp.ARC if i % 2 == 0 else bp.ARC_FADE+ f.paint(bp.line(43, 23, 44, 26) | bp.line(41, 20, 43, 19), color)+++# -- water_drop: a raindrop lands on its head, it drinks it up happily.+_DROP_Y = (1, 5, 9, None, None, None, None, None)+_DROP_EYES = ("up_left", "up_left", "up_left", "closed", "happy", "happy", "happy", "open")+_DROP_LEAVES = (35.0, 35.0, 35.0, 25.0, 50.0, 58.0, 52.0, 38.0)+++def _drop_pose(i: int) -> Any:+ mouth = "o" if i in (1, 2) else "smile" if 4 <= i <= 6 else "rest"+ return _pose(i, _DROP_EYES[i], mouth, stretch=1 if i in (4, 5) else -1 if i == 3 else 0)+++def _drop_look(i: int) -> Look:+ e = _DROP_LEAVES[i]+ return Look(leaves=(e, e), arms=(-45.0, -45.0) if i < 4 else (10.0, 10.0))+++_DROP_GLYPH = (".#.", "###", "#o#", ".#.")+++def _drop_fx(f: Any, pose: Any) -> None:+ i = pose.i+ palette = {"#": WATER, "o": WATER_LIGHT}+ y = _DROP_Y[i]+ if y is not None:+ f.glyph(23, y, _DROP_GLYPH, palette)+ if i == 3:+ # The splash on the stem's top.+ f.paint({(20, 13), (21, 12), (27, 13), (26, 12), (22, 11), (25, 11)}, WATER)+ if i == 4:+ f.paint({(18, 12), (19, 11), (29, 12), (28, 11)}, WATER_LIGHT)+ if 4 <= i <= 6:+ heart = ("#.#", "###", ".#.")+ f.glyph(36, 14 - (i - 4) * 2, heart, {"#": HEART})+ if i == 5:+ bp.fx_sparkles(f, 1, ((11, 20, 0),))+++# -- sun_stretch: a sun peeks out, Sprout stretches up towards it.+_SUN_STRETCH = (0, 1, 1, 1, 1, 1, 0, 0)+_SUN_ARMS = (-45.0, 5.0, 35.0, 45.0, 45.0, 35.0, 5.0, -45.0)+_SUN_EYES = ("open", "up_right", "happy", "happy", "happy", "happy", "open", "open")+++def _sun_pose(i: int) -> Any:+ mouth = "o" if i in (2, 3) else "smile" if i in (4, 5) else "rest"+ return _pose(i, _SUN_EYES[i], mouth, dy=-1 if 2 <= i <= 5 else 0, stretch=_SUN_STRETCH[i])+++def _sun_look(i: int) -> Look:+ a = _SUN_ARMS[i]+ e = 35.0 + max(0.0, a) * 0.6+ return Look(leaves=(e, e), leaf_len=12.5 if a > 30 else 11.5, arms=(a, a))+++def _sun_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if i == 0 or i == 7:+ return+ cx, cy = 41.0, 6.0+ sun = bp.ellipse(cx, cy, 3.2, 3.2)+ f.part(sun, bp.shade(sun, SUN, light=SUN_CORE, dark=POLLEN_DARK, dark_depth=1))+ long = i % 2 == 0+ for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)):+ diag = dx != 0 and dy != 0+ r0 = 4.5 if not diag else 3.6+ r1 = r0 + (2.0 if long else 1.0)+ a = (math.floor(cx + dx * r0), math.floor(cy + dy * r0))+ b = (math.floor(cx + dx * r1), math.floor(cy + dy * r1))+ ray = {(x, y) for x, y in bp.line(*a, *b) if 0 <= x < 48 and 0 <= y < 48}+ f.paint(ray, SUN_RAY)+++# -- butterfly_visit: a butterfly lands on its left leaf, then flies off.+_FLY = ((1, 24), (4, 17), (7, 11), (9, 6), (9, 6), (9, 6), (20, 2), (34, 5))+_FLY_EYES = ("look_left", "look_left", "up_left", "up_left", "up_left", "up_left", "up_right",+ "look_right")+_WINGS_OPEN = ("ww...ww", "wowbwow", "wwwbwww", ".wwbww.", "..w.w..")+_WINGS_SHUT = ("..w.w..", ".wwbww.", "..owo..", "...b...", ".......")+++def _fly_pose(i: int) -> Any:+ mouth = "o" if i == 3 else "smile" if 4 <= i <= 6 else "rest"+ return _pose(i, _FLY_EYES[i], mouth)+++def _fly_look(i: int) -> Look:+ # The left leaf dips a little under the butterfly's weight.+ left = 28.0 if 3 <= i <= 5 else 35.0+ return Look(leaves=(left, 35.0))+++def _fly_fx(f: Any, pose: Any) -> None:+ i = pose.i+ x, y = _FLY[i]+ landed = 3 <= i <= 5+ rows = _WINGS_SHUT if (i % 2 == 1 if not landed else i != 4) else _WINGS_OPEN+ f.glyph(x, y + (1 if landed else 0), rows, {"w": WING, "o": WING_LIGHT, "b": bp.OUTLINE})+ if i == 4:+ f.paint({(7, 4), (17, 4), (7, 7), (17, 7)}, bp.ARC_FADE)+++# -- seed_puff: it lifts a dandelion clock and blows the seeds away.+_PUFF_ARM = (-20.0, 40.0, 50.0, 50.0, 50.0, 50.0, 30.0, -45.0)+_PUFF_FACE = (+ ("open", "rest"),+ ("up_right", "smile"),+ ("closed", "puff"),+ ("closed", "o"),+ ("closed", "o"),+ ("happy", "smile"),+ ("happy", "smile"),+ ("open", "rest"),+)+#: Seeds by (start frame, dx, dy per frame), flying off to the upper right.+_SEEDS = ((3, 3, -2), (3, 2, -1), (4, 3, -1), (4, 2, -3), (5, 4, -2))+++def _puff_pose(i: int) -> Any:+ eyes, mouth = _PUFF_FACE[i]+ return _pose(i, eyes, mouth, stretch=1 if i == 2 else 0)+++def _puff_look(i: int) -> Look:+ return Look(arms=(-45.0, _PUFF_ARM[i]))+++def _puff_hand(i: int) -> tuple[int, int]:+ a = math.radians(_PUFF_ARM[i])+ x = ARM_ROOT_R[0] + (ARM_LEN - 1) * math.cos(a)+ y = ARM_ROOT_R[1] - (ARM_LEN - 1) * math.sin(a)+ return (math.floor(x), math.floor(y))+++def _puff_fx(f: Any, pose: Any) -> None:+ i = pose.i+ if i == 7:+ return+ hx, hy = _puff_hand(i)+ top = (hx, hy - 6)+ f.part(bp.line(hx, hy - 1, *top), LEAF_DARK)+ if i <= 2:+ puff = bp.ellipse(top[0] + 0.5, top[1] - 1.5, 3.0, 3.0)+ f.part(puff, FLUFF)+ f.paint({(top[0] - 1, top[1] - 2), (top[0] + 1, top[1] - 3), (top[0], top[1])}, FLUFF_DIM)+ else:+ # What is left once the seeds have flown: a bare seed head.+ f.part({(top[0], top[1] - 1), (top[0] + 1, top[1] - 1)}, POLLEN_DARK)+ for start, sx, sy in _SEEDS:+ age = i - start+ if age < 0:+ continue+ x = top[0] + 2 + sx * (age + 1)+ y = top[1] - 2 + sy * (age + 1) + (age % 2)+ if 0 <= x < 47 and 0 <= y < 47:+ # A white tuft over its little brown seed.+ f.paint({(x, y), (x + 1, y)}, FLUFF)+ f.paint({(x, y + 1)}, ROOT_DARK)+++_ACT_LOOKS: dict[str, Callable[[int], Look]] = {+ "bloom": _bloom_look,+ "leaf_wave": _wave_look,+ "water_drop": _drop_look,+ "sun_stretch": _sun_look,+ "butterfly_visit": _fly_look,+ "seed_puff": _puff_look,+}+++def design(module: ModuleType) -> Any:+ global bp # noqa: PLW0603 - the builder hands itself in at load time+ bp = module+ acts = (+ bp.IdleAct("bloom", 8, 6, _bloom_pose, _bloom_fx),+ bp.IdleAct("leaf_wave", 8, 8, _wave_pose, _wave_fx),+ bp.IdleAct("water_drop", 8, 7, _drop_pose, _drop_fx),+ bp.IdleAct("sun_stretch", 8, 6, _sun_pose, _sun_fx),+ bp.IdleAct("butterfly_visit", 8, 6, _fly_pose, _fly_fx),+ bp.IdleAct("seed_puff", 8, 7, _puff_pose, _puff_fx),+ )+ return bp.PetDesign(+ id="sprout",+ name="Sprout",+ description=(+ "A little forest plant spirit whose leaves perk up to listen and who"+ " bursts into bloom when something goes right."+ ),+ draw=draw_sprout,+ pose=sprout_pose,+ waist=40,+ anchors=bp.Anchors(+ arcs_left=(9, 28),+ arcs_right=(38, 28),+ dots=(19, 3),+ zzz=(34, 18),+ bang=(42, 6),+ sparkles=((10, 12, 2), (38, 8, 3), (42, 22, 4)),+ sweat=(37, 22),+ laptop=(17, 34),+ magnifier=(24, 30),+ ),+ fx={+ "talking": _talking_fx,+ "success": _success_fx,+ "error": _error_fx,+ "working": _working_fx,+ "sleeping": _sleeping_fx,+ },+ acts=acts,+ )diff --git a/scripts/pets/designs/toasty.py b/scripts/pets/designs/toasty.pynew file mode 100644index 0000000000..ae6b8aef37--- /dev/null+++ b/scripts/pets/designs/toasty.py@@ -0,0 +1,651 @@+"""Toasty: a retro two-slot toaster.++A rounded mint-enamel toaster with a chrome band, a coral lever on its right+side, a little chrome dial on its left and two slices of golden toast peeking+out of its slots. Toasty's moods live in its toast (it rises to listen, pops+sky-high on a success, burns on an error and sinks out of sight asleep), its+slots (they glow orange while it thinks and works) and its lever, which perks+up, bounces and clicks down with the toast.+"""++from __future__ import annotations+++def design(bp): # noqa: C901, PLR0915 - one closure per drawing helper+ hexc = bp.hexc+ Pose = bp.Pose+ rect = bp.rect+ line = bp.line+ ellipse = bp.ellipse+ rounded_rect = bp.rounded_rect+ shade = bp.shade+ mirrored = bp.mirrored+ shifted = bp.shifted+ OUTLINE = bp.OUTLINE+ FX_WHITE = bp.FX_WHITE++ # -- Palette ----------------------------------------------------------------+ enamel = (hexc("#9fdec4"), hexc("#d3f4e5"), hexc("#5fae95"))+ #: Asleep the enamel cools down: the same toaster, dimmed.+ enamel_cool = (hexc("#7fb9a6"), hexc("#a9d6c6"), hexc("#4a8a77"))+ chrome = hexc("#d9dee7")+ chrome_light = hexc("#ffffff")+ chrome_dark = hexc("#8d95a6")+ lever = hexc("#f07a5a")+ lever_light = hexc("#ffb59c")+ lever_dark = hexc("#b9503b")+ foot = hexc("#a9b0bf")+ foot_light = hexc("#e3e7ef")+ ink = hexc("#2a2430")+ blush = hexc("#ff8fa3")+ eyes_style = bp.EyeStyle(w=3, h=4, iris=ink, glint=FX_WHITE, lid=ink)+ #: Toast: (crust, crumb, highlight) per doneness.+ toast_golden = (hexc("#a65d24"), hexc("#f3c46e"), hexc("#ffe6ae"))+ toast_burnt = (hexc("#2e211b"), hexc("#5a3e2e"), hexc("#7a5844"))+ toast_hot = (hexc("#a65d24"), hexc("#f6a54c"), hexc("#ffd28f"))+ glow = hexc("#ff8a2a")+ glow_light = hexc("#ffc06a")+ #: Heat shimmer: a mid orange that holds on dark AND light desktops.+ heat = hexc("#ff9a3c")+ heat_fade = hexc("#f7c27e")+ smoke = hexc("#6e6872")+ smoke_light = hexc("#9b95a2")+ #: Flying crumbs: a mid tone that holds on dark AND light desktops.+ crumb = hexc("#d98a3a")+ butter = hexc("#fff0a0")+ butter_dark = hexc("#e8c95a")++ # -- Geometry ---------------------------------------------------------------+ #: The slots' left columns; each slice is 9 px wide.+ slots = (14, 25)+ #: The deck: the body's top row, where the slots open.+ deck = 20+ #: A slice's top row at rest (six rows peek out of the slot).+ rest = 15+ #: A slice's top row when it has sunk out of sight.+ sunk = 22+ #: The lever knob's top row at rest.+ lever_rest = 28+ dial_c = (8, 32)++ # -- Per-state tables -------------------------------------------------------+ #: (left, right) toast top per frame.+ tops: dict[str, tuple[tuple[int, int], ...]] = {+ "listening": ((14, 14), (13, 13), (12, 12), (12, 12), (13, 13), (14, 14)),+ "talking": ((rest, rest), (rest, rest), (14, 14), (13, 13)),+ "success": (+ (rest, rest),+ (12, 14),+ (7, 10),+ (6, 6),+ (8, 5),+ (12, 9),+ (rest, 14),+ (rest, rest),+ ),+ "error": ((rest, rest), (13, 14), (rest, rest), (14, 13), (rest, rest), (rest, rest)),+ "sleeping": ((sunk, sunk),) * 6,+ "working": (+ (16, 16),+ (17, 16),+ (16, 16),+ (16, 17),+ (16, 16),+ (17, 16),+ (16, 16),+ (16, 16),+ ),+ "searching": ((16, 16),) * 8,+ "held": ((13, 16), (14, 15), (16, 13), (15, 14), (13, 16), (14, 15)),+ }+ levers: dict[str, tuple[int, ...]] = {+ "listening": (26, 25, 24, 24, 25, 26),+ "talking": (28, 28, 27, 26),+ "success": (28, 27, 24, 24, 25, 27, 28, 28),+ "error": (28, 26, 30, 26, 30, 28),+ "sleeping": (34,) * 6,+ "working": (31, 30, 31, 31, 30, 31, 30, 31),+ "held": (27, 29, 27, 29, 27, 29),+ }+ #: Slot glow per frame: 0 off, 1 warm, 2 bright.+ glows: dict[str, tuple[int, ...]] = {+ "thinking": (1, 1, 2, 2, 2, 2, 1, 1),+ "working": (1, 1, 1, 1, 1, 1, 1, 1),+ "error": (2, 2, 1, 1, 0, 0),+ }+ #: The dial pointer's direction per frame, as a (dx, dy) step from its hub.+ dial_dirs = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1))+ #: Idle: the chrome band's glint sliding across (None = no glint).+ band_glint = (None, None, 13, 18, 23, 28, None, None)++ # -- Idle acts' tables ------------------------------------------------------+ act_tops: dict[str, tuple[tuple[int, int], ...]] = {+ "toast_pop": (+ (rest, rest),+ (17, 17),+ (6, 7),+ (1, 2),+ (2, 1),+ (8, 7),+ (14, 14),+ (rest, rest),+ ),+ "toast_juggle": (+ (rest, rest),+ (7, 17),+ (3, 15),+ (9, 8),+ (15, 3),+ (17, 7),+ (14, 14),+ (rest, rest),+ ),+ "crumb_shake": ((rest, rest), (14, 16), (16, 14), (14, 16), (16, 14), (rest, rest)) + (+ (rest, rest),+ (rest, rest),+ ),+ "lever_bounce": (+ (rest, rest),+ (18, 18),+ (sunk, sunk),+ (sunk, sunk),+ (7, 7),+ (11, 11),+ (14, 14),+ (rest, rest),+ ),+ "butter_shine": ((rest, rest),) * 8,+ "heat_shimmer": (+ (rest, rest),+ (16, 16),+ (17, 17),+ (17, 17),+ (17, 17),+ (17, 17),+ (12, 12),+ (rest, rest),+ ),+ }+ #: Juggle: the slices drift towards the middle at the top of their arc.+ act_toast_dx: dict[str, tuple[tuple[int, int], ...]] = {+ "toast_juggle": ((0, 0), (1, 0), (3, 0), (1, -1), (0, -3), (0, -1), (0, 0), (0, 0)),+ }+ act_levers: dict[str, tuple[int, ...]] = {+ "toast_pop": (28, 31, 24, 24, 24, 26, 28, 28),+ "toast_juggle": (28, 25, 28, 25, 28, 25, 28, 28),+ "crumb_shake": (28, 27, 29, 27, 29, 28, 28, 28),+ "lever_bounce": (28, 31, 34, 34, 22, 25, 27, 28),+ "butter_shine": (28,) * 8,+ "heat_shimmer": (28, 30, 32, 32, 32, 32, 25, 28),+ }+ act_glows: dict[str, tuple[int, ...]] = {+ "lever_bounce": (0, 0, 1, 2, 0, 0, 0, 0),+ "heat_shimmer": (0, 1, 1, 2, 2, 2, 1, 0),+ }+ act_blush: dict[str, tuple[int, ...]] = {+ "toast_pop": (7,),+ "butter_shine": (5, 6),+ "heat_shimmer": (3, 4, 5),+ }++ # -- Pose -------------------------------------------------------------------+ def toasty_pose(state: str, i: int) -> Pose:+ if state == "held":+ mouth = ("half", "open")[i % 2]+ return Pose(state, i, dx=bp.HELD_SWAY[i], dy=-2, eyes="wide", mouth=mouth, stretch=1)+ if state == "thinking":+ eyes = "up_left" if i < 4 else "up_right"+ return Pose(state, i, eyes=eyes, mouth="closed", stretch=(0, 0, 0, 1, 1, 0, 0, 0)[i])+ if state == "error":+ mouth = ("frown", "half", "frown", "half", "frown", "frown")[i]+ return Pose(state, i, dx=bp.SHAKE[i], eyes="x", mouth=mouth)+ return bp.base_pose(state, i)++ def act_pose(name: str, **columns):+ def pose(i: int) -> Pose:+ fields = {key: values[i] for key, values in columns.items()}+ return Pose("idle", i, act=name, **fields)++ return pose++ # -- Lookups ----------------------------------------------------------------+ def _tops(pose: Pose) -> tuple[int, int]:+ table = act_tops.get(pose.act) if pose.act else tops.get(pose.state)+ return table[pose.i] if table is not None else (rest, rest)++ def _toast_dx(pose: Pose) -> tuple[int, int]:+ table = act_toast_dx.get(pose.act) if pose.act else None+ return table[pose.i] if table is not None else (0, 0)++ def _lever(pose: Pose) -> int:+ table = act_levers.get(pose.act) if pose.act else levers.get(pose.state)+ return table[pose.i] if table is not None else lever_rest++ def _glow(pose: Pose) -> int:+ table = act_glows.get(pose.act) if pose.act else glows.get(pose.state)+ return table[pose.i] if table is not None else 0++ def _dial_dir(pose: Pose) -> tuple[int, int]:+ if pose.act == "heat_shimmer" or pose.state in ("thinking", "working"):+ return dial_dirs[(pose.i + 1) % 8]+ if pose.state == "error":+ return dial_dirs[(3 * pose.i) % 8]+ if pose.state == "sleeping":+ return dial_dirs[6]+ return dial_dirs[1]++ # -- Parts ------------------------------------------------------------------+ def _slice(x0: int, top: int) -> set:+ """A bread slice: a rounded crown with shoulders over a narrower body."""+ crown = rounded_rect(x0, top, x0 + 8, top + 3, 2)+ return crown | rect(x0 + 1, top + 2, x0 + 7, top + 16)++ def _draw_toast(f, x0: int, top: int, kind: tuple, glowing: int, edge: bool = False) -> None:+ if top >= sunk:+ return+ crust, crumb_c, high = kind+ if edge:+ # Flipping in the air, seen edge-on: a thin slab of crust.+ slab = rect(x0, top + 4, x0 + 8, top + 5)+ f.part(slab, {p: crumb_c if p[1] == top + 4 else crust for p in slab})+ f.paint({(x0 + 1, top + 4), (x0 + 2, top + 4)}, high)+ return+ full = _slice(x0, top)+ colors = shade(full, crumb_c, rim=crust)+ if glowing >= 2 and kind is not toast_burnt:+ for p in list(colors):+ if p[1] >= deck - 3 and colors[p] == crumb_c:+ colors[p] = toast_hot[1]+ colors[(x0 + 2, top + 1)] = high+ colors[(x0 + 3, top + 1)] = high+ colors[(x0 + 1, top + 2)] = high+ if kind is toast_burnt:+ # Charred: a few black specks on the crumb.+ for p in ((x0 + 5, top + 2), (x0 + 3, top + 4), (x0 + 6, top + 4)):+ colors[p] = crust+ else:+ # A darker toasted patch in the middle of the slice.+ for p in ((x0 + 4, top + 3), (x0 + 5, top + 3), (x0 + 5, top + 4)):+ colors[p] = toast_hot[1] if kind is toast_golden else crumb_c+ shown = {p for p in full if p[1] <= deck}+ f.part(shown, {p: colors[p] for p in shown})++ def _draw_slots(f, glowing: int) -> None:+ for x0 in slots:+ f.paint(rect(x0 - 1, deck, x0 + 9, deck), OUTLINE)++ def _glow_slots(f, glowing: int) -> None:+ if not glowing:+ return+ for x0 in slots:+ edge = {(x0 - 1, deck), (x0, deck), (x0 + 8, deck), (x0 + 9, deck)}+ f.paint(edge, glow if glowing >= 2 else glow_light)+ if glowing >= 2:+ f.paint({(x0 - 1, deck + 1), (x0 + 9, deck + 1)}, glow_light)++ def _draw_lever(f, top: int) -> None:+ stem = rect(36, top + 1, 37, top + 1)+ f.paint(stem, chrome_dark)+ knob = rounded_rect(38, top, 42, top + 2, 1)+ f.part(knob, shade(knob, lever, light=lever_light, dark=lever_dark, dark_depth=1))++ def _draw_dial(f, direction: tuple[int, int]) -> None:+ cx, cy = dial_c+ disc = ellipse(cx + 0.5, cy + 0.5, 2.6, 2.6)+ f.part(disc, shade(disc, chrome, light=chrome_light, dark=chrome_dark, dark_depth=1))+ dx, dy = direction+ f.paint({(cx, cy), (cx + dx, cy + dy)}, ink)++ def _feet(pose: Pose) -> set:+ left = rounded_rect(13, 41, 17, 42, 1)+ if pose.state == "held":+ kick = 1 if pose.i % 2 == 0 else 0+ return shifted(left, 0, kick) | shifted(mirrored(left), 0, 1 - kick)+ return left | mirrored(left)++ def draw_toasty(f, pose: Pose) -> None:+ base, light, dark = enamel_cool if pose.state == "sleeping" else enamel+ kind = toast_golden+ if pose.state == "error":+ kind = toast_burnt+ glowing = _glow(pose)+ left_top, right_top = _tops(pose)+ ldx, rdx = _toast_dx(pose)+ with f.offset(pose.dx, pose.dy):+ feet = _feet(pose)+ f.part(feet, shade(feet, foot, light=foot_light, dark=chrome_dark, dark_depth=1))+ body = rounded_rect(10, deck, 37, 40, 3)+ f.part(body, shade(body, base, light=light, dark=dark, dark_depth=2))+ # The enamel's shine: a soft highlight in the upper left corner.+ f.paint({(12, 24), (12, 25), (13, 23), (14, 23)}, FX_WHITE)+ # The chrome band, with its own glint.+ band = rect(11, 35, 36, 37)+ band_colors = {+ p: chrome_light if p[1] == 35 else chrome if p[1] == 36 else chrome_dark+ for p in band+ }+ f.paint_map(band_colors)+ f.paint({(10, 35), (10, 36), (10, 37), (37, 35), (37, 36), (37, 37)}, chrome_dark)+ glint_x = band_glint[pose.i] if pose.state == "idle" and not pose.act else None+ if glint_x is not None:+ f.paint({(glint_x, 36), (glint_x + 1, 36), (glint_x + 2, 36)}, chrome_light)+ else:+ f.paint({(14, 36), (15, 36)}, chrome_light)+ _draw_slots(f, glowing)+ edge = pose.act == "toast_pop" and pose.i == 3+ _draw_toast(f, slots[0] + ldx, left_top, kind, glowing, edge)+ _draw_toast(f, slots[1] + rdx, right_top, kind, glowing, edge)+ _glow_slots(f, glowing)+ _draw_lever(f, _lever(pose))+ _draw_dial(f, _dial_dir(pose))+ # The face.+ bp.draw_eyes(f, pose.eyes, (15, 26), (30, 26), eyes_style)+ cheeks = rect(12, 31, 13, 31) | rect(34, 31, 35, 31)+ wide = pose.state in ("success", "held") or pose.i in act_blush.get(pose.act, ())+ if wide:+ cheeks |= {(12, 30), (35, 30)}+ f.paint(cheeks, blush)+ bp.draw_mouth(f, pose.mouth, 22, 30, 4, color=ink)++ # -- Effects ----------------------------------------------------------------+ def _puff(f, x: int, y: int, r: float) -> None:+ mask = ellipse(x + 0.5, y + 0.5, r, r)+ f.part(mask, shade(mask, smoke, light=smoke_light, dark=smoke, dark_depth=1))++ def _shimmer(f, x: int, y: int, length: int, phase: int, color) -> None:+ """A thin wavy thread of hot air rising from ``(x, y)``."""+ wave = (0, 1, 1, 0, -1, -1)+ f.paint({(x + wave[(k + phase) % 6], y - k) for k in range(length)}, color)++ def _juggle_fx(f, pose: Pose) -> None:+ """Whoosh ticks under each slice as it is tossed up."""+ if pose.i in (1, 2):+ f.paint(line(16, 21, 16, 19) | line(20, 21, 20, 19), bp.ARC_FADE)+ if pose.i in (4, 5):+ f.paint(line(27, 21, 27, 19) | line(31, 21, 31, 19), bp.ARC_FADE)+ if pose.i == 3:+ bp.fx_sparkles(f, 0, ((23, 7, 0),))++ def _crumbs(f, pixels) -> None:+ f.paint({(x, y) for x, y in pixels} | {(x + 1, y) for x, y in pixels}, crumb)++ def toasty_thinking(f, pose: Pose) -> None:+ """Thinking dots, and heat shimmering up off the glowing slices."""+ with f.offset(pose.dx, pose.dy):+ bp.fx_dots(f, pose.i, 19, 4)+ for k, x0 in enumerate(slots):+ x = x0 + 4+ y = rest - 3 - (pose.i % 2)+ color = heat if (pose.i + k) % 2 == 0 else heat_fade+ _shimmer(f, x, y, 4, pose.i + 3 * k, color)++ def toasty_talking(f, pose: Pose) -> None:+ """At the loudest, a crumb or two jumps out of the slots."""+ with f.offset(pose.dx, pose.dy):+ if pose.i == 3:+ _crumbs(f, ((18, 11), (29, 10)))+ elif pose.i == 2:+ _crumbs(f, ((19, 13),))++ def toasty_success(f, pose: Pose) -> None:+ bp.fx_sparkles(f, pose.i, anchors.sparkles)+ if 2 <= pose.i <= 4:+ # Pop! Ticks beside the slices at the top of their flight.+ with f.offset(pose.dx, pose.dy):+ f.paint(line(10, 14, 8, 12) | line(37, 14, 39, 12), bp.ARC)++ #: Error: dark smoke puffs from the slots, (dx, dy, radius) per frame.+ smoke_puffs = (+ ((4, -4, 1.6),),+ ((3, -6, 2.2), (5, -3, 1.4)),+ ((2, -9, 2.6), (5, -5, 2.0)),+ ((1, -12, 2.6), (4, -8, 2.4)),+ ((0, -14, 2.0), (3, -11, 2.4)),+ ((2, -13, 1.6),),+ )++ def toasty_error(f, pose: Pose) -> None:+ """Burnt! Dark smoke billows from the slots and the "!" pops up."""+ bp.fx_bang(f, *anchors.bang)+ with f.offset(pose.dx, pose.dy):+ for k, x0 in enumerate(slots):+ for dx, dy, r in smoke_puffs[pose.i]:+ x = x0 + dx + (2 * k)+ _puff(f, x, deck - 3 + dy, r)+ with f.offset(pose.dx, 0):+ bp.fx_sweat(f, pose.i, *anchors.sweat)++ def toasty_sleeping(f, pose: Pose) -> None:+ bp.fx_zzz(f, pose.i, *anchors.zzz)++ #: Working: keystroke ticks above the lid per frame, (dx, dy) from its top-left.+ key_ticks = (((3, -2),), ((9, -2),), ((5, -2), (11, -3)), (), ((7, -2),), ((2, -3),), (), ())+ code_glyphs = (("#.#", "#.#", ".#."), ("##.", ".##", "##."), (".#.", "##.", ".#."))++ def toasty_working(f, pose: Pose) -> None:+ """Typing on a laptop while the slots glow softly; instead of toast,+ little code chips pop out of the slots."""+ x, y = anchors.laptop+ lid = rounded_rect(x, y, x + 13, y + 7, 1)+ f.part(lid, shade(lid, bp.LAPTOP_LID, light=bp.LAPTOP_LID_LIGHT, dark=bp.LAPTOP_LID))+ logo = bp.LAPTOP_LOGO if pose.i % 4 != 3 else bp.LAPTOP_LID_LIGHT+ f.paint(rect(x + 6, y + 3, x + 7, y + 4), logo)+ base = rect(x - 2, y + 8, x + 15, y + 9)+ f.part(base, {p: bp.LAPTOP_BASE if p[1] == y + 8 else bp.LAPTOP_BASE_DARK for p in base})+ ticks = key_ticks[pose.i]+ if ticks:+ f.paint(rect(x + 2, y - 1, x + 11, y - 1), bp.SCREEN_GLOW)+ for dx, dy in ticks:+ f.paint({(x + dx, y + dy - 1), (x + dx, y + dy - 2)}, FX_WHITE)+ with f.offset(pose.dx, pose.dy):+ # One chip pops out of a slot every four frames, alternating slots.+ age = pose.i % 4+ k = (pose.i // 4) % 2+ glyph = code_glyphs[(pose.i // 4 + 1) % len(code_glyphs)]+ gx = slots[k] + 3 + (k * 2 - 1) * (age // 2)+ f.glyph(gx, rest - 5 - 2 * age, glyph, {"#": bp.CODE_CHIP})+ if age in (1, 3):+ _shimmer(f, slots[1 - k] + 4, rest - 2, 2, pose.i, heat_fade)++ # -- Idle acts' effects -----------------------------------------------------+ def _pop_fx(f, pose: Pose) -> None:+ """Both slices spring out, flip at the top and land back with a ding."""+ i = pose.i+ if i == 2:+ f.paint(line(11, 18, 9, 16) | line(36, 18, 38, 16), bp.ARC)+ if i in (3, 4):+ f.paint(line(12, 5, 10, 5) | line(35, 5, 37, 5), bp.ARC_FADE)+ if i == 7:+ f.paint(line(11, 15, 9, 13) | line(36, 15, 38, 13), bp.ARC)+ bp.fx_sparkles(f, 0, ((40, 12, 0),))++ #: Crumb shake: crumbs per frame, flying out of the slots and settling.+ shake_crumbs = (+ (),+ ((16, 14), (30, 15)),+ ((13, 12), (33, 11), (22, 13)),+ ((11, 15), (36, 14), (21, 9), (26, 12)),+ ((9, 21), (38, 20), (20, 14), (27, 17)),+ ((8, 30), (39, 30), (19, 20), (28, 22)),+ ((7, 42), (40, 42), (5, 43), (42, 43)),+ (),+ )++ def _crumb_fx(f, pose: Pose) -> None:+ _crumbs(f, shake_crumbs[pose.i])+ if pose.i in (1, 2, 3, 4):+ side = -1 if pose.dx > 0 else 1+ x = 8 if side < 0 else 39+ f.paint(line(x, 26, x, 30), bp.ARC_FADE)++ def _lever_fx(f, pose: Pose) -> None:+ """Click down, a beat of toasting, then BOING: everything springs up."""+ i = pose.i+ if i == 1:+ f.paint(line(44, 30, 45, 32), bp.ARC)+ if i in (2, 3):+ # The slots heat up: a shimmer each.+ for k, x0 in enumerate(slots):+ _shimmer(f, x0 + 4, deck - 2, 3 + i - 2, i + 2 * k, heat)+ if i == 4:+ f.paint(line(44, 20, 46, 18) | line(44, 24, 46, 24), bp.ARC)+ f.paint(line(11, 9, 9, 7) | line(36, 9, 38, 7), bp.ARC)+ if i == 5:+ f.paint(line(44, 23, 46, 22), bp.ARC_FADE)++ #: Butter: the pat's (x, y) top-left per frame (None = not there yet).+ butter_fall = (None, (27, 1), (27, 6), (27, 11), None, None, None, None)++ def _butter_fx(f, pose: Pose) -> None:+ """A pat of butter drops onto the right slice and melts in; it shines."""+ i = pose.i+ at = butter_fall[i]+ if at is not None:+ pat = rect(at[0], at[1], at[0] + 4, at[1] + 2)+ f.part(pat, {p: butter if p[1] == at[1] or p[0] == at[0] else butter_dark for p in pat})+ f.paint({(at[0] + 1, at[1])}, FX_WHITE)+ x0, top = slots[1], rest + (1 if pose.stretch < 0 else 0)+ if i == 4:+ pat = rect(x0 + 1, top - 1, x0 + 7, top)+ f.part(pat, {p: butter if p[1] == top - 1 else butter_dark for p in pat})+ ticks = line(x0 - 1, top - 3, x0 - 2, top - 4) | line(x0 + 9, top - 3, x0 + 10, top - 4)+ f.paint(ticks, bp.ARC)+ elif i in (5, 6):+ # Melted: a glossy pool on the crumb with a drip down the side.+ f.paint(rect(x0 + 2, top + 1, x0 + 6, top + 1), butter)+ drip = 1 + min(i - 5, 2)+ f.paint(rect(x0 + 6, top + 2, x0 + 6, top + 1 + drip), butter_dark)+ f.paint({(x0 + 3, top + 1)}, FX_WHITE)+ if i in (5, 6):+ bp.fx_sparkles(f, i - 4, ((x0 + 11, top - 3, 0),))++ def _heat_fx(f, pose: Pose) -> None:+ """Toasty basks: the slots glow, heat shimmers up and the dial turns;+ then the toast pops with a ding."""+ i = pose.i+ if 2 <= i <= 5:+ for k, x0 in enumerate(slots):+ color = heat if (i + k) % 2 == 0 else heat_fade+ top = _tops(pose)[k] - pose.stretch+ _shimmer(f, x0 + 4, top - 2 - (i % 2), 3 + min(i - 2, 2), i + 3 * k, color)+ if i == 6:+ f.paint(line(11, 13, 9, 11) | line(36, 13, 38, 11), bp.ARC)+ bp.fx_sparkles(f, 1, ((41, 9, 0),))+ if i == 7:+ bp.fx_sparkles(f, 3, ((41, 9, 0),))++ acts = (+ bp.IdleAct(+ "toast_pop",+ 8,+ 10,+ act_pose(+ "toast_pop",+ eyes=("open", "closed", "wide", "up_left", "up_right", "wide", "open", "happy"),+ mouth=("rest", "closed", "open", "open", "wide", "half", "rest", "smile"),+ stretch=(0, -1, 1, 0, 0, 0, -1, 0),+ ),+ _pop_fx,+ ),+ bp.IdleAct(+ "toast_juggle",+ 8,+ 8,+ act_pose(+ "toast_juggle",+ dx=(0, -1, -1, 0, 1, 1, 0, 0),+ eyes=(+ "open",+ "up_left",+ "up_left",+ "up_right",+ "up_right",+ "up_right",+ "open",+ "happy",+ ),+ mouth=("rest", "half", "open", "rest", "open", "half", "rest", "smile"),+ ),+ _juggle_fx,+ ),+ bp.IdleAct(+ "crumb_shake",+ 8,+ 10,+ act_pose(+ "crumb_shake",+ dx=(0, -2, 2, -2, 2, -1, 0, 0),+ eyes=("open", "closed", "closed", "closed", "closed", "open", "happy", "open"),+ mouth=("rest", "wide", "open", "wide", "open", "closed", "smile", "rest"),+ ),+ _crumb_fx,+ ),+ bp.IdleAct(+ "lever_bounce",+ 8,+ 8,+ act_pose(+ "lever_bounce",+ dy=(0, 0, 0, 0, -2, -1, 0, 0),+ stretch=(0, -1, -1, 0, 1, 0, 0, 0),+ eyes=("open", "closed", "down", "down", "wide", "happy", "happy", "open"),+ mouth=("rest", "closed", "closed", "closed", "open", "smile", "smile", "rest"),+ ),+ _lever_fx,+ ),+ bp.IdleAct(+ "butter_shine",+ 8,+ 8,+ act_pose(+ "butter_shine",+ stretch=(0, 0, 0, 0, -1, 0, 0, 0),+ eyes=("open", "up_right", "up_right", "up_right", "wide", "happy", "happy", "open"),+ mouth=("rest", "half", "half", "open", "wide", "smile", "smile", "rest"),+ ),+ _butter_fx,+ ),+ bp.IdleAct(+ "heat_shimmer",+ 8,+ 8,+ act_pose(+ "heat_shimmer",+ eyes=("open", "down", "happy", "happy", "happy", "happy", "wide", "open"),+ mouth=("rest", "closed", "smile", "smile", "smile", "smile", "open", "rest"),+ stretch=(0, 0, 0, 1, 1, 0, 1, 0),+ ),+ _heat_fx,+ ),+ )++ anchors = bp.Anchors(+ arcs_left=(11, 14),+ arcs_right=(36, 14),+ dots=(19, 4),+ zzz=(30, 15),+ bang=(41, 3),+ sweat=(39, 13),+ sparkles=((6, 12, 1), (38, 4, 2), (4, 38, 3), (43, 30, 3)),+ laptop=(17, 34),+ magnifier=(bp.CX, 11),+ )++ return bp.PetDesign(+ id="toasty",+ name="Toasty",+ description="A cheerful toaster that glows while it thinks and pops up when it's done.",+ draw=draw_toasty,+ pose=toasty_pose,+ anchors=anchors,+ fx={+ "thinking": toasty_thinking,+ "talking": toasty_talking,+ "success": toasty_success,+ "error": toasty_error,+ "sleeping": toasty_sleeping,+ "working": toasty_working,+ },+ acts=acts,+ waist=40,+ )diff --git a/tests/unit/ui/pets/test_builtin_pets.py b/tests/unit/ui/pets/test_builtin_pets.pyindex 683e49849e..ec19267cf2 100644--- a/tests/unit/ui/pets/test_builtin_pets.py+++ b/tests/unit/ui/pets/test_builtin_pets.py@@ -27,6 +27,17 @@ EXPECTED_PETS = ("ember","cocoa","dodaios",+ "pip",+ "hazel",+ "otto",+ "nox",+ "kiko",+ "inky",+ "toasty",+ "cassie",+ "sprout",+ "flick",+ "aurelia",)