-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimple.java
More file actions
558 lines (497 loc) · 19.1 KB
/
Simple.java
File metadata and controls
558 lines (497 loc) · 19.1 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
<Team name>
<Team member name> : <evaluation_score>
<Team member name> : <evaluation_score>
Add a comment at the top of the simple.java file specifying your robocup agent's task environment and
its properties.
Is your robocup agent simple reflex, model based, goal based, utility based or a learning agent? Explain
why in no more than five sentences (include this also as a comment at the top of the simple.java file).
/**
* Simple is a reflex agent. Simple stores its percepts in fields which get reset and effectively lost
* before every new set of percepts is received (reset in preInfo() method). Because of this, Simple is
* only reacting to its latest sensory info at any one time. This reaction is defined in postInfo() method
* and never changes.
*
* Performance: Score, Aggregate Score, Goals, Successful Passes, Possession, Environment: Dynamic, Continuous, Multi-Agent, Episodic, Stochtastic, Goal Posts, Coach, Players, Crowd, Football Pitch, Football,
Actuators: Robotic Legs, Robotic Arms.
Sensors: Visual Sensors,
//~--- non-JDK imports --------------------------------------------------------
import com.github.robocup_atan.atan.model.ActionsPlayer;
import com.github.robocup_atan.atan.model.ControllerPlayer;
import com.github.robocup_atan.atan.model.enums.Errors;
import com.github.robocup_atan.atan.model.enums.Flag;
import com.github.robocup_atan.atan.model.enums.Line;
import com.github.robocup_atan.atan.model.enums.Ok;
import com.github.robocup_atan.atan.model.enums.PlayMode;
import com.github.robocup_atan.atan.model.enums.RefereeMessage;
import com.github.robocup_atan.atan.model.enums.ServerParams;
import com.github.robocup_atan.atan.model.enums.ViewAngle;
import com.github.robocup_atan.atan.model.enums.ViewQuality;
import com.github.robocup_atan.atan.model.enums.Warning;
import org.apache.log4j.Logger;
//~--- JDK imports ------------------------------------------------------------
import java.util.HashMap;
import java.util.Random;
/**
* A simple controller. It implements the following simple behaviour. If the
* client sees nothing (it might be out of the field) it turns 180 degree. If
* the client sees the own goal and the distance is less than 40 and greater
* than 10 it turns to his own goal and dashes. If it cannot see the own goal
* but can see the ball it turns to the ball and dashes. If it sees anything but
* not the ball or the own goals it dashes a little bit and turns a fixed amount
* of degree to the right.
*
* @author Atan
*/
public class Simple implements ControllerPlayer {
private static int count = 0;
private static Logger log = Logger.getLogger(Simple.class);
private Random random = null;
private boolean canSeeOwnGoal = false;
private boolean canSeeNothing = true;
private boolean canSeeBall = false;
private double directionBall;
private double directionOwnGoal;
private double distanceBall;
private double distanceOwnGoal;
private ActionsPlayer player;
//i used to turn player's head every preInfo().
private int i = 1;
//Type of player added
private String type;
//fields for holding opponent's goal information
private double directionOtherGoal;
private double distanceOtherGoal;
//fixed values for dashing keep stamina under control
private int fastDash = 70;
private int slowDash = 40;
private double distanceRight;
private double distanceLeft;
private boolean canSeeFlagRight = false;
private boolean canSeeFlagLeft = false;
private double distanceFlagCenter;
/**
* Constructs a new simple client.
*/
public Simple() {
random = new Random(System.currentTimeMillis() + count);
count++;
}
/** {@inheritDoc} */
@Override
public ActionsPlayer getPlayer() {
return player;
}
/** {@inheritDoc}
* turns player neck 30degres to the right to begin with so
* player will turn head evenly, compared to body,
every preInfo().*/
@Override
public void setPlayer(ActionsPlayer p) {
player = p;
player.turnNeck(30);
}
/** {@inheritDoc}
*Resets canSeeOwnGoal, canSeeBall, and canSeeNothing so that
*if the player has lost track of it's own goal or the ball since
*the last preInfo() it will default to seeing nothing.
*Also alternately turns the player's head right and left by 80degrees.*/
@Override
public void preInfo() {
canSeeOwnGoal = false;
canSeeBall = false;
canSeeNothing = true;
distanceRight = 0.0;
distanceLeft = 0.0;
distanceFlagCenter = 0.0;
if(i%2 == 1){
getPlayer().turnNeck(-60);
} else {
getPlayer().turnNeck(60);
}
i++;
}
/** {@inheritDoc}
* Provides different behaviour based on the player type.*/
@Override
public void postInfo() {
//defines Goalie behaviour
if(this.getType().equals("Goalie")){
if (canSeeNothing) {
canSeeNothingAction();}
else if(canSeeBall && (distanceBall < 0.7)) {
this.getPlayer().catchBall(directionBall);
}else if(canSeeBall) {
this.getPlayer().turn(directionBall);
}else if((distanceRight > 50) || (distanceLeft > 50) || (distanceFlagCenter < 30)){
if(canSeeOwnGoal){
this.getPlayer().turn(directionOwnGoal);
this.getPlayer().dash(60);
} else {
this.getPlayer().turn(160);
}
}else if(canSeeOwnGoal) {
this.getPlayer().turn(160);
}else {
canSeeAnythingAction();
}
//defines Defender behaviour
}else if (this.getType().equals("Defender")){
if (canSeeNothing) {
canSeeNothingAction();
}else if((distanceRight > 60) || (distanceLeft > 60) || (distanceFlagCenter > 30)) {
}
//defines Midfielder behaviour
else if(this.getType().equals("Midfielder")){
if (canSeeNothing) {
canSeeNothingAction();
}
//defines Attacker behaviour
else if(this.getType().equals("Attacker")){
if (canSeeNothing) {
canSeeNothingAction();
}
}
}
}
}
/**
if (canSeeNothing) {
canSeeNothingAction();
} else if (canSeeOwnGoal) {
//decreased distance players must be from own goal before canSeeOwnGoalAction is called.
if ((distanceOwnGoal < 25) && (distanceOwnGoal > 10)) {
canSeeOwnGoalAction();
} else if (canSeeBall) {
canSeeBallAction();
} else {
canSeeAnythingAction();
}
} else if (canSeeBall) {
canSeeBallAction();
} else {
canSeeAnythingAction();
}
*/
/**
* if (distBall < 10) {
getPlayer().turn(10.0);
getPlayer().dash(5);
} else if (minDistLine < 10) {
getPlayer().turn(-90.0);
} else {
getPlayer().dash(50);
}
log.info("I am a silly Client");
*/
/** {@inheritDoc} */
@Override
public void infoSeeFlagRight(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
canSeeFlagRight = true;
distanceRight = distance;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagLeft(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
canSeeFlagLeft = true;
distanceLeft = distance;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagOwn(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagOther(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagCenter(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
distanceFlagCenter = distance;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagCornerOwn(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagCornerOther(Flag flag, double distance, double direction, double distChange,
double dirChange, double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagPenaltyOwn(Flag flag, double distance, double direction, double distChange,
double dirChange, double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagPenaltyOther(Flag flag, double distance, double direction, double distChange,
double dirChange, double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagGoalOwn(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
if (flag == Flag.CENTER) {
this.canSeeOwnGoal = true;
this.distanceOwnGoal = distance;
this.directionOwnGoal = direction;
}
}
/** {@inheritDoc} */
@Override
public void infoSeeFlagGoalOther(Flag flag, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
directionOtherGoal = direction;
distanceOtherGoal = distance;
}
/** {@inheritDoc} */
@Override
public void infoSeeLine(Line line, double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
}
/** {@inheritDoc} */
@Override
public void infoSeePlayerOther(int number, boolean goalie, double distance, double direction, double distChange,
double dirChange, double bodyFacingDirection, double headFacingDirection) {}
/** {@inheritDoc} */
@Override
public void infoSeePlayerOwn(int number, boolean goalie, double distance, double direction, double distChange,
double dirChange, double bodyFacingDirection, double headFacingDirection) {}
/** {@inheritDoc} */
@Override
public void infoSeeBall(double distance, double direction, double distChange, double dirChange,
double bodyFacingDirection, double headFacingDirection) {
canSeeNothing = false;
canSeeBall = true;
distanceBall = distance;
directionBall = direction;
}
/** {@inheritDoc} */
@Override
public void infoHearReferee(RefereeMessage refereeMessage) {}
/** {@inheritDoc} */
//added player repositioning on the field in case of goal scored by either side.
@Override
public void infoHearPlayMode(PlayMode playMode) {
if ((playMode == PlayMode.BEFORE_KICK_OFF) || (playMode == PlayMode.GOAL_OWN) || (playMode == PlayMode.GOAL_OTHER)) {
this.pause(1000);
switch (this.getPlayer().getNumber()) {
case 1 :
this.getPlayer().move(-45, 0);
this.setType("Goalie");
break;
case 2 :
this.getPlayer().move(-10, 10);
this.setType("Attacker");
break;
case 3 :
this.getPlayer().move(-10, -10);
this.setType("Attacker");
break;
case 4 :
this.getPlayer().move(-20, 0);
this.setType("Attacker");
break;
case 5 :
this.getPlayer().move(-20, 10);
this.setType("Midfielder");
break;
case 6 :
this.getPlayer().move(-20, -10);
this.setType("Midfielder");
break;
case 7 :
this.getPlayer().move(-20, 20);
this.setType("Midfielder");
break;
case 8 :
this.getPlayer().move(-20, -20);
this.setType("Midfielder");
break;
case 9 :
this.getPlayer().move(-30, 0);
this.setType("Defender");
break;
case 10 :
this.getPlayer().move(-40, 10);
this.setType("Defender");
break;
case 11 :
this.getPlayer().move(-40, -10);
this.setType("Defender");
break;
default :
throw new Error("number must be initialized before move");
}
}
}
/** {@inheritDoc} */
@Override
//awesome. keep distances.
public void infoHearPlayer(double direction, String message) {}
/** {@inheritDoc} */
@Override
public void infoSenseBody(ViewQuality viewQuality, ViewAngle viewAngle, double stamina, double unknown,
double effort, double speedAmount, double speedDirection, double headAngle,
int kickCount, int dashCount, int turnCount, int sayCount, int turnNeckCount,
int catchCount, int moveCount, int changeViewCount) {}
/** {@inheritDoc} */
@Override
public String getType() {
return type;
}
/** {@inheritDoc} */
@Override
public void setType(String newType) {
type = newType;
}
/** {@inheritDoc} */
@Override
public void infoHearError(Errors error) {}
/** {@inheritDoc} */
@Override
public void infoHearOk(Ok ok) {}
/** {@inheritDoc} */
@Override
public void infoHearWarning(Warning warning) {}
/** {@inheritDoc} */
@Override
public void infoPlayerParam(double allowMultDefaultType, double dashPowerRateDeltaMax,
double dashPowerRateDeltaMin, double effortMaxDeltaFactor, double effortMinDeltaFactor,
double extraStaminaDeltaMax, double extraStaminaDeltaMin,
double inertiaMomentDeltaFactor, double kickRandDeltaFactor,
double kickableMarginDeltaMax, double kickableMarginDeltaMin,
double newDashPowerRateDeltaMax, double newDashPowerRateDeltaMin,
double newStaminaIncMaxDeltaFactor, double playerDecayDeltaMax,
double playerDecayDeltaMin, double playerTypes, double ptMax, double randomSeed,
double staminaIncMaxDeltaFactor, double subsMax) {}
/** {@inheritDoc} */
@Override
public void infoPlayerType(int id, double playerSpeedMax, double staminaIncMax, double playerDecay,
double inertiaMoment, double dashPowerRate, double playerSize, double kickableMargin,
double kickRand, double extraStamina, double effortMax, double effortMin) {}
/** {@inheritDoc} */
@Override
public void infoCPTOther(int unum) {}
/** {@inheritDoc} */
@Override
public void infoCPTOwn(int unum, int type) {}
/** {@inheritDoc} */
@Override
public void infoServerParam(HashMap<ServerParams, Object> info) {}
/**
* This is the action performed when the player can see the ball.
* It involves running at it and kicking it...
*/
//substituted for fastDash
private void canSeeBallAction() {
if (distanceBall < 0.7) {
getPlayer().kick(50, directionOtherGoal);
} else {
if (distanceBall < 25) {
getPlayer().dash(fastDash);
} else {
}
}
getPlayer().dash(fastDash);
turnTowardBall();
if (log.isDebugEnabled()) {
log.debug("b(" + directionBall + "," + distanceBall + ")");
}
}
/**
* If the player can see anything that is not a ball or a goal, it turns.
*/
//substituted for fastDash
private void canSeeAnythingAction() {
getPlayer().dash(0);
if (log.isDebugEnabled()) {
log.debug("a");
}
}
/**
* If the player can see nothing, it turns 180 degrees.
*/
private void canSeeNothingAction() {
getPlayer().turn(180);
if (log.isDebugEnabled()) {
log.debug("n");
}
}
/**
* If the player can see its own goal, it goes and stands by it...
*/
//new fastDash and slowDash fields. Substituted random values in order to get stamina under control.
private void canSeeOwnGoalAction() {
getPlayer().dash(fastDash);
turnTowardOwnGoal();
if (log.isDebugEnabled()) {
log.debug("g(" + directionOwnGoal + "," + distanceOwnGoal + ")");
}
}
/**
* Randomly choose a fast dash value.
* @return
*
private int randomDashValueFast() {
return 30 + random.nextInt(100);
}
/**
* Randomly choose a slow dash value.
* @return
*
private int randomDashValueSlow() {
return -10 + random.nextInt(50);
}
*/
/**
* Turn towards the ball.
*/
private void turnTowardBall() {
getPlayer().turn(directionBall);
}
/**
* Turn towards our goal.
*/
private void turnTowardOwnGoal() {
getPlayer().turn(directionOwnGoal);
}
/**
* Randomly choose a kick direction.
* @return
*
private int randomKickDirectionValue() {
return -45 + random.nextInt(90);
}
*/
/**
* Pause the thread.
* @param ms How long to pause the thread for (in ms).
*/
private synchronized void pause(int ms) {
try {
this.wait(ms);
} catch (InterruptedException ex) {
log.warn("Interrupted Exception ", ex);
}
}
}