Class Environment
- java.lang.Object
-
- jadex.bdi.examples.hunterprey_classic.environment.Environment
-
- All Implemented Interfaces:
IEnvironment
public class Environment extends java.lang.Object implements IEnvironment
The environment is the container all objects and creatures.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
age
The world age.protected java.util.Map
creatures
The creatures.static int
DEFAULT_LEASE_TICKS
The default number of lease ticks.protected java.util.Set
food
The prey food.protected int
foodrate
The foodrate determines how often new food pops up.protected java.util.List
highscore
The highscore location.protected static Environment
instance
The singleton instance.protected java.lang.Object
monitor
The monitor.protected java.util.Set
obstacles
The obstacles.protected SimplePropertyChangeSupport
pcs
The helper object for bean events.protected java.util.Random
rand
The radnom number generator.protected long
saveinterval
The interval between saves of highscore (-1 for autosave off).protected long
savetime
The last time the highscore was saved.protected int
sizex
The horizontal size.protected int
sizey
The vertictal size.protected java.util.List
tasklist
The list for move and eat requests.MultiCollection
world
All world objects accessible per location.
-
Constructor Summary
Constructors Constructor Description Environment()
Create a new environment.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Creature
addCreature(Creature creature)
Add a new creature to the world.TaskInfo
addEatTask(Creature me, WorldObject obj)
Add a move or eat action to the queue.void
addFood(Food nfood)
Add a new prey food to the world.TaskInfo
addMoveTask(Creature me, java.lang.String dir)
Add a move or eat action to the queue.void
addObstacle(Obstacle obstacle)
Add a new obstacle to the world.void
addPropertyChangeListener(PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list.protected void
clearTaskList()
Clear the TaskList HACK! Should be done in executeStep method, but that leads to problems with other Agents.protected Location
createLocation(Location loc, java.lang.String dir)
Create a location.boolean
eat(Creature me, WorldObject food)
Eat some object.void
executeStep()
Execute a step.WorldObject[]
getAllObjects()
Get all objects in the world (obstacles, food, and creature).protected Creature
getCreature(Creature creature)
Get the internal representation of a creature.Creature[]
getCreatures()
Get the creatures.protected Location
getEmptyLocation()
Get an empty location.Food[]
getFood()
Get the obstacles.int
getFoodrate()
Get the foodrate.int
getHeight()
Get the height of the world.Creature[]
getHighscore()
Get the current highscore.static Environment
getInstance()
Get the singleton.protected WorldObject[]
getNearObjects(Location loc, int range)
Get objects near a position.Obstacle[]
getObstacles()
Get the obstacles.long
getSaveInterval()
Get the highscore save interval (-1 for autosave off).int
getTaskSize()
Return the size of the task list.Vision
getVision(Creature me)
Get the current vision.int
getWidth()
Get the width of the world.int
getWorldAge()
Get the world age.Vision
internalGetVision(Creature me)
Get the current vision (without updating the creatures leaseticks).boolean
move(Creature me, java.lang.String dir)
Perform a move.boolean
moveDown(Creature me)
Move one field downwards.boolean
moveLeft(Creature me)
Move one field to the left.boolean
moveRight(Creature me)
Move one field to the right.boolean
moveUp(Creature me)
Move one field upwards.boolean
removeCreature(Creature creature)
Remove a creature to the world.boolean
removeFood(Food nfood)
remove a prey food to the world.boolean
removeObstacle(Obstacle obstacle)
Remove a obstacle to the world.void
removePropertyChangeListener(PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list.void
saveHighscore()
Save the highscore to a file.void
setFoodrate(int foodrate)
Set the foodrate.void
setSaveInterval(long saveinterval)
Set the highscore save interval (-1 for autosave off).
-
-
-
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.
-
pcs
protected SimplePropertyChangeSupport pcs
The helper object for bean events.
-
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.
-
-
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 interfaceIEnvironment
- 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 interfaceIEnvironment
- 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 interfaceIEnvironment
- 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 interfaceIEnvironment
- 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 interfaceIEnvironment
- Parameters:
food
- The object.- Returns:
- True, when the operation succeeded.
-
addEatTask
public TaskInfo addEatTask(Creature me, WorldObject obj)
Add a move or eat action to the queue.
-
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 interfaceIEnvironment
- 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.
-
-