package alive;

/**
 * <ALIVE Project>
 * Experiment 3 Environment class
 *
 * Flocking Behavior
 *
 * By Rogerio Neves
 * LSI - POLI - USP
 */

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

public class xp3_Environment extends Environment {

  java.awt.Scrollbar p = new java.awt.Scrollbar(0);
  java.awt.Label lp = new java.awt.Label("------");
  java.awt.Scrollbar q = new java.awt.Scrollbar(0);
  java.awt.Label lq = new java.awt.Label("------");
  java.awt.Scrollbar r = new java.awt.Scrollbar(0);
  java.awt.Label lr = new java.awt.Label("------");

  public xp3_Environment(EnvConfig cfg) { // Object Constructor (class name)
    configureEnvironment(cfg);
    param = new double[6];
    paramName = new String[6];
    set(0,"temperature",20);
    set(1,"ph",4);
    set(2,"friction",.0);
    set(3,"desire",.1);
    set(4,"keenness",.001);
    set(5,"randomness",.01);
    message("\nTemplate started...");
    super.start();
  }

  public void createContent() {
    for (int i=0; i<config.POP_LIMIT/20;i++) addGull();
    rest(1000);  // Give time to agents to startup
  }

  public void createCustomUI() {
    control.ui.add(new java.awt.Label("Experiment 3 Interface"));
    control.ui.add(new java.awt.Label("Flock desire"));
    p.setMaximum(110); p.setValue(60);
    control.ui.add(p);
    control.ui.add(lp);
    control.ui.add(new java.awt.Label("Keenness"));
    q.setMaximum(110); q.setValue(40);
    control.ui.add(q);
    control.ui.add(lq);
    control.ui.add(new java.awt.Label("Randomness"));
    r.setMaximum(110); r.setValue(10);
    control.ui.add(r);
    control.ui.add(lr);
    java.awt.Button b = new java.awt.Button("+Gull");
    b.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(java.awt.event.ActionEvent e) {addGull();}});
    control.ui.add(b);
  }

  public void addGull() {
    if (nAgents<config.POP_LIMIT) addAgent(new xp3_Gull(this));
  }

  public void update() { // Put environmental computations here
    set("desire", (double) p.getValue() / 100);
    lp.setText(String.valueOf(get("desire")));
    set("keenness", (double) q.getValue() / 100);
    lq.setText(String.valueOf(get("keenness")));
    set("randomness", (double) r.getValue() / 100);
    lr.setText(String.valueOf(get("randomness")));

    for (int i=0; i<agents.size();i++)
      if (agents.get(i)!=null) getAgent(i).speed.scale(1-get("friction"));
  }

  // *** Main method: Just makes experiment executable
  public static void main(String[] args) {
    EnvConfig cfg = new EnvConfig();
    new  xp3_Environment(cfg);
  }
}
