// Seek_Arrive
// Daniel Shiffman <http://www.shiffman.net>
// Adapted for Eclipse for Robert Carlsen

// The "Boid" class

// Created 2 May 2005
import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PVector;

public class Boid {
  PApplet parent;
  
  PVector loc;
  PVector vel;
  PVector acc;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

  int color;
  
  float lifespan=100;
  float lifeDecrement = 1.0f;
  
  boolean debug = false;
  
  Boid(PApplet _p, PVector l, float ms, float mf) {
    parent = _p;
//    acc = new PVector(0,0);
//    vel = new PVector(0,0);
    acc = new PVector(_p.random(-1,1),_p.random(-1,1) );
    vel = new PVector(_p.random(-1,1),_p.random(-1,1) );
    loc = l.get();
    r = 20.0f; // originally 3.0f;
    maxspeed = ms;
    maxforce = mf;

  }
  
  Boid(PApplet _p, PVector l, float ms, float mf, int _c) {
    parent = _p;
//    acc = new PVector(0,0);
//    vel = new PVector(0,0);
    acc = new PVector(_p.random(-1,1),_p.random(-1,1) );
    vel = new PVector(_p.random(-1,1),_p.random(-1,1) );
    loc = l.get();
    r = 25.0f; // originally 3.0f;
    maxspeed = ms;
    maxforce = mf;
    color = _c;
  }
  
  void run() {
    update();
    borders();
    //render();
    renderPacket();
  }
  
  void run(ArrayList<Boid> boids) {
    flock(boids);
    update();
    borders();
//    render();
    renderPacket();
  }
  
  // Method to update location
  void update() {
    // Update velocity
    vel.add(acc);
    // Limit speed
    vel.limit(maxspeed);
    loc.add(vel);
    // Reset accelertion to 0 each cycle
    acc.mult(0);
    
    // decrement the lifespan
    lifespan -= lifeDecrement;
  }

  void seek(PVector target) {
    acc.add(steer(target,false));
  }
 
  void arrive(PVector target) {
    acc.add(steer(target,true));
  }

  // A method that calculates a steering vector towards a target
  // Takes a second argument, if true, it slows down as it approaches the target
  PVector steer(PVector target, boolean slowdown) {
    PVector steer;  // The steering vector
    PVector desired = PVector.sub(target,loc);  // A vector pointing from the location to the target
    float d = desired.mag(); // Distance from the target is the magnitude of the vector
    // If the distance is greater than 0, calc steering (otherwise return zero vector)
    if (d > 0) {
      // Normalize desired
      desired.normalize();
      // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
      if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary
      else desired.mult(maxspeed);
      // Steering = Desired minus Velocity
      steer = PVector.sub(desired,vel);
      steer.limit(maxforce);  // Limit to maximum steering force
    } else {
      steer = new PVector(0,0);
    }
    return steer;
  }
  
  // Implementing Reynolds' flow field following algorithm
  // http://www.red3d.com/cwr/steer/FlowFollow.html
  void follow(FlowField f) {

    // Look ahead
    PVector ahead = vel.get();
    ahead.normalize();
    ahead.mult(32); // Arbitrarily look 32 pixels ahead
    PVector lookup = PVector.add(loc,ahead);


    
    // Draw in debug mode
    if (debug) {
      parent.stroke(0);
      parent.line(loc.x,loc.y,lookup.x,lookup.y);
      parent.fill(0);
      parent.ellipse(lookup.x,lookup.y,3,3);
    }

    // What is the vector at that spot in the flow field?
    PVector desired = f.lookup(lookup);
    // Scale it up by maxspeed
    desired.mult(maxspeed);
    // Steering is desired minus velocity
    PVector steer = PVector.sub(desired, vel);
    steer.limit(maxforce);  // Limit to maximum steering force
    acc.add(steer);
  }
  

  // We accumulate a new acceleration each time based on three rules
  void flock(ArrayList<Boid> boids) {
    PVector sep = separate(boids);   // Separation
    PVector ali = align(boids);      // Alignment
    PVector coh = cohesion(boids);   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(10.0f);
    ali.mult(1.0f);
    coh.mult(2.5f);
    // Add the force vectors to acceleration
    acc.add(sep);
    acc.add(ali);
    acc.add(coh);
  }
  
//Separation
  // Method checks for nearby boids and steers away
  PVector separate (ArrayList<Boid> boids) {
    float desiredseparation = 50.0f;
    PVector sum = new PVector(0,0,0);
    int count = 0;
    // For every boid in the system, check if it's too close
    for (int i = 0 ; i < boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = PVector.dist(loc,other.loc);
      // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
      if ((d > 0) && (d < desiredseparation)) {
        // Calculate vector pointing away from neighbor
        PVector diff = PVector.sub(loc,other.loc);
        diff.normalize();
        diff.div(d);        // Weight by distance
        sum.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count > 0) {
      sum.div((float)count);
    }
    return sum;
  }
  
  // Alignment
  // For every nearby boid in the system, calculate the average velocity
  PVector align (ArrayList<Boid> boids) {
    float neighbordist = 50.0f;
    PVector sum = new PVector(0,0,0);
    int count = 0;
    for (int i = 0 ; i < boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = PVector.dist(loc,other.loc);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.vel);
        count++;
      }
    }
    if (count > 0) {
      sum.div((float)count);
      sum.limit(maxforce);
    }
    return sum;
  }

  // Cohesion
  // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
  PVector cohesion (ArrayList<Boid> boids) {
    float neighbordist = 50.0f;
    PVector sum = new PVector(0,0,0);   // Start with empty vector to accumulate all locations
    int count = 0;
    for (int i = 0 ; i < boids.size(); i++) {
      Boid other = (Boid) boids.get(i);
      float d = PVector.dist(loc,other.loc);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.loc); // Add location
        count++;
      }
    }
    if (count > 0) {
      sum.div((float)count);
      return steer(sum,false);  // Steer towards the location
    }
    return sum;
  }
  
  void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = vel.heading2D() + PApplet.radians(90);
    parent.fill(175);
    parent.stroke(0);
    parent.pushMatrix();
    parent.translate(loc.x,loc.y);
    parent.rotate(theta);
    parent.beginShape(PApplet.TRIANGLES);
    parent.vertex(0, -r*2);
    parent.vertex(-r, r*2);
    parent.vertex(r, r*2);
    parent.endShape();
    parent.popMatrix();
  }
  
  void renderPacket() {
    parent.ellipseMode(PApplet.CENTER);
    parent.noStroke();
//    p.stroke(0,timer);
    parent.fill(color,100-100 * PApplet.pow(PApplet.norm(lifespan,100,0),2)); // all this for a non-linear fade
    parent.ellipse(loc.x,loc.y,r,r);
    
    // lets do a bit of a glow in the center
    // using the additive blending from openGL
    
    for(int i=2;i<r;i+=5){
      parent.ellipse(loc.x,loc.y,r-i,r-i);
    }
    
  }
  
  // Wraparound
  void borders() {
    if (loc.x < -r) loc.x = parent.width+r;
    if (loc.y < -r) loc.y = parent.height+r;
    if (loc.x > parent.width+r) loc.x = -r;
    if (loc.y > parent.height+r) loc.y = -r;
  }

  // test for our lifespan
  boolean dead() {
    if (lifespan <= 0.0) {
      return true;
    } else {
      return false;
    }
  }
  
  public void toggleDebug() {
    this.debug = !this.debug;
  }
}