Package cannibals

Source Code of cannibals.Cannibal

package cannibals;

import swarm.sim.*;

import insects.AntHill;
import insects.DeadInsect;

import java.awt.Polygon;
import java.awt.Color;


public class Cannibal 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=20;
  private static final double speed=3;
 
  //The current behavior of the cannibal
  private BehaviorState mode;
  private Agent prey;
  private Resource food;
 
  public Cannibal(double x, double y){
    super(x, y, startSize, Math.random()*Math.PI*2,new java.awt.Color(0, 0, 255));
    mode=BehaviorState.SEARCH;
  }
 
  public void tick(){
   
    //We determine behavior based on the current behavior setting
    if(mode==BehaviorState.SEARCH){
      //First the agent looks to see if there are any other cannibals
      java.util.Vector<SimElement> vision=look(viewRange, viewField);
     
      //We search through the view range for other Cannibals
      for(SimElement s: vision)
        if(s instanceof Agent && !(s instanceof AntHill)){
          prey=(Agent)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);
     
      xPos += speed*Math.cos(angle);
      yPos += speed*Math.sin(angle);
     
    }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(prey)){
        mode=BehaviorState.SEARCH;
        return;
      }
     
      //If the prey is within attacking range, then attack
      if(Simulation.distance(this, prey) < attackRange){
        double power=attackPower * Math.random() * health/maxHealth;
        prey.attack(power);
       
        //Check to see if we've killed our prey
        if(!prey.alive()){
          prey=null;
          mode=BehaviorState.SEARCH;
        }
      }else{
        //Otherwise, move towards the prey
        angle=Math.atan2(prey.getY()-yPos, prey.getX()-xPos);
        xPos += speed * Math.cos(angle);
        yPos += speed * Math.sin(angle);
      }
     
    }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);
        xPos += speed*Math.cos(angle);
        yPos += speed*Math.sin(angle);
        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 Cannibal(xPos,yPos));
      }
    }
   
  }
 
  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 * 2.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(1.0 * radius * Math.sin(Math.PI * 2.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 1.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 1.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.8 * radius * Math.cos(Math.PI * 31.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.8 * radius * Math.sin(Math.PI * 31.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(1.0 * radius * Math.cos(Math.PI * 30.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(1.0 * radius * Math.sin(Math.PI * 30.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.7 * radius * Math.cos(Math.PI * 31.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.7 * radius * Math.sin(Math.PI * 31.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.5 * radius * Math.cos(Math.PI * 31.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.5 * radius * Math.sin(Math.PI * 31.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.6 * radius * Math.cos(Math.PI * 28.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 28.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 27.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * 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 * 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 * 23.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 23.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 23.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 23.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 22.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 22.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.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.6 * radius * Math.cos(Math.PI * 19.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 19.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 18.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * 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 * 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 * 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.6 * radius * Math.cos(Math.PI * 14.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 14.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 13.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * 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 * 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 * 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.6 * radius * Math.cos(Math.PI * 10.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 10.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 9.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 9.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.4 * radius * Math.cos(Math.PI * 9.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.4 * radius * Math.sin(Math.PI * 9.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.6 * radius * Math.cos(Math.PI * 5.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * radius * Math.sin(Math.PI * 5.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.6 * radius * Math.cos(Math.PI * 4.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.6 * 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 * 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.5 * radius * Math.cos(Math.PI * 1.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.5 * radius * Math.sin(Math.PI * 1.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    xPoint = (int)(0.7 * radius * Math.cos(Math.PI * 1.0 / 16.0 + angle));
    xPoint += xPos;

    yPoint = (int)(0.7 * radius * Math.sin(Math.PI * 1.0 / 16.0 + angle));
    yPoint += yPos;

    retVal.addPoint(xPoint, yPoint);

    //End of auto-generated code

   
    return retVal;
  }
 
}
TOP

Related Classes of cannibals.Cannibal

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.