Package eas.users.demos.stupidModel

Source Code of eas.users.demos.stupidModel.StupidBug

/*
* File name:        StupidBug.java (package eas.users.lukas.demos.stupidModel)
* Author(s):        aifb
* Java version:     6.0
* Generation date:  14.09.2011 (16:11:08)
*
* (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.stupidModel;

import java.awt.Color;
import java.util.Random;

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.gridSimulation.standardGridObjects.GridDoubleValue;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.startSetup.ParCollection;

/**
* @author aifb
*
*/
public class StupidBug extends AbstractAgent2D<StupidEnvironment> {

    /**
     *
     */
    private static final long serialVersionUID = 8242290346316720243L;

    private double myScale = 1;
   
    public int getMyScale() {
        return (int) this.myScale;
    }

    public StupidBug(int id, StupidEnvironment env, ParCollection params) {
        super(id, env, params);
       
        this.addSensor(new GenericSensor<Integer, StupidEnvironment, StupidBug>() {

            /**
             *
             */
            private static final long serialVersionUID = -1110760976633604405L;

            @Override
            public Integer sense(StupidEnvironment env, StupidBug agent) {
                return (int) myScale;
            }

            @Override
            public String id() {
                return "Size";
            }
        });
       
        this.addActuator(new GenericActuator<StupidEnvironment, StupidBug>() {

            /**
             *
             */
            private static final long serialVersionUID = -5142552214511930038L;

            @Override
            public void actuate(StupidEnvironment env, StupidBug agent) {
                Vector2D pos = env.getAgentPosition(agent.id());
               
                GridDoubleValue gridDoubleValue = (GridDoubleValue) env.getFieldPosition(pos.x, pos.y).get(0);
                double fac = gridDoubleValue.getValue() / 200;
                myScale = myScale + fac;
                env.setAgentScale(agent.id(), new Vector2D((int) myScale, (int) myScale));
                gridDoubleValue.setValue(0);
            }

            @Override
            public String id() {
                return "Eat'n'grow";
            }
        });

        this.addActuator(new GenericActuator<StupidEnvironment, StupidBug>() {

            /**
             *
             */
            private static final long serialVersionUID = 1087683922214029940L;

            @Override
            public void actuate(StupidEnvironment env, StupidBug agent) {
                Vector2D pos = env.getAgentPosition(agent.id());
               
                int i, j = 0;
               
                if (rand.nextBoolean()) {
                    i = 1;
                } else {
                    i = -1;
                }
                if (rand.nextBoolean()) {
                    j = 1;
                } else {
                    j = -1;
                }
               
                Vector2D bestfood = new Vector2D(pos.x + i, pos.y + j);
               
                double maxFood = 0;
               
                double value;
                value = ((GridDoubleValue) env.getFieldPosition(pos.x + 1, pos.y).get(0)).getValue();
                if (value > maxFood) {
                    maxFood = value;
                    bestfood = new Vector2D(pos.x + 1, pos.y);
                }
                value = ((GridDoubleValue) env.getFieldPosition(pos.x, pos.y + 1).get(0)).getValue();
                if (value > maxFood) {
                    maxFood = value;
                    bestfood = new Vector2D(pos.x, pos.y + 1);
                }
                value = ((GridDoubleValue) env.getFieldPosition(pos.x - 1, pos.y).get(0)).getValue();
                if (value > maxFood) {
                    maxFood = value;
                    bestfood = new Vector2D(pos.x - 1, pos.y);
                }
                value = ((GridDoubleValue) env.getFieldPosition(pos.x, pos.y - 1).get(0)).getValue();
                if (value > maxFood) {
                    maxFood = value;
                    bestfood = new Vector2D(pos.x, pos.y - 1);
                }
               
                env.setAgentPosition(agent.id(), bestfood);
            }

            @Override
            public String id() {
                return "RandomWalk";
            }
        });
    }
   
    private Random rand = new Random();
   
    private Polygon2D pacman;

    @Override
    public Polygon2D getAgentShape() {
        if (pacman == null) {
            final double precision = 10;
            final double radius = 0.5;
            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 Color getAgentColor() {
        return Color.yellow;
    }
   
    @Override
    public String toString() {
        return "scale " + myScale;
    }
   
    @Override
    public void step(Wink simTime) {
        super.step(simTime);
        this.actuate("RandomWalk");
        this.actuate("Eat'n'grow");
        if (this.myScale > 10) {
            this.getEnvironment().addAgent(new StupidBug(0, this.getEnvironment(), this.getPars()), new Vector2D(this.getEnvironment().getAgentPosition(this.id())), rand.nextDouble() * 360);
            this.myScale = 5;
        }
       
        double epsilon = 0.005;
       
        if (rand.nextDouble() * Math.min(myScale, 5) / 5 < epsilon) {
            this.getEnvironment().removeAgent(this.id());
        }
    }
}
TOP

Related Classes of eas.users.demos.stupidModel.StupidBug

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.