-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_inspector.go
186 lines (153 loc) · 5.53 KB
/
snapshot_inspector.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 lugo4go
import (
"fmt"
"github.com/lugobots/lugo4go/v3/helpers"
"github.com/lugobots/lugo4go/v3/field"
"github.com/lugobots/lugo4go/v3/proto"
"github.com/lugobots/lugo4go/v3/specs"
)
type inspector struct {
mySide proto.Team_Side
myNumber int
me *proto.Player
snapshot *proto.GameSnapshot
}
func NewGameSnapshotInspector(botSide proto.Team_Side, playerNumber int, gameSnapshot *proto.GameSnapshot) (SnapshotInspector, error) {
tools := &inspector{mySide: botSide, myNumber: playerNumber, snapshot: gameSnapshot}
me := tools.GetPlayer(botSide, playerNumber)
if me == nil {
return nil, fmt.Errorf("could not to find the player %s-%d", botSide, playerNumber)
}
tools.me = me
return tools, nil
}
func (i *inspector) GetSnapshot() *proto.GameSnapshot {
return i.snapshot
}
func (i *inspector) GetMe() *proto.Player {
return i.me
}
func (i *inspector) GetBall() *proto.Ball {
return i.snapshot.GetBall()
}
func (i *inspector) GetBallHolder() (*proto.Player, bool) {
return helpers.GetBallHolder(i.snapshot)
}
func (i *inspector) IsBallHolder(player *proto.Player) bool {
return helpers.IsBallHolder(i.snapshot, player)
}
func (i *inspector) GetTeam(side proto.Team_Side) *proto.Team {
return helpers.GetTeam(i.snapshot, side)
}
func (i *inspector) GetMyTeam() *proto.Team {
return i.GetTeam(i.mySide)
}
func (i *inspector) GetOpponentTeam() *proto.Team {
return i.GetTeam(i.GetOpponentSide())
}
func (i *inspector) GetMyTeamSide() proto.Team_Side {
return i.mySide
}
func (i *inspector) GetOpponentSide() proto.Team_Side {
return helpers.GetOpponentSide(i.mySide)
}
func (i *inspector) GetPlayer(side proto.Team_Side, number int) *proto.Player {
return helpers.GetPlayer(i.snapshot, side, number)
}
func (i *inspector) GetMyTeamPlayers() []*proto.Player {
return i.GetTeam(i.mySide).Players
}
func (i *inspector) GetOpponentPlayers() []*proto.Player {
return i.GetTeam(i.GetOpponentSide()).Players
}
func (i *inspector) GetMyTeamGoalkeeper() *proto.Player {
return i.GetPlayer(i.GetMyTeamSide(), int(specs.GoalkeeperNumber))
}
func (i *inspector) GetOpponentGoalkeeper() *proto.Player {
return i.GetPlayer(i.GetOpponentSide(), int(specs.GoalkeeperNumber))
}
func (i *inspector) MakeOrderMove(target proto.Point, speed float64) (*proto.Order_Move, error) {
return i.MakeOrderMoveFromPoint(*i.me.Position, target, speed)
}
func (i *inspector) MakeOrderMoveMaxSpeed(target proto.Point) (*proto.Order_Move, error) {
return i.MakeOrderMoveFromPoint(*i.me.Position, target, specs.PlayerMaxSpeed)
}
func (i *inspector) MakeOrderMoveFromPoint(origin, target proto.Point, speed float64) (*proto.Order_Move, error) {
vec, err := proto.NewVector(origin, target)
if err != nil {
return nil, err
}
vel := proto.NewZeroedVelocity(*vec.Normalize())
vel.Speed = speed
return &proto.Order_Move{Move: &proto.Move{Velocity: &vel}}, nil
}
func (i *inspector) MakeOrderMoveFromVector(vector proto.Vector, speed float64) *proto.Order_Move {
targetPoint := vector.TargetFrom(*i.me.Position)
// no need to check for errors since a vector will always lead to a valid destination
order, _ := i.MakeOrderMoveFromPoint(*i.me.Position, targetPoint, speed)
return order
}
func (i *inspector) MakeOrderMoveByDirection(direction field.Direction, speed float64) *proto.Order_Move {
directionTarget := directionOrientationMap[i.mySide][direction]
// no need to check for errors since the vector is known and valid
return i.MakeOrderMoveFromVector(proto.Vector(directionTarget), speed)
}
func (i *inspector) MakeOrderMoveToStop() *proto.Order_Move {
myDirection := i.GetMe().GetVelocity().GetDirection()
if myDirection == nil {
v := proto.Vector(directionOrientationMap[i.mySide][field.Forward])
myDirection = &v
}
return i.MakeOrderMoveFromVector(*myDirection, 0)
}
func (i *inspector) MakeOrderJump(target proto.Point, speed float64) (*proto.Order_Jump, error) {
vec, err := proto.NewVector(*i.me.Position, target)
if err != nil {
return nil, err
}
vel := proto.NewZeroedVelocity(*vec.Normalize())
vel.Speed = speed
return &proto.Order_Jump{Jump: &proto.Jump{Velocity: &vel}}, nil
}
func (i *inspector) MakeOrderKick(target proto.Point, speed float64) (*proto.Order_Kick, error) {
ballExpectedDirection, err := proto.NewVector(*i.snapshot.GetBall().Position, target)
if err != nil {
return nil, err
}
diffVector, err := ballExpectedDirection.Sub(i.snapshot.GetBall().Velocity.Direction)
if err != nil {
return nil, err
}
vel := proto.NewZeroedVelocity(*diffVector)
vel.Direction.Normalize()
vel.Speed = speed
return &proto.Order_Kick{Kick: &proto.Kick{Velocity: &vel}}, nil
}
func (i *inspector) MakeOrderKickMaxSpeed(target proto.Point) (*proto.Order_Kick, error) {
return i.MakeOrderKick(target, specs.BallMaxSpeed)
}
func (i *inspector) MakeOrderCatch() *proto.Order_Catch {
return &proto.Order_Catch{Catch: &proto.Catch{}}
}
var directionOrientationMap = map[proto.Team_Side]map[field.Direction]field.Orientation{
proto.Team_HOME: {
field.Forward: field.East,
field.Backward: field.West,
field.Left: field.North,
field.Right: field.South,
field.BackwardLeft: field.NorthWest,
field.BackwardRight: field.SouthWest,
field.ForwardLeft: field.NorthEast,
field.ForwardRight: field.SouthEast,
},
proto.Team_AWAY: {
field.Forward: field.West,
field.Backward: field.East,
field.Left: field.South,
field.Right: field.North,
field.BackwardLeft: field.SouthEast,
field.BackwardRight: field.NorthEast,
field.ForwardLeft: field.SouthWest,
field.ForwardRight: field.NorthWest,
},
}