]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/vote.qc
Re-write vote information printing
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / vote.qc
1 // =============================================
2 //  Server side voting code, reworked by Samual
3 //  Last updated: December 14th, 2011
4 // =============================================
5
6 //  Nagger for players to know status of voting
7 float Nagger_SendEntity(entity to, float sendflags)
8 {
9         float nags, i, f, b;
10         entity e;
11         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
12
13         // bits:
14         //   1 = ready
15         //   2 = player needs to ready up
16         //   4 = vote
17         //   8 = player needs to vote
18         //  16 = warmup
19         // sendflags:
20         //  64 = vote counts
21         // 128 = vote string
22
23         nags = 0;
24         if(readycount)
25         {
26                 nags |= 1;
27                 if(to.ready == 0)
28                         nags |= 2;
29         }
30         if(votecalled)
31         {
32                 nags |= 4;
33                 if(to.vote_selection == 0)
34                         nags |= 8;
35         }
36         if(inWarmupStage)
37                 nags |= 16;
38
39         if(sendflags & 64)
40                 nags |= 64;
41
42         if(sendflags & 128)
43                 nags |= 128;
44
45         if(!(nags & 4)) // no vote called? send no string
46                 nags &~= (64 | 128);
47
48         WriteByte(MSG_ENTITY, nags);
49
50         if(nags & 64)
51         {
52                 WriteByte(MSG_ENTITY, vote_accept_count);
53                 WriteByte(MSG_ENTITY, vote_reject_count);
54                 WriteByte(MSG_ENTITY, vote_needed_overall);
55                 WriteChar(MSG_ENTITY, to.vote_selection);
56         }
57
58         if(nags & 128)
59                 WriteString(MSG_ENTITY, votecalledvote_display);
60
61         if(nags & 1)
62         {
63                 for(i = 1; i <= maxclients; i += 8)
64                 {
65                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
66                                 if(clienttype(e) != CLIENTTYPE_REAL || e.ready)
67                                         f |= b;
68                         WriteByte(MSG_ENTITY, f);
69                 }
70         }
71
72         return TRUE;
73 }
74
75 void Nagger_Init()
76 {
77         Net_LinkEntity(nagger = spawn(), FALSE, 0, Nagger_SendEntity);
78 }
79
80 void Nagger_VoteChanged()
81 {
82         if(nagger)
83                 nagger.SendFlags |= 128;
84 }
85
86 void Nagger_VoteCountChanged()
87 {
88         if(nagger)
89                 nagger.SendFlags |= 64;
90 }
91
92 void Nagger_ReadyCounted()
93 {
94         if(nagger)
95                 nagger.SendFlags |= 1;
96 }
97
98
99 // =======================
100 //  Game logic for voting
101 // =======================
102
103 void VoteReset() 
104 {
105         entity tmp_player;
106
107         FOR_EACH_CLIENT(tmp_player) { tmp_player.vote_selection = 0; }
108
109         if(votecalled)
110         {
111                 strunzone(votecalledvote);
112                 strunzone(votecalledvote_display);
113         }
114
115         votecalled = FALSE;
116         votecalledmaster = FALSE;
117         votefinished = 0;
118         votecalledvote = string_null;
119         votecalledvote_display = string_null;
120         
121         vote_parsed_command = string_null;
122         vote_parsed_display = string_null;
123
124         Nagger_VoteChanged();
125 }
126
127 void VoteStop(entity stopper) 
128 {
129         bprint("\{1}^2* ^3", VoteCommand_getname(stopper), "^2 stopped ^3", VoteCommand_getname(votecaller), "^2's vote\n");
130         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid))); }
131         
132         // Don't force them to wait for next vote, this way they can e.g. correct their vote.
133         if((votecaller) && (stopper == votecaller)) { votecaller.vote_next = time + autocvar_sv_vote_stop; }
134
135         VoteReset();
136 }
137
138 void VoteThink() 
139 {
140         if(votefinished > 0) // a vote was called
141         if(time > votefinished) // time is up
142         {
143                 VoteCount();
144         }
145         
146         return;
147 }
148
149 void VoteAccept() 
150 {
151         bprint("\{1}^2* ^3", VoteCommand_getname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
152         
153         if(votecalledmaster && votecaller)
154                 votecaller.vote_master = 1;
155         else
156                 localcmd(strcat(votecalledvote, "\n"));
157         
158         if(votecaller) { votecaller.vote_next = 0; } // people like your votes, you don't need to wait to vote again // todo separate anti-spam even for succeeded votes
159
160         VoteReset();
161         Announce("voteaccept");
162 }
163
164 void VoteReject() 
165 {
166         bprint("\{1}^2* ^3", VoteCommand_getname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
167         VoteReset();
168         Announce("votefail");
169 }
170
171 void VoteTimeout() 
172 {
173         bprint("\{1}^2* ^3", VoteCommand_getname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
174         VoteReset();
175         Announce("votefail");
176 }
177
178 void VoteSpam(float notvoters, float mincount, string result)
179 {
180         bprint(strcat(
181                 strcat("\{1}^2* vote results: ^1", ftos(vote_accept_count)),
182                 strcat("^2:^1", ftos(vote_reject_count)),
183                 ((mincount >= 0) ? strcat("^2 (^1", ftos(mincount), "^2 needed)") : "^2"),
184                 strcat(", ^1", ftos(vote_abstain_count), "^2 didn't care"),
185                 strcat(", ^1", ftos(notvoters), strcat("^2 didn't ", ((mincount >= 0) ? string_null : "have to "), "vote\n"))));
186         
187         if(autocvar_sv_eventlog)
188         {
189                 GameLogEcho(strcat(
190                         strcat(":vote:v", result, ":", ftos(vote_accept_count)),
191                         strcat(":", ftos(vote_reject_count)),
192                         strcat(":", ftos(vote_abstain_count)),
193                         strcat(":", ftos(notvoters)),
194                         strcat(":", ftos(mincount))));
195         }
196 }
197
198 void VoteCount() 
199 {
200         // declarations
201         vote_accept_count = vote_reject_count = vote_abstain_count = 0;
202         
203         float spectators_allowed = ((autocvar_sv_vote_nospectators != 2) 
204                                 || ((autocvar_sv_vote_nospectators == 1) && inWarmupStage) 
205                                 || (autocvar_sv_vote_nospectators == 0));
206                                 
207         float vote_player_count, is_player, notvoters;
208         float vote_real_player_count, vote_real_accept_count;
209         float vote_real_reject_count, vote_real_abstain_count;
210         float vote_needed_of_voted, final_needed_votes;
211         float vote_factor_overall, vote_factor_of_voted;
212         
213         entity tmp_player;
214
215         Nagger_VoteCountChanged();
216         
217         // add up all the votes from each connected client
218         FOR_EACH_REALCLIENT(tmp_player)
219         {
220                 is_player = (tmp_player.classname == "player");
221                 
222                 ++vote_player_count;
223                 if(is_player) { ++vote_real_player_count; }
224                 
225                 switch(tmp_player.vote_selection)
226                 {
227                         case VOTE_SELECT_REJECT: { ++vote_reject_count; { if(is_player) ++vote_real_reject_count; } break; }
228                         case VOTE_SELECT_ACCEPT: { ++vote_accept_count; { if(is_player) ++vote_real_reject_count; } break; }
229                         case VOTE_SELECT_ABSTAIN: { ++vote_abstain_count; { if(is_player) ++vote_real_abstain_count; } break; }
230                         default: break;
231                 }
232         }
233         
234         // Check to see if there are enough players on the server to allow master voting... otherwise, vote master could be used for evil.
235         if(votecalledmaster && autocvar_sv_vote_master_playerlimit > vote_player_count) 
236         {
237                 if(votecaller) { votecaller.vote_next = 0; }
238                 print_to(votecaller, "^1There are not enough players on this server to allow you to become vote master.");
239                 VoteReset();
240                 return;
241         }
242         
243         // 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. 
244         if(!spectators_allowed && (vote_real_player_count > 0))
245         {
246                 vote_accept_count = vote_real_accept_count;
247                 vote_reject_count = vote_real_reject_count;
248                 vote_abstain_count = vote_real_abstain_count;
249                 vote_player_count = vote_real_player_count;
250         }
251         
252         // people who have no opinion in any way :D
253         notvoters = (vote_player_count - vote_accept_count - vote_reject_count - vote_abstain_count);
254
255         // determine the goal for the vote to be passed or rejected normally
256         vote_factor_overall = bound(0.5, autocvar_sv_vote_majority_factor, 0.999);
257         vote_needed_overall = floor((vote_player_count - vote_abstain_count) * vote_factor_overall) + 1;
258         
259         // if the vote times out, determine the amount of votes needed of the people who actually already voted
260         vote_factor_of_voted = bound(0.5, autocvar_sv_vote_majority_factor_of_voted, 0.999);
261         vote_needed_of_voted = floor((vote_accept_count + vote_reject_count) * vote_factor_of_voted) + 1;
262         
263         
264         // finally calculate the result of the vote     
265         if(vote_accept_count >= vote_needed_overall)
266         {
267                 VoteSpam(notvoters, -1, "yes"); // there is enough acceptions to pass the vote
268                 VoteAccept();
269                 return;
270         }
271         
272         if(vote_reject_count > vote_player_count - vote_abstain_count - vote_needed_overall)
273         {
274                 VoteSpam(notvoters, -1, "no"); // there is enough rejections to deny the vote
275                 VoteReject();
276                 return;
277         }
278         
279         // there is not enough votes in either direction, now lets just calculate what the voters have said
280         if(time > votefinished)
281         {
282                 final_needed_votes = vote_needed_overall;
283                 
284                 if(autocvar_sv_vote_majority_factor_of_voted)
285                 {
286                         if(vote_accept_count >= vote_needed_of_voted)
287                         {
288                                 VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "yes");
289                                 VoteAccept();
290                                 return;
291                         }
292                         
293                         if(vote_accept_count + vote_reject_count > 0)
294                         {
295                                 VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "no");
296                                 VoteReject();
297                                 return;
298                         }
299                         
300                         final_needed_votes = min(vote_needed_overall, vote_needed_of_voted);
301                 }
302
303                 // it didn't pass or fail, so not enough votes to even make a decision. 
304                 VoteSpam(notvoters, final_needed_votes, "timeout");
305                 VoteTimeout();
306         }
307 }
308
309
310 // =======================
311 //  Game logic for warmup
312 // =======================
313
314 // Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown is set)
315 void ReadyRestart_think() 
316 {
317         restart_mapalreadyrestarted = 1;
318         reset_map(TRUE);
319         Score_ClearAll();
320         remove(self);
321         
322         return;
323 }
324
325 // Forces a restart of the game without actually reloading the map // this is a mess...
326 void ReadyRestart_force()
327 {
328         entity tmp_player, restart_timer;
329
330         bprint("^1Server is restarting...\n");
331
332         VoteReset();
333
334         // clear overtime, we have to decrease timelimit to its original value again.
335         if (checkrules_overtimesadded > 0 && g_race_qualifying != 2) { cvar_set("timelimit", ftos(autocvar_timelimit - (checkrules_overtimesadded * autocvar_timelimit_overtime))); }
336
337         checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;
338
339         readyrestart_happened = 1;
340         game_starttime = time;
341         if(!g_ca && !g_arena) { game_starttime += RESTART_COUNTDOWN; }
342                 
343         restart_mapalreadyrestarted = 0; // reset this var, needed when cvar sv_ready_restart_repeatable is in use
344
345         // disable the warmup global for the server
346         inWarmupStage = 0; // once the game is restarted the game is in match stage
347
348         // reset the .ready status of all players (also spectators)
349         FOR_EACH_CLIENTSLOT(tmp_player) { tmp_player.ready = 0; }
350         readycount = 0;
351         Nagger_ReadyCounted(); // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
352
353         // lock teams with lockonrestart
354         if(autocvar_teamplay_lockonrestart && teamplay) 
355         {
356                 lockteams = 1;
357                 bprint("^1The teams are now locked.\n");
358         }
359
360         //initiate the restart-countdown-announcer entity
361         if(autocvar_sv_ready_restart_after_countdown && !g_ca && !g_arena)
362         {
363                 restart_timer = spawn();
364                 restart_timer.think = ReadyRestart_think;
365                 restart_timer.nextthink = game_starttime;
366         }
367
368         // after a restart every players number of allowed timeouts gets reset, too
369         if(autocvar_sv_timeout) { FOR_EACH_REALPLAYER(tmp_player) { tmp_player.allowedTimeouts = autocvar_sv_timeout_number; } }
370
371         //reset map immediately if this cvar is not set
372         if not(autocvar_sv_ready_restart_after_countdown) { reset_map(TRUE); }
373
374         if(autocvar_sv_eventlog) { GameLogEcho(":restart"); }
375 }
376
377 void ReadyRestart()
378 {
379         // no arena, assault support yet...
380         if(g_arena | g_assault | gameover | intermission_running | race_completing)
381                 localcmd("restart\n");
382         else
383                 localcmd("\nsv_hook_gamerestart\n");
384
385         // Reset ALL scores, but only do that at the beginning of the countdown if sv_ready_restart_after_countdown is off!
386         // Otherwise scores could be manipulated during the countdown.
387         if not(autocvar_sv_ready_restart_after_countdown) { Score_ClearAll(); }
388
389         ReadyRestart_force();
390         
391         return;
392 }
393
394 // Count the players who are ready and determine whether or not to restart the match // todo: add percentage ready support
395 void ReadyCount()
396 {
397         entity tmp_player;
398         float ready_needed_factor, ready_needed_count;
399         float t_ready, t_players;
400
401         FOR_EACH_REALPLAYER(tmp_player)
402         {
403                 ++t_players;
404                 if(tmp_player.ready) { ++t_ready; }
405         }
406
407         readycount = t_ready;
408
409         Nagger_ReadyCounted();
410
411         ready_needed_factor = bound(0.5, cvar("g_warmup_majority_factor"), 0.999);
412         ready_needed_count = floor(t_players * ready_needed_factor) + 1;
413         
414         if(readycount >= ready_needed_count)
415         {
416                 ReadyRestart();
417         }
418                 
419         return;
420 }
421
422
423 // ======================================
424 //  Supporting functions for VoteCommand
425 // ======================================
426
427 float Votecommand_check_assignment(entity caller, float assignment)
428 {
429         float from_server = (!caller);
430         
431         if((assignment == VC_ASGNMNT_BOTH) 
432                 || ((!from_server && assignment == VC_ASGNMNT_CLIENTONLY) 
433                 || (from_server && assignment == VC_ASGNMNT_SERVERONLY)))
434         {
435                 return TRUE;
436         }
437
438         return FALSE;
439 }
440
441 string VoteCommand_getprefix(entity caller)
442 {
443         if(caller)
444                 return "cmd";
445         else
446                 return "sv_cmd";
447 }
448
449 string VoteCommand_getname(entity caller)
450 {
451         if(caller)
452                 return caller.netname;
453         else
454                 return ((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
455 }
456
457 string VoteCommand_extractcommand(string input, float startpos, float argc) 
458 {
459         string output;
460         
461         if((argc - 1) < startpos)
462                 output = "";
463         else
464                 output = substring(input, argv_start_index(startpos), argv_end_index(-1) - argv_start_index(startpos));
465                 
466         print("VoteCommand_parse: '", output, "'. \n");
467         return output;
468 }
469
470 float VoteCommand_checknasty(string vote_command)
471 {
472         if((strstrofs(vote_command, ";", 0) >= 0)
473                 || (strstrofs(vote_command, "\n", 0) >= 0)
474                 || (strstrofs(vote_command, "\r", 0) >= 0)
475                 || (strstrofs(vote_command, "$", 0) >= 0))
476                 return FALSE;
477                 
478         return TRUE;
479 }
480
481 float VoteCommand_checkinlist(string vote_command, string list)
482 {
483         string l = strcat(" ", list, " ");
484         
485         if(strstrofs(l, strcat(" ", vote_command, " "), 0) >= 0)
486                 return TRUE;
487         
488         // if gotomap is allowed, chmap is too, and vice versa
489         if(vote_command == "gotomap")
490                 if(strstrofs(l, " chmap ", 0) >= 0)
491                         return TRUE;
492                         
493         if(vote_command == "chmap")
494                 if(strstrofs(l, " gotomap ", 0) >= 0)
495                         return TRUE;
496         
497         return FALSE;
498 }
499
500 string ValidateMap(string validated_map, entity caller)
501 {
502         validated_map = MapInfo_FixName(validated_map);
503         
504         if(!validated_map)
505         {
506                 print_to(caller, "This map is not available on this server.");
507                 return string_null;
508         }
509         
510         if(!autocvar_sv_vote_override_mostrecent && caller)
511         {
512                 if(Map_IsRecent(validated_map))
513                 {
514                         print_to(caller, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
515                         return string_null;
516                 }
517         }
518         
519         if(!MapInfo_CheckMap(validated_map))
520         {
521                 print_to(caller, strcat("^1Invalid mapname, \"^3", validated_map, "^1\" does not support the current game mode."));
522                 return string_null;
523         }
524
525         return validated_map;
526 }
527
528 float VoteCommand_parse(entity caller, string vote_command, string vote_list, float startpos, float argc)
529 {
530         string first_command;
531         entity victim;
532         
533         first_command = argv(startpos);
534
535         if not(VoteCommand_checkinlist(vote_command, vote_list))
536                 return FALSE;
537
538         if((argc - 1) < startpos) // These commands won't work without arguments
539         {
540                 switch(first_command)
541                 {
542                         case "map":
543                         case "chmap":
544                         case "gotomap":
545                         case "kick":
546                         case "kickban":
547                                 return FALSE;
548                                 
549                         default: { break; }
550                 }
551         }
552         
553         switch(first_command) // now go through and parse the proper commands to adjust as needed.
554         {
555                 case "kick":
556                 case "kickban": // catch all kick/kickban commands
557                 {
558                         victim = edict_num(GetFilteredNumber(substring(vote_command, argv_start_index(startpos + 1), argv_end_index(-1) - argv_start_index(startpos + 1))));
559                         if not(victim) { return FALSE; }
560                         // TODO: figure out how kick/kickban/ban commands work and re-write this to fit around them
561                         vote_parsed_command = vote_command;
562                         vote_parsed_display = strcat("^1", vote_command, " (^7", victim.netname, "^1): ", "todo");
563                         
564                         break;
565                 }
566                 
567                 case "map":
568                 case "chmap":
569                 case "gotomap": // re-direct all map selection commands to gotomap
570                 {
571                         vote_command = ValidateMap(substring(vote_command, argv_start_index(startpos + 1), argv_end_index(-1) - argv_start_index(startpos + 1)), caller);
572                         if not(vote_command) { return FALSE; }
573                         vote_parsed_command = strcat("gotomap ", vote_command);
574                         vote_parsed_display = strzone(strcat("^1", vote_parsed_command));
575                         
576                         break;
577                 }
578                 
579                 default: 
580                 { 
581                         vote_parsed_command = vote_command;
582                         vote_parsed_display = strzone(strcat("^1", vote_command));
583                         
584                         break; 
585                 }
586         }
587
588         return TRUE;
589 }
590
591
592 // =======================
593 //  Command Sub-Functions
594 // =======================
595
596 void VoteCommand_abstain(float request, entity caller) // CLIENT ONLY
597 {
598         switch(request)
599         {
600                 case CMD_REQUEST_COMMAND:
601                 {
602                         if not(votecalled) { print_to(caller, "^1No vote called."); }
603                         else if not(caller.vote_selection == VOTE_SELECT_NULL || autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
604                         
605                         else // everything went okay, continue changing vote
606                         {
607                                 print_to(caller, "^1You abstained from your vote.");
608                                 caller.vote_selection = VOTE_SELECT_ABSTAIN;
609                                 msg_entity = caller;
610                                 if(!autocvar_sv_vote_singlecount) { VoteCount(); }
611                         }
612                         
613                         return;
614                 }
615                         
616                 default:
617                 case CMD_REQUEST_USAGE:
618                 {
619                         print("\nUsage:^3 vote abstain\n");
620                         print("  No arguments required.\n");
621                         return;
622                 }
623         }
624 }
625
626 void VoteCommand_call(float request, entity caller, float argc, string vote_command) // BOTH
627 {
628         switch(request)
629         {
630                 case CMD_REQUEST_COMMAND:
631                 {
632                         float spectators_allowed = ((autocvar_sv_vote_nospectators != 2) 
633                                 || ((autocvar_sv_vote_nospectators == 1) && inWarmupStage) 
634                                 || (autocvar_sv_vote_nospectators == 0));
635                                 
636                         float tmp_playercount;
637                         entity tmp_player;
638                         
639                         vote_command = VoteCommand_extractcommand(vote_command, 2, argc);
640                         
641                         if not(autocvar_sv_vote_call || !caller) { print_to(caller, "^1Vote calling is not allowed."); }
642                         else if(votecalled) { print_to(caller, "^1There is already a vote called."); }
643                         else if(!spectators_allowed && (caller && (caller.classname != "player"))) { print_to(caller, "^1Only players can call a vote."); }
644                         else if(timeoutStatus) { print_to(caller, "^1You can not call a vote while a timeout is active."); }
645                         else if(caller && (time < caller.vote_next)) { print_to(caller, strcat("^1You have to wait ^2", ftos(ceil(caller.vote_next - time)), "^1 seconds before you can again call a vote.")); }
646                         else if not(VoteCommand_checknasty(vote_command)) { print_to(caller, "^1Syntax error in command, see 'vhelp' for more info."); }
647                         else if not(VoteCommand_parse(caller, vote_command, autocvar_sv_vote_commands, 2, argc)) { print_to(caller, "^1This command is not acceptable, see 'vhelp' for more info."); }
648
649                         else // everything went okay, continue with calling the vote // TODO: fixes to make this more compatible with sv_cmd
650                         {
651                                 votecalled = TRUE;
652                                 votecalledmaster = FALSE;
653                                 votecalledvote = strzone(vote_parsed_command);
654                                 votecalledvote_display = strzone(vote_parsed_display);
655                                 votefinished = time + autocvar_sv_vote_timeout;
656                                 votecaller = caller; // remember who called the vote
657                                 
658                                 if(caller)
659                                 {
660                                         caller.vote_selection = VOTE_SELECT_ACCEPT;
661                                         caller.vote_next = time + autocvar_sv_vote_wait;
662                                         msg_entity = caller; // todo: what is this for?
663                                 }
664                                 
665                                 FOR_EACH_REALCLIENT(tmp_player) { ++tmp_playercount; }
666                                 if(tmp_playercount > 1) { Announce("votecall"); } // don't announce a "vote now" sound if player is alone
667                                 
668                                 bprint("\{1}^2* ^3", VoteCommand_getname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
669                                 if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display)); }
670                                 Nagger_VoteChanged();
671                                 VoteCount(); // needed if you are the only one
672                         }
673                         
674                         return;
675                 }
676                         
677                 default:
678                 case CMD_REQUEST_USAGE:
679                 {
680                         print("\nUsage:^3 vote call\n");
681                         print("  TODO.\n");
682                         return;
683                 }
684         }
685 }
686
687 void VoteCommand_master(float request, entity caller, float argc, string vote_command) // CLIENT ONLY
688 {
689         switch(request)
690         {
691                 case CMD_REQUEST_COMMAND:
692                 {
693                         if(autocvar_sv_vote_master)
694                         {
695                                 switch(strtolower(argv(2)))
696                                 {
697                                         case "do":
698                                         {
699                                                 vote_command = VoteCommand_extractcommand(vote_command, 3, argc);
700                                                 
701                                                 if not(caller.vote_master) { print_to(caller, "^1You do not have vote master privelages."); }
702                                                 else if not(VoteCommand_checknasty(vote_command)) { print_to(caller, "^1Syntax error in command, see 'vhelp' for more info."); }
703                                                 else if not(VoteCommand_parse(caller, vote_command, autocvar_sv_vote_master_commands, 3, argc)) { print_to(caller, "^1This command is not acceptable, see 'vhelp' for more info."); }
704                                                 
705                                                 else // everything went okay, proceed with command
706                                                 {
707                                                         localcmd(strcat(vote_parsed_command, "\n"));
708                                                         print_to(caller, strcat("Executing command '", vote_parsed_display, "' on server."));
709                                                         bprint("\{1}^2* ^3", VoteCommand_getname(caller), "^2 used their ^3master^2 status to do \"^2", vote_parsed_display, "^2\".\n");
710                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vdo:", ftos(caller.playerid), ":", vote_parsed_display)); }
711                                                 }
712                                                 
713                                                 return;
714                                         }
715                                         
716                                         case "login":
717                                         {
718                                                 if not(autocvar_sv_vote_master_password != "") { print_to(caller, "^1Login to vote master is not allowed."); }
719                                                 else if(caller.vote_master) { print_to(caller, "^1You are already logged in as vote master."); }
720                                                 else if not(autocvar_sv_vote_master_password == argv(3)) { print_to(caller, strcat("Rejected vote master login from ", VoteCommand_getname(caller))); }
721
722                                                 else // everything went okay, proceed with giving this player master privilages
723                                                 {
724                                                         caller.vote_master = TRUE;
725                                                         print_to(caller, strcat("Accepted vote master login from ", VoteCommand_getname(caller)));
726                                                         bprint("\{1}^2* ^3", VoteCommand_getname(caller), "^2 logged in as ^3master^2\n");
727                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vlogin:", ftos(caller.playerid))); }
728                                                 }
729                                                 
730                                                 return;
731                                         }
732                                         
733                                         default: // calling a vote for master
734                                         {
735                                                 if not(autocvar_sv_vote_master_callable) { print_to(caller, "^1Vote to become vote master is not allowed."); }
736                                                 else if(votecalled) { print_to(caller, "^1There is already a vote called."); }
737                                                 
738                                                 else // everything went okay, continue with creating vote
739                                                 {
740                                                         votecalled = TRUE;
741                                                         votecalledmaster = TRUE;
742                                                         votecalledvote = strzone("XXX");
743                                                         votecalledvote_display = strzone("^3master");
744                                                         votefinished = time + autocvar_sv_vote_timeout;
745                                                         votecaller = caller;
746                                                         
747                                                         caller.vote_selection = VOTE_SELECT_ACCEPT;
748                                                         caller.vote_next = time + autocvar_sv_vote_wait;
749                                                         
750                                                         bprint("\{1}^2* ^3", VoteCommand_getname(votecaller), "^2 calls a vote to become ^3master^2.\n");
751                                                         if(autocvar_sv_eventlog) { GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display)); }
752                                                         Nagger_VoteChanged();
753                                                         VoteCount(); // needed if you are the only one
754                                                 }
755                                                 
756                                                 return;
757                                         }
758                                 }
759                         }
760                         else { print_to(caller, "^1Master control of voting is not allowed."); }
761                         
762                         return;
763                 }
764                         
765                 default:
766                 case CMD_REQUEST_USAGE:
767                 {
768                         print("\nUsage:^3 vote master action [arguments]\n");
769                         print("  TODO.\n");
770                         return;
771                 }
772         }
773 }
774
775 void VoteCommand_no(float request, entity caller) // CLIENT ONLY
776 {
777         switch(request)
778         {
779                 case CMD_REQUEST_COMMAND:
780                 {
781                         if not(votecalled) { print_to(caller, "^1No vote called."); }
782                         else if not(caller.vote_selection == VOTE_SELECT_NULL || autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
783                         
784                         else // everything went okay, continue changing vote
785                         {
786                                 print_to(caller, "^1You rejected the vote.");
787                                 caller.vote_selection = VOTE_SELECT_REJECT;
788                                 msg_entity = caller;
789                                 if(!autocvar_sv_vote_singlecount) { VoteCount(); }
790                         }
791                         
792                         return;
793                 }
794                         
795                 default:
796                 case CMD_REQUEST_USAGE:
797                 {
798                         print("\nUsage:^3 vote no\n");
799                         print("  No arguments required.\n");
800                         return;
801                 }
802         }
803 }
804
805 void VoteCommand_status(float request, entity caller) // BOTH
806 {
807         switch(request)
808         {
809                 case CMD_REQUEST_COMMAND:
810                 {
811                         if(votecalled)
812                                 print_to(caller, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteCommand_getname(votecaller), "^7."));
813                         else
814                                 print_to(caller, "^1No vote called.");
815                                 
816                         return;
817                 }
818                         
819                 default:
820                 case CMD_REQUEST_USAGE:
821                 {
822                         print("\nUsage:^3 vote status\n");
823                         print("  No arguments required.\n");
824                         return;
825                 }
826         }
827 }
828
829 void VoteCommand_stop(float request, entity caller) // BOTH
830 {
831         switch(request)
832         {
833                 case CMD_REQUEST_COMMAND:
834                 {
835                         if not(votecalled) { print_to(caller, "^1No vote called."); }
836                         else if((caller == votecaller) || !caller || caller.vote_master) { VoteStop(caller); }
837                         else { print_to(caller, "^1You are not allowed to stop that vote."); }
838                         
839                         return;
840                 }
841                         
842                 default:
843                 case CMD_REQUEST_USAGE:
844                 {
845                         print("\nUsage:^3 vote stop\n");
846                         print("  No arguments required.\n");
847                         return;
848                 }
849         }
850 }
851
852 void VoteCommand_yes(float request, entity caller) // CLIENT ONLY
853 {
854         switch(request)
855         {
856                 case CMD_REQUEST_COMMAND:
857                 {
858                         if not(votecalled) { print_to(caller, "^1No vote called."); }
859                         if not(caller.vote_selection == VOTE_SELECT_NULL || autocvar_sv_vote_change) { print_to(caller, "^1You have already voted."); }
860                         
861                         else // everything went okay, continue changing vote
862                         {
863                                 print_to(caller, "^1You accepted the vote.");
864                                 caller.vote_selection = VOTE_SELECT_ACCEPT;
865                                 msg_entity = caller;
866                                 if(!autocvar_sv_vote_singlecount) { VoteCount(); }
867                         }
868                         
869                         return;
870                 }
871                         
872                 default:
873                 case CMD_REQUEST_USAGE:
874                 {
875                         print("\nUsage:^3 vote yes\n");
876                         print("  No arguments required.\n");
877                         return;
878                 }
879         }
880 }
881
882 /* use this when creating a new command, making sure to place it in alphabetical order.
883 void VoteCommand_(float request)
884 {
885         switch(request)
886         {
887                 case CMD_REQUEST_COMMAND:
888                 {
889                         
890                         return;
891                 }
892                         
893                 default:
894                 case CMD_REQUEST_USAGE:
895                 {
896                         print("\nUsage:^3 vote \n");
897                         print("  No arguments required.\n");
898                         return;
899                 }
900         }
901 }
902 */
903
904
905 // ================================
906 //  Macro system for vote commands
907 // ================================
908
909 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
910 #define VOTE_COMMANDS(request,caller,arguments,command) \
911         VOTE_COMMAND("abstain", VoteCommand_abstain(request, caller), "Abstain your vote in current vote", VC_ASGNMNT_CLIENTONLY) \
912         VOTE_COMMAND("call", VoteCommand_call(request, caller, arguments, command), "Create a new vote for players to decide on", VC_ASGNMNT_BOTH) \
913         VOTE_COMMAND("help", VoteCommand_macro_help(caller, arguments), "Shows this information", VC_ASGNMNT_BOTH) \
914         VOTE_COMMAND("master", VoteCommand_master(request, caller, arguments, command), "", VC_ASGNMNT_CLIENTONLY) \
915         VOTE_COMMAND("no", VoteCommand_no(request, caller), "Select no in current vote", VC_ASGNMNT_CLIENTONLY) \
916         VOTE_COMMAND("status", VoteCommand_status(request, caller), "Prints information about current vote", VC_ASGNMNT_BOTH) \
917         VOTE_COMMAND("stop", VoteCommand_stop(request, caller), "Immediately end a vote", VC_ASGNMNT_BOTH) \
918         VOTE_COMMAND("yes", VoteCommand_yes(request, caller), "Select yes in current vote", VC_ASGNMNT_CLIENTONLY) \
919         /* nothing */
920
921 void VoteCommand_macro_help(entity caller, float argc)
922 {
923         string command_origin = VoteCommand_getprefix(caller);
924         
925         if(argc == 2) // help display listing all commands
926         {
927                 print("\nUsage:^3 ", command_origin, " vote COMMAND...^7, where possible commands are:\n");
928                 
929                 #define VOTE_COMMAND(name,function,description,assignment) \
930                         { if(Votecommand_check_assignment(caller, assignment)) { print("  ^2", name, "^7: ", description, "\n"); } }
931                         
932                 VOTE_COMMANDS(0, caller, 0, "")
933                 #undef VOTE_COMMAND
934                 
935                 print("For help about specific commands, type ", command_origin, " vote help COMMAND\n");
936         }
937         else // usage for individual command
938         {
939                 #define VOTE_COMMAND(name,function,description,assignment) \
940                         { if(Votecommand_check_assignment(caller, assignment)) { if(name == strtolower(argv(2))) { function; return; } } }
941                         
942                 VOTE_COMMANDS(CMD_REQUEST_USAGE, caller, argc, "")
943                 #undef VOTE_COMMAND
944         }
945         
946         return;
947 }
948
949 float VoteCommand_macro_command(entity caller, float argc, string vote_command)
950 {
951         #define VOTE_COMMAND(name,function,description,assignment) \
952                 { if(Votecommand_check_assignment(caller, assignment)) { if(name == strtolower(argv(1))) { function; return TRUE; } } }
953                 
954         VOTE_COMMANDS(CMD_REQUEST_COMMAND, caller, argc, vote_command)
955         #undef VOTE_COMMAND
956         
957         return FALSE;
958 }
959
960
961 // ======================================
962 //  Main function handling vote commands
963 // ======================================
964
965 void VoteCommand(float request, entity caller, float argc, string vote_command) 
966 {
967         // Guide for working with argc arguments by example:
968         // argc:   1    - 2      - 3     - 4
969         // argv:   0    - 1      - 2     - 3 
970         // cmd     vote - master - login - password
971         
972         switch(request)
973         {
974                 case CMD_REQUEST_COMMAND:
975                 {
976                         if(VoteCommand_macro_command(caller, argc, vote_command))
977                                 return;
978                 }
979                         
980                 default:
981                         print_to(caller, strcat("Unknown vote command", ((argv(1) != "") ? strcat(" \"", argv(1), "\"") : ""), ". For a list of supported commands, try ", VoteCommand_getprefix(caller), " help.\n"));
982                 case CMD_REQUEST_USAGE:
983                 {
984                         VoteCommand_macro_help(caller, argc);
985                         return;
986                 }
987         }
988 }
989
990 // =======================
991 //  Game logic for voting
992 // =======================
993
994 void VoteHelp(entity e) {
995         string vmasterdis;
996         if(!autocvar_sv_vote_master) {
997                 vmasterdis = " ^1(disabled)";
998         }
999
1000         string vlogindis;
1001         if("" == autocvar_sv_vote_master_password) {
1002                 vlogindis = " ^1(disabled)";
1003         }
1004
1005         string vcalldis;
1006         if(!autocvar_sv_vote_call) {
1007                 vcalldis = " ^1(disabled)";
1008         }
1009
1010         print_to(e, "^7You can use voting with \"^2cmd vote help^7\" \"^2cmd vote status^7\" \"^2cmd vote call ^3COMMAND ARGUMENTS^7\" \"^2cmd vote stop^7\" \"^2cmd vote master^7\" \"^2cmd vote login^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\" \"^2cmd vote abstain^7\" \"^2cmd vote dontcare^7\".");
1011         print_to(e, "^7Or if your version is up to date you can use these aliases \"^2vhelp^7\" \"^2vstatus^7\" \"^2vcall ^3COMMAND ARGUMENTS^7\" \"^2vstop^7\" \"^2vmaster^7\" \"^2vlogin^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\" \"^2abstain^7\" \"^2vdontcare^7\".");
1012         print_to(e, "^7\"^2help^7\" shows this info.");
1013         print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
1014         print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
1015         print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
1016         print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
1017         print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
1018         print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
1019         print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
1020         print_to(e, "^7If enough of the players vote yes the vote is accepted.");
1021         print_to(e, "^7If enough of the players vote no the vote is rejected.");
1022         print_to(e, strcat("^7If neither the vote will timeout after ", ftos(autocvar_sv_vote_timeout), "^7 seconds."));
1023         print_to(e, "^7You can call a vote for or execute these commands:");
1024         print_to(e, strcat("^3", autocvar_sv_vote_commands, "^7 and maybe further ^3arguments^7"));
1025 }