-
Notifications
You must be signed in to change notification settings - Fork 1
/
kots_maplist.c
859 lines (708 loc) · 20.7 KB
/
kots_maplist.c
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#include "kots_maplist.h"
#include "kots_array.h"
#include "kots_utils.h"
#define KOTS_MAPVOTE_TIMEOUT 30
//keep track of previously visited maps
array_t *prevmaps;
array_t *failed_votes;
void Kots_Maplist_FreeEntry(void *item)
{
gi.TagFree(item);
}
void Kots_Maplist_Init()
{
prevmaps = Array_Create(NULL, MAPLIST_CAPACITY, MAPLIST_GROWSIZE);
maplist.maps = Array_Create(NULL, MAPLIST_CAPACITY, MAPLIST_GROWSIZE);
failed_votes = Array_Create(NULL, MAPLIST_CAPACITY, MAPLIST_GROWSIZE);
//we don't want to free previous maps we only want to remove them
prevmaps->FreeItem = NULL;
maplist.maps->FreeItem = Kots_Maplist_FreeEntry;
}
void Kots_Maplist_Free()
{
Array_ClearAll(prevmaps);
Array_Delete(maplist.maps);
Array_Delete(prevmaps);
Array_Delete(failed_votes);
prevmaps = NULL;
maplist.maps = NULL;
}
votefail_t *GetVoteFail(int mapindex)
{
//find the vote fail
ULONG i;
votefail_t *vote = NULL;
//loop through each item to find it
for (i = 0; i < failed_votes->length; i++)
{
vote = (votefail_t *)failed_votes->values[i];
if (vote->mapindex == mapindex)
return vote;
}
return NULL; //index not found
}
void AddVoteFail(int mapindex)
{
//find the vote fail
votefail_t *vote = GetVoteFail(mapindex);
//if the vote fail was found increment the number of failures
if (vote)
vote->fails++;
else
{
//create a new failed vote attempt
vote = malloc(sizeof(*vote));
vote->mapindex = mapindex;
vote->fails = 1;
//add the new failed vote to the list
Array_PushBack(failed_votes, vote);
}
}
qboolean CanVoteMap(int mapindex)
{
//find the vote fail
votefail_t *vote = GetVoteFail(mapindex);
//allow voting if we haven't failed 3 or more times
return (!vote || vote->fails < 3);
}
ULONG GetPreviousMapIndex(char *mapname)
{
//find the index of the specified map
ULONG i;
//loop through each item to find it's index
for (i = 0; i < prevmaps->length; i++)
if (Q_stricmp(((mapentry_t *)prevmaps->values[i])->mapname, mapname) == 0)
return i;
return -1; //index not found
}
ULONG GetMaplistIndex(char *mapname)
{
//find the index of the specified map
ULONG i;
//loop through each item to find it's index
for (i = 0; i < maplist.maps->length; i++)
if (Q_stricmp(((mapentry_t *)maplist.maps->values[i])->mapname, mapname) == 0)
return i;
return -1; //index not found
}
void Kots_Maplist_UpdateMapIndex()
{
maplist.currentmap = GetMaplistIndex(level.mapname);
}
void Kots_Maplist_RemoveMap(ULONG mapindex)
{
if (maplist.maps->length <= mapindex)
{
gi.dprintf("ERROR: Tried to remove map at index %u. Out of array bounds.\n", mapindex);
return;
}
Array_DeleteAt(maplist.maps, mapindex);
//loop through each item to update it's index
for ( ; mapindex < maplist.maps->length; mapindex++)
{
((mapentry_t *)maplist.maps->values[mapindex])->index = mapindex;
}
}
FILE *Kots_Maplist_OpenFile(char *filename)
{
FILE *fp = NULL;
char path[MAPLIST_MAX_FILENAME];
//create path to the file using gamedir
Kots_snprintf(path, MAPLIST_MAX_FILENAME, "%s/%s", gamedir->string, filename);
//try to open the file
if ((fp = fopen(path, "r")) == NULL)
{
// file did not load
gi.dprintf ("Could not open file \"%s\".\n", filename);
return NULL;
}
return fp;
}
void Kots_Maplist_CloseFile(FILE *fp)
{
//if we have a valid file pointer
if (fp)
fclose(fp);
else //no file is opened
gi.dprintf ("ERROR: Attempted to close invalid file handle.\n");
}
int Kots_Maplist_SortCompare(void *item1, void *item2)
{
mapentry_t *a = (mapentry_t *)item1;
mapentry_t *b = (mapentry_t *)item2;
return Q_stricmp(a->mapname, b->mapname);
}
int Kots_Maplist_Load(char *filename)
{
FILE *fp;
char line[1024];
mapentry_t *entry;
int result;
//if we try to load a null or empty string just clear the maplist and return
if (!filename || !filename[0])
{
Kots_Maplist_ClearMapList();
return 1;
}
//open the specified file
fp = Kots_Maplist_OpenFile(filename);
//if we got a file poniter
if (fp)
{
//if there's already a maplist loaded then clear it
if (maplist.maps->length > 0)
Kots_Maplist_ClearMapList();
while (fgets(line, sizeof(line), fp) != NULL)
{
//ignore lines starting with # because they are comments
if (line[0] == '#')
continue;
//allocate a new mapentry to read in
entry = gi.TagMalloc(sizeof(*entry), TAG_GAME);
//read in the map information
result = sscanf(line, "%d %d %d %256s", &entry->min_players, &entry->max_players, &entry->priority, entry->mapname);
//check if the line was successfully formatted
if (result == 4)
{
if (entry->min_players < 0)
entry->min_players = 0;
else if (entry->min_players > 99)
entry->min_players = 99;
if (entry->max_players > 99)
entry->max_players = 99;
else if (entry->max_players < entry->min_players)
entry->max_players = entry->min_players;
if (entry->priority < 0)
entry->priority = 0;
else if (entry->priority > 9)
entry->priority = 9;
entry->index = maplist.maps->length;
Array_PushBack(maplist.maps, entry);
}
else
{
gi.TagFree(entry);
entry = NULL;
}
}
Kots_Maplist_CloseFile(fp);
if (!maplist.maps->length)
{
gi.dprintf("No maps found in map list %s.\n", filename);
return 0; // abnormal exit -- no maps in file
}
else
{
Kots_strncpy(maplist.filename, filename, MAPLIST_MAX_FILENAME);
gi.cvar_forceset(kots_maplist->name, maplist.filename);
gi.dprintf ("%d map(s) loaded from maplist %s.\n", maplist.maps->length, filename);
maplist.currentmap = GetMaplistIndex(level.mapname);
return 1; // normal exit
}
}
return 0; // abnormal exit -- couldn't open file
}
int Kots_Maplist_GetPlayerCount()
{
int i;
int count = 0;
for (i = 1; i <= maxclients->value; i++)
if (g_edicts[i].inuse)
count++;
return count;
}
int Kots_Maplist_GetNextMap()
{
ULONG i;
ULONG start = maplist.currentmap + 1;
mapentry_t *entry;
int players = Kots_Maplist_GetPlayerCount();
if (start >= maplist.maps->length)
start = 0; //start back at the beginning
i = start;
//try to obey all the rules in the first pass
do
{
entry = (mapentry_t *)maplist.maps->values[i];
if (entry->priority != 0 && players >= entry->min_players && players <= entry->max_players)
return i;
i = (i + 1) % maplist.maps->length;
} while (i != start);
//only obey priority now
do
{
entry = (mapentry_t *)maplist.maps->values[i];
if (entry->priority != 0)
return i;
i = (i + 1) % maplist.maps->length;
} while (i != start);
return start;
}
int Kots_Maplist_GetRandomMap()
{
ULONG i;
int players, priority, rand_index;
int total = 0;
mapentry_t *entry;
array_t *valid = Array_Create(NULL, maplist.maps->length, MAPLIST_GROWSIZE);
valid->FreeItem = NULL;
//get the number of players in the game
players = Kots_Maplist_GetPlayerCount();
//first get all qualified maps
for (i = 0; i < maplist.maps->length; i++)
{
entry = (mapentry_t *)maplist.maps->values[i];
//no matter what don't allow re-entry into the current map
if (entry->index == maplist.currentmap)
continue;
//check normal requirements first
if (entry->min_players > players)
continue;
if (entry->max_players < players)
continue;
if (entry->priority == 0)
continue;
//try not to use the previous maps if possible
if (Array_IndexOf(prevmaps, entry) != -1)
continue;
//map appears to be valid
Array_PushBack(valid, entry);
total += entry->priority; //tally up priority
}
//if we don't have enough maps then just pick any map
if (valid->length < MAPLIST_MINMAPS)
{
Array_ClearAll(valid);
total = 0;
//first get all qualified maps
for (i = 0; i < maplist.maps->length; i++)
{
entry = (mapentry_t *)maplist.maps->values[i];
//no matter what don't allow re-entry into the current map
if (entry->index == maplist.currentmap)
continue;
//only check priority now
if (entry->priority == 0)
continue;
//try not to use the previous maps if possible
if (Array_IndexOf(prevmaps, entry) != -1)
continue;
//map appears to be valid
Array_PushBack(valid, entry);
total += entry->priority; //tally up priority
}
}
//if we still don't have enough maps try dipping into previous maps
if (valid->length < MAPLIST_MINMAPS)
{
//loop through previous maps for valid maps or until we hit the minimum
for (i = 0; i < prevmaps->length && valid->length < MAPLIST_MINMAPS; i++)
{
entry = (mapentry_t *)prevmaps->values[i];
//no matter what don't allow re-entry into the current map
if (entry->index == maplist.currentmap)
continue;
//only check priority now
if (entry->priority == 0)
continue;
//map appears to be valid
Array_PushBack(valid, entry);
total += entry->priority; //tally up priority
}
}
//if for some reason we still don't have any choices just blindly pick one
if (!valid->length)
{
//randomly pick a new map, but make sure it's not the current one
do
{
rand_index = rand() % maplist.maps->length;
} while (rand_index != maplist.currentmap && maplist.maps->length > 1);
}
else
{
rand_index = rand() % total; //pick a number between 0 and total priority
priority = 0;
//find the map we picked
for (i = 0; i < valid->length; i++)
{
entry = (mapentry_t *)valid->values[i];
priority += entry->priority;
if (priority > rand_index)
{
rand_index = entry->index;
break;
}
}
}
//delete the array
Array_Delete(valid);
return rand_index; //return index of map to use
}
void Kots_Maplist_AddPrevMap()
{
if (maplist.maps->length > 0 && maplist.currentmap != -1)
{
int prev = kots_prevmaps->value;
//enforce at least 1 previous map to be saved
if (prev <= 0)
{
gi.cvar_forceset(kots_prevmaps->name, "1");
prev = 1;
}
//if we've hit the limit pop the first map off the list
if (prevmaps->length >= kots_prevmaps->value || prevmaps->length >= maplist.maps->length)
Array_PopFront(prevmaps);
Array_PushBack(prevmaps, maplist.maps->values[maplist.currentmap]);
}
}
char *Kots_Maplist_GetName(int index)
{
return ((mapentry_t *)maplist.maps->values[index])->mapname;
}
void Kots_Maplist_ClearMapList()
{
if (!maplist.maps->length)
gi.cprintf(NULL, PRINT_HIGH, "No maplist loaded.\n");
{
if (level.mapvotestarted)
{
level.mapvote = -1;
level.mapvotestarted = 0;
gi.bprintf(PRINT_HIGH, "Map vote was cancelled because the server cleared the maplist.\n");
}
maplist.currentmap = -1;
maplist.filename[0] = '\0';
gi.cvar_forceset(kots_maplist->name, "");
Array_ClearAll(prevmaps);
Array_DeleteAll(maplist.maps);
gi.cprintf(NULL, PRINT_HIGH, "Maplist cleared.\n");
}
}
void Kots_Maplist_Show(edict_t *ent)
{
if (!maplist.maps->length)
{
gi.cprintf(ent, PRINT_HIGH, "No maplist currently active.\nServers should use 'sv maplist' to set one.\n");
}
else
{
//static char *modes[] = {NULL, "Deathmatch", "Defend The Base", "Capture The Flag", NULL};
static char *rotations[] = {"Sequential", "Random"};
ULONG i;
mapentry_t *entry;
if (ent == NULL) // only show filename to server
Kots_SlowPrint(ent, "Maplist File: %s\n", maplist.filename);
for (i = 0; i < maplist.maps->length; i++)
{
entry = (mapentry_t *)maplist.maps->values[i];
Kots_SlowPrint(ent, "#%4d. %s\n", i + 1, entry->mapname);
}
Kots_SlowPrint(ent, "%d map(s) in list.\n", maplist.maps->length);
Kots_SlowPrint(ent, "Rotation Mode: %s\n", rotations[maplist.rotationflag]);
if (maplist.currentmap == -1)
Kots_SlowPrint(ent, "Current map: %s (not in maplist)\n");
else
Kots_SlowPrint(ent, "Current map: #%d %s\n", maplist.currentmap + 1 , Kots_Maplist_GetName(maplist.currentmap));
}
}
void Kots_Maplist_ServerCommand()
{
char *command = gi.argv(2);
switch (gi.argc())
{
case 3:
//determine which command was used
if (Q_stricmp(command, "next") == 0)
{
Kots_Maplist_Goto(NULL, "next");
break;
}
else if (Q_stricmp(command, "clear") == 0)
{
Kots_Maplist_ClearMapList();
break;
}
else if (Q_stricmp(command, "show") == 0)
{
Kots_Maplist_Show(NULL);
break;
}
else if (Q_stricmp(command, "reload") == 0)
{
Kots_Maplist_Load(maplist.filename);
break;
}
//case 3 falls into case 4 when setting a maplist
case 4:
//if we specified to go to a specific map
if (Q_stricmp(command, "goto") == 0)
Kots_Maplist_Goto(NULL, gi.argv(3));
//if we're just changing the rotation flag stop here
else if (Q_stricmp(command, "rotation") == 0)
Kots_Maplist_ServerChangeRotation();
else //assume we're trying to set a new maplist
{
// load new maplist
if (Kots_Maplist_Load(command))
{
maplist.rotationflag = atoi(gi.argv(3));
gi.cvar_forceset(kots_maprotate->name, va("%i", maplist.rotationflag));
gi.cprintf(NULL, PRINT_HIGH, "Maplist loaded.\n");
}
}
break;
default:
Kots_Maplist_ServerDisplayUsage(NULL);
}
}
void Kots_Maplist_ServerDisplayUsage(edict_t *ent)
{
gi.cprintf(ent, PRINT_HIGH, "Usage:\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist <filename> [<rotation>]\n");
gi.cprintf(ent, PRINT_HIGH, " <filename> - server ini file\n");
gi.cprintf(ent, PRINT_HIGH, " <rotation> - 0 = sequential (default)\n");
gi.cprintf(ent, PRINT_HIGH, " 1 = random\n\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist rotation <rotation> - change the rotation\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist next - go to the next map\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist goto <mapname> - go to the specified map\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist clear - clear the current maplist\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist show - show the maplist\n");
gi.cprintf(ent, PRINT_HIGH, "sv maplist reload - reloads the maplist\n");
}
edict_t *CreateTargetChangeLevel(char *map);
void Kots_Maplist_Goto(edict_t *ent, char *mapname)
{
//if a maplist exists end the current level and go to the next
if (maplist.maps->length > 0)
{
if (Q_stricmp(mapname, "next") == 0)
{
//end the level to force next map
EndDMLevel();
}
else
{
//attempt to find the map by name
int index = GetMaplistIndex(mapname);
if (index >= 0)
{
BeginIntermission(CreateTargetChangeLevel(mapname));
maplist.currentmap = index;
}
else
gi.cprintf(ent, PRINT_HIGH, "Specified map was not found in the maplist.\n");
}
}
else
gi.cprintf(ent, PRINT_HIGH, "No maplist loaded.\n");
}
void Kots_Maplist_ServerChangeRotation()
{
//if a maplist exists end the current level and go to the next
if (maplist.maps->length > 0)
{
int rotation = atoi(gi.argv(3));
if (rotation < ML_ROTATE_SEQ || rotation > ML_ROTATE_RANDOM)
gi.cprintf(NULL, PRINT_HIGH, "Invalid rotation setting. Should be 0 (sequential) or 1 (random).\n");
else
{
gi.cvar_forceset(kots_maprotate->name, va("%i", rotation));
maplist.rotationflag = rotation;
}
}
else
gi.cprintf(NULL, PRINT_HIGH, "No maplist loaded.\n");
}
void Kots_Maplist_CheckVote(edict_t *ent)
{
int i;
int players = 0;
int yes = 0;
int no = 0;
int needed = 0;
edict_t *cl_ent;
//don't check the vote when none in progress or at intermission
if (!level.mapvotestarted || level.intermissiontime)
return;
cl_ent = g_edicts + 1;
for (i = 0; i<game.maxclients; i++, cl_ent++)
{
if (!cl_ent->inuse || !cl_ent->character)
continue;
if(!cl_ent->character->is_loggedin)
continue;
players++;
if(cl_ent->client->pers.kots_persist.map_vote == MAPVOTE_NO)
no++;
else if(cl_ent->client->pers.kots_persist.map_vote == MAPVOTE_YES)
yes++;
}
needed = (players / 2) + 1;
if (ent) //no entity is passed when a player disconnects so we don't print this message
gi.bprintf(PRINT_MEDIUM, "Vote for map %s: %i YES, %i NO, NEEDED: %i\n", Kots_Maplist_GetName(level.mapvote), yes, no, needed);
if(yes >= needed) // passed
{
char *name = Kots_Maplist_GetName(level.mapvote);
gi.bprintf(PRINT_MEDIUM, "Vote for map %s PASSED!\n", name);
Kots_snprintf(level.nextmap, 64, "%s", name); //SWB - we need to set the map in order to go to it
level.passed = true;
EndDMLevel();
//reset mapvote state
level.mapvote = -1;
level.mapvotestarted = 0;
}
else if(players <= (yes + no)) // everyone voted
{
gi.bprintf(PRINT_MEDIUM, "Vote for map %s FAILED!\n", Kots_Maplist_GetName(level.mapvote));
//reset mapvote state
AddVoteFail(level.mapvote);
level.mapvote = -1;
level.mapvotestarted = 0;
cl_ent = g_edicts + 1;
for (i=0 ; i<game.maxclients ; i++, cl_ent++) // reset votes
{
if (!cl_ent->inuse || !cl_ent->character)
continue;
if(!cl_ent->character->is_loggedin) // specs dont count
continue;
cl_ent->client->pers.kots_persist.map_vote = MAPVOTE_NONE;
}
}
}
void Kots_Maplist_CheckVoteTimeout()
{
int i;
edict_t *cl_ent;
if (level.intermissiontime) //already in intermission
return;
if (!level.mapvotestarted) // no vote
return;
else if (level.mapvotestarted > level.time - KOTS_MAPVOTE_TIMEOUT) // timeout
return;
for (i=0 ; i<game.maxclients ; i++) // reset votes
{
cl_ent = g_edicts + 1 + i;
if (!cl_ent->inuse)
continue;
if(!cl_ent->character->is_loggedin)
continue;
cl_ent->client->pers.kots_persist.map_vote = MAPVOTE_NONE;
}
// Send a message
gi.bprintf(PRINT_MEDIUM, "Vote for map %s FAILED (time out)\n", Kots_Maplist_GetName(level.mapvote));
// Ok expire the map vote
AddVoteFail(level.mapvote);
level.mapvote = 0;
level.mapvotestarted = 0;
gi.sound (NULL, CHAN_VOICE, gi.soundindex("world/fuseout.wav"), 1, ATTN_NONE, 0); // play a sound
}
void Kots_Maplist_Vote(edict_t *ent, int vote)
{
//ensure vote is in progress
if(!level.mapvotestarted)
{
gi.cprintf(ent, PRINT_HIGH, "There is no vote currently in progress.\n");
return;
}
//ensure that we haven't already voted
if(ent->client->pers.kots_persist.map_vote != MAPVOTE_NONE)
{
gi.cprintf(ent, PRINT_HIGH, "You have already voted.\n");
return;
}
ent->client->pers.kots_persist.map_vote = vote;
Kots_Maplist_CheckVote(ent);
}
void Kots_Maplist_VoteCommand(edict_t *ent)
{
char *mapname;
int index;
if (gi.argc() < 3)
{
gi.cprintf(ent, PRINT_HIGH, "Usage: vote map mapname\n");
return;
}
if(level.mapvotestarted) // theres a vote going on
{
gi.cprintf(ent, PRINT_HIGH, "There is already a vote in progress.\n");
return;
}
if(level.time < 20) // give people a chance to join
{
gi.cprintf(ent, PRINT_HIGH, "This map just started. Please wait %i more seconds.\n", (int)ceil(20.0 - level.time));
return;
}
mapname = gi.argv(2);
if (Q_stricmp(mapname, "next") == 0)
index = Kots_Maplist_GetNextMap();
else
index = GetMaplistIndex(mapname);
if(index >= 0)
{
if(index == maplist.currentmap)
{
gi.cprintf(ent, PRINT_HIGH, "Cannot vote for current map!\n");
return;
}
else
{
//determine if it was recently played
int prev_index = GetPreviousMapIndex(mapname);
//don't allow voting for maps that were recently played
if (prev_index >= 0)
{
gi.cprintf(ent, PRINT_HIGH, "This map was recently played and cannot be voted again for a while.\n");
return;
}
//check if this map hasn't failed a vote too many times
if (!CanVoteMap(index))
{
gi.cprintf(ent, PRINT_HIGH, "Voting has failed too many times for this map.\n");
return;
}
}
level.mapvote = index;
level.mapvotestarted = level.time;
gi.bprintf(PRINT_MEDIUM, "%s started a map vote for %s. Type 'vote yes' or 'vote no' to vote.\n", ent->client->pers.netname, mapname);
gi.sound (ent, CHAN_VOICE, gi.soundindex("world/x_light.wav"), 1, ATTN_NONE, 0); // play a sound
Kots_Maplist_Vote(ent, MAPVOTE_YES); // this guy votes yes
}
else
gi.cprintf(ent, PRINT_HIGH, "Could not find map '%s'. Type 'maplist' to see all map names.\n", mapname);
}
void Kots_Maplist_ClearVoteInfo()
{
//clear the voting information
level.mapvote = -1;
level.mapvotestarted = 0.0;
level.passed = false;
//remove all the failed vote attempts
Array_DeleteAll(failed_votes);
}
void Kots_Maplist_PrintVoteInfo(edict_t *ent)
{
static char *vote_names[] = {"Not voted", "Yes", "No"};
Kots_SlowPrint(ent, "Map Vote Info:\n");
if (!level.mapvotestarted)
{
Kots_SlowPrint(ent, "No vote in progress.\n");
}
else
{
edict_t *player = NULL;
int i;
Kots_SlowPrint(ent, "Voting for map: %s\n", Kots_Maplist_GetName(level.mapvote));
Kots_SlowPrint(ent, "Time remaining: %2.2f second(s)\n", (level.mapvotestarted + KOTS_MAPVOTE_TIMEOUT) - level.time);
Kots_SlowPrint(ent, "\nPlayer Votes:\n");
Kots_SlowPrint(ent, "%-20s %-10s\n", "Name", "Vote");
player = g_edicts + 1;
for (i = 0; i < game.maxclients; i++, player++)
{
if (player->inuse && player->character && player->character->is_loggedin)
{
Kots_SlowPrint(ent, "%-20s %-10s\n", player->client->pers.netname, vote_names[player->client->pers.kots_persist.map_vote]);
}
}
}
}