package alive;

/**
 * <ALIVE Project>
 * Experiment 2 Fish agent
 * Fish swims and eat, controlled by a Neural Net
 * that learns from experience end observation
 */

import javax.vecmath.*;
import javax.media.j3d.*;

public class xp3_Gull extends Agent {
  double r = 0;
  Vector3d dis = new Vector3d(),
        target = new Vector3d();

// ----------------------------- Constructors ------------------------------
  public xp3_Gull(Environment env) {  // Environment Contructor
    super.configureToEnvironment(env); // Update references
    color.set(.0f,.0f,.0f);
    speed = generateRandomVector();
    System.out.print("v"); // "Agent Created" char code
    super.makeAlive();
  }

// --------------------------- Overrided methods ----------------------------
  public void behavior() { //  ***  Main Cycle, evaluate common agent functions
    target.set(0,0,0);
    if (probability(env.get("desire")))  {
      for (int i = 0; i < env.agents.size(); i++) {
        Agent ag = env.getAgent(i);
        if (ag != null)
          if (ag.id != this.id) {
            dis.sub(ag.position, this.position);
            target.add(dis);
          }
      }
      target.normalize();
      target.scale(env.get("keenness")/200);
      if (target.length()>0) speed.add(target);
    }
    if (probability(env.get("randomness"))) {
      r = env.get("randomness")/10;
      speed.set(speed.x+rnd(-r,r), speed.y+rnd(-r,r),speed.y+rnd(-r,r));
      speed.normalize();
    }
    direction.set(speed);
    direction.normalize();
  }
  // Actor Stuff
  public Actor createActor() { return new Actor_Gull(); }
  public void updateActor() { actor.set(position,direction,1); }
}
