-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsandbox.go
More file actions
545 lines (477 loc) · 13.5 KB
/
Copy pathsandbox.go
File metadata and controls
545 lines (477 loc) · 13.5 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package main
import (
"sync"
"time"
rlbot "github.com/RLBot/go-interface"
"github.com/RLBot/go-interface/flat"
)
// SandboxState holds the current sandbox connection and manages concurrent access.
type SandboxState struct {
conn *rlbot.RLBotConnection
mu sync.Mutex
active bool
}
// SandboxBallPacket is sent to the frontend for each game tick.
type SandboxGamePacket struct {
Ball SandboxBall `json:"ball"`
Cars []SandboxCar `json:"cars"`
SecondsElapsed float32 `json:"seconds_elapsed"`
}
type SandboxBall struct {
Physics SandboxPhysics `json:"physics"`
}
type SandboxCar struct {
Index int32 `json:"index"`
Physics SandboxPhysics `json:"physics"`
Team uint32 `json:"team"`
Boost float32 `json:"boost"`
IsBot bool `json:"is_bot"`
Name string `json:"name"`
}
type SandboxPhysics struct {
Location SandboxVec3 `json:"location"`
Rotation SandboxRot3 `json:"rotation"`
Velocity SandboxVec3 `json:"velocity"`
AngularVelocity SandboxVec3 `json:"angular_velocity"`
}
type SandboxVec3 struct {
X float32 `json:"x"`
Y float32 `json:"y"`
Z float32 `json:"z"`
}
type SandboxRot3 struct {
Pitch float32 `json:"pitch"`
Yaw float32 `json:"yaw"`
Roll float32 `json:"roll"`
}
// SandboxStateSetting is sent from the frontend to set game state.
type SandboxStateSetting struct {
Ball *SandboxBallSetting `json:"ball,omitempty"`
Cars []SandboxCarSetting `json:"cars,omitempty"`
Game *SandboxGameSetting `json:"game_info,omitempty"`
Cmds []string `json:"console_commands,omitempty"`
}
type SandboxBallSetting struct {
Location *SandboxVec3 `json:"location,omitempty"`
Velocity *SandboxVec3 `json:"velocity,omitempty"`
Rotation *SandboxRot3 `json:"rotation,omitempty"`
}
type SandboxCarSetting struct {
Index int32 `json:"index"`
Location *SandboxVec3 `json:"location,omitempty"`
Velocity *SandboxVec3 `json:"velocity,omitempty"`
Rotation *SandboxRot3 `json:"rotation,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}
type SandboxGameSetting struct {
GravityZ *float32 `json:"world_gravity_z,omitempty"`
GameSpeed *float32 `json:"game_speed,omitempty"`
}
// SandboxMatchConfig is sent to the frontend with match configuration info for debug rendering.
type SandboxMatchConfig struct {
EnableRendering int `json:"enable_rendering"`
PerformanceMonitor int `json:"performance_monitor"`
Agents []SandboxRenderAgent `json:"agents"`
}
type SandboxRenderAgent struct {
Index int `json:"index"`
Name string `json:"name"`
IsBot bool `json:"is_bot"`
}
type SandboxRenderingStatus struct {
Index uint32 `json:"index"`
IsBot bool `json:"is_bot"`
Status bool `json:"status"`
}
// OpenSandbox connects to RLBotServer and starts reading game packets.
// The frontend should call this when entering the sandbox page.
func (a *App) OpenSandbox() error {
a.sandboxMu.Lock()
defer a.sandboxMu.Unlock()
if a.sandbox != nil && a.sandbox.active {
return nil // already connected
}
a.app.Event.Emit("sandbox:connecting", nil)
conn, err := rlbot.Connect(a.rlbotAddress)
if err != nil {
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Failed to connect to RLBotServer: " + err.Error(),
})
return err
}
// Send initial connection settings
err = conn.SendPacket(&flat.ConnectionSettingsT{
AgentId: "",
WantsBallPredictions: false,
WantsComms: false,
CloseBetweenMatches: false,
})
if err != nil {
conn.SendPacket(&flat.DisconnectSignalT{})
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Failed to send connection settings: " + err.Error(),
})
return err
}
state := &SandboxState{
conn: &conn,
active: true,
}
a.sandbox = state
// Start the reader goroutine
go a.sandboxReader(state, conn)
return nil
}
// CloseSandbox disconnects from RLBotServer.
// The frontend should call this when leaving the sandbox page.
func (a *App) CloseSandbox() {
a.sandboxMu.Lock()
state := a.sandbox
a.sandbox = nil
a.sandboxMu.Unlock()
if state == nil || !state.active {
return
}
state.mu.Lock()
state.active = false
if state.conn != nil {
state.conn.SendPacket(&flat.DisconnectSignalT{})
}
state.mu.Unlock()
}
// SandboxSetState sets the game state (ball, cars, game info, console commands).
func (a *App) SandboxSetState(setting SandboxStateSetting) error {
a.sandboxMu.Lock()
state := a.sandbox
a.sandboxMu.Unlock()
if state == nil || !state.active {
return nil // silently ignore if not connected
}
desired := &flat.DesiredGameStateT{}
if setting.Ball != nil {
ballState := &flat.DesiredBallStateT{}
if setting.Ball.Location != nil {
ballState.Physics = &flat.DesiredPhysicsT{
Location: &flat.Vector3PartialT{
X: &flat.FloatT{Val: setting.Ball.Location.X},
Y: &flat.FloatT{Val: setting.Ball.Location.Y},
Z: &flat.FloatT{Val: setting.Ball.Location.Z},
},
}
}
if setting.Ball.Velocity != nil {
if ballState.Physics == nil {
ballState.Physics = &flat.DesiredPhysicsT{}
}
ballState.Physics.Velocity = &flat.Vector3PartialT{
X: &flat.FloatT{Val: setting.Ball.Velocity.X},
Y: &flat.FloatT{Val: setting.Ball.Velocity.Y},
Z: &flat.FloatT{Val: setting.Ball.Velocity.Z},
}
}
desired.BallStates = []*flat.DesiredBallStateT{ballState}
}
if len(setting.Cars) > 0 {
carStates := make([]*flat.DesiredCarStateT, 0)
for _, cs := range setting.Cars {
carState := &flat.DesiredCarStateT{}
if cs.Location != nil {
carState.Physics = &flat.DesiredPhysicsT{
Location: &flat.Vector3PartialT{
X: &flat.FloatT{Val: cs.Location.X},
Y: &flat.FloatT{Val: cs.Location.Y},
Z: &flat.FloatT{Val: cs.Location.Z},
},
}
}
if cs.Velocity != nil {
if carState.Physics == nil {
carState.Physics = &flat.DesiredPhysicsT{}
}
carState.Physics.Velocity = &flat.Vector3PartialT{
X: &flat.FloatT{Val: cs.Velocity.X},
Y: &flat.FloatT{Val: cs.Velocity.Y},
Z: &flat.FloatT{Val: cs.Velocity.Z},
}
}
if cs.Rotation != nil {
if carState.Physics == nil {
carState.Physics = &flat.DesiredPhysicsT{}
}
carState.Physics.Rotation = &flat.RotatorPartialT{
Pitch: &flat.FloatT{Val: cs.Rotation.Pitch},
Yaw: &flat.FloatT{Val: cs.Rotation.Yaw},
Roll: &flat.FloatT{Val: cs.Rotation.Roll},
}
}
if cs.Boost != nil {
carState.BoostAmount = &flat.FloatT{Val: *cs.Boost}
}
// Ensure slice is large enough to hold this car index
needed := int(cs.Index + 1)
if len(carStates) < needed {
padded := make([]*flat.DesiredCarStateT, needed)
copy(padded, carStates)
carStates = padded
}
carStates[cs.Index] = carState
}
desired.CarStates = carStates
}
if setting.Game != nil {
desired.MatchInfo = &flat.DesiredMatchInfoT{}
if setting.Game.GravityZ != nil {
desired.MatchInfo.WorldGravityZ = &flat.FloatT{Val: *setting.Game.GravityZ}
}
if setting.Game.GameSpeed != nil {
desired.MatchInfo.GameSpeed = &flat.FloatT{Val: *setting.Game.GameSpeed}
}
}
if len(setting.Cmds) > 0 {
cmds := make([]*flat.ConsoleCommandT, len(setting.Cmds))
for i, cmd := range setting.Cmds {
cmds[i] = &flat.ConsoleCommandT{Command: cmd}
}
desired.ConsoleCommands = cmds
}
state.mu.Lock()
defer state.mu.Unlock()
if state.conn != nil {
return state.conn.SendPacket(desired)
}
return nil
}
// sandboxReader runs in a goroutine and reads packets from the RLBot connection.
func (a *App) sandboxReader(state *SandboxState, conn rlbot.RLBotConnection) {
defer func() {
a.sandboxMu.Lock()
if a.sandbox == state {
a.sandbox = nil
}
a.sandboxMu.Unlock()
a.app.Event.Emit("sandbox:disconnected", nil)
}()
// Wait for MatchConfigurationT and FieldInfoT
hasFieldInfo := false
waitStart := time.Now()
timeout := 30 * time.Second
for !hasFieldInfo {
if time.Since(waitStart) > timeout {
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Timed out waiting for FieldInfo",
})
return
}
packet, err := conn.RecvPacket()
if err != nil {
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Connection error: " + err.Error(),
})
return
}
switch p := packet.Value.(type) {
case *flat.FieldInfoT:
// Send InitCompleteT to signal we're ready for GamePackets
conn.SendPacket(&flat.InitCompleteT{})
hasFieldInfo = true
case *flat.MatchConfigurationT:
// Extract match config info for debug rendering UI
matchConfig := simplifyMatchConfig(p)
a.app.Event.Emit("sandbox:match-config", matchConfig)
case *flat.DisconnectSignalT:
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Received disconnect signal while connecting",
})
return
}
}
// Signal that we're connected and ready
a.app.Event.Emit("sandbox:connected", nil)
// Now read GamePackets in a loop
for {
state.mu.Lock()
isActive := state.active
state.mu.Unlock()
if !isActive {
return
}
packet, err := conn.RecvPacket()
if err != nil {
a.app.Event.Emit("sandbox:error", map[string]any{
"message": "Connection unexpectedly closed: " + err.Error(),
})
return
}
switch p := packet.Value.(type) {
case *flat.GamePacketT:
sandboxPacket := simplifyGamePacket(p)
a.app.Event.Emit("sandbox:game-packet", sandboxPacket)
case *flat.DisconnectSignalT:
return
}
}
}
// SandboxSetRendering sends a per-agent rendering status update to RLBot.
func (a *App) SandboxSetRendering(settings SandboxRenderingStatus) error {
a.sandboxMu.Lock()
state := a.sandbox
a.sandboxMu.Unlock()
if state == nil || !state.active {
return nil
}
state.mu.Lock()
defer state.mu.Unlock()
if state.conn == nil {
return nil
}
return state.conn.SendPacket(&flat.RenderingStatusT{
Index: settings.Index,
IsBot: settings.IsBot,
Status: settings.Status,
})
}
func (a *App) SandboxSetPerfMonDisplayMode(mode int) error {
a.sandboxMu.Lock()
state := a.sandbox
a.sandboxMu.Unlock()
if state == nil || !state.active {
return nil
}
state.mu.Lock()
defer state.mu.Unlock()
if state.conn == nil {
return nil
}
var displayMode flat.PerformanceMonitor
switch mode {
case 0:
displayMode = flat.PerformanceMonitorShowWhenSuboptimal
case 1:
displayMode = flat.PerformanceMonitorAlwaysShow
case 2:
displayMode = flat.PerformanceMonitorNeverShow
default:
return nil // invalid mode, ignore
}
return state.conn.SendPacket(&flat.UpdatePerformanceMonitorT{
Show: displayMode,
})
}
// simplifyMatchConfig extracts rendering-relevant info from MatchConfigurationT.
func simplifyMatchConfig(mc *flat.MatchConfigurationT) SandboxMatchConfig {
cfg := SandboxMatchConfig{
EnableRendering: int(mc.EnableRendering),
PerformanceMonitor: int(mc.PerformanceMonitor),
}
// Collect bots (players that are not humans)
// The loop index corresponds to the player's position in the GamePacket's Players array.
for i, player := range mc.PlayerConfigurations {
if player.Variety == nil {
continue
}
switch v := player.Variety.Value.(type) {
case *flat.CustomBotT:
cfg.Agents = append(cfg.Agents, SandboxRenderAgent{
Index: i,
Name: v.Name,
IsBot: true,
})
}
}
// Collect scripts
for i, script := range mc.ScriptConfigurations {
cfg.Agents = append(cfg.Agents, SandboxRenderAgent{
Index: i,
Name: script.Name,
IsBot: false,
})
}
return cfg
}
// simplifyGamePacket converts a flat.GamePacketT to a lightweight SandboxGamePacket for the frontend.
func simplifyGamePacket(gp *flat.GamePacketT) SandboxGamePacket {
pkt := SandboxGamePacket{}
if gp.MatchInfo != nil {
pkt.SecondsElapsed = gp.MatchInfo.SecondsElapsed
}
if len(gp.Balls) > 0 && gp.Balls[0] != nil && gp.Balls[0].Physics != nil {
phys := gp.Balls[0].Physics
physics := SandboxPhysics{}
if phys.Location != nil {
physics.Location = SandboxVec3{
X: phys.Location.X,
Y: phys.Location.Y,
Z: phys.Location.Z,
}
}
if phys.Rotation != nil {
physics.Rotation = SandboxRot3{
Pitch: phys.Rotation.Pitch,
Yaw: phys.Rotation.Yaw,
Roll: phys.Rotation.Roll,
}
}
if phys.Velocity != nil {
physics.Velocity = SandboxVec3{
X: phys.Velocity.X,
Y: phys.Velocity.Y,
Z: phys.Velocity.Z,
}
}
if phys.AngularVelocity != nil {
physics.AngularVelocity = SandboxVec3{
X: phys.AngularVelocity.X,
Y: phys.AngularVelocity.Y,
Z: phys.AngularVelocity.Z,
}
}
pkt.Ball.Physics = physics
}
pkt.Cars = make([]SandboxCar, len(gp.Players))
for i, player := range gp.Players {
if player == nil {
continue
}
car := SandboxCar{
Index: int32(i),
Team: player.Team,
Boost: player.Boost,
IsBot: player.IsBot,
Name: player.Name,
}
if player.Physics != nil {
physics := SandboxPhysics{}
if player.Physics.Location != nil {
physics.Location = SandboxVec3{
X: player.Physics.Location.X,
Y: player.Physics.Location.Y,
Z: player.Physics.Location.Z,
}
}
if player.Physics.Rotation != nil {
physics.Rotation = SandboxRot3{
Pitch: player.Physics.Rotation.Pitch,
Yaw: player.Physics.Rotation.Yaw,
Roll: player.Physics.Rotation.Roll,
}
}
if player.Physics.Velocity != nil {
physics.Velocity = SandboxVec3{
X: player.Physics.Velocity.X,
Y: player.Physics.Velocity.Y,
Z: player.Physics.Velocity.Z,
}
}
if player.Physics.AngularVelocity != nil {
physics.AngularVelocity = SandboxVec3{
X: player.Physics.AngularVelocity.X,
Y: player.Physics.AngularVelocity.Y,
Z: player.Physics.AngularVelocity.Z,
}
}
car.Physics = physics
}
pkt.Cars[i] = car
}
return pkt
}