initial commit
[applet-bots.git] / src / appletbots / tag / TagAgent.java
blob0e91f7608d928cff93bdaac08ba6e7c571517dcf
1 /*
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
3 */
4 package appletbots.tag;
6 import appletbots.Agent;
7 import appletbots.WorldObject;
8 import appletbots.geometry.Vector;
10 import java.awt.*;
11 import java.util.List;
13 /**
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
27 /**
28 * The "it" flag
30 private boolean it;
31 /**
32 * The number of time cycles to wait after being tagged "it"
34 private int timeToWait;
36 /**
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;
50 /**
51 * Observes the world, and follows the Tag Agent Algorithm.
53 public void observeWorld()
55 if (timeToWait > 0)
57 setAcceleration(new Vector(0, 0));
58 timeToWait--;
59 return;
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));
83 else
85 Vector toClosestOppNeighbor = world.getVectorToObject(this, closestNeighborWithOppositeItFlag);
86 toClosestOppNeighbor = toClosestOppNeighbor.setLength(maxAcceleration);
87 setAcceleration(it ? toClosestOppNeighbor : toClosestOppNeighbor.multiply(-1));
91 /**
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)
98 if (this.it == !it)
100 this.it = it;
101 if (it)
103 color = Color.pink;
104 maxSpeed *= 0.8;
105 timeToWait = 10;
107 else
109 color = Color.cyan;
110 maxSpeed *= 1.25;
111 timeToWait = 0;
117 * Returns whether or not this agent is "it"
119 * @return Whether or not this agent is "it"
121 public boolean isIt()
123 return it;
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)
138 tagAgent.setIt(it);
139 setIt(!it);