q3rally/engine/code/game/g_session.c

231 lines
5.5 KiB
C
Raw Normal View History

2011-02-18 14:31:32 +00:00
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2002-2015 Q3Rally Team (Per Thormann - q3rally@gmail.com)
2011-02-18 14:31:32 +00:00
This file is part of q3rally source code.
q3rally source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
q3rally source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with q3rally; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
//
#include "g_local.h"
/*
=======================================================================
SESSION DATA
Session data is the only data that stays persistant across level loads
and tournament restarts.
=======================================================================
*/
/*
================
G_WriteClientSessionData
Called on game shutdown
================
*/
void G_WriteClientSessionData( gclient_t *client ) {
const char *s;
const char *var;
s = va("%i %i %i %i %i %i %i",
client->sess.sessionTeam,
client->sess.spectatorNum,
2011-02-18 14:31:32 +00:00
client->sess.spectatorState,
client->sess.spectatorClient,
client->sess.wins,
client->sess.losses,
client->sess.teamLeader
);
2011-03-08 10:19:47 +00:00
var = va( "session%i", (int)(client - level.clients) );
2011-02-18 14:31:32 +00:00
trap_Cvar_Set( var, s );
}
/*
================
G_ReadSessionData
Called on a reconnect
================
*/
void G_ReadSessionData( gclient_t *client ) {
char s[MAX_STRING_CHARS];
const char *var;
int teamLeader;
int spectatorState;
int sessionTeam;
2011-03-08 10:19:47 +00:00
var = va( "session%i", (int)(client - level.clients) );
2011-02-18 14:31:32 +00:00
trap_Cvar_VariableStringBuffer( var, s, sizeof(s) );
sscanf( s, "%i %i %i %i %i %i %i",
2011-03-08 10:19:47 +00:00
&sessionTeam,
&client->sess.spectatorNum,
2011-03-08 10:19:47 +00:00
&spectatorState,
2011-02-18 14:31:32 +00:00
&client->sess.spectatorClient,
&client->sess.wins,
&client->sess.losses,
2011-03-08 10:19:47 +00:00
&teamLeader
2011-02-18 14:31:32 +00:00
);
client->sess.sessionTeam = (team_t)sessionTeam;
client->sess.spectatorState = (spectatorState_t)spectatorState;
client->sess.teamLeader = (qboolean)teamLeader;
}
/*
================
G_InitSessionData
Called on a first-time connect
================
*/
void G_InitSessionData( gclient_t *client, char *userinfo ) {
clientSession_t *sess;
const char *value;
sess = &client->sess;
// check for team preference, mainly for bots
value = Info_ValueForKey( userinfo, "teampref" );
// check for human's team preference set by start server menu
if ( !value[0] && g_localTeamPref.string[0] && client->pers.localClient ) {
value = g_localTeamPref.string;
// clear team so it's only used once
trap_Cvar_Set( "g_localTeamPref", "" );
}
2011-02-18 14:31:32 +00:00
// initial team determination
if ( g_gametype.integer >= GT_TEAM ) {
// always spawn as spectator in team games
sess->sessionTeam = TEAM_SPECTATOR;
sess->spectatorState = SPECTATOR_FREE;
2011-02-18 14:31:32 +00:00
// STONELANCE
// can only spawn as spectator after race starts
// if ( value[0] || g_teamAutoJoin.integer ) {
if ( ( value[0] || g_teamAutoJoin.integer ) && !( isRallyRace() && level.startRaceTime ) ) {
2011-02-18 14:31:32 +00:00
// END
SetTeam( &g_entities[client - level.clients], value );
2011-02-18 14:31:32 +00:00
}
} else {
// STONELANCE
if ( value[0] == 's' ) {
// if ( value[0] == 's' && (!isRallyRace() && !g_gametype.integer == GT_DERBY)) {
// END
// a willing spectator, not a waiting-in-line
sess->sessionTeam = TEAM_SPECTATOR;
} else {
switch ( g_gametype.integer ) {
default:
// STONELANCE - removed gametype
// case GT_FFA:
case GT_RACING:
case GT_RACING_DM:
case GT_DERBY:
if ( g_maxGameClients.integer > 0 &&
level.numNonSpectatorClients >= g_maxGameClients.integer ) {
sess->sessionTeam = TEAM_SPECTATOR;
}
else if (level.startRaceTime){
sess->sessionTeam = TEAM_SPECTATOR;
} else {
sess->sessionTeam = TEAM_FREE;
}
break;
case GT_DEATHMATCH:
// END
case GT_SINGLE_PLAYER:
if ( g_maxGameClients.integer > 0 &&
level.numNonSpectatorClients >= g_maxGameClients.integer ) {
sess->sessionTeam = TEAM_SPECTATOR;
} else {
sess->sessionTeam = TEAM_FREE;
}
break;
// STONELANCE - removed gametype
/*
case GT_TOURNAMENT:
// if the game is full, go into a waiting mode
if ( level.numNonSpectatorClients >= 2 ) {
sess->sessionTeam = TEAM_SPECTATOR;
} else {
sess->sessionTeam = TEAM_FREE;
}
break;
*/
// END
}
}
sess->spectatorState = SPECTATOR_FREE;
2011-02-18 14:31:32 +00:00
}
AddTournamentQueue(client);
2011-02-18 14:31:32 +00:00
G_WriteClientSessionData( client );
}
/*
==================
G_InitWorldSession
==================
*/
void G_InitWorldSession( void ) {
char s[MAX_STRING_CHARS];
int gt;
trap_Cvar_VariableStringBuffer( "session", s, sizeof(s) );
gt = atoi( s );
// if the gametype changed since the last session, don't use any
// client sessions
if ( g_gametype.integer != gt ) {
level.newSession = qtrue;
G_Printf( "Gametype changed, clearing session data.\n" );
}
}
/*
==================
G_WriteSessionData
==================
*/
void G_WriteSessionData( void ) {
int i;
trap_Cvar_Set( "session", va("%i", g_gametype.integer) );
for ( i = 0 ; i < level.maxclients ; i++ ) {
if ( level.clients[i].pers.connected == CON_CONNECTED ) {
G_WriteClientSessionData( &level.clients[i] );
}
}
}