package alive;

/**
 * <ALIVE Project>
 * Experiment 1 Food agent
 * Food is an animated agent, just grow and splits
 * anyway, uses the Agent class to implement procedures
 */

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

public class XP_Test_Lws extends Agent {
  Actor actor = null;
// ----------------------------- Constructors ------------------------------
  public XP_Test_Lws(Environment env) {  // Contructor
    super.configureToEnvironment(env); // Update references
    // Init vectors & parameters
    param = new double[3];
    paramName = new String[3];
    set(0, "energy", 6);
    set(1, "param1", rnd(-10,10));
    set(2, "param2", env.get("temperature"));
    color = new Color3f(.0f,.3f,.0f);
    speed = generateRandomVector(); speed.scale(.01);
    position.set(rnd(env.config.xMin,env.config.xMax),
                 rnd(env.config.yMin,env.config.yMax),
                 rnd(env.config.zMin,env.config.zMax));
    super.makeAlive();
    System.out.print("L"); // "Agent Created" char code
  }

  public XP_Test_Lws(XP_Test_Lws parent) {
    super.configureToEnvironment(env);
    param = new double[3];
    paramName = new String[3];
    for (int i = 0; i < param.length; i++) {
      set(i, parent.paramName[i], parent.param[i]);
      if (probability(.1)) param[i] = param[i] * rnd(.8, 1.2);
    }
    color = new Color3f(.0f,.3f,.0f);
    position.set(parent.position.x,parent.position.y,parent.position.z);
    speed = generateRandomVector(); speed.scale(.1);
    position.add(speed);
    speed.scale(.2);
    message("l"); // "Agent Created" char code
    super.makeAlive();
  }

// ----------------------------- User methods ------------------------------
  public synchronized void reproduce() {
    set("energy",get("energy")/2);
    if (env.nAgents<env.config.POP_LIMIT) {
      XP_Test_Lws child = new XP_Test_Lws(env);
      env.addAgent(child);
    }
  }

// --------------------------- Overrided methods ----------------------------

  public void behavior() { //  ***  Main Cycle
    // evaluate common agent functions
    if (probability(.1)) speed.add(generateRandomVector());
    if (Math.abs(param[0]) > 10) reproduce();
    if (Math.abs(param[0]) < 1)  die();
  }

  public Actor createActor() {
    //this.actor = new Actor_ObjFile(path + "objects/galleon.obj");
    this.actor = new Actor_LwsFile(path + "objects/ballcone.lws");
    //actor.setTransparency(0.3f);
    return actor;
  }

  public void updateActor() {
    size = Math.abs(get("energy")/5);
    actor.set(position,direction,size);
  }
}
