-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactivePredator.java
150 lines (131 loc) · 4.25 KB
/
ReactivePredator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
/**
* A chasing agent that targets other boids.
* Skeleton code
*
* @version 1.0
*/
public class ReactivePredator extends Follower {
/**
* Class information
*/
/** Tag for XML element */
static final String XML_NAME = "reactive-predator";
/** Can go forward up to 6 pixels per step */
static final double PREDATOR_MAX_SPEED = 6;
/** Record that allows XML files to set reactive predator defaults */
static FixedAgentAttributes defaultFixedAgentAttributes =
new Agent.FixedAgentAttributes(
FOLLOWER_SIZE,
PREDATOR_MAX_SPEED,
FOLLOWER_MIN_SPEED,
FOLLOWER_MAX_ACCEL,
FOLLOWER_MAX_DECEL,
HALF_CIRCLE,
0, HALF_CIRCLE, Color.RED,
false, false);
/** Record that allows XML files to set reactive predator defaults */
static DynamicAgentAttributes defaultDynamicAgentAttributes =
new Agent.DynamicAgentAttributes(250, 250, 0, 0);
/**
* Constructor - initialize general agent fields to describe
* agent that will follow other peaceful agents and eat them.
*
* @param w world to which agent belongs
* @param id number to identify agent in its world
* @param x horizontal coordinate where agent appears
* @param y vertical coordinate where agent appears
* @param heading initial orientation of agent
*/
public ReactivePredator(World w, int id, Attributes atts, Locator loc)
throws SAXException {
super(w, id, atts, loc);
form.set(atts, defaultFixedAgentAttributes, loc);
status.set(atts, defaultDynamicAgentAttributes, loc);
}
/**
* Output an XML element describing the current state of
* this reactive predator.
*
* @param out an open file to write to, wrapped in BufferedWriter
* convenience class
*/
public void log(BufferedWriter out)
throws IOException {
out.write(" <" + XML_NAME + " " + ID_PARAM + OPEN + Integer.toString(id) + CLOSE +
"\n ");
form.log(out);
out.write(" ");
status.log(out);
out.write(" />\n");
}
/**
* Predators attack their prey!
*
* @param neighbor A category of agent nearby
* @return Agent's disposition for what to do with another agent
*/
@Override
public InteractiveBehavior behaviorOnApproach(Percept.ObjectCategory neighbor) {
if (neighbor == Percept.ObjectCategory.BOID)
return InteractiveBehavior.ATTACK;
else
return InteractiveBehavior.COEXIST;
}
/**
* Predators look scary! While alive,
* they look like predators.
*
* @return appearance PREDATOR as Percept.ObjectCategory
* (or CORPSE if dead)
*/
@Override
public Percept.ObjectCategory looksLike() {
if (isAlive)
return Percept.ObjectCategory.PREDATOR;
else
return Percept.ObjectCategory.CORPSE;
}
/********************************************************************
*
* Code block for solution to behavior generation.
*
*/
/**
* Specialized drawing method in case you want debugging help
*/
@Override
public void draw(Graphics g) {
// TBC: Debug visualization here
super.draw(g);
}
/**
* Specialized target method: helps define chasing behavior
*/
@Override
public boolean isTarget(Percept p) {
return p.getObjectCategory() == Percept.ObjectCategory.BOID;
}
/**
* Specialized deliberate method: helps define chasing behavior.
* Default version copied directly from superclass Follower.
*
* @param ps A description of everything the agent can see
*/
@Override
public void deliberate(List<Percept> ps) {
Percept closestSeen = bestTarget(ps);
todo = new LinkedList<Intention>();
if (closestSeen != null) {
steerTo(closestSeen);
}
}
}