2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
4 package appletbots
.gatherers
;
6 import appletbots
.CarriableObject
;
7 import appletbots
.CarrierAgent
;
8 import appletbots
.CollisionException
;
9 import appletbots
.WorldObject
;
10 import appletbots
.geometry
.Vector
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
17 * A gatherer agent chooses the closest visible piece of food
18 * that is currently not being carried by anyone (and is not
19 * already in the agent's home base) and accelerates towards it.
20 * If no "uncarried" food can be found, the agent tries to find
21 * food carried by a member of the opposing team to accelerate
22 * towards. If no food is visible the agent sets his acceleration
23 * to a random vector to search for food. When it gets close
24 * enough to pick up a piece of food, it picks it up and carries
25 * it towards its home base corner for 20 time cycles, and drops
26 * it again. The gatherer must then rest for 10 time cycles
27 * before picking up another piece of food. During these 10
28 * time cycles, the "exhausted" agent does not accelerate.
30 * @author Erik Rasmussen
32 public class Gatherer
extends CarrierAgent
35 * Whether or not the agent is on the team with the home base in the
36 * bottom right-hand corner of the world
38 protected boolean bottomRightTeam
;
40 * The amount of time the gatherer has been carrying a piece of food
42 protected int timeCarried
;
44 * The the amount of time needed to rest
46 protected int timeToRest
;
48 * The vector pointing in the direction of the agent's home base
50 protected Vector towardsHome
;
53 * Constructs a new Gatherer with the given parameters
55 * @param pickupDistance The distance from an object the agent must be to
57 * @param bottomRightTeam Whether or not this agent is on the team moving
58 * food to the bottom right corner of the world
59 * @param size The agent's radius
60 * @param sight The distance the agent can see
61 * @param maxSpeed The maximum speed the agent can travel
62 * @param maxAcceleration The maximum acceleration for this agent
64 public Gatherer(final int pickupDistance
, final boolean bottomRightTeam
, final int size
, final int sight
, final double maxSpeed
, final double maxAcceleration
)
66 super(pickupDistance
, size
, sight
, maxSpeed
, maxAcceleration
);
67 this.bottomRightTeam
= bottomRightTeam
;
70 this.color
= Color
.cyan
;
71 towardsHome
= new Vector(1, 1);
72 towardsHome
= towardsHome
.setLength(maxAcceleration
);
76 this.color
= Color
.pink
;
77 towardsHome
= new Vector(-1, -1);
78 towardsHome
= towardsHome
.setLength(maxAcceleration
);
83 * Observes the world, and follows the Gatherer Algorithm.
85 public void observeWorld()
91 setAcceleration(towardsHome
);
95 else if (timeToRest
<= 0)
97 Food food
= chooseFood(false);
98 // if no uncarried food found, search for carried food
100 food
= chooseFood(true);
102 setAcceleration(Vector
.getRandom(maxAcceleration
));
103 else if (world
.getVectorToObject(this, food
).getLength() <= getPickUpDistance() &&
104 food
.getCarriedBy() == null)
107 setAcceleration(towardsHome
);
110 setAcceleration(world
.getVectorToObject(this, food
));
113 setAcceleration(new Vector(0, 0));
117 * Selects closest visible food not already in home base
119 * @param carried Whether or not the food we are looking for is being
121 * @return The closest visible food not already in home base
123 protected Food
chooseFood(final boolean carried
)
125 final List seenObjects
= world
.getSeenObjects(this);
126 final List foods
= new ArrayList();
127 for (int i
= 0; i
< seenObjects
.size(); i
++)
129 final WorldObject object
= (WorldObject
) seenObjects
.get(i
);
130 if (object
instanceof Food
)
132 final Food food
= (Food
) object
;
133 if ((carried
&& food
.getCarriedBy() != null && ((Gatherer
) food
.getCarriedBy()).isBottomRightTeam() != bottomRightTeam
) ||
134 (!carried
&& food
.getCarriedBy() == null && !((GatherersWorld
) world
).isInHome(object
, bottomRightTeam
)))
138 if (!foods
.isEmpty())
140 // choose closest piece of food
141 double minDistance
= Double
.MAX_VALUE
;
142 Food closestFood
= null;
143 for (int i
= 0; i
< foods
.size(); i
++)
145 final Food food
= (Food
) foods
.get(i
);
146 final double distance
= world
.getVectorToObject(this, food
).getLength();
147 if (distance
< minDistance
)
149 minDistance
= distance
;
160 * Returns the nearest visible gatherer
162 * @param opponent If true will return nearest visible opponent, otherwise nearest teammate
163 * @return The nearest visible gatherer
165 protected Gatherer
getNearestGatherer(final boolean opponent
)
167 final List seenObjects
= world
.getSeenObjects(this);
168 final List gatherers
= new ArrayList();
169 for (int i
= 0; i
< seenObjects
.size(); i
++)
171 final WorldObject object
= (WorldObject
) seenObjects
.get(i
);
172 if (object
instanceof Gatherer
&&
173 ((opponent
&& ((Gatherer
) object
).bottomRightTeam
!= bottomRightTeam
) ||
174 (!opponent
&& ((Gatherer
) object
).bottomRightTeam
== bottomRightTeam
)))
175 gatherers
.add(object
);
177 if (!gatherers
.isEmpty())
179 // choose nearest opponent
180 double minDistance
= Double
.MAX_VALUE
;
181 Gatherer nearestGatherer
= null;
182 for (int i
= 0; i
< gatherers
.size(); i
++)
184 final Gatherer gatherer
= (Gatherer
) gatherers
.get(i
);
185 final double distance
= world
.getVectorToObject(this, gatherer
).getLength();
186 if (distance
< minDistance
)
188 minDistance
= distance
;
189 nearestGatherer
= gatherer
;
192 return nearestGatherer
;
199 * Returns whether or not the agent is on the team with the home base in
200 * the bottom right-hand corner of the world
202 * @return Whether or not the agent is on the team with the home base in the
203 * bottom right-hand corner of the world
205 public boolean isBottomRightTeam()
207 return bottomRightTeam
;
211 * Attempts to drop any carried food
213 protected void dropFood()
219 drop(getInventory()[0]);
222 catch (CollisionException e
)
231 * @param item The object to pick up
233 protected void pickUp(final CarriableObject item
)