package alive;

/**
 * <ALIVE Project>
 * Experiment 2 Environment class
 *
 * Fish Schooling
 *
 * By Rogerio Neves
 * LSI - POLI - USP
 */

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

public class xp2_Environment extends Environment {

  java.awt.Scrollbar p = new java.awt.Scrollbar(0);
  java.awt.Label lp = new java.awt.Label("-------");

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

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

  public void createCustomUI() {
    control.ui.add(new java.awt.Label("Experiment 2 Interface"));
    control.ui.add(new java.awt.Label("Food Dropping Probability"));
    p.setMaximum(1010); p.setValue(10);
    control.ui.add(p);
    control.ui.add(lp);
    java.awt.Button b = new java.awt.Button("drop now");
    b.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(java.awt.event.ActionEvent e) {dropFood();}});
    control.ui.add(b);
  }

  public void dropFood() {
    if (nAgents<config.POP_LIMIT) addAgent(new xp2_Food(this));
    message(".");
  }

  public void update() { // Put environmental computations here
    set("foodprobability", (double) p.getValue() / 1000);
    lp.setText(String.valueOf(get("foodprobability")));
    for (int i=0; i<agents.size();i++)
      if (agents.get(i)!=null) getAgent(i).speed.scale(1-get("friction"));
    if (probability(get("foodprobability"))) dropFood();
  }

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