@@ -14,12 +14,27 @@ import (
14
14
type RobotCountMaintainer struct {
15
15
c * SimulationController
16
16
17
- lastTimeSendCommand time.Time
18
- haltTime * time.Time
17
+ lastTimeSendCommand time.Time
18
+ robotCountMismatchSince map [referee.Team ]time.Time
19
+ lastUpdate time.Time
20
+ }
21
+
22
+ func (r * RobotCountMaintainer ) init () {
23
+ r .lastTimeSendCommand = time .Now ()
24
+ r .robotCountMismatchSince = map [referee.Team ]time.Time {}
25
+ r .robotCountMismatchSince [referee .Team_BLUE ] = time.Time {}
26
+ r .robotCountMismatchSince [referee .Team_YELLOW ] = time.Time {}
19
27
}
20
28
21
29
func (r * RobotCountMaintainer ) handleRobotCount () {
22
30
31
+ preLastUpdate := r .lastUpdate
32
+ r .lastUpdate = time .Now ()
33
+ if r .lastUpdate .Sub (preLastUpdate ) > time .Second {
34
+ // First update since a second, probably a simulator restart
35
+ r .init ()
36
+ }
37
+
23
38
if time .Now ().Sub (r .lastTimeSendCommand ) < 500 * time .Millisecond {
24
39
// Placed ball just recently
25
40
return
@@ -51,12 +66,24 @@ func (r *RobotCountMaintainer) updateRobotCount(robots []*tracker.TrackedRobot,
51
66
substCenterNeg := geom .NewVector2Float32 (0 , - * substCenterPos .Y )
52
67
substRectPos := geom .NewRectangleFromCenter (substCenterPos , 2 , float64 (* r .c .fieldSize .BoundaryWidth )/ 1000 + 0.2 )
53
68
substRectNeg := geom .NewRectangleFromCenter (substCenterNeg , 2 , float64 (* r .c .fieldSize .BoundaryWidth )/ 1000 + 0.2 )
54
- if len (robots ) > 0 && len (robots ) > maxRobots {
69
+
70
+ if len (robots ) != maxRobots && r .robotCountMismatchSince [team ].IsZero () {
71
+ r .robotCountMismatchSince [team ] = time .Now ()
72
+ }
73
+
74
+ if time .Now ().Sub (r .robotCountMismatchSince [team ]) < 3 * time .Second {
75
+ return
76
+ }
77
+
78
+ if len (robots ) > maxRobots {
55
79
r .sortRobotsByDistanceToSubstitutionPos (robots )
56
- if * r .c .lastRefereeMsg .Command == referee .Referee_HALT ||
57
- substRectPos .IsPointInside (robots [0 ].Pos ) ||
58
- substRectNeg .IsPointInside (robots [0 ].Pos ) {
59
- r .removeRobot (robots [0 ].RobotId )
80
+ for i := 0 ; i < len (robots )- maxRobots ; i ++ {
81
+ if * r .c .lastRefereeMsg .Command == referee .Referee_HALT ||
82
+ substRectPos .IsPointInside (robots [i ].Pos ) ||
83
+ substRectNeg .IsPointInside (robots [i ].Pos ) {
84
+ r .removeRobot (robots [i ].RobotId )
85
+ r .robotCountMismatchSince [team ] = time.Time {}
86
+ }
60
87
}
61
88
}
62
89
if len (robots ) < maxRobots {
@@ -70,6 +97,7 @@ func (r *RobotCountMaintainer) updateRobotCount(robots []*tracker.TrackedRobot,
70
97
if r .isFreeOfObstacles (pos ) {
71
98
id := r .nextFreeRobotId (team )
72
99
r .addRobot (id , pos )
100
+ r .robotCountMismatchSince [team ] = time.Time {}
73
101
break
74
102
}
75
103
x *= - 1
0 commit comments