forked from WorldUnitedNFS/freeroam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket.go
68 lines (57 loc) · 1.76 KB
/
packet.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package freeroam
import (
"bytes"
"encoding/binary"
"github.com/WorldUnitedNFS/freeroam/carstate"
"github.com/WorldUnitedNFS/freeroam/math"
)
// WriteSubpacket writes a subpacket with specified type and payload to a bytes.Buffer
func WriteSubpacket(buf *bytes.Buffer, typ uint8, data []byte) {
if len(data) > 255 {
panic("WriteSubpacket: subpacket length exceeds 255")
}
buf.WriteByte(typ)
buf.WriteByte(uint8(len(data)))
buf.Write(data)
}
type CarPosPacket struct {
time uint16
packet []byte
pos math.Vector2D
rotation float64
}
// Valid returns true if CarPosPacket contains valid packet data.
func (p *CarPosPacket) Valid() bool {
return p.packet != nil
}
// Pos returns the car position as a Vector2D.
func (p *CarPosPacket) Pos() math.Vector2D {
return p.pos
}
// Rotation returns the car rotation in degrees.
func (p *CarPosPacket) Rotation() float64 {
return p.rotation
}
// Packet returns the packet data with the packet time replaced by the argument.
func (p *CarPosPacket) Packet(time uint16) []byte {
binary.BigEndian.PutUint16(p.packet, time)
return p.packet
}
// Update updates CarPosPacket with the specified byte slice.
// The supplied slice shouldn't be modified after calling this method.
func (p *CarPosPacket) Update(packet []byte) {
p.time = binary.BigEndian.Uint16(packet[0:2])
p.packet = packet
reader := carstate.NewPacketReader(packet)
decodedPacket, err := reader.Decode()
if err != nil {
panic(err)
}
coords := decodedPacket.Coordinates()
p.pos.X = coords.X
p.pos.Y = coords.Y
p.rotation = decodedPacket.Rotation()
}