/*
* Datei: PacmanAgent.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt: 2010-10-27
*
* (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.demos.cleaningPacmen;
import java.util.LinkedList;
import eas.math.geometry.Polygon2D;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.agent.GenericActuator;
import eas.simulation.agent.GenericSensor;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardAgents.PacmanAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.ParCollection;
public class StandardCleaningAgent extends PacmanAgent2D {
/**
*
*/
private static final long serialVersionUID = 8324434746386647118L;
private final double cleaningConstant = 10;
private double cleaned = 0;
/**
* @param id
* @param env
*/
public StandardCleaningAgent(int id, AbstractEnvironment2D<AbstractAgent2D<?>> env, ParCollection params) {
super(id, env, params);
this.addSensor(new GenericSensor<LinkedList<Double>, StandardCleaningEnvironment, AbstractAgent2D<?>>() {
/**
*
*/
private static final long serialVersionUID = -5388830532825444542L;
@Override
public LinkedList<Double> sense(StandardCleaningEnvironment env, AbstractAgent2D<?> agent) {
Vector2D pos = env.getAgentPosition(agent.id());
LinkedList<Double> liste = new LinkedList<Double>();
liste.add(env.getStandardDirt(pos.x, pos.y).getSchmutzWert());
liste.add(env.getStandardDirt(pos.x, pos.y - 1).getSchmutzWert());
liste.add(env.getStandardDirt(pos.x - 1, pos.y).getSchmutzWert());
liste.add(env.getStandardDirt(pos.x, pos.y + 1).getSchmutzWert());
liste.add(env.getStandardDirt(pos.x + 1, pos.y).getSchmutzWert());
return liste;
}
@Override
public String id() {
return "Dirty";
}
});
this.addSensor(new GenericSensor<Double, StandardCleaningEnvironment, AbstractAgent2D<?>>() {
/**
*
*/
private static final long serialVersionUID = -481020239596459678L;
@Override
public Double sense(StandardCleaningEnvironment env, AbstractAgent2D<?> agent) {
return StandardCleaningAgent.this.cleaned;
}
@Override
public String id() {
return "Cleaned so far";
}
});
this.addActuator(new GenericActuator<StandardCleaningEnvironment, AbstractAgent2D<?>>() {
/**
*
*/
private static final long serialVersionUID = -2348035844530849106L;
@Override
public void actuate(
StandardCleaningEnvironment env,
AbstractAgent2D<?> agent) {
Vector2D pos = env.getAgentPosition(agent.id());
StandardDirt standardDirt = env.getStandardDirt(pos.x, pos.y);
if (standardDirt.getSchmutzWert() > cleaningConstant) {
double toClean = standardDirt.getSchmutzWert() / 1.2;
standardDirt.setSchmutzWert(toClean);
StandardCleaningAgent.this.cleaned += toClean;
}
}
@Override
public String id() {
return "Clean Current Field";
}
});
this.addActuator(new GenericActuator<StandardCleaningEnvironment, AbstractAgent2D<?>>() {
/**
*
*/
private static final long serialVersionUID = 5506723630457611912L;
@Override
public void actuate(StandardCleaningEnvironment env, AbstractAgent2D<?> agent) {
Vector2D position = env.getAgentPosition(agent.id());
@SuppressWarnings("unchecked")
LinkedList<Double> dirty = (LinkedList<Double>) StandardCleaningAgent.this.sense("Dirty");
double max = dirty.get(0);
double maxIndex = 0;
for (int i = 1; i < dirty.size(); i++) {
if (dirty.get(i) > max) {
max = dirty.get(i);
maxIndex = i;
}
}
if (maxIndex == 1) {
// hoch.
env.setAgentPosition(agent.id(), new Vector2D(position.x, position.y - 1));
env.setAgentAngle(agent.id(), 180);
}
if (maxIndex == 2) {
// links.
env.setAgentPosition(agent.id(), new Vector2D(position.x - 1, position.y));
env.setAgentAngle(agent.id(), 90);
}
if (maxIndex == 3) {
// runter.
env.setAgentPosition(agent.id(), new Vector2D(position.x, position.y + 1));
env.setAgentAngle(agent.id(), 0);
}
if (maxIndex == 4) {
// rechts.
env.setAgentAngle(agent.id(), 270);
env.setAgentPosition(agent.id(), new Vector2D(position.x + 1, position.y));
}
if (maxIndex == 0) {
// Do not move.
}
}
@Override
public String id() {
return "Move to where it is dirty";
}
});
}
private Polygon2D pacman;
@Override
public Polygon2D getAgentShape() {
if (pacman == null) {
final double precision = 40;
final double radius = 1;
pacman = new Polygon2D();
for (double d = 0; d < Math.PI * 1.75; d += Math.PI * 2 / precision) {
pacman.add(new Vector2D(Math.sin(d) * radius, Math.cos(d) * radius));
}
pacman.add(new Vector2D(0, 0));
pacman.rotate(Vector2D.NULL_VECTOR, -Math.PI * 0.125);
}
return pacman;
}
@Override
public synchronized void step(Wink simTime) {
// super.step(simTime);
this.actuate("Clean Current Field");
this.actuate("Move to where it is dirty");
}
}