Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
047deb65e7 | ||
|
|
f1728c11da | ||
|
|
245d6ac0e1 |
@@ -1,9 +1,7 @@
|
||||
// Package bngsdk defines an API to interact with the BeamNG outgauge data in
|
||||
// Go
|
||||
// Package bngsdk defines an API to interact with the BeamNG outgauge data in Go
|
||||
package bngsdk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -22,8 +20,6 @@ type BeamNGSDK struct {
|
||||
Conn *net.UDPConn
|
||||
Buffer []byte
|
||||
Data Outgauge
|
||||
// Internal data
|
||||
reader bytes.Reader
|
||||
}
|
||||
|
||||
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
|
||||
@@ -84,7 +80,6 @@ func (sdk *BeamNGSDK) ReadData() error {
|
||||
}
|
||||
|
||||
// Read the binary data into the struct
|
||||
sdk.reader.Reset(sdk.Buffer[:n])
|
||||
err = sdk.parseData()
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding UDP packet:", err)
|
||||
@@ -121,80 +116,59 @@ func Init(ip string, port int) (BeamNGSDK, error) {
|
||||
|
||||
// ShowLights - functions to check if a given dash light is on [START]
|
||||
|
||||
// ShiftLight returns:
|
||||
// true if the shift light is on
|
||||
// false if the shift light is off
|
||||
// ShiftLight reports whether the shift light is on
|
||||
func (sdk *BeamNGSDK) ShiftLight() bool {
|
||||
return sdk.Data.ShowLights&DL_SHIFT != 0
|
||||
}
|
||||
|
||||
// HighBeam returns:
|
||||
// true if the high beams are on
|
||||
// false if the high beams are off
|
||||
// HighBeam reports whether high beams are on
|
||||
func (sdk *BeamNGSDK) HighBeam() bool {
|
||||
return sdk.Data.ShowLights&DL_FULLBEAM != 0
|
||||
}
|
||||
|
||||
// Handbrake returns:
|
||||
// true if the handbrake is pulled
|
||||
// false if the handbrake is down
|
||||
// Handbrake reports whether the handbrake is pulled
|
||||
func (sdk *BeamNGSDK) Handbrake() bool {
|
||||
return sdk.Data.ShowLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// Pitspeed returns:
|
||||
// true if the pit speed limiter is on
|
||||
// false if the pit speed limiter is off
|
||||
// Pitspeed reports whether the pit speed limiter is engaged
|
||||
//
|
||||
// NOTE: this may not be used in BeamNG.drive, haven't checked yet
|
||||
func (sdk *BeamNGSDK) Pitspeed() bool {
|
||||
return sdk.Data.ShowLights&DL_HANDBRAKE != 0
|
||||
return sdk.Data.ShowLights&DL_PITSPEED != 0
|
||||
}
|
||||
|
||||
// TractionControl returns:
|
||||
// true if traction control is on
|
||||
// false if traction control is off
|
||||
// TractionControl reports wheter TC is engaged
|
||||
func (sdk *BeamNGSDK) TractionControl() bool {
|
||||
return sdk.Data.ShowLights&DL_TC != 0
|
||||
}
|
||||
|
||||
// LeftIndicator returns:
|
||||
// true if the left indicator is on
|
||||
// false if the left indicator is off
|
||||
// LeftIndicator reports whether the left indicator is on
|
||||
func (sdk *BeamNGSDK) LeftIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_L != 0
|
||||
}
|
||||
|
||||
// RightIndicator returns:
|
||||
// true if the right indicator is on
|
||||
// false if the right indicator is off
|
||||
// RightIndicator reports wheter the right indicator is on
|
||||
func (sdk *BeamNGSDK) RightIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_R != 0
|
||||
}
|
||||
|
||||
// AnyIndicator returns:
|
||||
// true if the any indicator is on
|
||||
// false if the all indicators are off
|
||||
// AnyIndicator reports whether any indicator is on
|
||||
func (sdk *BeamNGSDK) AnyIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_ANY != 0
|
||||
}
|
||||
|
||||
// OilLight returns
|
||||
// true if the oil light is on
|
||||
// false if the oil light is off
|
||||
// OilLight reports whether the oil warning light is on
|
||||
func (sdk *BeamNGSDK) OilLight() bool {
|
||||
return sdk.Data.ShowLights&DL_OILWARN != 0
|
||||
}
|
||||
|
||||
// BatteryLight returns
|
||||
// true if the battery light is on
|
||||
// false if the battery light is off
|
||||
// BatteryLight reports whether the battery light is on
|
||||
func (sdk *BeamNGSDK) BatteryLight() bool {
|
||||
return sdk.Data.ShowLights&DL_BATTERY != 0
|
||||
}
|
||||
|
||||
// ABS returns
|
||||
// true if the ABS is engaged
|
||||
// false if the ABS isn't engaged
|
||||
// ABS reports whether the ABS light is on
|
||||
func (sdk *BeamNGSDK) ABS() bool {
|
||||
return sdk.Data.ShowLights&DL_ABS != 0
|
||||
}
|
||||
@@ -203,80 +177,58 @@ func (sdk *BeamNGSDK) ABS() bool {
|
||||
|
||||
// DashLights - functions to check if a given dash light is provided [START]
|
||||
|
||||
// HasShiftLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasShiftLight reports whether a shift light is available
|
||||
func (sdk *BeamNGSDK) HasShiftLight() bool {
|
||||
return sdk.Data.DashLights&DL_SHIFT != 0
|
||||
}
|
||||
|
||||
// HasHighBeamLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasHighBeamLight reports whether a high beam light is available
|
||||
func (sdk *BeamNGSDK) HasHighBeamLight() bool {
|
||||
return sdk.Data.DashLights&DL_FULLBEAM != 0
|
||||
}
|
||||
|
||||
// HasHandbrakeLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasHandbrakeLight reports wheter a handbrake light is available
|
||||
func (sdk *BeamNGSDK) HasHandbrakeLight() bool {
|
||||
return sdk.Data.DashLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// HasPitspeed returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasPitspeed reports whether a pit speed limitr is available
|
||||
// NOTE: this may not be used in BeamNG.drive, haven't checked yet
|
||||
func (sdk *BeamNGSDK) HasPitspeed() bool {
|
||||
return sdk.Data.DashLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// HasTractionControlLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasTractionControlLight reports whether a traction control light is available
|
||||
func (sdk *BeamNGSDK) HasTractionControlLight() bool {
|
||||
return sdk.Data.DashLights&DL_TC != 0
|
||||
}
|
||||
|
||||
// HasLeftIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasLeftIndicatorLight reports whether a left indicator is available
|
||||
func (sdk *BeamNGSDK) HasLeftIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_L != 0
|
||||
}
|
||||
|
||||
// HasRightIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasRightIndicatorLight reports whether a right indicator is available
|
||||
func (sdk *BeamNGSDK) HasRightIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_R != 0
|
||||
}
|
||||
|
||||
// HasAnyIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasAnyIndicatorLight reports whether an any indicator light is available
|
||||
func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_ANY != 0
|
||||
}
|
||||
|
||||
// HasOilLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasOilLight reports whether a oil light is available
|
||||
func (sdk *BeamNGSDK) HasOilLight() bool {
|
||||
return sdk.Data.DashLights&DL_OILWARN != 0
|
||||
}
|
||||
|
||||
// HasBatteryLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasBatteryLight reports whether a battery light is available
|
||||
func (sdk *BeamNGSDK) HasBatteryLight() bool {
|
||||
return sdk.Data.DashLights&DL_BATTERY != 0
|
||||
}
|
||||
|
||||
// HasABSLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// HasABSLight reports whether an ABS light is available
|
||||
func (sdk *BeamNGSDK) HasABSLight() bool {
|
||||
return sdk.Data.DashLights&DL_ABS != 0
|
||||
}
|
||||
@@ -285,25 +237,21 @@ func (sdk *BeamNGSDK) HasABSLight() bool {
|
||||
|
||||
// Flags - functions to check if a given flag is ON [START]
|
||||
|
||||
// HasTurbo returns:
|
||||
// true if the car has a turbo
|
||||
// false if it doesn't
|
||||
// HasTurbo reports whether there's a turbo
|
||||
func (sdk *BeamNGSDK) HasTurbo() bool {
|
||||
return sdk.Data.Flags&OG_TURBO != 0
|
||||
}
|
||||
|
||||
// PrefersKm returns:
|
||||
// true if true
|
||||
// false if false
|
||||
// wow
|
||||
// PrefersKm reports whether the user prefers kilometers:
|
||||
// - true is prefers Km
|
||||
// - false is prefers Mi
|
||||
func (sdk *BeamNGSDK) PrefersKm() bool {
|
||||
return sdk.Data.Flags&OG_KM != 0
|
||||
}
|
||||
|
||||
// PrefersBAR returns:
|
||||
// true if true
|
||||
// false if false
|
||||
// wow
|
||||
// PrefersBAR reports whether the user prefers BAR:
|
||||
// - true is prefers BAR
|
||||
// - false is prefers PSI
|
||||
func (sdk *BeamNGSDK) PrefersBAR() bool {
|
||||
return sdk.Data.Flags&OG_BAR != 0
|
||||
}
|
||||
|
||||
+1
-2
@@ -38,9 +38,8 @@ func BenchmarkReadData(b *testing.B) {
|
||||
}
|
||||
packetBytes := buf.Bytes()
|
||||
|
||||
// 4. Reset the timer to isolate the setup allocations from the loop metrics
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs() // Tracks memory allocations per operation (B/op and allocs/op)
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Feed a packet into the network buffer right before reading it
|
||||
|
||||
Reference in New Issue
Block a user