home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / game / p_client.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  24.4 KB  |  1,126 lines

  1. #include "g_local.h"
  2. #include "m_player.h"
  3.  
  4. void ClientUserinfoChanged (edict_t *ent, char *userinfo);
  5.  
  6. void SP_misc_teleporter_dest (edict_t *ent);
  7.  
  8. /*QUAKED info_player_start (1 0 0) (-16 -16 -24) (16 16 32)
  9. The normal starting point for a level.
  10. */
  11. void SP_info_player_start(void)
  12. {
  13. }
  14.  
  15. /*QUAKED info_player_deathmatch (1 0 1) (-16 -16 -24) (16 16 32)
  16. potential spawning position for deathmatch games
  17. */
  18. void SP_info_player_deathmatch(edict_t *self)
  19. {
  20.     if (!deathmatch->value)
  21.     {
  22.         G_FreeEdict (self);
  23.         return;
  24.     }
  25.     SP_misc_teleporter_dest (self);
  26. }
  27.  
  28. /*QUAKED info_player_coop (1 0 1) (-16 -16 -24) (16 16 32)
  29. potential spawning position for coop games
  30. */
  31. void SP_info_player_coop(edict_t *self)
  32. {
  33.     if ( 1) //!coop->value)
  34.     {
  35.         G_FreeEdict (self);
  36.         return;
  37.     }
  38. //    SP_misc_teleporter_dest (self);
  39. }
  40.  
  41.  
  42. /*QUAKED info_player_intermission (1 0 1) (-16 -16 -24) (16 16 32)
  43. The deathmatch intermission point will be at one of these
  44. Use 'angles' instead of 'angle', so you can set pitch or roll as well as yaw.  'pitch yaw roll'
  45. */
  46. void SP_info_player_intermission(void)
  47. {
  48. }
  49.  
  50.  
  51.  
  52. int    SexedSoundIndex (edict_t *ent, char *base)
  53. {
  54.     char    buffer[MAX_QPATH];
  55.  
  56.     Com_sprintf (buffer, sizeof(buffer), "%s/%s.wav", ent->client->pers.sounddir, base);
  57.  
  58.     return gi.soundindex(buffer);
  59. }
  60.  
  61. //=======================================================================
  62.  
  63.  
  64. void player_pain (edict_t *self, edict_t *other, float kick, int damage)
  65. {
  66.     // player pain is handled at the end of the frame in P_DamageFeedback
  67. }
  68.  
  69.  
  70. void ClientObituary (edict_t *self, edict_t *inflictor, edict_t *attacker)
  71. {
  72.     if (attacker == self)
  73.     {
  74.         gi.bprintf (PRINT_MEDIUM,"%s killed self.\n", self->client->pers.netname);
  75.         self->client->resp.score--;
  76.         self->enemy = NULL;
  77.         return;
  78.     }
  79.  
  80.     self->enemy = attacker;
  81.     if (attacker && attacker->client)
  82.     {
  83.         gi.bprintf (PRINT_MEDIUM,"%s was killed by %s\n", self->client->pers.netname, attacker->client->pers.netname);
  84.         attacker->client->resp.score++;
  85.         return;
  86.     }
  87.  
  88.     gi.bprintf (PRINT_MEDIUM,"%s died.\n", self->client->pers.netname);
  89.     self->client->resp.score--;
  90. }
  91.  
  92. void TossClientWeapon (edict_t *self)
  93. {
  94.     gitem_t        *item;
  95.     edict_t        *drop;
  96.  
  97.     if (!deathmatch->value)
  98.         return;
  99.     item = self->client->pers.weapon;
  100.     if (!item)
  101.         return;
  102.     if (! self->client->pers.inventory[self->client->ammo_index] )
  103.         return;
  104.     if (!strcmp (item->pickup_name, "Blaster"))
  105.         return;
  106.     drop = Drop_Item (self, item);
  107.     // let them have some ammo with it
  108.     drop->spawnflags = DROPPED_PLAYER_ITEM;
  109. }
  110.  
  111. /*
  112. ==================
  113. LookAtKiller
  114. ==================
  115. */
  116. void LookAtKiller (edict_t *self, edict_t *inflictor, edict_t *attacker)
  117. {
  118.     vec3_t        dir;
  119.  
  120.     if (attacker && attacker != world && attacker != self)
  121.     {
  122.         VectorSubtract (attacker->s.origin, self->s.origin, dir);
  123.     }
  124.     else if (inflictor && inflictor != world && inflictor != self)
  125.     {
  126.         VectorSubtract (inflictor->s.origin, self->s.origin, dir);
  127.     }
  128.     else
  129.     {
  130.         self->client->killer_yaw = self->s.angles[YAW];
  131.         return;
  132.     }
  133.  
  134.     self->client->killer_yaw = 180/M_PI*atan2(dir[1], dir[0]);
  135. }
  136.  
  137. /*
  138. ==================
  139. player_die
  140. ==================
  141. */
  142. void player_die (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  143. {
  144.     char    sound[MAX_QPATH];
  145.     int        n;
  146.  
  147.     VectorClear (self->avelocity);
  148.  
  149.     self->takedamage = DAMAGE_YES;
  150.     self->movetype = MOVETYPE_TOSS;
  151.  
  152.     self->s.modelindex2 = 0;    // remove linked weapon model
  153.  
  154.     self->s.angles[0] = 0;
  155.     self->s.angles[2] = 0;
  156.  
  157.     self->s.sound = 0;
  158.     self->client->weapon_sound = 0;
  159.  
  160.     self->maxs[2] = -8;
  161.  
  162.     self->solid = SOLID_NOT;
  163.  
  164.     if (!self->deadflag)
  165.     {
  166.         self->client->respawn_time = level.time + 1.0;
  167.         LookAtKiller (self, inflictor, attacker);
  168.         self->client->ps.pmove.pm_type = PM_DEAD;
  169.         ClientObituary (self, inflictor, attacker);
  170.         TossClientWeapon (self);
  171.         if (deathmatch->value)
  172.             Cmd_Help_f (self);        // show scores
  173.         memset (self->client->pers.inventory, 0, sizeof(self->client->pers.inventory));
  174.     }
  175.  
  176.     if (self->health < -40)
  177.     {    // gib
  178.         gi.sound (self, CHAN_BODY, gi.soundindex ("misc/udeath.wav"), 1, ATTN_NORM, 0);
  179.         for (n= 0; n < 4; n++)
  180.             ThrowGib (self, "models/objects/gibs/sm_meat/tris.md2", damage, GIB_ORGANIC);
  181.         ThrowClientHead (self, damage);
  182.  
  183.         self->takedamage = DAMAGE_NO;
  184.     }
  185.     else
  186.     {    // normal death
  187.         if (!self->deadflag)
  188.         {
  189.             static int i;
  190.  
  191.             i = (i+1)%3;
  192.             // start a death animation
  193.             self->client->anim_priority = ANIM_DEATH;
  194.             if (self->client->ps.pmove.pm_flags & PMF_DUCKED)
  195.             {
  196.                 self->s.frame = FRAME_crdeath1-1;
  197.                 self->client->anim_end = FRAME_crdeath5;
  198.             }
  199.             else switch (i)
  200.             {
  201.             case 0:
  202.                 self->s.frame = FRAME_death101-1;
  203.                 self->client->anim_end = FRAME_death106;
  204.                 break;
  205.             case 1:
  206.                 self->s.frame = FRAME_death201-1;
  207.                 self->client->anim_end = FRAME_death206;
  208.                 break;
  209.             case 2:
  210.                 self->s.frame = FRAME_death301-1;
  211.                 self->client->anim_end = FRAME_death308;
  212.                 break;
  213.             }
  214.             Com_sprintf(sound, sizeof(sound), "death%i", (rand()%4)+1);
  215.             gi.sound (self, CHAN_VOICE, SexedSoundIndex(self,sound), 1, ATTN_NORM, 0);
  216.         }
  217.     }
  218.  
  219. // FIXME once we have death frames
  220. //    self->deadflag = DEAD_DYING;
  221.     self->deadflag = DEAD_DEAD;
  222.  
  223.     gi.linkentity (self);
  224. }
  225.  
  226. //=======================================================================
  227.  
  228. /*
  229. ==============
  230. InitClientPersistant
  231.  
  232. This is only called when the game first initializes in single player,
  233. but is called after each death and level change in deathmatch
  234. ==============
  235. */
  236. void InitClientPersistant (gclient_t *client)
  237. {
  238.     gitem_t        *item;
  239.  
  240.     memset (&client->pers, 0, sizeof(client->pers));
  241.  
  242.     item = FindItem("Blaster");
  243.     client->pers.selected_item = ITEM_INDEX(item);
  244.     client->pers.inventory[client->pers.selected_item] = 1;
  245.  
  246.     client->pers.weapon = item;
  247.  
  248.     client->pers.health            = 100;
  249.     client->pers.max_health        = 100;
  250.  
  251.     client->pers.max_bullets    = 200;
  252.     client->pers.max_shells        = 100;
  253.     client->pers.max_rockets    = 50;
  254.     client->pers.max_grenades    = 50;
  255.     client->pers.max_cells        = 200;
  256.     client->pers.max_slugs        = 50;
  257. }
  258.  
  259.  
  260. void InitClientResp (gclient_t *client)
  261. {
  262.     memset (&client->resp, 0, sizeof(client->resp));
  263.     client->resp.enterframe = level.framenum;
  264. }
  265.  
  266. /*
  267. ==================
  268. SaveClientData
  269.  
  270. Some information that should be persistant, like health, 
  271. is still stored in the edict structure, so it needs to
  272. be mirrored out to the client structure before all the
  273. edicts are wiped.
  274. ==================
  275. */
  276. void SaveClientData (void)
  277. {
  278.     int        i;
  279.     edict_t    *ent;
  280.  
  281.     for (i=0 ; i<game.maxclients ; i++)
  282.     {
  283.         ent = &g_edicts[1+i];
  284.         if (!ent->inuse)
  285.             continue;
  286.         game.clients[i].pers.health = ent->health;
  287.         game.clients[i].pers.max_health = ent->max_health;
  288.     }
  289. }
  290.  
  291. void FetchClientEntData (edict_t *ent)
  292. {
  293.     ent->health = ent->client->pers.health;
  294.     ent->max_health = ent->client->pers.max_health;
  295. }
  296.  
  297.  
  298.  
  299. /*
  300. =======================================================================
  301.  
  302.   SelectSpawnPoint
  303.  
  304. =======================================================================
  305. */
  306.  
  307. /*
  308. ================
  309. PlayersRangeFromSpot
  310.  
  311. Returns the distance to the nearest player from the given spot
  312. ================
  313. */
  314. float    PlayersRangeFromSpot (edict_t *spot)
  315. {
  316.     edict_t    *player;
  317.     float    bestplayerdistance;
  318.     vec3_t    v;
  319.     int        n;
  320.     float    playerdistance;
  321.  
  322.  
  323.     bestplayerdistance = 9999999;
  324.  
  325.     for (n = 1; n <= maxclients->value; n++)
  326.     {
  327.         player = &g_edicts[n];
  328.  
  329.         if (!player->inuse)
  330.             continue;
  331.  
  332.         if (player->health <= 0)
  333.             continue;
  334.  
  335.         VectorSubtract (spot->s.origin, player->s.origin, v);
  336.         playerdistance = VectorLength (v);
  337.  
  338.         if (playerdistance < bestplayerdistance)
  339.             bestplayerdistance = playerdistance;
  340.     }
  341.  
  342.     return bestplayerdistance;
  343. }
  344.  
  345. /*
  346. ================
  347. SelectRandomDeathmatchSpawnPoint
  348.  
  349. go to a random point, but NOT the two points closest
  350. to other players
  351. ================
  352. */
  353. edict_t *SelectRandomDeathmatchSpawnPoint (void)
  354. {
  355.     edict_t    *spot, *spot1, *spot2;
  356.     int        count = 0;
  357.     int        selection;
  358.     float    range, range1, range2;
  359.  
  360.     spot = NULL;
  361.     range1 = range2 = 99999;
  362.     spot1 = spot2 = NULL;
  363.  
  364.     while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
  365.     {
  366.         count++;
  367.         range = PlayersRangeFromSpot(spot);
  368.         if (range < range1)
  369.         {
  370.             range1 = range;
  371.             spot1 = spot;
  372.         }
  373.         else if (range < range2)
  374.         {
  375.             range2 = range;
  376.             spot2 = spot;
  377.         }
  378.     }
  379.  
  380.     if (!count)
  381.         return NULL;
  382.  
  383.     if (count <= 2)
  384.     {
  385.         spot1 = spot2 = NULL;
  386.     }
  387.     else
  388.         count -= 2;
  389.  
  390.     selection = rand() % count;
  391.  
  392.     spot = NULL;
  393.     do
  394.     {
  395.         spot = G_Find (spot, FOFS(classname), "info_player_deathmatch");
  396.         if (spot == spot1 || spot == spot2)
  397.             selection++;
  398.     } while(selection--);
  399.  
  400.     return spot;
  401. }
  402.  
  403. /*
  404. ================
  405. SelectFarthestDeathmatchSpawnPoint
  406.  
  407. ================
  408. */
  409. edict_t *SelectFarthestDeathmatchSpawnPoint (void)
  410. {
  411.     edict_t    *bestspot;
  412.     float    bestdistance, bestplayerdistance;
  413.     edict_t    *spot;
  414.  
  415.  
  416.     spot = NULL;
  417.     bestspot = NULL;
  418.     bestdistance = 0;
  419.     while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
  420.     {
  421.         bestplayerdistance = PlayersRangeFromSpot (spot);
  422.  
  423.         if (bestplayerdistance > bestdistance)
  424.         {
  425.             bestspot = spot;
  426.             bestdistance = bestplayerdistance;
  427.         }
  428.     }
  429.  
  430.     if (bestspot)
  431.     {
  432.         return bestspot;
  433.     }
  434.  
  435.     // if there is a player just spawned on each and every start spot
  436.     // we have no choice to turn one into a telefrag meltdown
  437.     spot = G_Find (NULL, FOFS(classname), "info_player_deathmatch");
  438.  
  439.     return spot;
  440. }
  441.  
  442. edict_t *SelectDeathmatchSpawnPoint (void)
  443. {
  444.     if ( (int)(dmflags->value) & DF_SPAWN_FARTHEST)
  445.         return SelectFarthestDeathmatchSpawnPoint ();
  446.     else
  447.         return SelectRandomDeathmatchSpawnPoint ();
  448. }
  449.  
  450.  
  451. edict_t *SelectCoopSpawnPoint (void)
  452. {
  453.     edict_t    *spot = NULL;
  454.  
  455.     while ((spot = G_Find (spot, FOFS(classname), "info_player_coop")) != NULL)
  456.     {
  457.         if (!game.spawnpoint[0] && !spot->targetname)
  458.             break;
  459.  
  460.         if (!game.spawnpoint[0] || !spot->targetname)
  461.             continue;
  462.  
  463.         if (Q_stricmp(game.spawnpoint, spot->targetname) == 0)
  464.             break;
  465.     }
  466.     return spot;
  467. }
  468.  
  469.  
  470. /*
  471. ===========
  472. SelectSpawnPoint
  473.  
  474. Chooses a player start, deathmatch start, coop start, etc
  475. ============
  476. */
  477. void    SelectSpawnPoint (vec3_t origin, vec3_t angles)
  478. {
  479.     edict_t    *spot = NULL;
  480.  
  481.     if (deathmatch->value)
  482.         spot = SelectDeathmatchSpawnPoint ();
  483. #if 0
  484.     else if (coop->value)
  485.         spot = SelectCoopSpawnPoint ();
  486. #endif
  487.  
  488.     // find a single player start spot
  489.     if (!spot)
  490.     {
  491.         while ((spot = G_Find (spot, FOFS(classname), "info_player_start")) != NULL)
  492.         {
  493.             if (!game.spawnpoint[0] && !spot->targetname)
  494.                 break;
  495.  
  496.             if (!game.spawnpoint[0] || !spot->targetname)
  497.                 continue;
  498.  
  499.             if (Q_stricmp(game.spawnpoint, spot->targetname) == 0)
  500.                 break;
  501.         }
  502.  
  503.         if (!spot)
  504.         {
  505.             if (!game.spawnpoint[0])
  506.             {    // there wasn't a spawnpoint without a target, so use any
  507.                 spot = G_Find (spot, FOFS(classname), "info_player_start");
  508.             }
  509.             if (!spot)
  510.                 gi.error ("Couldn't find spawn point %s\n", game.spawnpoint);
  511.         }
  512.     }
  513.  
  514.     VectorCopy (spot->s.origin, origin);
  515.     origin[2] += 9;
  516.     VectorCopy (spot->s.angles, angles);
  517. }
  518.  
  519. //======================================================================
  520.  
  521.  
  522. void InitBodyQue (void)
  523. {
  524.     int        i;
  525.     edict_t    *ent, *head;
  526.  
  527.     head = G_Spawn();
  528.     head->chain = head;
  529.     level.body_que = head;
  530.  
  531.     for (i=0 ; i<7 ; i++)
  532.     {
  533.         ent = G_Spawn();
  534.         ent->chain = head->chain;
  535.         head->chain = ent;
  536.     }
  537. }
  538.  
  539. void CopyToBodyQue (edict_t *ent)
  540. {
  541.     edict_t        *body;
  542.  
  543.     // grab a body que and cycle to the next one
  544.     body = level.body_que;
  545.     level.body_que = body->chain;
  546.  
  547.     // FIXME: send an effect on the removed body
  548.  
  549.     gi.unlinkentity (ent);
  550.  
  551.     gi.unlinkentity (body);
  552.     body->s = ent->s;
  553.     body->s.number = body - g_edicts;
  554.  
  555.     // FIXME: make fall to ground?
  556.     gi.linkentity (body);
  557. }
  558.  
  559.  
  560. void respawn (edict_t *self)
  561. {
  562.     if (deathmatch->value /*|| coop->value */)
  563.     {
  564.         // FIXME: make bodyque objects obey gravity
  565.         CopyToBodyQue (self);
  566.         PutClientInServer (self);
  567.  
  568.         // add a teleportation effect
  569.         self->s.event = EV_PLAYER_TELEPORT;
  570.  
  571.         // hold in place briefly
  572.         self->client->ps.pmove.teleport_time = 50;
  573.         return;
  574.     }
  575.  
  576.     // restart the entire server
  577.     gi.AddCommandString ("menu_loadgame\n");
  578. }
  579.  
  580. //==============================================================
  581.  
  582.  
  583. /*
  584. ===========
  585. PutClientInServer
  586.  
  587. Called when a player connects to a server or respawns in
  588. a deathmatch.
  589. ============
  590. */
  591. void PutClientInServer (edict_t *ent)
  592. {
  593.     vec3_t    mins = {-16, -16, -24};
  594.     vec3_t    maxs = {16, 16, 32};
  595.     int        index;
  596.     vec3_t    spawn_origin, spawn_angles;
  597.     gclient_t    *client;
  598.     int        i;
  599.     client_persistant_t    saved;
  600.     client_respawn_t    resp;
  601.  
  602.     // find a spawn point
  603.     // do it before setting health back up, so farthest
  604.     // ranging doesn't count this client
  605.     SelectSpawnPoint (spawn_origin, spawn_angles);
  606.  
  607.     index = ent-g_edicts-1;
  608.     client = ent->client;
  609.  
  610.     // deathmatch wipes most client data every spawn
  611.     if (deathmatch->value)
  612.     {
  613.         char        userinfo[MAX_INFO_STRING];
  614.  
  615.         resp = ent->client->resp;
  616.         memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));
  617.         InitClientPersistant (client);
  618.         ClientUserinfoChanged (ent, userinfo);
  619.     }
  620.     else
  621.         memset (&resp, 0, sizeof(resp));
  622.  
  623.     // clear everything but the persistant data
  624.     saved = client->pers;
  625.     memset (client, 0, sizeof(*client));
  626.     client->pers = saved;
  627.     client->resp = resp;
  628.  
  629.     // copy some data from the client to the entity
  630.     FetchClientEntData (ent);
  631.  
  632.     // clear entity values
  633.     ent->groundentity = NULL;
  634.     ent->client = &game.clients[index];
  635.     ent->takedamage = DAMAGE_AIM;
  636.     ent->movetype = MOVETYPE_WALK;
  637.     ent->viewheight = 22;
  638.     ent->inuse = true;
  639.     ent->classname = "player";
  640.     ent->mass = 200;
  641.     ent->solid = SOLID_BBOX;
  642.     ent->deadflag = DEAD_NO;
  643.     ent->air_finished = level.time + 12;
  644.     ent->clipmask = MASK_PLAYERSOLID;
  645.     ent->model = "players/male/tris.md2";
  646.     ent->pain = player_pain;
  647.     ent->die = player_die;
  648.     ent->waterlevel = 0;
  649.     ent->watertype = 0;
  650.     ent->flags &= ~FL_NO_KNOCKBACK;
  651.  
  652.     VectorCopy (mins, ent->mins);
  653.     VectorCopy (maxs, ent->maxs);
  654.     VectorClear (ent->velocity);
  655.  
  656.     // clear playerstate values
  657.     memset (&ent->client->ps, 0, sizeof(client->ps));
  658.  
  659.     client->ps.pmove.origin[0] = spawn_origin[0]*8;
  660.     client->ps.pmove.origin[1] = spawn_origin[1]*8;
  661.     client->ps.pmove.origin[2] = spawn_origin[2]*8;
  662.  
  663.     client->ps.fov = 90;
  664.     client->ps.gunindex = gi.modelindex(client->pers.weapon->view_model);
  665.  
  666.     // clear entity state values
  667.     ent->s.effects = 0;
  668.     ent->s.skinnum = ent - g_edicts - 1;
  669.     ent->s.modelindex = 255;        // will use the skin specified model
  670.     ent->s.modelindex2 = 255;        // custom gun model
  671.     ent->s.frame = 0;
  672.     VectorCopy (spawn_origin, ent->s.origin);
  673.     ent->s.origin[2] += 1;    // make sure off ground
  674.  
  675.     // set the delta angle
  676.     for (i=0 ; i<3 ; i++)
  677.         client->ps.pmove.delta_angles[i] = ANGLE2SHORT(spawn_angles[i] - client->resp.cmd_angles[i]);
  678.  
  679.     ent->s.angles[PITCH] = 0;
  680.     ent->s.angles[YAW] = spawn_angles[YAW];
  681.     ent->s.angles[ROLL] = 0;
  682.     VectorCopy (ent->s.angles, client->ps.viewangles);
  683.     VectorCopy (ent->s.angles, client->v_angle);
  684.  
  685.     if (!KillBox (ent))
  686.     {    // could't spawn in?
  687.     }
  688.  
  689.     gi.linkentity (ent);
  690.  
  691.     // force the current weapon up
  692.     client->newweapon = client->pers.weapon;
  693.     ChangeWeapon (ent);
  694. }
  695.  
  696. /*
  697. =====================
  698. ClientBeginDeathmatch
  699.  
  700. A client has just connected to the server in 
  701. deathmatch mode, so clear everything out before starting them.
  702. =====================
  703. */
  704. void ClientBeginDeathmatch (edict_t *ent)
  705. {
  706.     G_InitEdict (ent);
  707.  
  708.     InitClientResp (ent->client);
  709.  
  710.     // locate ent at a spawn point
  711.     PutClientInServer (ent);
  712.  
  713.     // send effect
  714.     gi.WriteByte (svc_muzzleflash);
  715.     gi.WriteShort (ent-g_edicts);
  716.     gi.WriteByte (MZ_LOGIN);
  717.     gi.multicast (ent->s.origin, MULTICAST_PVS);
  718.  
  719.     gi.bprintf (PRINT_HIGH, "%s entered the game\n", ent->client->pers.netname);
  720.  
  721.     // make sure all view stuff is valid
  722.     ClientEndServerFrame (ent);
  723. }
  724.  
  725.  
  726. /*
  727. ===========
  728. ClientBegin
  729.  
  730. called when a client has finished connecting, and is ready
  731. to be placed into the game.  This will happen every level load.
  732. ============
  733. */
  734. void ClientBegin (edict_t *ent, qboolean loadgame)
  735. {
  736.     int        i;
  737.  
  738.     if (deathmatch->value)
  739.     {
  740.         ClientBeginDeathmatch (ent);
  741.         return;
  742.     }
  743.  
  744.     if (loadgame && ent->classname && !strcmp(ent->classname, "player") )
  745.     {
  746.         // a loadgame will just use the entity exactly as it is
  747.         // if more clients connect than were saved, they will be
  748.         // spawned as normal
  749.         ent->inuse = true;
  750.  
  751.         // the client has cleared the client side viewangles upon
  752.         // connecting to the server, which is different than the
  753.         // state when the game is saved, so we need to compensate
  754.         // with deltaangles
  755.         for (i=0 ; i<3 ; i++)
  756.             ent->client->ps.pmove.delta_angles[i] = ANGLE2SHORT(ent->client->ps.viewangles[i]);
  757.     }
  758.     else
  759.     {
  760.         // a spawn point will completely reinitialize the entity
  761.         G_InitEdict (ent);
  762.         InitClientResp (ent->client);
  763.         PutClientInServer (ent);
  764.     }
  765.  
  766.     if (level.intermissiontime)
  767.     {
  768.         MoveClientToIntermission (ent);
  769.     }
  770.     else
  771.     {
  772.         // send effect if in a multiplayer game
  773.         if (game.maxclients > 1)
  774.         {
  775.             gi.WriteByte (svc_muzzleflash);
  776.             gi.WriteShort (ent-g_edicts);
  777.             gi.WriteByte (MZ_LOGIN);
  778.             gi.multicast (ent->s.origin, MULTICAST_PVS);
  779.  
  780.             gi.bprintf (PRINT_HIGH, "%s entered the game\n", ent->client->pers.netname);
  781.         }
  782.     }
  783.  
  784.     // make sure all view stuff is valid
  785.     ClientEndServerFrame (ent);
  786. }
  787.  
  788. /*
  789. ===========
  790. ClientUserInfoChanged
  791.  
  792. called whenever the player updates a userinfo variable.
  793.  
  794. The game can override any of the settings in place
  795. (forcing skins or names, etc) before copying it off.
  796. ============
  797. */
  798. void ClientUserinfoChanged (edict_t *ent, char *userinfo)
  799. {
  800.     char    *s;
  801.     int        playernum;
  802.  
  803.     // set name
  804.     s = Info_ValueForKey (userinfo, "name");
  805.     strncpy (ent->client->pers.netname, s, sizeof(ent->client->pers.netname)-1);
  806.  
  807.     // set skin
  808.     s = Info_ValueForKey (userinfo, "skin");
  809.  
  810.     // decide sound directory
  811.     if (s[0] == 'f' || s[0] == 'F')
  812.         strcpy (ent->client->pers.sounddir, "player/female");
  813.     else
  814.         strcpy (ent->client->pers.sounddir, "player/male");
  815.  
  816.     playernum = ent-g_edicts-1;
  817.  
  818.     // combine name and skin into a configstring
  819.     gi.configstring (CS_PLAYERSKINS+playernum, va("%s\\%s", ent->client->pers.netname, s) );
  820.  
  821.     // handedness
  822.     s = Info_ValueForKey (userinfo, "hand");
  823.     if (strlen(s))
  824.     {
  825.         ent->client->pers.hand = atoi(s);
  826.     }
  827.  
  828.     // save off the userinfo in case we want to check something later
  829.     strncpy (ent->client->pers.userinfo, userinfo, sizeof(ent->client->pers.userinfo)-1);
  830. }
  831.  
  832.  
  833. /*
  834. ===========
  835. ClientConnect
  836.  
  837. Called when a player begins connecting to the server.
  838. The game can refuse entrance to a client by returning false.
  839. If the client is allowed, the connection process will continue
  840. and eventually get to ClientBegin()
  841. Changing levels will NOT cause this to be called again.
  842. ============
  843. */
  844. qboolean ClientConnect (edict_t *ent, char *userinfo, qboolean loadgame)
  845. {
  846.     if (!loadgame)
  847.     {
  848.         // clear the respawning variables
  849.         InitClientResp (ent->client);
  850.         InitClientPersistant (ent->client);
  851.     }
  852.  
  853.     ClientUserinfoChanged (ent, userinfo);
  854.  
  855.     if (game.maxclients > 1)
  856.         gi.dprintf ("%s connected\n", ent->client->pers.netname);
  857.  
  858.     level.players++;
  859.     return true;
  860. }
  861.  
  862. /*
  863. ===========
  864. ClientDisconnect
  865.  
  866. called when a player drops from the server
  867.  
  868. ============
  869. */
  870. void ClientDisconnect (edict_t *ent)
  871. {
  872.     int        playernum;
  873.  
  874.     if (!ent->client)
  875.         return;
  876.  
  877.     gi.bprintf (PRINT_HIGH, "%s disconnected\n", ent->client->pers.netname);
  878.  
  879.     // send effect
  880.     gi.WriteByte (svc_muzzleflash);
  881.     gi.WriteShort (ent-g_edicts);
  882.     gi.WriteByte (MZ_LOGOUT);
  883.     gi.multicast (ent->s.origin, MULTICAST_PVS);
  884.  
  885.     ent->s.modelindex = 0;
  886.     ent->solid = SOLID_NOT;
  887.     gi.linkentity (ent);
  888.  
  889.     ent->inuse = false;
  890.  
  891.     level.players--;
  892.  
  893.     playernum = ent-g_edicts-1;
  894.     gi.configstring (CS_PLAYERSKINS+playernum, "");
  895. }
  896.  
  897.  
  898. //==============================================================
  899.  
  900.  
  901. void respawn (edict_t *self);
  902.  
  903. edict_t    *pm_passent;
  904.  
  905. // pmove doesn't need to know about passent and contentmask
  906. trace_t    PM_trace (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
  907. {
  908.     if (pm_passent->health > 0)
  909.         return gi.trace (start, mins, maxs, end, pm_passent, MASK_PLAYERSOLID);
  910.     else
  911.         return gi.trace (start, mins, maxs, end, pm_passent, MASK_DEADSOLID);
  912. }
  913.  
  914. unsigned CheckBlock (void *b, int c)
  915. {
  916.     int    v,i;
  917.     v = 0;
  918.     for (i=0 ; i<c ; i++)
  919.         v+= ((byte *)b)[i];
  920.     return v;
  921. }
  922. void PrintPmove (pmove_t *pm)
  923. {
  924.     unsigned    c1, c2;
  925.  
  926.     c1 = CheckBlock (&pm->s, sizeof(pm->s));
  927.     c2 = CheckBlock (&pm->cmd, sizeof(pm->cmd));
  928.     Com_Printf ("sv %3i:%i %i\n", pm->cmd.impulse, c1, c2);
  929. }
  930.  
  931. /*
  932. ==============
  933. ClientThink
  934.  
  935. This will be called once for each client frame, which will
  936. usually be a couple times for each server frame.
  937. ==============
  938. */
  939. void ClientThink (edict_t *ent, usercmd_t *ucmd)
  940. {
  941.     gclient_t    *client;
  942.     edict_t    *other;
  943.     int        i, j;
  944.     pmove_t    pm;
  945.  
  946.     level.current_entity = ent;
  947.     client = ent->client;
  948.  
  949.     if (level.intermissiontime)
  950.     {
  951.         client->ps.pmove.pm_type = PM_FREEZE;
  952.         // can exit intermission after five seconds
  953.         if (level.time > level.intermissiontime + 5.0 
  954.             && (ucmd->buttons & BUTTON_ANY) )
  955.             level.exitintermission = true;
  956.         return;
  957.     }
  958.  
  959.     pm_passent = ent;
  960.  
  961.     // set up for pmove
  962.     memset (&pm, 0, sizeof(pm));
  963.  
  964.     if (ent->movetype == MOVETYPE_NOCLIP)
  965.         client->ps.pmove.pm_type = PM_SPECTATOR;
  966.     else if (ent->s.modelindex != 255)
  967.         client->ps.pmove.pm_type = PM_GIB;
  968.     else if (ent->deadflag)
  969.         client->ps.pmove.pm_type = PM_DEAD;
  970.     else
  971.         client->ps.pmove.pm_type = PM_NORMAL;
  972.  
  973.     client->ps.pmove.gravity = sv_gravity->value;
  974.     pm.s = client->ps.pmove;
  975.  
  976.     for (i=0 ; i<3 ; i++)
  977.     {
  978.         pm.s.origin[i] = ent->s.origin[i]*8;
  979.         pm.s.velocity[i] = ent->velocity[i]*8;
  980.     }
  981.  
  982.     if (memcmp(&client->old_pmove, &pm.s, sizeof(pm.s)))
  983.     {
  984.         pm.snapinitial = true;
  985. //        gi.dprintf ("pmove changed!\n");
  986.     }
  987.  
  988.     pm.cmd = *ucmd;
  989.  
  990.     pm.trace = PM_trace;    // adds default parms
  991.     pm.pointcontents = gi.pointcontents;
  992.  
  993.     // perform a pmove
  994.     gi.Pmove (&pm);
  995.  
  996.     // save results of pmove
  997.     client->ps.pmove = pm.s;
  998.     client->old_pmove = pm.s;
  999.  
  1000.     for (i=0 ; i<3 ; i++)
  1001.     {
  1002.         ent->s.origin[i] = pm.s.origin[i]*0.125;
  1003.         ent->velocity[i] = pm.s.velocity[i]*0.125;
  1004.     }
  1005.  
  1006.     VectorCopy (pm.mins, ent->mins);
  1007.     VectorCopy (pm.maxs, ent->maxs);
  1008.  
  1009.     client->resp.cmd_angles[0] = SHORT2ANGLE(ucmd->angles[0]);
  1010.     client->resp.cmd_angles[1] = SHORT2ANGLE(ucmd->angles[1]);
  1011.     client->resp.cmd_angles[2] = SHORT2ANGLE(ucmd->angles[2]);
  1012.  
  1013.     if (ent->groundentity && !pm.groundentity && (pm.cmd.upmove >= 10) && (pm.waterlevel == 0))
  1014.     {
  1015.         gi.sound(ent, CHAN_VOICE, SexedSoundIndex(ent, "jump1"), 1, ATTN_NORM, 0);
  1016.         PlayerNoise(ent, ent->s.origin, PNOISE_SELF);
  1017.     }
  1018.  
  1019.     ent->viewheight = pm.viewheight;
  1020.     ent->waterlevel = pm.waterlevel;
  1021.     ent->watertype = pm.watertype;
  1022.     ent->groundentity = pm.groundentity;
  1023.     if (pm.groundentity)
  1024.         ent->groundentity_linkcount = pm.groundentity->linkcount;
  1025.  
  1026.     if (ent->deadflag)
  1027.     {
  1028.         client->ps.viewangles[ROLL] = 40;
  1029.         client->ps.viewangles[PITCH] = -15;
  1030.         client->ps.viewangles[YAW] = client->killer_yaw;
  1031.     }
  1032.     else
  1033.     {
  1034.         VectorCopy (pm.viewangles, client->v_angle);
  1035.         VectorCopy (pm.viewangles, client->ps.viewangles);
  1036.     }
  1037.  
  1038.  
  1039.     gi.linkentity (ent);
  1040.  
  1041.     if (ent->movetype != MOVETYPE_NOCLIP)
  1042.         G_TouchTriggers (ent);
  1043.  
  1044.     // touch other objects
  1045.     for (i=0 ; i<pm.numtouch ; i++)
  1046.     {
  1047.         other = pm.touchents[i];
  1048.         for (j=0 ; j<i ; j++)
  1049.             if (pm.touchents[j] == other)
  1050.                 break;
  1051.         if (j != i)
  1052.             continue;    // duplicated
  1053.         if (!other->touch)
  1054.             continue;
  1055.         other->touch (other, ent, NULL, NULL);
  1056.     }
  1057.  
  1058.  
  1059.     client->oldbuttons = client->buttons;
  1060.     client->buttons = ucmd->buttons;
  1061.     client->latched_buttons |= client->buttons & ~client->oldbuttons;
  1062.  
  1063.     // save light level the player is standing on for
  1064.     // monster sighting AI
  1065.     ent->light_level = ucmd->lightlevel;
  1066.  
  1067.     // fire weapon from final position if needed
  1068.     if (client->latched_buttons & BUTTON_ATTACK)
  1069.     {
  1070.         if (!client->weapon_thunk)
  1071.         {
  1072.             client->weapon_thunk = true;
  1073.             Think_Weapon (ent);
  1074.         }
  1075.     }
  1076.  
  1077.  
  1078. }
  1079.  
  1080.  
  1081. /*
  1082. ==============
  1083. ClientBeginServerFrame
  1084.  
  1085. This will be called once for each server frame, before running
  1086. any other entities in the world.
  1087. ==============
  1088. */
  1089. void ClientBeginServerFrame (edict_t *ent)
  1090. {
  1091.     gclient_t    *client;
  1092.  
  1093.     if (level.intermissiontime)
  1094.         return;
  1095.  
  1096.     client = ent->client;
  1097.  
  1098.     // run weapon animations if it hasn't been done by a ucmd_t
  1099.     if (!client->weapon_thunk)
  1100.         Think_Weapon (ent);
  1101.     else
  1102.         client->weapon_thunk = false;
  1103.  
  1104.     if (ent->deadflag)
  1105.     {
  1106.         // wait for any button just going down
  1107.         if ( level.time > client->respawn_time)
  1108.         {
  1109.             if (client->latched_buttons ||
  1110.                 (deathmatch->value && ((int)dmflags->value & DF_FORCE_RESPAWN) ) )
  1111.             {
  1112.                 respawn(ent);
  1113.                 client->latched_buttons = 0;
  1114.             }
  1115.         }
  1116.         return;
  1117.     }
  1118.  
  1119.     // add player trail so monsters can follow
  1120.     if (!deathmatch->value)
  1121.         if (!visible (ent, PlayerTrail_LastSpot() ) )
  1122.             PlayerTrail_Add (ent->s.old_origin);
  1123.  
  1124.     client->latched_buttons = 0;
  1125. }
  1126.