terra is a super customizable framework for creating and analyzing biological simulations. It's open-source and licenced under MIT.
Usage
Including terra
Getting started is as easy as including the script!
<script src="path/to/terra.min.js"></script>
terra can also be used as a module with most popular module systems. ?
Creating creatures
Now that we've included the script, let's create a simple creature using the creatureFactory. Each creature requires a type.
terra.creatureFactory.register({
type: 'firstCreature'
});
This creature is valid, but it's pretty boring. To make a more interesting creature, let's override some of the default options and methods.
terra.creatureFactory.register({
type: 'secondCreature',
color: [41, 128, 185],
metabolism: 1,
sustainability: 3,
reproduceLv: 0.60,
etc...
});
We've just created a creature that will ____.
Creating the environment
To run a simulation, we'll need to create an environment. Let's make a 25x25 grid and populate 40% of the space with our first creature and 20% with our second.
var ex1 = new terra.Terrarium(25, 25, 'ex1');
ex1.populate([['firstCreature', 40], ['secondCreature', 20]]);
Running a simulation
Terrariums have a few methods that allow you to interact with the simulation. The most important are step
and draw
.
function stepAndDraw () {
// run the next step of the simulation
ex1.step();
// update its display
ex1.draw();
}
// start an interval to update the simulation twice per second
var updater = setInterval(stepAndDraw, 500);
step
can take an argument for the number of steps to run through. If we want to see how our environment is doing in 1000 steps, we can simply run:
ex1.step(1000);
ex1.draw();
Examples
Conway's Game of Life ?
terra can also run cellular automata.
terra.creatureFactory.register({
type: 'GoL',
colorFn: function () { return this.alive ? this.color + ',1' : '0,0,0,0'; },
wait: function () {},
isDead: function () { return false; },
queue: function (neighbors) {
var surrounding = _.filter(neighbors, function (spot) {
return spot.creature.alive;
}).length;
this.alive = surrounding === 3 || surrounding === 2 && this.alive;
return false;
}
}, function () {
this.alive = Math.random() < 0.5;
});
Cyclic Cellular Automata
terra.creatureFactory.register({
type: 'cyclic',
colors: ['255,0,0,1', '255,217,0,1', '72,255,0,1', '0,255,145,1', '0,145,255,1', '72,0,255,1', '255,0,217,1'],
colorFn: function () { return this.colors[this.state];},
wait: function () {},
isDead: function () { return false; },
queue: function (neighbors) {
var next = (this.state + 1) % 7;
var changing = _.some(neighbors, function (spot) {
return spot.creature.state === next;
});
if (changing) this.state = next;
}
}, function () {
this.state = Math.floor(Math.random() * 7);
});
Options
The following options can be passed in an object to creatureFactory.register( ) as the first argument.
Required
- (string) type:
Optional
- (array) color:
- (int) initialEnergy:
- (int) maxEnergy:
- (int) metabolism:
- (int) size:
- (int) speed:
- (int) vision:
- (int) sustainability: 2
- (float) reproduceLv: 0.70
- (float) moveLv: 0.20
- (char) character:
Methods
- queue
- reproduce
- move
- wait
- isDead
- boundEnergy
Events
- finished