-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalTestRendererListener.java
More file actions
353 lines (281 loc) · 8.85 KB
/
LocalTestRendererListener.java
File metadata and controls
353 lines (281 loc) · 8.85 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.net.*;
import model.*;
import static java.lang.StrictMath.*;
import static java.lang.System.out;
public final class LocalTestRendererListener {
private Graphics graphics;
private World world;
private Game game;
private int canvasWidth;
private int canvasHeight;
private double left;
private double top;
private double width;
private double height;
private Socket clientSocket;
PrintWriter outToServer;
BufferedReader inFromServer;
public void initClient()
{
if (clientSocket != null)
return;
try
{
clientSocket = new Socket("127.0.0.1", 1723);
outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
catch (Exception e)
{
try
{
Thread.sleep(100);
}
catch (Exception ex)
{
}
}
}
public void executeMyDebugCommands()
{
if (clientSocket == null)
return;
String line;
outToServer.println("frame " + world.getTick());
outToServer.flush();
line = readLine();
int num = Integer.parseInt(line);
//out.println("Start frame with " + num + " commands.");
for (int i = 0; i < num; i++)
{
line = readLine();
if (line == ".") {
out.println("Line is dot!");
break;
}
processMyDebugLine(line);
}
//out.println("End frame!");
}
public String readLine()
{
String line;
try
{
line = inFromServer.readLine();
}
catch (Exception e)
{
line = ".";
out.println("Exception!");
}
return line;
}
public void beforeDrawScene(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height)
{
initClient();
updateFields(graphics, world, game, canvasWidth, canvasHeight, left, top, width, height);
//out.println("Before... " + world.getTick());
double trackTileSize = game.getTrackTileSize();
long myId = -1;
int wpId = 1;
double nOffset = 60.0D;
for (Player player : world.getPlayers()) {
if (player.getName().equals("MyStrategy")) {
myId = player.getId();
}
}
for (int[] waypoint : world.getWaypoints()) {
double x = waypoint[0] * trackTileSize + 100.0D;
double y = waypoint[1] * trackTileSize + 100.0D;
graphics.setColor(new Color(252, 255, 127));
fillRect(x, y, trackTileSize - 200.0D, trackTileSize - 200.0D);
}
for (Car car : world.getCars()) {
if (car.getPlayerId() == myId) {
double x = car.getNextWaypointX() * trackTileSize + 100.0D;
double y = car.getNextWaypointY() * trackTileSize + 100.0D;
graphics.setColor(new Color(75, 255, 63));
fillRect(x, y, trackTileSize - 200.0D, trackTileSize - 200.0D);
}
}
for (int[] waypoint : world.getWaypoints()) {
double x = waypoint[0] * trackTileSize + 320.0D;
double y = waypoint[1] * trackTileSize + 490.0D;
if (wpId >= 10) {
x = x - nOffset;
}
graphics.setColor(Color.BLACK);
drawString(wpId++ + "", x, y);
}
executeMyDebugCommands();
for (Car car : world.getCars()) {
drawCircle(car.getX(), car.getY(), hypot(car.getWidth(), car.getHeight()) / 2.0D);
}
graphics.setColor(Color.BLACK);
}
public void afterDrawScene(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height)
{
updateFields(graphics, world, game, canvasWidth, canvasHeight, left, top, width, height);
//out.println("After...");
graphics.setColor(Color.BLACK);
}
private void updateFields(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height) {
this.graphics = graphics;
this.world = world;
this.game = game;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}
private void processMyDebugLine(String line)
{
String[] p = line.split(" ");
switch (p[0])
{
case "setColor":
int r = Integer.parseInt(p[1]);
int g = Integer.parseInt(p[2]);
int b = Integer.parseInt(p[3]);
graphics.setColor(new Color(r, g, b));
//out.println("Cmd: " + line);
break;
case "drawLine":
double x1 = Double.parseDouble(p[1]);
double y1 = Double.parseDouble(p[2]);
double x2 = Double.parseDouble(p[3]);
double y2 = Double.parseDouble(p[4]);
drawLine(x1, y1, x2, y2);
//out.println("Cmd: " + line);
break;
case "fillCircle":
double centerX = Double.parseDouble(p[1]);
double centerY = Double.parseDouble(p[2]);
double radius = Double.parseDouble(p[3]);
fillCircle(centerX, centerY, radius);
//out.println("Cmd: " + line);
break;
}
}
private void drawString(String text, double x1, double y1) {
Point2I stringBegin = toCanvasPosition(x1, y1);
graphics.setFont(new Font("Serif", Font.PLAIN, 42));
graphics.drawString(text, stringBegin.getX(), stringBegin.getY());
}
private void drawLine(double x1, double y1, double x2, double y2) {
Point2I lineBegin = toCanvasPosition(x1, y1);
Point2I lineEnd = toCanvasPosition(x2, y2);
graphics.drawLine(lineBegin.getX(), lineBegin.getY(), lineEnd.getX(), lineEnd.getY());
}
private void fillCircle(double centerX, double centerY, double radius) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.fillOval(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawCircle(double centerX, double centerY, double radius) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.drawOval(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void fillArc(double centerX, double centerY, double radius, int startAngle, int arcAngle) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.fillArc(topLeft.getX(), topLeft.getY(), size.getX(), size.getY(), startAngle, arcAngle);
}
private void drawArc(double centerX, double centerY, double radius, int startAngle, int arcAngle) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.drawArc(topLeft.getX(), topLeft.getY(), size.getX(), size.getY(), startAngle, arcAngle);
}
private void fillRect(double left, double top, double width, double height) {
Point2I topLeft = toCanvasPosition(left, top);
Point2I size = toCanvasOffset(width, height);
graphics.fillRect(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawRect(double left, double top, double width, double height) {
Point2I topLeft = toCanvasPosition(left, top);
Point2I size = toCanvasOffset(width, height);
graphics.drawRect(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawPolygon(Point2D... points) {
int pointCount = points.length;
for (int pointIndex = 1; pointIndex < pointCount; ++pointIndex) {
Point2D pointA = points[pointIndex];
Point2D pointB = points[pointIndex - 1];
drawLine(pointA.getX(), pointA.getY(), pointB.getX(), pointB.getY());
}
Point2D pointA = points[0];
Point2D pointB = points[pointCount - 1];
drawLine(pointA.getX(), pointA.getY(), pointB.getX(), pointB.getY());
}
private Point2I toCanvasOffset(double x, double y) {
return new Point2I(x * canvasWidth / width, y * canvasHeight / height);
}
private Point2I toCanvasPosition(double x, double y) {
return new Point2I((x - left) * canvasWidth / width, (y - top) * canvasHeight / height);
}
private static final class Point2I {
private int x;
private int y;
private Point2I(double x, double y) {
this.x = toInt(round(x));
this.y = toInt(round(y));
}
private Point2I(int x, int y) {
this.x = x;
this.y = y;
}
private Point2I() {
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private static int toInt(double value) {
@SuppressWarnings("NumericCastThatLosesPrecision") int intValue = (int) value;
if (abs((double) intValue - value) < 1.0D) {
return intValue;
}
throw new IllegalArgumentException("Can't convert double " + value + " to int.");
}
}
private static final class Point2D {
private double x;
private double y;
private Point2D(double x, double y) {
this.x = x;
this.y = y;
}
private Point2D() {
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
}