2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2000-2020 DarkPlaces contributors
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ============================================================================
30 ============================================================================
34 float BuffBigFloat (const unsigned char *buffer)
42 u.i = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
46 int BuffBigLong (const unsigned char *buffer)
48 return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
51 short BuffBigShort (const unsigned char *buffer)
53 return (buffer[0] << 8) | buffer[1];
56 float BuffLittleFloat (const unsigned char *buffer)
64 u.i = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
68 int BuffLittleLong (const unsigned char *buffer)
70 return (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
73 short BuffLittleShort (const unsigned char *buffer)
75 return (buffer[1] << 8) | buffer[0];
78 void StoreBigLong (unsigned char *buffer, unsigned int i)
80 buffer[0] = (i >> 24) & 0xFF;
81 buffer[1] = (i >> 16) & 0xFF;
82 buffer[2] = (i >> 8) & 0xFF;
86 void StoreBigShort (unsigned char *buffer, unsigned short i)
88 buffer[0] = (i >> 8) & 0xFF;
92 void StoreLittleLong (unsigned char *buffer, unsigned int i)
95 buffer[1] = (i >> 8) & 0xFF;
96 buffer[2] = (i >> 16) & 0xFF;
97 buffer[3] = (i >> 24) & 0xFF;
100 void StoreLittleShort (unsigned char *buffer, unsigned short i)
102 buffer[0] = i & 0xFF;
103 buffer[1] = (i >> 8) & 0xFF;
107 ==============================================================================
111 Handles byte ordering and avoids alignment errors
112 ==============================================================================
119 void MSG_WriteChar (sizebuf_t *sb, int c)
123 buf = SZ_GetSpace (sb, 1);
127 void MSG_WriteByte (sizebuf_t *sb, int c)
131 buf = SZ_GetSpace (sb, 1);
135 void MSG_WriteShort (sizebuf_t *sb, int c)
139 buf = SZ_GetSpace (sb, 2);
144 void MSG_WriteLong (sizebuf_t *sb, int c)
148 buf = SZ_GetSpace (sb, 4);
150 buf[1] = (c>>8)&0xff;
151 buf[2] = (c>>16)&0xff;
155 void MSG_WriteFloat (sizebuf_t *sb, float f)
165 dat.l = LittleLong (dat.l);
167 SZ_Write (sb, (unsigned char *)&dat.l, 4);
170 void MSG_WriteString (sizebuf_t *sb, const char *s)
173 MSG_WriteChar (sb, 0);
175 SZ_Write (sb, (unsigned char *)s, (int)strlen(s)+1);
178 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s)
181 SZ_Write (sb, (unsigned char *)s, (int)strlen(s));
184 void MSG_WriteCoord13i (sizebuf_t *sb, float f)
186 MSG_WriteShort (sb, Q_rint(f*8));
189 void MSG_WriteCoord16i (sizebuf_t *sb, float f)
191 MSG_WriteShort (sb, Q_rint(f));
194 void MSG_WriteCoord32f (sizebuf_t *sb, float f)
196 MSG_WriteFloat (sb, f);
199 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol)
201 if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_QUAKEWORLD)
202 MSG_WriteCoord13i (sb, f);
203 else if (protocol == PROTOCOL_DARKPLACES1)
204 MSG_WriteCoord32f (sb, f);
205 else if (protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4)
206 MSG_WriteCoord16i (sb, f);
208 MSG_WriteCoord32f (sb, f);
211 void MSG_WriteVector (sizebuf_t *sb, const vec3_t v, protocolversion_t protocol)
213 MSG_WriteCoord (sb, v[0], protocol);
214 MSG_WriteCoord (sb, v[1], protocol);
215 MSG_WriteCoord (sb, v[2], protocol);
218 // LadyHavoc: round to nearest value, rather than rounding toward zero, fixes crosshair problem
219 void MSG_WriteAngle8i (sizebuf_t *sb, float f)
221 MSG_WriteByte (sb, (int)Q_rint(f*(256.0/360.0)) & 255);
224 void MSG_WriteAngle16i (sizebuf_t *sb, float f)
226 MSG_WriteShort (sb, (int)Q_rint(f*(65536.0/360.0)) & 65535);
229 void MSG_WriteAngle32f (sizebuf_t *sb, float f)
231 MSG_WriteFloat (sb, f);
234 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol)
236 if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_DARKPLACES1 || protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4 || protocol == PROTOCOL_QUAKEWORLD)
237 MSG_WriteAngle8i (sb, f);
239 MSG_WriteAngle16i (sb, f);
246 void MSG_InitReadBuffer (sizebuf_t *buf, unsigned char *data, int size)
248 memset(buf, 0, sizeof(*buf));
250 buf->maxsize = buf->cursize = size;
251 MSG_BeginReading(buf);
254 void MSG_BeginReading(sizebuf_t *sb)
260 int MSG_ReadLittleShort(sizebuf_t *sb)
262 if (sb->readcount+2 > sb->cursize)
268 return (short)(sb->data[sb->readcount-2] | (sb->data[sb->readcount-1]<<8));
271 int MSG_ReadBigShort (sizebuf_t *sb)
273 if (sb->readcount+2 > sb->cursize)
279 return (short)((sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1]);
282 int MSG_ReadLittleLong (sizebuf_t *sb)
284 if (sb->readcount+4 > sb->cursize)
290 return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
293 int MSG_ReadBigLong (sizebuf_t *sb)
295 if (sb->readcount+4 > sb->cursize)
301 return (sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1];
304 float MSG_ReadLittleFloat (sizebuf_t *sb)
311 if (sb->readcount+4 > sb->cursize)
317 dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
321 float MSG_ReadBigFloat (sizebuf_t *sb)
328 if (sb->readcount+4 > sb->cursize)
334 dat.l = (sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1];
338 char *MSG_ReadString (sizebuf_t *sb, char *string, size_t maxstring)
342 // read string into sbfer, but only store as many characters as will fit
343 while ((c = MSG_ReadByte(sb)) > 0)
344 if (l < maxstring - 1)
350 int MSG_ReadBytes (sizebuf_t *sb, int numbytes, unsigned char *out)
353 for (l = 0;l < numbytes && (c = MSG_ReadByte(sb)) != -1;l++)
358 float MSG_ReadCoord13i (sizebuf_t *sb)
360 return MSG_ReadLittleShort(sb) * (1.0/8.0);
363 float MSG_ReadCoord16i (sizebuf_t *sb)
365 return (signed short) MSG_ReadLittleShort(sb);
368 float MSG_ReadCoord32f (sizebuf_t *sb)
370 return MSG_ReadLittleFloat(sb);
373 float MSG_ReadCoord (sizebuf_t *sb, protocolversion_t protocol)
375 if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_QUAKEWORLD)
376 return MSG_ReadCoord13i(sb);
377 else if (protocol == PROTOCOL_DARKPLACES1)
378 return MSG_ReadCoord32f(sb);
379 else if (protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4)
380 return MSG_ReadCoord16i(sb);
382 return MSG_ReadCoord32f(sb);
385 void MSG_ReadVector (sizebuf_t *sb, vec3_t v, protocolversion_t protocol)
387 v[0] = MSG_ReadCoord(sb, protocol);
388 v[1] = MSG_ReadCoord(sb, protocol);
389 v[2] = MSG_ReadCoord(sb, protocol);
392 // LadyHavoc: round to nearest value, rather than rounding toward zero, fixes crosshair problem
393 float MSG_ReadAngle8i (sizebuf_t *sb)
395 return (signed char) MSG_ReadByte (sb) * (360.0/256.0);
398 float MSG_ReadAngle16i (sizebuf_t *sb)
400 return (signed short)MSG_ReadShort (sb) * (360.0/65536.0);
403 float MSG_ReadAngle32f (sizebuf_t *sb)
405 return MSG_ReadFloat (sb);
408 float MSG_ReadAngle (sizebuf_t *sb, protocolversion_t protocol)
410 if (protocol == PROTOCOL_QUAKE || protocol == PROTOCOL_QUAKEDP || protocol == PROTOCOL_NEHAHRAMOVIE || protocol == PROTOCOL_NEHAHRABJP || protocol == PROTOCOL_NEHAHRABJP2 || protocol == PROTOCOL_NEHAHRABJP3 || protocol == PROTOCOL_DARKPLACES1 || protocol == PROTOCOL_DARKPLACES2 || protocol == PROTOCOL_DARKPLACES3 || protocol == PROTOCOL_DARKPLACES4 || protocol == PROTOCOL_QUAKEWORLD)
411 return MSG_ReadAngle8i (sb);
413 return MSG_ReadAngle16i (sb);