-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuakePacket.h
447 lines (379 loc) · 10.7 KB
/
QuakePacket.h
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
// QuakePacket.h : interface for the CQuakePacket
//
// Pandora Bot v2.0
// By Darrin W. Cullop (dcullop@arizona.edu)
//
// Copyright (c) 1998-1999 by Darrin W. Cullop. All Rights Reserved.
//
// Permission to use, copy and distribute unedited copies of this whole
// document is hereby granted, provided that no fee is charged for the
// use or availability of this document (other than the normal connection
// costs for on-line services, if applicable). The above copyright notice
// and this permission notice must be left intact in all copies of this
// document. Short excerpts of this document may be quoted in discussion
// groups or mailing list articles, as long as a reference to the full
// document is given.
//
// Commercial distribution of this document, in whole or in part,
// requires prior agreement with the author. Commercial distribution
// includes any means by which the user has to pay either for the support
// (e.g. book, newsletter or CD-ROM) or for the document itself.
//
// Unauthorized commercial distribution is prohibited.
//
//////////////////////////////////////////////////////////////////////
#ifndef QUAKE_PACKET_H
#define QUAKE_PACKET_H
//////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Includes
#include "StdAfx.h"
#include "SystemObject.h"
#include "Vector.h"
//////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Defines
//////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Class Definition
//class CQuakePacket : public CSystemObject
class CQuakePacket : public CObject
{
DECLARE_DYNCREATE(CQuakePacket);
// Attributes
private:
BOOL m_bReadMode;
CByteArray m_arrData;
protected:
WORD m_nPosition;
// Construction / Destruction
public:
CQuakePacket() { Clear(); };
CQuakePacket(const CQuakePacket* pNew) { Copy( *pNew ); };
CQuakePacket(const CQuakePacket& New) { Copy( New ); };
virtual ~CQuakePacket() { };
// Data Manipulation Operations
public:
// Get Data Functions
const BOOL& GetReadMode() const { return m_bReadMode; };
const WORD& GetPosition() const { return m_nPosition; };
const CByteArray& GetData() const { return m_arrData; };
BYTE* GetDataPtr() { return m_arrData.GetData(); };
CByteArray& GetDataArray() { return m_arrData; };
// Operations
private:
void MergeBuffer(CQuakePacket&, const WORD&);
public:
void InsertData(CQuakePacket&);
void PrependData(CQuakePacket&);
BOOL DataToRead() const;
void RemoveData(const WORD&, const WORD&);
virtual void SetReadMode();
virtual void SetWriteMode();
virtual void Clear();
virtual void Copy(const CQuakePacket&);
void SetSize(int nNew) { GetDataArray().SetSize(nNew); GetDataArray().FreeExtra(); m_nPosition = GetSize(); };
int GetSize() const { return GetData().GetSize(); };
void SkipToEnd() { m_nPosition = GetSize(); };
BYTE PeekNext() const { return GetData().GetAt(m_nPosition); };
void CheckLength() { GetDataArray().SetSize(m_nPosition); GetDataArray().FreeExtra(); };
void Insert(BYTE nNew) { m_arrData.InsertAt(0, nNew); m_nPosition++; };
void SetAt(int nIndex, BYTE nNew) { m_arrData.SetAt(nIndex, nNew); };
// Overloaded Operators
CQuakePacket& operator=(const CQuakePacket&);
// Read / Write Operations
private:
BYTE GetByte();
void PutByte(const BYTE&);
public:
// Read Operations
char ReadChar() { return char(GetByte()); };
void ReadChar(char& nGet) { nGet = char(GetByte()); };
BYTE ReadByte() { return (GetByte()); };
void ReadByte(BYTE& nGet) { nGet = (GetByte()); };
WORD ReadWord() { return ((WORD)ReadShort()); };
void ReadWord(WORD& nGet) { ReadShort( (short&)nGet ); };
UINT ReadUInt() { return ((UINT)ReadLong()); };
void ReadUInt(UINT& nGet) { ReadLong( (long&)nGet ); };
short ReadShort();
void ReadShort(short&);
long ReadLong();
void ReadLong(long&);
float ReadFloat();
void ReadFloat(float&);
float ReadCoord();
void ReadCoord(float&);
float ReadAngle();
void ReadAngle(float&);
float ReadAngle16();
void ReadAngle16(float&);
CVector ReadAngles();
void ReadAngles(CVector&);
CVector ReadAngles16();
void ReadAngles16(CVector&);
CVector ReadVector();
void ReadVector(CVector&);
CString ReadString();
void ReadString(CString&);
// Write Operations
void WriteChar(const char& nData) { PutByte((BYTE)nData); };
void WriteByte(const BYTE& nData) { PutByte(nData); };
void WriteAngle(const float&);
void WriteAngle16(const float&);
void WriteShort(const short&);
void WriteLong(const long&);
void WriteFloat(const float&);
void WriteCoord(const float&);
void WriteVector(const CVector&);
void WriteString(const char*);
// Overrides
public:
void Serialize(CArchive&);
// Diagnostics
#ifdef _DEBUG
public:
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif //_DEBUG
};
/*
//////////////////////////////////////////////////////////////////////////////////
// CQuakePacket Inline Functions
///////////////////////////////////////////////////////////////////////////
// CQuakePacket Operations
inline void CQuakePacket::RemoveData(const WORD& nPosition, const WORD& nLength)
{
ASSERT(nPosition < GetSize());
if (m_nPosition > nPosition)
{
if (nLength > m_nPosition)
{
m_nPosition = 0;
}
else
{
m_nPosition -= nLength;
}
}
GetDataArray().RemoveAt(nPosition, nLength);
}
inline void CQuakePacket::PrependData(CQuakePacket& rOther)
{
MergeBuffer(rOther, 0);
}
inline void CQuakePacket::InsertData(CQuakePacket& rOther)
{
MergeBuffer(rOther, m_nPosition);
}
inline void CQuakePacket::Clear()
{
SetWriteMode();
m_nPosition = 0;
m_arrData.RemoveAll();
// Call the base class
CQuakeSystemObject::Clear();
}
inline void CQuakePacket::Copy(const CQuakePacket& rCopyMe)
{
m_bReadMode = rCopyMe.GetReadMode();
m_nPosition = rCopyMe.GetPosition();
m_arrData.RemoveAll();
m_arrData.SetSize(rCopyMe.GetData().GetSize());
m_arrData.Append(rCopyMe.GetData());
// Call the base class
CQuakeSystemObject::Copy(rCopyMe);
}
inline BOOL CQuakePacket::DataToRead() const
{
if ((m_bReadMode) && (m_nPosition < GetSize()))
{
return TRUE;
}
else
{
return FALSE;
}
}
//////////////////////////////////////////////////////////////////////////////
// CQuakePacket Overloaded Operators
inline CQuakePacket& CQuakePacket::operator=(const CQuakePacket& rCopyMe)
{
Copy(rCopyMe);
return (*this);
}
//////////////////////////////////////////////////////////////////////////////////
// Private Read/Write Functions
inline BYTE CQuakePacket::GetByte()
{
ASSERT(GetReadMode() == TRUE);
ASSERT(GetPosition() < GetDataArray().GetSize());
return GetDataArray().GetAt(m_nPosition++);
}
inline void CQuakePacket::PutByte(const BYTE& nData)
{
ASSERT(GetReadMode() == FALSE);
GetDataArray().SetAtGrow(m_nPosition++, nData);
}
//////////////////////////////////////////////////////////////////////////////////
// Read Functions
inline short CQuakePacket::ReadShort()
{
short nReturnMe;
ReadShort(nReturnMe);
return nReturnMe;
}
inline void CQuakePacket::ReadShort(short& nGet)
{
BYTE *pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(short); nCount++)
{
pTemp[nCount] = GetByte();
}
}
inline long CQuakePacket::ReadLong()
{
long nReturnMe;
ReadLong(nReturnMe);
return nReturnMe;
}
inline void CQuakePacket::ReadLong(long& nGet)
{
BYTE *pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(long); nCount++)
{
pTemp[nCount] = GetByte();
}
}
inline float CQuakePacket::ReadFloat()
{
float nReturnMe;
ReadFloat(nReturnMe);
return nReturnMe;
}
inline void CQuakePacket::ReadFloat(float& nGet)
{
BYTE *pTemp;
register int nCount;
pTemp = (BYTE *)&nGet;
for(nCount = 0; nCount < sizeof(float); nCount++)
{
pTemp[nCount] = GetByte();
}
}
inline float CQuakePacket::ReadCoord()
{
return ((float)((float)ReadShort() / (float)8.0));
}
inline void CQuakePacket::ReadCoord(float& nGet)
{
nGet = ReadCoord();
}
inline float CQuakePacket::ReadAngle()
// Angles are sent as a single byte (Scaled down from 360 to 256)
// NOTE: (360 / 256) = 1.40625
{
return ((float)((float)ReadChar() * (float)1.40625));
}
inline void CQuakePacket::ReadAngle(float& nGet)
{
nGet = ReadAngle();
}
inline CVector CQuakePacket::ReadVector()
{
CVector vReturnMe;
ReadVector(vReturnMe);
return vReturnMe;
}
inline void CQuakePacket::ReadVector(CVector& rReadMe)
{
rReadMe.SetX( ReadCoord() );
rReadMe.SetY( ReadCoord() );
rReadMe.SetZ( ReadCoord() );
}
inline CString CQuakePacket::ReadString()
{
CString strReturnMe;
ReadString(strReturnMe);
return strReturnMe;
}
inline void CQuakePacket::ReadString(CString& strReadMe)
{
register int nCounter = 0;
BYTE nTemp;
strReadMe.Empty();
nTemp = ReadByte();
if (nTemp > 128)
{
nTemp -= 128;
}
strReadMe += nTemp;
while (strReadMe[nCounter++] != '\0')
{
strReadMe += ReadChar();
}
}
//////////////////////////////////////////////////////////////////////////////////
// Write Operations
inline void CQuakePacket::WriteShort(const short& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE *)&nData;
for (nCount = 0; nCount < sizeof(short); nCount++)
{
PutByte(pTemp[nCount]);
}
}
inline void CQuakePacket::WriteLong(const long& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE*)&nData;
for (nCount = 0; nCount < sizeof(long); nCount++)
{
PutByte(pTemp[nCount]);
}
}
inline void CQuakePacket::WriteFloat(const float& nData)
{
BYTE* pTemp;
register int nCount;
pTemp = (BYTE *)&nData;
for (nCount = 0; nCount < sizeof(float); nCount++)
{
PutByte(pTemp[nCount]);
}
}
inline void CQuakePacket::WriteCoord(const float& nData)
{
WriteShort( ((short)(nData * 8)) );
}
inline void CQuakePacket::WriteAngle(const float& nData)
// Angles are sent as a single byte (Scaled down from 360 to 256)
// NOTE: (256 / 360) = 0.7111111111111
{
WriteByte((BYTE)(nData * 0.7111));
}
inline void CQuakePacket::WriteVector(const CVector& rData)
{
WriteCoord( rData.GetX() );
WriteCoord( rData.GetY() );
WriteCoord( rData.GetZ() );
}
inline void CQuakePacket::WriteString(const char* strData)
{
WORD nTemp;
VERIFY(strData != NULL);
nTemp = 0;
while (strData[nTemp] != '\0')
{
PutByte(strData[nTemp++]);
}
PutByte('\0');
}
//////////////////////////////////////////////////////////////////////////////////
*/
//////////////////////////////////////////////////////////////////////////////////
#endif
//////////////////////////////////////////////////////////////////////////////////