2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
4 package appletbots
.tag
;
6 import appletbots
.Agent
;
7 import appletbots
.WorldObject
;
8 import appletbots
.geometry
.Vector
;
11 import java
.util
.List
;
14 * If a tag agent is "it", he will try to touch whichever non-"it" agent is
15 * closest to him. When that contact is made, the agent he touched is "it".
16 * If a tag agent is not "it", he will not accelerate unless he sees an agent
17 * that is "it", in which case the non-"it" agent will accelerate away from
18 * the agent that is "it". Additionally, when an agent is tagged "it", its
19 * maximum speed decreases by 20% and it must wait 10 time cycles before
20 * accelerating; this is to prevent two agents getting locked together
21 * repeatedly tagging one another.
23 * @author Erik Rasmussen
25 public class TagAgent
extends Agent
32 * The number of time cycles to wait after being tagged "it"
34 private int timeToWait
;
37 * Constructs a new TagAgent with the given parameters
39 * @param size The agent's radius
40 * @param sight The distance the agent can see
41 * @param maxSpeed The maximum speed the agent can travel
42 * @param maxAcceleration The maximum acceleration for this agent
44 public TagAgent(final int size
, final int sight
, final double maxSpeed
, final double maxAcceleration
)
46 super(size
, sight
, maxSpeed
, maxAcceleration
);
47 this.color
= Color
.cyan
;
51 * Observes the world, and follows the Tag Agent Algorithm.
53 public void observeWorld()
57 setAcceleration(new Vector(0, 0));
61 final List neighbors
= world
.getNeighbors(this);
62 double distanceToClosestOppNeighbor
= Double
.MAX_VALUE
;
63 TagAgent closestNeighborWithOppositeItFlag
= null;
64 for (int i
= 0; i
< neighbors
.size(); i
++)
66 final Agent neighbor
= (Agent
) neighbors
.get(i
);
67 if (neighbor
instanceof TagAgent
)
69 final TagAgent tagNeighbor
= (TagAgent
) neighbor
;
70 if (tagNeighbor
.isIt() == !it
)
72 final double distance
= world
.getVectorToObject(this, tagNeighbor
).getLength();
73 if (distance
< distanceToClosestOppNeighbor
)
75 distanceToClosestOppNeighbor
= distance
;
76 closestNeighborWithOppositeItFlag
= tagNeighbor
;
81 if (closestNeighborWithOppositeItFlag
== null)
82 setAcceleration(new Vector(0, 0));
85 Vector toClosestOppNeighbor
= world
.getVectorToObject(this, closestNeighborWithOppositeItFlag
);
86 toClosestOppNeighbor
= toClosestOppNeighbor
.setLength(maxAcceleration
);
87 setAcceleration(it ? toClosestOppNeighbor
: toClosestOppNeighbor
.multiply(-1));
92 * Sets whether or not this agent is "it"
94 * @param it Whether or not this agent is "it"
96 public void setIt(final boolean it
)
117 * Returns whether or not this agent is "it"
119 * @return Whether or not this agent is "it"
121 public boolean isIt()
127 * Informs the object that it has been in a collision
129 * @param object The object collided with
131 public void collidedWith(final WorldObject object
)
133 if (object
instanceof TagAgent
)
135 final TagAgent tagAgent
= (TagAgent
) object
;
136 if (tagAgent
.isIt() == !it
)