-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.java
More file actions
162 lines (140 loc) · 4.92 KB
/
Runner.java
File metadata and controls
162 lines (140 loc) · 4.92 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
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 basic agent that proceeds forward in an initial direction.
* Inspired by Braitenberg's vehicle 1.
*
* @author Matthew Stone
* @version 1.0
*/
public class Runner extends Agent {
/**
* Class information
*/
/** Tag for XML element */
static final String XML_NAME = "runner";
/** Size in display */
static final int RUNNER_SIZE = 15;
/** Can go forward up to 4 pixels per step */
static final double RUNNER_MAX_SPEED = 4;
/** Cannot go backward */
static final double RUNNER_MIN_SPEED = 0;
/** No constraint on acceleration */
static final double RUNNER_MAX_ACCEL = 0;
/** No constraint on braking */
static final double RUNNER_MAX_DECEL = 0;
/** Relatively unable to make sharp turns */
static final double RUNNER_MAX_TURN = 0;
/* Record that allows XML files to set runner defaults */
static FixedAgentAttributes defaultFixedAgentAttributes =
new Agent.FixedAgentAttributes(
RUNNER_SIZE,
RUNNER_MAX_SPEED,
RUNNER_MIN_SPEED,
RUNNER_MAX_ACCEL,
RUNNER_MAX_DECEL,
RUNNER_MAX_TURN,
0,
HALF_CIRCLE,
Color.BLUE,
false, false);
/* Record that allows XML files to set runner defaults */
static DynamicAgentAttributes defaultDynamicAgentAttributes =
new Agent.DynamicAgentAttributes(250, 250, 0, RUNNER_MAX_SPEED);
/**
* Instance members
*/
/**
* Constructor: initialize general agent fields to describe
* agent that will follow a light source.
*
* @param w world to which agent belongs
* @param id number to identify agent in its world
* @param atts SAX attributes corresponding to XML agent spec
* @param loc file information for error messages
* @throws SAXException if data is formatted incorrectly
*/
public Runner(World w, int id, Attributes atts, Locator loc)
throws SAXException {
myWorld = w;
this.id = id;
form = new FixedAgentAttributes(atts, defaultFixedAgentAttributes, loc);
status = new DynamicAgentAttributes(atts, defaultDynamicAgentAttributes, loc);
}
/**
* Output an XML element describing the current state of
* this runner.
*
* @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");
}
/*
* Draw a runner as a triangle pointing in the direction
* of the agent's heading.
* @param g object to control drawing mechanism
* @see Agent#draw(java.awt.Graphics)
*/
@Override
public void draw(Graphics g) {
int[] xpoints = new int[4];
int[] ypoints = new int[4];
double baseAngle = status.heading + Math.PI / 2;
double s = form.size;
int baseOffsetX = (int) Math.round(2 * s * Math.cos(baseAngle) / 3);
int baseOffsetY = (int) Math.round(2 * s * Math.sin(baseAngle) / 3);
int x0 = ((int) Math.round(status.locX - baseOffsetX / 2
- s * Math.cos(status.heading) / 3));
int y0 = ((int) Math.round(status.locY - baseOffsetY / 2
- s * Math.sin(status.heading) / 3));
xpoints[0] = x0;
xpoints[1] = x0 + baseOffsetX;
xpoints[2] = x0 + baseOffsetX / 2 + (int) Math.round(s * Math.cos(status.heading));
xpoints[3] = x0 + (int) Math.round(s * Math.cos(status.heading));
ypoints[0] = y0;
ypoints[1] = y0 + baseOffsetY;
ypoints[2] = y0 + baseOffsetY / 2 + (int) Math.round(s * Math.sin(status.heading));
ypoints[3] = y0 + (int) Math.round(s * Math.sin(status.heading));
g.setColor(form.color);
myWorld.fillPolygon(xpoints, ypoints, 3, g);
}
/**
* Do nothing
*
* @param ps A description of everything the agent can see
*/
@Override
public void deliberate(List<Percept> ps) {
todo = new LinkedList<Intention>();
}
/**
* A runner is one of many agents that
* appears like an active agent with peaceful
* behavior
*
* @return the generic appearance BOID while
* creature is alive, otherwise CORPSE.
*/
@Override
public Percept.ObjectCategory looksLike() {
if (isAlive)
return Percept.ObjectCategory.BOID;
else
return Percept.ObjectCategory.CORPSE;
}
}