📄 common.c
字号:
}
if ( to->renderfx != from->renderfx )
{
if (to->renderfx < 256)
bits |= U_RENDERFX8;
else if (to->renderfx < 0x8000)
bits |= U_RENDERFX16;
else
bits |= U_RENDERFX8|U_RENDERFX16;
}
if ( to->solid != from->solid )
bits |= U_SOLID;
// event is not delta compressed, just 0 compressed
if ( to->event )
bits |= U_EVENT;
if ( to->modelindex != from->modelindex )
bits |= U_MODEL;
if ( to->modelindex2 != from->modelindex2 )
bits |= U_MODEL2;
if ( to->modelindex3 != from->modelindex3 )
bits |= U_MODEL3;
if ( to->modelindex4 != from->modelindex4 )
bits |= U_MODEL4;
if ( to->sound != from->sound )
bits |= U_SOUND;
if (newentity || (to->renderfx & RF_BEAM))
bits |= U_OLDORIGIN;
//
// write the message
//
if (!bits && !force)
return; // nothing to send!
//----------
if (bits & 0xff000000)
bits |= U_MOREBITS3 | U_MOREBITS2 | U_MOREBITS1;
else if (bits & 0x00ff0000)
bits |= U_MOREBITS2 | U_MOREBITS1;
else if (bits & 0x0000ff00)
bits |= U_MOREBITS1;
MSG_WriteByte (msg, bits&255 );
if (bits & 0xff000000)
{
MSG_WriteByte (msg, (bits>>8)&255 );
MSG_WriteByte (msg, (bits>>16)&255 );
MSG_WriteByte (msg, (bits>>24)&255 );
}
else if (bits & 0x00ff0000)
{
MSG_WriteByte (msg, (bits>>8)&255 );
MSG_WriteByte (msg, (bits>>16)&255 );
}
else if (bits & 0x0000ff00)
{
MSG_WriteByte (msg, (bits>>8)&255 );
}
//----------
if (bits & U_NUMBER16)
MSG_WriteShort (msg, to->number);
else
MSG_WriteByte (msg, to->number);
if (bits & U_MODEL)
MSG_WriteByte (msg, to->modelindex);
if (bits & U_MODEL2)
MSG_WriteByte (msg, to->modelindex2);
if (bits & U_MODEL3)
MSG_WriteByte (msg, to->modelindex3);
if (bits & U_MODEL4)
MSG_WriteByte (msg, to->modelindex4);
if (bits & U_FRAME8)
MSG_WriteByte (msg, to->frame);
if (bits & U_FRAME16)
MSG_WriteShort (msg, to->frame);
if ((bits & U_SKIN8) && (bits & U_SKIN16)) //used for laser colors
MSG_WriteLong (msg, to->skinnum);
else if (bits & U_SKIN8)
MSG_WriteByte (msg, to->skinnum);
else if (bits & U_SKIN16)
MSG_WriteShort (msg, to->skinnum);
if ( (bits & (U_EFFECTS8|U_EFFECTS16)) == (U_EFFECTS8|U_EFFECTS16) )
MSG_WriteLong (msg, to->effects);
else if (bits & U_EFFECTS8)
MSG_WriteByte (msg, to->effects);
else if (bits & U_EFFECTS16)
MSG_WriteShort (msg, to->effects);
if ( (bits & (U_RENDERFX8|U_RENDERFX16)) == (U_RENDERFX8|U_RENDERFX16) )
MSG_WriteLong (msg, to->renderfx);
else if (bits & U_RENDERFX8)
MSG_WriteByte (msg, to->renderfx);
else if (bits & U_RENDERFX16)
MSG_WriteShort (msg, to->renderfx);
if (bits & U_ORIGIN1)
MSG_WriteCoord (msg, to->origin[0]);
if (bits & U_ORIGIN2)
MSG_WriteCoord (msg, to->origin[1]);
if (bits & U_ORIGIN3)
MSG_WriteCoord (msg, to->origin[2]);
if (bits & U_ANGLE1)
MSG_WriteAngle(msg, to->angles[0]);
if (bits & U_ANGLE2)
MSG_WriteAngle(msg, to->angles[1]);
if (bits & U_ANGLE3)
MSG_WriteAngle(msg, to->angles[2]);
if (bits & U_OLDORIGIN)
{
MSG_WriteCoord (msg, to->old_origin[0]);
MSG_WriteCoord (msg, to->old_origin[1]);
MSG_WriteCoord (msg, to->old_origin[2]);
}
if (bits & U_SOUND)
MSG_WriteByte (msg, to->sound);
if (bits & U_EVENT)
MSG_WriteByte (msg, to->event);
if (bits & U_SOLID)
MSG_WriteShort (msg, to->solid);
}
//============================================================
//
// reading functions
//
void MSG_BeginReading (sizebuf_t *msg)
{
msg->readcount = 0;
}
// returns -1 if no more characters are available
int MSG_ReadChar (sizebuf_t *msg_read)
{
int c;
if (msg_read->readcount+1 > msg_read->cursize)
c = -1;
else
c = (signed char)msg_read->data[msg_read->readcount];
msg_read->readcount++;
return c;
}
int MSG_ReadByte (sizebuf_t *msg_read)
{
int c;
if (msg_read->readcount+1 > msg_read->cursize)
c = -1;
else
c = (unsigned char)msg_read->data[msg_read->readcount];
msg_read->readcount++;
return c;
}
int MSG_ReadShort (sizebuf_t *msg_read)
{
int c;
if (msg_read->readcount+2 > msg_read->cursize)
c = -1;
else
c = (short)(msg_read->data[msg_read->readcount]
+ (msg_read->data[msg_read->readcount+1]<<8));
msg_read->readcount += 2;
return c;
}
int MSG_ReadLong (sizebuf_t *msg_read)
{
int c;
if (msg_read->readcount+4 > msg_read->cursize)
c = -1;
else
c = msg_read->data[msg_read->readcount]
+ (msg_read->data[msg_read->readcount+1]<<8)
+ (msg_read->data[msg_read->readcount+2]<<16)
+ (msg_read->data[msg_read->readcount+3]<<24);
msg_read->readcount += 4;
return c;
}
float MSG_ReadFloat (sizebuf_t *msg_read)
{
union
{
byte b[4];
float f;
int l;
} dat;
if (msg_read->readcount+4 > msg_read->cursize)
dat.f = -1;
else
{
dat.b[0] = msg_read->data[msg_read->readcount];
dat.b[1] = msg_read->data[msg_read->readcount+1];
dat.b[2] = msg_read->data[msg_read->readcount+2];
dat.b[3] = msg_read->data[msg_read->readcount+3];
}
msg_read->readcount += 4;
dat.l = LittleLong (dat.l);
return dat.f;
}
char *MSG_ReadString (sizebuf_t *msg_read)
{
static char string[2048];
int l,c;
l = 0;
do
{
c = MSG_ReadChar (msg_read);
if (c == -1 || c == 0)
break;
string[l] = c;
l++;
} while (l < sizeof(string)-1);
string[l] = 0;
return string;
}
char *MSG_ReadStringLine (sizebuf_t *msg_read)
{
static char string[2048];
int l,c;
l = 0;
do
{
c = MSG_ReadChar (msg_read);
if (c == -1 || c == 0 || c == '\n')
break;
string[l] = c;
l++;
} while (l < sizeof(string)-1);
string[l] = 0;
return string;
}
float MSG_ReadCoord (sizebuf_t *msg_read)
{
return MSG_ReadShort(msg_read) * (1.0/8);
}
void MSG_ReadPos (sizebuf_t *msg_read, vec3_t pos)
{
pos[0] = MSG_ReadShort(msg_read) * (1.0/8);
pos[1] = MSG_ReadShort(msg_read) * (1.0/8);
pos[2] = MSG_ReadShort(msg_read) * (1.0/8);
}
float MSG_ReadAngle (sizebuf_t *msg_read)
{
return MSG_ReadChar(msg_read) * (360.0/256);
}
float MSG_ReadAngle16 (sizebuf_t *msg_read)
{
return SHORT2ANGLE(MSG_ReadShort(msg_read));
}
void MSG_ReadDeltaUsercmd (sizebuf_t *msg_read, usercmd_t *from, usercmd_t *move)
{
int bits;
memcpy (move, from, sizeof(*move));
bits = MSG_ReadByte (msg_read);
// read current angles
if (bits & CM_ANGLE1)
move->angles[0] = MSG_ReadShort (msg_read);
if (bits & CM_ANGLE2)
move->angles[1] = MSG_ReadShort (msg_read);
if (bits & CM_ANGLE3)
move->angles[2] = MSG_ReadShort (msg_read);
// read movement
if (bits & CM_FORWARD)
move->forwardmove = MSG_ReadShort (msg_read);
if (bits & CM_SIDE)
move->sidemove = MSG_ReadShort (msg_read);
if (bits & CM_UP)
move->upmove = MSG_ReadShort (msg_read);
// read buttons
if (bits & CM_BUTTONS)
move->buttons = MSG_ReadByte (msg_read);
if (bits & CM_IMPULSE)
move->impulse = MSG_ReadByte (msg_read);
// read time to run command
move->msec = MSG_ReadByte (msg_read);
// read the light level
move->lightlevel = MSG_ReadByte (msg_read);
}
void MSG_ReadData (sizebuf_t *msg_read, void *data, int len)
{
int i;
for (i=0 ; i<len ; i++)
((byte *)data)[i] = MSG_ReadByte (msg_read);
}
//===========================================================================
void SZ_Init (sizebuf_t *buf, byte *data, int length)
{
memset (buf, 0, sizeof(*buf));
buf->data = data;
buf->maxsize = length;
}
void SZ_Clear (sizebuf_t *buf)
{
buf->cursize = 0;
buf->overflowed = false;
}
void *SZ_GetSpace (sizebuf_t *buf, int length)
{
void *data;
if (buf->cursize + length > buf->maxsize)
{
if (!buf->allowoverflow)
Com_Error (ERR_FATAL, "SZ_GetSpace: overflow without allowoverflow set");
if (length > buf->maxsize)
Com_Error (ERR_FATAL, "SZ_GetSpace: %i is > full buffer size", length);
Com_Printf ("SZ_GetSpace: overflow\n");
SZ_Clear (buf);
buf->overflowed = true;
}
data = buf->data + buf->cursize;
buf->cursize += length;
return data;
}
void SZ_Write (sizebuf_t *buf, void *data, int length)
{
memcpy (SZ_GetSpace(buf,length),data,length);
}
void SZ_Print (sizebuf_t *buf, char *data)
{
int len;
len = strlen(data)+1;
if (buf->cursize)
{
if (buf->data[buf->cursize-1])
memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
else
memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
}
else
memcpy ((byte *)SZ_GetSpace(buf, len),data,len);
}
//============================================================================
/*
================
COM_CheckParm
Returns the position (1 to argc-1) in the program's argument list
where the given parameter apears, or 0 if not present
================
*/
int COM_CheckParm (char *parm)
{
int i;
for (i=1 ; i<com_argc ; i++)
{
if (!strcmp (parm,com_argv[i]))
return i;
}
return 0;
}
int COM_Argc (void)
{
return com_argc;
}
char *COM_Argv (int arg)
{
if (arg < 0 || arg >= com_argc || !com_argv[arg])
return "";
return com_argv[arg];
}
void COM_ClearArgv (int arg)
{
if (arg < 0 || arg >= com_argc || !com_argv[arg])
return;
com_argv[arg] = "";
}
/*
================
COM_InitArgv
================
*/
void COM_InitArgv (int argc, char **argv)
{
int i;
if (argc > MAX_NUM_ARGVS)
Com_Error (ERR_FATAL, "argc > MAX_NUM_ARGVS");
com_argc = argc;
for (i=0 ; i<argc ; i++)
{
if (!argv[i] || strlen(argv[i]) >= MAX_TOKEN_CHARS )
com_argv[i] = "";
else
com_argv[i] = argv[i];
}
}
/*
================
COM_AddParm
Adds the given string at the end of the current argument list
================
*/
void COM_AddParm (char *parm)
{
if (com_argc == MAX_NUM_ARGVS)
Com_Error (ERR_FATAL, "COM_AddParm: MAX_NUM)ARGS");
com_argv[com_argc++] = parm;
}
/// just for debugging
int memsearch (byte *start, int count, int search)
{
int i;
for (i=0 ; i<count ; i++)
if (start[i] == search)
return i;
return -1;
}
char *CopyString (char *in)
{
char *out;
out = Z_Malloc (strlen(in)+1);
strcpy (out, in);
return out;
}
void Info_Print (char *s)
{
char key[512];
char value[512];
char *o;
int l;
if (*s == '\\')
s++;
while (*s)
{
o = key;
while (*s && *s != '\\')
*o++ = *s++;
l = o - key;
if (l < 20)
{
memset (o, ' ', 20-l);
key[20] = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -