-
Notifications
You must be signed in to change notification settings - Fork 2
/
1
389 lines (339 loc) · 11.4 KB
/
1
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*****************************************************************************
** ANGRYBIRDS AI AGENT FRAMEWORK
** Copyright (c) 2014, XiaoYu (Gary) Ge, Stephen Gould, Jochen Renz
** Sahan Abeyasinghe,Jim Keys, Andrew Wang, Peng Zhang
** All rights reserved.
**This work is licensed under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
**To view a copy of this license, visit http://www.gnu.org/licenses/
*****************************************************************************/
package ab.demo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import ab.demo.other.ClientActionRobot;
import ab.demo.other.ClientActionRobotJava;
import ab.planner.TrajectoryPlanner;
import ab.vision.ABObject;
import ab.vision.GameStateExtractor.GameState;
import ab.vision.Vision;
import ab.internal.InternalClient;
import ab.internal.InternalServer;
//Naive agent (server/client version)
public class ClientNaiveAgent implements Runnable {
//Wrapper of the communicating messages
private ClientActionRobotJava ar;
public byte currentLevel = -1;
public int failedCounter = 0;
public int[] solved;
TrajectoryPlanner tp;
private int id = 28888;
private boolean firstShot;
private Point prevTarget;
private Random randomGenerator;
private InternalClient inClient;
private InternalServer inServer;
/**
* Constructor using the default IP
* */
public ClientNaiveAgent() {
// the default ip is the localhost
ar = new ClientActionRobotJava("127.0.0.1");
tp = new TrajectoryPlanner();
randomGenerator = new Random();
prevTarget = null;
firstShot = true;
inClient = new InternalClient();
try {
inServer = new InternalServer();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Constructor with a specified IP
* */
public ClientNaiveAgent(String ip) {
ar = new ClientActionRobotJava(ip);
tp = new TrajectoryPlanner();
randomGenerator = new Random();
prevTarget = null;
firstShot = true;
inClient = new InternalClient();
try {
inServer = new InternalServer();
} catch (Exception e) {
e.printStackTrace();
}
}
public ClientNaiveAgent(String ip, int id)
{
ar = new ClientActionRobotJava(ip);
tp = new TrajectoryPlanner();
randomGenerator = new Random();
prevTarget = null;
firstShot = true;
this.id = id;
inClient = new InternalClient();
try {
inServer = new InternalServer();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getNextLevel()
{
int level = 0;
boolean unsolved = false;
//all the level have been solved, then get the first unsolved level
for (int i = 0; i < solved.length; i++)
{
if(solved[i] == 0 )
{
unsolved = true;
level = i + 1;
if(level <= currentLevel && currentLevel < solved.length)
continue;
else
return level;
}
}
if(unsolved)
return level;
level = (currentLevel + 1)%solved.length;
if(level == 0)
level = solved.length;
return level;
}
/*
* Run the Client (Naive Agent)
*/
private void checkMyScore()
{
int[] scores = ar.checkMyScore();
System.out.println(" My score: ");
int level = 1;
for(int i: scores)
{
System.out.println(" level " + level + " " + i);
if (i > 0)
solved[level - 1] = 1;
level ++;
}
}
public void run() {
byte[] info = ar.configure(ClientActionRobot.intToByteArray(id));
solved = new int[info[2]];
//load the initial level (default 1)
//Check my score
checkMyScore();
currentLevel = (byte)getNextLevel();
ar.loadLevel(currentLevel);
//ar.loadLevel((byte)9);
GameState state;
while (true) {
state = solve();
//If the level is solved , go to the next level
if (state == GameState.WON) {
///System.out.println(" loading the level " + (currentLevel + 1) );
checkMyScore();
System.out.println();
currentLevel = (byte)getNextLevel();
ar.loadLevel(currentLevel);
//ar.loadLevel((byte)9);
//display the global best scores
int[] scores = ar.checkScore();
System.out.println("Global best score: ");
for (int i = 0; i < scores.length ; i ++)
{
System.out.print( " level " + (i+1) + ": " + scores[i]);
}
System.out.println();
// make a new trajectory planner whenever a new level is entered
tp = new TrajectoryPlanner();
// first shot on this level, try high shot first
firstShot = true;
} else
//If lost, then restart the level
if (state == GameState.LOST) {
failedCounter++;
if(failedCounter > 3)
{
failedCounter = 0;
currentLevel = (byte)getNextLevel();
ar.loadLevel(currentLevel);
//ar.loadLevel((byte)9);
}
else
{
System.out.println("restart");
ar.restartLevel();
}
} else
if (state == GameState.LEVEL_SELECTION) {
System.out.println("unexpected level selection page, go to the last current level : "
+ currentLevel);
ar.loadLevel(currentLevel);
} else if (state == GameState.MAIN_MENU) {
System.out
.println("unexpected main menu page, reload the level : "
+ currentLevel);
ar.loadLevel(currentLevel);
} else if (state == GameState.EPISODE_MENU) {
System.out.println("unexpected episode menu page, reload the level: "
+ currentLevel);
ar.loadLevel(currentLevel);
}
}
}
/**
* Solve a particular level by shooting birds directly to pigs
* @return GameState: the game state after shots.
*/
public GameState solve()
{
// capture Image
BufferedImage screenshot = ar.doScreenShot();
// process image
Vision vision = new Vision(screenshot);
Rectangle sling = vision.findSlingshotMBR();
//If the level is loaded (in PLAYING state)but no slingshot detected, then the agent will request to fully zoom out.
while (sling == null && ar.checkState() == GameState.PLAYING) {
System.out.println("no slingshot detected. Please remove pop up or zoom out");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ar.fullyZoomOut();
screenshot = ar.doScreenShot();
vision = new Vision(screenshot);
sling = vision.findSlingshotMBR();
}
// Passing values to InternalServer
try {
inClient.send("HELLO ANGRYBIRDS");
} catch (Exception e) {
e.printStackTrace();
}
// get all the pigs
List<ABObject> pigs = vision.findPigsMBR();
GameState state = ar.checkState();
// if there is a sling, then play, otherwise skip.
if (sling != null) {
//If there are pigs, we pick up a pig randomly and shoot it.
if (!pigs.isEmpty()) {
Point releasePoint = null;
// random pick up a pig
ABObject pig = pigs.get(randomGenerator.nextInt(pigs.size()));
Point _tpt = pig.getCenter();
// if the target is very close to before, randomly choose a
// point near it
if (prevTarget != null && distance(prevTarget, _tpt) < 10) {
double _angle = randomGenerator.nextDouble() * Math.PI * 2;
_tpt.x = _tpt.x + (int) (Math.cos(_angle) * 10);
_tpt.y = _tpt.y + (int) (Math.sin(_angle) * 10);
System.out.println("Randomly changing to " + _tpt);
}
prevTarget = new Point(_tpt.x, _tpt.y);
// estimate the trajectory
ArrayList<Point> pts = tp.estimateLaunchPoint(sling, _tpt);
// do a high shot when entering a level to find an accurate velocity
if (firstShot && pts.size() > 1) {
releasePoint = pts.get(1);
} else
if (pts.size() == 1)
releasePoint = pts.get(0);
else
if(pts.size() == 2)
{
// System.out.println("first shot " + firstShot);
// randomly choose between the trajectories, with a 1 in
// 6 chance of choosing the high one
if (randomGenerator.nextInt(6) == 0)
releasePoint = pts.get(1);
else
releasePoint = pts.get(0);
}
Point refPoint = tp.getReferencePoint(sling);
// Get the release point from the trajectory prediction module
int tapTime = 0;
if (releasePoint != null) {
double releaseAngle = tp.getReleaseAngle(sling,
releasePoint);
System.out.println("Release Point: " + releasePoint);
System.out.println("Release Angle: "
+ Math.toDegrees(releaseAngle));
int tapInterval = 0;
switch (ar.getBirdTypeOnSling())
{
case RedBird:
tapInterval = 0; break; // start of trajectory
case YellowBird:
tapInterval = 65 + randomGenerator.nextInt(25);break; // 65-90% of the way
case WhiteBird:
tapInterval = 50 + randomGenerator.nextInt(20);break; // 50-70% of the way
case BlackBird:
tapInterval = 0;break; // 70-90% of the way
case BlueBird:
tapInterval = 65 + randomGenerator.nextInt(20);break; // 65-85% of the way
default:
tapInterval = 60;
}
tapTime = tp.getTapTime(sling, releasePoint, _tpt, tapInterval);
} else
{
System.err.println("No Release Point Found");
return ar.checkState();
}
// check whether the slingshot is changed. the change of the slingshot indicates a change in the scale.
ar.fullyZoomOut();
screenshot = ar.doScreenShot();
vision = new Vision(screenshot);
Rectangle _sling = vision.findSlingshotMBR();
if(_sling != null)
{
double scale_diff = Math.pow((sling.width - _sling.width),2) + Math.pow((sling.height - _sling.height),2);
if(scale_diff < 25)
{
int dx = (int) releasePoint.getX() - refPoint.x;
int dy = (int) releasePoint.getY() - refPoint.y;
if(dx < 0)
{
long timer = System.currentTimeMillis();
ar.shoot(refPoint.x, refPoint.y, dx, dy, 0, tapTime, false);
System.out.println("It takes " + (System.currentTimeMillis() - timer) + " ms to take a shot");
state = ar.checkState();
if ( state == GameState.PLAYING )
{
screenshot = ar.doScreenShot();
vision = new Vision(screenshot);
List<Point> traj = vision.findTrajPoints();
tp.adjustTrajectory(traj, sling, releasePoint);
firstShot = false;
}
}
}
else
System.out.println("Scale is changed, can not execute the shot, will re-segement the image");
}
else
System.out.println("no sling detected, can not execute the shot, will re-segement the image");
}
}
return state;
}
private double distance(Point p1, Point p2) {
return Math.sqrt((double) ((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)* (p1.y - p2.y)));
}
public static void main(String args[]) {
ClientNaiveAgent na;
if(args.length > 0)
na = new ClientNaiveAgent(args[0]);
else
na = new ClientNaiveAgent();
na.run();
}
}