README excerpt
# Boids in Pharo
[Boids](https://en.wikipedia.org/wiki/Boids), Craig Reynolds' flocking
simulation (1987), in [Pharo](https://pharo.org) 14, flying in a Morphic
window.
It's a port of [Boids in Niva](https://github.com/dharmatech/boids-niva),
which was inspired by Boids in Aloe. There's also a Scheme port in
Schevana. As in all three, the simulation is pure: stepping a flock
returns a new one.
## Loading
In a Playground:
```smalltalk
Metacello new
baseline: 'Boids';
repository: 'tonel:///path/to/boids-pharo/src';
load.
```
Then open a window with 150 boids:
```smalltalk
BoidsMorph open.
```
The tests are `BoidFlockTest` and `BoidsMorphTest`.
## Keys
Click the flock to give it the keyboard.
| Key | What |
|---|---|
| **R** | Random rules and speed limits |
| **D** | The default rules and speed limits |
| **S** | Scatter: a fresh random flock under the same rules |
| **Space** | Pause and resume |
| **H** | Hide or show the rules |
## Poking at it
Everything is a live object. `BoidsMorph open` answers the morph, and
`BoidsMorph allInstances first` finds it again:
```smalltalk
| morph |
morph := BoidsMorph allInstances first.
morph flock boids first. "a Boid at (…) moving (…)"
morph rules: (morph rules withRule: #viewAngle setTo: 270.0).
morph count: 300.
morph maxSpeed: 6.0.
```
With no window:
```smalltalk
BoidFlock demo step. "three boids, one step on"
(BoidFlock random: 150 across: 900 @ 650) stepOn: 900 @ 650.
BoidRules random.
```
## Packages
| Package | What |
|---|---|
| `Boids` | The simulation: `Boid`, `BoidRules` and `BoidFlock`. Vectors are `Point`s. |
| `Boids-World` | What a screen needs: speed limits, edge wrapping, random flocks (`stepOn:`, `random:across:`, `resizedTo:across:`). These are extension methods on `Boid` and `BoidFlock`, the way Niva's `world/` extends `sim/`. |
| `Boids-Morphic` | `BoidsMorph`, the window. |
| `Boids-Tests` | SUnit tests. The first pins the demo's first step to the same exact values as Niva's. |
Every class name starts with `Boid`, because Pharo has one global
namespace.
## The rules
Besides Reynolds' three rules (cohesion, alignment and separation, each
with a weight and a radius, plus the view angle), two rules react to
*blockers*. A blocker is a boid within `blockerRadius`, inside a cone
`blockerCone` degrees wide, straight ahead.
- **Clear view** steps sideways out from behind each blocker, toward the
side the boid is already heading.
- **Back off** steps straight away from each blocker.
They come from Gary William Flake's *The Computational Beauty of Nature*
(MIT Press, 1998), whose boids add a fourth rule to Reynolds' three: keep
a clear view ahead. Both are off by default. The code is written from his
description, not copied from his (GPL) source.
`Boids-World` caps the speed at 4 and keeps it above 1. With the default
rules, boids only see ahead, so cohesion pulls stragglers forward and
nothing pulls the leaders back: speed grows without bound. Back off,
unchecked, brakes boids almost to a standstill. Niva's README has the
details.