-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
193 lines (150 loc) · 6.08 KB
/
bot.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
187
188
189
190
191
192
193
package bot
import (
"context"
"math"
"github.com/lugobots/lugo4go/v3"
"github.com/lugobots/lugo4go/v3/field"
"github.com/lugobots/lugo4go/v3/proto"
"github.com/lugobots/lugo4go/v3/specs"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func NewBot(FieldMapper field.Mapper, Config lugo4go.Config, Logger *zap.SugaredLogger) *Bot {
return &Bot{
FieldMapper: FieldMapper,
Config: Config,
Logger: Logger,
}
}
type Bot struct {
FieldMapper field.Mapper
Config lugo4go.Config
Logger *zap.SugaredLogger
}
func (b *Bot) OnDisputing(_ context.Context, inspector lugo4go.SnapshotInspector) ([]proto.PlayerOrder, string, error) {
me := inspector.GetMe()
ballPosition := inspector.GetBall().GetPosition()
ballRegion, err := b.FieldMapper.GetPointRegion(ballPosition)
if err != nil {
return nil, "", errors.Wrap(err, "failed to find the ball region")
}
myRegion, err := b.FieldMapper.GetPointRegion(me.GetPosition())
if err != nil {
return nil, "", errors.Wrap(err, "failed to find my region")
}
// if the ball is max 2 blocks away from me, I will move toward the ball
if !isNear(myRegion, ballRegion) {
return b.holdPosition(inspector)
}
moveOrder, err := inspector.MakeOrderMoveMaxSpeed(*ballPosition)
if err != nil {
return nil, "", errors.Wrap(err, "error creating move order")
}
// we can ALWAYS try to catch the ball if we are not holding it
catchOrder := inspector.MakeOrderCatch()
return []proto.PlayerOrder{moveOrder, catchOrder}, "trying to catch the ball", nil
}
func (b *Bot) OnDefending(_ context.Context, inspector lugo4go.SnapshotInspector) ([]proto.PlayerOrder, string, error) {
me := inspector.GetMe()
ballPosition := inspector.GetBall().GetPosition()
ballRegion, err := b.FieldMapper.GetPointRegion(ballPosition)
if err != nil {
return nil, "", errors.Wrapf(err, "failed to find the ball region")
}
myRegion, err := b.FieldMapper.GetPointRegion(me.GetPosition())
if err != nil {
return nil, "", errors.Wrapf(err, "failed to find my region")
}
// if the ball is max 2 blocks away from me, I will move toward the ball
if !isNear(myRegion, ballRegion) {
return b.holdPosition(inspector)
}
moveOrder, err := inspector.MakeOrderMoveMaxSpeed(*ballPosition)
if err != nil {
return nil, "", errors.Wrapf(err, "error creating move order")
}
// we can ALWAYS try to catch the ball if we are not holding it
catchOrder := inspector.MakeOrderCatch()
return []proto.PlayerOrder{moveOrder, catchOrder}, "trying to catch the ball", nil
}
func (b *Bot) OnHolding(_ context.Context, inspector lugo4go.SnapshotInspector) ([]proto.PlayerOrder, string, error) {
me := inspector.GetMe()
goal := b.FieldMapper.GetAttackGoal()
goalRegion, err := b.FieldMapper.GetPointRegion(&goal.Center)
if err != nil {
return nil, "", errors.Wrapf(err, "failed to find my the goal redion")
}
myRegion, err := b.FieldMapper.GetPointRegion(me.GetPosition())
if err != nil {
return nil, "", errors.Wrap(err, "failed to find my region")
}
// if we are near to the goal, let's kick it!
if !isNear(myRegion, goalRegion) {
return []proto.PlayerOrder{inspector.MakeOrderMoveByDirection(field.Forward, specs.PlayerMaxSpeed)}, "trying to catch the ball", nil
}
kickOrder, err := inspector.MakeOrderKickMaxSpeed(goal.Center)
if err != nil {
return nil, "", errors.Wrap(err, "failed to create kick order")
}
return []proto.PlayerOrder{kickOrder}, "trying to catch the ball", nil
}
func (b *Bot) OnSupporting(_ context.Context, inspector lugo4go.SnapshotInspector) ([]proto.PlayerOrder, string, error) {
me := inspector.GetMe()
teammatePosition := inspector.GetBall().GetHolder().GetPosition()
teammateRegion, err := b.FieldMapper.GetPointRegion(teammatePosition)
if err != nil {
return nil, "", errors.Wrap(err, "failed to find the teammate region")
}
myRegion, err := b.FieldMapper.GetPointRegion(me.GetPosition())
if err != nil {
return nil, "", errors.Wrap(err, "failed to find my region")
}
// if the player mate is max 2 blocks away from me, I will help
if !isNear(myRegion, teammateRegion) {
return b.holdPosition(inspector)
}
if teammatePosition.DistanceTo(*me.Position) < specs.PlayerSize {
// make order to stop
return []proto.PlayerOrder{inspector.MakeOrderMoveToStop()}, "supporting teammate", nil
}
moveOrder, err := inspector.MakeOrderMoveMaxSpeed(*teammatePosition)
if err != nil {
return nil, "", errors.Wrap(err, "error creating move order to teammate position")
}
return []proto.PlayerOrder{moveOrder}, "supporting teammate", nil
}
func (b *Bot) AsGoalkeeper(_ context.Context, inspector lugo4go.SnapshotInspector, myState lugo4go.PlayerState) ([]proto.PlayerOrder, string, error) {
if myState == lugo4go.HoldingTheBall {
kickOrder, err := inspector.MakeOrderKickMaxSpeed(field.FieldCenter())
if err != nil {
return nil, "", errors.Wrap(err, "failed to create kick order")
}
return []proto.PlayerOrder{kickOrder}, "kick the ball", nil
}
ballPosition := inspector.GetBall().GetPosition()
moveOrder, err := inspector.MakeOrderMoveMaxSpeed(*ballPosition)
if err != nil {
return nil, "", errors.Wrap(err, "error creating move order")
}
return []proto.PlayerOrder{moveOrder, inspector.MakeOrderCatch()}, "trying to catch the ball", nil
}
func (b *Bot) OnGetReady(_ context.Context, _ lugo4go.SnapshotInspector) {
b.Logger.Debug("game ready to start OR score has been changed")
}
func (b *Bot) holdPosition(inspector lugo4go.SnapshotInspector) ([]proto.PlayerOrder, string, error) {
expectedRegion := GetPlayerTacticRegion(inspector, b.FieldMapper, b.Config.Number)
if inspector.GetMe().Position.DistanceTo(*expectedRegion.Center()) < specs.PlayerSize {
return []proto.PlayerOrder{inspector.MakeOrderMoveToStop()}, "Holding position", nil
}
moveOrder, err := inspector.MakeOrderMoveMaxSpeed(*expectedRegion.Center())
if err != nil {
return nil, "", errors.Wrap(err, "error creating move order")
}
return []proto.PlayerOrder{moveOrder}, "moving to my region", nil
}
func isNear(a, b field.Region) bool {
const minDist = 2
colDist := a.Col() - b.Col()
rowDist := a.Row() - b.Row()
return math.Hypot(float64(colDist), float64(rowDist)) <= minDist
}