home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quake_src / sv_main.c < prev    next >
C/C++ Source or Header  |  2000-06-17  |  29KB  |  1,202 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. // sv_main.c -- server main program
  21.  
  22. #include "quakedef.h"
  23.  
  24. server_t    sv;
  25. server_static_t svs;
  26.  
  27. char  localmodels[MAX_MODELS][5];     // inline model names for precache
  28.  
  29. //============================================================================
  30.  
  31. /*
  32. ===============
  33. SV_Init
  34. ===============
  35. */
  36. void SV_Init (void)
  37. {
  38.   int   i;
  39.   extern  cvar_t  sv_maxvelocity;
  40.   extern  cvar_t  sv_gravity;
  41.   extern  cvar_t  sv_nostep;
  42.   extern  cvar_t  sv_friction;
  43.   extern  cvar_t  sv_edgefriction;
  44.   extern  cvar_t  sv_stopspeed;
  45.   extern  cvar_t  sv_maxspeed;
  46.   extern  cvar_t  sv_accelerate;
  47.   extern  cvar_t  sv_idealpitchscale;
  48.   extern  cvar_t  sv_aim;
  49.  
  50.   Cvar_RegisterVariable (&sv_maxvelocity);
  51.   Cvar_RegisterVariable (&sv_gravity);
  52.   Cvar_RegisterVariable (&sv_friction);
  53.   Cvar_RegisterVariable (&sv_edgefriction);
  54.   Cvar_RegisterVariable (&sv_stopspeed);
  55.   Cvar_RegisterVariable (&sv_maxspeed);
  56.   Cvar_RegisterVariable (&sv_accelerate);
  57.   Cvar_RegisterVariable (&sv_idealpitchscale);
  58.   Cvar_RegisterVariable (&sv_aim);
  59.   Cvar_RegisterVariable (&sv_nostep);
  60.  
  61.   for (i=0 ; i<MAX_MODELS ; i++)
  62.     sprintf (localmodels[i], "*%i", i);
  63. }
  64.  
  65. /*
  66. =============================================================================
  67.  
  68. EVENT MESSAGES
  69.  
  70. =============================================================================
  71. */
  72.  
  73. /*  
  74. ==================
  75. SV_StartParticle
  76.  
  77. Make sure the event gets sent to all clients
  78. ==================
  79. */
  80. void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
  81. {
  82.   int   i, v;
  83.  
  84.   if (sv.datagram.cursize > MAX_DATAGRAM-16)
  85.     return; 
  86.   MSG_WriteByte (&sv.datagram, svc_particle);
  87.   MSG_WriteCoord (&sv.datagram, org[0]);
  88.   MSG_WriteCoord (&sv.datagram, org[1]);
  89.   MSG_WriteCoord (&sv.datagram, org[2]);
  90.   for (i=0 ; i<3 ; i++)
  91.   {
  92.     v = dir[i]*16;
  93.     if (v > 127)
  94.       v = 127;
  95.     else if (v < -128)
  96.       v = -128;
  97.     MSG_WriteChar (&sv.datagram, v);
  98.   }
  99.   MSG_WriteByte (&sv.datagram, count);
  100.   MSG_WriteByte (&sv.datagram, color);
  101. }           
  102.  
  103. /*  
  104. ==================
  105. SV_StartSound
  106.  
  107. Each entity can have eight independant sound sources, like voice,
  108. weapon, feet, etc.
  109.  
  110. Channel 0 is an auto-allocate channel, the others override anything
  111. allready running on that entity/channel pair.
  112.  
  113. An attenuation of 0 will play full volume everywhere in the level.
  114. Larger attenuations will drop off.  (max 4 attenuation)
  115.  
  116. ==================
  117. */  
  118. void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
  119.     float attenuation)
  120. {       
  121.     int         sound_num;
  122.     int field_mask;
  123.     int     i;
  124.   int     ent;
  125.   
  126.   if (volume < 0 || volume > 255)
  127.     Sys_Error ("SV_StartSound: volume = %i", volume);
  128.  
  129.   if (attenuation < 0 || attenuation > 4)
  130.     Sys_Error ("SV_StartSound: attenuation = %f", attenuation);
  131.  
  132.   if (channel < 0 || channel > 7)
  133.     Sys_Error ("SV_StartSound: channel = %i", channel);
  134.  
  135.   if (sv.datagram.cursize > MAX_DATAGRAM-16)
  136.     return; 
  137.  
  138. // find precache number for sound
  139.     for (sound_num=1 ; sound_num<MAX_SOUNDS
  140.         && sv.sound_precache[sound_num] ; sound_num++)
  141.         if (!strcmp(sample, sv.sound_precache[sound_num]))
  142.             break;
  143.     
  144.     if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
  145.     {
  146.         Con_Printf ("SV_StartSound: %s not precacheed\n", sample);
  147.         return;
  148.     }
  149.     
  150.   ent = NUM_FOR_EDICT(entity);
  151.  
  152.   channel = (ent<<3) | channel;
  153.  
  154.   field_mask = 0;
  155.   if (volume != DEFAULT_SOUND_PACKET_VOLUME)
  156.     field_mask |= SND_VOLUME;
  157.   if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
  158.     field_mask |= SND_ATTENUATION;
  159.  
  160. // directed messages go only to the entity the are targeted on
  161.   MSG_WriteByte (&sv.datagram, svc_sound);
  162.   MSG_WriteByte (&sv.datagram, field_mask);
  163.   if (field_mask & SND_VOLUME)
  164.     MSG_WriteByte (&sv.datagram, volume);
  165.   if (field_mask & SND_ATTENUATION)
  166.     MSG_WriteByte (&sv.datagram, attenuation*64);
  167.   MSG_WriteShort (&sv.datagram, channel);
  168.   MSG_WriteByte (&sv.datagram, sound_num);
  169.   for (i=0 ; i<3 ; i++)
  170.     MSG_WriteCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
  171. }           
  172.  
  173. /*
  174. ==============================================================================
  175.  
  176. CLIENT SPAWNING
  177.  
  178. ==============================================================================
  179. */
  180.  
  181. /*
  182. ================
  183. SV_SendServerinfo
  184.  
  185. Sends the first message from the server to a connected client.
  186. This will be sent on the initial connection and upon each server load.
  187. ================
  188. */
  189. void SV_SendServerinfo (client_t *client)
  190. {
  191.   char      **s;
  192.   char      message[2048];
  193.  
  194.   MSG_WriteByte (&client->message, svc_print);
  195.   sprintf (message, "%c\nVERSION %4.2f SERVER (%i CRC)",
  196.            2, VERSION, (int)pr_crc);
  197.   MSG_WriteString (&client->message,message);
  198.  
  199.   MSG_WriteByte (&client->message, svc_serverinfo);
  200.   MSG_WriteLong (&client->message, PROTOCOL_VERSION);
  201.   MSG_WriteByte (&client->message, svs.maxclients);
  202.  
  203.   if (!coop.value && deathmatch.value)
  204.     MSG_WriteByte (&client->message, GAME_DEATHMATCH);
  205.   else
  206.     MSG_WriteByte (&client->message, GAME_COOP);
  207.  
  208.   sprintf (message, pr_strings+sv.edicts->v.message);
  209.  
  210.   MSG_WriteString (&client->message,message);
  211.  
  212.   for (s = sv.model_precache+1 ; *s ; s++)
  213.     MSG_WriteString (&client->message, *s);
  214.   MSG_WriteByte (&client->message, 0);
  215.  
  216.   for (s = sv.sound_precache+1 ; *s ; s++)
  217.     MSG_WriteString (&client->message, *s);
  218.   MSG_WriteByte (&client->message, 0);
  219.  
  220. // send music
  221.   MSG_WriteByte (&client->message, svc_cdtrack);
  222.   MSG_WriteByte (&client->message, sv.edicts->v.sounds);
  223.   MSG_WriteByte (&client->message, sv.edicts->v.sounds);
  224.  
  225. // set view 
  226.   MSG_WriteByte (&client->message, svc_setview);
  227.   MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
  228.  
  229.   MSG_WriteByte (&client->message, svc_signonnum);
  230.   MSG_WriteByte (&client->message, 1);
  231.  
  232.   client->sendsignon = true;
  233.   client->spawned = false;    // need prespawn, spawn, etc
  234. }
  235.  
  236. /*
  237. ================
  238. SV_ConnectClient
  239.  
  240. Initializes a client_t for a new net connection.  This will only be called
  241. once for a player each game, not once for each level change.
  242. ================
  243. */
  244. void SV_ConnectClient (int clientnum)
  245. {
  246.   edict_t     *ent;
  247.   client_t    *client;
  248.   int       edictnum;
  249.   struct qsocket_s *netconnection;
  250.   int       i;
  251.   float     spawn_parms[NUM_SPAWN_PARMS];
  252.  
  253.   client = svs.clients + clientnum;
  254.  
  255.   Con_DPrintf ("Client %s connected\n", client->netconnection->address);
  256.  
  257.   edictnum = clientnum+1;
  258.  
  259.   ent = EDICT_NUM(edictnum);
  260.   
  261. // set up the client_t
  262.   netconnection = client->netconnection;
  263.   
  264.   if (sv.loadgame)
  265.     memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
  266.   memset (client, 0, sizeof(*client));
  267.   client->netconnection = netconnection;
  268.  
  269.   strcpy (client->name, "unconnected");
  270.   client->active = true;
  271.   client->spawned = false;
  272.   client->edict = ent;
  273.   client->message.data = client->msgbuf;
  274.   client->message.maxsize = sizeof(client->msgbuf);
  275.   client->message.allowoverflow = true;   // we can catch it
  276.  
  277. #ifdef IDGODS
  278.   client->privileged = IsID(&client->netconnection->addr);
  279. #else 
  280.   client->privileged = false;       
  281. #endif
  282.  
  283.   if (sv.loadgame)
  284.     memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
  285.   else
  286.   {
  287.   // call the progs to get default spawn parms for the new client
  288.     PR_ExecuteProgram (pr_global_struct->SetNewParms);
  289.     for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
  290.       client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
  291.   }
  292.  
  293.   SV_SendServerinfo (client);
  294. }
  295.  
  296.  
  297. /*
  298. ===================
  299. SV_CheckForNewClients
  300.  
  301. ===================
  302. */
  303. void SV_CheckForNewClients (void)
  304. {
  305.   struct qsocket_s  *ret;
  306.   int       i;
  307.     
  308. //
  309. // check for new connections
  310. //
  311.   while (1)
  312.   {
  313.     ret = NET_CheckNewConnections ();
  314.     if (!ret)
  315.       break;
  316.  
  317.   // 
  318.   // init a new client structure
  319.   //  
  320.     for (i=0 ; i<svs.maxclients ; i++)
  321.       if (!svs.clients[i].active)
  322.         break;
  323.     if (i == svs.maxclients)
  324.       Sys_Error ("Host_CheckForNewClients: no free clients");
  325.     
  326.     svs.clients[i].netconnection = ret;
  327.     SV_ConnectClient (i); 
  328.   
  329.     net_activeconnections++;
  330.   }
  331. }
  332.  
  333.  
  334.  
  335. /*
  336. ===============================================================================
  337.  
  338. FRAME UPDATES
  339.  
  340. ===============================================================================
  341. */
  342.  
  343. /*
  344. ==================
  345. SV_ClearDatagram
  346.  
  347. ==================
  348. */
  349. void SV_ClearDatagram (void)
  350. {
  351.   SZ_Clear (&sv.datagram);
  352. }
  353.  
  354. /*
  355. =============================================================================
  356.  
  357. The PVS must include a small area around the client to allow head bobbing
  358. or other small motion on the client side.  Otherwise, a bob might cause an
  359. entity that should be visible to not show up, especially when the bob
  360. crosses a waterline.
  361.  
  362. =============================================================================
  363. */
  364.  
  365. int   fatbytes;
  366. byte  fatpvs[MAX_MAP_LEAFS/8];
  367.  
  368. void SV_AddToFatPVS (vec3_t org, mnode_t *node)
  369. {
  370.   int   i;
  371.   byte  *pvs;
  372.   mplane_t  *plane;
  373.   float d;
  374.  
  375.   while (1)
  376.   {
  377.   // if this is a leaf, accumulate the pvs bits
  378.     if (node->contents < 0)
  379.     {
  380.       if (node->contents != CONTENTS_SOLID)
  381.       {
  382.         pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
  383.         for (i=0 ; i<fatbytes ; i++)
  384.           fatpvs[i] |= pvs[i];
  385.       }
  386.       return;
  387.     }
  388.   
  389.     plane = node->plane;
  390.     d = DotProduct (org, plane->normal) - plane->dist;
  391.     if (d > 8)
  392.       node = node->children[0];
  393.     else if (d < -8)
  394.       node = node->children[1];
  395.     else
  396.     { // go down both
  397.       SV_AddToFatPVS (org, node->children[0]);
  398.       node = node->children[1];
  399.     }
  400.   }
  401. }
  402.  
  403. /*
  404. =============
  405. SV_FatPVS
  406.  
  407. Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
  408. given point.
  409. =============
  410. */
  411. byte *SV_FatPVS (vec3_t org)
  412. {
  413.   fatbytes = (sv.worldmodel->numleafs+31)>>3;
  414.   Q_memset (fatpvs, 0, fatbytes);
  415.   SV_AddToFatPVS (org, sv.worldmodel->nodes);
  416.   return fatpvs;
  417. }
  418.  
  419. //=============================================================================
  420.  
  421.  
  422. /*
  423. =============
  424. SV_WriteEntitiesToClient
  425.  
  426. =============
  427. */
  428. void SV_WriteEntitiesToClient (edict_t  *clent, sizebuf_t *msg)
  429. {
  430.   int   e, i;
  431.   int   bits;
  432.   byte  *pvs;
  433.   vec3_t  org;
  434.   float miss;
  435.   edict_t *ent;
  436.  
  437. // find the client's PVS
  438.   VectorAdd (clent->v.origin, clent->v.view_ofs, org);
  439.   pvs = SV_FatPVS (org);
  440.  
  441. // send over all entities (excpet the client) that touch the pvs
  442.   ent = NEXT_EDICT(sv.edicts);
  443.   for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
  444.   {
  445. #ifdef QUAKE2
  446.     // don't send if flagged for NODRAW and there are no lighting effects
  447.     if (ent->v.effects == EF_NODRAW)
  448.       continue;
  449. #endif
  450.  
  451. // ignore if not touching a PV leaf
  452.     if (ent != clent) // clent is ALLWAYS sent
  453.     {
  454. // ignore ents without visible models
  455.       if (!ent->v.modelindex || !pr_strings[ent->v.model])
  456.         continue;
  457.  
  458.       for (i=0 ; i < ent->num_leafs ; i++)
  459.         if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
  460.           break;
  461.         
  462.       if (i == ent->num_leafs)
  463.         continue;   // not visible
  464.     }
  465.  
  466.     if (msg->maxsize - msg->cursize < 16)
  467.     {
  468.       Con_Printf ("packet overflow\n");
  469.       return;
  470.     }
  471.  
  472. // send an update
  473.     bits = 0;
  474.     
  475.     for (i=0 ; i<3 ; i++)
  476.     {
  477.       miss = ent->v.origin[i] - ent->baseline.origin[i];
  478.       if ( miss < -0.1 || miss > 0.1 )
  479.         bits |= U_ORIGIN1<<i;
  480.     }
  481.  
  482.     if ( ent->v.angles[0] != ent->baseline.angles[0] )
  483.       bits |= U_ANGLE1;
  484.       
  485.     if ( ent->v.angles[1] != ent->baseline.angles[1] )
  486.       bits |= U_ANGLE2;
  487.       
  488.     if ( ent->v.angles[2] != ent->baseline.angles[2] )
  489.       bits |= U_ANGLE3;
  490.       
  491.     if (ent->v.movetype == MOVETYPE_STEP)
  492.       bits |= U_NOLERP; // don't mess up the step animation
  493.   
  494.     if (ent->baseline.colormap != ent->v.colormap)
  495.       bits |= U_COLORMAP;
  496.       
  497.     if (ent->baseline.skin != ent->v.skin)
  498.       bits |= U_SKIN;
  499.       
  500.     if (ent->baseline.frame != ent->v.frame)
  501.       bits |= U_FRAME;
  502.     
  503.     if (ent->baseline.effects != ent->v.effects)
  504.       bits |= U_EFFECTS;
  505.     
  506.     if (ent->baseline.modelindex != ent->v.modelindex)
  507.       bits |= U_MODEL;
  508.  
  509.     if (e >= 256)
  510.       bits |= U_LONGENTITY;
  511.       
  512.     if (bits >= 256)
  513.       bits |= U_MOREBITS;
  514.  
  515.   //
  516.   // write the message
  517.   //
  518.     MSG_WriteByte (msg,bits | U_SIGNAL);
  519.     
  520.     if (bits & U_MOREBITS)
  521.       MSG_WriteByte (msg, bits>>8);
  522.     if (bits & U_LONGENTITY)
  523.       MSG_WriteShort (msg,e);
  524.     else
  525.       MSG_WriteByte (msg,e);
  526.  
  527.     if (bits & U_MODEL)
  528.       MSG_WriteByte (msg, ent->v.modelindex);
  529.     if (bits & U_FRAME)
  530.       MSG_WriteByte (msg, ent->v.frame);
  531.     if (bits & U_COLORMAP)
  532.       MSG_WriteByte (msg, ent->v.colormap);
  533.     if (bits & U_SKIN)
  534.       MSG_WriteByte (msg, ent->v.skin);
  535.     if (bits & U_EFFECTS)
  536.       MSG_WriteByte (msg, ent->v.effects);
  537.     if (bits & U_ORIGIN1)
  538.       MSG_WriteCoord (msg, ent->v.origin[0]);   
  539.     if (bits & U_ANGLE1)
  540.       MSG_WriteAngle(msg, ent->v.angles[0]);
  541.     if (bits & U_ORIGIN2)
  542.       MSG_WriteCoord (msg, ent->v.origin[1]);
  543.     if (bits & U_ANGLE2)
  544.       MSG_WriteAngle(msg, ent->v.angles[1]);
  545.     if (bits & U_ORIGIN3)
  546.       MSG_WriteCoord (msg, ent->v.origin[2]);
  547.     if (bits & U_ANGLE3)
  548.       MSG_WriteAngle(msg, ent->v.angles[2]);
  549.   }
  550. }
  551.  
  552. /*
  553. =============
  554. SV_CleanupEnts
  555.  
  556. =============
  557. */
  558. void SV_CleanupEnts (void)
  559. {
  560.   int   e;
  561.   edict_t *ent;
  562.   
  563.   ent = NEXT_EDICT(sv.edicts);
  564.   for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
  565.   {
  566.     ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
  567.   }
  568.  
  569. }
  570.  
  571. /*
  572. ==================
  573. SV_WriteClientdataToMessage
  574.  
  575. ==================
  576. */
  577. void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
  578. {
  579.   int   bits;
  580.   int   i;
  581.   edict_t *other;
  582.   int   items;
  583. #ifndef QUAKE2
  584.   eval_t  *val;
  585. #endif
  586.  
  587. //
  588. // send a damage message
  589. //
  590.   if (ent->v.dmg_take || ent->v.dmg_save)
  591.   {
  592.     other = PROG_TO_EDICT(ent->v.dmg_inflictor);
  593.     MSG_WriteByte (msg, svc_damage);
  594.     MSG_WriteByte (msg, ent->v.dmg_save);
  595.     MSG_WriteByte (msg, ent->v.dmg_take);
  596.     for (i=0 ; i<3 ; i++)
  597.       MSG_WriteCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
  598.   
  599.     ent->v.dmg_take = 0;
  600.     ent->v.dmg_save = 0;
  601.   }
  602.  
  603. //
  604. // send the current viewpos offset from the view entity
  605. //
  606.   SV_SetIdealPitch ();    // how much to look up / down ideally
  607.  
  608. // a fixangle might get lost in a dropped packet.  Oh well.
  609.   if ( ent->v.fixangle )
  610.   {
  611.     MSG_WriteByte (msg, svc_setangle);
  612.     for (i=0 ; i < 3 ; i++)
  613.       MSG_WriteAngle (msg, ent->v.angles[i] );
  614.     ent->v.fixangle = 0;
  615.   }
  616.  
  617.   bits = 0;
  618.   
  619.   if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
  620.     bits |= SU_VIEWHEIGHT;
  621.     
  622.   if (ent->v.idealpitch)
  623.     bits |= SU_IDEALPITCH;
  624.  
  625. // stuff the sigil bits into the high bits of items for sbar, or else
  626. // mix in items2
  627. #ifdef QUAKE2
  628.   items = (int)ent->v.items | ((int)ent->v.items2 << 23);
  629. #else
  630.   val = GetEdictFieldValue(ent, "items2");
  631.  
  632.   if (val)
  633.     items = (int)ent->v.items | ((int)val->_float << 23);
  634.   else
  635.     items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
  636. #endif
  637.  
  638.   bits |= SU_ITEMS;
  639.   
  640.   if ( (int)ent->v.flags & FL_ONGROUND)
  641.     bits |= SU_ONGROUND;
  642.   
  643.   if ( ent->v.waterlevel >= 2)
  644.     bits |= SU_INWATER;
  645.   
  646.   for (i=0 ; i<3 ; i++)
  647.   {
  648.     if (ent->v.punchangle[i])
  649.       bits |= (SU_PUNCH1<<i);
  650.     if (ent->v.velocity[i])
  651.       bits |= (SU_VELOCITY1<<i);
  652.   }
  653.   
  654.   if (ent->v.weaponframe)
  655.     bits |= SU_WEAPONFRAME;
  656.  
  657.   if (ent->v.armorvalue)
  658.     bits |= SU_ARMOR;
  659.  
  660. //  if (ent->v.weapon)
  661.     bits |= SU_WEAPON;
  662.  
  663. // send the data
  664.  
  665.   MSG_WriteByte (msg, svc_clientdata);
  666.   MSG_WriteShort (msg, bits);
  667.  
  668.   if (bits & SU_VIEWHEIGHT)
  669.     MSG_WriteChar (msg, ent->v.view_ofs[2]);
  670.  
  671.   if (bits & SU_IDEALPITCH)
  672.     MSG_WriteChar (msg, ent->v.idealpitch);
  673.  
  674.   for (i=0 ; i<3 ; i++)
  675.   {
  676.     if (bits & (SU_PUNCH1<<i))
  677.       MSG_WriteChar (msg, ent->v.punchangle[i]);
  678.     if (bits & (SU_VELOCITY1<<i))
  679.       MSG_WriteChar (msg, ent->v.velocity[i]/16);
  680.   }
  681.  
  682. // [always sent]  if (bits & SU_ITEMS)
  683.   MSG_WriteLong (msg, items);
  684.  
  685.   if (bits & SU_WEAPONFRAME)
  686.     MSG_WriteByte (msg, ent->v.weaponframe);
  687.   if (bits & SU_ARMOR)
  688.     MSG_WriteByte (msg, ent->v.armorvalue);
  689.   if (bits & SU_WEAPON)
  690.     MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
  691.   
  692.   MSG_WriteShort (msg, ent->v.health);
  693.   MSG_WriteByte (msg, ent->v.currentammo);
  694.   MSG_WriteByte (msg, ent->v.ammo_shells);
  695.   MSG_WriteByte (msg, ent->v.ammo_nails);
  696.   MSG_WriteByte (msg, ent->v.ammo_rockets);
  697.   MSG_WriteByte (msg, ent->v.ammo_cells);
  698.  
  699.   if (standard_quake)
  700.   {
  701.     MSG_WriteByte (msg, ent->v.weapon);
  702.   }
  703.   else
  704.   {
  705.     for(i=0;i<32;i++)
  706.     {
  707.       if ( ((int)ent->v.weapon) & (1<<i) )
  708.       {
  709.         MSG_WriteByte (msg, i);
  710.         break;
  711.       }
  712.     }
  713.   }
  714. }
  715.  
  716. /*
  717. =======================
  718. SV_SendClientDatagram
  719. =======================
  720. */
  721. qboolean SV_SendClientDatagram (client_t *client)
  722. {
  723.   byte    buf[MAX_DATAGRAM];
  724.   sizebuf_t msg;
  725.   
  726.   msg.data = buf;
  727.   msg.maxsize = sizeof(buf);
  728.   msg.cursize = 0;
  729.  
  730.   MSG_WriteByte (&msg, svc_time);
  731.   MSG_WriteFloat (&msg, sv.time);
  732.  
  733. // add the client specific data to the datagram
  734.   SV_WriteClientdataToMessage (client->edict, &msg);
  735.  
  736.   SV_WriteEntitiesToClient (client->edict, &msg);
  737.  
  738. // copy the server datagram if there is space
  739.   if (msg.cursize + sv.datagram.cursize < msg.maxsize)
  740.     SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
  741.  
  742. // send the datagram
  743.   if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
  744.   {
  745.     SV_DropClient (true);// if the message couldn't send, kick off
  746.     return false;
  747.   }
  748.   
  749.   return true;
  750. }
  751.  
  752. /*
  753. =======================
  754. SV_UpdateToReliableMessages
  755. =======================
  756. */
  757. void SV_UpdateToReliableMessages (void)
  758. {
  759.   int     i, j;
  760.   client_t *client;
  761.  
  762. // check for changes to be sent over the reliable streams
  763.   for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  764.   {
  765.     if (host_client->old_frags != host_client->edict->v.frags)
  766.     {
  767.       for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
  768.       {
  769.         if (!client->active)
  770.           continue;
  771.         MSG_WriteByte (&client->message, svc_updatefrags);
  772.         MSG_WriteByte (&client->message, i);
  773.         MSG_WriteShort (&client->message, host_client->edict->v.frags);
  774.       }
  775.  
  776.       host_client->old_frags = host_client->edict->v.frags;
  777.     }
  778.   }
  779.   
  780.   for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
  781.   {
  782.     if (!client->active)
  783.       continue;
  784.     SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
  785.   }
  786.  
  787.   SZ_Clear (&sv.reliable_datagram);
  788. }
  789.  
  790.  
  791. /*
  792. =======================
  793. SV_SendNop
  794.  
  795. Send a nop message without trashing or sending the accumulated client
  796. message buffer
  797. =======================
  798. */
  799. void SV_SendNop (client_t *client)
  800. {
  801.   sizebuf_t msg;
  802.   byte    buf[4];
  803.   
  804.   msg.data = buf;
  805.   msg.maxsize = sizeof(buf);
  806.   msg.cursize = 0;
  807.  
  808.   MSG_WriteChar (&msg, svc_nop);
  809.  
  810.   if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
  811.     SV_DropClient (true); // if the message couldn't send, kick off
  812.   client->last_message = realtime;
  813. }
  814.  
  815. /*
  816. =======================
  817. SV_SendClientMessages
  818. =======================
  819. */
  820. void SV_SendClientMessages (void)
  821. {
  822.   int     i;
  823.   
  824. // update frags, names, etc
  825.   SV_UpdateToReliableMessages ();
  826.  
  827. // build individual updates
  828.   for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  829.   {
  830.     if (!host_client->active)
  831.       continue;
  832.  
  833.     if (host_client->spawned)
  834.     {
  835.       if (!SV_SendClientDatagram (host_client))
  836.         continue;
  837.     }
  838.     else
  839.     {
  840.     // the player isn't totally in the game yet
  841.     // send small keepalive messages if too much time has passed
  842.     // send a full message when the next signon stage has been requested
  843.     // some other message data (name changes, etc) may accumulate 
  844.     // between signon stages
  845.       if (!host_client->sendsignon)
  846.       {
  847.         if (realtime - host_client->last_message > 5)
  848.           SV_SendNop (host_client);
  849.         continue; // don't send out non-signon messages
  850.       }
  851.     }
  852.  
  853.     // check for an overflowed message.  Should only happen
  854.     // on a very fucked up connection that backs up a lot, then
  855.     // changes level
  856.     if (host_client->message.overflowed)
  857.     {
  858.       SV_DropClient (true);
  859.       host_client->message.overflowed = false;
  860.       continue;
  861.     }
  862.       
  863.     if (host_client->message.cursize || host_client->dropasap)
  864.     {
  865.       if (!NET_CanSendMessage (host_client->netconnection))
  866.       {
  867. //        I_Printf ("can't write\n");
  868.         continue;
  869.       }
  870.  
  871.       if (host_client->dropasap)
  872.         SV_DropClient (false);  // went to another level
  873.       else
  874.       {
  875.         if (NET_SendMessage (host_client->netconnection
  876.         , &host_client->message) == -1)
  877.           SV_DropClient (true); // if the message couldn't send, kick off
  878.         SZ_Clear (&host_client->message);
  879.         host_client->last_message = realtime;
  880.         host_client->sendsignon = false;
  881.       }
  882.     }
  883.   }
  884.   
  885.   
  886. // clear muzzle flashes
  887.   SV_CleanupEnts ();
  888. }
  889.  
  890.  
  891. /*
  892. ==============================================================================
  893.  
  894. SERVER SPAWNING
  895.  
  896. ==============================================================================
  897. */
  898.  
  899. /*
  900. ================
  901. SV_ModelIndex
  902.  
  903. ================
  904. */
  905. int SV_ModelIndex (char *name)
  906. {
  907.   int   i;
  908.   
  909.   if (!name || !name[0])
  910.     return 0;
  911.  
  912.   for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
  913.     if (!strcmp(sv.model_precache[i], name))
  914.       return i;
  915.   if (i==MAX_MODELS || !sv.model_precache[i])
  916.     Sys_Error ("SV_ModelIndex: model %s not precached", name);
  917.   return i;
  918. }
  919.  
  920. /*
  921. ================
  922. SV_CreateBaseline
  923.  
  924. ================
  925. */
  926. void SV_CreateBaseline (void)
  927. {
  928.   int     i;
  929.   edict_t     *svent;
  930.   int       entnum; 
  931.     
  932.   for (entnum = 0; entnum < sv.num_edicts ; entnum++)
  933.   {
  934.   // get the current server version
  935.     svent = EDICT_NUM(entnum);
  936.     if (svent->free)
  937.       continue;
  938.     if (entnum > svs.maxclients && !svent->v.modelindex)
  939.       continue;
  940.  
  941.   //
  942.   // create entity baseline
  943.   //
  944.     VectorCopy (svent->v.origin, svent->baseline.origin);
  945.     VectorCopy (svent->v.angles, svent->baseline.angles);
  946.     svent->baseline.frame = svent->v.frame;
  947.     svent->baseline.skin = svent->v.skin;
  948.     if (entnum > 0 && entnum <= svs.maxclients)
  949.     {
  950.       svent->baseline.colormap = entnum;
  951.       svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
  952.     }
  953.     else
  954.     {
  955.       svent->baseline.colormap = 0;
  956.       svent->baseline.modelindex =
  957.         SV_ModelIndex(pr_strings + svent->v.model);
  958.     }
  959.     
  960.   //
  961.   // add to the message
  962.   //
  963.     MSG_WriteByte (&sv.signon,svc_spawnbaseline);   
  964.     MSG_WriteShort (&sv.signon,entnum);
  965.  
  966.     MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
  967.     MSG_WriteByte (&sv.signon, svent->baseline.frame);
  968.     MSG_WriteByte (&sv.signon, svent->baseline.colormap);
  969.     MSG_WriteByte (&sv.signon, svent->baseline.skin);
  970.     for (i=0 ; i<3 ; i++)
  971.     {
  972.       MSG_WriteCoord(&sv.signon, svent->baseline.origin[i]);
  973.       MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
  974.     }
  975.   }
  976. }
  977.  
  978.  
  979. /*
  980. ================
  981. SV_SendReconnect
  982.  
  983. Tell all the clients that the server is changing levels
  984. ================
  985. */
  986. void SV_SendReconnect (void)
  987. {
  988.   char  data[128];
  989.   sizebuf_t msg;
  990.  
  991.   msg.data = data;
  992.   msg.cursize = 0;
  993.   msg.maxsize = sizeof(data);
  994.  
  995.   MSG_WriteChar (&msg, svc_stufftext);
  996.   MSG_WriteString (&msg, "reconnect\n");
  997.   NET_SendToAll (&msg, 5);
  998.   
  999.   if (cls.state != ca_dedicated)
  1000. #ifdef QUAKE2
  1001.     Cbuf_InsertText ("reconnect\n");
  1002. #else
  1003.     Cmd_ExecuteString ("reconnect\n", src_command);
  1004. #endif
  1005. }
  1006.  
  1007.  
  1008. /*
  1009. ================
  1010. SV_SaveSpawnparms
  1011.  
  1012. Grabs the current state of each client for saving across the
  1013. transition to another level
  1014. ================
  1015. */
  1016. void SV_SaveSpawnparms (void)
  1017. {
  1018.   int   i, j;
  1019.  
  1020.   svs.serverflags = pr_global_struct->serverflags;
  1021.  
  1022.   for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  1023.   {
  1024.     if (!host_client->active)
  1025.       continue;
  1026.  
  1027.   // call the progs to get default spawn parms for the new client
  1028.     pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
  1029.     PR_ExecuteProgram (pr_global_struct->SetChangeParms);
  1030.     for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
  1031.       host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
  1032.   }
  1033. }
  1034.  
  1035.  
  1036. /*
  1037. ================
  1038. SV_SpawnServer
  1039.  
  1040. This is called at the start of each level
  1041. ================
  1042. */
  1043. extern float    scr_centertime_off;
  1044.  
  1045. #ifdef QUAKE2
  1046. void SV_SpawnServer (char *server, char *startspot)
  1047. #else
  1048. void SV_SpawnServer (char *server)
  1049. #endif
  1050. {
  1051.   edict_t   *ent;
  1052.   int     i;
  1053.  
  1054.   // let's not have any servers with no name
  1055.   if (hostname.string[0] == 0)
  1056.     Cvar_Set ("hostname", "UNNAMED");
  1057.   scr_centertime_off = 0;
  1058.  
  1059.   Con_DPrintf ("SpawnServer: %s\n",server);
  1060.   svs.changelevel_issued = false;   // now safe to issue another
  1061.  
  1062. //
  1063. // tell all connected clients that we are going to a new level
  1064. //
  1065.   if (sv.active)
  1066.   {
  1067.     SV_SendReconnect ();
  1068.   }
  1069.  
  1070. //
  1071. // make cvars consistant
  1072. //
  1073.   if (coop.value)
  1074.     Cvar_SetValue ("deathmatch", 0);
  1075.   current_skill = (int)(skill.value + 0.5);
  1076.   if (current_skill < 0)
  1077.     current_skill = 0;
  1078.   if (current_skill > 3)
  1079.     current_skill = 3;
  1080.  
  1081.   Cvar_SetValue ("skill", (float)current_skill);
  1082.   
  1083. //
  1084. // set up the new server
  1085. //
  1086.   Host_ClearMemory ();
  1087.  
  1088.   memset (&sv, 0, sizeof(sv));
  1089.  
  1090.   strcpy (sv.name, server);
  1091. #ifdef QUAKE2
  1092.   if (startspot)
  1093.     strcpy(sv.startspot, startspot);
  1094. #endif
  1095.  
  1096. // load progs to get entity field count
  1097.   PR_LoadProgs ();
  1098.  
  1099. // allocate server memory
  1100.   sv.max_edicts = MAX_EDICTS;
  1101.   
  1102.   sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
  1103.  
  1104.   sv.datagram.maxsize = sizeof(sv.datagram_buf);
  1105.   sv.datagram.cursize = 0;
  1106.   sv.datagram.data = sv.datagram_buf;
  1107.   
  1108.   sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
  1109.   sv.reliable_datagram.cursize = 0;
  1110.   sv.reliable_datagram.data = sv.reliable_datagram_buf;
  1111.   
  1112.   sv.signon.maxsize = sizeof(sv.signon_buf);
  1113.   sv.signon.cursize = 0;
  1114.   sv.signon.data = sv.signon_buf;
  1115.   
  1116. // leave slots at start for clients only
  1117.   sv.num_edicts = svs.maxclients+1;
  1118.   for (i=0 ; i<svs.maxclients ; i++)
  1119.   {
  1120.     ent = EDICT_NUM(i+1);
  1121.     svs.clients[i].edict = ent;
  1122.   }
  1123.   
  1124.   sv.state = ss_loading;
  1125.   sv.paused = false;
  1126.  
  1127.   sv.time = 1.0;
  1128.   
  1129.   strcpy (sv.name, server);
  1130.   sprintf (sv.modelname,"maps/%s.bsp", server);
  1131.   sv.worldmodel = Mod_ForName (sv.modelname, false);
  1132.   if (!sv.worldmodel)
  1133.   {
  1134.     Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
  1135.     sv.active = false;
  1136.     return;
  1137.   }
  1138.   sv.models[1] = sv.worldmodel;
  1139.   
  1140. //
  1141. // clear world interaction links
  1142. //
  1143.   SV_ClearWorld ();
  1144.   
  1145.   sv.sound_precache[0] = pr_strings;
  1146.  
  1147.   sv.model_precache[0] = pr_strings;
  1148.   sv.model_precache[1] = sv.modelname;
  1149.   for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
  1150.   {
  1151.     sv.model_precache[1+i] = localmodels[i];
  1152.     sv.models[i+1] = Mod_ForName (localmodels[i], false);
  1153.   }
  1154.  
  1155. //
  1156. // load the rest of the entities
  1157. //  
  1158.   ent = EDICT_NUM(0);
  1159.   memset (&ent->v, 0, progs->entityfields * 4);
  1160.   ent->free = false;
  1161.   ent->v.model = sv.worldmodel->name - pr_strings;
  1162.   ent->v.modelindex = 1;    // world model
  1163.   ent->v.solid = SOLID_BSP;
  1164.   ent->v.movetype = MOVETYPE_PUSH;
  1165.  
  1166.   if (coop.value)
  1167.     pr_global_struct->coop = coop.value;
  1168.   else
  1169.     pr_global_struct->deathmatch = deathmatch.value;
  1170.  
  1171.   pr_global_struct->mapname = sv.name - pr_strings;
  1172. #ifdef QUAKE2
  1173.   pr_global_struct->startspot = sv.startspot - pr_strings;
  1174. #endif
  1175.  
  1176. // serverflags are for cross level information (sigils)
  1177.   pr_global_struct->serverflags = svs.serverflags;
  1178.   
  1179.   ED_LoadFromFile (sv.worldmodel->entities);
  1180.  
  1181.   sv.active = true;
  1182.  
  1183. // all setup is completed, any further precache statements are errors
  1184.   sv.state = ss_active;
  1185.   
  1186. // run two frames to allow everything to settle
  1187.   host_frametime = 0.1;
  1188.   SV_Physics ();
  1189.   SV_Physics ();
  1190.  
  1191. // create a baseline for more efficient communications
  1192.   SV_CreateBaseline ();
  1193.  
  1194. // send serverinfo to all connected clients
  1195.   for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  1196.     if (host_client->active)
  1197.       SV_SendServerinfo (host_client);
  1198.   
  1199.   Con_DPrintf ("Server spawned.\n");
  1200. }
  1201.  
  1202.