Repository navigation
Expand file tree
/
Copy pathKrisletEnvironment.java
More file actions
645 lines (596 loc) · 20.9 KB
/
Copy pathKrisletEnvironment.java
File metadata and controls
645 lines (596 loc) · 20.9 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import java.io.*;
import java.net.*;
import jason.mas2j.*;
import jason.asSyntax.*;
import jason.environment.*;
import java.util.*;
import java.util.logging.*;
import java.util.regex.Pattern;
public class KrisletEnvironment extends Environment
{
/* We can make a 5-on-5 team if needed. */
public static final int C_MAX_PLAYERS = 10;
public static final int C_INVALID_PLAYER = -99;
public static final boolean DEBUG_LOGGING = false;
private Logger logger = Logger.getLogger("SYSC5103_Project.mas2j."+KrisletEnvironment.class.getName());
private Krislet player[];
/** Called before the MAS execution with the args informed in .mas2j */
@Override
public void init(String[] args) {
jason.mas2j.parser.mas2j parser;
MAS2JProject project;
List<String> names = new ArrayList<String>();
boolean isGoalie = false;
String hostName = "localhost";
String team = "MEMA";
int port = 6000;
int playerNumber = 0;
int count = 0;
super.init(args);
player = new Krislet[C_MAX_PLAYERS];
try
{
/**
* Parse the project file to get the list of agents that exist. When
* we create our Krislet player, we will assign them that name,
* since that's what jason will refer to them as when it executes
* actions.
*/
parser = new jason.mas2j.parser.mas2j(new FileInputStream(args[0]));
project = parser.mas();
// Get the names from the project
for (AgentParameters ap : project.getAgents())
{
String agName = ap.name;
for (int cAg = 0; cAg < ap.getNbInstances(); cAg++)
{
String numberedAg = agName;
if (ap.getNbInstances() > 1)
{
numberedAg += (cAg + 1);
}
/**
* If we need to generate the entire team ourselves, we can
* double the number of agents created in the .mas2j file
* and enable this code.
*/
/*
if (count % 2 == 0)
{
team = "MEMA_Home";
}
else
{
team = "MEMA_Away";
}
++count;
*/
/**
* We want to notify the Soccer Server that this agent is to
* be designated as a goalie.
*/
isGoalie = agName.equals("keeperAgent");
/**
* Stick the agent name into the Krislet object so we can
* associate it with what we get from the jason framework.
*/
player[playerNumber] = new Krislet(InetAddress.getByName(hostName),
port,
team,
numberedAg,
isGoalie);
player[playerNumber].mainInit();
player[playerNumber].mainUpdate();
updatePlayer(player[playerNumber]);
++playerNumber;
}
}
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
/**
* List of different actions that the agents will take
*/
public static final Literal enteringInTheField = Literal.parseLiteral("enteringInTheField");
public static final Literal turn20 = Literal.parseLiteral("turn20");
public static final Literal turn40 = Literal.parseLiteral("turn40");
public static final Literal turn120 = Literal.parseLiteral("turn120");
public static final Literal goToBall = Literal.parseLiteral("goToBall");
public static final Literal goToBallSlowly = Literal.parseLiteral("goToBallSlowly");
public static final Literal goToBallQuickly = Literal.parseLiteral("goToBallQuickly");
public static final Literal turnToBall = Literal.parseLiteral("turnToBall");
public static final Literal kickBall50 = Literal.parseLiteral("kickBall50");
public static final Literal noAction = Literal.parseLiteral("noAction");
public static final Literal runToDefGoal = Literal.parseLiteral("runTowardsDefGoal");
public static final Literal turnTowardsDefGoal = Literal.parseLiteral("turnTowardsDefGoal");
public static final Literal turnTowardsDefFlag = Literal.parseLiteral("turnTowardsDefFlag");
public static final Literal runTowardsDefFlag = Literal.parseLiteral("runTowardsDefFlag");
public static final Literal clearBall = Literal.parseLiteral("clearBall");
public static final Literal clearBallBackwards = Literal.parseLiteral("clearBallBackwards");
public static final Literal enteringInTheGoal = Literal.parseLiteral("enteringInTheGoal");
@Override
public boolean executeAction(String agName, Structure action) {
int playerNumber = C_INVALID_PLAYER;
// Check which player is execing an action
playerNumber = getPlayerNumber(agName);
if (playerNumber == C_INVALID_PLAYER)
return false;
try{
player[playerNumber].mainUpdate();
}catch(Exception e){
logger.info(e.getMessage());
}
// Call act with the given command, if it fails log the result.
if(!act(action.toString(),player[playerNumber]))
{
if (DEBUG_LOGGING)
{
logger.info("*****\nError: " + action + ", failed to execute!\n*****");
}
}
else
{
if (DEBUG_LOGGING)
{
logger.info("Executed: " + action);
}
}
updatePlayer(player[playerNumber]);
informAgsEnvironmentChanged();
try
{
player[playerNumber].mainUpdate();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true; // the action was executed with success
}
/*
* This function takes as parameters:
* @action: String for the action to be performed
* @player: Krislet object to whom the action should be applied
* returns true if the action as been performed and false otherwise
*/
public boolean act(String action, Krislet player)
{
ObjectInfo ball;
ObjectInfo attackingGoal;
ObjectInfo defendingGoal;
ObjectInfo defendingBox;
ObjectInfo defendingLine;
/**
* Get the information that a given player can see. The ball will be the
* same for both sides, but attacking/defending objects will be
* different for each side.
*/
ball = player.m_memory.getObject("ball");
if( player.m_side == 'l' )
{
attackingGoal = player.m_memory.getObject("goal r");
defendingGoal = player.m_memory.getObject("goal l");
defendingBox = player.m_memory.getObject("flag p l c");
defendingLine = player.m_memory.getObject("line l");
}
else
{
attackingGoal = player.m_memory.getObject("goal l");
defendingGoal = player.m_memory.getObject("goal r");
defendingBox = player.m_memory.getObject("flag p r c");
defendingLine = player.m_memory.getObject("line r");
}
if(action.equals("enteringInTheField")) {
/**
* A non-goalie player enters the field in a random location on
* their side.
*/
player.inField = true;
player.move( -Math.random()*52.5 , 34 - Math.random()*68.0 );
}
else if(action.equals("stopPlaying")) {
/**
* Players disconnect from the server
*/
player.bye();
}
else if(action.equals("turn20")) {
/**
* Player turns 20 degrees
*/
player.turn(20);
}
else if(action.equals("turn40")) {
/**
* Player turns 40 degrees
*/
player.turn(40);
}
else if(action.equals("turn80")) {
/**
* Player turns 80 degrees
*/
player.turn(80);
}
else if(action.equals("turn120")) {
/**
* Player turns 120 degrees
*/
player.turn(120);
}
else if(action.equals("turnToBall") && ball!=null) {
/**
* Player turns towards the ball, if the ball can be seen
*/
player.turn(ball.m_direction);
}
else if(action.equals("dash")) {
/**
* Player dashes forwards with a power of 100
*/
player.dash(100);
}
else if(action.equals("goToBall") && ball!=null) {
/**
* Player dashes towards the ball with a power of 10 * the distance
* from the ball, if the ball can be seen
*/
player.dash(10*ball.m_distance);
}
else if(action.equals("goToBallSlowly") && ball!=null) {
/**
* Player dashes towards the ball with a power of 0.5 * the distance
* from the ball, if the ball can be seen
*/
player.dash(0.5*ball.m_distance);
}
else if(action.equals("goToBallQuickly") && ball!=null) {
/**
* Player dashes towards the ball with a power of 20 * the distance
* from the ball, if the ball can be seen
*/
player.dash(20*ball.m_distance);
}
else if(action.equals("kickBall50") && attackingGoal!=null) {
/**
* Player kicks the ball with a power of 50 towards the attacking
* goal, if it can be seen.
*/
player.kick(50, attackingGoal.m_direction);
}
else if(action.equals("kickBall100") && attackingGoal!=null) {
/**
* Player kicks the ball with a power of 100 towards the attacking
* goal, if it can be seen.
*/
player.kick(100, attackingGoal.m_direction);
}
else if(action.equals("runTowardsDefGoal") && defendingGoal!=null) {
/**
* Player dashes towards the defending goal with a power of 100 *
* the distance from the goal, if the goal can be seen
*/
player.dash(100 * defendingGoal.m_distance);
}
else if(action.equals("turnTowardsDefGoal") && defendingGoal!=null) {
/**
* Player turns towards the defending goal, if the goal can be seen
*/
player.turn(defendingGoal.m_direction);
}
else if(action.equals("runTowardsDefFlag") && defendingBox!=null) {
/**
* Player dashes towards the defending flag (middle flag of the
* penalty box) with a power of 100 * the distance from the flag,
* if the flag can be seen
*/
player.dash(100 * defendingBox.m_distance);
}
else if(action.equals("turnTowardsDefFlag") && defendingBox!=null) {
/**
* Player turns towards the defending flag (middle flag of the
* penalty box), if the flag can be seen
*/
player.turn(defendingBox.m_direction);
}
else if(action.equals("clearBall")) {
/**
* Player kicks the ball with a power of 100 forwards
*/
player.kick(100, 0);
}
else if (action.equals("clearBallBackwards")) {
/**
* Player kicks the ball with a power of 100 backwards
*/
player.kick(100, 180);
}
else if(action.equals("enteringInTheGoal")) {
/**
* A goalie player enters the field in front of their goal
*/
player.inField = true;
player.move(-50, 0);
}
else if(action.equals("noAction")) {
}
else {
// If the action was unrecognized then return false
return false;
}
// Otherwise the action was properly handled, return true.
return true;
}
/** Called before the end of MAS execution */
@Override
public void stop() {
super.stop();
}
/** Private helper functions*/
private void updatePlayer(Krislet p)
{
try
{
clearPercepts(p.m_name);
ObjectInfo ball = p.m_memory.getObject("ball");
ObjectInfo attackingGoal;
ObjectInfo defendingGoal;
ObjectInfo defendingBox;
ObjectInfo defendingLine;
if( p.m_side == 'l' )
{
attackingGoal = p.m_memory.getObject("goal r");
defendingGoal = p.m_memory.getObject("goal l");
defendingBox = p.m_memory.getObject("flag p l c");
defendingLine = p.m_memory.getObject("line l");
}
else
{
attackingGoal = p.m_memory.getObject("goal l");
defendingGoal = p.m_memory.getObject("goal r");
defendingBox = p.m_memory.getObject("flag p r c");
defendingLine = p.m_memory.getObject("line r");
}
if(/*Pattern.matches("^before_kick_off.*",m_playMode) &&*/ !p.inField) {
p.inField= true;
addPercept(p.m_name, ASSyntax.parseLiteral("readyToStart"));
if (DEBUG_LOGGING)
{
logger.info("readyToStart");
}
}
/*else if(//m_timeOver) {
//return "TimeOver";
}*/
if( ball == null ) {
// If you don't know where is ball then find it
//addPercept(p.m_name, ASSyntax.parseLiteral("noBall"));
if (DEBUG_LOGGING)
{
logger.info("Add cannotSeeBall to " + p.m_name);
}
} else if(ball.m_distance > 1.0 && ball.m_direction != 0 ) {
// If ball is too far to kick and we are not facing it
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeBall"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeBall to " + p.m_name);
}
} else if (ball.m_distance > 1.0 && ball.m_direction == 0){
// If ball is too far to kick and we are facing it
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeBall"));
addPercept(p.m_name, ASSyntax.parseLiteral("facingBall"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeBall to " + p.m_name);
logger.info("Add facingBall to " + p.m_name);
}
} else {
// Close enough to kick the ball
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeBall"));
addPercept(p.m_name, ASSyntax.parseLiteral("canKickBall"));
addPercept(p.m_name, ASSyntax.parseLiteral("clearBall"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeBall to " + p.m_name);
logger.info("Add canKickBall to " + p.m_name);
logger.info("Add clearBall to " + p.m_name);
}
}
if(p.inField && closestToBall(p,2)){
addPercept(p.m_name, ASSyntax.parseLiteral("closestToBall"));
if (DEBUG_LOGGING)
{
logger.info("Add closestToBall " + p.m_name);
}
}
else{
if (DEBUG_LOGGING)
{
logger.info("Add not closestToBall" + p.m_name);
}
}
// Look for attackingGoal
if ( attackingGoal == null ) {
//addPercept(p.m_name, ASSyntax.parseLiteral("noGoal"));
if (DEBUG_LOGGING)
{
logger.info("Add cannotSeeAttackingGoal to " + p.m_name);
}
} else {
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeAttackingGoal"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeAttackingGoal to " + p.m_name);
}
}
// Look for defendingGoal
if (defendingGoal != null)
{
/**
* Player can see the defending goal, but might not be aligned
* with it
*/
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeDefGoal"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeDefGoal to " + p.m_name);
}
if (defendingGoal.m_direction == 0)
{
/**
* Player is aligned with the defending goal
*/
addPercept(p.m_name, ASSyntax.parseLiteral("facingDefGoal"));
if (DEBUG_LOGGING)
{
logger.info("Add facingDefGoal to " + p.m_name);
}
}
if (defendingGoal.m_distance < 5)
{
/**
* Player is at the defending goal
*/
addPercept(p.m_name, ASSyntax.parseLiteral("atDefGoal"));
if (DEBUG_LOGGING)
{
logger.info("Add atDefGoal to " + p.m_name);
}
}
}
// Look for the flag at the center of the defending penalty box
if (defendingBox != null)
{
/**
* Player can see the defending flag, but might not be aligned
* with it
*/
addPercept(p.m_name, ASSyntax.parseLiteral("canSeeDefFlag"));
if (DEBUG_LOGGING)
{
logger.info("Add canSeeDefFlag to " + p.m_name);
}
if (defendingBox.m_direction == 0)
{
/**
* Player is aligned with the defending flag
*/
addPercept(p.m_name, ASSyntax.parseLiteral("facingDefFlag"));
if (DEBUG_LOGGING)
{
logger.info("Add facingDefFlag to " + p.m_name);
}
}
if (defendingBox.m_distance < 10)
{
/**
* Player is at the defending flag
*/
addPercept(p.m_name, ASSyntax.parseLiteral("inPositionToDefend"));
if (DEBUG_LOGGING)
{
logger.info("Add inPositionToDefend to " + p.m_name);
}
}
}
if ((ball != null) && (ball.m_distance < 15.0))
{
// Ball is close enough to the goal to be dangerous
addPercept(p.m_name, ASSyntax.parseLiteral("dangerBall"));
if (DEBUG_LOGGING)
{
logger.info("Add dangerBall to " + p.m_name);
}
}
if (defendingLine == null)
{
/**
* Player is facing forwards (goal is at its back)
*/
addPercept(p.m_name, ASSyntax.parseLiteral("facingForwards"));
if (DEBUG_LOGGING)
{
logger.info("Add facingForwards to " + p.m_name);
}
}
} catch (Exception e) {}
}
/*
* This function takes as parameters:
* @p: Krislet object to whom the action should be applied
* @number: an int, if less than number player(s) (of my team) are "closest" to the ball than me,
* then I will run with "normal" speed to the ball, else I will reduced my seed
* returns true if number of closest than me is < number, else false
*/
private boolean closestToBall(Krislet p, int number){
int closestThanMe = 0;
LinkedList<PlayerInfo> team = getTeamPlayers(p);
ObjectInfo ball = p.m_memory.getObject("ball");
if (ball==null){
return false;
}
float distance = 0;
float my_distance = ball.m_distance;
for(int i = 0; i < team.size(); i++){
float partner_distance = team.get(i).m_distance;
if(partner_distance < my_distance){
closestThanMe++;
}
}
return closestThanMe < number;
}
/*
* This function takes as parameters:
* @player: Krislet object
* returns in a LinkedList<PlayerInfo> all the players that I can see and are in the same team
*/
public LinkedList<PlayerInfo> getTeamPlayers(Krislet player)
{
LinkedList<PlayerInfo> players = new LinkedList<PlayerInfo>();
if( player.m_memory.getInfo() == null )
return null;
for(int c = 0 ; c < player.m_memory.getInfo().m_objects.size() ; c ++)
{
ObjectInfo object = (ObjectInfo)player.m_memory.getInfo().m_objects.elementAt(c);
if(object.m_type.compareTo("player")==0){
PlayerInfo p = (PlayerInfo) object;
if(p.m_teamName.equals(player.getTeam())){
players.add(p);
}
}
}
return players;
}
/**
* Name: getPlayerNumber
*
* Parameters: agentName - String passed from the jason framework identifying a specific Agent
*
* Takes an Agent name given to us from the jason framework and sees if there is a Krislet
* object with that name. If so, returns the player number assigned to that player,
* otherwise will return an invalid number.
*
* Returns: Player number associated with the name, C_INVALID_PLAYER otherwise.
*/
private int getPlayerNumber(String agentName)
{
if (agentName == null)
{
return C_INVALID_PLAYER;
}
for (int i = 0; i < C_MAX_PLAYERS; i++)
{
if (agentName.equals(player[i].m_name))
{
return i;
}
}
return C_INVALID_PLAYER;
}
}