forked from ivanseidel/Robot-Soccer-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJudge.java
223 lines (175 loc) · 4.74 KB
/
Judge.java
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
import processing.core.*;
import processing.*;
import java.util.*;
public class Judge{
private float BALL_OUTSIDE_TIME_LIMIT = 0.5f;
private float ROBOT_TAKEN_OUTSIDE_TIME = 5f;
private float BALL_STOPPED_TIME_LIMIT = 5f;
private float GAME_DURATION = 60 * 5;
private GameController controller;
private GameSimulator simulator;
// Robots that have been taken out of field for some reason
private HashSet<Robot> takenOutRobots = new HashSet<Robot>();
private HashMap<Robot, Timer> takenOutTimers = new HashMap<Robot, Timer>();
Judge(GameController controller){
this.controller = controller;
}
// Set the time for the entire game (defaults to 5min)
float duration = GAME_DURATION;
public void setDuration(float duration){
this.duration = duration;
}
// The actual game half time (1st, 2nd...)
int gameHalf = 1;
float time;
float realTime = 0;
public void judge(float time){
// Integrate time, only if no glitches and simulation is running
if(time - this.time < 0.2 && controller.isRunning())
realTime += time - this.time;
this.time = time;
// Skip Judging if is paused
if(!controller.isRunning())
return;
// Check if Ball is outside field for a long time
checkBallOutside();
// Check if Ball is stopped for a long time
checkBallStopped();
// Remove robots from game, if they are outside
checkRobotsOutside();
// Put back robots that have been outside for longer time
putBackRobotsOutside();
// Check if scored goals
checkGoal();
// If no player in the game, re-position players
checkNoPlayer();
// Check end of Half
checkEndOfHalf();
}
/*
Returns the time to show in the UI
*/
public float getCurrentTime(){
return realTime;
}
/*
Returns the current state
*/
public String getCurrentState(){
String running = controller.isRunning() ? "Running" : "Stopped";
String half = gameHalf + " half";
if(gameHalf > 2)
half = "ENDED";
return half + " - " + running;
}
private void setHalf(int half){
if(half == 2){
controller.initTeamSides(false);
controller.restartPositions(TeamSide.LEFT);
controller.resumeGame();
}
if(half > 2){
controller.restartPositions(null);
controller.pauseGame();
}
gameHalf = half;
}
private Timer ballOutsideTimer = new Timer(BALL_OUTSIDE_TIME_LIMIT, true);
private boolean isOut = false;
private void checkBallOutside(){
Ball b = simulator.ball;
if(b.colliding(simulator.fieldArea)){
ballOutsideTimer.reset(time);
}else{
if(ballOutsideTimer.triggered(time)){
controller.moveBallToSpot(null);
}
}
}
private Timer ballStoppedTimer = new Timer(BALL_STOPPED_TIME_LIMIT, true);
private void checkBallStopped(){
Ball b = simulator.ball;
if(b.getRealSpeed().magSq() == 0){
if(ballStoppedTimer.triggered(time)){
controller.moveBallToSpot(null);
}
}else{
ballStoppedTimer.reset(time);
}
}
private void checkRobotsOutside(){
for(Robot r:controller.getRobots()){
if(takenOutRobots.contains(r))
continue;
if(!simulator.fieldArea.colliding(r)){
controller.removeRobot(r);
}
}
}
private void putBackRobotsOutside(){
for(Robot r:controller.getRobots()){
if(!takenOutRobots.contains(r))
continue;
Timer outTimer = takenOutTimers.get(r);
if(outTimer.triggered(time)){
controller.startRobot(r);
}
}
}
private void checkGoal(){
Ball b = simulator.ball;
if(b.colliding(simulator.goalLeft) && !b.colliding(simulator.fieldArea)){
controller.addPointsFor(TeamSide.RIGHT, 1);
controller.restartPositions(TeamSide.LEFT);
}else if(b.colliding(simulator.goalRight) && !b.colliding(simulator.fieldArea)){
controller.addPointsFor(TeamSide.LEFT, 1);
controller.restartPositions(TeamSide.RIGHT);
controller.resumeGame();
}
}
private void checkNoPlayer(){
int playerCount = 0;
for(Robot r:controller.getRobots()){
if(!takenOutRobots.contains(r))
playerCount++;
}
if(playerCount <= 0){
controller.restartPositions(null);
controller.resumeGame();
}
}
private void checkEndOfHalf(){
if(gameHalf <= 1 && realTime > duration / 2){
setHalf(2);
}else if(gameHalf <= 2 && realTime > duration){
// 3 Means end;
setHalf(3);
}
}
/*
***** Callbacks from Controller
*/
/*
Did reset everything
*/
public void onGameControllerReseted(){
simulator = controller.getSimulator();
takenOutRobots.clear();
setHalf(1);
time = realTime = 0;
}
public void onRobotRegistered(Robot r, TeamSide side){
takenOutRobots.add(r);
takenOutTimers.put(r, new Timer(ROBOT_TAKEN_OUTSIDE_TIME));
}
public void onRobotUnegistered(Robot r){
takenOutRobots.remove(r);
}
public void onRobotPlaced(Robot r){
takenOutRobots.remove(r);
}
public void onRobotRemoved(Robot r){
takenOutRobots.add(r);
takenOutTimers.get(r).reset(time);
}
}