1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2006 Tim Angus
This file is part of Tremulous.
Tremulous 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.
Tremulous 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 Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// g_maprotation.c -- the map rotation system
#include "g_local.h"
static mapRotations_t mapRotations;
/*
===============
G_MapExists
Check if a map exists
===============
*/
static qboolean G_MapExists( char *name )
{
return trap_FS_FOpenFile( va( "maps/%s.bsp", name ), NULL, FS_READ );
}
/*
===============
G_RotationExists
Check if a rotation exists
===============
*/
static qboolean G_RotationExists( char *name )
{
int i;
for( i = 0; i < mapRotations.numRotations; i++ )
{
if( Q_strncmp( mapRotations.rotations[ i ].name, name, MAX_QPATH ) == 0 )
return qtrue;
}
return qfalse;
}
/*
===============
G_ParseCommandSection
Parse a map rotation command section
===============
*/
static qboolean G_ParseMapCommandSection( mapRotationEntry_t *mre, char **text_p )
{
char *token;
// read optional parameters
while( 1 )
{
token = COM_Parse( text_p );
if( !token )
break;
if( !Q_stricmp( token, "" ) )
return qfalse;
if( !Q_stricmp( token, "}" ) )
return qtrue; //reached the end of this command section
if( !Q_stricmp( token, "layouts" ) )
{
token = COM_ParseExt( text_p, qfalse );
mre->layouts[ 0 ] = '\0';
while( token && token[ 0 ] != 0 )
{
Q_strcat( mre->layouts, sizeof( mre->layouts ), token );
Q_strcat( mre->layouts, sizeof( mre->layouts ), " " );
token = COM_ParseExt( text_p, qfalse );
}
continue;
}
Q_strncpyz( mre->postCmds[ mre->numCmds ], token, sizeof( mre->postCmds[ 0 ] ) );
Q_strcat( mre->postCmds[ mre->numCmds ], sizeof( mre->postCmds[ 0 ] ), " " );
token = COM_ParseExt( text_p, qfalse );
while( token && token[ 0 ] != 0 )
{
Q_strcat( mre->postCmds[ mre->numCmds ], sizeof( mre->postCmds[ 0 ] ), token );
Q_strcat( mre->postCmds[ mre->numCmds ], sizeof( mre->postCmds[ 0 ] ), " " );
token = COM_ParseExt( text_p, qfalse );
}
if( mre->numCmds == MAX_MAP_COMMANDS )
{
G_Printf( S_COLOR_RED "ERROR: maximum number of map commands (%d) reached\n",
MAX_MAP_COMMANDS );
return qfalse;
}
else
mre->numCmds++;
}
return qfalse;
}
/*
===============
G_ParseMapRotation
Parse a map rotation section
===============
*/
static qboolean G_ParseMapRotation( mapRotation_t *mr, char **text_p )
{
char *token;
qboolean mnSet = qfalse;
mapRotationEntry_t *mre = NULL;
mapRotationCondition_t *mrc;
// read optional parameters
while( 1 )
{
token = COM_Parse( text_p );
if( !token )
break;
if( !Q_stricmp( token, "" ) )
return qfalse;
if( !Q_stricmp( token, "{" ) )
{
if( !mnSet )
{
G_Printf( S_COLOR_RED "ERROR: map settings section with no name\n" );
return qfalse;
}
if( !G_ParseMapCommandSection( mre, text_p ) )
{
G_Printf( S_COLOR_RED "ERROR: failed to parse map command section\n" );
return qfalse;
}
mnSet = qfalse;
continue;
}
else if( !Q_stricmp( token, "goto" ) )
{
token = COM_Parse( text_p );
if( !token )
break;
mrc = &mre->conditions[ mre->numConditions ];
mrc->unconditional = qtrue;
Q_strncpyz( mrc->dest, token, sizeof( mrc->dest ) );
if( mre->numConditions == MAX_MAP_ROTATION_CONDS )
{
G_Printf( S_COLOR_RED "ERROR: maximum number of conditions for one map (%d) reached\n",
MAX_MAP_ROTATION_CONDS );
return qfalse;
}
else
mre->numConditions++;
continue;
}
else if( !Q_stricmp( token, "if" ) )
{
token = COM_Parse( text_p );
if( !token )
break;
mrc = &mre->conditions[ mre->numConditions ];
if( !Q_stricmp( token, "numClients" ) )
{
mrc->lhs = MCV_NUMCLIENTS;
token = COM_Parse( text_p );
if( !token )
break;
if( !Q_stricmp( token, "<" ) )
mrc->op = MCO_LT;
else if( !Q_stricmp( token, ">" ) )
mrc->op = MCO_GT;
else if( !Q_stricmp( token, "=" ) )
mrc->op = MCO_EQ;
else
{
G_Printf( S_COLOR_RED "ERROR: invalid operator in expression: %s\n", token );
return qfalse;
}
token = COM_Parse( text_p );
if( !token )
break;
mrc->numClients = atoi( token );
}
else if( !Q_stricmp( token, "lastWin" ) )
{
mrc->lhs = MCV_LASTWIN;
token = COM_Parse( text_p );
if( !token )
break;
if( !Q_stricmp( token, "aliens" ) )
mrc->lastWin = PTE_ALIENS;
else if( !Q_stricmp( token, "humans" ) )
mrc->lastWin = PTE_HUMANS;
else
{
G_Printf( S_COLOR_RED "ERROR: invalid right hand side in expression: %s\n", token );
return qfalse;
}
}
else if( !Q_stricmp( token, "random" ) )
mrc->lhs = MCV_RANDOM;
else
{
G_Printf( S_COLOR_RED "ERROR: invalid left hand side in expression: %s\n", token );
return qfalse;
}
token = COM_Parse( text_p );
if( !token )
break;
mrc->unconditional = qfalse;
Q_strncpyz( mrc->dest, token, sizeof( mrc->dest ) );
if( mre->numConditions == MAX_MAP_ROTATION_CONDS )
{
G_Printf( S_COLOR_RED "ERROR: maximum number of conditions for one map (%d) reached\n",
MAX_MAP_ROTATION_CONDS );
return qfalse;
}
else
mre->numConditions++;
continue;
}
else if( !Q_stricmp( token, "}" ) )
return qtrue; //reached the end of this map rotation
mre = &mr->maps[ mr->numMaps ];
if( mr->numMaps == MAX_MAP_ROTATION_MAPS )
{
G_Printf( S_COLOR_RED "ERROR: maximum number of maps in one rotation (%d) reached\n",
MAX_MAP_ROTATION_MAPS );
return qfalse;
}
else
mr->numMaps++;
Q_strncpyz( mre->name, token, sizeof( mre->name ) );
mnSet = qtrue;
}
return qfalse;
}
/*
===============
G_ParseMapRotationFile
Load the map rotations from a map rotation file
===============
*/
static qboolean G_ParseMapRotationFile( const char *fileName )
{
char *text_p;
int i, j, k;
int len;
char *token;
char text[ 20000 ];
char mrName[ MAX_QPATH ];
qboolean mrNameSet = qfalse;
fileHandle_t f;
// load the file
len = trap_FS_FOpenFile( fileName, &f, FS_READ );
if( len < 0 )
return qfalse;
if( len == 0 || len >= sizeof( text ) - 1 )
{
trap_FS_FCloseFile( f );
G_Printf( S_COLOR_RED "ERROR: map rotation file %s is %s\n", fileName,
len == 0 ? "empty" : "too long" );
return qfalse;
}
trap_FS_Read( text, len, f );
text[ len ] = 0;
trap_FS_FCloseFile( f );
// parse the text
text_p = text;
// read optional parameters
while( 1 )
{
token = COM_Parse( &text_p );
if( !token )
break;
if( !Q_stricmp( token, "" ) )
break;
if( !Q_stricmp( token, "{" ) )
{
if( mrNameSet )
{
//check for name space clashes
for( i = 0; i < mapRotations.numRotations; i++ )
{
if( !Q_stricmp( mapRotations.rotations[ i ].name, mrName ) )
{
G_Printf( S_COLOR_RED "ERROR: a map rotation is already named %s\n", mrName );
return qfalse;
}
}
Q_strncpyz( mapRotations.rotations[ mapRotations.numRotations ].name, mrName, MAX_QPATH );
if( !G_ParseMapRotation( &mapRotations.rotations[ mapRotations.numRotations ], &text_p ) )
{
G_Printf( S_COLOR_RED "ERROR: %s: failed to parse map rotation %s\n", fileName, mrName );
return qfalse;
}
//start parsing map rotations again
mrNameSet = qfalse;
if( mapRotations.numRotations == MAX_MAP_ROTATIONS )
{
G_Printf( S_COLOR_RED "ERROR: maximum number of map rotations (%d) reached\n",
MAX_MAP_ROTATIONS );
return qfalse;
}
else
mapRotations.numRotations++;
continue;
}
else
{
G_Printf( S_COLOR_RED "ERROR: unamed map rotation\n" );
return qfalse;
}
}
if( !mrNameSet )
{
Q_strncpyz( mrName, token, sizeof( mrName ) );
mrNameSet = qtrue;
}
else
{
G_Printf( S_COLOR_RED "ERROR: map rotation already named\n" );
return qfalse;
}
}
for( i = 0; i < mapRotations.numRotations; i++ )
{
for( j = 0; j < mapRotations.rotations[ i ].numMaps; j++ )
{
if( !G_MapExists( mapRotations.rotations[ i ].maps[ j ].name ) )
{
G_Printf( S_COLOR_RED "ERROR: map \"%s\" doesn't exist\n",
mapRotations.rotations[ i ].maps[ j ].name );
return qfalse;
}
for( k = 0; k < mapRotations.rotations[ i ].maps[ j ].numConditions; k++ )
{
if( !G_MapExists( mapRotations.rotations[ i ].maps[ j ].conditions[ k ].dest ) &&
!G_RotationExists( mapRotations.rotations[ i ].maps[ j ].conditions[ k ].dest ) )
{
G_Printf( S_COLOR_RED "ERROR: conditional destination \"%s\" doesn't exist\n",
mapRotations.rotations[ i ].maps[ j ].conditions[ k ].dest );
return qfalse;
}
}
}
}
return qtrue;
}
/*
===============
G_PrintRotations
Print the parsed map rotations
===============
*/
void G_PrintRotations( void )
{
int i, j, k;
G_Printf( "Map rotations as parsed:\n\n" );
for( i = 0; i < mapRotations.numRotations; i++ )
{
G_Printf( "rotation: %s\n{\n", mapRotations.rotations[ i ].name );
for( j = 0; j < mapRotations.rotations[ i ].numMaps; j++ )
{
G_Printf( " map: %s\n {\n", mapRotations.rotations[ i ].maps[ j ].name );
for( k = 0; k < mapRotations.rotations[ i ].maps[ j ].numCmds; k++ )
{
G_Printf( " command: %s\n",
mapRotations.rotations[ i ].maps[ j ].postCmds[ k ] );
}
G_Printf( " }\n" );
for( k = 0; k < mapRotations.rotations[ i ].maps[ j ].numConditions; k++ )
{
G_Printf( " conditional: %s\n",
mapRotations.rotations[ i ].maps[ j ].conditions[ k ].dest );
}
}
G_Printf( "}\n" );
}
G_Printf( "Total memory used: %d bytes\n", sizeof( mapRotations ) );
}
/*
===============
G_GetCurrentMapArray
Fill a static array with the current map of each rotation
===============
*/
static int *G_GetCurrentMapArray( void )
{
static int currentMap[ MAX_MAP_ROTATIONS ];
int i = 0;
char text[ MAX_MAP_ROTATIONS * 2 ];
char *text_p, *token;
Q_strncpyz( text, g_currentMap.string, sizeof( text ) );
text_p = text;
while( 1 )
{
token = COM_Parse( &text_p );
if( !token )
break;
if( !Q_stricmp( token, "" ) )
break;
currentMap[ i++ ] = atoi( token );
}
return currentMap;
}
/*
===============
G_SetCurrentMap
Set the current map in some rotation
===============
*/
static void G_SetCurrentMap( int currentMap, int rotation )
{
char text[ MAX_MAP_ROTATIONS * 2 ] = { 0 };
int *p = G_GetCurrentMapArray( );
int i;
p[ rotation ] = currentMap;
for( i = 0; i < mapRotations.numRotations; i++ )
Q_strcat( text, sizeof( text ), va( "%d ", p[ i ] ) );
trap_Cvar_Set( "g_currentMap", text );
trap_Cvar_Update( &g_currentMap );
}
/*
===============
G_GetCurrentMap
Return the current map in some rotation
===============
*/
static int G_GetCurrentMap( int rotation )
{
int *p = G_GetCurrentMapArray( );
return p[ rotation ];
}
/*
===============
G_IssueMapChange
Send commands to the server to actually change the map
===============
*/
static void G_IssueMapChange( int rotation )
{
int i;
int map = G_GetCurrentMap( rotation );
char cmd[ MAX_TOKEN_CHARS ];
// allow a manually defined g_layouts setting to override the maprotation
if( !g_layouts.string[ 0 ] &&
mapRotations.rotations[ rotation ].maps[ map ].layouts[ 0 ] )
{
trap_Cvar_Set( "g_layouts",
mapRotations.rotations[ rotation ].maps[ map ].layouts );
}
trap_SendConsoleCommand( EXEC_APPEND, va( "map %s\n",
mapRotations.rotations[ rotation ].maps[ map ].name ) );
// load up map defaults if g_mapConfigs is set
G_MapConfigs( mapRotations.rotations[ rotation ].maps[ map ].name );
for( i = 0; i < mapRotations.rotations[ rotation ].maps[ map ].numCmds; i++ )
{
Q_strncpyz( cmd, mapRotations.rotations[ rotation ].maps[ map ].postCmds[ i ],
sizeof( cmd ) );
Q_strcat( cmd, sizeof( cmd ), "\n" );
trap_SendConsoleCommand( EXEC_APPEND, cmd );
}
}
/*
===============
G_ResolveConditionDestination
Resolve the destination of some condition
===============
*/
static mapConditionType_t G_ResolveConditionDestination( int *n, char *name )
{
int i;
//search the current rotation first...
for( i = 0; i < mapRotations.rotations[ g_currentMapRotation.integer ].numMaps; i++ )
{
if( !Q_stricmp( mapRotations.rotations[ g_currentMapRotation.integer ].maps[ i ].name, name ) )
{
*n = i;
return MCT_MAP;
}
}
//...then search the rotation names
for( i = 0; i < mapRotations.numRotations; i++ )
{
if( !Q_stricmp( mapRotations.rotations[ i ].name, name ) )
{
*n = i;
return MCT_ROTATION;
}
}
return MCT_ERR;
}
/*
===============
G_EvaluateMapCondition
Evaluate a map condition
===============
*/
static qboolean G_EvaluateMapCondition( mapRotationCondition_t *mrc )
{
switch( mrc->lhs )
{
case MCV_RANDOM:
return rand( ) & 1;
break;
case MCV_NUMCLIENTS:
switch( mrc->op )
{
case MCO_LT:
return level.numConnectedClients < mrc->numClients;
break;
case MCO_GT:
return level.numConnectedClients > mrc->numClients;
break;
case MCO_EQ:
return level.numConnectedClients == mrc->numClients;
break;
}
break;
case MCV_LASTWIN:
return level.lastWin == mrc->lastWin;
break;
default:
case MCV_ERR:
G_Printf( S_COLOR_RED "ERROR: malformed map switch condition\n" );
break;
}
return qfalse;
}
/*
===============
G_AdvanceMapRotation
Increment the current map rotation
===============
*/
qboolean G_AdvanceMapRotation( void )
{
mapRotation_t *mr;
mapRotationEntry_t *mre;
mapRotationCondition_t *mrc;
int currentRotation, currentMap, nextMap;
int i, n;
mapConditionType_t mct;
if( ( currentRotation = g_currentMapRotation.integer ) == NOT_ROTATING )
return qfalse;
currentMap = G_GetCurrentMap( currentRotation );
mr = &mapRotations.rotations[ currentRotation ];
mre = &mr->maps[ currentMap ];
nextMap = ( currentMap + 1 ) % mr->numMaps;
for( i = 0; i < mre->numConditions; i++ )
{
mrc = &mre->conditions[ i ];
if( mrc->unconditional || G_EvaluateMapCondition( mrc ) )
{
mct = G_ResolveConditionDestination( &n, mrc->dest );
switch( mct )
{
case MCT_MAP:
nextMap = n;
break;
case MCT_ROTATION:
G_StartMapRotation( mrc->dest, qtrue );
return qtrue;
break;
default:
case MCT_ERR:
G_Printf( S_COLOR_YELLOW "WARNING: map switch destination could not be resolved: %s\n",
mrc->dest );
break;
}
}
}
G_SetCurrentMap( nextMap, currentRotation );
G_IssueMapChange( currentRotation );
return qtrue;
}
/*
===============
G_StartMapRotation
Switch to a new map rotation
===============
*/
qboolean G_StartMapRotation( char *name, qboolean changeMap )
{
int i;
for( i = 0; i < mapRotations.numRotations; i++ )
{
if( !Q_stricmp( mapRotations.rotations[ i ].name, name ) )
{
trap_Cvar_Set( "g_currentMapRotation", va( "%d", i ) );
trap_Cvar_Update( &g_currentMapRotation );
if( changeMap )
G_IssueMapChange( i );
break;
}
}
if( i == mapRotations.numRotations )
return qfalse;
else
return qtrue;
}
/*
===============
G_StopMapRotation
Stop the current map rotation
===============
*/
void G_StopMapRotation( void )
{
trap_Cvar_Set( "g_currentMapRotation", va( "%d", NOT_ROTATING ) );
trap_Cvar_Update( &g_currentMapRotation );
}
/*
===============
G_MapRotationActive
Test if any map rotation is currently active
===============
*/
qboolean G_MapRotationActive( void )
{
return ( g_currentMapRotation.integer != NOT_ROTATING );
}
/*
===============
G_InitMapRotations
Load and intialise the map rotations
===============
*/
void G_InitMapRotations( void )
{
const char *fileName = "maprotation.cfg";
//load the file if it exists
if( trap_FS_FOpenFile( fileName, NULL, FS_READ ) )
{
if( !G_ParseMapRotationFile( fileName ) )
G_Printf( S_COLOR_RED "ERROR: failed to parse %s file\n", fileName );
}
else
G_Printf( "%s file not found.\n", fileName );
if( g_currentMapRotation.integer == NOT_ROTATING )
{
if( g_initialMapRotation.string[ 0 ] != 0 )
{
G_StartMapRotation( g_initialMapRotation.string, qfalse );
trap_Cvar_Set( "g_initialMapRotation", "" );
trap_Cvar_Update( &g_initialMapRotation );
}
}
}
|