Debian Codegraph @

code/game/g_client.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 #include "g_local.h"
24
25 // g_client.c -- client functions that don't happen every frame
26
27 static vec3_t playerMins = {-15, -15, -24};
28 static vec3_t playerMaxs = {15, 15, 32};
29
30 /*QUAKED info_player_deathmatch (1 0 1) (-16 -16 -24) (16 16 32) initial
31 potential spawning position for deathmatch games.
32 The first time a player enters the game, they will be at an 'initial' spot.
33 Targets will be fired when someone spawns in on them.
34 "nobots" will prevent bots from using this spot.
35 "nohumans" will prevent non-bots from using this spot.
36 */
37 void SP_info_player_deathmatch( gentity_t *ent ) {
38 int i;
39
40 G_SpawnInt( "nobots", "0", &i);
41 if ( i ) {
42 ent->flags |= FL_NO_BOTS;
43 }
44 G_SpawnInt( "nohumans", "0", &i );
45 if ( i ) {
46 ent->flags |= FL_NO_HUMANS;
47 }
48 }
49
50 /*QUAKED info_player_start (1 0 0) (-16 -16 -24) (16 16 32)
51 equivalent to info_player_deathmatch
52 */
53 void SP_info_player_start(gentity_t *ent) {
54 ent->classname = "info_player_deathmatch";
55 SP_info_player_deathmatch( ent );
56 }
57
58 /*QUAKED info_player_intermission (1 0 1) (-16 -16 -24) (16 16 32)
59 The intermission will be viewed from this point. Target an info_notnull for the view direction.
60 */
61 void SP_info_player_intermission( gentity_t *ent ) {
62
63 }
64
65
66
67 /*
68 =======================================================================
69
70 SelectSpawnPoint
71
72 =======================================================================
73 */
74
75 /*
76 ================
77 SpotWouldTelefrag
78
79 ================
80 */
81 qboolean SpotWouldTelefrag( gentity_t *spot ) {
82 int i, num;
83 int touch[MAX_GENTITIES];
84 gentity_t *hit;
85 vec3_t mins, maxs;
86
87 VectorAdd( spot->s.origin, playerMins, mins );
88 VectorAdd( spot->s.origin, playerMaxs, maxs );
89 num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES );
90
91 for (i=0 ; i<num ; i++) {
92 hit = &g_entities[touch[i]];
93 //if ( hit->client && hit->client->ps.stats[STAT_HEALTH] > 0 ) {
94 if ( hit->client) {
95 return qtrue;
96 }
97
98 }
99
100 return qfalse;
101 }
102
103 /*
104 ================
105 SelectNearestDeathmatchSpawnPoint
106
107 Find the spot that we DON'T want to use
108 ================
109 */
110 #define MAX_SPAWN_POINTS 128
111 gentity_t *SelectNearestDeathmatchSpawnPoint( vec3_t from ) {
112 gentity_t *spot;
113 vec3_t delta;
114 float dist, nearestDist;
115 gentity_t *nearestSpot;
116
117 nearestDist = 999999;
118 nearestSpot = NULL;
119 spot = NULL;
120
121 while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL) {
122
123 VectorSubtract( spot->s.origin, from, delta );
124 dist = VectorLength( delta );
125 if ( dist < nearestDist ) {
126 nearestDist = dist;
127 nearestSpot = spot;
128 }
129 }
130
131 return nearestSpot;
132 }
133
134
135 /*
136 ================
137 SelectRandomDeathmatchSpawnPoint
138
139 go to a random point that doesn't telefrag
140 ================
141 */
142 #define MAX_SPAWN_POINTS 128
143 gentity_t *SelectRandomDeathmatchSpawnPoint(qboolean isbot) {
144 gentity_t *spot;
145 int count;
146 int selection;
147 gentity_t *spots[MAX_SPAWN_POINTS];
148
149 count = 0;
150 spot = NULL;
151
152 while((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL && count < MAX_SPAWN_POINTS)
153 {
154 if(SpotWouldTelefrag(spot))
155 continue;
156
157 if(((spot->flags & FL_NO_BOTS) && isbot) ||
158 ((spot->flags & FL_NO_HUMANS) && !isbot))
159 {
160 // spot is not for this human/bot player
161 continue;
162 }
163
164 spots[count] = spot;
165 count++;
166 }
167
168 if ( !count ) { // no spots that won't telefrag
169 return G_Find( NULL, FOFS(classname), "info_player_deathmatch");
170 }
171
172 selection = rand() % count;
173 return spots[ selection ];
174 }
175
176 /*
177 ===========
178 SelectRandomFurthestSpawnPoint
179
180 Chooses a player start, deathmatch start, etc
181 ============
182 */
183 gentity_t *SelectRandomFurthestSpawnPoint ( vec3_t avoidPoint, vec3_t origin, vec3_t angles, qboolean isbot ) {
184 gentity_t *spot;
185 vec3_t delta;
186 float dist;
187 float list_dist[MAX_SPAWN_POINTS];
188 gentity_t *list_spot[MAX_SPAWN_POINTS];
189 int numSpots, rnd, i, j;
190
191 numSpots = 0;
192 spot = NULL;
193
194 while((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
195 {
196 if(SpotWouldTelefrag(spot))
197 continue;
198
199 if(((spot->flags & FL_NO_BOTS) && isbot) ||
200 ((spot->flags & FL_NO_HUMANS) && !isbot))
201 {
202 // spot is not for this human/bot player
203 continue;
204 }
205
206 VectorSubtract( spot->s.origin, avoidPoint, delta );
207 dist = VectorLength( delta );
208
209 for (i = 0; i < numSpots; i++)
210 {
211 if(dist > list_dist[i])
212 {
213 if (numSpots >= MAX_SPAWN_POINTS)
214 numSpots = MAX_SPAWN_POINTS - 1;
215
216 for(j = numSpots; j > i; j--)
217 {
218 list_dist[j] = list_dist[j-1];
219 list_spot[j] = list_spot[j-1];
220 }
221
222 list_dist[i] = dist;
223 list_spot[i] = spot;
224
225 numSpots++;
226 break;
227 }
228 }
229
230 if(i >= numSpots && numSpots < MAX_SPAWN_POINTS)
231 {
232 list_dist[numSpots] = dist;
233 list_spot[numSpots] = spot;
234 numSpots++;
235 }
236 }
237
238 if(!numSpots)
239 {
240 spot = G_Find(NULL, FOFS(classname), "info_player_deathmatch");
241
242 if (!spot)
243 G_Error( "Couldn't find a spawn point" );
244
245 VectorCopy (spot->s.origin, origin);
246 origin[2] += 9;
247 VectorCopy (spot->s.angles, angles);
248 return spot;
249 }
250
251 // select a random spot from the spawn points furthest away
252 rnd = random() * (numSpots / 2);
253
254 VectorCopy (list_spot[rnd]->s.origin, origin);
255 origin[2] += 9;
256 VectorCopy (list_spot[rnd]->s.angles, angles);
257
258 return list_spot[rnd];
259 }
260
261 /*
262 ===========
263 SelectSpawnPoint
264
265 Chooses a player start, deathmatch start, etc
266 ============
267 */
268 gentity_t *SelectSpawnPoint ( vec3_t avoidPoint, vec3_t origin, vec3_t angles, qboolean isbot ) {
269 return SelectRandomFurthestSpawnPoint( avoidPoint, origin, angles, isbot );
270
271 /*
272 gentity_t *spot;
273 gentity_t *nearestSpot;
274
275 nearestSpot = SelectNearestDeathmatchSpawnPoint( avoidPoint );
276
277 spot = SelectRandomDeathmatchSpawnPoint ( );
278 if ( spot == nearestSpot ) {
279 // roll again if it would be real close to point of death
280 spot = SelectRandomDeathmatchSpawnPoint ( );
281 if ( spot == nearestSpot ) {
282 // last try
283 spot = SelectRandomDeathmatchSpawnPoint ( );
284 }
285 }
286
287 // find a single player start spot
288 if (!spot) {
289 G_Error( "Couldn't find a spawn point" );
290 }
291
292 VectorCopy (spot->s.origin, origin);
293 origin[2] += 9;
294 VectorCopy (spot->s.angles, angles);
295
296 return spot;
297 */
298 }
299
300 /*
301 ===========
302 SelectInitialSpawnPoint
303
304 Try to find a spawn point marked 'initial', otherwise
305 use normal spawn selection.
306 ============
307 */
308 gentity_t *SelectInitialSpawnPoint( vec3_t origin, vec3_t angles, qboolean isbot ) {
309 gentity_t *spot;
310
311 spot = NULL;
312
313 while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
314 {
315 if(((spot->flags & FL_NO_BOTS) && isbot) ||
316 ((spot->flags & FL_NO_HUMANS) && !isbot))
317 {
318 continue;
319 }
320
321 if((spot->spawnflags & 0x01))
322 break;
323 }
324
325 if (!spot || SpotWouldTelefrag(spot))
326 return SelectSpawnPoint(vec3_origin, origin, angles, isbot);
327
328 VectorCopy (spot->s.origin, origin);
329 origin[2] += 9;
330 VectorCopy (spot->s.angles, angles);
331
332 return spot;
333 }
334
335 /*
336 ===========
337 SelectSpectatorSpawnPoint
338
339 ============
340 */
341 gentity_t *SelectSpectatorSpawnPoint( vec3_t origin, vec3_t angles ) {
342 FindIntermissionPoint();
343
344 VectorCopy( level.intermission_origin, origin );
345 VectorCopy( level.intermission_angle, angles );
346
347 return NULL;
348 }
349
350 /*
351 =======================================================================
352
353 BODYQUE
354
355 =======================================================================
356 */
357
358 /*
359 ===============
360 InitBodyQue
361 ===============
362 */
363 void InitBodyQue (void) {
364 int i;
365 gentity_t *ent;
366
367 level.bodyQueIndex = 0;
368 for (i=0; i<BODY_QUEUE_SIZE ; i++) {
369 ent = G_Spawn();
370 ent->classname = "bodyque";
371 ent->neverFree = qtrue;
372 level.bodyQue[i] = ent;
373 }
374 }
375
376 /*
377 =============
378 BodySink
379
380 After sitting around for five seconds, fall into the ground and disappear
381 =============
382 */
383 void BodySink( gentity_t *ent ) {
384 if ( level.time - ent->timestamp > 6500 ) {
385 // the body ques are never actually freed, they are just unlinked
386 trap_UnlinkEntity( ent );
387 ent->physicsObject = qfalse;
388 return;
389 }
390 ent->nextthink = level.time + 100;
391 ent->s.pos.trBase[2] -= 1;
392 }
393
394 /*
395 =============
396 CopyToBodyQue
397
398 A player is respawning, so make an entity that looks
399 just like the existing corpse to leave behind.
400 =============
401 */
402 void CopyToBodyQue( gentity_t *ent ) {
403 #ifdef MISSIONPACK
404 gentity_t *e;
405 int i;
406 #endif
407 gentity_t *body;
408 int contents;
409
410 trap_UnlinkEntity (ent);
411
412 // if client is in a nodrop area, don't leave the body
413 contents = trap_PointContents( ent->s.origin, -1 );
414 if ( contents & CONTENTS_NODROP ) {
415 return;
416 }
417
418 // grab a body que and cycle to the next one
419 body = level.bodyQue[ level.bodyQueIndex ];
420 level.bodyQueIndex = (level.bodyQueIndex + 1) % BODY_QUEUE_SIZE;
421
422 body->s = ent->s;
423 body->s.eFlags = EF_DEAD; // clear EF_TALK, etc
424 #ifdef MISSIONPACK
425 if ( ent->s.eFlags & EF_KAMIKAZE ) {
426 body->s.eFlags |= EF_KAMIKAZE;
427
428 // check if there is a kamikaze timer around for this owner
429 for (i = 0; i < level.num_entities; i++) {
430 e = &g_entities[i];
431 if (!e->inuse)
432 continue;
433 if (e->activator != ent)
434 continue;
435 if (strcmp(e->classname, "kamikaze timer"))
436 continue;
437 e->activator = body;
438 break;
439 }
440 }
441 #endif
442 body->s.powerups = 0; // clear powerups
443 body->s.loopSound = 0; // clear lava burning
444 body->s.number = body - g_entities;
445 body->timestamp = level.time;
446 body->physicsObject = qtrue;
447 body->physicsBounce = 0; // don't bounce
448 if ( body->s.groundEntityNum == ENTITYNUM_NONE ) {
449 body->s.pos.trType = TR_GRAVITY;
450 body->s.pos.trTime = level.time;
451 VectorCopy( ent->client->ps.velocity, body->s.pos.trDelta );
452 } else {
453 body->s.pos.trType = TR_STATIONARY;
454 }
455 body->s.event = 0;
456
457 // change the animation to the last-frame only, so the sequence
458 // doesn't repeat anew for the body
459 switch ( body->s.legsAnim & ~ANIM_TOGGLEBIT ) {
460 case BOTH_DEATH1:
461 case BOTH_DEAD1:
462 body->s.torsoAnim = body->s.legsAnim = BOTH_DEAD1;
463 break;
464 case BOTH_DEATH2:
465 case BOTH_DEAD2:
466 body->s.torsoAnim = body->s.legsAnim = BOTH_DEAD2;
467 break;
468 case BOTH_DEATH3:
469 case BOTH_DEAD3:
470 default:
471 body->s.torsoAnim = body->s.legsAnim = BOTH_DEAD3;
472 break;
473 }
474
475 body->r.svFlags = ent->r.svFlags;
476 VectorCopy (ent->r.mins, body->r.mins);
477 VectorCopy (ent->r.maxs, body->r.maxs);
478 VectorCopy (ent->r.absmin, body->r.absmin);
479 VectorCopy (ent->r.absmax, body->r.absmax);
480
481 body->clipmask = CONTENTS_SOLID | CONTENTS_PLAYERCLIP;
482 body->r.contents = CONTENTS_CORPSE;
483 body->r.ownerNum = ent->s.number;
484
485 body->nextthink = level.time + 5000;
486 body->think = BodySink;
487
488 body->die = body_die;
489
490 // don't take more damage if already gibbed
491 if ( ent->health <= GIB_HEALTH ) {
492 body->takedamage = qfalse;
493 } else {
494 body->takedamage = qtrue;
495 }
496
497
498 VectorCopy ( body->s.pos.trBase, body->r.currentOrigin );
499 trap_LinkEntity (body);
500 }
501
502 //======================================================================
503
504
505 /*
506 ==================
507 SetClientViewAngle
508
509 ==================
510 */
511 void SetClientViewAngle( gentity_t *ent, vec3_t angle ) {
512 int i;
513
514 // set the delta angle
515 for (i=0 ; i<3 ; i++) {
516 int cmdAngle;
517
518 cmdAngle = ANGLE2SHORT(angle[i]);
519 ent->client->ps.delta_angles[i] = cmdAngle - ent->client->pers.cmd.angles[i];
520 }
521 VectorCopy( angle, ent->s.angles );
522 VectorCopy (ent->s.angles, ent->client->ps.viewangles);
523 }
524
525 /*
526 ================
527 ClientRespawn
528 ================
529 */
530 void ClientRespawn( gentity_t *ent ) {
531
532 CopyToBodyQue (ent);
533 ClientSpawn(ent);
534 }
535
536 /*
537 ================
538 TeamCount
539
540 Returns number of players on a team
541 ================
542 */
543 int TeamCount( int ignoreClientNum, team_t team ) {
544 int i;
545 int count = 0;
546
547 for ( i = 0 ; i < level.maxclients ; i++ ) {
548 if ( i == ignoreClientNum ) {
549 continue;
550 }
551 if ( level.clients[i].pers.connected == CON_DISCONNECTED ) {
552 continue;
553 }
554 if ( level.clients[i].sess.sessionTeam == team ) {
555 count++;
556 }
557 }
558
559 return count;
560 }
561
562 /*
563 ================
564 TeamLeader
565
566 Returns the client number of the team leader
567 ================
568 */
569 int TeamLeader( int team ) {
570 int i;
571
572 for ( i = 0 ; i < level.maxclients ; i++ ) {
573 if ( level.clients[i].pers.connected == CON_DISCONNECTED ) {
574 continue;
575 }
576 if ( level.clients[i].sess.sessionTeam == team ) {
577 if ( level.clients[i].sess.teamLeader )
578 return i;
579 }
580 }
581
582 return -1;
583 }
584
585
586 /*
587 ================
588 PickTeam
589
590 ================
591 */
592 team_t PickTeam( int ignoreClientNum ) {
593 int counts[TEAM_NUM_TEAMS];
594
595 counts[TEAM_BLUE] = TeamCount( ignoreClientNum, TEAM_BLUE );
596 counts[TEAM_RED] = TeamCount( ignoreClientNum, TEAM_RED );
597
598 if ( counts[TEAM_BLUE] > counts[TEAM_RED] ) {
599 return TEAM_RED;
600 }
601 if ( counts[TEAM_RED] > counts[TEAM_BLUE] ) {
602 return TEAM_BLUE;
603 }
604 // equal team count, so join the team with the lowest score
605 if ( level.teamScores[TEAM_BLUE] > level.teamScores[TEAM_RED] ) {
606 return TEAM_RED;
607 }
608 return TEAM_BLUE;
609 }
610
611 /*
612 ===========
613 ForceClientSkin
614
615 Forces a client's skin (for teamplay)
616 ===========
617 */
618 /*
619 static void ForceClientSkin( gclient_t *client, char *model, const char *skin ) {
620 char *p;
621
622 if ((p = strrchr(model, '/')) != 0) {
623 *p = 0;
624 }
625
626 Q_strcat(model, MAX_QPATH, "/");
627 Q_strcat(model, MAX_QPATH, skin);
628 }
629 */
630
631 /*
632 ===========
633 ClientCleanName
634 ============
635 */
636 static void ClientCleanName(const char *in, char *out, int outSize)
637 {
638 int outpos = 0, colorlessLen = 0, spaces = 0;
639
640 // discard leading spaces
641 for(; *in == ' '; in++);
642
643 for(; *in && outpos < outSize - 1; in++)
644 {
645 out[outpos] = *in;
646
647 if(*in == ' ')
648 {
649 // don't allow too many consecutive spaces
650 if(spaces > 2)
651 continue;
652
653 spaces++;
654 }
655 else if(outpos > 0 && out[outpos - 1] == Q_COLOR_ESCAPE)
656 {
657 if(Q_IsColorString(&out[outpos - 1]))
658 {
659 colorlessLen--;
660
661 if(ColorIndex(*in) == 0)
662 {
663 // Disallow color black in names to prevent players
664 // from getting advantage playing in front of black backgrounds
665 outpos--;
666 continue;
667 }
668 }
669 else
670 {
671 spaces = 0;
672 colorlessLen++;
673 }
674 }
675 else
676 {
677 spaces = 0;
678 colorlessLen++;
679 }
680
681 outpos++;
682 }
683
684 out[outpos] = '\0';
685
686 // don't allow empty names
687 if( *out == '\0' || colorlessLen == 0)
688 Q_strncpyz(out, "UnnamedPlayer", outSize );
689 }
690
691
692 /*
693 ===========
694 ClientUserInfoChanged
695
696 Called from ClientConnect when the player first connects and
697 directly by the server system when the player updates a userinfo variable.
698
699 The game can override any of the settings and call trap_SetUserinfo
700 if desired.
701 ============
702 */
703 void ClientUserinfoChanged( int clientNum ) {
704 gentity_t *ent;
705 int teamTask, teamLeader, health;
706 char *s;
707 char model[MAX_QPATH];
708 char headModel[MAX_QPATH];
709 char oldname[MAX_STRING_CHARS];
710 gclient_t *client;
711 char c1[MAX_INFO_STRING];
712 char c2[MAX_INFO_STRING];
713 char redTeam[MAX_INFO_STRING];
714 char blueTeam[MAX_INFO_STRING];
715 char userinfo[MAX_INFO_STRING];
716
717 ent = g_entities + clientNum;
718 client = ent->client;
719
720 trap_GetUserinfo( clientNum, userinfo, sizeof( userinfo ) );
721
722 // check for malformed or illegal info strings
723 if ( !Info_Validate(userinfo) ) {
724 strcpy (userinfo, "\\name\\badinfo");
725 // don't keep those clients and userinfo
726 trap_DropClient(clientNum, "Invalid userinfo");
727 }
728
729 // check the item prediction
730 s = Info_ValueForKey( userinfo, "cg_predictItems" );
731 if ( !atoi( s ) ) {
732 client->pers.predictItemPickup = qfalse;
733 } else {
734 client->pers.predictItemPickup = qtrue;
735 }
736
737 // set name
738 Q_strncpyz ( oldname, client->pers.netname, sizeof( oldname ) );
739 s = Info_ValueForKey (userinfo, "name");
740 ClientCleanName( s, client->pers.netname, sizeof(client->pers.netname) );
741
742 if ( client->sess.sessionTeam == TEAM_SPECTATOR ) {
743 if ( client->sess.spectatorState == SPECTATOR_SCOREBOARD ) {
744 Q_strncpyz( client->pers.netname, "scoreboard", sizeof(client->pers.netname) );
745 }
746 }
747
748 if ( client->pers.connected == CON_CONNECTED ) {
749 if ( strcmp( oldname, client->pers.netname ) ) {
750 trap_SendServerCommand( -1, va("print \"%s" S_COLOR_WHITE " renamed to %s\n\"", oldname,
751 client->pers.netname) );
752 }
753 }
754
755 // set max health
756 #ifdef MISSIONPACK
757 if (client->ps.powerups[PW_GUARD]) {
758 client->pers.maxHealth = 200;
759 } else {
760 health = atoi( Info_ValueForKey( userinfo, "handicap" ) );
761 client->pers.maxHealth = health;
762 if ( client->pers.maxHealth < 1 || client->pers.maxHealth > 100 ) {
763 client->pers.maxHealth = 100;
764 }
765 }
766 #else
767 health = atoi( Info_ValueForKey( userinfo, "handicap" ) );
768 client->pers.maxHealth = health;
769 if ( client->pers.maxHealth < 1 || client->pers.maxHealth > 100 ) {
770 client->pers.maxHealth = 100;
771 }
772 #endif
773 client->ps.stats[STAT_MAX_HEALTH] = client->pers.maxHealth;
774
775 // set model
776 if( g_gametype.integer >= GT_TEAM ) {
777 Q_strncpyz( model, Info_ValueForKey (userinfo, "team_model"), sizeof( model ) );
778 Q_strncpyz( headModel, Info_ValueForKey (userinfo, "team_headmodel"), sizeof( headModel ) );
779 } else {
780 Q_strncpyz( model, Info_ValueForKey (userinfo, "model"), sizeof( model ) );
781 Q_strncpyz( headModel, Info_ValueForKey (userinfo, "headmodel"), sizeof( headModel ) );
782 }
783
784 /* NOTE: all client side now
785
786 // team
787 switch( team ) {
788 case TEAM_RED:
789 ForceClientSkin(client, model, "red");
790 // ForceClientSkin(client, headModel, "red");
791 break;
792 case TEAM_BLUE:
793 ForceClientSkin(client, model, "blue");
794 // ForceClientSkin(client, headModel, "blue");
795 break;
796 }
797 // don't ever use a default skin in teamplay, it would just waste memory
798 // however bots will always join a team but they spawn in as spectator
799 if ( g_gametype.integer >= GT_TEAM && team == TEAM_SPECTATOR) {
800 ForceClientSkin(client, model, "red");
801 // ForceClientSkin(client, headModel, "red");
802 }
803 */
804
805 #ifdef MISSIONPACK
806 if (g_gametype.integer >= GT_TEAM && !(ent->r.svFlags & SVF_BOT)) {
807 client->pers.teamInfo = qtrue;
808 } else {
809 s = Info_ValueForKey( userinfo, "teamoverlay" );
810 if ( ! *s || atoi( s ) != 0 ) {
811 client->pers.teamInfo = qtrue;
812 } else {
813 client->pers.teamInfo = qfalse;
814 }
815 }
816 #else
817 // teamInfo
818 s = Info_ValueForKey( userinfo, "teamoverlay" );
819 if ( ! *s || atoi( s ) != 0 ) {
820 client->pers.teamInfo = qtrue;
821 } else {
822 client->pers.teamInfo = qfalse;
823 }
824 #endif
825 /*
826 s = Info_ValueForKey( userinfo, "cg_pmove_fixed" );
827 if ( !*s || atoi( s ) == 0 ) {
828 client->pers.pmoveFixed = qfalse;
829 }
830 else {
831 client->pers.pmoveFixed = qtrue;
832 }
833 */
834
835 // team task (0 = none, 1 = offence, 2 = defence)
836 teamTask = atoi(Info_ValueForKey(userinfo, "teamtask"));
837 // team Leader (1 = leader, 0 is normal player)
838 teamLeader = client->sess.teamLeader;
839
840 // colors
841 Q_strncpyz(c1, Info_ValueForKey( userinfo, "color1" ), sizeof( c1 ));
842 Q_strncpyz(c2, Info_ValueForKey( userinfo, "color2" ), sizeof( c2 ));
843
844 Q_strncpyz(redTeam, Info_ValueForKey( userinfo, "g_redteam" ), sizeof( redTeam ));
845 Q_strncpyz(blueTeam, Info_ValueForKey( userinfo, "g_blueteam" ), sizeof( blueTeam ));
846
847 // send over a subset of the userinfo keys so other clients can
848 // print scoreboards, display models, and play custom sounds
849 if (ent->r.svFlags & SVF_BOT)
850 {
851 s = va("n\\%s\\t\\%i\\model\\%s\\hmodel\\%s\\c1\\%s\\c2\\%s\\hc\\%i\\w\\%i\\l\\%i\\skill\\%s\\tt\\%d\\tl\\%d",
852 client->pers.netname, client->sess.sessionTeam, model, headModel, c1, c2,
853 client->pers.maxHealth, client->sess.wins, client->sess.losses,
854 Info_ValueForKey( userinfo, "skill" ), teamTask, teamLeader );
855 }
856 else
857 {
858 s = va("n\\%s\\t\\%i\\model\\%s\\hmodel\\%s\\g_redteam\\%s\\g_blueteam\\%s\\c1\\%s\\c2\\%s\\hc\\%i\\w\\%i\\l\\%i\\tt\\%d\\tl\\%d",
859 client->pers.netname, client->sess.sessionTeam, model, headModel, redTeam, blueTeam, c1, c2,
860 client->pers.maxHealth, client->sess.wins, client->sess.losses, teamTask, teamLeader);
861 }
862
863 trap_SetConfigstring( CS_PLAYERS+clientNum, s );
864
865 // this is not the userinfo, more like the configstring actually
866 G_LogPrintf( "ClientUserinfoChanged: %i %s\n", clientNum, s );
867 }
868
869
870 /*
871 ===========
872 ClientConnect
873
874 Called when a player begins connecting to the server.
875 Called again for every map change or tournement restart.
876
877 The session information will be valid after exit.
878
879 Return NULL if the client should be allowed, otherwise return
880 a string with the reason for denial.
881
882 Otherwise, the client will be sent the current gamestate
883 and will eventually get to ClientBegin.
884
885 firstTime will be qtrue the very first time a client connects
886 to the server machine, but qfalse on map changes and tournement
887 restarts.
888 ============
889 */
890 char *ClientConnect( int clientNum, qboolean firstTime, qboolean isBot ) {
891 char *value;
892 // char *areabits;
893 gclient_t *client;
894 char userinfo[MAX_INFO_STRING];
895 gentity_t *ent;
896
897 ent = &g_entities[ clientNum ];
898
899 trap_GetUserinfo( clientNum, userinfo, sizeof( userinfo ) );
900
901 // IP filtering
902 // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=500
903 // recommanding PB based IP / GUID banning, the builtin system is pretty limited
904 // check to see if they are on the banned IP list
905 value = Info_ValueForKey (userinfo, "ip");
906 if ( G_FilterPacket( value ) ) {
907 return "You are banned from this server.";
908 }
909
910 // we don't check password for bots and local client
911 // NOTE: local client <-> "ip" "localhost"
912 // this means this client is not running in our current process
913 if ( !isBot && (strcmp(value, "localhost") != 0)) {
914 // check for a password
915 value = Info_ValueForKey (userinfo, "password");
916 if ( g_password.string[0] && Q_stricmp( g_password.string, "none" ) &&
917 strcmp( g_password.string, value) != 0) {
918 return "Invalid password";
919 }
920 }
921 // if a player reconnects quickly after a disconnect, the client disconnect may never be called, thus flag can get lost in the ether
922 if (ent->inuse) {
923 G_LogPrintf("Forcing disconnect on active client: %i\n", clientNum);
924 // so lets just fix up anything that should happen on a disconnect
925 ClientDisconnect(clientNum);
926 }
927 // they can connect
928 ent->client = level.clients + clientNum;
929 client = ent->client;
930
931 // areabits = client->areabits;
932
933 memset( client, 0, sizeof(*client) );
934
935 client->pers.connected = CON_CONNECTING;
936
937 // check for local client
938 value = Info_ValueForKey( userinfo, "ip" );
939 if ( !strcmp( value, "localhost" ) ) {
940 client->pers.localClient = qtrue;
941 }
942
943 if( isBot ) {
944 ent->r.svFlags |= SVF_BOT;
945 ent->inuse = qtrue;
946 if( !G_BotConnect( clientNum, !firstTime ) ) {
947 return "BotConnectfailed";
948 }
949 }
950
951 // read or initialize the session data
952 if ( firstTime || level.newSession ) {
953 G_InitSessionData( client, userinfo );
954 }
955 G_ReadSessionData( client );
956
957 // get and distribute relevant parameters
958 G_LogPrintf( "ClientConnect: %i\n", clientNum );
959 ClientUserinfoChanged( clientNum );
960
961 // don't do the "xxx connected" messages if they were caried over from previous level
962 if ( firstTime ) {
963 trap_SendServerCommand( -1, va("print \"%s" S_COLOR_WHITE " connected\n\"", client->pers.netname) );
964 }
965
966 if ( g_gametype.integer >= GT_TEAM &&
967 client->sess.sessionTeam != TEAM_SPECTATOR ) {
968 BroadcastTeamChange( client, -1 );
969 }
970
971 // count current clients and rank for scoreboard
972 CalculateRanks();
973
974 // for statistics
975 // client->areabits = areabits;
976 // if ( !client->areabits )
977 // client->areabits = G_Alloc( (trap_AAS_PointReachabilityAreaIndex( NULL ) + 7) / 8 );
978
979 return NULL;
980 }
981
982 /*
983 ===========
984 ClientBegin
985
986 called when a client has finished connecting, and is ready
987 to be placed into the level. This will happen every level load,
988 and on transition between teams, but doesn't happen on respawns
989 ============
990 */
991 void ClientBegin( int clientNum ) {
992 gentity_t *ent;
993 gclient_t *client;
994 int flags;
995
996 ent = g_entities + clientNum;
997
998 client = level.clients + clientNum;
999
1000 if ( ent->r.linked ) {
1001 trap_UnlinkEntity( ent );
1002 }
1003 G_InitGentity( ent );
1004 ent->touch = 0;
1005 ent->pain = 0;
1006 ent->client = client;
1007
1008 client->pers.connected = CON_CONNECTED;
1009 client->pers.enterTime = level.time;
1010 client->pers.teamState.state = TEAM_BEGIN;
1011
1012 // save eflags around this, because changing teams will
1013 // cause this to happen with a valid entity, and we
1014 // want to make sure the teleport bit is set right
1015 // so the viewpoint doesn't interpolate through the
1016 // world to the new position
1017 flags = client->ps.eFlags;
1018 memset( &client->ps, 0, sizeof( client->ps ) );
1019 client->ps.eFlags = flags;
1020
1021 // locate ent at a spawn point
1022 ClientSpawn( ent );
1023
1024 if ( client->sess.sessionTeam != TEAM_SPECTATOR ) {
1025 if ( g_gametype.integer != GT_TOURNAMENT ) {
1026 trap_SendServerCommand( -1, va("print \"%s" S_COLOR_WHITE " entered the game\n\"", client->pers.netname) );
1027 }
1028 }
1029 G_LogPrintf( "ClientBegin: %i\n", clientNum );
1030
1031 // count current clients and rank for scoreboard
1032 CalculateRanks();
1033 }
1034
1035 /*
1036 ===========
1037 ClientSpawn
1038
1039 Called every time a client is placed fresh in the world:
1040 after the first ClientBegin, and after each respawn
1041 Initializes all non-persistant parts of playerState
1042 ============
1043 */
1044 void ClientSpawn(gentity_t *ent) {
1045 int index;
1046 vec3_t spawn_origin, spawn_angles;
1047 gclient_t *client;
1048 int i;
1049 clientPersistant_t saved;
1050 clientSession_t savedSess;
1051 int persistant[MAX_PERSISTANT];
1052 gentity_t *spawnPoint;
1053 gentity_t *tent;
1054 int flags;
1055 int savedPing;
1056 // char *savedAreaBits;
1057 int accuracy_hits, accuracy_shots;
1058 int eventSequence;
1059 char userinfo[MAX_INFO_STRING];
1060
1061 index = ent - g_entities;
1062 client = ent->client;
1063
1064 VectorClear(spawn_origin);
1065
1066 // find a spawn point
1067 // do it before setting health back up, so farthest
1068 // ranging doesn't count this client
1069 if ( client->sess.sessionTeam == TEAM_SPECTATOR ) {
1070 spawnPoint = SelectSpectatorSpawnPoint (
1071 spawn_origin, spawn_angles);
1072 } else if (g_gametype.integer >= GT_CTF ) {
1073 // all base oriented team games use the CTF spawn points
1074 spawnPoint = SelectCTFSpawnPoint (
1075 client->sess.sessionTeam,
1076 client->pers.teamState.state,
1077 spawn_origin, spawn_angles,
1078 !!(ent->r.svFlags & SVF_BOT));
1079 }
1080 else
1081 {
1082 // the first spawn should be at a good looking spot
1083 if ( !client->pers.initialSpawn && client->pers.localClient )
1084 {
1085 client->pers.initialSpawn = qtrue;
1086 spawnPoint = SelectInitialSpawnPoint(spawn_origin, spawn_angles,
1087 !!(ent->r.svFlags & SVF_BOT));
1088 }
1089 else
1090 {
1091 // don't spawn near existing origin if possible
1092 spawnPoint = SelectSpawnPoint (
1093 client->ps.origin,
1094 spawn_origin, spawn_angles, !!(ent->r.svFlags & SVF_BOT));
1095 }
1096 }
1097 client->pers.teamState.state = TEAM_ACTIVE;
1098
1099 // always clear the kamikaze flag
1100 ent->s.eFlags &= ~EF_KAMIKAZE;
1101
1102 // toggle the teleport bit so the client knows to not lerp
1103 // and never clear the voted flag
1104 flags = ent->client->ps.eFlags & (EF_TELEPORT_BIT | EF_VOTED | EF_TEAMVOTED);
1105 flags ^= EF_TELEPORT_BIT;
1106
1107 // clear everything but the persistant data
1108
1109 saved = client->pers;
1110 savedSess = client->sess;
1111 savedPing = client->ps.ping;
1112 // savedAreaBits = client->areabits;
1113 accuracy_hits = client->accuracy_hits;
1114 accuracy_shots = client->accuracy_shots;
1115 for ( i = 0 ; i < MAX_PERSISTANT ; i++ ) {
1116 persistant[i] = client->ps.persistant[i];
1117 }
1118 eventSequence = client->ps.eventSequence;
1119
1120 Com_Memset (client, 0, sizeof(*client));
1121
1122 client->pers = saved;
1123 client->sess = savedSess;
1124 client->ps.ping = savedPing;
1125 // client->areabits = savedAreaBits;
1126 client->accuracy_hits = accuracy_hits;
1127 client->accuracy_shots = accuracy_shots;
1128 client->lastkilled_client = -1;
1129
1130 for ( i = 0 ; i < MAX_PERSISTANT ; i++ ) {
1131 client->ps.persistant[i] = persistant[i];
1132 }
1133 client->ps.eventSequence = eventSequence;
1134 // increment the spawncount so the client will detect the respawn
1135 client->ps.persistant[PERS_SPAWN_COUNT]++;
1136 client->ps.persistant[PERS_TEAM] = client->sess.sessionTeam;
1137
1138 client->airOutTime = level.time + 12000;
1139
1140 trap_GetUserinfo( index, userinfo, sizeof(userinfo) );
1141 // set max health
1142 client->pers.maxHealth = atoi( Info_ValueForKey( userinfo, "handicap" ) );
1143 if ( client->pers.maxHealth < 1 || client->pers.maxHealth > 100 ) {
1144 client->pers.maxHealth = 100;
1145 }
1146 // clear entity values
1147 client->ps.stats[STAT_MAX_HEALTH] = client->pers.maxHealth;
1148 client->ps.eFlags = flags;
1149
1150 ent->s.groundEntityNum = ENTITYNUM_NONE;
1151 ent->client = &level.clients[index];
1152 ent->takedamage = qtrue;
1153 ent->inuse = qtrue;
1154 ent->classname = "player";
1155 ent->r.contents = CONTENTS_BODY;
1156 ent->clipmask = MASK_PLAYERSOLID;
1157 ent->die = player_die;
1158 ent->waterlevel = 0;
1159 ent->watertype = 0;
1160 ent->flags = 0;
1161
1162 VectorCopy (playerMins, ent->r.mins);
1163 VectorCopy (playerMaxs, ent->r.maxs);
1164
1165 client->ps.clientNum = index;
1166
1167 client->ps.stats[STAT_WEAPONS] = ( 1 << WP_MACHINEGUN );
1168 if ( g_gametype.integer == GT_TEAM ) {
1169 client->ps.ammo[WP_MACHINEGUN] = 50;
1170 } else {
1171 client->ps.ammo[WP_MACHINEGUN] = 100;
1172 }
1173
1174 client->ps.stats[STAT_WEAPONS] |= ( 1 << WP_GAUNTLET );
1175 client->ps.ammo[WP_GAUNTLET] = -1;
1176 client->ps.ammo[WP_GRAPPLING_HOOK] = -1;
1177
1178 // health will count down towards max_health
1179 ent->health = client->ps.stats[STAT_HEALTH] = client->ps.stats[STAT_MAX_HEALTH] + 25;
1180
1181 G_SetOrigin( ent, spawn_origin );
1182 VectorCopy( spawn_origin, client->ps.origin );
1183
1184 // the respawned flag will be cleared after the attack and jump keys come up
1185 client->ps.pm_flags |= PMF_RESPAWNED;
1186
1187 trap_GetUsercmd( client - level.clients, &ent->client->pers.cmd );
1188 SetClientViewAngle( ent, spawn_angles );
1189 // don't allow full run speed for a bit
1190 client->ps.pm_flags |= PMF_TIME_KNOCKBACK;
1191 client->ps.pm_time = 100;
1192
1193 client->respawnTime = level.time;
1194 client->inactivityTime = level.time + g_inactivity.integer * 1000;
1195 client->latched_buttons = 0;
1196
1197 // set default animations
1198 client->ps.torsoAnim = TORSO_STAND;
1199 client->ps.legsAnim = LEGS_IDLE;
1200
1201 if (!level.intermissiontime) {
1202 if (ent->client->sess.sessionTeam != TEAM_SPECTATOR) {
1203 G_KillBox(ent);
1204 // force the base weapon up
1205 client->ps.weapon = WP_MACHINEGUN;
1206 client->ps.weaponstate = WEAPON_READY;
1207 // fire the targets of the spawn point
1208 G_UseTargets(spawnPoint, ent);
1209 // select the highest weapon number available, after any spawn given items have fired
1210 client->ps.weapon = 1;
1211
1212 for (i = WP_NUM_WEAPONS - 1 ; i > 0 ; i--) {
1213 if (client->ps.stats[STAT_WEAPONS] & (1 << i)) {
1214 client->ps.weapon = i;
1215 break;
1216 }
1217 }
1218 // positively link the client, even if the command times are weird
1219 VectorCopy(ent->client->ps.origin, ent->r.currentOrigin);
1220
1221 tent = G_TempEntity(ent->client->ps.origin, EV_PLAYER_TELEPORT_IN);
1222 tent->s.clientNum = ent->s.clientNum;
1223
1224 trap_LinkEntity (ent);
1225 }
1226 } else {
1227 // move players to intermission
1228 MoveClientToIntermission(ent);
1229 }
1230 // run a client frame to drop exactly to the floor,
1231 // initialize animations and other things
1232 client->ps.commandTime = level.time - 100;
1233 ent->client->pers.cmd.serverTime = level.time;
1234 ClientThink( ent-g_entities );
1235 // run the presend to set anything else, follow spectators wait
1236 // until all clients have been reconnected after map_restart
1237 if ( ent->client->sess.spectatorState != SPECTATOR_FOLLOW ) {
1238 ClientEndFrame( ent );
1239 }
1240
1241 // clear entity state values
1242 BG_PlayerStateToEntityState( &client->ps, &ent->s, qtrue );
1243 }
1244
1245
1246 /*
1247 ===========
1248 ClientDisconnect
1249
1250 Called when a player drops from the server.
1251 Will not be called between levels.
1252
1253 This should NOT be called directly by any game logic,
1254 call trap_DropClient(), which will call this and do
1255 server system housekeeping.
1256 ============
1257 */
1258 void ClientDisconnect( int clientNum ) {
1259 gentity_t *ent;
1260 gentity_t *tent;
1261 int i;
1262
1263 // cleanup if we are kicking a bot that
1264 // hasn't spawned yet
1265 G_RemoveQueuedBotBegin( clientNum );
1266
1267 ent = g_entities + clientNum;
1268 if (!ent->client || ent->client->pers.connected == CON_DISCONNECTED) {
1269 return;
1270 }
1271
1272 // stop any following clients
1273 for ( i = 0 ; i < level.maxclients ; i++ ) {
1274 if ( level.clients[i].sess.sessionTeam == TEAM_SPECTATOR
1275 && level.clients[i].sess.spectatorState == SPECTATOR_FOLLOW
1276 && level.clients[i].sess.spectatorClient == clientNum ) {
1277 StopFollowing( &g_entities[i] );
1278 }
1279 }
1280
1281 // send effect if they were completely connected
1282 if ( ent->client->pers.connected == CON_CONNECTED
1283 && ent->client->sess.sessionTeam != TEAM_SPECTATOR ) {
1284 tent = G_TempEntity( ent->client->ps.origin, EV_PLAYER_TELEPORT_OUT );
1285 tent->s.clientNum = ent->s.clientNum;
1286
1287 // They don't get to take powerups with them!
1288 // Especially important for stuff like CTF flags
1289 TossClientItems( ent );
1290 #ifdef MISSIONPACK
1291 TossClientPersistantPowerups( ent );
1292 if( g_gametype.integer == GT_HARVESTER ) {
1293 TossClientCubes( ent );
1294 }
1295 #endif
1296
1297 }
1298
1299 G_LogPrintf( "ClientDisconnect: %i\n", clientNum );
1300
1301 // if we are playing in tourney mode and losing, give a win to the other player
1302 if ( (g_gametype.integer == GT_TOURNAMENT )
1303 && !level.intermissiontime
1304 && !level.warmupTime && level.sortedClients[1] == clientNum ) {
1305 level.clients[ level.sortedClients[0] ].sess.wins++;
1306 ClientUserinfoChanged( level.sortedClients[0] );
1307 }
1308
1309 if( g_gametype.integer == GT_TOURNAMENT &&
1310 ent->client->sess.sessionTeam == TEAM_FREE &&
1311 level.intermissiontime ) {
1312
1313 trap_SendConsoleCommand( EXEC_APPEND, "map_restart 0\n" );
1314 level.restarted = qtrue;
1315 level.changemap = NULL;
1316 level.intermissiontime = 0;
1317 }
1318
1319 trap_UnlinkEntity (ent);
1320 ent->s.modelindex = 0;
1321 ent->inuse = qfalse;
1322 ent->classname = "disconnected";
1323 ent->client->pers.connected = CON_DISCONNECTED;
1324 ent->client->ps.persistant[PERS_TEAM] = TEAM_FREE;
1325 ent->client->sess.sessionTeam = TEAM_FREE;
1326
1327 trap_SetConfigstring( CS_PLAYERS + clientNum, "");
1328
1329 CalculateRanks();
1330
1331 if ( ent->r.svFlags & SVF_BOT ) {
1332 BotAIShutdownClient( clientNum, qfalse );
1333 }
1334 }
1335
1336