aboutsummaryrefslogtreecommitdiffstats
path: root/packet/command/playerinfo/playerinfo.go
blob: 962101b12f42394bebcf5620c972d5a020020f72 (plain) (blame)
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
package playerinfo

import (
	"github.com/osm/quake/common/buffer"
	"github.com/osm/quake/common/context"
	"github.com/osm/quake/packet/command/deltausercommand"
	"github.com/osm/quake/protocol"
	"github.com/osm/quake/protocol/fte"
	"github.com/osm/quake/protocol/mvd"
)

const fteExtensions uint32 = fte.ExtensionHullSize |
	fte.ExtensionTrans |
	fte.ExtensionScale |
	fte.ExtensionFatness

type Command struct {
	IsMVD bool

	Index   byte
	Default *CommandDefault
	MVD     *CommandMVD
}

type CommandDefault struct {
	CoordSize            uint8
	MVDProtocolExtension uint32
	FTEProtocolExtension uint32

	Bits             uint16
	ExtraBits        byte
	Coord            [3]float32
	Frame            byte
	Msec             byte
	DeltaUserCommand *deltausercommand.Command
	Velocity         [3]uint16
	ModelIndex       byte
	SkinNum          byte
	Effects          byte
	WeaponFrame      byte
}

type CommandMVD struct {
	CoordSize uint8

	Bits        uint16
	Frame       byte
	Coord       [3]float32
	Angle       [3]float32
	ModelIndex  byte
	SkinNum     byte
	Effects     byte
	WeaponFrame byte
}

func (cmd *Command) Bytes() []byte {
	buf := buffer.New()

	buf.PutByte(protocol.SVCPlayerInfo)
	buf.PutByte(cmd.Index)

	if cmd.IsMVD && cmd.MVD != nil {
		buf.PutBytes(cmd.MVD.Bytes())
	} else if cmd.Default != nil {
		buf.PutBytes(cmd.Default.Bytes())
	}

	return buf.Bytes()
}

func (cmd *CommandDefault) Bytes() []byte {
	buf := buffer.New()

	writeCoord := buf.PutCoord16
	if cmd.CoordSize == 4 ||
		cmd.MVDProtocolExtension&mvd.ExtensionFloatCoords != 0 ||
		cmd.FTEProtocolExtension&fteExtensions != 0 {
		writeCoord = buf.PutCoord32
	}

	buf.PutUint16(cmd.Bits)

	if cmd.FTEProtocolExtension&fteExtensions != 0 && cmd.Bits&fte.UFarMore != 0 {
		buf.PutByte(cmd.ExtraBits)
	}

	for i := 0; i < 3; i++ {
		writeCoord(cmd.Coord[i])
	}

	buf.PutByte(cmd.Frame)

	if cmd.Bits&protocol.PFMsec != 0 {
		buf.PutByte(cmd.Msec)
	}

	if cmd.Bits&protocol.PFCommand != 0 {
		buf.PutBytes(cmd.DeltaUserCommand.Bytes())
	}

	for i := 0; i < 3; i++ {
		if cmd.Bits&(protocol.PFVelocity1<<i) != 0 {
			buf.PutUint16(cmd.Velocity[i])
		}
	}

	if cmd.Bits&protocol.PFModel != 0 {
		buf.PutByte(cmd.ModelIndex)
	}

	if cmd.Bits&protocol.PFSkinNum != 0 {
		buf.PutByte(cmd.SkinNum)
	}

	if cmd.Bits&protocol.PFEffects != 0 {
		buf.PutByte(cmd.Effects)
	}

	if cmd.Bits&protocol.PFWeaponFrame != 0 {
		buf.PutByte(cmd.WeaponFrame)
	}

	return buf.Bytes()
}

func (cmd *CommandMVD) Bytes() []byte {
	buf := buffer.New()

	writeCoord := buf.PutCoord16
	if cmd.CoordSize == 4 {
		writeCoord = buf.PutCoord32
	}

	buf.PutUint16(cmd.Bits)
	buf.PutByte(cmd.Frame)

	for i := 0; i < 3; i++ {
		if cmd.Bits&(protocol.DFOrigin<<i) != 0 {
			writeCoord(cmd.Coord[i])
		}
	}

	for i := 0; i < 3; i++ {
		if cmd.Bits&(protocol.DFAngles<<i) != 0 {
			buf.PutAngle16(cmd.Angle[i])
		}
	}

	if cmd.Bits&protocol.DFModel != 0 {
		buf.PutByte(cmd.ModelIndex)
	}

	if cmd.Bits&protocol.DFSkinNum != 0 {
		buf.PutByte(cmd.SkinNum)
	}

	if cmd.Bits&protocol.DFEffects != 0 {
		buf.PutByte(cmd.Effects)
	}

	if cmd.Bits&protocol.DFWeaponFrame != 0 {
		buf.PutByte(cmd.WeaponFrame)
	}

	return buf.Bytes()
}

func Parse(ctx *context.Context, buf *buffer.Buffer) (*Command, error) {
	var err error
	var cmd Command

	cmd.IsMVD = ctx.GetIsMVD()

	if cmd.Index, err = buf.ReadByte(); err != nil {
		return nil, err
	}

	if cmd.IsMVD {
		if cmd.MVD, err = parseCommandMVD(ctx, buf); err != nil {
			return nil, err
		}
	} else {
		if cmd.Default, err = parseCommandDefault(ctx, buf); err != nil {
			return nil, err
		}
	}

	return &cmd, nil
}

func parseCommandDefault(ctx *context.Context, buf *buffer.Buffer) (*CommandDefault, error) {
	var err error
	var cmd CommandDefault

	cmd.FTEProtocolExtension = ctx.GetFTEProtocolExtension()
	cmd.MVDProtocolExtension = ctx.GetMVDProtocolExtension()
	cmd.CoordSize = ctx.GetCoordSize()

	readCoord := buf.GetCoord16

	if cmd.CoordSize == 4 ||
		cmd.MVDProtocolExtension&mvd.ExtensionFloatCoords != 0 ||
		cmd.FTEProtocolExtension&fteExtensions != 0 {
		readCoord = buf.GetCoord32
	}

	if cmd.Bits, err = buf.GetUint16(); err != nil {
		return nil, err
	}

	var bits uint32 = uint32(cmd.Bits)

	if cmd.FTEProtocolExtension&fteExtensions != 0 && bits&fte.UFarMore != 0 {
		if cmd.ExtraBits, err = buf.ReadByte(); err != nil {
			return nil, err
		}

		bits = (uint32(cmd.ExtraBits) << 16) | uint32(cmd.Bits)
	}

	for i := 0; i < 3; i++ {
		if cmd.Coord[i], err = readCoord(); err != nil {
			return nil, err
		}
	}

	if cmd.Frame, err = buf.ReadByte(); err != nil {
		return nil, err
	}

	if bits&protocol.PFMsec != 0 {
		if cmd.Msec, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if bits&protocol.PFCommand != 0 {
		if cmd.DeltaUserCommand, err = deltausercommand.Parse(ctx, buf); err != nil {
			return nil, err
		}
	}

	for i := 0; i < 3; i++ {
		if bits&(protocol.PFVelocity1<<i) != 0 {
			if cmd.Velocity[i], err = buf.GetUint16(); err != nil {
				return nil, err
			}
		}
	}

	if bits&protocol.PFModel != 0 {
		if cmd.ModelIndex, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if bits&protocol.PFSkinNum != 0 {
		if cmd.SkinNum, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if bits&protocol.PFEffects != 0 {
		if cmd.Effects, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if bits&protocol.PFWeaponFrame != 0 {
		if cmd.WeaponFrame, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	return &cmd, nil
}

func parseCommandMVD(ctx *context.Context, buf *buffer.Buffer) (*CommandMVD, error) {
	var err error
	var cmd CommandMVD

	cmd.CoordSize = ctx.GetCoordSize()
	readCoord := buf.GetCoord16
	if cmd.CoordSize == 4 {
		readCoord = buf.GetCoord32
	}

	if cmd.Bits, err = buf.GetUint16(); err != nil {
		return nil, err
	}

	if cmd.Frame, err = buf.ReadByte(); err != nil {
		return nil, err
	}

	for i := 0; i < 3; i++ {
		if cmd.Bits&(protocol.DFOrigin<<i) != 0 {
			if cmd.Coord[i], err = readCoord(); err != nil {
				return nil, err
			}
		}
	}

	for i := 0; i < 3; i++ {
		if cmd.Bits&(protocol.DFAngles<<i) != 0 {
			if cmd.Angle[i], err = buf.GetAngle16(); err != nil {
				return nil, err
			}
		}
	}

	if cmd.Bits&protocol.DFModel != 0 {
		if cmd.ModelIndex, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if cmd.Bits&protocol.DFSkinNum != 0 {
		if cmd.SkinNum, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if cmd.Bits&protocol.DFEffects != 0 {
		if cmd.Effects, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	if cmd.Bits&protocol.DFWeaponFrame != 0 {
		if cmd.WeaponFrame, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	}

	return &cmd, nil
}