Package eas.users.students.fabian.curling

Source Code of eas.users.students.fabian.curling.CurlingScheduler

/*
* File name:        ODESchedPrimitive.java (package eas.users.fabian.ODEVersuche)
* Author(s):        Fabian
* Java version:     6.0
* Generation date:  26.01.2012 (16:00:12)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.users.students.fabian.curling;

import java.util.LinkedList;
import java.util.List;

import org.ode4j.math.DVector3;

import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.standard.visualization.visualization3D.VideoPluginLWJGL;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;

/**
* @author Fabian
*
*/
public class CurlingScheduler extends AbstractDefaultMaster<CurlingEnv> {
 
  /**
     *
     */
    private static final long serialVersionUID = -155526395934700907L;
    private double shortestDistance = 10000;
  private double distance;
  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#getRequiredPlugins()
   */
  @Override
  public List<String> getRequiredPlugins() {
    return null;
  }

  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#getParameters()
   */
  @Override
  public List<SingleParameter> getParameters() {
    return super.getParameters();
  }

  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#id()
   */
  @Override
  public String id() {
    return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID
        + "-fr.curling";
  }

  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#runBeforeSim(eas.simulation.environment.
   * AbstractEnvironment, eas.startSetup.ParCollection)
   */
  @Override
  public void runBeforeSimulation(CurlingEnv env, ParCollection params) {   
    env.setMu(0.001);
   
    DVector3 position = new DVector3(0,40,5);
    BumperAgent bumper = new BumperAgent(0, env, params, position);
    env.addAgent(bumper);
   
    position = new DVector3(0,33,5);
    PuckAgent puck = new PuckAgent(1, env, params, position);
    env.addAgent(puck);
   
    float[] xyz = new float[] {0,-6,6};
    float[] hpr = new float[] {90,-30,0};   
    VideoPluginLWJGL.dsSetViewpoint(xyz, hpr);
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * eas.plugins.Plugin#runAfterSim(eas.simulation.environment.AbstractEnvironment
   * , eas.startSetup.ParCollection)
   */
  @Override
  public void runAfterSimulation(CurlingEnv env, ParCollection params) {
//    env.tearDownEnvironment();
  }

  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#runDuringSim(eas.simulation.environment.
   * AbstractEnvironment, eas.simulation.Wink, eas.startSetup.ParCollection)
   */
  @Override
  public void runDuringSimulation(CurlingEnv env, Wink currentSimTime,
      ParCollection params) {
    env.step(currentSimTime);
    if (currentSimTime.getLastTick() == 1) {
      for (int i = 0; i < 2; i++) {
        System.out.println();
      }
      System.out.println("Use the arrow keys to steer the puck... Have fun!");
      env.getAgent(0).getBody(0).addForce(new DVector3(0, -5000, 0));
      env.getAgent(0).getBody(0).addTorque(0, 0, Math.random()*50);
    }
   
    distance = 0;
    distance += env.getAgent(1).getBody(0).getPosition().get0() * env.getAgent(1).getBody(0).getPosition().get0();
    distance += env.getAgent(1).getBody(0).getPosition().get1() * env.getAgent(1).getBody(0).getPosition().get1();
    distance = Math.sqrt(distance);
    distance = ((int)(distance*100))/100.;
    if (distance < shortestDistance) {
      shortestDistance = distance;
    }   
   
    if (currentSimTime.getLastTick() % 200 == 199) {
     
      System.out.println ("Distance: " + distance + " m.");
    }
   
    if (currentSimTime.getLastTick() > 1000 && env.getAgent(1).getBody(0).getLinearVel().length() < 0.01) {
      System.out.println("Game over: " + distance + ".\nThe minimum distance to the center was " + shortestDistance + " meters!");
      if (distance == shortestDistance) {
        System.out.println("Well done!");
      }
      else {
        System.out.println("Hrmpf! You should have stopped earlier...");
      }
      currentSimTime.getSimTime().timeTerminate();
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see eas.plugins.Plugin#handleEvent(eas.simulation.event.EASEvent,
   * eas.simulation.environment.AbstractEnvironment, eas.simulation.Wink,
   * eas.startSetup.ParCollection)
   */
  @Override
  public void handleEvent(EASEvent e, CurlingEnv env, Wink lastSimTime,
      ParCollection params) {
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * eas.simulation.masterScheduler.MasterScheduler#generateEnvironments(eas
   * .startSetup.ParCollection)
   */
  @Override
  public CurlingEnv[] generateRunnables(ParCollection params) {
    CurlingEnv env = new CurlingEnv(0, params);
    return new CurlingEnv[] { env };
  }

    /* (non-Javadoc)
     * @see eas.plugins.Plugin#getSupportedPlugins()
     */
    @Override
    public List<String> getSupportedPlugins() {
        LinkedList<String> list = new LinkedList<String>();
        list.add(new VideoPluginLWJGL().id());
        return list;
    }

}
TOP

Related Classes of eas.users.students.fabian.curling.CurlingScheduler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.