Class Environment

  • All Implemented Interfaces:
    IEnvironment

    public class Environment
    extends java.lang.Object
    implements IEnvironment
    The environment is the container all objects and creatures.
    • Field Detail

      • DEFAULT_LEASE_TICKS

        public static final int DEFAULT_LEASE_TICKS
        The default number of lease ticks.
        See Also:
        Constant Field Values
      • instance

        protected static Environment instance
        The singleton instance.
      • creatures

        protected java.util.Map creatures
        The creatures.
      • obstacles

        protected java.util.Set obstacles
        The obstacles.
      • food

        protected java.util.Set food
        The prey food.
      • world

        public MultiCollection world
        All world objects accessible per location.
      • sizex

        protected int sizex
        The horizontal size.
      • sizey

        protected int sizey
        The vertictal size.
      • monitor

        protected java.lang.Object monitor
        The monitor.
      • tasklist

        protected java.util.List tasklist
        The list for move and eat requests.
      • rand

        protected java.util.Random rand
        The radnom number generator.
      • highscore

        protected java.util.List highscore
        The highscore location.
      • savetime

        protected long savetime
        The last time the highscore was saved.
      • saveinterval

        protected long saveinterval
        The interval between saves of highscore (-1 for autosave off).
      • foodrate

        protected int foodrate
        The foodrate determines how often new food pops up.
      • age

        protected int age
        The world age.
    • Constructor Detail

      • Environment

        public Environment()
        Create a new environment.
    • Method Detail

      • getInstance

        public static Environment getInstance()
        Get the singleton.
        Returns:
        The environment.
      • moveUp

        public boolean moveUp​(Creature me)
        Move one field upwards. The method will block until the current simulation step has finished.
        Specified by:
        moveUp in interface IEnvironment
        Returns:
        True, when the operation succeeded.
      • moveDown

        public boolean moveDown​(Creature me)
        Move one field downwards. The method will block until the current simulation step has finished.
        Specified by:
        moveDown in interface IEnvironment
        Returns:
        True, when the operation succeeded.
      • moveLeft

        public boolean moveLeft​(Creature me)
        Move one field to the left. The method will block until the current simulation step has finished.
        Specified by:
        moveLeft in interface IEnvironment
        Returns:
        True, when the operation succeeded.
      • moveRight

        public boolean moveRight​(Creature me)
        Move one field to the right. The method will block until the current simulation step has finished.
        Specified by:
        moveRight in interface IEnvironment
        Returns:
        True, when the operation succeeded.
      • eat

        public boolean eat​(Creature me,
                           WorldObject food)
        Eat some object. The object has to be at the same location. This method does not block, and can be called multiple times during each simulation step.
        Specified by:
        eat in interface IEnvironment
        Parameters:
        food - The object.
        Returns:
        True, when the operation succeeded.
      • addMoveTask

        public TaskInfo addMoveTask​(Creature me,
                                    java.lang.String dir)
        Add a move or eat action to the queue.
      • clearTaskList

        protected void clearTaskList()
        Clear the TaskList HACK! Should be done in executeStep method, but that leads to problems with other Agents. They compute their next step with the old vision. :-( This is a race condition too! Tasks can be lost.
      • internalGetVision

        public Vision internalGetVision​(Creature me)
        Get the current vision (without updating the creatures leaseticks).
        Parameters:
        me - The creature.
      • getVision

        public Vision getVision​(Creature me)
        Get the current vision. This method does not block, and can be called multiple times during each simulation step.
        Specified by:
        getVision in interface IEnvironment
        Parameters:
        me - The creature.
      • getWidth

        public int getWidth()
        Get the width of the world.
      • getHeight

        public int getHeight()
        Get the height of the world.
      • addFood

        public void addFood​(Food nfood)
        Add a new prey food to the world.
        Parameters:
        nfood - The new food.
      • removeFood

        public boolean removeFood​(Food nfood)
        remove a prey food to the world.
        Parameters:
        nfood - Thefood.
      • addObstacle

        public void addObstacle​(Obstacle obstacle)
        Add a new obstacle to the world.
        Parameters:
        obstacle - The new obstacle.
      • removeObstacle

        public boolean removeObstacle​(Obstacle obstacle)
        Remove a obstacle to the world.
        Parameters:
        obstacle - The obstacle.
      • addCreature

        public Creature addCreature​(Creature creature)
        Add a new creature to the world.
        Parameters:
        creature - The creature.
      • removeCreature

        public boolean removeCreature​(Creature creature)
        Remove a creature to the world.
        Parameters:
        creature - The creature.
      • executeStep

        public void executeStep()
        Execute a step.
      • getWorldAge

        public int getWorldAge()
        Get the world age.
        Returns:
        The age of the world.
      • getFoodrate

        public int getFoodrate()
        Get the foodrate.
        Returns:
        The foodrate.
      • setFoodrate

        public void setFoodrate​(int foodrate)
        Set the foodrate.
        Parameters:
        foodrate - The foodrate.
      • move

        public boolean move​(Creature me,
                            java.lang.String dir)
        Perform a move.
        Parameters:
        me - The creature.
        dir - The direction.
      • getCreatures

        public Creature[] getCreatures()
        Get the creatures.
        Returns:
        The creatures.
      • getObstacles

        public Obstacle[] getObstacles()
        Get the obstacles.
        Returns:
        The obstacles.
      • getFood

        public Food[] getFood()
        Get the obstacles.
        Returns:
        The obstacles.
      • getAllObjects

        public WorldObject[] getAllObjects()
        Get all objects in the world (obstacles, food, and creature).
      • createLocation

        protected Location createLocation​(Location loc,
                                          java.lang.String dir)
        Create a location.
        Parameters:
        loc - The location.
        dir - The direction.
        Returns:
        The new location.
      • getEmptyLocation

        protected Location getEmptyLocation()
        Get an empty location.
        Returns:
        The location.
      • getTaskSize

        public int getTaskSize()
        Return the size of the task list.
        Returns:
        The task size.
      • getCreature

        protected Creature getCreature​(Creature creature)
        Get the internal representation of a creature. If the creature is unknown it gets added to the environment.
        Parameters:
        creature - The creature.
        Returns:
        The creature as known in the environment.
      • getNearObjects

        protected WorldObject[] getNearObjects​(Location loc,
                                               int range)
        Get objects near a position.
        Parameters:
        loc - The location.
        range - The range.
      • getHighscore

        public Creature[] getHighscore()
        Get the current highscore.
        Returns:
        The 10 best creatures.
      • saveHighscore

        public void saveHighscore()
        Save the highscore to a file.
      • setSaveInterval

        public void setSaveInterval​(long saveinterval)
        Set the highscore save interval (-1 for autosave off).
      • getSaveInterval

        public long getSaveInterval()
        Get the highscore save interval (-1 for autosave off).
      • addPropertyChangeListener

        public void addPropertyChangeListener​(PropertyChangeListener listener)
        Add a PropertyChangeListener to the listener list. The listener is registered for all properties.
        Parameters:
        listener - The PropertyChangeListener to be added.
      • removePropertyChangeListener

        public void removePropertyChangeListener​(PropertyChangeListener listener)
        Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.
        Parameters:
        listener - The PropertyChangeListener to be removed.