1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2000-2006 Tim Angus
5 
6 This file is part of Tremulous.
7 
8 Tremulous is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12 
13 Tremulous is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Tremulous; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 ===========================================================================
22 */
23 
24 // g_ptr.c -- post timeout restoration handling
25 
26 #include "g_local.h"
27 
28 static connectionRecord_t connections[ MAX_CLIENTS ];
29 
30 /*
31 ===============
32 G_CheckForUniquePTRC
33 
34 Callback to detect ptrc clashes
35 ===============
36 */
G_CheckForUniquePTRC(int code)37 static qboolean G_CheckForUniquePTRC( int code )
38 {
39   int i;
40 
41   if( code == 0 )
42     return qfalse;
43 
44   for( i = 0; i < MAX_CLIENTS; i++ )
45   {
46     if( connections[ i ].ptrCode == code )
47       return qfalse;
48   }
49 
50   return qtrue;
51 }
52 
53 /*
54 ===============
55 G_UpdatePTRConnection
56 
57 Update the data in a connection record
58 ===============
59 */
G_UpdatePTRConnection(gclient_t * client)60 void G_UpdatePTRConnection( gclient_t *client )
61 {
62   if( client && client->pers.connection )
63   {
64     client->pers.connection->clientTeam = client->ps.stats[ STAT_PTEAM ];
65     client->pers.connection->clientCredit = client->ps.persistant[ PERS_CREDIT ];
66   }
67 }
68 
69 /*
70 ===============
71 G_GenerateNewConnection
72 
73 Generates a new connection
74 ===============
75 */
G_GenerateNewConnection(gclient_t * client)76 connectionRecord_t *G_GenerateNewConnection( gclient_t *client )
77 {
78   int     code = 0;
79   int     i;
80 
81   // this should be really random
82   srand( trap_Milliseconds( ) );
83 
84   // there is a very very small possibility that this
85   // will loop infinitely
86   do
87   {
88     code = rand( );
89   } while( !G_CheckForUniquePTRC( code ) );
90 
91   for( i = 0; i < MAX_CLIENTS; i++ )
92   {
93     //found an unused slot
94     if( !connections[ i ].ptrCode )
95     {
96       connections[ i ].ptrCode = code;
97       connections[ i ].clientNum = client->ps.clientNum;
98       client->pers.connection = &connections[ i ];
99       G_UpdatePTRConnection( client );
100 
101       return &connections[ i ];
102     }
103   }
104 
105   return NULL;
106 }
107 
108 /*
109 ===============
110 G_VerifyPTRC
111 
112 Check a PTR code for validity
113 ===============
114 */
G_VerifyPTRC(int code)115 qboolean G_VerifyPTRC( int code )
116 {
117   int i;
118 
119   if( code == 0 )
120     return qfalse;
121 
122   for( i = 0; i < MAX_CLIENTS; i++ )
123   {
124     if( connections[ i ].ptrCode == code )
125       return qtrue;
126   }
127 
128   return qfalse;
129 }
130 
131 /*
132 ===============
133 G_FindConnectionForCode
134 
135 Finds a connection for a given code
136 ===============
137 */
G_FindConnectionForCode(int code)138 connectionRecord_t *G_FindConnectionForCode( int code )
139 {
140   int i;
141 
142   if( code == 0 )
143     return NULL;
144 
145   for( i = 0; i < MAX_CLIENTS; i++ )
146   {
147     if( connections[ i ].ptrCode == code )
148       return &connections[ i ];
149   }
150 
151   return NULL;
152 }
153 
154 /*
155 ===============
156 G_DeletePTRConnection
157 
158 Finds a connection and deletes it
159 ===============
160 */
G_DeletePTRConnection(connectionRecord_t * connection)161 void G_DeletePTRConnection( connectionRecord_t *connection )
162 {
163   if( connection )
164     memset( connection, 0, sizeof( connectionRecord_t ) );
165 }
166 
167 /*
168 ===============
169 G_ResetPTRConnections
170 
171 Invalidate any existing codes
172 ===============
173 */
G_ResetPTRConnections(void)174 void G_ResetPTRConnections( void )
175 {
176   memset( connections, 0, sizeof( connectionRecord_t ) * MAX_CLIENTS );
177 }
178