-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocket.go
188 lines (157 loc) · 5.47 KB
/
socket.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
package RLBotGo
import (
"encoding/binary"
"flag"
"fmt"
"io"
"net"
schema "github.com/Trey2k/RLBotGo/flat"
)
type RLBot struct {
conn net.Conn
debugRenderGroup *RenderGroup
PlayerIndex int32
}
type payload struct {
data []byte
dataType uint16
dataSize uint16
}
type rlData interface {
marshal() []byte
}
// SendQuickChat This will allow your bot to be toxic. Who doesn't want that?
func (socket *RLBot) SendQuickChat(quickChatSelection int8, teamOnly bool) error {
quickChat := &QuickChat{
QuickChatSelection: quickChatSelection,
TeamOnly: teamOnly,
PlayerIndex: socket.PlayerIndex,
}
return socket.SendMessage(DataType_QuickChat, quickChat)
}
// SendReadyMessage Send the ready message to RLBot
func (socket *RLBot) SendReadyMessage(wantsBallPredictions, wantsQuickChat, wantsGameMessages bool) error {
readyMsg := &ReadyMessage{
WantsBallPredictions: wantsBallPredictions,
WantsQuickChat: wantsQuickChat,
WantsGameMessages: wantsGameMessages,
}
return socket.SendMessage(DataType_ReadyMessage, readyMsg)
}
// SendDesiredGameState Send a specific game state. Good for testing
func (socket *RLBot) SendDesiredGameState(desiredGameState *DesiredGameState) error {
return socket.SendMessage(DataType_DesiredGameState, desiredGameState)
}
// Connect (port int) (Socket, error) Initiate the connection to RLBot returns a socket and a error on failure.
// Default port is 23234
func Connect(port int) (*RLBot, error) {
var index = flag.Int("player-index", 0, "The player index for the bot")
// Go has to know about these two otherwise will fail to launch.
flag.String("rlbot-version", "0", "RLBot version")
flag.String("rlbot-dll-directory", "0", "RLBot DLL dir")
flag.Parse()
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
socket := &RLBot{
conn: conn,
PlayerIndex: int32(*index),
}
return socket, err
}
// SendMessage (dataType uint16, data rlData) error Send a data payload to RLBot, returns a error on failure
func (socket *RLBot) SendMessage(dataType uint16, data rlData) error {
dataTypePayload := make([]byte, 2)
binary.BigEndian.PutUint16(dataTypePayload, dataType)
payload := data.marshal()
size := make([]byte, 2)
binary.BigEndian.PutUint16(size, uint16(len(payload)))
bytes := append([]byte{}, dataTypePayload...)
bytes = append(bytes, size...)
bytes = append(bytes, payload...)
_, err := socket.conn.Write(bytes)
return err
}
// read game data from the RLBot API
func (socket *RLBot) startReadingBytes(payloadChannel chan *payload, errChan chan error) {
for {
dataInfo := make([]byte, 4)
_, err := io.ReadFull(socket.conn, dataInfo)
if err != nil && err != io.EOF {
errChan <- err
break
}
dataType := binary.BigEndian.Uint16(dataInfo[:2])
dataSize := binary.BigEndian.Uint16(dataInfo[2:])
data := make([]byte, dataSize)
_, err = io.ReadFull(socket.conn, data)
if err != nil && err != io.EOF {
errChan <- err
break
}
payloadChannel <- &payload{
data: data,
dataType: dataType,
dataSize: dataSize,
}
}
}
// SetGetInput (handler func(gameState *GameState, socket *Socket) Set your tick handler function and start listening for gameTickPackets
func (socket *RLBot) SetGetInput(handler func(gameState *GameState, socket *RLBot) *ControllerState) error {
gameState := &GameState{}
gameState.BallPrediction = &BallPrediction{}
gameState.FieldInfo = &FieldInfo{}
gameState.GameTick = &GameTickPacket{}
gameState.MatchSettings = &MatchSettings{}
gameState.GameMessage = &GameMessagePacket{}
gameState.GameMessageOK = false
gameState.FieldInfoOK = false
gameState.MatchSettingsOK = false
payloadChan := make(chan *payload, 5) // Makeing a payload channel with a buffer size of 5
errChan := make(chan error)
go socket.startReadingBytes(payloadChan, errChan) // Start reading packets in go routine and sending them over a channel
for {
select {
case err := <-errChan: // Check for a error every loop
return err
default:
payload := <-payloadChan // Hold until we get a payload
switch payload.dataType {
case DataType_TickPacket:
flatGameTick := schema.GetRootAsGameTickPacket(payload.data, 0)
gameState.GameTick = &GameTickPacket{} // Resetting to 0 values just in case
gameState.GameTick.unmarshal(flatGameTick)
input := handler(gameState, socket)
// Get input from handler and send it
if input != nil {
playerInput := &PlayerInput{
PlayerIndex: socket.PlayerIndex,
ControllerState: *input,
}
err := socket.SendMessage(DataType_PlayerInput, playerInput)
if err != nil {
return err
}
}
// Reset game message Ok to false
if gameState.GameMessageOK {
gameState.GameMessageOK = false
gameState.GameMessage = &GameMessagePacket{}
}
case DataType_FieldInfo:
flatFieldInfo := schema.GetRootAsFieldInfo(payload.data, 0)
gameState.FieldInfoOK = true
gameState.FieldInfo.unmarshal(flatFieldInfo)
case DataType_MatchSettings:
flatMatchSettings := schema.GetRootAsMatchSettings(payload.data, 0)
gameState.MatchSettingsOK = true
gameState.MatchSettings.unmarshal(flatMatchSettings)
case DataType_BallPrediction:
flatBallPrediction := schema.GetRootAsBallPrediction(payload.data, 0)
gameState.BallPrediction.unmarshal(flatBallPrediction)
case DataType_MessagePacket:
flatMessagePacket := schema.GetRootAsMessagePacket(payload.data, 0)
gameState.GameMessageOK = true
gameState.GameMessage.unmarshal(flatMessagePacket)
}
}
}
}