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