Merge branch 'master' into Mario/qc_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / vote.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../../dpdefs/progsdefs.qh"
5     #include "../../dpdefs/dpextensions.qh"
6     #include "../../common/constants.qh"
7     #include "../../common/util.qh"
8     #include "../../common/command/shared_defs.qh"
9     #include "../autocvars.qh"
10     #include "../constants.qh"
11     #include "../defs.qh"
12     #include "../../common/notifications.qh"
13     #include "../mutators/mutators_include.qh"
14     #include "../../common/mapinfo.qh"
15     #include "common.qh"
16     #include "vote.qh"
17     #include "../../common/playerstats.qh"
18     #include "../scores.qh"
19     #include "../race.qh"
20     #include "../round_handler.qh"
21 #endif
22
23 // =============================================
24 //  Server side voting code, reworked by Samual
25 //  Last updated: December 27th, 2011
26 // =============================================
27
28 //  Nagger for players to know status of voting
29 float Nagger_SendEntity(entity to, float sendflags)
30 {
31         int nags, i, f, b;
32         entity e;
33         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
34
35         // bits:
36         //   1 = ready
37         //   2 = player needs to ready up
38         //   4 = vote
39         //   8 = player needs to vote
40         //  16 = warmup
41         // sendflags:
42         //  64 = vote counts
43         // 128 = vote string
44
45         nags = 0;
46         if(readycount)
47         {
48                 nags |= 1;
49                 if(to.ready == 0)
50                         nags |= 2;
51         }
52         if(vote_called)
53         {
54                 nags |= 4;
55                 if(to.vote_selection == 0)
56                         nags |= 8;
57         }
58         if(warmup_stage)
59                 nags |= 16;
60
61         if(sendflags & 64)
62                 nags |= 64;
63
64         if(sendflags & 128)
65                 nags |= 128;
66
67         if(!(nags & 4)) // no vote called? send no string
68                 nags &= ~(64 | 128);
69
70         WriteByte(MSG_ENTITY, nags);
71
72         if(nags & 64)
73         {
74                 WriteByte(MSG_ENTITY, vote_accept_count);
75                 WriteByte(MSG_ENTITY, vote_reject_count);
76                 WriteByte(MSG_ENTITY, vote_needed_overall);
77                 WriteChar(MSG_ENTITY, to.vote_selection);
78         }
79
80         if(nags & 128)
81                 WriteString(MSG_ENTITY, vote_called_display);
82
83         if(nags & 1)
84         {
85                 for(i = 1; i <= maxclients; i += 8)
86                 {
87                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
88                                 if(!IS_REAL_CLIENT(e) || e.ready)
89                                         f |= b;
90                         WriteByte(MSG_ENTITY, f);
91                 }
92         }
93
94         return true;
95 }
96
97 void Nagger_Init()
98 {
99         Net_LinkEntity(nagger = spawn(), false, 0, Nagger_SendEntity);
100 }
101
102 void Nagger_VoteChanged()
103 {
104         if(nagger)
105                 nagger.SendFlags |= 128;
106 }
107
108 void Nagger_VoteCountChanged()
109 {
110         if(nagger)
111                 nagger.SendFlags |= 64;
112 }
113
114 void Nagger_ReadyCounted()
115 {
116         if(nagger)
117                 nagger.SendFlags |= 1;
118 }
119
120
121 // =======================
122 //  Game logic for voting
123 // =======================
124
125 void VoteReset()
126 {
127         entity tmp_player;
128
129         FOR_EACH_CLIENT(tmp_player) { tmp_player.vote_selection = 0; }
130
131         if(vote_called)
132         {
133                 strunzone(vote_called_command);
134                 strunzone(vote_called_display);
135         }
136
137         vote_called = VOTE_NULL;
138         vote_caller = world;
139         vote_endtime = 0;
140
141         vote_called_command = string_null;
142         vote_called_display = string_null;
143
144         vote_parsed_command = string_null;
145         vote_parsed_display = string_null;
146
147         Nagger_VoteChanged();
148 }
149
150 void VoteStop(entity stopper)
151 {
152         bprint("\{1}^2* ^3", GetCallerName(stopper), "^2 stopped ^3", GetCallerName(vote_caller), "^2's vote\n");
153         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid))); }
154
155         // Don't force them to wait for next vote, this way they can e.g. correct their vote.
156         if((vote_caller) && (stopper == vote_caller)) { vote_caller.vote_waittime = time + autocvar_sv_vote_stop; }
157
158         VoteReset();
159 }
160
161 void VoteAccept()
162 {
163         bprint("\{1}^2* ^3", GetCallerName(vote_caller), "^2's vote for ^1", vote_called_display, "^2 was accepted\n");
164
165         if((vote_called == VOTE_MASTER) && vote_caller)
166                 vote_caller.vote_master = 1;
167         else
168                 localcmd(strcat(vote_called_command, "\n"));
169
170         if(vote_caller) { vote_caller.vote_waittime = 0; } // people like your votes, you don't need to wait to vote again
171
172         VoteReset();
173         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_VOTE_ACCEPT);
174 }
175
176 void VoteReject()
177 {
178         bprint("\{1}^2* ^3", GetCallerName(vote_caller), "^2's vote for ", vote_called_display, "^2 was rejected\n");
179         VoteReset();
180         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_VOTE_FAIL);
181 }
182
183 void VoteTimeout()
184 {
185         bprint("\{1}^2* ^3", GetCallerName(vote_caller), "^2's vote for ", vote_called_display, "^2 timed out\n");
186         VoteReset();
187         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_VOTE_FAIL);
188 }
189
190 void VoteSpam(float notvoters, float mincount, string result)
191 {
192         bprint(strcat(
193                 strcat("\{1}^2* vote results: ^1", ftos(vote_accept_count)),
194                 strcat("^2:^1", ftos(vote_reject_count)),
195                 ((mincount >= 0) ? strcat("^2 (^1", ftos(mincount), "^2 needed)") : "^2"),
196                 strcat(", ^1", ftos(vote_abstain_count), "^2 didn't care"),
197                 strcat(", ^1", ftos(notvoters), strcat("^2 didn't ", ((mincount >= 0) ? "" : "have to "), "vote\n"))));
198
199         if(autocvar_sv_eventlog)
200         {
201                 GameLogEcho(strcat(
202                         strcat(":vote:v", result, ":", ftos(vote_accept_count)),
203                         strcat(":", ftos(vote_reject_count)),
204                         strcat(":", ftos(vote_abstain_count)),
205                         strcat(":", ftos(notvoters)),
206                         strcat(":", ftos(mincount))));
207         }
208 }
209
210 void VoteCount(float first_count)
211 {
212         // declarations
213         vote_accept_count = vote_reject_count = vote_abstain_count = 0;
214
215         float spectators_allowed = ((autocvar_sv_vote_nospectators != 2)
216                                 || ((autocvar_sv_vote_nospectators == 1) && (warmup_stage || gameover))
217                                 || (autocvar_sv_vote_nospectators == 0));
218
219         float vote_player_count = 0, notvoters = 0;
220         float vote_real_player_count = 0, vote_real_accept_count = 0;
221         float vote_real_reject_count = 0, vote_real_abstain_count = 0;
222         float vote_needed_of_voted, final_needed_votes;
223         float vote_factor_overall, vote_factor_of_voted;
224
225         entity tmp_player;
226
227         Nagger_VoteCountChanged();
228
229         // add up all the votes from each connected client
230         FOR_EACH_REALCLIENT(tmp_player)
231         {
232                 ++vote_player_count;
233                 if(IS_PLAYER(tmp_player)) { ++vote_real_player_count; }
234
235                 switch(tmp_player.vote_selection)
236                 {
237                         case VOTE_SELECT_REJECT: { ++vote_reject_count; { if(IS_PLAYER(tmp_player)) ++vote_real_reject_count; } break; }
238                         case VOTE_SELECT_ACCEPT: { ++vote_accept_count; { if(IS_PLAYER(tmp_player)) ++vote_real_reject_count; } break; }
239                         case VOTE_SELECT_ABSTAIN: { ++vote_abstain_count; { if(IS_PLAYER(tmp_player)) ++vote_real_abstain_count; } break; }
240                         default: break;
241                 }
242         }
243
244         // Check to see if there are enough players on the server to allow master voting... otherwise, vote master could be used for evil.
245         if((vote_called == VOTE_MASTER) && autocvar_sv_vote_master_playerlimit > vote_player_count)
246         {
247                 if(vote_caller) { vote_caller.vote_waittime = 0; }
248                 print_to(vote_caller, "^1There are not enough players on this server to allow you to become vote master.");
249                 VoteReset();
250                 return;
251         }
252
253         // if spectators aren't allowed to vote and there are players in a match, then only count the players in the vote and ignore spectators.
254         if(!spectators_allowed && (vote_real_player_count > 0))
255         {
256                 vote_accept_count = vote_real_accept_count;
257                 vote_reject_count = vote_real_reject_count;
258                 vote_abstain_count = vote_real_abstain_count;
259                 vote_player_count = vote_real_player_count;
260         }
261
262         // people who have no opinion in any way :D
263         notvoters = (vote_player_count - vote_accept_count - vote_reject_count - vote_abstain_count);
264
265         // determine the goal for the vote to be passed or rejected normally
266         vote_factor_overall = bound(0.5, autocvar_sv_vote_majority_factor, 0.999);
267         vote_needed_overall = floor((vote_player_count - vote_abstain_count) * vote_factor_overall) + 1;
268
269         // if the vote times out, determine the amount of votes needed of the people who actually already voted
270         vote_factor_of_voted = bound(0.5, autocvar_sv_vote_majority_factor_of_voted, 0.999);
271         vote_needed_of_voted = floor((vote_accept_count + vote_reject_count) * vote_factor_of_voted) + 1;
272
273         // are there any players at all on the server? it could be an admin vote
274         if(vote_player_count == 0 && first_count)
275         {
276                 VoteSpam(0, -1, "yes"); // no players at all, just accept it
277                 VoteAccept();
278                 return;
279         }
280
281         // since there ARE players, finally calculate the result of the vote
282         if(vote_accept_count >= vote_needed_overall)
283         {
284                 VoteSpam(notvoters, -1, "yes"); // there is enough acceptions to pass the vote
285                 VoteAccept();
286                 return;
287         }
288
289         if(vote_reject_count > vote_player_count - vote_abstain_count - vote_needed_overall)
290         {
291                 VoteSpam(notvoters, -1, "no"); // there is enough rejections to deny the vote
292                 VoteReject();
293                 return;
294         }
295
296         // there is not enough votes in either direction, now lets just calculate what the voters have said
297         if(time > vote_endtime)
298         {
299                 final_needed_votes = vote_needed_overall;
300
301                 if(autocvar_sv_vote_majority_factor_of_voted)
302                 {
303                         if(vote_accept_count >= vote_needed_of_voted)
304                         {
305                                 VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "yes");
306                                 VoteAccept();
307                                 return;
308                         }
309
310                         if(vote_accept_count + vote_reject_count > 0)
311                         {
312                                 VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "no");
313                                 VoteReject();
314                                 return;
315                         }
316
317                         final_needed_votes = min(vote_needed_overall, vote_needed_of_voted);
318                 }
319
320                 // it didn't pass or fail, so not enough votes to even make a decision.
321                 VoteSpam(notvoters, final_needed_votes, "timeout");
322                 VoteTimeout();
323         }
324 }
325
326 void VoteThink()
327 {
328         if(vote_endtime > 0) // a vote was called
329         if(time > vote_endtime) // time is up
330         {
331                 VoteCount(false);
332         }
333
334         return;
335 }
336
337
338 // =======================
339 //  Game logic for warmup
340 // =======================
341
342 // Resets the state of all clients, items, weapons, waypoints, ... of the map.
343 void reset_map(float dorespawn)
344 {
345         entity oldself;
346         oldself = self;
347
348         if(time <= game_starttime && round_handler_IsActive())
349                 round_handler_Reset(game_starttime);
350
351         MUTATOR_CALLHOOK(reset_map_global);
352
353         for(self = world; (self = nextent(self)); )
354         if(IS_NOT_A_CLIENT(self))
355         {
356                 if(self.reset)
357                 {
358                         self.reset();
359                         continue;
360                 }
361
362                 if(self.team_saved)
363                         self.team = self.team_saved;
364
365                 if(self.flags & FL_PROJECTILE) // remove any projectiles left
366                         remove(self);
367         }
368
369         // Waypoints and assault start come LAST
370         for(self = world; (self = nextent(self)); )
371         if(IS_NOT_A_CLIENT(self))
372         {
373                 if(self.reset2)
374                 {
375                         self.reset2();
376                         continue;
377                 }
378         }
379
380         FOR_EACH_PLAYER(self)
381         if(self.frozen)
382                 Unfreeze(self);
383
384         // Moving the player reset code here since the player-reset depends
385         // on spawnpoint entities which have to be reset first --blub
386         if(dorespawn)
387         if(!MUTATOR_CALLHOOK(reset_map_players))
388         FOR_EACH_CLIENT(self) // reset all players
389         {
390                 /*
391                 only reset players if a restart countdown is active
392                 this can either be due to cvar sv_ready_restart_after_countdown having set
393                 restart_mapalreadyrestarted to 1 after the countdown ended or when
394                 sv_ready_restart_after_countdown is not used and countdown is still running
395                 */
396                 if (restart_mapalreadyrestarted || (time < game_starttime))
397                 {
398                         //NEW: changed behaviour so that it prevents that previous spectators/observers suddenly spawn as players
399                         if (IS_PLAYER(self)) {
400                                 //PlayerScore_Clear(self);
401                                 self.killcount = 0;
402                                 //stop the player from moving so that he stands still once he gets respawned
403                                 self.velocity = '0 0 0';
404                                 self.avelocity = '0 0 0';
405                                 self.movement = '0 0 0';
406                                 PutClientInServer();
407                         }
408                 }
409         }
410
411         if(g_keyhunt)
412                 kh_Controller_SetThink(autocvar_g_balance_keyhunt_delay_round + (game_starttime - time), kh_StartRound);
413
414         self = oldself;
415 }
416
417 // Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown is set)
418 void ReadyRestart_think()
419 {
420         restart_mapalreadyrestarted = 1;
421         reset_map(true);
422         Score_ClearAll();
423         remove(self);
424
425         return;
426 }
427
428 // Forces a restart of the game without actually reloading the map // this is a mess...
429 void ReadyRestart_force()
430 {
431         entity tmp_player, restart_timer;
432
433         bprint("^1Server is restarting...\n");
434
435         VoteReset();
436
437         // clear overtime, we have to decrease timelimit to its original value again.
438         if (checkrules_overtimesadded > 0 && g_race_qualifying != 2) { cvar_set("timelimit", ftos(autocvar_timelimit - (checkrules_overtimesadded * autocvar_timelimit_overtime))); }
439
440         checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;
441
442         readyrestart_happened = 1;
443         game_starttime = time + RESTART_COUNTDOWN;
444
445         // clear player attributes
446         FOR_EACH_CLIENT(tmp_player)
447         {
448                 tmp_player.alivetime = 0;
449                 tmp_player.killcount = 0;
450                 PS_GR_P_ADDVAL(tmp_player, PLAYERSTATS_ALIVETIME, -PS_GR_P_ADDVAL(tmp_player, PLAYERSTATS_ALIVETIME, 0));
451         }
452
453         restart_mapalreadyrestarted = 0; // reset this var, needed when cvar sv_ready_restart_repeatable is in use
454
455         // disable the warmup global for the server
456         warmup_stage = 0; // once the game is restarted the game is in match stage
457
458         // reset the .ready status of all players (also spectators)
459         FOR_EACH_REALCLIENT(tmp_player) { tmp_player.ready = 0; }
460         readycount = 0;
461         Nagger_ReadyCounted(); // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
462
463         // lock teams with lockonrestart
464         if(autocvar_teamplay_lockonrestart && teamplay)
465         {
466                 lockteams = 1;
467                 bprint("^1The teams are now locked.\n");
468         }
469
470         //initiate the restart-countdown-announcer entity
471         if(autocvar_sv_ready_restart_after_countdown)
472         {
473                 restart_timer = spawn();
474                 restart_timer.think = ReadyRestart_think;
475                 restart_timer.nextthink = game_starttime;
476         }
477
478         // after a restart every players number of allowed timeouts gets reset, too
479         if(autocvar_sv_timeout) { FOR_EACH_REALPLAYER(tmp_player) { tmp_player.allowed_timeouts = autocvar_sv_timeout_number; } }
480
481         //reset map immediately if this cvar is not set
482         if (!autocvar_sv_ready_restart_after_countdown) { reset_map(true); }
483
484         if(autocvar_sv_eventlog) { GameLogEcho(":restart"); }
485 }
486
487 void ReadyRestart()
488 {
489         // no arena, assault support yet...
490         if(g_assault | gameover | intermission_running | race_completing)
491                 localcmd("restart\n");
492         else
493                 localcmd("\nsv_hook_gamerestart\n");
494
495         // Reset ALL scores, but only do that at the beginning of the countdown if sv_ready_restart_after_countdown is off!
496         // Otherwise scores could be manipulated during the countdown.
497         if (!autocvar_sv_ready_restart_after_countdown) { Score_ClearAll(); }
498
499         ReadyRestart_force();
500
501         return;
502 }
503
504 // Count the players who are ready and determine whether or not to restart the match
505 void ReadyCount()
506 {
507         entity tmp_player;
508         float ready_needed_factor, ready_needed_count;
509         float t_ready = 0, t_players = 0;
510
511         FOR_EACH_REALCLIENT(tmp_player)
512         {
513                 if(IS_PLAYER(tmp_player) || tmp_player.caplayer == 1)
514                 {
515                         ++t_players;
516                         if(tmp_player.ready) { ++t_ready; }
517                 }
518         }
519
520         readycount = t_ready;
521
522         Nagger_ReadyCounted();
523
524         ready_needed_factor = bound(0.5, cvar("g_warmup_majority_factor"), 0.999);
525         ready_needed_count = floor(t_players * ready_needed_factor) + 1;
526
527         if(readycount >= ready_needed_count)
528         {
529                 ReadyRestart();
530         }
531
532         return;
533 }
534
535
536 // ======================================
537 //  Supporting functions for VoteCommand
538 // ======================================
539
540 float Votecommand_check_assignment(entity caller, float assignment)
541 {
542         float from_server = (!caller);
543
544         if((assignment == VC_ASGNMNT_BOTH)
545                 || ((!from_server && assignment == VC_ASGNMNT_CLIENTONLY)
546                 || (from_server && assignment == VC_ASGNMNT_SERVERONLY)))
547         {
548                 return true;
549         }
550
551         return false;
552 }
553
554 string VoteCommand_extractcommand(string input, float startpos, float argc)
555 {
556         string output;
557
558         if((argc - 1) < startpos)
559                 output = "";
560         else
561                 output = substring(input, argv_start_index(startpos), argv_end_index(-1) - argv_start_index(startpos));
562
563         return output;
564 }
565
566 float VoteCommand_checknasty(string vote_command)
567 {
568         if((strstrofs(vote_command, ";", 0) >= 0)
569                 || (strstrofs(vote_command, "\n", 0) >= 0)
570                 || (strstrofs(vote_command, "\r", 0) >= 0)
571                 || (strstrofs(vote_command, "$", 0) >= 0))
572                 return false;
573
574         return true;
575 }
576
577 float VoteCommand_checkinlist(string vote_command, string list)
578 {
579         string l = strcat(" ", list, " ");
580
581         if(strstrofs(l, strcat(" ", vote_command, " "), 0) >= 0)
582                 return true;
583
584         return false;
585 }
586
587 string ValidateMap(string validated_map, entity caller)
588 {
589         validated_map = MapInfo_FixName(validated_map);
590
591         if (!validated_map)
592         {
593                 print_to(caller, "This map is not available on this server.");
594                 return string_null;
595         }
596
597         if(!autocvar_sv_vote_override_mostrecent && caller)
598         {
599                 if(Map_IsRecent(validated_map))
600                 {
601                         print_to(caller, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
602                         return string_null;
603                 }
604         }
605
606         if(!MapInfo_CheckMap(validated_map))
607         {
608                 print_to(caller, strcat("^1Invalid mapname, \"^3", validated_map, "^1\" does not support the current game mode."));
609                 return string_null;
610         }
611
612         return validated_map;
613 }
614
615 float VoteCommand_checkargs(float startpos, float argc)
616 {
617         float p, q, check, minargs;
618         string cvarname = strcat("sv_vote_command_restriction_", argv(startpos));
619         string cmdrestriction = cvar_string(cvarname); // note: this warns on undefined cvar. We want that.
620         string charlist, arg;
621         float checkmate;
622
623         if(cmdrestriction == "")
624                 return true;
625
626         ++startpos; // skip command name
627
628         // check minimum arg count
629
630         // 0 args: argc == startpos
631         // 1 args: argc == startpos + 1
632         // ...
633
634         minargs = stof(cmdrestriction);
635         if(argc - startpos < minargs)
636                 return false;
637
638         p = strstrofs(cmdrestriction, ";", 0); // find first semicolon
639
640         for (;;)
641         {
642                 // we know that at any time, startpos <= argc - minargs
643                 // so this means: argc-minargs >= startpos >= argc, thus
644                 // argc-minargs >= argc, thus minargs <= 0, thus all minargs
645                 // have been seen already
646
647                 if(startpos >= argc) // all args checked? GOOD
648                         break;
649
650                 if(p < 0) // no more args? FAIL
651                 {
652                         // exception: exactly minargs left, this one included
653                         if(argc - startpos == minargs)
654                                 break;
655
656                         // otherwise fail
657                         return false;
658                 }
659
660                 // cut to next semicolon
661                 q = strstrofs(cmdrestriction, ";", p+1); // find next semicolon
662                 if(q < 0)
663                         charlist = substring(cmdrestriction, p+1, -1);
664                 else
665                         charlist = substring(cmdrestriction, p+1, q - (p+1));
666
667                 // in case we ever want to allow semicolons in VoteCommand_checknasty
668                 // charlist = strreplace("^^", ";", charlist);
669
670                 if(charlist != "")
671                 {
672                         // verify the arg only contains allowed chars
673                         arg = argv(startpos);
674                         checkmate = strlen(arg);
675                         for(check = 0; check < checkmate; ++check)
676                                 if(strstrofs(charlist, substring(arg, check, 1), 0) < 0)
677                                         return false; // not allowed character
678                         // all characters are allowed. FINE.
679                 }
680
681                 ++startpos;
682                 --minargs;
683                 p = q;
684         }
685
686         return true;
687 }
688
689 float VoteCommand_parse(entity caller, string vote_command, string vote_list, float startpos, float argc)
690 {
691         string first_command;
692
693         first_command = argv(startpos);
694
695         /*printf("VoteCommand_parse(): Command: '%s', Length: %f.\n",
696                 substring(vote_command, argv_start_index(startpos), strlen(vote_command) - argv_start_index(startpos)),
697                 strlen(substring(vote_command, argv_start_index(startpos), strlen(vote_command) - argv_start_index(startpos)))
698         );*/
699
700         if(
701                 (autocvar_sv_vote_limit > 0)
702                 &&
703                 (strlen(substring(vote_command, argv_start_index(startpos), strlen(vote_command) - argv_start_index(startpos))) > autocvar_sv_vote_limit)
704         )
705                 return false;
706
707         if (!VoteCommand_checkinlist(first_command, vote_list))
708                 return false;
709
710         if (!VoteCommand_checkargs(startpos, argc))
711                 return false;
712
713         switch(first_command) // now go through and parse the proper commands to adjust as needed.
714         {
715                 case "kick":
716                 case "kickban": // catch all kick/kickban commands
717                 {
718                         entity victim = GetIndexedEntity(argc, (startpos + 1));
719                         float accepted = VerifyClientEntity(victim, true, false);
720
721                         if(accepted > 0)
722                         {
723                                 string reason = ((argc > next_token) ? substring(vote_command, argv_start_index(next_token), strlen(vote_command) - argv_start_index(next_token)) : "No reason provided");
724                                 string command_arguments;
725
726                                 if(first_command == "kickban")
727                                         command_arguments = strcat(ftos(autocvar_g_ban_default_bantime), " ", ftos(autocvar_g_ban_default_masksize), " ~");
728                                 else
729                                         command_arguments = reason;
730
731                                 vote_parsed_command = strcat(first_command, " # ", ftos(num_for_edict(victim)), " ", command_arguments);
732                                 vote_parsed_display = strcat("^1", vote_command, " (^7", victim.netname, "^1): ", reason);
733                         }
734                         else { print_to(caller, strcat("vcall: ", GetClientErrorString(accepted, argv(startpos + 1)), ".\n")); return false; }
735
736                         break;
737                 }
738
739                 case "map":
740                 case "chmap":
741                 case "gotomap": // re-direct all map selection commands to gotomap
742                 {
743                         vote_command = ValidateMap(argv(startpos + 1), caller);
744                         if (!vote_command) { return false; }
745                         vote_parsed_command = strcat("gotomap ", vote_command);
746                         vote_parsed_display = strzone(strcat("^1", vote_parsed_command));
747
748                         break;
749                 }
750
751                 default:
752                 {
753                         vote_parsed_command = vote_command;
754                         vote_parsed_display = strzone(strcat("^1", vote_command));
755
756                         break;
757                 }
758         }
759
760         return true;
761 }
762
763
764 // =======================
765 //  Command Sub-Functions
766 // =======================
767
768 void VoteCommand_abstain(float request, entity caller) // CLIENT ONLY
769 {
770         switch(request)
771         {
772                 case CMD_REQUEST_COMMAND:
773                 {
774                         if (!vote_called) { print_to(caller, "^1No vote called."); }
775                         else if(caller.vote_selection != VOTE_SELECT_NULL && !autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
776
777                         else // everything went okay, continue changing vote
778                         {
779                                 print_to(caller, "^1You abstained from your vote.");
780                                 caller.vote_selection = VOTE_SELECT_ABSTAIN;
781                                 msg_entity = caller;
782                                 if(!autocvar_sv_vote_singlecount) { VoteCount(false); }
783                         }
784
785                         return;
786                 }
787
788                 default:
789                 case CMD_REQUEST_USAGE:
790                 {
791                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote abstain"));
792                         print_to(caller, "  No arguments required.");
793                         return;
794                 }
795         }
796 }
797
798 void VoteCommand_call(float request, entity caller, float argc, string vote_command) // BOTH
799 {
800         switch(request)
801         {
802                 case CMD_REQUEST_COMMAND:
803                 {
804                         float spectators_allowed = ((autocvar_sv_vote_nospectators != 2)
805                                 || ((autocvar_sv_vote_nospectators == 1) && warmup_stage)
806                                 || (autocvar_sv_vote_nospectators == 0));
807
808                         float tmp_playercount = 0;
809                         entity tmp_player;
810
811                         vote_command = VoteCommand_extractcommand(vote_command, 2, argc);
812
813                         if(!autocvar_sv_vote_call && caller) { print_to(caller, "^1Vote calling is not allowed."); }
814                         else if(!autocvar_sv_vote_gamestart && time < game_starttime) { print_to(caller, "^1Vote calling is not allowed before the match has started."); }
815                         else if(vote_called) { print_to(caller, "^1There is already a vote called."); }
816                         else if(!spectators_allowed && (caller && !IS_PLAYER(caller))) { print_to(caller, "^1Only players can call a vote."); }
817                         else if(caller && !IS_CLIENT(caller)) { print_to(caller, "^1Only connected clients can vote."); }
818                         else if(timeout_status) { print_to(caller, "^1You can not call a vote while a timeout is active."); }
819                         else if(caller && (time < caller.vote_waittime)) { print_to(caller, strcat("^1You have to wait ^2", ftos(ceil(caller.vote_waittime - time)), "^1 seconds before you can again call a vote.")); }
820                         else if (!VoteCommand_checknasty(vote_command)) { print_to(caller, "^1Syntax error in command, see 'vhelp' for more info."); }
821                         else if (!VoteCommand_parse(caller, vote_command, autocvar_sv_vote_commands, 2, argc)) { print_to(caller, "^1This command is not acceptable, see 'vhelp' for more info."); }
822
823                         else // everything went okay, continue with calling the vote
824                         {
825                                 vote_caller = caller; // remember who called the vote
826                                 vote_called = VOTE_NORMAL;
827                                 vote_called_command = strzone(vote_parsed_command);
828                                 vote_called_display = strzone(vote_parsed_display);
829                                 vote_endtime = time + autocvar_sv_vote_timeout;
830
831                                 if(caller)
832                                 {
833                                         caller.vote_selection = VOTE_SELECT_ACCEPT;
834                                         caller.vote_waittime = time + autocvar_sv_vote_wait;
835                                         msg_entity = caller;
836                                 }
837
838                                 FOR_EACH_REALCLIENT(tmp_player) { ++tmp_playercount; }
839                                 if(tmp_playercount > 1) { Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_VOTE_CALL); } // don't announce a "vote now" sound if player is alone
840
841                                 bprint("\{1}^2* ^3", GetCallerName(vote_caller), "^2 calls a vote for ", vote_called_display, "\n");
842                                 if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vcall:", ftos(vote_caller.playerid), ":", vote_called_display)); }
843                                 Nagger_VoteChanged();
844                                 VoteCount(true); // needed if you are the only one
845                         }
846
847                         return;
848                 }
849
850                 default:
851                 case CMD_REQUEST_USAGE:
852                 {
853                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote call command"));
854                         print_to(caller, "  Where 'command' is the command to request a vote upon.");
855                         print_to(caller, strcat("Examples: ", GetCommandPrefix(caller), " vote call gotomap dance"));
856                         print_to(caller, strcat("          ", GetCommandPrefix(caller), " vote call endmatch"));
857                         return;
858                 }
859         }
860 }
861
862 void VoteCommand_master(float request, entity caller, float argc, string vote_command) // CLIENT ONLY
863 {
864         switch(request)
865         {
866                 case CMD_REQUEST_COMMAND:
867                 {
868                         if(autocvar_sv_vote_master)
869                         {
870                                 switch(strtolower(argv(2)))
871                                 {
872                                         case "do":
873                                         {
874                                                 vote_command = VoteCommand_extractcommand(vote_command, 3, argc);
875
876                                                 if (!caller.vote_master) { print_to(caller, "^1You do not have vote master privelages."); }
877                                                 else if (!VoteCommand_checknasty(vote_command)) { print_to(caller, "^1Syntax error in command, see 'vhelp' for more info."); }
878                                                 else if (!VoteCommand_parse(caller, vote_command, strcat(autocvar_sv_vote_commands, " ", autocvar_sv_vote_master_commands), 3, argc)) { print_to(caller, "^1This command is not acceptable, see 'vhelp' for more info."); }
879
880                                                 else // everything went okay, proceed with command
881                                                 {
882                                                         localcmd(strcat(vote_parsed_command, "\n"));
883                                                         print_to(caller, strcat("Executing command '", vote_parsed_display, "' on server."));
884                                                         bprint("\{1}^2* ^3", GetCallerName(caller), "^2 used their ^3master^2 status to do \"^2", vote_parsed_display, "^2\".\n");
885                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vdo:", ftos(caller.playerid), ":", vote_parsed_display)); }
886                                                 }
887
888                                                 return;
889                                         }
890
891                                         case "login":
892                                         {
893                                                 if(autocvar_sv_vote_master_password == "") { print_to(caller, "^1Login to vote master is not allowed."); }
894                                                 else if(caller.vote_master) { print_to(caller, "^1You are already logged in as vote master."); }
895                                                 else if(autocvar_sv_vote_master_password != argv(3)) { print_to(caller, strcat("Rejected vote master login from ", GetCallerName(caller))); }
896
897                                                 else // everything went okay, proceed with giving this player master privilages
898                                                 {
899                                                         caller.vote_master = true;
900                                                         print_to(caller, strcat("Accepted vote master login from ", GetCallerName(caller)));
901                                                         bprint("\{1}^2* ^3", GetCallerName(caller), "^2 logged in as ^3master^2\n");
902                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vlogin:", ftos(caller.playerid))); }
903                                                 }
904
905                                                 return;
906                                         }
907
908                                         default: // calling a vote for master
909                                         {
910                                                 float spectators_allowed = ((autocvar_sv_vote_nospectators != 2)
911                                                         || ((autocvar_sv_vote_nospectators == 1) && warmup_stage)
912                                                         || (autocvar_sv_vote_nospectators == 0));
913
914                                                 if (!autocvar_sv_vote_master_callable) { print_to(caller, "^1Vote to become vote master is not allowed."); }
915                                                 else if(vote_called) { print_to(caller, "^1There is already a vote called."); }
916                                                 else if(!spectators_allowed && (caller && !IS_PLAYER(caller))) { print_to(caller, "^1Only players can call a vote."); }
917                                                 else if(timeout_status) { print_to(caller, "^1You can not call a vote while a timeout is active."); }
918
919                                                 else // everything went okay, continue with creating vote
920                                                 {
921                                                         vote_caller = caller;
922                                                         vote_called = VOTE_MASTER;
923                                                         vote_called_command = strzone("XXX");
924                                                         vote_called_display = strzone("^3master");
925                                                         vote_endtime = time + autocvar_sv_vote_timeout;
926
927                                                         caller.vote_selection = VOTE_SELECT_ACCEPT;
928                                                         caller.vote_waittime = time + autocvar_sv_vote_wait;
929
930                                                         bprint("\{1}^2* ^3", GetCallerName(vote_caller), "^2 calls a vote to become ^3master^2.\n");
931                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vcall:", ftos(vote_caller.playerid), ":", vote_called_display)); }
932                                                         Nagger_VoteChanged();
933                                                         VoteCount(true); // needed if you are the only one
934                                                 }
935
936                                                 return;
937                                         }
938                                 }
939                         }
940                         else { print_to(caller, "^1Master control of voting is not allowed."); }
941
942                         return;
943                 }
944
945                 default:
946                 case CMD_REQUEST_USAGE:
947                 {
948                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote master [action [command | password]]"));
949                         print_to(caller, "  If action is left blank, it calls a vote for you to become master.");
950                         print_to(caller, "  Otherwise the actions are either 'do' a command or 'login' as master.");
951                         return;
952                 }
953         }
954 }
955
956 void VoteCommand_no(float request, entity caller) // CLIENT ONLY
957 {
958         switch(request)
959         {
960                 case CMD_REQUEST_COMMAND:
961                 {
962                         if (!vote_called) { print_to(caller, "^1No vote called."); }
963                         else if(caller.vote_selection != VOTE_SELECT_NULL && !autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
964                         else if(((caller == vote_caller) || caller.vote_master) && autocvar_sv_vote_no_stops_vote) { VoteStop(caller); }
965
966                         else // everything went okay, continue changing vote
967                         {
968                                 print_to(caller, "^1You rejected the vote.");
969                                 caller.vote_selection = VOTE_SELECT_REJECT;
970                                 msg_entity = caller;
971                                 if(!autocvar_sv_vote_singlecount) { VoteCount(false); }
972                         }
973
974                         return;
975                 }
976
977                 default:
978                 case CMD_REQUEST_USAGE:
979                 {
980                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote no"));
981                         print_to(caller, "  No arguments required.");
982                         return;
983                 }
984         }
985 }
986
987 void VoteCommand_status(float request, entity caller) // BOTH
988 {
989         switch(request)
990         {
991                 case CMD_REQUEST_COMMAND:
992                 {
993                         if(vote_called)
994                                 print_to(caller, strcat("^7Vote for ", vote_called_display, "^7 called by ^7", GetCallerName(vote_caller), "^7."));
995                         else
996                                 print_to(caller, "^1No vote called.");
997
998                         return;
999                 }
1000
1001                 default:
1002                 case CMD_REQUEST_USAGE:
1003                 {
1004                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote status"));
1005                         print_to(caller, "  No arguments required.");
1006                         return;
1007                 }
1008         }
1009 }
1010
1011 void VoteCommand_stop(float request, entity caller) // BOTH
1012 {
1013         switch(request)
1014         {
1015                 case CMD_REQUEST_COMMAND:
1016                 {
1017                         if (!vote_called) { print_to(caller, "^1No vote called."); }
1018                         else if((caller == vote_caller) || !caller || caller.vote_master) { VoteStop(caller); }
1019                         else { print_to(caller, "^1You are not allowed to stop that vote."); }
1020
1021                         return;
1022                 }
1023
1024                 default:
1025                 case CMD_REQUEST_USAGE:
1026                 {
1027                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote stop"));
1028                         print_to(caller, "  No arguments required.");
1029                         return;
1030                 }
1031         }
1032 }
1033
1034 void VoteCommand_yes(float request, entity caller) // CLIENT ONLY
1035 {
1036         switch(request)
1037         {
1038                 case CMD_REQUEST_COMMAND:
1039                 {
1040                         if (!vote_called) { print_to(caller, "^1No vote called."); }
1041                         else if(caller.vote_selection != VOTE_SELECT_NULL && !autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
1042
1043                         else // everything went okay, continue changing vote
1044                         {
1045                                 print_to(caller, "^1You accepted the vote.");
1046                                 caller.vote_selection = VOTE_SELECT_ACCEPT;
1047                                 msg_entity = caller;
1048                                 if(!autocvar_sv_vote_singlecount) { VoteCount(false); }
1049                         }
1050
1051                         return;
1052                 }
1053
1054                 default:
1055                 case CMD_REQUEST_USAGE:
1056                 {
1057                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote yes"));
1058                         print_to(caller, "  No arguments required.");
1059                         return;
1060                 }
1061         }
1062 }
1063
1064 /* use this when creating a new command, making sure to place it in alphabetical order... also,
1065 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
1066 void VoteCommand_(float request)
1067 {
1068         switch(request)
1069         {
1070                 case CMD_REQUEST_COMMAND:
1071                 {
1072
1073                         return;
1074                 }
1075
1076                 default:
1077                 case CMD_REQUEST_USAGE:
1078                 {
1079                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " vote ");
1080                         print_to(caller, "  No arguments required.");
1081                         return;
1082                 }
1083         }
1084 }
1085 */
1086
1087
1088 // ================================
1089 //  Macro system for vote commands
1090 // ================================
1091
1092 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
1093 #define VOTE_COMMANDS(request,caller,arguments,command) \
1094         VOTE_COMMAND("abstain", VoteCommand_abstain(request, caller), "Abstain your vote in current vote", VC_ASGNMNT_CLIENTONLY) \
1095         VOTE_COMMAND("call", VoteCommand_call(request, caller, arguments, command), "Create a new vote for players to decide on", VC_ASGNMNT_BOTH) \
1096         VOTE_COMMAND("help", VoteCommand_macro_help(caller, arguments), "Shows this information", VC_ASGNMNT_BOTH) \
1097         VOTE_COMMAND("master", VoteCommand_master(request, caller, arguments, command), "Full control over all voting and vote commands", VC_ASGNMNT_CLIENTONLY) \
1098         VOTE_COMMAND("no", VoteCommand_no(request, caller), "Select no in current vote", VC_ASGNMNT_CLIENTONLY) \
1099         VOTE_COMMAND("status", VoteCommand_status(request, caller), "Prints information about current vote", VC_ASGNMNT_BOTH) \
1100         VOTE_COMMAND("stop", VoteCommand_stop(request, caller), "Immediately end a vote", VC_ASGNMNT_BOTH) \
1101         VOTE_COMMAND("yes", VoteCommand_yes(request, caller), "Select yes in current vote", VC_ASGNMNT_CLIENTONLY) \
1102         /* nothing */
1103
1104 void VoteCommand_macro_help(entity caller, float argc)
1105 {
1106         string command_origin = GetCommandPrefix(caller);
1107
1108         if(argc == 2 || argv(2) == "help") // help display listing all commands
1109         {
1110                 print_to(caller, "\nVoting commands:\n");
1111                 #define VOTE_COMMAND(name,function,description,assignment) \
1112                         { if(Votecommand_check_assignment(caller, assignment)) { print_to(caller, strcat("  ^2", name, "^7: ", description)); } }
1113
1114                 VOTE_COMMANDS(0, caller, 0, "");
1115                 #undef VOTE_COMMAND
1116
1117                 print_to(caller, strcat("\nUsage:^3 ", command_origin, " vote COMMAND...^7, where possible commands are listed above.\n"));
1118                 print_to(caller, strcat("For help about a specific command, type ", command_origin, " vote help COMMAND"));
1119                 print_to(caller, strcat("\n^7You can call a vote for or execute these commands: ^3", autocvar_sv_vote_commands, "^7 and maybe further ^3arguments^7"));
1120         }
1121         else // usage for individual command
1122         {
1123                 #define VOTE_COMMAND(name,function,description,assignment) \
1124                         { if(Votecommand_check_assignment(caller, assignment)) { if(name == strtolower(argv(2))) { function; return; } } }
1125
1126                 VOTE_COMMANDS(CMD_REQUEST_USAGE, caller, argc, "");
1127                 #undef VOTE_COMMAND
1128         }
1129
1130         return;
1131 }
1132
1133 float VoteCommand_macro_command(entity caller, float argc, string vote_command)
1134 {
1135         #define VOTE_COMMAND(name,function,description,assignment) \
1136                 { if(Votecommand_check_assignment(caller, assignment)) { if(name == strtolower(argv(1))) { function; return true; } } }
1137
1138         VOTE_COMMANDS(CMD_REQUEST_COMMAND, caller, argc, vote_command);
1139         #undef VOTE_COMMAND
1140
1141         return false;
1142 }
1143
1144
1145 // ======================================
1146 //  Main function handling vote commands
1147 // ======================================
1148
1149 void VoteCommand(float request, entity caller, float argc, string vote_command)
1150 {
1151         // Guide for working with argc arguments by example:
1152         // argc:   1    - 2      - 3     - 4
1153         // argv:   0    - 1      - 2     - 3
1154         // cmd     vote - master - login - password
1155
1156         switch(request)
1157         {
1158                 case CMD_REQUEST_COMMAND:
1159                 {
1160                         if(VoteCommand_macro_command(caller, argc, vote_command))
1161                                 return;
1162                 }
1163
1164                 default:
1165                         print_to(caller, strcat(((argv(1) != "") ? strcat("Unknown vote command \"", argv(1), "\"") : "No command provided"), ". For a list of supported commands, try ", GetCommandPrefix(caller), " vote help.\n"));
1166                 case CMD_REQUEST_USAGE:
1167                 {
1168                         VoteCommand_macro_help(caller, argc);
1169                         return;
1170                 }
1171         }
1172 }