home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  44.6 KB  |  2,321 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // common.c -- misc functions used in client and server
  21.  
  22. #include <ctype.h>
  23.  
  24. #ifdef SERVERONLY 
  25. #include "qwsvdef.h"
  26. #else
  27. #include "quakedef.h"
  28. #endif
  29.  
  30. #define MAX_NUM_ARGVS 50
  31. #define NUM_SAFE_ARGVS  6
  32.  
  33. usercmd_t nullcmd; // guarenteed to be zero
  34.  
  35. static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
  36. static char *argvdummy = " ";
  37.  
  38. static char *safeargvs[NUM_SAFE_ARGVS] =
  39.   {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse"};
  40.  
  41. cvar_t  registered = {"registered","0"};
  42.  
  43. qboolean  com_modified; // set true if using non-id files
  44.  
  45. int   static_registered = 1;  // only for startup check, then set
  46.  
  47. qboolean    msg_suppress_1 = 0;
  48.  
  49. void COM_InitFilesystem (void);
  50. void COM_Path_f (void);
  51.  
  52. #if defined(M68KASM) || defined(PPCASM)
  53. extern short ShortSwap(short);
  54. extern int LongSwap(int);
  55. extern float FloatSwap(float);
  56. #endif
  57.  
  58. // if a packfile directory differs from this, it is assumed to be hacked
  59. #define PAK0_COUNT    339
  60. #define PAK0_CRC    52883
  61.  
  62. qboolean    standard_quake = true, rogue, hipnotic;
  63.  
  64. char  gamedirfile[MAX_OSPATH];
  65.  
  66. // this graphic needs to be in the pak file to use registered features
  67. unsigned short pop[] =
  68. {
  69.  0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
  70. ,0x0000,0x0000,0x6600,0x0000,0x0000,0x0000,0x6600,0x0000
  71. ,0x0000,0x0066,0x0000,0x0000,0x0000,0x0000,0x0067,0x0000
  72. ,0x0000,0x6665,0x0000,0x0000,0x0000,0x0000,0x0065,0x6600
  73. ,0x0063,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6563
  74. ,0x0064,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6564
  75. ,0x0064,0x6564,0x0000,0x6469,0x6969,0x6400,0x0064,0x6564
  76. ,0x0063,0x6568,0x6200,0x0064,0x6864,0x0000,0x6268,0x6563
  77. ,0x0000,0x6567,0x6963,0x0064,0x6764,0x0063,0x6967,0x6500
  78. ,0x0000,0x6266,0x6769,0x6a68,0x6768,0x6a69,0x6766,0x6200
  79. ,0x0000,0x0062,0x6566,0x6666,0x6666,0x6666,0x6562,0x0000
  80. ,0x0000,0x0000,0x0062,0x6364,0x6664,0x6362,0x0000,0x0000
  81. ,0x0000,0x0000,0x0000,0x0062,0x6662,0x0000,0x0000,0x0000
  82. ,0x0000,0x0000,0x0000,0x0061,0x6661,0x0000,0x0000,0x0000
  83. ,0x0000,0x0000,0x0000,0x0000,0x6500,0x0000,0x0000,0x0000
  84. ,0x0000,0x0000,0x0000,0x0000,0x6400,0x0000,0x0000,0x0000
  85. };
  86.  
  87. /*
  88.  
  89.  
  90. All of Quake's data access is through a hierchal file system, but the contents of the file system can be transparently merged from several sources.
  91.  
  92. The "base directory" is the path to the directory holding the quake.exe and all game directories.  The sys_* files pass this to host_init in quakeparms_t->basedir.  This can be overridden with the "-basedir" command line parm to allow code debugging in a different directory.  The base directory is
  93. only used during filesystem initialization.
  94.  
  95. The "game directory" is the first tree on the search path and directory that all generated files (savegames, screenshots, demos, config files) will be saved to.  This can be overridden with the "-game" command line parameter.  The game directory can never be changed while quake is executing.  This is a precacution against having a malicious server instruct clients to write files over areas they shouldn't.
  96.  
  97. The "cache directory" is only used during development to save network bandwidth, especially over ISDN / T1 lines.  If there is a cache directory
  98. specified, when a file is found by the normal search path, it will be mirrored
  99. into the cache directory, then opened there.
  100.   
  101. */
  102.  
  103. //============================================================================
  104.  
  105.  
  106. // ClearLink is used for new headnodes
  107. void ClearLink (link_t *l)
  108. {
  109.   l->prev = l->next = l;
  110. }
  111.  
  112. void RemoveLink (link_t *l)
  113. {
  114.   l->next->prev = l->prev;
  115.   l->prev->next = l->next;
  116. }
  117.  
  118. void InsertLinkBefore (link_t *l, link_t *before)
  119. {
  120.   l->next = before;
  121.   l->prev = before->prev;
  122.   l->prev->next = l;
  123.   l->next->prev = l;
  124. }
  125. void InsertLinkAfter (link_t *l, link_t *after)
  126. {
  127.   l->next = after->next;
  128.   l->prev = after;
  129.   l->prev->next = l;
  130.   l->next->prev = l;
  131. }
  132.  
  133. /*
  134. ============================================================================
  135.  
  136.           LIBRARY REPLACEMENT FUNCTIONS
  137.  
  138. ============================================================================
  139. */
  140.  
  141. #if 0
  142. void Q_memset (void *dest, int fill, int count)
  143. {
  144.   int   i;
  145.   
  146.   if ( (((long)dest | count) & 3) == 0)
  147.   {
  148.     count >>= 2;
  149.     fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
  150.     for (i=0 ; i<count ; i++)
  151.       ((int *)dest)[i] = fill;
  152.   }
  153.   else
  154.     for (i=0 ; i<count ; i++)
  155.       ((byte *)dest)[i] = fill;
  156. }
  157.  
  158. void Q_memcpy (void *dest, void *src, int count)
  159. {
  160.   int   i;
  161.   
  162.   if (( ( (long)dest | (long)src | count) & 3) == 0 )
  163.   {
  164.     count>>=2;
  165.     for (i=0 ; i<count ; i++)
  166.       ((int *)dest)[i] = ((int *)src)[i];
  167.   }
  168.   else
  169.     for (i=0 ; i<count ; i++)
  170.       ((byte *)dest)[i] = ((byte *)src)[i];
  171. }
  172.  
  173. int Q_memcmp (void *m1, void *m2, int count)
  174. {
  175.   while(count)
  176.   {
  177.     count--;
  178.     if (((byte *)m1)[count] != ((byte *)m2)[count])
  179.       return -1;
  180.   }
  181.   return 0;
  182. }
  183.  
  184. void Q_strcpy (char *dest, char *src)
  185. {
  186.   while (*src)
  187.   {
  188.     *dest++ = *src++;
  189.   }
  190.   *dest++ = 0;
  191. }
  192.  
  193. void Q_strncpy (char *dest, char *src, int count)
  194. {
  195.   while (*src && count--)
  196.   {
  197.     *dest++ = *src++;
  198.   }
  199.   if (count)
  200.     *dest++ = 0;
  201. }
  202.  
  203. int Q_strlen (char *str)
  204. {
  205.   int   count;
  206.   
  207.   count = 0;
  208.   while (str[count])
  209.     count++;
  210.  
  211.   return count;
  212. }
  213.  
  214. char *Q_strrchr(char *s, char c)
  215. {
  216.     int len = Q_strlen(s);
  217.     s += len;
  218.     while (len--)
  219.         if (*--s == c) return s;
  220.     return 0;
  221. }
  222.  
  223. void Q_strcat (char *dest, char *src)
  224. {
  225.   dest += Q_strlen(dest);
  226.   Q_strcpy (dest, src);
  227. }
  228.  
  229. int Q_strcmp (char *s1, char *s2)
  230. {
  231.   while (1)
  232.   {
  233.     if (*s1 != *s2)
  234.       return -1;    // strings not equal  
  235.     if (!*s1)
  236.       return 0;   // strings are equal
  237.     s1++;
  238.     s2++;
  239.   }
  240.   
  241.   return -1;
  242. }
  243.  
  244. int Q_strncmp (char *s1, char *s2, int count)
  245. {
  246.   while (1)
  247.   {
  248.     if (!count--)
  249.       return 0;
  250.     if (*s1 != *s2)
  251.       return -1;    // strings not equal  
  252.     if (!*s1)
  253.       return 0;   // strings are equal
  254.     s1++;
  255.     s2++;
  256.   }
  257.   
  258.   return -1;
  259. }
  260.  
  261. int Q_strncasecmp (char *s1, char *s2, int n)
  262. {
  263.   int   c1, c2;
  264.   
  265.   while (1)
  266.   {
  267.     c1 = *s1++;
  268.     c2 = *s2++;
  269.  
  270.     if (!n--)
  271.       return 0;   // strings are equal until end point
  272.     
  273.     if (c1 != c2)
  274.     {
  275.       if (c1 >= 'a' && c1 <= 'z')
  276.         c1 -= ('a' - 'A');
  277.       if (c2 >= 'a' && c2 <= 'z')
  278.         c2 -= ('a' - 'A');
  279.       if (c1 != c2)
  280.         return -1;    // strings not equal
  281.     }
  282.     if (!c1)
  283.       return 0;   // strings are equal
  284. //    s1++;
  285. //    s2++;
  286.   }
  287.   
  288.   return -1;
  289. }
  290.  
  291. int Q_strcasecmp (char *s1, char *s2)
  292. {
  293.   return Q_strncasecmp (s1, s2, 99999);
  294. }
  295.  
  296. #endif
  297.  
  298. int Q_atoi (char *str)
  299. {
  300.   int   val;
  301.   int   sign;
  302.   int   c;
  303.   
  304.   if (*str == '-')
  305.   {
  306.     sign = -1;
  307.     str++;
  308.   }
  309.   else
  310.     sign = 1;
  311.     
  312.   val = 0;
  313.  
  314. //
  315. // check for hex
  316. //
  317.   if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  318.   {
  319.     str += 2;
  320.     while (1)
  321.     {
  322.       c = *str++;
  323.       if (c >= '0' && c <= '9')
  324.         val = (val<<4) + c - '0';
  325.       else if (c >= 'a' && c <= 'f')
  326.         val = (val<<4) + c - 'a' + 10;
  327.       else if (c >= 'A' && c <= 'F')
  328.         val = (val<<4) + c - 'A' + 10;
  329.       else
  330.         return val*sign;
  331.     }
  332.   }
  333.   
  334. //
  335. // check for character
  336. //
  337.   if (str[0] == '\'')
  338.   {
  339.     return sign * str[1];
  340.   }
  341.   
  342. //
  343. // assume decimal
  344. //
  345.   while (1)
  346.   {
  347.     c = *str++;
  348.     if (c <'0' || c > '9')
  349.       return val*sign;
  350.     val = val*10 + c - '0';
  351.   }
  352.   
  353.   return 0;
  354. }
  355.  
  356.  
  357. float Q_atof (char *str)
  358. {
  359.   double  val;
  360.   int   sign;
  361.   int   c;
  362.   int   decimal, total;
  363.   
  364.   if (*str == '-')
  365.   {
  366.     sign = -1;
  367.     str++;
  368.   }
  369.   else
  370.     sign = 1;
  371.     
  372.   val = 0;
  373.  
  374. //
  375. // check for hex
  376. //
  377.   if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  378.   {
  379.     str += 2;
  380.     while (1)
  381.     {
  382.       c = *str++;
  383.       if (c >= '0' && c <= '9')
  384.         val = (val*16) + c - '0';
  385.       else if (c >= 'a' && c <= 'f')
  386.         val = (val*16) + c - 'a' + 10;
  387.       else if (c >= 'A' && c <= 'F')
  388.         val = (val*16) + c - 'A' + 10;
  389.       else
  390.         return val*sign;
  391.     }
  392.   }
  393.   
  394. //
  395. // check for character
  396. //
  397.   if (str[0] == '\'')
  398.   {
  399.     return sign * str[1];
  400.   }
  401.   
  402. //
  403. // assume decimal
  404. //
  405.   decimal = -1;
  406.   total = 0;
  407.   while (1)
  408.   {
  409.     c = *str++;
  410.     if (c == '.')
  411.     {
  412.       decimal = total;
  413.       continue;
  414.     }
  415.     if (c <'0' || c > '9')
  416.       break;
  417.     val = val*10 + c - '0';
  418.     total++;
  419.   }
  420.  
  421.   if (decimal == -1)
  422.     return val*sign;
  423.   while (total > decimal)
  424.   {
  425.     val /= 10;
  426.     total--;
  427.   }
  428.   
  429.   return val*sign;
  430. }
  431.  
  432. /*
  433. ============================================================================
  434.  
  435.           BYTE ORDER FUNCTIONS
  436.  
  437. ============================================================================
  438. */
  439.  
  440. qboolean  bigendien;
  441.  
  442. short (*BigShort) (short l);
  443. short (*LittleShort) (short l);
  444. int (*BigLong) (int l);
  445. int (*LittleLong) (int l);
  446. float (*BigFloat) (float l);
  447. float (*LittleFloat) (float l);
  448.  
  449. #if !defined(M68KASM) && !defined(PPCASM)
  450. short   ShortSwap (short l)
  451. {
  452.   byte    b1,b2;
  453.  
  454.   b1 = l&255;
  455.   b2 = (l>>8)&255;
  456.  
  457.   return (b1<<8) + b2;
  458. }
  459.  
  460. int    LongSwap (int l)
  461. {
  462.   byte    b1,b2,b3,b4;
  463.  
  464.   b1 = l&255;
  465.   b2 = (l>>8)&255;
  466.   b3 = (l>>16)&255;
  467.   b4 = (l>>24)&255;
  468.  
  469.   return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  470. }
  471. #endif
  472.  
  473. short ShortNoSwap (short l)
  474. {
  475.   return l;
  476. }
  477.  
  478. int LongNoSwap (int l)
  479. {
  480.   return l;
  481. }
  482.  
  483. #if !defined(M68KASM) && !defined(PPCASM)
  484. float FloatSwap (float f)
  485. {
  486.   union
  487.   {
  488.     float f;
  489.     byte  b[4];
  490.   } dat1, dat2;
  491.   
  492.   
  493.   dat1.f = f;
  494.   dat2.b[0] = dat1.b[3];
  495.   dat2.b[1] = dat1.b[2];
  496.   dat2.b[2] = dat1.b[1];
  497.   dat2.b[3] = dat1.b[0];
  498.   return dat2.f;
  499. }
  500. #endif
  501.  
  502. float FloatNoSwap (float f)
  503. {
  504.   return f;
  505. }
  506.  
  507. /*
  508. ==============================================================================
  509.  
  510.       MESSAGE IO FUNCTIONS
  511.  
  512. Handles byte ordering and avoids alignment errors
  513. ==============================================================================
  514. */
  515.  
  516. //
  517. // writing functions
  518. //
  519.  
  520. void MSG_WriteChar (sizebuf_t *sb, int c)
  521. {
  522.   byte  *buf;
  523.   
  524. #ifdef PARANOID
  525.   if (c < -128 || c > 127)
  526.     Sys_Error ("MSG_WriteChar: range error");
  527. #endif
  528.  
  529.   buf = SZ_GetSpace (sb, 1);
  530.   buf[0] = c;
  531. }
  532.  
  533. void MSG_WriteByte (sizebuf_t *sb, int c)
  534. {
  535.   byte  *buf;
  536.   
  537. #ifdef PARANOID
  538.   if (c < 0 || c > 255)
  539.     Sys_Error ("MSG_WriteByte: range error");
  540. #endif
  541.  
  542.   buf = SZ_GetSpace (sb, 1);
  543.   buf[0] = c;
  544. }
  545.  
  546. void MSG_WriteShort (sizebuf_t *sb, int c)
  547. {
  548.   byte  *buf;
  549.   
  550. #ifdef PARANOID
  551.   if (c < ((short)0x8000) || c > (short)0x7fff)
  552.     Sys_Error ("MSG_WriteShort: range error");
  553. #endif
  554.  
  555.   buf = SZ_GetSpace (sb, 2);
  556.   buf[0] = c&0xff;
  557.   buf[1] = c>>8;
  558. }
  559.  
  560. void MSG_WriteLong (sizebuf_t *sb, int c)
  561. {
  562.   byte  *buf;
  563.   
  564.   buf = SZ_GetSpace (sb, 4);
  565.   buf[0] = c&0xff;
  566.   buf[1] = (c>>8)&0xff;
  567.   buf[2] = (c>>16)&0xff;
  568.   buf[3] = c>>24;
  569. }
  570.  
  571. void MSG_WriteFloat (sizebuf_t *sb, float f)
  572. {
  573.   union
  574.   {
  575.     float f;
  576.     int l;
  577.   } dat;
  578.   
  579.   
  580.   dat.f = f;
  581.   dat.l = LittleLong (dat.l);
  582.   
  583.   SZ_Write (sb, &dat.l, 4);
  584. }
  585.  
  586. void MSG_WriteString (sizebuf_t *sb, char *s)
  587. {
  588.   if (!s)
  589.     SZ_Write (sb, "", 1);
  590.   else
  591.     SZ_Write (sb, s, Q_strlen(s)+1);
  592. }
  593.  
  594. void MSG_WriteCoord (sizebuf_t *sb, float f)
  595. {
  596.   MSG_WriteShort (sb, (int)(f*8));
  597. }
  598.  
  599. void MSG_WriteAngle (sizebuf_t *sb, float f)
  600. {
  601.   MSG_WriteByte (sb, (int)(f*256/360) & 255);
  602. }
  603.  
  604. void MSG_WriteAngle16 (sizebuf_t *sb, float f)
  605. {
  606.   MSG_WriteShort (sb, (int)(f*65536/360) & 65535);
  607. }
  608.  
  609. void MSG_WriteDeltaUsercmd (sizebuf_t *buf, usercmd_t *from, usercmd_t *cmd)
  610. {
  611.   int   bits;
  612.  
  613. //
  614. // send the movement message
  615. //
  616.   bits = 0;
  617.   if (cmd->angles[0] != from->angles[0])
  618.     bits |= CM_ANGLE1;
  619.   if (cmd->angles[1] != from->angles[1])
  620.     bits |= CM_ANGLE2;
  621.   if (cmd->angles[2] != from->angles[2])
  622.     bits |= CM_ANGLE3;
  623.   if (cmd->forwardmove != from->forwardmove)
  624.     bits |= CM_FORWARD;
  625.   if (cmd->sidemove != from->sidemove)
  626.     bits |= CM_SIDE;
  627.   if (cmd->upmove != from->upmove)
  628.     bits |= CM_UP;
  629.   if (cmd->buttons != from->buttons)
  630.     bits |= CM_BUTTONS;
  631.   if (cmd->impulse != from->impulse)
  632.     bits |= CM_IMPULSE;
  633.  
  634.     MSG_WriteByte (buf, bits);
  635.  
  636.   if (bits & CM_ANGLE1)
  637.     MSG_WriteAngle16 (buf, cmd->angles[0]);
  638.   if (bits & CM_ANGLE2)
  639.     MSG_WriteAngle16 (buf, cmd->angles[1]);
  640.   if (bits & CM_ANGLE3)
  641.     MSG_WriteAngle16 (buf, cmd->angles[2]);
  642.   
  643.   if (bits & CM_FORWARD)
  644.     MSG_WriteShort (buf, cmd->forwardmove);
  645.   if (bits & CM_SIDE)
  646.       MSG_WriteShort (buf, cmd->sidemove);
  647.   if (bits & CM_UP)
  648.     MSG_WriteShort (buf, cmd->upmove);
  649.  
  650.   if (bits & CM_BUTTONS)
  651.       MSG_WriteByte (buf, cmd->buttons);
  652.   if (bits & CM_IMPULSE)
  653.       MSG_WriteByte (buf, cmd->impulse);
  654.   MSG_WriteByte (buf, cmd->msec);
  655. }
  656.  
  657.  
  658. //
  659. // reading functions
  660. //
  661. int     msg_readcount;
  662. qboolean  msg_badread;
  663.  
  664. void MSG_BeginReading (void)
  665. {
  666.   msg_readcount = 0;
  667.   msg_badread = false;
  668. }
  669.  
  670. int MSG_GetReadCount(void)
  671. {
  672.   return msg_readcount;
  673. }
  674.  
  675. // returns -1 and sets msg_badread if no more characters are available
  676. int MSG_ReadChar (void)
  677. {
  678.   int c;
  679.   
  680.   if (msg_readcount+1 > net_message.cursize)
  681.   {
  682.     msg_badread = true;
  683.     return -1;
  684.   }
  685.     
  686.   c = (signed char)net_message.data[msg_readcount];
  687.   msg_readcount++;
  688.   
  689.   return c;
  690. }
  691.  
  692. int MSG_ReadByte (void)
  693. {
  694.   int c;
  695.   
  696.   if (msg_readcount+1 > net_message.cursize)
  697.   {
  698.     msg_badread = true;
  699.     return -1;
  700.   }
  701.     
  702.   c = (unsigned char)net_message.data[msg_readcount];
  703.   msg_readcount++;
  704.   
  705.   return c;
  706. }
  707.  
  708. int MSG_ReadShort (void)
  709. {
  710.   int c;
  711.   
  712.   if (msg_readcount+2 > net_message.cursize)
  713.   {
  714.     msg_badread = true;
  715.     return -1;
  716.   }
  717.     
  718.   c = (short)(net_message.data[msg_readcount]
  719.   + (net_message.data[msg_readcount+1]<<8));
  720.   
  721.   msg_readcount += 2;
  722.   
  723.   return c;
  724. }
  725.  
  726. int MSG_ReadLong (void)
  727. {
  728.   int c;
  729.   
  730.   if (msg_readcount+4 > net_message.cursize)
  731.   {
  732.     msg_badread = true;
  733.     return -1;
  734.   }
  735.     
  736.   c = net_message.data[msg_readcount]
  737.   + (net_message.data[msg_readcount+1]<<8)
  738.   + (net_message.data[msg_readcount+2]<<16)
  739.   + (net_message.data[msg_readcount+3]<<24);
  740.   
  741.   msg_readcount += 4;
  742.   
  743.   return c;
  744. }
  745.  
  746. float MSG_ReadFloat (void)
  747. {
  748.   union
  749.   {
  750.     byte  b[4];
  751.     float f;
  752.     int l;
  753.   } dat;
  754.   
  755.   dat.b[0] =  net_message.data[msg_readcount];
  756.   dat.b[1] =  net_message.data[msg_readcount+1];
  757.   dat.b[2] =  net_message.data[msg_readcount+2];
  758.   dat.b[3] =  net_message.data[msg_readcount+3];
  759.   msg_readcount += 4;
  760.   
  761.   dat.l = LittleLong (dat.l);
  762.  
  763.   return dat.f; 
  764. }
  765.  
  766. char *MSG_ReadString (void)
  767. {
  768.   static char string[2048];
  769.   int   l,c;
  770.   
  771.   l = 0;
  772.   do
  773.   {
  774.     c = MSG_ReadChar ();
  775.     if (c == -1 || c == 0)
  776.       break;
  777.     string[l] = c;
  778.     l++;
  779.   } while (l < sizeof(string)-1);
  780.   
  781.   string[l] = 0;
  782.   
  783.   return string;
  784. }
  785.  
  786. char *MSG_ReadStringLine (void)
  787. {
  788.   static char string[2048];
  789.   int   l,c;
  790.   
  791.   l = 0;
  792.   do
  793.   {
  794.     c = MSG_ReadChar ();
  795.     if (c == -1 || c == 0 || c == '\n')
  796.       break;
  797.     string[l] = c;
  798.     l++;
  799.   } while (l < sizeof(string)-1);
  800.   
  801.   string[l] = 0;
  802.   
  803.   return string;
  804. }
  805.  
  806. float MSG_ReadCoord (void)
  807. {
  808.   return MSG_ReadShort() * (1.0/8);
  809. }
  810.  
  811. float MSG_ReadAngle (void)
  812. {
  813.   return MSG_ReadChar() * (360.0/256);
  814. }
  815.  
  816. float MSG_ReadAngle16 (void)
  817. {
  818.   return MSG_ReadShort() * (360.0/65536);
  819. }
  820.  
  821. void MSG_ReadDeltaUsercmd (usercmd_t *from, usercmd_t *move)
  822. {
  823.   int bits;
  824.  
  825.   memcpy (move, from, sizeof(*move));
  826.  
  827.   bits = MSG_ReadByte ();
  828.     
  829. // read current angles
  830.   if (bits & CM_ANGLE1)
  831.     move->angles[0] = MSG_ReadAngle16 ();
  832.   if (bits & CM_ANGLE2)
  833.     move->angles[1] = MSG_ReadAngle16 ();
  834.   if (bits & CM_ANGLE3)
  835.     move->angles[2] = MSG_ReadAngle16 ();
  836.     
  837. // read movement
  838.   if (bits & CM_FORWARD)
  839.     move->forwardmove = MSG_ReadShort ();
  840.   if (bits & CM_SIDE)
  841.     move->sidemove = MSG_ReadShort ();
  842.   if (bits & CM_UP)
  843.     move->upmove = MSG_ReadShort ();
  844.   
  845. // read buttons
  846.   if (bits & CM_BUTTONS)
  847.     move->buttons = MSG_ReadByte ();
  848.  
  849.   if (bits & CM_IMPULSE)
  850.     move->impulse = MSG_ReadByte ();
  851.  
  852. // read time to run command
  853.   move->msec = MSG_ReadByte ();
  854. }
  855.  
  856.  
  857. //===========================================================================
  858.  
  859. void SZ_Clear (sizebuf_t *buf)
  860. {
  861.   buf->cursize = 0;
  862.   buf->overflowed = false;
  863. }
  864.  
  865. void *SZ_GetSpace (sizebuf_t *buf, int length)
  866. {
  867.   void  *data;
  868.   
  869.   if (buf->cursize + length > buf->maxsize)
  870.   {
  871.     if (!buf->allowoverflow)
  872.       Sys_Error ("SZ_GetSpace: overflow without allowoverflow set (%d)", buf->maxsize);
  873.     
  874.     if (length > buf->maxsize)
  875.       Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
  876.       
  877.     Sys_Printf ("SZ_GetSpace: overflow\n"); // because Con_Printf may be redirected
  878.     SZ_Clear (buf); 
  879.     buf->overflowed = true;
  880.   }
  881.  
  882.   data = buf->data + buf->cursize;
  883.   buf->cursize += length;
  884.   
  885.   return data;
  886. }
  887.  
  888. void SZ_Write (sizebuf_t *buf, void *data, int length)
  889. {
  890.   Q_memcpy (SZ_GetSpace(buf,length),data,length);   
  891. }
  892.  
  893. void SZ_Print (sizebuf_t *buf, char *data)
  894. {
  895.   int   len;
  896.   
  897.   len = Q_strlen(data)+1;
  898.  
  899.   if (!buf->cursize || buf->data[buf->cursize-1])
  900.     Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
  901.   else
  902.     Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
  903. }
  904.  
  905.  
  906. //============================================================================
  907.  
  908.  
  909. /*
  910. ============
  911. COM_SkipPath
  912. ============
  913. */
  914. char *COM_SkipPath (char *pathname)
  915. {
  916.   char  *last;
  917.   
  918.   last = pathname;
  919.   while (*pathname)
  920.   {
  921.     if (*pathname=='/')
  922.       last = pathname+1;
  923.     pathname++;
  924.   }
  925.   return last;
  926. }
  927.  
  928. /*
  929. ============
  930. COM_StripExtension
  931. ============
  932. */
  933. void COM_StripExtension (char *in, char *out)
  934. {
  935.   while (*in && *in != '.')
  936.     *out++ = *in++;
  937.   *out = 0;
  938. }
  939.  
  940. /*
  941. ============
  942. COM_FileExtension
  943. ============
  944. */
  945. char *COM_FileExtension (char *in)
  946. {
  947.   static char exten[8];
  948.   int   i;
  949.  
  950.   while (*in && *in != '.')
  951.     in++;
  952.   if (!*in)
  953.     return "";
  954.   in++;
  955.   for (i=0 ; i<7 && *in ; i++,in++)
  956.     exten[i] = *in;
  957.   exten[i] = 0;
  958.   return exten;
  959. }
  960.  
  961. /*
  962. ============
  963. COM_FileBase
  964. ============
  965. */
  966. void COM_FileBase (char *in, char *out)
  967. {
  968.   char *s, *s2;
  969.   
  970.   s = in + strlen(in) - 1;
  971.   
  972.   while (s != in && *s != '.')
  973.     s--;
  974.   
  975.   for (s2 = s ; *s2 && *s2 != '/' ; s2--)
  976.   ;
  977.   
  978.   if (s-s2 < 2)
  979.     strcpy (out,"?model?");
  980.   else
  981.   {
  982.     s--;
  983.     strncpy (out,s2+1, s-s2);
  984.     out[s-s2] = 0;
  985.   }
  986. }
  987.  
  988.  
  989. /*
  990. ==================
  991. COM_DefaultExtension
  992. ==================
  993. */
  994. void COM_DefaultExtension (char *path, char *extension)
  995. {
  996.   char    *src;
  997. //
  998. // if path doesn't have a .EXT, append extension
  999. // (extension should include the .)
  1000. //
  1001.   src = path + strlen(path) - 1;
  1002.  
  1003.   while (*src != '/' && src != path)
  1004.   {
  1005.     if (*src == '.')
  1006.       return;                 // it has an extension
  1007.     src--;
  1008.   }
  1009.  
  1010.   strcat (path, extension);
  1011. }
  1012.  
  1013. //============================================================================
  1014.  
  1015. char    com_token[1024];
  1016. int   com_argc;
  1017. char  **com_argv;
  1018.  
  1019.  
  1020. /*
  1021. ==============
  1022. COM_Parse
  1023.  
  1024. Parse a token out of a string
  1025. ==============
  1026. */
  1027. char *COM_Parse (char *data)
  1028. {
  1029.   int   c;
  1030.   int   len;
  1031.   
  1032.   len = 0;
  1033.   com_token[0] = 0;
  1034.   
  1035.   if (!data)
  1036.     return NULL;
  1037.     
  1038. // skip whitespace
  1039. skipwhite:
  1040.   while ( (c = *data) <= ' ')
  1041.   {
  1042.     if (c == 0)
  1043.       return NULL;      // end of file;
  1044.     data++;
  1045.   }
  1046.   
  1047. // skip // comments
  1048.   if (c=='/' && data[1] == '/')
  1049.   {
  1050.     while (*data && *data != '\n')
  1051.       data++;
  1052.     goto skipwhite;
  1053.   }
  1054.   
  1055.  
  1056. // handle quoted strings specially
  1057.   if (c == '\"')
  1058.   {
  1059.     data++;
  1060.     while (1)
  1061.     {
  1062.       c = *data++;
  1063.       if (c=='\"' || !c)
  1064.       {
  1065.         com_token[len] = 0;
  1066.         return data;
  1067.       }
  1068.       com_token[len] = c;
  1069.       len++;
  1070.     }
  1071.   }
  1072.  
  1073. // parse a regular word
  1074.   do
  1075.   {
  1076.     com_token[len] = c;
  1077.     data++;
  1078.     len++;
  1079.     c = *data;
  1080.   } while (c>32);
  1081.   
  1082.   com_token[len] = 0;
  1083.   return data;
  1084. }
  1085.  
  1086.  
  1087. /*
  1088. ================
  1089. COM_CheckParm
  1090.  
  1091. Returns the position (1 to argc-1) in the program's argument list
  1092. where the given parameter apears, or 0 if not present
  1093. ================
  1094. */
  1095. int COM_CheckParm (char *parm)
  1096. {
  1097.   int   i;
  1098.   
  1099.   for (i=1 ; i<com_argc ; i++)
  1100.   {
  1101.     if (!com_argv[i])
  1102.       continue;   // NEXTSTEP sometimes clears appkit vars.
  1103.     if (!Q_strcmp (parm,com_argv[i]))
  1104.       return i;
  1105.   }
  1106.     
  1107.   return 0;
  1108. }
  1109.  
  1110. /*
  1111. ================
  1112. COM_CheckRegistered
  1113.  
  1114. Looks for the pop.txt file and verifies it.
  1115. Sets the "registered" cvar.
  1116. Immediately exits out if an alternate game was attempted to be started without
  1117. being registered.
  1118. ================
  1119. */
  1120. void COM_CheckRegistered (void)
  1121. {
  1122.   FILE    *h;
  1123.   unsigned short  check[128];
  1124.   int     i;
  1125.  
  1126.   COM_FOpenFile("gfx/pop.lmp", &h);
  1127.   static_registered = 0;
  1128.  
  1129.   if (!h)
  1130.   {
  1131.     Con_Printf ("Playing shareware version.\n");
  1132. #ifndef SERVERONLY
  1133. // FIXME DEBUG -- only temporary
  1134.     if (com_modified)
  1135.       Sys_Error ("You must have the registered version to play QuakeWorld");
  1136. #endif
  1137.     return;
  1138.   }
  1139.  
  1140.   fread (check, 1, sizeof(check), h);
  1141.   fclose (h);
  1142.   
  1143.   for (i=0 ; i<128 ; i++)
  1144.     if (pop[i] != (unsigned short)BigShort (check[i]))
  1145.       Sys_Error ("Corrupted data file.");
  1146.   
  1147.   Cvar_Set ("registered", "1");
  1148.   static_registered = 1;
  1149.   Con_Printf ("Playing registered version.\n");
  1150. }
  1151.  
  1152.  
  1153.  
  1154. /*
  1155. ================
  1156. COM_InitArgv
  1157. ================
  1158. */
  1159. void COM_InitArgv (int argc, char **argv)
  1160. {
  1161.   qboolean  safe;
  1162.   int     i;
  1163.  
  1164.   safe = false;
  1165.  
  1166.   for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
  1167.      com_argc++)
  1168.   {
  1169.     largv[com_argc] = argv[com_argc];
  1170.     if (!Q_strcmp ("-safe", argv[com_argc]))
  1171.       safe = true;
  1172.   }
  1173.  
  1174.   if (safe)
  1175.   {
  1176.   // force all the safe-mode switches. Note that we reserved extra space in
  1177.   // case we need to add these, so we don't need an overflow check
  1178.     for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
  1179.     {
  1180.       largv[com_argc] = safeargvs[i];
  1181.       com_argc++;
  1182.     }
  1183.   }
  1184.  
  1185.   largv[com_argc] = argvdummy;
  1186.   com_argv = largv;
  1187. }
  1188.  
  1189. /*
  1190. ================
  1191. COM_AddParm
  1192.  
  1193. Adds the given string at the end of the current argument list
  1194. ================
  1195. */
  1196. void COM_AddParm (char *parm)
  1197. {
  1198.   largv[com_argc++] = parm;
  1199. }
  1200.  
  1201.  
  1202. /*
  1203. ================
  1204. COM_Init
  1205. ================
  1206. */
  1207. void COM_Init (void)
  1208. {
  1209.   byte  swaptest[2] = {1,0};
  1210.  
  1211. // set the byte swapping variables in a portable manner 
  1212.   if ( *(short *)swaptest == 1)
  1213.   {
  1214.     bigendien = false;
  1215.     BigShort = ShortSwap;
  1216.     LittleShort = ShortNoSwap;
  1217.     BigLong = LongSwap;
  1218.     LittleLong = LongNoSwap;
  1219.     BigFloat = FloatSwap;
  1220.     LittleFloat = FloatNoSwap;
  1221.   }
  1222.   else
  1223.   {
  1224.     bigendien = true;
  1225.     BigShort = ShortNoSwap;
  1226.     LittleShort = ShortSwap;
  1227.     BigLong = LongNoSwap;
  1228.     LittleLong = LongSwap;
  1229.     BigFloat = FloatNoSwap;
  1230.     LittleFloat = FloatSwap;
  1231.   }
  1232.  
  1233.   Cvar_RegisterVariable (®istered);
  1234.   Cmd_AddCommand ("path", COM_Path_f);
  1235.  
  1236.   COM_InitFilesystem ();
  1237.   COM_CheckRegistered ();
  1238. }
  1239.  
  1240.  
  1241. /*
  1242. ============
  1243. va
  1244.  
  1245. does a varargs printf into a temp buffer, so I don't need to have
  1246. varargs versions of all text functions.
  1247. FIXME: make this buffer size safe someday
  1248. ============
  1249. */
  1250. char  *va(char *format, ...)
  1251. {
  1252.   va_list   argptr;
  1253.   static char   string[1024];
  1254.   
  1255.   va_start (argptr, format);
  1256.   vsprintf (string, format,argptr);
  1257.   va_end (argptr);
  1258.  
  1259.   return string;  
  1260. }
  1261.  
  1262.  
  1263. /// just for debugging
  1264. int memsearch (byte *start, int count, int search)
  1265. {
  1266.   int   i;
  1267.   
  1268.   for (i=0 ; i<count ; i++)
  1269.     if (start[i] == search)
  1270.       return i;
  1271.   return -1;
  1272. }
  1273.  
  1274. /*
  1275. =============================================================================
  1276.  
  1277. QUAKE FILESYSTEM
  1278.  
  1279. =============================================================================
  1280. */
  1281.  
  1282. int com_filesize;
  1283.  
  1284.  
  1285. //
  1286. // in memory
  1287. //
  1288.  
  1289. typedef struct
  1290. {
  1291.   char  name[MAX_QPATH];
  1292.   int   filepos, filelen;
  1293. } packfile_t;
  1294.  
  1295. typedef struct pack_s
  1296. {
  1297.   char  filename[MAX_OSPATH];
  1298.   FILE  *handle;
  1299.   int   numfiles;
  1300.   packfile_t  *files;
  1301. } pack_t;
  1302.  
  1303. //
  1304. // on disk
  1305. //
  1306. typedef struct
  1307. {
  1308.   char  name[56];
  1309.   int   filepos, filelen;
  1310. } dpackfile_t;
  1311.  
  1312. typedef struct
  1313. {
  1314.   char  id[4];
  1315.   int   dirofs;
  1316.   int   dirlen;
  1317. } dpackheader_t;
  1318.  
  1319. #define MAX_FILES_IN_PACK 2048
  1320.  
  1321. char  com_gamedir[MAX_OSPATH];
  1322. char  com_basedir[MAX_OSPATH];
  1323.  
  1324. typedef struct searchpath_s
  1325. {
  1326.   char  filename[MAX_OSPATH];
  1327.   pack_t  *pack;    // only one of filename / pack will be used
  1328.   struct searchpath_s *next;
  1329. } searchpath_t;
  1330.  
  1331. searchpath_t  *com_searchpaths;
  1332. searchpath_t  *com_base_searchpaths;  // without gamedirs
  1333.  
  1334. /*
  1335. ================
  1336. COM_filelength
  1337. ================
  1338. */
  1339. int COM_filelength (FILE *f)
  1340. {
  1341.   int   pos;
  1342.   int   end;
  1343.  
  1344.   pos = ftell (f);
  1345.   fseek (f, 0, SEEK_END);
  1346.   end = ftell (f);
  1347.   fseek (f, pos, SEEK_SET);
  1348.  
  1349.   return end;
  1350. }
  1351.  
  1352. int COM_FileOpenRead (char *path, FILE **hndl)
  1353. {
  1354.   FILE  *f;
  1355.  
  1356.   f = fopen(path, "rb");
  1357.   if (!f)
  1358.   {
  1359.     *hndl = NULL;
  1360.     return -1;
  1361.   }
  1362.   *hndl = f;
  1363.   
  1364.   return COM_filelength(f);
  1365. }
  1366.  
  1367. /*
  1368. ============
  1369. COM_Path_f
  1370.  
  1371. ============
  1372. */
  1373. void COM_Path_f (void)
  1374. {
  1375.   searchpath_t  *s;
  1376.   
  1377.   Con_Printf ("Current search path:\n");
  1378.   for (s=com_searchpaths ; s ; s=s->next)
  1379.   {
  1380.     if (s == com_base_searchpaths)
  1381.       Con_Printf ("----------\n");
  1382.     if (s->pack)
  1383.       Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
  1384.     else
  1385.       Con_Printf ("%s\n", s->filename);
  1386.   }
  1387. }
  1388.  
  1389. /*
  1390. ============
  1391. COM_WriteFile
  1392.  
  1393. The filename will be prefixed by the current game directory
  1394. ============
  1395. */
  1396. void COM_WriteFile (char *filename, void *data, int len)
  1397. {
  1398.   FILE  *f;
  1399.   char  name[MAX_OSPATH];
  1400.   
  1401.   sprintf (name, "%s/%s", com_gamedir, filename);
  1402.   
  1403.   f = fopen (name, "wb");
  1404.   if (!f) {
  1405.     Sys_mkdir(com_gamedir);
  1406.     f = fopen (name, "wb");
  1407.     if (!f)
  1408.       Sys_Error ("Error opening %s", filename);
  1409.   }
  1410.   
  1411.   Sys_Printf ("COM_WriteFile: %s\n", name);
  1412.   fwrite (data, 1, len, f);
  1413.   fclose (f);
  1414. }
  1415.  
  1416.  
  1417. /*
  1418. ============
  1419. COM_CreatePath
  1420.  
  1421. Only used for CopyFile and download
  1422. ============
  1423. */
  1424. void  COM_CreatePath (char *path)
  1425. {
  1426.   char  *ofs;
  1427.   
  1428.   for (ofs = path+1 ; *ofs ; ofs++)
  1429.   {
  1430.     if (*ofs == '/')
  1431.     { // create the directory
  1432.       *ofs = 0;
  1433.       Sys_mkdir (path);
  1434.       *ofs = '/';
  1435.     }
  1436.   }
  1437. }
  1438.  
  1439.  
  1440. /*
  1441. ===========
  1442. COM_CopyFile
  1443.  
  1444. Copies a file over from the net to the local cache, creating any directories
  1445. needed.  This is for the convenience of developers using ISDN from home.
  1446. ===========
  1447. */
  1448. void COM_CopyFile (char *netpath, char *cachepath)
  1449. {
  1450.   FILE  *in, *out;
  1451.   int   remaining, count;
  1452.   char  buf[4096];
  1453.   
  1454.   remaining = COM_FileOpenRead (netpath, &in);    
  1455.   COM_CreatePath (cachepath); // create directories up to the cache file
  1456.   out = fopen(cachepath, "wb");
  1457.   if (!out)
  1458.     Sys_Error ("Error opening %s", cachepath);
  1459.   
  1460.   while (remaining)
  1461.   {
  1462.     if (remaining < sizeof(buf))
  1463.       count = remaining;
  1464.     else
  1465.       count = sizeof(buf);
  1466.     fread (buf, 1, count, in);
  1467.     fwrite (buf, 1, count, out);
  1468.     remaining -= count;
  1469.   }
  1470.  
  1471.   fclose (in);
  1472.   fclose (out);
  1473. }
  1474.  
  1475. /*
  1476. ===========
  1477. COM_FindFile
  1478.  
  1479. Finds the file in the search path.
  1480. Sets com_filesize and one of handle or file
  1481. ===========
  1482. */
  1483. int file_from_pak; // global indicating file came from pack file ZOID
  1484.  
  1485. int COM_FOpenFile (char *filename, FILE **file)
  1486. {
  1487.   searchpath_t  *search;
  1488.   char    netpath[MAX_OSPATH];
  1489.   pack_t    *pak;
  1490.   int     i;
  1491.   int     findtime;
  1492.  
  1493.   file_from_pak = 0;
  1494.     
  1495. //
  1496. // search through the path, one element at a time
  1497. //
  1498.   for (search = com_searchpaths ; search ; search = search->next)
  1499.   {
  1500.   // is the element a pak file?
  1501.     if (search->pack)
  1502.     {
  1503.     // look through all the pak file elements
  1504.       pak = search->pack;
  1505.       for (i=0 ; i<pak->numfiles ; i++)
  1506.         if (!strcmp (pak->files[i].name, filename))
  1507.         { // found it!
  1508.           Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
  1509.         // open a new file on the pakfile
  1510.           *file = fopen (pak->filename, "rb");
  1511.           if (!*file)
  1512.             Sys_Error ("Couldn't reopen %s", pak->filename);  
  1513.           fseek (*file, pak->files[i].filepos, SEEK_SET);
  1514.           com_filesize = pak->files[i].filelen;
  1515.           file_from_pak = 1;
  1516.           return com_filesize;
  1517.         }
  1518.     }
  1519.     else
  1520.     {   
  1521.   // check a file in the directory tree
  1522.       if (!static_registered)
  1523.       { // if not a registered version, don't ever go beyond base
  1524.         if ( strchr (filename, '/') || strchr (filename,'\\'))
  1525.           continue;
  1526.       }
  1527.       
  1528.       sprintf (netpath, "%s/%s",search->filename, filename);
  1529.       
  1530.       findtime = Sys_FileTime (netpath);
  1531.       if (findtime == -1)
  1532.         continue;
  1533.         
  1534.       Sys_Printf ("FindFile: %s\n",netpath);
  1535.  
  1536.       *file = fopen (netpath, "rb");
  1537.       return COM_filelength (*file);
  1538.     }
  1539.     
  1540.   }
  1541.   
  1542.   Sys_Printf ("FindFile: can't find %s\n", filename);
  1543.   
  1544.   *file = NULL;
  1545.   com_filesize = -1;
  1546.   return -1;
  1547. }
  1548.  
  1549. /*
  1550. ============
  1551. COM_LoadFile
  1552.  
  1553. Filename are reletive to the quake directory.
  1554. Allways appends a 0 byte to the loaded data.
  1555. ============
  1556. */
  1557. cache_user_t *loadcache;
  1558. byte  *loadbuf;
  1559. int   loadsize;
  1560. byte *COM_LoadFile (char *path, int usehunk)
  1561. {
  1562.   FILE  *h;
  1563.   byte  *buf;
  1564.   char  base[32];
  1565.   int   len;
  1566.  
  1567.   buf = NULL; // quiet compiler warning
  1568.  
  1569. // look for it in the filesystem or pack files
  1570.   len = com_filesize = COM_FOpenFile (path, &h);
  1571.   if (!h)
  1572.     return NULL;
  1573.   
  1574. // extract the filename base name for hunk tag
  1575.   COM_FileBase (path, base);
  1576.   
  1577.   if (usehunk == 1)
  1578.     buf = Hunk_AllocName (len+1, base);
  1579.   else if (usehunk == 2)
  1580.     buf = Hunk_TempAlloc (len+1);
  1581.   else if (usehunk == 0)
  1582.     buf = Z_Malloc (len+1);
  1583.   else if (usehunk == 3)
  1584.     buf = Cache_Alloc (loadcache, len+1, base);
  1585.   else if (usehunk == 4)
  1586.   {
  1587.     if (len+1 > loadsize)
  1588.       buf = Hunk_TempAlloc (len+1);
  1589.     else
  1590.       buf = loadbuf;
  1591.   }
  1592.   else
  1593.     Sys_Error ("COM_LoadFile: bad usehunk");
  1594.  
  1595.   if (!buf)
  1596.     Sys_Error ("COM_LoadFile: not enough space for %s", path);
  1597.     
  1598.   ((byte *)buf)[len] = 0;
  1599. #ifndef SERVERONLY
  1600.   Draw_BeginDisc ();
  1601. #endif
  1602.   fread (buf, 1, len, h);
  1603.   fclose (h);
  1604. #ifndef SERVERONLY
  1605.   Draw_EndDisc ();
  1606. #endif
  1607.  
  1608.   return buf;
  1609. }
  1610.  
  1611. byte *COM_LoadHunkFile (char *path)
  1612. {
  1613.   return COM_LoadFile (path, 1);
  1614. }
  1615.  
  1616. byte *COM_LoadTempFile (char *path)
  1617. {
  1618.   return COM_LoadFile (path, 2);
  1619. }
  1620.  
  1621. void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
  1622. {
  1623.   loadcache = cu;
  1624.   COM_LoadFile (path, 3);
  1625. }
  1626.  
  1627. // uses temp hunk if larger than bufsize
  1628. byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
  1629. {
  1630.   byte  *buf;
  1631.   
  1632.   loadbuf = (byte *)buffer;
  1633.   loadsize = bufsize;
  1634.   buf = COM_LoadFile (path, 4);
  1635.   
  1636.   return buf;
  1637. }
  1638.  
  1639. /*
  1640. =================
  1641. COM_LoadPackFile
  1642.  
  1643. Takes an explicit (not game tree related) path to a pak file.
  1644.  
  1645. Loads the header and directory, adding the files at the beginning
  1646. of the list so they override previous pack files.
  1647. =================
  1648. */
  1649. pack_t *COM_LoadPackFile (char *packfile)
  1650. {
  1651.   dpackheader_t header;
  1652.   int       i;
  1653.   packfile_t    *newfiles;
  1654.   int       numpackfiles;
  1655.   pack_t      *pack;
  1656.   FILE      *packhandle;
  1657.   dpackfile_t   info[MAX_FILES_IN_PACK];
  1658.   unsigned short    crc;
  1659.  
  1660.   if (COM_FileOpenRead (packfile, &packhandle) == -1)
  1661.     return NULL;
  1662.  
  1663.   fread (&header, 1, sizeof(header), packhandle);
  1664.   if (header.id[0] != 'P' || header.id[1] != 'A'
  1665.   || header.id[2] != 'C' || header.id[3] != 'K')
  1666.     Sys_Error ("%s is not a packfile", packfile);
  1667.   header.dirofs = LittleLong (header.dirofs);
  1668.   header.dirlen = LittleLong (header.dirlen);
  1669.  
  1670.   numpackfiles = header.dirlen / sizeof(dpackfile_t);
  1671.  
  1672.   if (numpackfiles > MAX_FILES_IN_PACK)
  1673.     Sys_Error ("%s has %i files", packfile, numpackfiles);
  1674.  
  1675.   if (numpackfiles != PAK0_COUNT)
  1676.     com_modified = true;  // not the original file
  1677.  
  1678.   newfiles = Z_Malloc (numpackfiles * sizeof(packfile_t));
  1679.  
  1680.   fseek (packhandle, header.dirofs, SEEK_SET);
  1681.   fread (&info, 1, header.dirlen, packhandle);
  1682.  
  1683. // crc the directory to check for modifications
  1684.   crc = CRC_Block((byte *)info, header.dirlen);
  1685.  
  1686. //  CRC_Init (&crc);
  1687. //  for (i=0 ; i<header.dirlen ; i++)
  1688. //    CRC_ProcessByte (&crc, ((byte *)info)[i]);
  1689.   if (crc != PAK0_CRC)
  1690.     com_modified = true;
  1691.  
  1692. // parse the directory
  1693.   for (i=0 ; i<numpackfiles ; i++)
  1694.   {
  1695.     strcpy (newfiles[i].name, info[i].name);
  1696.     newfiles[i].filepos = LittleLong(info[i].filepos);
  1697.     newfiles[i].filelen = LittleLong(info[i].filelen);
  1698.   }
  1699.  
  1700.   pack = Z_Malloc (sizeof (pack_t));
  1701.   strcpy (pack->filename, packfile);
  1702.   pack->handle = packhandle;
  1703.   pack->numfiles = numpackfiles;
  1704.   pack->files = newfiles;
  1705.   
  1706.   Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
  1707.   return pack;
  1708. }
  1709.  
  1710.  
  1711. /*
  1712. ================
  1713. COM_AddGameDirectory
  1714.  
  1715. Sets com_gamedir, adds the directory to the head of the path,
  1716. then loads and adds pak1.pak pak2.pak ... 
  1717. ================
  1718. */
  1719. void COM_AddGameDirectory (char *dir)
  1720. {
  1721.   int       i;
  1722.   searchpath_t  *search;
  1723.   pack_t      *pak;
  1724.   char      pakfile[MAX_OSPATH];
  1725.   char      *p;
  1726.  
  1727.   if ((p = strrchr(dir, '/')) != NULL)
  1728.     strcpy(gamedirfile, ++p);
  1729. #ifdef AMIGA
  1730.   else if ((p = strrchr(dir, ':')) != NULL)  /*phx*/
  1731.     strcpy(gamedirfile, ++p);
  1732. #endif
  1733.   else
  1734.     strcpy(gamedirfile, dir);  /*fixed, phx*/
  1735.   strcpy (com_gamedir, dir);
  1736.  
  1737. //
  1738. // add the directory to the search path
  1739. //
  1740.   search = Hunk_Alloc (sizeof(searchpath_t));
  1741.   strcpy (search->filename, dir);
  1742.   search->next = com_searchpaths;
  1743.   com_searchpaths = search;
  1744.  
  1745. //
  1746. // add any pak files in the format pak0.pak pak1.pak, ...
  1747. //
  1748.   for (i=0 ; ; i++)
  1749.   {
  1750.     sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1751.     pak = COM_LoadPackFile (pakfile);
  1752.     if (!pak)
  1753.       break;
  1754.     search = Hunk_Alloc (sizeof(searchpath_t));
  1755.     search->pack = pak;
  1756.     search->next = com_searchpaths;
  1757.     com_searchpaths = search;   
  1758.   }
  1759.  
  1760. }
  1761.  
  1762. /*
  1763. ================
  1764. COM_Gamedir
  1765.  
  1766. Sets the gamedir and path to a different directory.
  1767. ================
  1768. */
  1769. void COM_Gamedir (char *dir)
  1770. {
  1771.   searchpath_t  *search, *next;
  1772.   int       i;
  1773.   pack_t      *pak;
  1774.   char      pakfile[MAX_OSPATH];
  1775.  
  1776.   if (strstr(dir, "..") || strstr(dir, "/")
  1777.     || strstr(dir, "\\") || strstr(dir, ":") )
  1778.   {
  1779.     Con_Printf ("Gamedir should be a single filename, not a path\n");
  1780.     return;
  1781.   }
  1782.  
  1783.   if (!strcmp(gamedirfile, dir))
  1784.     return;   // still the same
  1785.   strcpy (gamedirfile, dir);
  1786.  
  1787.   //
  1788.   // free up any current game dir info
  1789.   //
  1790.   while (com_searchpaths != com_base_searchpaths)
  1791.   {
  1792.     if (com_searchpaths->pack)
  1793.     {
  1794.       fclose (com_searchpaths->pack->handle);
  1795.       Z_Free (com_searchpaths->pack->files);
  1796.       Z_Free (com_searchpaths->pack);
  1797.     }
  1798.     next = com_searchpaths->next;
  1799.     Z_Free (com_searchpaths);
  1800.     com_searchpaths = next;
  1801.   }
  1802.  
  1803.   //
  1804.   // flush all data, so it will be forced to reload
  1805.   //
  1806.   Cache_Flush ();
  1807.  
  1808.   if (!strcmp(dir,"id1") || !strcmp(dir, "qw"))
  1809.     return;
  1810.  
  1811.   sprintf (com_gamedir, "%s%s", com_basedir, dir);  /*phx*/
  1812.  
  1813.   //
  1814.   // add the directory to the search path
  1815.   //
  1816.   search = Z_Malloc (sizeof(searchpath_t));
  1817.   strcpy (search->filename, com_gamedir);
  1818.   search->next = com_searchpaths;
  1819.   com_searchpaths = search;
  1820.  
  1821.   //
  1822.   // add any pak files in the format pak0.pak pak1.pak, ...
  1823.   //
  1824.   for (i=0 ; ; i++)
  1825.   {
  1826.     sprintf (pakfile, "%s/pak%i.pak", com_gamedir, i);
  1827.     pak = COM_LoadPackFile (pakfile);
  1828.     if (!pak)
  1829.       break;
  1830.     search = Z_Malloc (sizeof(searchpath_t));
  1831.     search->pack = pak;
  1832.     search->next = com_searchpaths;
  1833.     com_searchpaths = search;   
  1834.   }
  1835. }
  1836.  
  1837. /*
  1838. ================
  1839. COM_InitFilesystem
  1840. ================
  1841. */
  1842. void COM_InitFilesystem (void)
  1843. {
  1844.   int   i;
  1845.  
  1846. //
  1847. // -basedir <path>
  1848. // Overrides the system supplied base directory (under id1)
  1849. //
  1850.   i = COM_CheckParm ("-basedir");
  1851.   if (i && i < com_argc-1)
  1852.     strcpy (com_basedir, com_argv[i+1]);
  1853.   else
  1854.     strcpy (com_basedir, host_parms.basedir);
  1855.  
  1856. // (phx) Make sure that basedir path is terminated by '/' or ':'
  1857.   i = strlen(com_basedir);
  1858.   if (com_basedir[i-1]!='/' && com_basedir[i-1]!=':'
  1859.       && com_basedir[i-1]!='\\') {
  1860.     com_basedir[i] = '/';
  1861.     com_basedir[i+1] = 0;
  1862.   }
  1863.  
  1864. //
  1865. // start up with id1 by default
  1866. //
  1867.   COM_AddGameDirectory (va("%sid1", com_basedir) ); /*phx*/
  1868.   COM_AddGameDirectory (va("%sqw", com_basedir) ); /*phx*/
  1869.  
  1870.   // any set gamedirs will be freed up to here
  1871.   com_base_searchpaths = com_searchpaths;
  1872. }
  1873.  
  1874.  
  1875.  
  1876. /*
  1877. =====================================================================
  1878.  
  1879.   INFO STRINGS
  1880.  
  1881. =====================================================================
  1882. */
  1883.  
  1884. /*
  1885. ===============
  1886. Info_ValueForKey
  1887.  
  1888. Searches the string for the given
  1889. key and returns the associated value, or an empty string.
  1890. ===============
  1891. */
  1892. char *Info_ValueForKey (char *s, char *key)
  1893. {
  1894.   char  pkey[512];
  1895.   static  char value[4][512]; // use two buffers so compares
  1896.                 // work without stomping on each other
  1897.   static  int valueindex;
  1898.   char  *o;
  1899.   
  1900.   valueindex = (valueindex + 1) % 4;
  1901.   if (*s == '\\')
  1902.     s++;
  1903.   while (1)
  1904.   {
  1905.     o = pkey;
  1906.     while (*s != '\\')
  1907.     {
  1908.       if (!*s)
  1909.         return "";
  1910.       *o++ = *s++;
  1911.     }
  1912.     *o = 0;
  1913.     s++;
  1914.  
  1915.     o = value[valueindex];
  1916.  
  1917.     while (*s != '\\' && *s)
  1918.     {
  1919.       if (!*s)
  1920.         return "";
  1921.       *o++ = *s++;
  1922.     }
  1923.     *o = 0;
  1924.  
  1925.     if (!strcmp (key, pkey) )
  1926.       return value[valueindex];
  1927.  
  1928.     if (!*s)
  1929.       return "";
  1930.     s++;
  1931.   }
  1932. }
  1933.  
  1934. void Info_RemoveKey (char *s, char *key)
  1935. {
  1936.   char  *start;
  1937.   char  pkey[512];
  1938.   char  value[512];
  1939.   char  *o;
  1940.  
  1941.   if (strstr (key, "\\"))
  1942.   {
  1943.     Con_Printf ("Can't use a key with a \\\n");
  1944.     return;
  1945.   }
  1946.  
  1947.   while (1)
  1948.   {
  1949.     start = s;
  1950.     if (*s == '\\')
  1951.       s++;
  1952.     o = pkey;
  1953.     while (*s != '\\')
  1954.     {
  1955.       if (!*s)
  1956.         return;
  1957.       *o++ = *s++;
  1958.     }
  1959.     *o = 0;
  1960.     s++;
  1961.  
  1962.     o = value;
  1963.     while (*s != '\\' && *s)
  1964.     {
  1965.       if (!*s)
  1966.         return;
  1967.       *o++ = *s++;
  1968.     }
  1969.     *o = 0;
  1970.  
  1971.     if (!strcmp (key, pkey) )
  1972.     {
  1973.       strcpy (start, s);  // remove this part
  1974.       return;
  1975.     }
  1976.  
  1977.     if (!*s)
  1978.       return;
  1979.   }
  1980.  
  1981. }
  1982.  
  1983. void Info_RemovePrefixedKeys (char *start, char prefix)
  1984. {
  1985.   char  *s;
  1986.   char  pkey[512];
  1987.   char  value[512];
  1988.   char  *o;
  1989.  
  1990.   s = start;
  1991.  
  1992.   while (1)
  1993.   {
  1994.     if (*s == '\\')
  1995.       s++;
  1996.     o = pkey;
  1997.     while (*s != '\\')
  1998.     {
  1999.       if (!*s)
  2000.         return;
  2001.       *o++ = *s++;
  2002.     }
  2003.     *o = 0;
  2004.     s++;
  2005.  
  2006.     o = value;
  2007.     while (*s != '\\' && *s)
  2008.     {
  2009.       if (!*s)
  2010.         return;
  2011.       *o++ = *s++;
  2012.     }
  2013.     *o = 0;
  2014.  
  2015.     if (pkey[0] == prefix)
  2016.     {
  2017.       Info_RemoveKey (start, pkey);
  2018.       s = start;
  2019.     }
  2020.  
  2021.     if (!*s)
  2022.       return;
  2023.   }
  2024.  
  2025. }
  2026.  
  2027.  
  2028. void Info_SetValueForStarKey (char *s, char *key, char *value, int maxsize)
  2029. {
  2030.   char  new[1024], *v;
  2031.   int   c;
  2032. #ifdef SERVERONLY
  2033.   extern cvar_t sv_highchars;
  2034. #endif
  2035.  
  2036.   if (strstr (key, "\\") || strstr (value, "\\") )
  2037.   {
  2038.     Con_Printf ("Can't use keys or values with a \\\n");
  2039.     return;
  2040.   }
  2041.  
  2042.   if (strstr (key, "\"") || strstr (value, "\"") )
  2043.   {
  2044.     Con_Printf ("Can't use keys or values with a \"\n");
  2045.     return;
  2046.   }
  2047.  
  2048.   if (strlen(key) > 63 || strlen(value) > 63)
  2049.   {
  2050.     Con_Printf ("Keys and values must be < 64 characters.\n");
  2051.     return;
  2052.   }
  2053.  
  2054.   // this next line is kinda trippy
  2055.   if (*(v = Info_ValueForKey(s, key))) {
  2056.     // key exists, make sure we have enough room for new value, if we don't,
  2057.     // don't change it!
  2058.     if (strlen(value) - strlen(v) + strlen(s) > maxsize) {
  2059.       Con_Printf ("Info string length exceeded\n");
  2060.       return;
  2061.     }
  2062.   }
  2063.   Info_RemoveKey (s, key);
  2064.   if (!value || !strlen(value))
  2065.     return;
  2066.  
  2067.   sprintf (new, "\\%s\\%s", key, value);
  2068.  
  2069.   if ((int)(strlen(new) + strlen(s)) > maxsize)
  2070.   {
  2071.     Con_Printf ("Info string length exceeded\n");
  2072.     return;
  2073.   }
  2074.  
  2075.   // only copy ascii values
  2076.   s += strlen(s);
  2077.   v = new;
  2078.   while (*v)
  2079.   {
  2080.     c = (unsigned char)*v++;
  2081. #ifndef SERVERONLY
  2082.     // client only allows highbits on name
  2083.     if (stricmp(key, "name") != 0) {
  2084.       c &= 127;
  2085.       if (c < 32 || c > 127)
  2086.         continue;
  2087.       // auto lowercase team
  2088.       if (stricmp(key, "team") == 0)
  2089.         c = tolower(c);
  2090.     }
  2091. #else
  2092.     if (!sv_highchars.value) {
  2093.       c &= 127;
  2094.       if (c < 32 || c > 127)
  2095.         continue;
  2096.     }
  2097. #endif
  2098. //    c &= 127;   // strip high bits
  2099.     if (c > 13) // && c < 127)
  2100.       *s++ = c;
  2101.   }
  2102.   *s = 0;
  2103. }
  2104.  
  2105. void Info_SetValueForKey (char *s, char *key, char *value, int maxsize)
  2106. {
  2107.   if (key[0] == '*')
  2108.   {
  2109.     Con_Printf ("Can't set * keys\n");
  2110.     return;
  2111.   }
  2112.  
  2113.   Info_SetValueForStarKey (s, key, value, maxsize);
  2114. }
  2115.  
  2116. void Info_Print (char *s)
  2117. {
  2118.   char  key[512];
  2119.   char  value[512];
  2120.   char  *o;
  2121.   int   l;
  2122.  
  2123.   if (*s == '\\')
  2124.     s++;
  2125.   while (*s)
  2126.   {
  2127.     o = key;
  2128.     while (*s && *s != '\\')
  2129.       *o++ = *s++;
  2130.  
  2131.     l = o - key;
  2132.     if (l < 20)
  2133.     {
  2134.       memset (o, ' ', 20-l);
  2135.       key[20] = 0;
  2136.     }
  2137.     else
  2138.       *o = 0;
  2139.     Con_Printf ("%s", key);
  2140.  
  2141.     if (!*s)
  2142.     {
  2143.       Con_Printf ("MISSING VALUE\n");
  2144.       return;
  2145.     }
  2146.  
  2147.     o = value;
  2148.     s++;
  2149.     while (*s && *s != '\\')
  2150.       *o++ = *s++;
  2151.     *o = 0;
  2152.  
  2153.     if (*s)
  2154.       s++;
  2155.     Con_Printf ("%s\n", value);
  2156.   }
  2157. }
  2158.  
  2159. static byte chktbl[1024 + 4] = {
  2160. 0x78,0xd2,0x94,0xe3,0x41,0xec,0xd6,0xd5,0xcb,0xfc,0xdb,0x8a,0x4b,0xcc,0x85,0x01,
  2161. 0x23,0xd2,0xe5,0xf2,0x29,0xa7,0x45,0x94,0x4a,0x62,0xe3,0xa5,0x6f,0x3f,0xe1,0x7a,
  2162. 0x64,0xed,0x5c,0x99,0x29,0x87,0xa8,0x78,0x59,0x0d,0xaa,0x0f,0x25,0x0a,0x5c,0x58,
  2163. 0xfb,0x00,0xa7,0xa8,0x8a,0x1d,0x86,0x80,0xc5,0x1f,0xd2,0x28,0x69,0x71,0x58,0xc3,
  2164. 0x51,0x90,0xe1,0xf8,0x6a,0xf3,0x8f,0xb0,0x68,0xdf,0x95,0x40,0x5c,0xe4,0x24,0x6b,
  2165. 0x29,0x19,0x71,0x3f,0x42,0x63,0x6c,0x48,0xe7,0xad,0xa8,0x4b,0x91,0x8f,0x42,0x36,
  2166. 0x34,0xe7,0x32,0x55,0x59,0x2d,0x36,0x38,0x38,0x59,0x9b,0x08,0x16,0x4d,0x8d,0xf8,
  2167. 0x0a,0xa4,0x52,0x01,0xbb,0x52,0xa9,0xfd,0x40,0x18,0x97,0x37,0xff,0xc9,0x82,0x27,
  2168. 0xb2,0x64,0x60,0xce,0x00,0xd9,0x04,0xf0,0x9e,0x99,0xbd,0xce,0x8f,0x90,0x4a,0xdd,
  2169. 0xe1,0xec,0x19,0x14,0xb1,0xfb,0xca,0x1e,0x98,0x0f,0xd4,0xcb,0x80,0xd6,0x05,0x63,
  2170. 0xfd,0xa0,0x74,0xa6,0x86,0xf6,0x19,0x98,0x76,0x27,0x68,0xf7,0xe9,0x09,0x9a,0xf2,
  2171. 0x2e,0x42,0xe1,0xbe,0x64,0x48,0x2a,0x74,0x30,0xbb,0x07,0xcc,0x1f,0xd4,0x91,0x9d,
  2172. 0xac,0x55,0x53,0x25,0xb9,0x64,0xf7,0x58,0x4c,0x34,0x16,0xbc,0xf6,0x12,0x2b,0x65,
  2173. 0x68,0x25,0x2e,0x29,0x1f,0xbb,0xb9,0xee,0x6d,0x0c,0x8e,0xbb,0xd2,0x5f,0x1d,0x8f,
  2174. 0xc1,0x39,0xf9,0x8d,0xc0,0x39,0x75,0xcf,0x25,0x17,0xbe,0x96,0xaf,0x98,0x9f,0x5f,
  2175. 0x65,0x15,0xc4,0x62,0xf8,0x55,0xfc,0xab,0x54,0xcf,0xdc,0x14,0x06,0xc8,0xfc,0x42,
  2176. 0xd3,0xf0,0xad,0x10,0x08,0xcd,0xd4,0x11,0xbb,0xca,0x67,0xc6,0x48,0x5f,0x9d,0x59,
  2177. 0xe3,0xe8,0x53,0x67,0x27,0x2d,0x34,0x9e,0x9e,0x24,0x29,0xdb,0x69,0x99,0x86,0xf9,
  2178. 0x20,0xb5,0xbb,0x5b,0xb0,0xf9,0xc3,0x67,0xad,0x1c,0x9c,0xf7,0xcc,0xef,0xce,0x69,
  2179. 0xe0,0x26,0x8f,0x79,0xbd,0xca,0x10,0x17,0xda,0xa9,0x88,0x57,0x9b,0x15,0x24,0xba,
  2180. 0x84,0xd0,0xeb,0x4d,0x14,0xf5,0xfc,0xe6,0x51,0x6c,0x6f,0x64,0x6b,0x73,0xec,0x85,
  2181. 0xf1,0x6f,0xe1,0x67,0x25,0x10,0x77,0x32,0x9e,0x85,0x6e,0x69,0xb1,0x83,0x00,0xe4,
  2182. 0x13,0xa4,0x45,0x34,0x3b,0x40,0xff,0x41,0x82,0x89,0x79,0x57,0xfd,0xd2,0x8e,0xe8,
  2183. 0xfc,0x1d,0x19,0x21,0x12,0x00,0xd7,0x66,0xe5,0xc7,0x10,0x1d,0xcb,0x75,0xe8,0xfa,
  2184. 0xb6,0xee,0x7b,0x2f,0x1a,0x25,0x24,0xb9,0x9f,0x1d,0x78,0xfb,0x84,0xd0,0x17,0x05,
  2185. 0x71,0xb3,0xc8,0x18,0xff,0x62,0xee,0xed,0x53,0xab,0x78,0xd3,0x65,0x2d,0xbb,0xc7,
  2186. 0xc1,0xe7,0x70,0xa2,0x43,0x2c,0x7c,0xc7,0x16,0x04,0xd2,0x45,0xd5,0x6b,0x6c,0x7a,
  2187. 0x5e,0xa1,0x50,0x2e,0x31,0x5b,0xcc,0xe8,0x65,0x8b,0x16,0x85,0xbf,0x82,0x83,0xfb,
  2188. 0xde,0x9f,0x36,0x48,0x32,0x79,0xd6,0x9b,0xfb,0x52,0x45,0xbf,0x43,0xf7,0x0b,0x0b,
  2189. 0x19,0x19,0x31,0xc3,0x85,0xec,0x1d,0x8c,0x20,0xf0,0x3a,0xfa,0x80,0x4d,0x2c,0x7d,
  2190. 0xac,0x60,0x09,0xc0,0x40,0xee,0xb9,0xeb,0x13,0x5b,0xe8,0x2b,0xb1,0x20,0xf0,0xce,
  2191. 0x4c,0xbd,0xc6,0x04,0x86,0x70,0xc6,0x33,0xc3,0x15,0x0f,0x65,0x19,0xfd,0xc2,0xd3,
  2192.  
  2193. // map checksum goes here
  2194. 0x00,0x00,0x00,0x00
  2195. };
  2196.  
  2197. static byte chkbuf[16 + 60 + 4];
  2198.  
  2199. static unsigned last_mapchecksum = 0;
  2200.  
  2201. #if 0
  2202. /*
  2203. ====================
  2204. COM_BlockSequenceCheckByte
  2205.  
  2206. For proxy protecting
  2207. ====================
  2208. */
  2209. byte  COM_BlockSequenceCheckByte (byte *base, int length, int sequence, unsigned mapchecksum)
  2210. {
  2211.   int   checksum;
  2212.   byte  *p;
  2213.  
  2214.   if (last_mapchecksum != mapchecksum) {
  2215.     last_mapchecksum = mapchecksum;
  2216.     chktbl[1024] = (mapchecksum & 0xff000000) >> 24;
  2217.     chktbl[1025] = (mapchecksum & 0x00ff0000) >> 16;
  2218.     chktbl[1026] = (mapchecksum & 0x0000ff00) >> 8;
  2219.     chktbl[1027] = (mapchecksum & 0x000000ff);
  2220.  
  2221.     Com_BlockFullChecksum (chktbl, sizeof(chktbl), chkbuf);
  2222.   }
  2223.  
  2224.   p = chktbl + (sequence % (sizeof(chktbl) - 8));
  2225.  
  2226.   if (length > 60)
  2227.     length = 60;
  2228.   memcpy (chkbuf + 16, base, length);
  2229.  
  2230.   length += 16;
  2231.  
  2232.   chkbuf[length] = (sequence & 0xff) ^ p[0];
  2233.   chkbuf[length+1] = p[1];
  2234.   chkbuf[length+2] = ((sequence>>8) & 0xff) ^ p[2];
  2235.   chkbuf[length+3] = p[3];
  2236.  
  2237.   length += 4;
  2238.  
  2239.   checksum = LittleLong(Com_BlockChecksum (chkbuf, length));
  2240.  
  2241.   checksum &= 0xff;
  2242.  
  2243.   return checksum;
  2244. }
  2245. #endif
  2246.  
  2247. /*
  2248. ====================
  2249. COM_BlockSequenceCRCByte
  2250.  
  2251. For proxy protecting
  2252. ====================
  2253. */
  2254. byte  COM_BlockSequenceCRCByte (byte *base, int length, int sequence)
  2255. {
  2256.   unsigned short crc;
  2257.   byte  *p;
  2258.   byte chkb[60 + 4];
  2259.  
  2260.   p = chktbl + (sequence % (sizeof(chktbl) - 8));
  2261.  
  2262.   if (length > 60)
  2263.     length = 60;
  2264.   memcpy (chkb, base, length);
  2265.  
  2266.   chkb[length] = (sequence & 0xff) ^ p[0];
  2267.   chkb[length+1] = p[1];
  2268.   chkb[length+2] = ((sequence>>8) & 0xff) ^ p[2];
  2269.   chkb[length+3] = p[3];
  2270.  
  2271.   length += 4;
  2272.  
  2273.   crc = CRC_Block(chkb, length);
  2274.  
  2275.   crc &= 0xff;
  2276.  
  2277.   return crc;
  2278. }
  2279.  
  2280. // char *date = "Oct 24 1996";
  2281. static char *date = __DATE__ ;
  2282. static char *mon[12] = 
  2283. { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  2284. static char mond[12] = 
  2285. { 31,    28,    31,    30,    31,    30,    31,    31,    30,    31,    30,    31 };
  2286.  
  2287. // returns days since Oct 24 1996
  2288. int build_number( void )
  2289. {
  2290.   int m = 0; 
  2291.   int d = 0;
  2292.   int y = 0;
  2293.   static int b = 0;
  2294.  
  2295.   if (b != 0)
  2296.     return b;
  2297.  
  2298.   for (m = 0; m < 11; m++)
  2299.   {
  2300.     if (Q_strncasecmp( &date[0], mon[m], 3 ) == 0)
  2301.       break;
  2302.     d += mond[m];
  2303.   }
  2304.  
  2305.   d += atoi( &date[4] ) - 1;
  2306.  
  2307.   y = atoi( &date[7] ) - 1900;
  2308.  
  2309.   b = d + (int)((y - 1) * 365.25);
  2310.  
  2311.   if (((y % 4) == 0) && m > 1)
  2312.   {
  2313.     b += 1;
  2314.   }
  2315.  
  2316.   b -= 35778; // Dec 16 1998
  2317.  
  2318.   return b;
  2319. }
  2320.  
  2321.