-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (62 loc) · 1.93 KB
/
main.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
package main
import (
"fmt"
math "github.com/chewxy/math32"
RLBot "github.com/Trey2k/RLBotGo"
)
var lastTouch float32
var totalTouches int = 0
// getInput takes in a GameState which contains the gameTickPacket, ballPredidctions, fieldInfo and matchSettings
// it also takes in the RLBot object. And returns a PlayerInput
func getInput(gameState *RLBot.GameState, rlBot *RLBot.RLBot) *RLBot.ControllerState {
PlayerInput := &RLBot.ControllerState{}
// Get Location and Rotation information for easier use later
selfLocation := gameState.GameTick.Players[rlBot.PlayerIndex].Physics.Location
selfRotation := gameState.GameTick.Players[rlBot.PlayerIndex].Physics.Rotation
ballLocation := gameState.GameTick.Ball.Physics.Location
// If in air, point wheels down
if !gameState.GameTick.Players[rlBot.PlayerIndex].HasWheelContact {
PlayerInput.Roll = -1.0*selfRotation.Roll/math.Pi
}
// Find angle to ball
localX := ballLocation.X - selfLocation.X
localY := ballLocation.Y - selfLocation.Y
toBallAngle := math.Atan2(localY,localX)
steer := toBallAngle - selfRotation.Yaw
// Adjust to be within a -pi to pi range
if steer < -math.Pi {
steer += math.Pi * 2.0;
} else if steer >= math.Pi {
steer -= math.Pi * 2.0;
}
// If angle is greater than 1 radian, limit to full turn
if (steer > 1) {
steer = 1
} else if (steer < -1) {
steer = -1
}
PlayerInput.Steer = steer
// Drift if close to the ball and not very aligned
distanceToBall := math.Sqrt(localX * localX + localY * localY)
if distanceToBall < 300 && math.Abs(steer) > 0.3 {
PlayerInput.Handbrake = true
}
// Go Forward
PlayerInput.Throttle = 1.0
return PlayerInput
}
func main() {
// connect to RLBot
rlBot, err := RLBot.Connect(23234)
if err != nil {
panic(err)
}
// Send ready message
err = rlBot.SendReadyMessage(true, true, true)
if err != nil {
panic(err)
}
// Set our tick handler
err = rlBot.SetGetInput(getInput)
fmt.Println(err.Error())
}