Debian Codegraph @

code/game/g_session.c

raw
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
26 /*
27 =======================================================================
28
29 SESSION DATA
30
31 Session data is the only data that stays persistant across level loads
32 and tournament restarts.
33 =======================================================================
34 */
35
36 /*
37 ================
38 G_WriteClientSessionData
39
40 Called on game shutdown
41 ================
42 */
43 void G_WriteClientSessionData( gclient_t *client ) {
44 const char *s;
45 const char *var;
46
47 s = va("%i %i %i %i %i %i %i",
48 client->sess.sessionTeam,
49 client->sess.spectatorNum,
50 client->sess.spectatorState,
51 client->sess.spectatorClient,
52 client->sess.wins,
53 client->sess.losses,
54 client->sess.teamLeader
55 );
56
57 var = va( "session%i", (int)(client - level.clients) );
58
59 trap_Cvar_Set( var, s );
60 }
61
62 /*
63 ================
64 G_ReadSessionData
65
66 Called on a reconnect
67 ================
68 */
69 void G_ReadSessionData( gclient_t *client ) {
70 char s[MAX_STRING_CHARS];
71 const char *var;
72
73 // bk001205 - format
74 int teamLeader;
75 int spectatorState;
76 int sessionTeam;
77
78 var = va( "session%i", (int)(client - level.clients) );
79 trap_Cvar_VariableStringBuffer( var, s, sizeof(s) );
80
81 sscanf( s, "%i %i %i %i %i %i %i",
82 &sessionTeam, // bk010221 - format
83 &client->sess.spectatorNum,
84 &spectatorState, // bk010221 - format
85 &client->sess.spectatorClient,
86 &client->sess.wins,
87 &client->sess.losses,
88 &teamLeader // bk010221 - format
89 );
90
91 // bk001205 - format issues
92 client->sess.sessionTeam = (team_t)sessionTeam;
93 client->sess.spectatorState = (spectatorState_t)spectatorState;
94 client->sess.teamLeader = (qboolean)teamLeader;
95 }
96
97
98 /*
99 ================
100 G_InitSessionData
101
102 Called on a first-time connect
103 ================
104 */
105 void G_InitSessionData( gclient_t *client, char *userinfo ) {
106 clientSession_t *sess;
107 const char *value;
108
109 sess = &client->sess;
110
111 // initial team determination
112 if ( g_gametype.integer >= GT_TEAM && g_ffa_gt!=1) {
113 if ( g_teamAutoJoin.integer ) {
114 sess->sessionTeam = PickTeam( -1 );
115 BroadcastTeamChange( client, -1 );
116 } else {
117 // always spawn as spectator in team games
118 sess->sessionTeam = TEAM_SPECTATOR;
119 }
120 } else {
121 value = Info_ValueForKey( userinfo, "team" );
122 if ( value[0] == 's' ) {
123 // a willing spectator, not a waiting-in-line
124 sess->sessionTeam = TEAM_SPECTATOR;
125 } else {
126 switch ( g_gametype.integer ) {
127 default:
128 case GT_FFA:
129 case GT_LMS:
130 case GT_SINGLE_PLAYER:
131 if ( g_maxGameClients.integer > 0 &&
132 level.numNonSpectatorClients >= g_maxGameClients.integer ) {
133 sess->sessionTeam = TEAM_SPECTATOR;
134 } else {
135 sess->sessionTeam = TEAM_FREE;
136 }
137 break;
138 case GT_TOURNAMENT:
139 // if the game is full, go into a waiting mode
140 if ( level.numNonSpectatorClients >= 2 ) {
141 sess->sessionTeam = TEAM_SPECTATOR;
142 } else {
143 sess->sessionTeam = TEAM_FREE;
144 }
145 break;
146 }
147 }
148 }
149
150 sess->spectatorState = SPECTATOR_FREE;
151 AddTournamentQueue(client);
152
153 G_WriteClientSessionData( client );
154 }
155
156
157 /*
158 ==================
159 G_InitWorldSession
160
161 ==================
162 */
163 void G_InitWorldSession( void ) {
164 char s[MAX_STRING_CHARS];
165 int gt;
166
167 trap_Cvar_VariableStringBuffer( "session", s, sizeof(s) );
168 gt = atoi( s );
169
170 // if the gametype changed since the last session, don't use any
171 // client sessions
172 if ( g_gametype.integer != gt ) {
173 level.newSession = qtrue;
174 G_Printf( "Gametype changed, clearing session data.\n" );
175 }
176 }
177
178 /*
179 ==================
180 G_WriteSessionData
181
182 ==================
183 */
184 void G_WriteSessionData( void ) {
185 int i;
186
187 trap_Cvar_Set( "session", va("%i", g_gametype.integer) );
188
189 for ( i = 0 ; i < level.maxclients ; i++ ) {
190 if ( level.clients[i].pers.connected == CON_CONNECTED ) {
191 G_WriteClientSessionData( &level.clients[i] );
192 }
193 }
194 }