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