forked from RoboCup-SSL/ssl-game-controller
/
placementPos.go
186 lines (174 loc) · 6.72 KB
/
placementPos.go
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
package controller
import (
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
"log"
"math"
)
// BallPlacementPos determines the ball placement position based on the primary game event
func (e *Engine) BallPlacementPos() *Location {
event := e.State.PrimaryGameEvent()
if event == nil {
return nil
}
switch event.Type {
case GameEventBallLeftFieldTouchLine:
if event.Details.BallLeftFieldTouchLine.Location != nil {
location := mapProtoLocation(event.Details.BallLeftFieldTouchLine.Location)
x := e.Geometry.FieldLength/2 - e.Geometry.PlacementOffsetGoalLine
if math.Abs(location.X) > x {
location.X = math.Copysign(x, location.X)
}
y := e.Geometry.FieldWidth/2 - e.Geometry.PlacementOffsetTouchLine
location.Y = math.Copysign(y, location.Y)
return e.validateLocation(location)
}
return nil
case GameEventBallLeftFieldGoalLine:
if event.Details.BallLeftFieldGoalLine.Location != nil {
location := mapProtoLocation(event.Details.BallLeftFieldGoalLine.Location)
return e.ballPlacementLocationGoalLine(event, location)
}
return nil
case GameEventPossibleGoal:
if event.Details.PossibleGoal.Location != nil {
location := mapProtoLocation(event.Details.PossibleGoal.Location)
return e.ballPlacementLocationGoalLine(event, location)
}
return nil
case GameEventAimlessKick:
return e.validateProtoLocation(event.Details.AimlessKick.KickLocation)
case GameEventGoal:
return &Location{X: 0.0, Y: 0.0}
case GameEventIndirectGoal:
return e.validateProtoLocation(event.Details.IndirectGoal.KickLocation)
case GameEventChippedGoal:
return e.validateProtoLocation(event.Details.ChippedGoal.KickLocation)
case GameEventBotTippedOver:
return e.validateProtoLocation(event.Details.BotTippedOver.Location)
case GameEventBotInterferedPlacement:
return e.validateLocation(e.State.PlacementPos)
case GameEventBotKickedBallTooFast:
return e.validateProtoLocation(event.Details.BotKickedBallTooFast.Location)
case GameEventBotDribbledBallTooFar:
return e.validateProtoLocation(event.Details.BotDribbledBallTooFar.Start)
case GameEventBotCrashUnique:
return e.validateProtoLocation(event.Details.BotCrashUnique.Location)
case GameEventBotPushedBot:
return e.validateProtoLocation(event.Details.BotPushedBot.Location)
case GameEventBotHeldBallDeliberately:
return e.validateProtoLocation(event.Details.BotHeldBallDeliberately.Location)
case GameEventAttackerDoubleTouchedBall:
return e.validateProtoLocation(event.Details.AttackerDoubleTouchedBall.Location)
case GameEventAttackerTooCloseToDefenseArea:
return e.validateProtoLocation(event.Details.AttackerTooCloseToDefenseArea.Location)
case GameEventAttackerTouchedBallInDefenseArea:
return e.validateProtoLocation(event.Details.AttackerTouchedBallInDefenseArea.Location)
case GameEventAttackerTouchedOpponentInDefenseArea:
return e.validateProtoLocation(event.Details.AttackerTouchedOpponentInDefenseArea.Location)
case GameEventDefenderTooCloseToKickPoint:
return e.validateLocation(e.State.PlacementPos)
case GameEventDefenderInDefenseAreaPartially:
return e.validateProtoLocation(event.Details.DefenderInDefenseAreaPartially.Location)
case GameEventDefenderInDefenseArea, GameEventMultipleCards:
teamInFavor := event.ByTeam().Opposite()
location := Location{}
location.X = (e.Geometry.FieldLength / 2.0) - e.Geometry.DefenseAreaDepth
if e.State.TeamState[teamInFavor].OnPositiveHalf {
location.X *= -1
}
return &location
case GameEventKeeperHeldBall:
return e.validateProtoLocation(event.Details.KeeperHeldBall.Location)
case GameEventKickTimeout:
return e.validateProtoLocation(event.Details.KickTimeout.Location)
case GameEventNoProgressInGame:
return e.validateProtoLocation(event.Details.NoProgressInGame.Location)
case GameEventPlacementFailed:
return e.validateLocation(e.State.PlacementPos)
case GameEventBotTooFastInStop,
GameEventBotCrashDrawn,
GameEventUnsportingBehaviorMinor,
GameEventUnsportingBehaviorMajor,
GameEventMultipleFouls,
GameEventMultiplePlacementFailures,
GameEventBotCrashUniqueSkipped,
GameEventBotPushedBotSkipped,
GameEventAttackerTouchedOpponentInDefenseAreaSkipped,
GameEventPlacementSucceeded,
GameEventPrepared,
GameEventBotSubstitution,
GameEventTooManyRobots:
return nil
default:
log.Print("Warn: Unknown game event: ", event.Type)
return nil
}
}
func (e *Engine) ballPlacementLocationGoalLine(event *GameEvent, lastBallLocation *Location) *Location {
var x float64
if e.isGoalKick(event) {
x = e.Geometry.FieldLength/2 - e.Geometry.PlacementOffsetGoalLineGoalKick
} else {
x = e.Geometry.FieldLength/2 - e.Geometry.PlacementOffsetGoalLine
}
var placementLocation Location
placementLocation.X = math.Copysign(x, lastBallLocation.X)
y := e.Geometry.FieldWidth/2 - e.Geometry.PlacementOffsetTouchLine
placementLocation.Y = math.Copysign(y, lastBallLocation.Y)
return e.validateLocation(&placementLocation)
}
func (e *Engine) isGoalKick(event *GameEvent) bool {
if event.Details.BallLeftFieldGoalLine == nil {
// failed goal shot -> goal kick
return true
}
teamInFavor := event.ByTeam().Opposite()
location := mapProtoLocation(event.Details.BallLeftFieldGoalLine.Location)
if e.State.TeamState[teamInFavor].OnPositiveHalf && location.X > 0 {
return true
}
if !e.State.TeamState[teamInFavor].OnPositiveHalf && location.X < 0 {
return true
}
return false
}
func mapProtoLocation(location *refproto.Location) *Location {
return &Location{X: float64(*location.X), Y: float64(*location.Y)}
}
func (e *Engine) validateProtoLocation(location *refproto.Location) *Location {
if location == nil {
return nil
}
return e.validateLocation(mapProtoLocation(location))
}
func (e *Engine) validateLocation(location *Location) *Location {
if location == nil {
return nil
}
e.movePositionInsideField(location)
e.movePositionOutOfDefenseArea(location)
return location
}
func (e *Engine) movePositionOutOfDefenseArea(location *Location) {
maxX := e.Geometry.FieldLength/2 - e.Geometry.DefenseAreaDepth - e.Geometry.PlacementOffsetDefenseArea
minY := e.Geometry.DefenseAreaWidth/2 + e.Geometry.PlacementOffsetDefenseArea
if math.Abs(location.X) > maxX && math.Abs(location.Y) < minY {
diffX := math.Abs(maxX - math.Abs(location.X))
diffY := math.Abs(minY - math.Abs(location.Y))
if diffX < diffY {
location.X = math.Copysign(maxX, location.X)
} else {
location.Y = math.Copysign(minY, location.Y)
}
}
}
func (e *Engine) movePositionInsideField(location *Location) {
maxX := e.Geometry.FieldLength/2 - e.Geometry.PlacementOffsetGoalLine
if math.Abs(location.X) > maxX {
location.X = math.Copysign(maxX, location.X)
}
maxY := e.Geometry.FieldWidth/2 - e.Geometry.PlacementOffsetTouchLine
if math.Abs(location.Y) > maxY {
location.Y = math.Copysign(maxY, location.Y)
}
}