-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstates.go
204 lines (159 loc) · 5.76 KB
/
states.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
194
195
196
197
198
199
200
201
202
203
204
package main
import (
RLBot "github.com/Trey2k/RLBotGo"
vector "github.com/xonmello/BotKoba/vector3"
// rotator "github.com/xonmello/BotKoba/rotator"
math "github.com/chewxy/math32"
)
type State int
const (
ATBA State = iota
Kickoff
DefensiveCorner
Air
OffensiveCorner
OnWall
)
type StateInfo struct {
current State
start int64
allotted int64
}
// (Mostly) Always Toward Ball Agent
func stateATBA(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo, ballPrediction *RLBot.BallPrediction) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, _, opponentPos, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// If in air, point wheels down
if !koba.HasWheelContact {
PlayerInput.Roll = -1.0 * kobaRot.Roll / math.Pi
}
// Go toward ball
PlayerInput.Steer = steerToward(kobaPos, kobaRot, ballPos)
// Drift if close to the ball and not very aligned
if kobaPos.Distance(ballPos) < 500 && math.Abs(PlayerInput.Steer) > 0.3 {
PlayerInput.Handbrake = true
}
// If on the wrong side of the ball, go back
goal := vector.New(0, 3000, 0)
if correctSide(kobaPos, ballPos, goal) {
predictionPosition := ballPrediction.Slices[4].Physics.Location
predictionPositionVector := vector.New(predictionPosition.X, predictionPosition.Y, predictionPosition.Z)
if predictionPositionVector.Magnitude() == 0 {
predictionPositionVector = ballPos
}
PlayerInput.Steer = steerToward(kobaPos, kobaRot, predictionPositionVector)
PlayerInput.Boost = true
} else if opponentPos.Distance(ballPos) > kobaPos.Distance(ballPos) {
PlayerInput.Boost = true
}
// Flip if close to the ball
if kobaPos.Distance(ballPos) < 400 && kobaPos.Y < ballPos.Y {
PlayerInput.Steer = steerToward(kobaPos, kobaRot, ballPos)
PlayerInput = flipToward(kobaPos, koba.Jumped, kobaRot, ballPos, PlayerInput)
}
// Go forward
PlayerInput.Throttle = 1.0
return PlayerInput
}
// Kickoff with 2 flips
func stateKickoff(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, kobaVel, _, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// Go toward ball
PlayerInput.Steer = steerToward(kobaPos, kobaRot, ballPos)
// Starting flip to gain speed
if kobaVel.Magnitude() > 1300 {
PlayerInput = flipToward(kobaPos, koba.Jumped, kobaRot, ballPos, PlayerInput)
}
// Flip when close to the ball
if kobaPos.Distance(ballPos) < 500 && kobaPos.Y < ballPos.Y {
PlayerInput = flipToward(kobaPos, koba.Jumped, kobaRot, ballPos, PlayerInput)
}
// Go forward and boost
if koba.HasWheelContact {
PlayerInput.Throttle = 1.0
PlayerInput.Boost = true
}
return PlayerInput
}
// Getting on the correct side of the ball when in the corner
func stateDefensiveCorner(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, _, _, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// If in air, point wheels down
if !koba.HasWheelContact {
PlayerInput.Roll = -1.0 * kobaRot.Roll / math.Pi
}
// Check if on the correct side of the ball
if math.Abs(kobaPos.X) < math.Abs(ballPos.X) && kobaPos.Y < ballPos.Y+500 {
PlayerInput.Steer = steerToward(kobaPos, kobaRot, ballPos)
// Flip when close to the ball
if kobaPos.Distance(ballPos) < 500 && kobaPos.Y < ballPos.Y {
PlayerInput = flipToward(kobaPos, koba.Jumped, kobaRot, ballPos, PlayerInput)
}
} else {
PlayerInput.Steer = steerToward(kobaPos, kobaRot, vector.New(0, -4500, 0))
}
// Drift if not very aligned
if math.Abs(PlayerInput.Steer) > 0.5 {
PlayerInput.Handbrake = true
}
// Go forward
PlayerInput.Throttle = 1.0
return PlayerInput
}
// General state for when the ball is in the air
func stateAir(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo, ballPrediction *RLBot.BallPrediction) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, _, _, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// Target is in front of where the ball will land
target := lowestPrediction(ballPrediction)
target.Y = ballPos.Y - 500
// Go toward ball
PlayerInput.Steer = steerToward(kobaPos, kobaRot, target)
// Drift if not very aligned
if math.Abs(PlayerInput.Steer) > 0.5 {
PlayerInput.Handbrake = true
}
// Go forward
PlayerInput.Throttle = 1.0
return PlayerInput
}
// Getting in position to hit it if it goes center
// TODO Needs work
func stateOffensiveCorner(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, _, _, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// Go to the side of the field the ball is in
// Wait for the ball to go in front of goal or out of corner
target := vector.New(0, 200, 0)
if ballPos.X > 0 {
target.X = 2000
} else {
target.X = -2000
}
PlayerInput.Steer = steerToward(kobaPos, kobaRot, target)
// Slow down if close to target location
PlayerInput.Throttle = 1
if kobaPos.Distance(target) < 1250 {
PlayerInput.Throttle = 0.2
}
// Drift if not very aligned
if math.Abs(PlayerInput.Steer) > 0.5 {
PlayerInput.Handbrake = true
}
return PlayerInput
}
// Jump off wall when on it
func stateOnWall(koba *RLBot.PlayerInfo, opponent *RLBot.PlayerInfo, ball *RLBot.BallInfo) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
kobaPos, kobaRot, _, _, _, _, ballPos, _ := initialSetup(koba, opponent, ball)
// Jump off wall
PlayerInput.Jump = true
// If in air, point wheels down and turn toward ball
if !koba.HasWheelContact {
PlayerInput.Roll = -1.0 * kobaRot.Roll / math.Pi
PlayerInput.Yaw = steerToward(kobaPos, kobaRot, ballPos)
}
return PlayerInput
}