package bees;
import swarm.sim.*;
import insects.Beetle;
import insects.DeadInsect;
import java.awt.Polygon;
import java.awt.Color;
import java.util.Vector;
import cannibals.Cannibal;
public class Bee extends Agent {
private static final double startSize=50;
private static final double viewRange=250;
private static final double viewField=Math.PI/3;
private static final double attackRange=80;
private static final double attackPower=50;
private static final double speed=3.25;
private final static double velocity=3;
private final static double vision=40;
private final static double fov=Math.PI*1.5;
//The current behavior of the cannibal
private BehaviorState mode;
private Agent neighbor;
private Resource food;
public Bee(double x, double y){
super(x, y, startSize, Math.random()*Math.PI*2,new java.awt.Color(0, 0, 255));
mode=BehaviorState.SEARCH;
}
public Bee(double x, double y, int healthValue){
super(x,y, startSize, Math.random()*Math.PI*2, null);
mode=BehaviorState.SEARCH;
health = healthValue;
}
public void tick(){
Vector<SimElement> neighbors=look(vision, fov);
//We determine behavior based on the current behavior setting
if(mode==BehaviorState.SEARCH){
//First the agent looks to see if there are any other bees
java.util.Vector<SimElement> vision=look(viewRange, viewField);
//We search through the view range for other Cannibals
for(SimElement s: vision)
if(s instanceof Bee){
neighbor =(Bee)s;
mode=BehaviorState.SWARM;
return;
}else if(s instanceof Cannibal){
neighbor=(Cannibal)s;
mode=BehaviorState.ENGAGE;
return;
}else if(s instanceof Beetle){
neighbor=(Beetle)s;
mode=BehaviorState.ENGAGE;
return;
}else if(s instanceof Resource && health < maxHealth){
food=(Resource)s;
mode=BehaviorState.CONSUME;
return;
}
//If nothing was found, the agent keeps wandering around
angle += Math.random()*Math.PI/8-Math.PI/16;
//If we're drawing too close to the border, point directly back
//at the center
if(xPos < radius || yPos < radius ||
sim.getWidth()-xPos < radius || sim.getHeight()-yPos < radius)
angle=Math.atan2(sim.getHeight()/2-yPos, sim.getWidth()/2-xPos);
move();
}else if(mode==BehaviorState.SWARM){
//If we've lost sight of the prey, then forget about it
java.util.Vector<SimElement> vision=look(viewRange, viewField);
if(!vision.contains(neighbor)){
mode=BehaviorState.SEARCH;
return;
}
if(Simulation.distance(this, neighbor) < 80 && Simulation.distance(this,neighbor)>30)
{
//if(neighbor.health == this.health)
if(health == this.health)
//angle= neighbor.angle;
angle=Math.atan2(neighbor.getY()-yPos, neighbor.getX()-xPos);
}
xPos += speed * Math.cos(angle);
yPos += speed * Math.sin(angle);
if(Simulation.distance(this, neighbor) <= 20 && health==this.health)
{
//angle = neighbor.angle-Math.PI;
angle=1;
move();
}
//System.out.println("angle= "+angle);
//If the prey is within attacking range, then attack
if(Simulation.distance(this, neighbor) < attackRange){
double power=attackPower * Math.random() * health/maxHealth;
//neighbor.attack(power);
//Check to see if we've killed our prey
if(!neighbor.alive()){
neighbor=null;
mode=BehaviorState.SEARCH;
}
}else{
//Otherwise, move towards the prey
// angle=Math.atan2(neighbor.getY()-yPos, neighbor.getX()-xPos);
// move();
}
}else if(mode==BehaviorState.CONSUME){
//If the food is out of range, move towards it
if(Simulation.distance(this, food) > attackRange){
angle=Math.atan2(food.getY()-yPos, food.getX()-xPos);
move();
return;
}
//If the food is decayed or completely consumed, abandon it
//Also if this Cannibal is full already
if(!food.alive() || health >= maxHealth){
mode=BehaviorState.SEARCH;
return;
}
//Otherwise, eat
health += food.eat()*10;
if(health >= maxHealth)
{
health = maxHealth/2;
sim.add(new Bee(xPos,yPos));
}
}else if(mode==BehaviorState.ENGAGE){
//If we've lost sight of the prey, then forget about it
java.util.Vector<SimElement> vision=look(viewRange, viewField);
if(!vision.contains(neighbor)){
mode=BehaviorState.SEARCH;
return;
}
//If the prey is within attacking range, then attack
if(Simulation.distance(this, neighbor) < attackRange){
double power=attackPower * Math.random() * health/maxHealth;
neighbor.attack(power/10);
//Check to see if we've killed our prey
if(!neighbor.alive()){
neighbor=null;
mode=BehaviorState.SEARCH;
}
}else{
//Otherwise, move towards the prey
angle=Math.atan2(neighbor.getY()-yPos, neighbor.getX()-xPos);
move();
}
}
//If we're drawing too close to the border, point directly back
//at the center
if(xPos < radius || yPos < radius ||
sim.getWidth()-xPos < radius || sim.getHeight()-yPos < radius)
{
angle=Math.atan2(sim.getHeight()/2-yPos, sim.getWidth()/2-xPos);
move();
}
}
public void move()
{
xPos += speed*Math.cos(angle);
yPos += speed*Math.sin(angle);
}
public boolean attack(double power){
boolean result=super.attack(power);
//If the attack killed the agent, replace it with a resource
if(result)
sim.add(new DeadInsect(xPos, yPos, startSize, 5));
//If the Cannibal is currently consuming, it will break off of its
//food source to escape
if(mode==BehaviorState.CONSUME)
mode=BehaviorState.SEARCH;
return result;
}
public Color getColor(){
float blue = (float)(health/maxHealth);
if(blue < 0)
blue=0;
if(blue > 1)
blue=1;
float red=1-blue;
return new Color(red, 0, blue);
}
public Polygon getShape(){
//Code auto-generated by Polygon Designer
Polygon retVal = new Polygon();
int xPoint;
int yPoint;
xPoint = (int)(1.0 * radius * Math.cos(Math.PI * 0.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(1.0 * radius * Math.sin(Math.PI * 0.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 1.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 1.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 2.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 2.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 3.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 3.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 4.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 4.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 5.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 5.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 6.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 6.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 6.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 6.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 7.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 7.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 8.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 8.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 9.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 9.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 10.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 10.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 10.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 10.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 11.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 11.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 12.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 12.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 13.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 13.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 14.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 14.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 15.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 15.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 16.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 16.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 17.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 17.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 18.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 18.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 19.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 19.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 20.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 20.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 21.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 21.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 22.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 22.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 22.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 22.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 23.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 23.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 24.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 24.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 25.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 25.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 26.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 26.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 26.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 26.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 27.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 27.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 28.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 28.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 29.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 29.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 30.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 30.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 31.0 / 16.0 + angle));
xPoint += xPos;
yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 31.0 / 16.0 + angle));
yPoint += yPos;
retVal.addPoint(xPoint, yPoint);
//End of auto-generated code
return retVal;
}
}