cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              var ideal_energy_total = 1500;

var agents = [];
var agent_energy_min = 0.1;
var agent_energy_replicate = 1.;
var agent_eating_efficiency = 0.1;
var agent_digestion_efficiency = 0.2;
var agent_age_cost = 0.001;
var agent_locomotion_cost = 10;
var agent_population_max = 50;

function make_agent() {
  return {
    pos: new vec2(random(), random()), //.add(0.5),
    vel: vec2.random(0.0001),
    size: 0.02,

    // stored recollection of previous sense
    sense_memory: 0.5,

    energy: 1 + random(),
    age: 0,
  };
}

var food = new field2D(128);
for (var i = 0; i < ideal_energy_total; i++) {
  //food.deposit(1, random(), random());
}
var food_past = food.clone();
var source = {
  pos: new vec2(0.5, 0.5),
  vel: vec2.random(),
};

function update() {

  var world_energy_total = food.sum();

  var i = agents.length;
  while (i--) {
    var a = agents[i];
    if (a.energy <= agent_energy_min) {
      // dead.
      agents.splice(i, 1);
    } else {
      world_energy_total += a.energy;

      var energy_use = 0;

      // age:
      a.age += dt;

      // sense environment:
      var sense = food.sample(a.pos);
      // sanity check
      sense = Math.max(sense, 0);

      // what you can eat may be less than what you sense
      var edible = Math.min(sense, 1) * agent_eating_efficiency;
      // remove from field
      food.deposit(-edible, a.pos);
      // gain energy from food:
      a.energy += edible * agent_digestion_efficiency;

      // reproduce?
      if (a.energy > agent_energy_replicate) {
        if (agents.length < agent_population_max) {
          // reproduce:
          a.energy *= 0.4;

          var child = make_agent();
          child.pos.set(a.pos);
          child.vel.set(a.vel);
          child.energy = a.energy;

          agents.push(child);
        } else {
          // limit:
          a.energy = agent_energy_replicate;
        }
      }

      // compute desired velocity:
      var newvel = a.vel.clone();

      // compare senses:
      var change = sense / a.sense_memory;
      if (change > 1) {
        // getting better
        // forward swimming behavior:
        newvel
          .rotate(srandom() * 0.1)
          .len(0.002);
      } else {
        // getting worse
        // tumbling behavior:
        newvel
          .rotate(srandom())
          .len(0.0002);
      }
      // remember for the next time:
      a.sense_memory = sense;

      // random walk
      var dev = srandom() * 0.1;
      newvel.rotate(dev);

      // compute acceleration:
      var acc = newvel.sub(a.vel);

      // locomotion
      a.vel.add(acc);
      a.pos.add(a.vel).wrap(1);

      // compute energy losses
      // locomotion
      // var force = acc * a.size; // need to make this fair by introducing other effects of a.size, e.g. amount it can eat
      var force = acc; // temporary simplification
      energy_use += agent_locomotion_cost * force.len();

      // energy use for metabolism
      // weakens with age
      energy_use += agent_age_cost * a.age

      //write(energy_use);
      // lose energy
      a.energy -= energy_use;
    }
  }

  // if the world all dies out, try again:
  if (agents.length < 1) {
    agents.push(make_agent());
    agents.push(make_agent());
    agents.push(make_agent());
    agents.push(make_agent());
  }

  // want to keep total food steady:
  var deficit = ideal_energy_total - world_energy_total

  // rain some food:
  write(deficit);
  if (deficit > 0) {
    // apply source agent to sugar:
    food.deposit(deficit, source.pos);
    // and then move it (random walk):
    source.vel.rotate(random() - 0.5).len(random() * 0.003);
    source.pos.add(source.vel).wrap(1);
  }

  var tmp = food;
  food = food_past;
  food_past = tmp;
  food.diffuse(food_past, 0.1);
  food.mul(0.99); // global drain
}

function mouse(e, p) {
  if (e == "down") {
    // move agent zero
    agents[0].pos.set(p);
  } else {
    // drop sugar
    source.pos.set(p);
  }
}

function draw() {
  draw2D.color("darkgreen");
  food.smooth = true;
  food.draw();

  for (var a of agents) {

    draw2D.push()
      .translate(a.pos)
      .rotate(a.vel.angle())
      .scale(a.size);

    var hue = 0.5;
    var sat = 2 / (2 + a.age);
    draw2D.hsl(hue, sat, 0.3)
      .circle([+0.2, 0])
      .circle([-0.2, 0]);

    var mouthsize = Math.sin(10 * (a.energy + now)) * a.energy;
    draw2D.hsl(hue, sat, 0.7)
      .circle([+0.2, 0], mouthsize);
    draw2D.hsl(hue, 0.5, a.sense_memory * 10)
      .circle([0.4, +0.2], 0.2)
      .circle([0.4, -0.2], 0.2);
    draw2D.pop();
  }
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console