@@ -14,12 +14,27 @@ import (
1414type RobotCountMaintainer struct {
1515 c * SimulationController
1616
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 {}
1927}
2028
2129func (r * RobotCountMaintainer ) handleRobotCount () {
2230
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+
2338 if time .Now ().Sub (r .lastTimeSendCommand ) < 500 * time .Millisecond {
2439 // Placed ball just recently
2540 return
@@ -51,12 +66,24 @@ func (r *RobotCountMaintainer) updateRobotCount(robots []*tracker.TrackedRobot,
5166 substCenterNeg := geom .NewVector2Float32 (0 , - * substCenterPos .Y )
5267 substRectPos := geom .NewRectangleFromCenter (substCenterPos , 2 , float64 (* r .c .fieldSize .BoundaryWidth )/ 1000 + 0.2 )
5368 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 {
5579 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+ }
6087 }
6188 }
6289 if len (robots ) < maxRobots {
@@ -70,6 +97,7 @@ func (r *RobotCountMaintainer) updateRobotCount(robots []*tracker.TrackedRobot,
7097 if r .isFreeOfObstacles (pos ) {
7198 id := r .nextFreeRobotId (team )
7299 r .addRobot (id , pos )
100+ r .robotCountMismatchSince [team ] = time.Time {}
73101 break
74102 }
75103 x *= - 1
0 commit comments