Debian Codegraph @

code/game/g_session.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
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 int teamLeader;
73 int spectatorState;
74 int sessionTeam;
75
76 var = va( "session%i", (int)(client - level.clients) );
77 trap_Cvar_VariableStringBuffer( var, s, sizeof(s) );
78
79 sscanf( s, "%i %i %i %i %i %i %i",
80 &sessionTeam,
81 &client->sess.spectatorNum,
82 &spectatorState,
83 &client->sess.spectatorClient,
84 &client->sess.wins,
85 &client->sess.losses,
86 &teamLeader
87 );
88
89 client->sess.sessionTeam = (team_t)sessionTeam;
90 client->sess.spectatorState = (spectatorState_t)spectatorState;
91 client->sess.teamLeader = (qboolean)teamLeader;
92 }
93
94
95 /*
96 ================
97 G_InitSessionData
98
99 Called on a first-time connect
100 ================
101 */
102 void G_InitSessionData( gclient_t *client, char *userinfo ) {
103 clientSession_t *sess;
104 const char *value;
105
106 sess = &client->sess;
107
108 // check for team preference, mainly for bots
109 value = Info_ValueForKey( userinfo, "teampref" );
110
111 // check for human's team preference set by start server menu
112 if ( !value[0] && g_localTeamPref.string[0] && client->pers.localClient ) {
113 value = g_localTeamPref.string;
114
115 // clear team so it's only used once
116 trap_Cvar_Set( "g_localTeamPref", "" );
117 }
118
119 // initial team determination
120 if ( g_gametype.integer >= GT_TEAM ) {
121 // always spawn as spectator in team games
122 sess->sessionTeam = TEAM_SPECTATOR;
123 sess->spectatorState = SPECTATOR_FREE;
124
125 if ( value[0] || g_teamAutoJoin.integer ) {
126 SetTeam( &g_entities[client - level.clients], value );
127 }
128 } else {
129 if ( value[0] == 's' ) {
130 // a willing spectator, not a waiting-in-line
131 sess->sessionTeam = TEAM_SPECTATOR;
132 } else {
133 switch ( g_gametype.integer ) {
134 default:
135 case GT_FFA:
136 case GT_SINGLE_PLAYER:
137 if ( g_maxGameClients.integer > 0 &&
138 level.numNonSpectatorClients >= g_maxGameClients.integer ) {
139 sess->sessionTeam = TEAM_SPECTATOR;
140 } else {
141 sess->sessionTeam = TEAM_FREE;
142 }
143 break;
144 case GT_TOURNAMENT:
145 // if the game is full, go into a waiting mode
146 if ( level.numNonSpectatorClients >= 2 ) {
147 sess->sessionTeam = TEAM_SPECTATOR;
148 } else {
149 sess->sessionTeam = TEAM_FREE;
150 }
151 break;
152 }
153 }
154
155 sess->spectatorState = SPECTATOR_FREE;
156 }
157
158 AddTournamentQueue(client);
159
160 G_WriteClientSessionData( client );
161 }
162
163
164 /*
165 ==================
166 G_InitWorldSession
167
168 ==================
169 */
170 void G_InitWorldSession( void ) {
171 char s[MAX_STRING_CHARS];
172 int gt;
173
174 trap_Cvar_VariableStringBuffer( "session", s, sizeof(s) );
175 gt = atoi( s );
176
177 // if the gametype changed since the last session, don't use any
178 // client sessions
179 if ( g_gametype.integer != gt ) {
180 level.newSession = qtrue;
181 G_Printf( "Gametype changed, clearing session data.\n" );
182 }
183 }
184
185 /*
186 ==================
187 G_WriteSessionData
188
189 ==================
190 */
191 void G_WriteSessionData( void ) {
192 int i;
193
194 trap_Cvar_Set( "session", va("%i", g_gametype.integer) );
195
196 for ( i = 0 ; i < level.maxclients ; i++ ) {
197 if ( level.clients[i].pers.connected == CON_CONNECTED ) {
198 G_WriteClientSessionData( &level.clients[i] );
199 }
200 }
201 }