2000-05-20 09:42:02 +00:00
|
|
|
/*
|
|
|
|
msg.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2000-12-08 06:51:37 +00:00
|
|
|
# include "config.h"
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
2000-10-16 15:34:53 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#include "msg.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "qendian.h"
|
|
|
|
#include "sys.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============================================================================
|
|
|
|
|
|
|
|
MESSAGE IO FUNCTIONS
|
|
|
|
|
|
|
|
Handles byte ordering and avoids alignment errors
|
|
|
|
==============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// writing functions
|
|
|
|
//
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteChar (sizebuf_t *sb, int c)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
byte *buf;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifdef PARANOID
|
|
|
|
if (c < -128 || c > 127)
|
|
|
|
Sys_Error ("MSG_WriteChar: range error");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 1);
|
|
|
|
buf[0] = c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteByte (sizebuf_t *sb, int c)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
byte *buf;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifdef PARANOID
|
|
|
|
if (c < 0 || c > 255)
|
|
|
|
Sys_Error ("MSG_WriteByte: range error");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 1);
|
|
|
|
buf[0] = c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteShort (sizebuf_t *sb, int c)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
byte *buf;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifdef PARANOID
|
2000-12-08 06:51:37 +00:00
|
|
|
if (c < ((short) 0x8000) || c > (short) 0x7fff)
|
2000-05-20 09:42:02 +00:00
|
|
|
Sys_Error ("MSG_WriteShort: range error");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 2);
|
2000-12-08 06:51:37 +00:00
|
|
|
buf[0] = c & 0xff;
|
|
|
|
buf[1] = c >> 8;
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteLong (sizebuf_t *sb, int c)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
byte *buf;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
buf = SZ_GetSpace (sb, 4);
|
2000-12-08 06:51:37 +00:00
|
|
|
buf[0] = c & 0xff;
|
|
|
|
buf[1] = (c >> 8) & 0xff;
|
|
|
|
buf[2] = (c >> 16) & 0xff;
|
|
|
|
buf[3] = c >> 24;
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteFloat (sizebuf_t *sb, float f)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
int l;
|
2000-05-20 09:42:02 +00:00
|
|
|
} dat;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
dat.f = f;
|
|
|
|
dat.l = LittleLong (dat.l);
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
SZ_Write (sb, &dat.l, 4);
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteString (sizebuf_t *sb, char *s)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
SZ_Write (sb, "", 1);
|
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
SZ_Write (sb, s, strlen (s) + 1);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteCoord (sizebuf_t *sb, float f)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
MSG_WriteShort (sb, (int) (f * 8));
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteAngle (sizebuf_t *sb, float f)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
MSG_WriteByte (sb, (int) (f * 256 / 360) & 255);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteAngle16 (sizebuf_t *sb, float f)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
MSG_WriteShort (sb, (int) (f * 65536 / 360) & 65535);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_WriteDeltaUsercmd (sizebuf_t *buf, usercmd_t *from, usercmd_t *cmd)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int bits;
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// send the movement message
|
|
|
|
//
|
|
|
|
bits = 0;
|
|
|
|
if (cmd->angles[0] != from->angles[0])
|
|
|
|
bits |= CM_ANGLE1;
|
|
|
|
if (cmd->angles[1] != from->angles[1])
|
|
|
|
bits |= CM_ANGLE2;
|
|
|
|
if (cmd->angles[2] != from->angles[2])
|
|
|
|
bits |= CM_ANGLE3;
|
|
|
|
if (cmd->forwardmove != from->forwardmove)
|
|
|
|
bits |= CM_FORWARD;
|
|
|
|
if (cmd->sidemove != from->sidemove)
|
|
|
|
bits |= CM_SIDE;
|
|
|
|
if (cmd->upmove != from->upmove)
|
|
|
|
bits |= CM_UP;
|
|
|
|
if (cmd->buttons != from->buttons)
|
|
|
|
bits |= CM_BUTTONS;
|
|
|
|
if (cmd->impulse != from->impulse)
|
|
|
|
bits |= CM_IMPULSE;
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
MSG_WriteByte (buf, bits);
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
if (bits & CM_ANGLE1)
|
|
|
|
MSG_WriteAngle16 (buf, cmd->angles[0]);
|
|
|
|
if (bits & CM_ANGLE2)
|
|
|
|
MSG_WriteAngle16 (buf, cmd->angles[1]);
|
|
|
|
if (bits & CM_ANGLE3)
|
|
|
|
MSG_WriteAngle16 (buf, cmd->angles[2]);
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
if (bits & CM_FORWARD)
|
|
|
|
MSG_WriteShort (buf, cmd->forwardmove);
|
|
|
|
if (bits & CM_SIDE)
|
2000-12-08 06:51:37 +00:00
|
|
|
MSG_WriteShort (buf, cmd->sidemove);
|
2000-05-20 09:42:02 +00:00
|
|
|
if (bits & CM_UP)
|
|
|
|
MSG_WriteShort (buf, cmd->upmove);
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
if (bits & CM_BUTTONS)
|
|
|
|
MSG_WriteByte (buf, cmd->buttons);
|
|
|
|
if (bits & CM_IMPULSE)
|
|
|
|
MSG_WriteByte (buf, cmd->impulse);
|
2000-05-20 09:42:02 +00:00
|
|
|
MSG_WriteByte (buf, cmd->msec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// reading functions
|
|
|
|
//
|
2000-12-08 06:51:37 +00:00
|
|
|
int msg_readcount;
|
|
|
|
qboolean msg_badread;
|
2000-05-20 09:42:02 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_BeginReading (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
|
|
|
msg_readcount = 0;
|
|
|
|
msg_badread = false;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
int
|
|
|
|
MSG_GetReadCount (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
|
|
|
return msg_readcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns -1 and sets msg_badread if no more characters are available
|
2000-12-08 06:51:37 +00:00
|
|
|
int
|
|
|
|
MSG_ReadChar (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (msg_readcount + 1 > net_message.cursize) {
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
c = (signed char) net_message.data[msg_readcount];
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_readcount++;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
int
|
|
|
|
MSG_ReadByte (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (msg_readcount + 1 > net_message.cursize) {
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
c = (unsigned char) net_message.data[msg_readcount];
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_readcount++;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
int
|
|
|
|
MSG_ReadShort (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (msg_readcount + 2 > net_message.cursize) {
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
c = (short) (net_message.data[msg_readcount]
|
|
|
|
+ (net_message.data[msg_readcount + 1] << 8));
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_readcount += 2;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
int
|
|
|
|
MSG_ReadLong (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (msg_readcount + 4 > net_message.cursize) {
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
c = net_message.data[msg_readcount]
|
2000-12-08 06:51:37 +00:00
|
|
|
+ (net_message.data[msg_readcount + 1] << 8)
|
|
|
|
+ (net_message.data[msg_readcount + 2] << 16)
|
|
|
|
+ (net_message.data[msg_readcount + 3] << 24);
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_readcount += 4;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
float
|
|
|
|
MSG_ReadFloat (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
union {
|
|
|
|
byte b[4];
|
|
|
|
float f;
|
|
|
|
int l;
|
2000-05-20 09:42:02 +00:00
|
|
|
} dat;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
dat.b[0] = net_message.data[msg_readcount];
|
|
|
|
dat.b[1] = net_message.data[msg_readcount + 1];
|
|
|
|
dat.b[2] = net_message.data[msg_readcount + 2];
|
|
|
|
dat.b[3] = net_message.data[msg_readcount + 3];
|
2000-05-20 09:42:02 +00:00
|
|
|
msg_readcount += 4;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
dat.l = LittleLong (dat.l);
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
return dat.f;
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
char *
|
|
|
|
MSG_ReadString (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
static char string[2048];
|
|
|
|
int l, c;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
l = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
do {
|
2000-05-20 09:42:02 +00:00
|
|
|
c = MSG_ReadChar ();
|
|
|
|
if (c == -1 || c == 0)
|
|
|
|
break;
|
|
|
|
string[l] = c;
|
|
|
|
l++;
|
2000-12-08 06:51:37 +00:00
|
|
|
} while (l < sizeof (string) - 1);
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
string[l] = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
char *
|
|
|
|
MSG_ReadStringLine (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
static char string[2048];
|
|
|
|
int l, c;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
l = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
do {
|
2000-05-20 09:42:02 +00:00
|
|
|
c = MSG_ReadChar ();
|
|
|
|
if (c == -1 || c == 0 || c == '\n')
|
|
|
|
break;
|
|
|
|
string[l] = c;
|
|
|
|
l++;
|
2000-12-08 06:51:37 +00:00
|
|
|
} while (l < sizeof (string) - 1);
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
string[l] = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
float
|
|
|
|
MSG_ReadCoord (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
return MSG_ReadShort () * (1.0 / 8);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
float
|
|
|
|
MSG_ReadAngle (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
return MSG_ReadChar () * (360.0 / 256);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
float
|
|
|
|
MSG_ReadAngle16 (void)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
return MSG_ReadShort () * (360.0 / 65536);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
MSG_ReadDeltaUsercmd (usercmd_t *from, usercmd_t *move)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int bits;
|
2000-05-20 09:42:02 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
memcpy (move, from, sizeof (*move));
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
bits = MSG_ReadByte ();
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
// read current angles
|
|
|
|
if (bits & CM_ANGLE1)
|
|
|
|
move->angles[0] = MSG_ReadAngle16 ();
|
|
|
|
if (bits & CM_ANGLE2)
|
|
|
|
move->angles[1] = MSG_ReadAngle16 ();
|
|
|
|
if (bits & CM_ANGLE3)
|
|
|
|
move->angles[2] = MSG_ReadAngle16 ();
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
// read movement
|
|
|
|
if (bits & CM_FORWARD)
|
|
|
|
move->forwardmove = MSG_ReadShort ();
|
|
|
|
if (bits & CM_SIDE)
|
|
|
|
move->sidemove = MSG_ReadShort ();
|
|
|
|
if (bits & CM_UP)
|
|
|
|
move->upmove = MSG_ReadShort ();
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
// read buttons
|
|
|
|
if (bits & CM_BUTTONS)
|
|
|
|
move->buttons = MSG_ReadByte ();
|
|
|
|
|
|
|
|
if (bits & CM_IMPULSE)
|
|
|
|
move->impulse = MSG_ReadByte ();
|
|
|
|
|
|
|
|
// read time to run command
|
|
|
|
move->msec = MSG_ReadByte ();
|
|
|
|
}
|