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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console