[ create a new paste ] login | about

Link: http://codepad.org/aVV5ZIHJ    [ raw code | fork ]

hurracane - C, pasted on Aug 15:
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//IMPORTANT NOTE:

//Call once at the start of the level
init_level_variables() 
{
	game["menu_castvote"] = "callvote_castmenu";
	game["menu_callvote"] = "callvote_mainmenu";
	precacheMenu(game["menu_castvote"]);
	precacheMenu(game["menu_callvote"]);
	level.cv_spechud_votestring = &"^3VOTE (";
	level.cv_spechud_voteyesstring = &"^3YES:";
	level.cv_spechud_votenostring = &"^3, NO: ";
	level.cv_votes_localizedstrings = [];
	level.cv_votes_localizedstrings["restartMap"] = &"^3): Restart map";
	level.cv_votes_localizedstrings["nextMap"] = &"^3): Next map";
	level.cv_votes_localizedstrings["changeMap"] = &"^3): Change map";
	level.cv_votes_localizedstrings["changeTypeMap"] = &"^3): Change gametype & map";
	level.cv_votes_localizedstrings["changeGametype"] = &"^3): Change Gametype";
	
	precacheString(level.cv_spechud_votestring);
	precacheString(level.cv_spechud_voteyesstring);
	precacheString(level.cv_spechud_votenostring);
	precacheString(level.cv_votes_localizedstrings["restartMap"]);
	precacheString(level.cv_votes_localizedstrings["nextMap"]);
	precacheString(level.cv_votes_localizedstrings["changeMap"]);
	precacheString(level.cv_votes_localizedstrings["changeTypeMap"]);
	precacheString(level.cv_votes_localizedstrings["changeGametype"]);
	
	setCvar("cv_inProgress", "0");
	//Prefix all vote-related level variables with cv (short for callvote)
	level.cv_amnt_of_slots = 6;
	level.cv_gametypes = [];
	level.cv_vote_inprogress = false;
	level.cv_vote_updatetime = 1;
	level.cv_allowVoteChanges = false;
	level.cv_vote_cvar = "cv_votestring";
	level.cv_intermediate_results_cvar = "cv_scorestring";
	level.cv_optional_third_cvar = "cg_optionalstring";
	level.cv_filter_cvar = "callvote_filter";
	level.cv_time_between_votes = 80;
	//Build the main list of all gametypes
	for(i=0;i<getCvarInt("cv_gametypes_amnt");i++)
	{
		type = getCvar("cv_gametype" + i);
		level.cv_gametypes[i] = makeGametypeNode(type, getCvar("cv_gametype" + i + "_name"), buildMapList(type, ""));
	}
}
//#######-----------------------------------------------------------------------#######//
//#######- Main thread. Handles all menuresponses handled by the voting system -#######//
//#######-----------------------------------------------------------------------#######//
//Called in Callback_Playerconnect in _ufacmds for every player.
init() 
{
	if(!isDefined(game["menu_castvote"]))
		return;
		
	//Initialize variables
	self.cv_list_of_gametypes = [];
	self.cv_list_of_maps = [];
	self.cv_selected_gametype = 0;
	self.cv_selected_map = 0;
	self.cv_filter_string = "";
	oldfilter = "";
	filter_string_max = 15;
	self.cv_apply_filter = false;
	current_page_gametypes = 0;
	current_page_maps = 0;
	current_gametype = getCvar("g_gametype");
	while(1)
	{
		//Deal with every possible menuresponse 
		self waittill("menuresponse", menu, response);
		//Add an exception for other menus (e.g. wm_quickmessage) so we don't block out vote_yes and vote_no responses
		if(menu != game["menu_callvote"] && menu != game["menu_castvote"] && response != "vote_yes" && response != "vote_no")
			continue;
		//Check if we're selecting a gametype. Doing it this way so we don't need a different case in the switch for every number
		if(maps\mp\_quicksearch::isSubStr("gametypes_select", response))
		{
			selection_slot = (int)response[16];
			response = "gametypes_select";
		}
		else if(maps\mp\_quicksearch::isSubStr("maplist_select", response))
		{
			selection_slot = (int)response[14];
			response = "maplist_select";
		}
		else if(maps\mp\_quicksearch::isSubStr("cv_kp_", response))
		{
			key = response[6];
			response = "keypress";
		}
		//Menu logic. Opening, closing, pages, filter, etc
		switch(response)
		{
			case "open":
				if(menu == game["menu_callvote"])
					clearSlots();
				break;
			//Close menu
			case "close":
				if(menu == game["menu_castvote"])
					self.cv_menuOpened = false;
				else if(menu == game["menu_callvote"])
					self.cv_apply_filter = false;
				break;
			//Yesvote
			case "vote_yes":
				self registerVote("yes");
				break;
			//Novote
			case "vote_no":
				self registerVote("no");
				break;
			
			case "change_vote":
				self.cv_voteCast = false;
				self setClientCvar("cv_allowchangevote", 0);
				self.cv_canVote[0] = true;
				self.cv_canVote[1] = "You can't vote (unknown reason)";
				self.cv_voteDecision = "maybe";
				break;
			case "start_filter":
				//If we're already applying a filter, clear it. This means we can click the filter-button again and it will clear every input
				if(self.cv_apply_filter)
				{
					//Clear the strings
					self setClientCvar(level.cv_filter_cvar, "");
					self.cv_filter_string = "";
					oldfilter = "";
					//Generate the original map- and gametype list
					self.cv_list_of_maps = getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype);
					current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
					self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;
				}
				//Else if we aren't filtering yet, allow a filter to be applied
				else
				{
					self.cv_apply_filter = true;
					self thread showVisualIndicator();
				}
				break;
			case "keypress":
				//If we didn't activate the filter mode, don't accept keypresses
				if(!self.cv_apply_filter)
					break;
				//Exceptions for special keys
				if(key == "0")
					key = "backspace";
				else if(key == "9")
					key = "_";

				//Build the new string
				if(key == "backspace")
				{
					//Double if, because else we might add "backspace" to our self.cv_filter_string in some special cases
					if(self.cv_filter_string.size > 0)
						self.cv_filter_string = removeLastChar(self.cv_filter_string);
				}
				//If we're not at the maximum length, add the character
				else if(self.cv_filter_string.size < filter_string_max)
					self.cv_filter_string = self.cv_filter_string + key;
				//Display the new string
				self setClientCvar(level.cv_filter_cvar, self.cv_filter_string);
				//Apply the filter. If we already had a filter applied and the oldfilter is a substring of the new filter, 
				//The filtered-out maps of the old filter will still be filtered out in the new filter, thus we can apply the filter on the already filtered list
				if(isDefined(oldfilter) && maps\mp\_quicksearch::isSubStr(oldfilter, self.cv_filter_string))
					self.cv_list_of_maps = filterMapList(self.cv_list_of_maps, self.cv_filter_string);
				else //Else, filter a newly generated list
					self.cv_list_of_maps = filterMapList(getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype), self.cv_filter_string);
				oldfilter = self.cv_filter_string;
				//Fill the slots with the new list of maps
				current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
				//Make sure we select the right slot
				self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;					
				break;
			case "open_changegametype":
				//Build a list of all gametypes, with the current gametype up front
				self.cv_list_of_gametypes = buildGametypeList(level.cg_gametypes, current_gametype);
				//Build a list of all maps
				self.cv_list_of_maps = getMapListFromGametype(self.cv_list_of_gametypes, current_gametype);
				//Display page 1 of list of gametypes
				self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, current_page_gametypes);
				//Display page 1 of list of maps
				self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, current_page_maps);
				//Select the first slots by default
				self.cv_selected_gametype = 0;
				self.cv_selected_map = 0;
				//Reset the filters
				self.cv_apply_filter = false;
				self.cv_filter_string = "";
				oldfilter = "";
				break;
			case "gametypes_prevpage":
				oldpage = current_page_gametypes;
				//fillGametypeSlots returns the number of the new page
				current_page_gametypes = self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, (current_page_gametypes - 1));
				if(oldpage > current_page_gametypes)
					self.cv_selected_gametype = self.cv_selected_gametype - level.cv_amnt_of_slots;
				break;
			case "gametypes_nextpage":
				oldpage = current_page_gametypes;
				current_page_gametypes = self fillGametypeSlots(self.cv_list_of_gametypes, level.cv_amnt_of_slots, (current_page_gametypes + 1));
				if(oldpage < current_page_gametypes)
					self.cv_selected_gametype = self.cv_selected_gametype + level.cv_amnt_of_slots;
				break;
			case "maplist_prevpage":
				oldpage = current_page_maps;
				current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, (current_page_maps - 1));
				if(oldpage > current_page_maps)
					self.cv_selected_map = self.cv_selected_map - level.cv_amnt_of_slots;
				break;
			case "maplist_nextpage":
				oldpage = current_page_maps;
				current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, (current_page_maps + 1));
				if(oldpage < current_page_maps)
					self.cv_selected_map = self.cv_selected_map + level.cv_amnt_of_slots;
				break;
			case "gametypes_select":
				self.cv_selected_gametype = (current_page_gametypes * level.cv_amnt_of_slots) + selection_slot;
				// Check if the player selected a valid gametype. If yes, change the map list. If no, empty the maplist
				if(self.cv_selected_gametype < self.cv_list_of_gametypes.size)
				{
					self.cv_list_of_maps = getGametypeMaplist(self.cv_list_of_gametypes, self.cv_selected_gametype);
					//Apply the current set filter on the map list
					if(self.cv_filter_string != "")
						self.cv_list_of_maps = filterMapList(self.cv_list_of_maps, self.cv_filter_string);
					//Fill the slots with the new maps
					current_page_maps = self fillMapSlots(self.cv_list_of_maps, level.cv_amnt_of_slots, 0);
					//Select the highlighted map
					self.cv_selected_map = self.cv_selected_map % level.cv_amnt_of_slots;
				}
				else // Player didn't select an actual gametype, i.e. an empty slot
				{
					self clearMapSlots();
					self.cv_selected_gametype = -1;
					self.cv_list_of_maps = [];
				}
				break;
			case "maplist_select":
				self.cv_selected_map = (current_page_maps * level.cv_amnt_of_slots) + selection_slot;
				break;
		}
		//Actual votes logic
		switch(response)
		{
			case "typemap":
				gametype = current_gametype;
				mapname = getCvar("mapname");
				//If the selected gametype is out of bounds (i.e. it was a blank slot), make sure we immediately know this is the case
				if(self.cv_selected_gametype >= self.cv_list_of_gametypes.size)
					self.cv_selected_gametype = -1;
				//If the selected slot is blank, make sure we know this is the case
				if(self.cv_selected_map >= self.cv_list_of_maps.size)
					self.cv_selected_map = -1;
				
				//If the played selected a valid gametype, get the name of the gametype
				if(self.cv_selected_gametype != -1)
					gametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
				//If the player selected a valid map, get the name of that map
				if(self.cv_selected_map != -1)
					mapname = self.cv_list_of_maps[self.cv_selected_map];
				
				//If he didn't select a valid gametype or the gametype isn't different from the current one, but he has a valid map, vote that map.
				if((self.cv_selected_gametype == -1 || gametype == current_gametype) && self.cv_selected_map != -1)
					self thread startVote("changeMap", "Change map: " + mapname, 30);
				//If the player selected a valid gametype that isn't the current one and an invalid map, vote only the gametype
				else if(self.cv_selected_gametype != -1 && self.cv_selected_gametype != current_gametype && self.cv_selected_map == -1)
					self thread startVote("changeGametype", "Gametype: " + gametype, 30);
				else if(self.cv_selected_gametype != -1 && self.cv_selected_map != -1)
					self thread startVote("changeTypeMap", "Gametype: " + gametype + ", map: " + mapname, 30);
				else self iprintlnbold("Sorry, I don't know what to do with your vote");
				break;
			case "restartmap":
				self thread startVote("restartMap", "Restart map", 30);
				break;
			case "nextmap":
				self thread startVote("nextMap", "Next map: " + getCvar("nextmapname"), 30);
				break;
		}
	}
}
// Useful when pressing backspace
removeLastChar(str)
{
	newstr = "";
	for(i=0;i<str.size;i++)
		if(i == str.size - 1)
			return newstr;
		else
			newstr += str[i];
}
showVisualIndicator()
{
	blink = true;
	while(isDefined(self) && self.cv_apply_filter)
	{
		wait .8;
		if(blink)
			self setClientCvar(level.cv_filter_cvar, self.cv_filter_string + "_");
		else
			self setClientCvar(level.cv_filter_cvar, self.cv_filter_string);
		blink = !blink;
	}
}
		
//#######-----------------------------------------------------------------------#######//
//#######------ Vote logic. Starting, stopping votes, counting votes, etc ------#######//
//#######--------------- Basically all logic when a vote is running-------------#######//
//#######-----------------------------------------------------------------------#######//

//Called with self, because certain parameters such as selected map (in case of map change) are saved in player variables
startVote(vote_type, vote_string, vote_time)
{
	// Every reason why a player wouldn't be able to start a vote goes here
	if(level.cv_vote_inprogress)
	{
		self iprintln("Vote is already in progress");
		return;
	}
	if(self.pers["team"] == "spectator")
	{
		self iprintln("Cannot start a vote as a spectator");
		return;
	}
	newtime = (getTime() - level.starttime) / 1000;
	if(isDefined(self.cv_lastvote_time) && newtime - self.cv_lastvote_time < level.cv_time_between_votes)
	{
		self iprintln("Can't start a vote. Please wait a little while.");
		return;
	}
	level.cv_vote_inprogress = true;
	setCvar("cv_inProgress", "1");
	
	//Enable the vote controls in the quickcommands menu
	players = getentarray("player","classname");
	for(i=0;i<players.size;i++)
		players[i] initVoteVariables();
		
	//Make sure the vote-starter's vote is already cast to "yes", although he would still be able to change it
	self setClientCvar("cv_voteControls","0");
	self.cv_voteCast = true;
	self.cv_voteDecision = "yes";
	self.cv_lastvote_time = newtime;

	//Monitor the keyboard for the shortcut to open a vote decision menu
	thread monitorKeyboardShortcut();
	
	//Start drawing the vote strings on the screen
	premature_end = false;
	while(vote_time > 0 && !premature_end)
	{
		//Count all votes
		vote_time = updateVoteTime(vote_time);
		countedVotes = countVotes();
		yes = getYesVotes(countedVotes);
		no = getNoVotes(countedVotes);
		maybe = getMaybeVotes(countedVotes);
		
		premature_end = checkEndCondition(yes, no, maybe);
		//For every player, print the current vote and vote results on the screen
		players = getentarray("player","classname");
		for(i=0;i<players.size;i++)
		{
			//Remove the spectator vote hud, just in case the player goes spec and joins the game again. We want to clean up the hud and replace it with the cvar
			players[i] removeSpectatorVoteHud();
			if(players[i].pers["team"] == "spectator")
			{
				if(vote_time == 29 || vote_time % 8 == 0 && vote_time != 0)
					self iprintln("^6Current vote: ^7" + vote_string);
				players[i] drawSpectatorVoteHud(level.cv_votes_localizedstrings[vote_type], vote_time, yes, no);
			}
			else
			{
				players[i] setClientCvar(level.cv_vote_cvar, "^3Vote (" + vote_time + "): " + vote_string);
				players[i] setClientCvar(level.cv_intermediate_results_cvar, "^3YES: " + yes + ", NO: " + no + ", Maybe: " + maybe);
			}
		}
		wait level.cv_vote_updatetime;
	}
	result = calculateResults(yes, no, maybe);
	self endVote(vote_type, result);
}
updateVoteTime(vote_time)
{
	//Make sure we don't go under zero
	if(vote_time <= level.cv_vote_updatetime)
		return 0;
	else
		return vote_time - level.cv_vote_updatetime;
}
checkEndCondition(yes, no, maybe)
{
	possible_votes = countPossibleVotes();
	if(yes > possible_votes / 2)
		return true;
	else
		return false;
}
//Calculate the result based on the amount of yes/no votes. Returns either true for a pass or false for a fail
calculateResults(yes, no, maybe)
{
	possible_votes = countPossibleVotes();
	if(yes > possible_votes / 2)
		return true;
	else
		return false;
}
//Count every player that can vote
countPossibleVotes()
{
	possible_votes = 0;
	players = getentarray("player","classname");
	for(i=0;i<players.size;i++)
		if(players[i].pers["team"] != "spectator")
			possible_votes++;
	return possible_votes;
}
//Called with self, the player who called the vote
endVote(vote_type, result)
{
	level.cv_vote_inprogress = false;
	setCvar("cv_inProgress", "0");
	
	//When a vote ends, clear the vote hud, calculate the result of the vote and end the vote
	players = getentarray("player","classname");
	for(i=0;i<players.size;i++)
		players[i] unloadVoteVariables();
		
	if(result)
		iprintln("Vote passed.");
	else 
	{
		iprintln("Vote failed.");
		return;
	}
		
	current_gametypestring = "gametype " + getCvar("g_gametype");
	waittime_before_exitlevel = 3;
	//Handling different actions for different votes. Add votes here.
	switch(vote_type)
	{
		case "restartMap":
			setCvar("sv_maprotation", current_gametypestring + " map " + getCvar("mapname"));
			wait waittime_before_exitlevel;
			exitLevel(false);
			break;
		case "nextMap":
			setCvar("sv_maprotation", current_gametypestring + " map " + getCvar("nextmapname"));
			wait waittime_before_exitlevel;
			exitLevel(false);
			break;
		case "changeMap":
			newMapname = self.cv_list_of_maps[self.cv_selected_map];
			setCvar("sv_maprotation", current_gametypestring + " map " + newMapname);
			wait waittime_before_exitlevel;
			exitLevel(false);
			break;
		case "changeGametype":
			newGametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
			setCvar("sv_maprotation", "gametype " + newGametype + " map " + getCvar("mapname"));
			wait waittime_before_exitlevel;
			exitLevel(false);
			break;
		case "changeTypeMap":
			newMapname = self.cv_list_of_maps[self.cv_selected_map];
			newGametype = getGametypeType(self.cv_list_of_gametypes, self.cv_selected_gametype);
			setCvar("sv_maprotation", "gametype " + newGametype + " map " + newMapname);
			wait waittime_before_exitlevel;
			exitLevel(false);
			break;
	}
}
//Called from the menuresponse-thread in _ufacmds to register votes. Self is the player who sent the vote
RegisterVote(decision)
{
	//We need to make sure the new vote system is actually in use, also don't register votes if there is no vote in progress
	if(!isDefined(level.cv_vote_inprogress) || !level.cv_vote_inprogress)
		return;
	//We know a vote is in progress, so check if his vote is legit. If it is, change his decision
	if(self canVote())
	{
		self.cv_voteCast = true;
		self.cv_voteDecision = decision;
		if(getCvar("cv_allowchangevote") == "0")
			self setClientCvar("cv_showexitbutton","1");
	}
	else
		self iprintln(self.cv_canVote[1]);
}
//Make sure the appropriate menu is openend and make sure proper keyboard commands work
monitorKeyboardShortcut()
{
	while(level.cv_vote_inprogress)
	{
		players = getentarray("player","classname");
		for(i=0;i<players.size;i++)
		{
			if(players[i].pers["team"] != "spectator" && !isAlive(players[i]) && players[i] meleebuttonpressed() && players[i] usebuttonpressed() && !players[i].cv_menuOpened)
			{
				players[i] openMenu(game["menu_castvote"]);
				players[i].cv_menuOpened = true;
			}
			//The closing of the menu is handeled by the init thread that handles all menuresponses
		}
		wait .1;
	}
}
initVoteVariables()
{
	//Tell the menu files the player needs the vote controls (i.e. yes/no menus)
	self setClientCvar("cv_voteControls","1");
	//Replace viewmap menu with votemenu
	self setClientCvar("g_scriptQuickMap", game["menu_castvote"]);
	//Allow the player to change his vote once.
	self setClientCvar("cv_allowchangevote", "1");
	self setClientCvar("cv_showexitbutton","0");
	//Setup player variables so the player is allowed to vote
	self.cv_voteCast = false;
	self.cv_voteDecision = "maybe";
	self.cv_menuOpened = false;
	self.cv_canVote = [];
	self.cv_canVote[0] = true;
	self.cv_canVote[1] = "You can't vote (unknown reason)";
	self.cv_lastvote_time = (getTime() - level.starttime) / 1000;
}
unloadVoteVariables()
{
	//un-draw spectator hud if applicable
	self removeSpectatorVoteHud();
	//Remove voting entries from menus
	self setClientCvar("cv_voteControls","0");
	//Reset viewmap button
	self setClientCvar("g_scriptQuickMap", game["menu_viewmap"]);
	self setClientCvar(level.cv_vote_cvar, " ");
	self setClientCvar(level.cv_intermediate_results_cvar, " ");
	self setClientCvar(level.cv_optional_third_cvar, " ");
	//Set it so the player can cast a vote again. This shouldn't technically be here because it's in the init thread too
	self.cv_voteCast = false;
	self.cv_canVote = [];
	self.cv_canVote[0] = true;
	self.cv_canVote[1] = "You can't vote (unknown reason)";
}
//Count all votes by iterating over the players list and looking at the player.cv_voteDecision variable
countVotes()
{
	countedVotes = [];
	countedVotes[0] = 0;
	countedVotes[1] = 0;
	countedVotes[2] = 0;
	players = getentarray("player","classname");
	for(i=0;i<players.size;i++)
	{
		//Make sure our player is properly "initialized" when he wasn't around when the vote started
		if(!isDefined(players[i].cv_voteCast))
		{
			players[i] initVoteVariables();
			countedVotes[2]++;
			continue;
		}
		//Try to invalidate the vote to disallow cheating
		if(players[i].pers["team"] == "spectator" && players[i].cv_voteCast && players[i].cv_voteDecision != "maybe")
		{
			players[i] iprintln("Your vote has been invalidated.");
			players[i].cv_voteDecision = "maybe";
		}
		else if(players[i].cv_voteCast && players[i].cv_voteDecision == "yes")
			countedVotes[0]++;
		else if(players[i].cv_voteCast && players[i].cv_voteDecision == "no")
			countedVotes[1]++;
		else countedVotes[2]++;
	}
	return countedVotes;			
}
canVote()
{
	if(!isDefined(self.cv_voteCast))
		self.cv_voteCast = false;
	if(isDefined(self.cv_canVote) && !self.cv_canVote[0])
		return true;
		
	//Self is a player. We don't want spectators voting
	if(self.pers["team"] != "spectator" && !self.cv_voteCast)
	{
		//If cv_voteCast isn't defined or it's false, the player can clearly vote
		if(!self.cv_voteCast)
			return true;
		//If we allow votechanges, the player can also vote
		else if(level.cv_allowVoteChanges)
			return true;
	}
	//If we didn't return true up until now, it's probably false. So try to set a reason.
	//You can't vote... "because ..."
	self.cv_canVote[0] = false;
	if(self.pers["team"] == "spectator")
		self.cv_canVote[1] = "You cannot vote as a spectator";
	else if(self.cv_voteCast)
		self.cv_canVote[1] = "Vote already cast.";
	else self.cv_canVote[1] = "You can't vote (no reason in particular)";
	return false;
}
getYesVotes(countedVotes)
{
	return countedVotes[0];
}
getNoVotes(countedVotes)
{
	return countedVotes[1];
}
getMaybeVotes(countedVotes)
{
	return countedVotes[2];
}

//#######-----------------------------------------------------------------------#######//
//#######---------------------- Draw the hud for spectators --------------------#######//
//#######-----------------------------------------------------------------------#######//
drawSpectatorVoteHud(votestring, time, yes, no)
{
	vote_string_height = 196;
	vote_result_height = 205;
	// Draw vote string
	self.cv_votestring = newHudElem();
	self.cv_votestring.x = 8;
	self.cv_votestring.y = vote_string_height;
	self.cv_votestring.alignX = "left";
	self.cv_votestring.alignY = "bottom";
	self.cv_votestring.alpha = 1;
	self.cv_votestring.fontScale = .8;
	self.cv_votestring setText(level.cv_spechud_votestring);
	
	time_x = 41;
	//Draw the time
	self.cv_vote_timer = newHudElem();
	self.cv_vote_timer.x = time_x;
	self.cv_vote_timer.y = vote_string_height;
	self.cv_vote_timer.alignX = "left";
	self.cv_vote_timer.alignY = "bottom";
	self.cv_vote_timer.alpha = 1;
	self.cv_vote_timer.fontScale = .8;
	self.cv_vote_timer.color = (1,1,0); // 1 1 0
	self.cv_vote_timer setValue(time);
	
	if(time >= 10 && time % 2 == 0) // if it's an even number it'll be 0
		vote_type_x = time_x + 12;
	else if(time >= 10 && time % 2 != 0)
		vote_type_x = time_x + 11;
	else if(time < 10 && time % 2 == 0)
		vote_type_x = time_x + 7;
	else if(time < 10 && time % 2 != 0)
		vote_type_x = time_x + 6;
	
	self.cv_vote_action_string = newHudElem();
	self.cv_vote_action_string.x = vote_type_x;
	self.cv_vote_action_string.y = vote_string_height;
	self.cv_vote_action_string.alignX = "left";
	self.cv_vote_action_string.alignY = "bottom";
	self.cv_vote_action_string.alpha = 1;
	self.cv_vote_action_string.fontScale = .8;
	self.cv_vote_action_string setText(votestring);
	
	//Draw yesvotes
	yes_x = 8;
	self.cv_yesvotes = newHudElem();
	self.cv_yesvotes.x = yes_x;
	self.cv_yesvotes.y = vote_result_height;
	self.cv_yesvotes.alignX = "left";
	self.cv_yesvotes.alignY = "bottom";
	self.cv_yesvotes.alpha = 1;
	self.cv_yesvotes.fontScale = .8;
	self.cv_yesvotes setText(level.cv_spechud_voteyesstring);
		
	self.cv_yesvotes_value = newHudElem();
	self.cv_yesvotes_value.x = yes_x + 23;
	self.cv_yesvotes_value.y = vote_result_height;
	self.cv_yesvotes_value.alignX = "left";
	self.cv_yesvotes_value.alignY = "bottom";
	self.cv_yesvotes_value.alpha = 1;
	self.cv_yesvotes_value.fontScale = .8;
	self.cv_yesvotes_value.color = (1,1,0);
	self.cv_yesvotes_value setValue(yes);

	if(yes < 10)
		no_x = yes_x + 30;
	else
		no_x = yes_x + 34;
	
	//Draw novotes
	self.cv_novotes = newHudElem();
	self.cv_novotes.x = no_x;
	self.cv_novotes.y = vote_result_height;
	self.cv_novotes.alignX = "left";
	self.cv_novotes.alignY = "bottom";
	self.cv_novotes.alpha = 1;
	self.cv_novotes.fontScale = .8;
	self.cv_novotes setText(level.cv_spechud_votenostring);
		
	self.cv_novotes_value = newHudElem();
	self.cv_novotes_value.x = no_x + 23;
	self.cv_novotes_value.y = vote_result_height;
	self.cv_novotes_value.alignX = "left";
	self.cv_novotes_value.alignY = "bottom";
	self.cv_novotes_value.alpha = 1;
	self.cv_novotes_value.fontScale = .8;
	self.cv_novotes_value.color = (1,1,0);
	self.cv_novotes_value setValue(no);
}
removeSpectatorVoteHud()
{
	//If one of the huds is defined, they must all be
	if(isDefined(self.cv_yesvotes))
	{
		self.cv_votestring destroy();
		self.cv_vote_timer destroy();
		self.cv_vote_action_string destroy();
		self.cv_novotes destroy();
		self.cv_novotes_value destroy();
		self.cv_yesvotes destroy();
		self.cv_yesvotes_value destroy();
	}
}
//#######-----------------------------------------------------------------------#######//
//#######-------- Procedures for handling lists, displaying pages, etc ---------#######//
//#######-----------------------------------------------------------------------#######//

//Make an array that contains all necessary info about a certain gametype
makeGametypeNode(type, name, maplist) 
{
	gametype = [];
	gametype[0] = type;
	gametype[1] = name;
	gametype[2] = maplist;
	return gametype;
}
//Takes gametype array and index, returns gametype type (e.g. "dm")
getGametypeType(gametype_array, idx) 
{
	return gametype_array[idx][0];
}
//Takes gametype array and index, returns gametype name (e.g. "Deathmatch")
getGametypeName(gametype_array, idx) 
{
	return gametype_array[idx][1];
}
//Takes gametype array and index, returns maplist for this gametype
getGametypeMapList(gametype_array, idx)
{
	return gametype_array[idx][2];
}
//Takes gametype array and gametype-type (e.g. "dm"), returns maplist
getMapListFromGametype(gametype_array, gametype) 
{
	return getGametypeMapList(gametype_array, getGametypeNumber(gametype_array, gametype));
}
//Takes a gametype array and a gametype-type (e.g. "dm"), returns the index in this array for the given gametype
getGametypeNumber(gametype_array, gametype) 
{
	for(i=0;i<gametype_array.size;i++)
		if(getGametypeType(gametype_array, i) == gametype)
			return i;
	return "ERROR in getGametypeNumber, couldn't find number for gametype: " + gametype;
}
//Removes any info displayed in the slots that display the gametype and mapname
clearMapSlots() 
{
	for(i=0;i<level.cv_amnt_of_slots;i++) 
		self setClientCvar("vote_map_slot" + i, "");
}
clearGametypeSlots() 
{
	for(i=0;i<level.cv_amnt_of_slots;i++) 
		self setClientCvar("vote_gametype_slot" + i, "");
}
clearSlots()
{
	self clearMapSlots();
	self clearGametypeSlots();
}
//Takes a list of gametypes, an amount of slots per page and the page we wish to display. Returns the pagenumber displayed.
fillGametypeSlots(list_of_gametypes, amnt_per_page, page_nr) 
{
	//Get the correct page number. Will make sure page numbers aren't out of bounds and that we're always showing content (i.e. no blank pages)
	page_nr = getPageNumber(page_nr, list_of_gametypes.size, amnt_per_page);
	//Calculate where in the gametypes array our page starts and ends
	page_start = amnt_per_page * page_nr;
	page_end = (amnt_per_page * page_nr) + amnt_per_page;
	//Go from start to end and copy the content to the actual menu
	for(i=page_start;i<=page_end;i++) 
	{
		slot = i - page_start;
		//Fill with blank strings if we can't fill a complete page with content
		if(i >= list_of_gametypes.size)
			self setClientCvar("vote_gametype_slot" + slot, "");
		else 
			self setClientCvar("vote_gametype_slot" + slot, getGametypeName(list_of_gametypes, i));
	}
	return page_nr;
}
//Takes a list of maps, an amount of maps per page and a page number. Returns the pagenumber displayed
fillMapSlots(list_of_maps, amnt_per_page, page_nr) 
{	
	//Works exactly the same as the gametypes one. Read those comments.
	page_nr = getPageNumber(page_nr, list_of_maps.size, amnt_per_page);
	page_start = amnt_per_page * page_nr;
	page_end = (amnt_per_page * page_nr) + amnt_per_page;
	for(i=page_start;i<=page_end;i++) 
	{
		slot = i - page_start;
		if(i >= list_of_maps.size)
			self setClientCvar("vote_map_slot" + slot, "");
		else
			self setClientCvar("vote_map_slot" + slot, list_of_maps[i]);
	}
	return page_nr;
}
getPageNumber(page_nr, amnt_of_entries, amnt_per_page) 
{
	max_page = 0;
	if(page_nr < 0)
		return 0;
	for(i=0;i<=page_nr;i++) 
	{
		//Try to determine how many pages we can fill with actual content
		amnt_of_entries = amnt_of_entries - amnt_per_page;
		if(page_nr > max_page && amnt_of_entries > 0)
			max_page++;
		//If we can fill more pages than the one requested, return the one requested
		else if (page_nr <= max_page)
			break;
		//We do not have enough entries left to fill another page, so return the higher possible page number
		else
			page_nr = max_page;
	}
	return page_nr;
}
//Takes a certain gametype-type (e.g. "dm") and builds the list of all maps for this gametype that have the filter-variable as a substring
buildMapList(gametype, filter) 
{
	maplist = [];
	//Build an array of all valid maps. Valid means no empty entries.
	//Include the maprotation maps
	for(i=0;i<=getCvarInt("cv_" + gametype + "_map_last");i++)
	{
		mapname = getCvar("cv_" + gametype + "_map" + i);
		if(mapname != "")
			if(maps\mp\_quicksearch::isSubStr(filter, mapname)) 
				maplist[maplist.size] = mapname;
	}
	//Include extra maps that aren't shown in the maprotation but should be votable
	for(i=0;i<=getCvarInt("cv_" + gametype + "_emap_last");i++)
	{
		mapname = getCvar("cv_" + gametype + "_emap" + i);
		if(mapname != "")
			if(maps\mp\_quicksearch::isSubStr(filter, mapname))
				maplist[maplist.size] = mapname;
	}
	return maplist;
}
//Have this extra function to filter an already existing map list, rather than build a new list every time
//We do not have to check for empty mapnames because those would've been filtered out by buildMapList
FilterMapList(maplist, filter) 
{
	new_maplist = [];
	empty_idx = 0;
	//Iterate over the map list and build a new map list (array) with only those maps that have filter as a substring
	for(i=0;i<maplist.size;i++)
		if(maps\mp\_quicksearch::isSubStr(filter, maplist[i])) 
		{
			new_maplist[empty_idx] = maplist[i];
			empty_idx++;
		}
	return new_maplist;
}
//Takes a gametype array and a gametype that should be put upfront.
buildGametypeList(gametype_array, first_gametype) 
{
	gametypes = [];
	empty_idx = 1;
	//Iterate over the gametype list and build a new list starting at 1. If we find the current gametype anywhere in the iteration, put it up front.
	for(i=0;i<level.cv_gametypes.size;i++) 
	{
		//We want the current gametype up front
		if(getGametypeType(level.cv_gametypes, i) == first_gametype)
			idx = 0;
		else 
		{
			idx = empty_idx;
			empty_idx++;
		}
		gametypes[idx] = makeGametypeNode(getGametypeType(level.cv_gametypes, i), getGametypeName(level.cv_gametypes, i), getGametypeMapList(level.cv_gametypes, i));
	}
	return gametypes;
}


Create a new paste based on this one


Comments: