Skip to content

Commit 158c94f

Browse files
committed
Fix: Continue immediately after successful ball placement
1 parent d9da522 commit 158c94f

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

internal/app/engine/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func createStageChange(stage *state.Referee_Stage) *statemachine.Change {
9595
}
9696
}
9797

98-
func (e *Engine) findRobotInsideRadius(robots []*Robot, pos *geom.Vector2, radius float64) *Robot {
98+
func (e *Engine) findRobotInsideRadius(robots []*Robot, pos *geom.Vector2, radius float64) (matchingRobots []*Robot) {
9999
for _, robot := range robots {
100100
distance := robot.Pos.DistanceTo(pos)
101101
if distance < radius {
102-
return robot
102+
matchingRobots = append(matchingRobots, robot)
103103
}
104104
}
105105

106-
return nil
106+
return
107107
}
108108

109109
func goDur(duration *durationpb.Duration) time.Duration {

internal/app/engine/process_continue_next_command.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"fmt"
55
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/geom"
66
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
7+
"github.com/RoboCup-SSL/ssl-game-controller/pkg/timer"
78
"google.golang.org/protobuf/types/known/timestamppb"
9+
"strings"
10+
"time"
811
)
912

1013
const distanceToBallDuringPenalty = 1.0
@@ -28,7 +31,11 @@ func (e *Engine) createNextCommandContinueAction(
2831
readyAt = nil
2932
} else {
3033
if lastReadyAt == nil {
31-
readyAt = timestamppb.New(e.timeProvider().Add(e.gameConfig.PreparationTimeBeforeResume))
34+
if e.placementSucceededRecently() {
35+
readyAt = timestamppb.New(e.timeProvider())
36+
} else {
37+
readyAt = timestamppb.New(e.timeProvider().Add(e.gameConfig.PreparationTimeBeforeResume))
38+
}
3239
} else {
3340
readyAt = lastReadyAt
3441
}
@@ -53,6 +60,20 @@ func (e *Engine) createNextCommandContinueAction(
5360
}
5461
}
5562

63+
func (e *Engine) placementSucceededRecently() bool {
64+
events := e.currentState.FindGameEvents(state.GameEvent_PLACEMENT_SUCCEEDED)
65+
for _, event := range events {
66+
if event.CreatedTimestamp == nil {
67+
continue
68+
}
69+
placementSucceededTime := timer.TimestampMicroToTime(event.GetCreatedTimestamp())
70+
if e.timeProvider().Sub(placementSucceededTime) < time.Second {
71+
return true
72+
}
73+
}
74+
return false
75+
}
76+
5677
func (e *Engine) LastContinueAction(actionType ContinueAction_Type) *ContinueAction {
5778
for _, action := range e.gcState.ContinueActions {
5879
if *action.Type == actionType {
@@ -175,9 +196,23 @@ func (e *Engine) readyToContinueFromStop() (issues []string) {
175196
issues = append(issues, "Blue team has too many robots")
176197
}
177198
radius := e.gameConfig.DistanceToBallInStop + robotRadius - distanceThreshold
178-
robotNearBall := e.findRobotInsideRadius(e.trackerStateGc.Robots, e.trackerStateGc.Ball.Pos.ToVector2(), radius)
179-
if robotNearBall != nil {
180-
issues = append(issues, fmt.Sprintf("Robot %s is too close to ball", robotNearBall.Id.PrettyString()))
199+
200+
var robots []*Robot
201+
if e.currentState.NextCommand != nil && *e.currentState.NextCommand.Type == state.Command_DIRECT {
202+
// Only check for opponent teams robots
203+
// This is not strictly correct, but necessary to continue directly after a ball placement, without
204+
// waiting for robots to move out of the stop radius.
205+
robots = robotsOfTeam(e.trackerStateGc.Robots, e.currentState.NextCommand.ForTeam.Opposite())
206+
} else {
207+
robots = e.trackerStateGc.Robots
208+
}
209+
robotsNearBall := e.findRobotInsideRadius(robots, e.trackerStateGc.Ball.Pos.ToVector2(), radius)
210+
if len(robotsNearBall) > 0 {
211+
var robotsStr []string
212+
for _, robot := range robotsNearBall {
213+
robotsStr = append(robotsStr, robot.Id.PrettyString())
214+
}
215+
issues = append(issues, fmt.Sprintf("Robots too close to ball: %s", strings.Join(robotsStr, ", ")))
181216
}
182217
if e.currentState.PlacementPos != nil {
183218
ballToPlacementPosDist := e.currentState.PlacementPos.DistanceTo(e.trackerStateGc.Ball.Pos.ToVector2())
@@ -192,6 +227,15 @@ func (e *Engine) readyToContinueFromStop() (issues []string) {
192227
return
193228
}
194229

230+
func robotsOfTeam(robots []*Robot, team state.Team) (teamRobots []*Robot) {
231+
for _, robot := range robots {
232+
if *robot.Id.Team == team {
233+
teamRobots = append(teamRobots, robot)
234+
}
235+
}
236+
return
237+
}
238+
195239
func (e *Engine) penaltyKeeperId() *state.RobotId {
196240
forTeam := e.currentState.Command.ForTeam.Opposite()
197241
teamInfo := e.currentState.TeamState[forTeam.String()]

pkg/timer/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ func TimestampToTime(timestamp float64) time.Time {
1515
nSec := int64((timestamp - float64(sec)) * 1e9)
1616
return time.Unix(sec, nSec)
1717
}
18+
19+
func TimestampMicroToTime(timestamp uint64) time.Time {
20+
sec := int64(timestamp / 1000)
21+
nSec := int64((timestamp - uint64(sec)) * 1000)
22+
return time.Unix(sec, nSec)
23+
}

0 commit comments

Comments
 (0)