/*
* File name: StupidAgent.java (package eas.users.daniel.stupid)
* Author(s): phoenix
* Java version: 6.0
* Generation date: 20.09.2011 (11:30:19)
* (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.daniel.stupid;
import java.util.Random;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.agent.GenericActuator;
import eas.simulation.spatial.sim2D.gridSimulation.standardGridObjects.GridDoubleValue;
import eas.simulation.spatial.sim2D.standardAgents.PacmanAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.ParCollection;
/**
* @author phoenix
*/
public class StupidAgent extends PacmanAgent2D {
/**
*
*/
private static final long serialVersionUID = -6065755538764582870L;
private float size;
/**
* @param id
* @param env
* @param params
*/
public StupidAgent(int id, AbstractEnvironment2D<?> env,
ParCollection params) {
super(id, env, params);
size = 1;
this.addActuator(new GenericActuator<StupidEnvironment, StupidAgent>() {
/**
*
*/
private static final long serialVersionUID = 8204658794033737417L;
@Override
public void actuate(StupidEnvironment env, StupidAgent agent) {
Vector2D pos = new Vector2D(env.getAgentPosition(agent.id()));
GridDoubleValue food = (GridDoubleValue) env.getFieldPosition(
pos).get(0);
agent.grow(food.getValue() / 255);
((GridDoubleValue) env.getFieldPosition(pos).get(0))
.setValue(0);
}
@Override
public String id() {
return "Eat'n'grow";
}
});
}
/**
* moves the Agent by +/- 1 field
* works without actuators, directly on the environment
* (quick and dirty)
*/
private void moveRandom() {
Random rnd = new Random();
int xOffset = rnd.nextInt(3) - 1;
int yOffset = rnd.nextInt(3) - 1;
Vector2D pos = this.getEnvironment().getAgentPosition(this.id());
pos.setzeKoord(pos.x + xOffset, pos.y + yOffset);
this.getEnvironment().setAgentPosition(this.id(), pos);
}
private void grow(double d) {
this.size += d;
if (size < 0.1) {
this.getEnvironment().removeAgent(this.id());
return;
}
this.getEnvironment()
.setAgentScale(this.id(), new Vector2D(size, size));
}
@Override
public void step(Wink simTime) {
super.step(simTime);
moveRandom();
this.actuate("Eat'n'grow");
grow(-size * 0.2);
}
}