Debian Codegraph @

code/game/g_killspree.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 ===========================================================================
3 Copyright (C) 2009 Karl Kuglin
4
5 This file is part of the Open Arena source code.
6 Parts of this file utilize code originally written for Tremulous by
7 Tony J. White.
8 Use of that code is governed by GPL version 2 and any later versions.
9
10 Open Arena source code is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
14
15 Open Arena source code is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with Open Arena source code; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 ===========================================================================
24 */
25 // NOTE: This code is by no means complete.
26
27 #include "g_local.h"
28
29 killspree_t *killSprees[ MAX_KSPREE ];
30 deathspree_t *deathSprees[ MAX_DSPREE ];
31 multikill_t *multiKills[ MAX_MULTIKILLS ];
32
33 /*
34 =================
35 G_ReadAltKillSettings
36 Since this is cvar dependent, it has to be placed in G_InitGame after cvars are registered.
37 =================
38 */
39 qboolean G_ReadAltKillSettings( gentity_t *ent, int skiparg )
40 {
41 //Let's Initialize some Spree structs/objects
42 killspree_t *k = NULL;
43 deathspree_t *d = NULL;
44 multikill_t *m = NULL;
45 //spree counters
46 int ksc = 0, dsc = 0, mc = 0;
47 int spreeDivisor;
48
49 //Give us an int to use in "for" loops
50 int i = 0;
51
52 //File Stuff
53 fileHandle_t file;
54 int length;
55 qboolean kspree_read;
56 qboolean dspree_read;
57 qboolean mkill_read;
58 char *cnf, *cnf2;
59 char *t;
60
61 //Let's clear out any existing killing sprees/death sprees. YAYY BG_FREE!!!!!
62 for( i = 0; i < MAX_KSPREE && killSprees[ i ]; i++ ) {
63 BG_Free( killSprees[ i ] );
64 killSprees[ i ] = NULL;
65 }
66
67 for( i = 0; i < MAX_KSPREE && deathSprees[ i ]; i++ ) {
68 BG_Free( deathSprees[ i ] );
69 deathSprees[ i ] = NULL;
70 }
71
72 for( i = 0; i < MAX_MULTIKILLS && multiKills[ i ]; i++ ) {
73 BG_Free( multiKills[ i ] );
74 multiKills[ i ] = NULL;
75 }
76
77 // If the config file is not defined...forget reading/loading
78 if( !g_sprees.string[0] ) {
79 //Let's disable multikills to keep stock excellent sound
80 if( g_altExcellent.integer == 1 )
81 {
82 trap_Cvar_Set( "g_altExcellent", "0" );
83 }
84 return qfalse;
85 }
86 /*
87 only set spreeDivisor to cvar g_spreeDiv if g_spreeDiv is >= "2".
88 0 will cause problems and having 1 is just a fool who wants to hear something
89 on every kill.
90 */
91 if( g_spreeDiv.integer >= 2 ) {
92 level.spreeDivisor = g_spreeDiv.integer;
93 spreeDivisor = level.spreeDivisor;
94 } else {
95 level.spreeDivisor = 5;
96 // We don't want to change the value, keep reminding the server operator.
97 //g_spreeDiv.integer = 5;
98 spreeDivisor = 5;
99 G_Printf( "Error: cvar g_spreeDiv must not be set to 0 or 1, reverting to default settings!\n" );
100 G_Printf( "Error: Set g_spreeDiv higher than 1 if 5 is not desired!\n" );
101 }
102
103 length = trap_FS_FOpenFile( g_sprees.string, &file, FS_READ );
104
105 //If the file can't be accessed/opened.
106 if( length < 0 ) {
107 G_Printf( "Could not open configuration file for Sprees and Multikills %s\n", g_sprees.string );
108 trap_Cvar_Set( "g_altExcellent", "0" );
109 return qfalse;
110 }
111 //Allocate some memory.
112 cnf = BG_Alloc( length + 1 );
113 cnf2 = cnf;
114
115 //Load the whole file up.
116 trap_FS_Read( cnf, length, file );
117 *( cnf + length ) = '\0';
118 trap_FS_FCloseFile( file );
119
120 kspree_read = dspree_read = mkill_read = qfalse;
121
122 //Let's start parsing em.
123 COM_BeginParseSession( g_sprees.string );
124 while( 1 )
125 {
126 t = COM_Parse( &cnf );
127 if( !*t )
128 break;
129 if( !Q_stricmp( t, "[kspree]" ) ) {
130
131 if( ksc >= MAX_KSPREE )
132 return qfalse;
133 k = BG_Alloc( sizeof( killspree_t ) );
134 killSprees[ ksc++ ] = k;
135 kspree_read = qtrue;
136 dspree_read = qfalse;
137 mkill_read = qfalse;
138
139 } else if ( !Q_stricmp( t, "[dspree]" ) ) {
140
141 if( dsc >= MAX_DSPREE )
142 return qfalse;
143 d = BG_Alloc( sizeof( deathspree_t ) );
144 deathSprees[ dsc++ ] = d;
145 dspree_read = qtrue;
146 kspree_read = qfalse;
147 mkill_read = qfalse;
148 } else if ( !Q_stricmp( t, "[mkill]" ) ) {
149
150 if( mc >= MAX_MULTIKILLS )
151 return qfalse;
152 m = BG_Alloc( sizeof( multikill_t ) );
153 multiKills[ mc++ ] = m;
154 mkill_read = qtrue;
155 kspree_read = qfalse;
156 dspree_read = qfalse;
157 //Parse a killing spree
158 } else if ( kspree_read ) {
159 if( !Q_stricmp( t, "level" ) ) {
160 readFile_int( &cnf, &k->spreeLevel );
161 //Let's take the spreeLevel and multiply it by the spreeDivisor to give us our count
162 k->streakCount = ( ( k->spreeLevel ) * ( spreeDivisor ) );
163 } else if ( !Q_stricmp( t, "message" ) ) {
164 readFile_string( &cnf, k->spreeMsg, sizeof( k->spreeMsg ) );
165 } else if ( !Q_stricmp( t, "printpos" ) ) {
166 readFile_int( &cnf, &k->position );
167 } else if ( !Q_stricmp( t, "sound" ) ) {
168 readFile_string( &cnf, k->sound2Play, sizeof( k->sound2Play ) );
169 } else {
170 COM_ParseError( "Killing Spree unrecognized token \"%s\"", t );
171 }
172 } else if ( dspree_read ) {
173 if( !Q_stricmp( t, "level" ) ) {
174 readFile_int( &cnf, &d->spreeLevel );
175 //Let's take the spreeLevel and multiply it by the spreeDivisor to give us our count
176 d->streakCount = ( ( d->spreeLevel ) * ( spreeDivisor ) );
177 } else if ( !Q_stricmp( t, "message" ) ) {
178 readFile_string( &cnf, d->spreeMsg, sizeof( d->spreeMsg ) );
179 } else if ( !Q_stricmp( t, "printpos" ) ) {
180 readFile_int( &cnf, &d->position );
181 } else if ( !Q_stricmp( t, "sound" ) ) {
182 readFile_string( &cnf, d->sound2Play, sizeof( d->sound2Play ) );
183 } else {
184 COM_ParseError( "Death Spree unrecognized token \"%s\"", t );
185 }
186 } else if ( mkill_read ) {
187 if ( !Q_stricmp( t, "kills" ) ) {
188 readFile_int( &cnf, &m->kills );
189 } else if ( !Q_stricmp( t, "message" ) ) {
190 readFile_string( &cnf, m->killMsg, sizeof( m->killMsg ) );
191 } else if ( !Q_stricmp( t, "sound" ) ) {
192 readFile_string( &cnf, m->sound2Play, sizeof( m->sound2Play ) );
193 } else {
194 COM_ParseError( "Multikill unrecognized token \"%s\"", t );
195 }
196 } else {
197 COM_ParseError( "unexpected token \"%s\"", t );
198 }
199 }
200 //Let's "free" some memory now.
201 BG_Free( cnf2 );
202 G_Printf("Sprees/Kills: loaded %d killing sprees, %d death sprees, and %d multikills.\n", ksc, dsc, mc );
203 //Mark the Upper Bounds of the Arrays (Since they start numbering at 0, We subtract 1 )
204 level.kSpreeUBound = ( ksc - 1 );
205 level.dSpreeUBound = ( dsc - 1 );
206 if( mc > 0 ) {
207 level.mKillUBound = ( mc - 1 );
208 } else {
209 level.mKillUBound = -1;
210 //KK-OAX We don't have any kills defined, revert to stock.
211 //FIXME: Make sure this change shows up in the console...
212 if( g_altExcellent.integer == 1 ) {
213 trap_Cvar_Set( "g_altExcellent", "0" );
214 }
215
216 }
217 return qtrue;
218 }
219
220
221 static char *fillPlaceHolder( char *stringToSearch, char *placeHolder, char *replaceWith )
222 {
223 static char output[ MAX_SAY_TEXT ];
224 char *p;
225
226 if( !( p = strstr( stringToSearch, placeHolder ) ) )
227 return stringToSearch;
228
229 strncpy( output, stringToSearch, p - stringToSearch );
230 output[ p - stringToSearch ] = '\0';
231
232 Q_snprintf( output + ( p - stringToSearch ), output - stringToSearch, "%s%s", replaceWith, p + strlen( placeHolder ) );
233
234 return output;
235 }
236
237 //This concatenate's the message to broadcast to the clients.
238 static char *CreateMessage( gentity_t *ent, char *message, char *spreeNumber )
239 {
240 static char output[ MAX_SAY_TEXT ] = { "" };
241
242 char name[ MAX_NAME_LENGTH ];
243 //Do some sanity checks
244 if( !ent ) {
245 return output;
246 } else if ( !*message ) {
247 return output;
248 } else if ( !spreeNumber ) {
249 return output;
250 }
251 //Get the player name.
252 Q_strncpyz( name, ent->client->pers.netname, sizeof( name ) );
253 //Do Our Replacements
254 Q_strncpyz( output, fillPlaceHolder( message, "[n]", name ), sizeof( output ) );
255 Q_strncpyz( output, fillPlaceHolder( output, "[k]", spreeNumber ), sizeof( output ) );
256 return output;
257 }
258
259
260 /*
261 ================
262 G_RunStreakLogic
263 KK-OAX This is called from player_die.
264 It does all the adding resetting of kill/death streaks
265 to be compared against the kill/death spree levels.
266 ================
267 */
268 void G_RunStreakLogic( gentity_t *attacker, gentity_t *victim )
269 {
270 //We only want to sanity check for the victim at this point.
271 if( !victim || !victim->client )
272 return;
273
274 //We will reset the victim's killstreak counter
275 victim->client->pers.killstreak = 0;
276 //Add one to the death streak counter
277 victim->client->pers.deathstreak++;
278
279 //Let's check for a deathspree for the victim
280 G_CheckForSpree( victim, victim->client->pers.deathstreak, qfalse );
281
282 //Move on to the attacker
283 //Make sure they are a client and that the attacker and victim are not the same client
284 //We don't want suicide to count towards a killstreak...
285 if( ( attacker )
286 && ( attacker->client )
287 && ( attacker != victim ) ) {
288
289 //Check the gametype--If FFA enabled, everybody's on the same team.
290 if( g_gametype.integer >= GT_TEAM && g_ffa_gt!= 1 ) {
291 //If they are on the same team we don't want to count it towards a killing spree.
292 if( OnSameTeam( victim, attacker ) ) {
293 return;
294 }
295 }
296 //Add to the killstreak, reset the deathstreak
297 attacker->client->pers.deathstreak = 0;
298 attacker->client->pers.killstreak++;
299
300 //Let's check for a killingspree for the attacker
301 G_CheckForSpree( attacker, attacker->client->pers.killstreak, qtrue );
302
303 }
304 }
305
306
307
308 //If the streak / spree divisor is a whole number, we have a spree
309 static qboolean TestSpreeWhole( int streak2Test ) {
310 float float2Test;
311 float spreeFDiv;
312 float resultf;
313 int spreeDiv;
314 int result;
315
316 float2Test = streak2Test;
317 spreeFDiv = level.spreeDivisor;
318 spreeDiv = level.spreeDivisor;
319 result = ( streak2Test / spreeDiv );
320 resultf = ( float2Test / spreeFDiv );
321
322 if( result == resultf ) {
323 return qtrue;
324 } else {
325 return qfalse;
326 }
327 }
328
329 /*
330 ==================
331 G_CheckForSpree
332 ==================
333 */
334 void G_CheckForSpree( gentity_t *ent, int streak2Test, qboolean checkKillSpree )
335 {
336 int i;
337 char *returnedString;
338 //If somebody want's to award killing sprees above 99 kills he/she can mod this his/herself!!! :)
339 char streakcount[ 3 ];
340 char *sound;
341 int position;
342 int soundIndex;
343
344 qboolean isSpree = qfalse;
345 int divisionHolder;
346
347 //Probably Not Needed, but to protect Server Ops from Crashing their Stuff MidMatch
348 if( level.spreeDivisor < 1 ) {
349 return;
350 }
351 divisionHolder = ( streak2Test / level.spreeDivisor );
352 //if it's a deathspree
353 if( !checkKillSpree ) {
354 //Is the streak higher than the largest level defined?
355 if( divisionHolder > level.dSpreeUBound ) {
356 //Let's make sure it's a whole number to mimic the other sprees
357 isSpree = TestSpreeWhole( streak2Test );
358 if( !isSpree ) {
359 return;
360 }
361 //We've made it this far...now do the largest spree defined.
362 Q_snprintf( streakcount, sizeof( streakcount ), "%i", streak2Test );
363 //Check if deathSprees is NULL (actual problem!)
364 if(!deathSprees[ level.dSpreeUBound ])
365 return;
366 returnedString = CreateMessage( ent, deathSprees[ level.dSpreeUBound ]->spreeMsg, streakcount );
367 position = deathSprees[ level.dSpreeUBound ]->position;
368 sound = deathSprees[ level.dSpreeUBound ]->sound2Play;
369 soundIndex = G_SoundIndex( sound );
370 //Play the Sound
371 G_GlobalSound( soundIndex );
372 //Print the Message
373 if( position == CENTER_PRINT ) {
374 AP( va("cp \"%s\"", returnedString ) );
375 } else {
376 AP( va("chat \"%s\"", returnedString ) );
377 }
378 } else {
379 for( i = 0; deathSprees[ i ]; i++ ) {
380 //If the deathSpree is equal to the streak to test
381 if( deathSprees[ i ]->streakCount == streak2Test ) {
382 //Using Q_snprintf to change the int into a char for replacement.
383 Q_snprintf( streakcount, sizeof( streakcount ), "%i", deathSprees[ i ]->streakCount );
384 //Let's grab the message to show, fill up the placeholders and concatenate it.
385 returnedString = CreateMessage ( ent, deathSprees[ i ]->spreeMsg, streakcount );
386 //Grab the Print Position ( 1 for Center Printing, 2 for Chat )
387 position = deathSprees[ i ]->position;
388 //Grab the Sound
389 sound = deathSprees[ i ]->sound2Play;
390 //Index the Sound
391 soundIndex = G_SoundIndex( sound );
392 //Play the Sound
393 G_GlobalSound( soundIndex );
394 //Print the Message
395 if( position == CENTER_PRINT ) {
396 AP( va("cp \"%s\"", returnedString ) );
397 } else {
398 AP( va("chat \"%s\"", returnedString ) );
399 }
400 break;
401 }
402 }
403 }
404 } else /*if( checkKillSpree )*/ {
405 //Is the streak higher than the largest level defined?
406 if( divisionHolder > level.kSpreeUBound ) {
407 //Let's make sure it's a whole number to mimic the other sprees
408 isSpree = TestSpreeWhole( streak2Test );
409 if( !isSpree ) {
410 return;
411 }
412 //We've made it this far...now do the largest spree defined.
413 Q_snprintf( streakcount, sizeof( streakcount ), "%i", streak2Test );
414 //Check if killSprees is NULL (actual problem!)
415 if(!killSprees[ level.kSpreeUBound ])
416 return;
417 returnedString = CreateMessage( ent, killSprees[ level.kSpreeUBound ]->spreeMsg, streakcount );
418 position = killSprees[ level.kSpreeUBound ]->position;
419 sound = killSprees[ level.kSpreeUBound ]->sound2Play;
420 soundIndex = G_SoundIndex( sound );
421 soundIndex = G_SoundIndex( sound );
422 //G_GlobalSound( soundIndex );
423 G_Sound(ent,0,soundIndex);
424 /* Doesn't do anything at the moment. cp does not work while kill message is displayed
425 * if( position == CENTER_PRINT ) {
426 //Only Center print for player doing the killing spree
427 CP( va("cp \"%s\"", returnedString ) );
428 }*/
429 AP( va("chat \"%s\"", returnedString ) );
430 } else {
431 for( i = 0; killSprees[ i ]; i++ ) {
432 if( killSprees[ i ]->streakCount == streak2Test ) {
433 Q_snprintf( streakcount, sizeof( streakcount ), "%i", killSprees[ i ]->streakCount );
434 returnedString = CreateMessage ( ent, killSprees[ i ]->spreeMsg, streakcount );
435 position = killSprees[ i ]->position;
436 sound = killSprees[ i ]->sound2Play;
437 soundIndex = G_SoundIndex( sound );
438 soundIndex = G_SoundIndex( sound );
439 //G_GlobalSound( soundIndex );
440 G_Sound(ent,0,soundIndex);
441 /*if( position == CENTER_PRINT ) {
442 //Only Center print for player doing the killing spree
443 CP( va("cp \"%s\"", returnedString ) );
444 }*/
445 AP( va("chat \"%s\"", returnedString ) );
446 break;
447 }
448 }
449 }
450 } /*else {
451 G_Printf("Killing Spree Error in G_CheckForSpree\n");
452 return;
453 }*/
454 }
455
456
457 /*
458 ===============
459 G_checkForMultiKill
460 ===============
461 */
462 void G_checkForMultiKill( gentity_t *ent ) {
463
464 int i;
465 char *returnedString;
466 char *sound;
467 int soundIndex;
468 int multiKillCount;
469 char multiKillString[ 2 ];
470 gclient_t *client;
471 int clientNum;
472
473 client = ent->client;
474 clientNum = client - level.clients;
475
476 //Let's grab the multikill count for the player first
477 multiKillCount = ent->client->pers.multiKillCount;
478
479 if( multiKillCount > multiKills[ level.mKillUBound ]->kills ) {
480 Q_snprintf( multiKillString, sizeof( multiKillString ), "%i", multiKillCount );
481 if(!multiKills[ level.mKillUBound ])
482 return; //If null
483 returnedString = CreateMessage ( ent, multiKills[ level.mKillUBound ]->killMsg, multiKillString );
484 sound = multiKills[ level.mKillUBound ]->sound2Play;
485 soundIndex = G_SoundIndex( sound );
486 G_Sound(ent, 0, soundIndex );
487 AP( va("chat \"%s\"", returnedString ) );
488 return;
489 } else {
490 for( i = 0; multiKills[ i ]; i++ ) {
491 //If the multikill count is equal to a killLevel let's do this.
492
493 if( multiKills[ i ]->kills == multiKillCount ) {
494 Q_snprintf( multiKillString, sizeof( multiKillString ), "%i", multiKills[ i ]->kills );
495 //Build the Message
496 returnedString = CreateMessage ( ent, multiKills[ i ]->killMsg, multiKillString );
497 //Grab the sound
498 sound = multiKills[ i ]->sound2Play;
499 //Index the sound
500 soundIndex = G_SoundIndex( sound );
501 //Play the sound
502 G_Sound(ent, 0, soundIndex );
503 /* Print the String
504 Since we don't want to clutter screens (the player is already going to get the excellent icon)
505 we won't give them an option to centerprint.
506 */
507 AP( va("chat \"%s\"", returnedString ) );
508 break;
509 }
510 }
511
512 }
513
514 }
515
516
517