Skip to content

Commit 3aa21c3

Browse files
committed
Implement ready to continue for prepare and placement
1 parent a2e0228 commit 3aa21c3

File tree

11 files changed

+754
-123
lines changed

11 files changed

+754
-123
lines changed

config/ssl-game-controller.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ game:
3333
ball-placement-required-distance: 1.0
3434
ball-placement-tolerance: 0.15
3535
ball-placement-min-distance-to-defense-area: 0.7
36+
ball-placement-min-robot-distance: 0.05
37+
distance-to-ball-in-stop: 0.5
3638
normal:
3739
half-duration: 5m
3840
half-time-duration: 5m

internal/app/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type Game struct {
5454
BallPlacementRequiredDistance float64 `yaml:"ball-placement-required-distance"`
5555
BallPlacementMinDistanceToDefenseArea float64 `yaml:"ball-placement-min-distance-to-defense-area"`
5656
BallPlacementTolerance float64 `yaml:"ball-placement-tolerance"`
57+
BallPlacementMinRobotDistance float64 `yaml:"ball-placement-min-robot-distance"`
58+
DistanceToBallInStop float64 `yaml:"distance-to-ball-in-stop"`
5759
}
5860

5961
// Network holds configs for network communication
@@ -160,6 +162,8 @@ func DefaultControllerConfig() (c Controller) {
160162
c.Game.BallPlacementRequiredDistance = 1.0
161163
c.Game.BallPlacementMinDistanceToDefenseArea = 0.7
162164
c.Game.BallPlacementTolerance = 0.15
165+
c.Game.BallPlacementMinRobotDistance = 0.05
166+
c.Game.DistanceToBallInStop = 0.5
163167

164168
c.Game.Normal.HalfDuration = 5 * time.Minute
165169
c.Game.Normal.HalfTimeDuration = 5 * time.Minute

internal/app/config/testdata/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ game:
3333
ball-placement-required-distance: 1.0
3434
ball-placement-tolerance: 0.15
3535
ball-placement-min-distance-to-defense-area: 0.7
36+
ball-placement-min-robot-distance: 0.05
37+
distance-to-ball-in-stop: 0.5
3638
normal:
3739
half-duration: 5m
3840
half-time-duration: 5m

internal/app/engine/common.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
const ballSteadyThreshold = 0.2
11+
const robotRadius = 0.09
12+
const distanceThreshold = 0.03
1113

1214
func createGameEventChange(eventType state.GameEvent_Type, event state.GameEvent) *statemachine.Change {
1315
event.Type = &eventType
@@ -22,14 +24,14 @@ func createGameEventChange(eventType state.GameEvent_Type, event state.GameEvent
2224
}
2325
}
2426

25-
func (e *Engine) readyToContinue() bool {
26-
// robot to ball distance
27-
// forTeam: > 0.05m
28-
// opponent: > 0.5m
29-
30-
// ball is stationary
27+
func (e *Engine) robotsInsideRadius(robots []*Robot, pos *geom.Vector2, radius float64) bool {
28+
for _, robot := range robots {
29+
distance := robot.Pos.DistanceTo(pos)
30+
if distance < radius {
31+
return true
32+
}
33+
}
3134

32-
// TODO
3335
return false
3436
}
3537

internal/app/engine/process_ballplacement.go

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,35 @@ func (c *BallPlacementCoordinator) process() {
2020
}
2121

2222
if c.ballPlacementStartPos == nil || c.ballPlacementStartTime == nil {
23-
c.ballPlacementStartPos = e.gcState.TrackerStateGc.BallPos
23+
c.ballPlacementStartPos = e.gcState.TrackerStateGc.Ball.Pos
2424
c.ballPlacementStartTime = new(time.Time)
2525
*c.ballPlacementStartTime = e.timeProvider()
2626
}
2727

2828
remainingDistance := c.remainingPlacementDistance()
29-
if e.readyToContinue() &&
29+
30+
if e.ballSteady() && e.teamInFavorKeepsSufficientDistance() &&
3031
float64(remainingDistance) <= e.gameConfig.BallPlacementTolerance {
31-
duration := float32(e.timeProvider().Sub(*c.ballPlacementStartTime).Seconds())
32-
var distance *float32
33-
if c.ballPlacementStartPos != nil && e.gcState.TrackerStateGc.BallPos != nil {
34-
distance = new(float32)
35-
*distance = float32(c.ballPlacementStartPos.DistanceTo(e.gcState.TrackerStateGc.BallPos))
36-
}
37-
e.Enqueue(createGameEventChange(state.GameEvent_PLACEMENT_SUCCEEDED, state.GameEvent{
38-
Event: &state.GameEvent_PlacementSucceeded_{
39-
PlacementSucceeded: &state.GameEvent_PlacementSucceeded{
40-
ByTeam: e.currentState.Command.ForTeam,
41-
TimeTaken: &duration,
42-
Precision: &remainingDistance,
43-
Distance: distance,
32+
// if opponent does not yet keep sufficient distance, that's not the placing teams fold,
33+
// so just wait a bit more
34+
if e.opponentTeamKeepsSufficientDistance() {
35+
duration := float32(e.timeProvider().Sub(*c.ballPlacementStartTime).Seconds())
36+
var distance *float32
37+
if c.ballPlacementStartPos != nil && e.gcState.TrackerStateGc.Ball.Pos != nil {
38+
distance = new(float32)
39+
*distance = float32(c.ballPlacementStartPos.DistanceTo(e.gcState.TrackerStateGc.Ball.Pos))
40+
}
41+
e.Enqueue(createGameEventChange(state.GameEvent_PLACEMENT_SUCCEEDED, state.GameEvent{
42+
Event: &state.GameEvent_PlacementSucceeded_{
43+
PlacementSucceeded: &state.GameEvent_PlacementSucceeded{
44+
ByTeam: e.currentState.Command.ForTeam,
45+
TimeTaken: &duration,
46+
Precision: &remainingDistance,
47+
Distance: distance,
48+
},
4449
},
45-
},
46-
}))
50+
}))
51+
}
4752
} else if goDur(e.currentState.CurrentActionTimeRemaining) <= 0 {
4853
e.Enqueue(createGameEventChange(state.GameEvent_PLACEMENT_FAILED, state.GameEvent{
4954
Event: &state.GameEvent_PlacementFailed_{
@@ -57,10 +62,39 @@ func (c *BallPlacementCoordinator) process() {
5762
}
5863

5964
func (c *BallPlacementCoordinator) remainingPlacementDistance() float32 {
60-
if c.gcEngine.currentState.PlacementPos == nil || c.gcEngine.gcState.TrackerStateGc.BallPos == nil {
65+
if c.gcEngine.currentState.PlacementPos == nil || c.gcEngine.gcState.TrackerStateGc.Ball.Pos == nil {
6166
return -1
6267
}
6368
placementPos := locationToVector2(c.gcEngine.currentState.PlacementPos)
64-
ballPos := c.gcEngine.gcState.TrackerStateGc.BallPos
69+
ballPos := c.gcEngine.gcState.TrackerStateGc.Ball.Pos
6570
return float32(placementPos.DistanceTo(ballPos))
6671
}
72+
73+
func (e *Engine) teamInFavorKeepsSufficientDistance() bool {
74+
radius := e.gameConfig.BallPlacementMinRobotDistance + robotRadius + distanceThreshold
75+
if e.currentState.NextCommand != nil && e.currentState.NextCommand.ForTeam == nil {
76+
radius = e.gameConfig.DistanceToBallInStop + robotRadius + distanceThreshold
77+
}
78+
79+
var robots []*Robot
80+
for _, robot := range e.gcState.TrackerStateGc.Robots {
81+
if *robot.Id.Team == *e.currentState.Command.ForTeam {
82+
robots = append(robots, robot)
83+
}
84+
}
85+
86+
return !e.robotsInsideRadius(robots, e.gcState.TrackerStateGc.Ball.Pos, radius)
87+
}
88+
89+
func (e *Engine) opponentTeamKeepsSufficientDistance() bool {
90+
radius := e.gameConfig.DistanceToBallInStop + robotRadius + distanceThreshold
91+
92+
var robots []*Robot
93+
for _, robot := range e.gcState.TrackerStateGc.Robots {
94+
if *robot.Id.Team != *e.currentState.Command.ForTeam {
95+
robots = append(robots, robot)
96+
}
97+
}
98+
99+
return !e.robotsInsideRadius(robots, e.gcState.TrackerStateGc.Ball.Pos, radius)
100+
}

internal/app/engine/process_noprogress.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func (d *NoProgressDetector) process() {
2424
}
2525

2626
if d.lastBallPos == nil {
27-
d.lastBallPos = d.gcEngine.gcState.TrackerStateGc.BallPos
27+
d.lastBallPos = d.gcEngine.gcState.TrackerStateGc.Ball.Pos
2828
return
2929
}
3030

31-
if d.lastBallPos.DistanceTo(d.gcEngine.gcState.TrackerStateGc.BallPos) > distanceTolerance {
31+
if d.lastBallPos.DistanceTo(d.gcEngine.gcState.TrackerStateGc.Ball.Pos) > distanceTolerance {
3232
d.lastTime = nil
33-
d.lastBallPos = d.gcEngine.gcState.TrackerStateGc.BallPos
33+
d.lastBallPos = d.gcEngine.gcState.TrackerStateGc.Ball.Pos
3434
return
3535
}
3636

@@ -43,10 +43,10 @@ func (d *NoProgressDetector) process() {
4343
timeSinceLastProgress := d.gcEngine.timeProvider().Sub(*d.lastTime)
4444
if timeSinceLastProgress > d.gcEngine.gameConfig.NoProgressTimeout[d.gcEngine.currentState.Division.Div()] {
4545
duration := float32(timeSinceLastProgress.Seconds())
46-
location := vector2ToLocation(d.gcEngine.gcState.TrackerStateGc.BallPos)
46+
location := vector2ToLocation(d.gcEngine.gcState.TrackerStateGc.Ball.Pos)
4747
for _, team := range state.BothTeams() {
4848
defenseArea := geom.NewDefenseArea(d.gcEngine.GetGeometry(), *d.gcEngine.currentState.TeamState[team.String()].OnPositiveHalf)
49-
if defenseArea.IsPointInside(d.gcEngine.gcState.TrackerStateGc.BallPos) {
49+
if defenseArea.IsPointInside(d.gcEngine.gcState.TrackerStateGc.Ball.Pos) {
5050
d.gcEngine.Enqueue(createGameEventChange(state.GameEvent_KEEPER_HELD_BALL, state.GameEvent{
5151
Event: &state.GameEvent_KeeperHeldBall_{
5252
KeeperHeldBall: &state.GameEvent_KeeperHeldBall{

internal/app/engine/process_prepare.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ func (e *Engine) processPrepare() {
3232
},
3333
})
3434
}
35+
36+
func (e *Engine) readyToContinue() bool {
37+
radius := e.gameConfig.DistanceToBallInStop + robotRadius + distanceThreshold
38+
if !e.ballSteady() ||
39+
e.robotsInsideRadius(e.gcState.TrackerStateGc.Robots, e.gcState.TrackerStateGc.Ball.Pos, radius) {
40+
return false
41+
}
42+
return false
43+
}

internal/app/engine/process_runningtostop.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func (e *Engine) processRunningToStop() {
3131
}
3232

3333
func (e *Engine) ballPlacementRequired() bool {
34-
if e.currentState.PlacementPos == nil || e.gcState.TrackerStateGc.BallPos == nil {
34+
if e.currentState.PlacementPos == nil || e.gcState.TrackerStateGc.Ball.Pos == nil {
3535
// fallback if the fields are not set
3636
return false
3737
}
3838
placementPos := locationToVector2(e.currentState.PlacementPos)
39-
ballPos := e.gcState.TrackerStateGc.BallPos
39+
ballPos := e.gcState.TrackerStateGc.Ball.Pos
4040

4141
// The ball is closer than 1m to the designated position.
4242
if ballPos.DistanceTo(placementPos) > e.gameConfig.BallPlacementRequiredDistance {
@@ -67,8 +67,8 @@ func (e *Engine) ballPlacementRequired() bool {
6767
}
6868

6969
func (e *Engine) ballSteady() bool {
70-
if e.gcState.TrackerStateGc.BallVel == nil {
70+
if e.gcState.TrackerStateGc.Ball.Vel == nil {
7171
return true
7272
}
73-
return e.gcState.TrackerStateGc.BallVel.Length() < ballSteadyThreshold
73+
return e.gcState.TrackerStateGc.Ball.Vel.Length() < ballSteadyThreshold
7474
}

0 commit comments

Comments
 (0)