]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
get rid of unnecessary zoning
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 string ValidateMap(string vote);
3 void(entity e) DropFlag;
4 string MapVote_Suggest(string m);
5
6 .float floodcontrol_chat;
7 .float floodcontrol_team;
8 void Say(entity source, float teamsay, string msgin)
9 {
10         string msgstr, colorstr, cmsgstr, namestr;
11         float flood;
12         entity head;
13
14         if(!teamsay)
15                 if(substring(msgin, 0, 1) == " ")
16                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
17
18         msgin = formatmessage(msgin);
19
20         if(msgin == "")
21                 return;
22
23         if(source.classname != "player")
24                 colorstr = "^0"; // black for spectators
25         else if(teams_matter)
26                 colorstr = Team_ColorCode(source.team);
27         else
28                 teamsay = FALSE;
29
30         if(intermission_running)
31                 teamsay = FALSE;
32
33         /*
34          * using bprint solves this... me stupid
35         // how can we prevent the message from appearing in a listen server?
36         // for now, just give "say" back and only handle say_team
37         if(!teamsay)
38         {
39                 clientcommand(self, strcat("say ", msgin));
40                 return;
41         }
42         */
43
44         if(cvar("g_chat_teamcolors"))
45                 namestr = playername(source);
46         else
47                 namestr = source.netname;
48         if(teamsay)
49         {
50                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
51                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
52         }
53         else
54                 msgstr = strzone(strcat("\{1}", namestr, "^7: ", msgin, "\n"));
55
56         // FLOOD CONTROL
57         flood = 0;
58         {
59                 float flood_spl;
60                 float flood_burst;
61                 float flood_lmax;
62                 var .float flood_field;
63                 float lines;
64                 if(teamsay)
65                 {
66                         flood_spl = cvar("g_chat_flood_spl_team");
67                         flood_burst = cvar("g_chat_flood_burst_team");
68                         flood_lmax = cvar("g_chat_flood_lmax_team");
69                         flood_field = floodcontrol_team;
70                 }
71                 else
72                 {
73                         flood_spl = cvar("g_chat_flood_spl");
74                         flood_burst = cvar("g_chat_flood_burst");
75                         flood_lmax = cvar("g_chat_flood_lmax");
76                         flood_field = floodcontrol_chat;
77                 }
78                 flood_burst = max(0, flood_burst - 1);
79                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
80                 lines = ceil(strlennocol(msgstr) / 75);
81                 if(flood_lmax && lines > flood_lmax)
82                         flood = 2;
83                 else if(time >= self.flood_field)
84                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
85                 else
86                         flood = 1;
87         }
88
89         if(flood)
90         {
91                 if(cvar("g_chat_flood_notify_flooder"))
92                 {
93                         if(flood == 1)
94                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
95                         else if(flood == 2)
96                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
97                 }
98                 else
99                         sprint(self, msgstr);
100                 ServerConsoleEcho(strcat("NOTE: ", playername(self), "^7 is flooding."), TRUE);
101         }
102         else if(teamsay)
103         {
104                 if(source.classname == "player")
105                 {
106                         FOR_EACH_REALPLAYER(head)
107                         {
108                                 if(head.team == source.team)
109                                 {
110                                         sprint(head, msgstr);
111                                         centerprint(head, cmsgstr);
112                                 }
113                         }
114                 }
115                 else
116                 {
117                         FOR_EACH_REALCLIENT(head) if(head.classname != "player")
118                         {
119                                 sprint(head, msgstr);
120                                 centerprint(head, cmsgstr);
121                         }
122                 }
123         }
124         else
125         {
126                 bprint(msgstr);
127                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
128         }
129
130         strunzone(msgstr);
131 }
132
133 float VoteCheckNasty(string cmd)
134 {
135         if(strstrofs(cmd, ";", 0) >= 0)
136                 return TRUE;
137         if(strstrofs(cmd, "\n", 0) >= 0)
138                 return TRUE;
139         if(strstrofs(cmd, "\r", 0) >= 0)
140                 return TRUE;
141         if(strstrofs(cmd, "$", 0) >= 0)
142                 return TRUE;
143         return FALSE;
144 }
145
146 string GetKickVoteVictim_newcommand;
147 string GetKickVoteVictim_reason;
148 entity GetKickVoteVictim(string vote, string cmd)
149 {
150         float tokens;
151         float i, n, t;
152         string ns;
153         entity e;
154
155         tokens = tokenize(vote);
156         ns = "";
157
158         if(tokens >= 2)
159                 if(substring(argv(1), 0, 1) == "#")
160                 {
161                         ns = substring(argv(1), 1, 999);
162                         t = 2;
163                 }
164         
165         if(tokens >= 3)
166                 if(argv(1) == "#")
167                 {
168                         ns = argv(2);
169                         t = 3;
170                 }
171
172         if(ns != "")
173         {
174                 GetKickVoteVictim_reason = "";
175                 for(i = t; i < tokens; ++i)
176                         GetKickVoteVictim_reason = strcat(GetKickVoteVictim_reason, argv(i), " ");
177                 GetKickVoteVictim_reason = substring(GetKickVoteVictim_reason, 0, strlen(GetKickVoteVictim_reason) - 1);
178                 
179                 n = stof(ns);
180                 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
181                 {
182                         for((e = world), (i = 0); i < n; ++i, (e = nextent(e)))
183                                 ;
184                         if(clienttype(e) == CLIENTTYPE_REAL)
185                         {
186                                 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
187                                 return e;
188                         }
189                 }
190         }
191
192         sprint(self, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
193         return world;
194 }
195
196 void SV_ParseClientCommand(string s) {
197         local string cmd;
198         local entity e;
199         local float i;
200
201         tokenize(s);
202
203         if(argv(0) == "vote") {
204                 if(argv(1) == "help") {
205                         local string vmasterdis;
206                         if(!cvar("sv_vote_master")) {
207                                 vmasterdis = " ^1(disabled)";
208                         }
209                         local string vcalldis;
210                         if(!cvar("sv_vote_call")) {
211                                 vcalldis = " ^1(disabled)";
212                         }
213                         sprint(self, "^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 do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\" \"^2cmd vote dontcare^7\".\n");
214                         sprint(self, "^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\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\" \"^2vdontcare^7\".\n");
215                         sprint(self, "^7\"^2help^7\" shows this info.\n");
216                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
217                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
218                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
219                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
220                         sprint(self, "^7\"^2do^7\" If you are a master you can execute a command without a vote. See the list of allowed commands.\n");
221                         sprint(self, "^7\"^2yes^7\", \"^2no^7\" and \"^2dontcare^7\" to make your vote.\n");
222                         sprint(self, "^7If enough of the players vote yes the vote is accepted.\n");
223                         sprint(self, "^7If enough of the players vote no the vote is rejected.\n");
224                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
225                         sprint(self, "^7You can call a vote for or execute these commands:\n");
226                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
227                 } else if(argv(1) == "status") {
228                         if(votecalled) {
229                                 sprint(self, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", votecaller.netname, "^7.\n"));
230                         } else {
231                                 sprint(self, "^1No vote called.\n");
232                         }
233                 } else if(argv(1) == "call") {
234                         if(cvar("sv_vote_call")) {
235                                 if(votecalled) {
236                                         sprint(self, "^1There is already a vote called.\n");
237                                 } else {
238                                         local string vote;
239                                         vote = VoteParse();
240                                         if(vote == "") {
241                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
242                                         } else if(time < self.vote_next) {
243                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
244                                         } else if(VoteCheckNasty(vote)) {
245                                                 sprint(self, "Syntax error in command.\n");
246                                         } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
247                                                 // remap chmap to gotomap (forces intermission)
248                                                 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
249                                                         return;
250                                                 if(substring(vote, 0, 6) == "chmap ")
251                                                         vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
252                                                 if(substring(vote, 0, 8) == "gotomap ")
253                                                 {
254                                                         if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8))))
255                                                                 return;
256                                                         vote = strcat("gotomap ", vote);
257                                                 }
258
259                                                 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
260                                                 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
261                                                 {
262                                                         if(!(e = GetKickVoteVictim(vote, "vcall")))
263                                                                 return;
264                                                         vote = GetKickVoteVictim_newcommand;
265                                                         votecalledvote_display = strzone(strcat("^1", vote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason));
266                                                 }
267                                                 else
268                                                 {
269                                                         votecalledvote_display = strzone(strcat("^1", vote));
270                                                 }
271                                                 votecalledvote = strzone(vote);
272                                                 votecalled = TRUE;
273                                                 votecalledmaster = FALSE;
274                                                 votecaller = self; // remember who called the vote
275                                                 votefinished = time + cvar("sv_vote_timeout");
276                                                 votecaller.vote_vote = 1; // of course you vote yes
277                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
278                                                 bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ", votecalledvote_display, "\n");
279                                                 VoteCount(); // needed if you are the only one
280                                         } else {
281                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
282                                         }
283                                 }
284                         } else {
285                                 sprint(self, "^1Vote calling is NOT allowed.\n");
286                         }
287                 } else if(argv(1) == "stop") {
288                         if(!votecalled) {
289                                 sprint(self, "^1No vote called.\n");
290                         } else if(self == votecaller) { // the votecaller can stop a vote
291                                 VoteStop(self);
292                         } else if(self.vote_master) { // masters can, too
293                                 VoteStop(self);
294                         } else {
295                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
296                         }
297                 } else if(argv(1) == "master") {
298                         if(cvar("sv_vote_master")) {
299                                 if(votecalled) {
300                                         sprint(self, "^1There is already a vote called.\n");
301                                 } else {
302                                         votecalled = TRUE;
303                                         votecalledmaster = TRUE;
304                                         votecalledvote = strzone("XXX");
305                                         votecalledvote_display = strzone("^3master");
306                                         votecaller = self; // remember who called the vote
307                                         votefinished = time + cvar("sv_vote_timeout");
308                                         votecaller.vote_vote = 1; // of course you vote yes
309                                         votecaller.vote_next = time + cvar("sv_vote_wait");
310                                         bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n");
311                                         VoteCount(); // needed if you are the only one
312                                 }
313                         } else {
314                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
315                         }
316                 } else if(argv(1) == "do") {
317                         if(argv(2) == "login") {
318                                 local string masterpwd;
319                                 masterpwd = cvar_string("sv_vote_master_password");
320                                 if(masterpwd != "") {
321                                         self.vote_master = (masterpwd == argv(3));
322                                         if(self.vote_master) {
323                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
324                                                 bprint("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n");
325                                         }
326                                         else
327                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
328                                 }
329                                 else
330                                         sprint(self, "^1You are NOT a master.\n");
331                         } else if(self.vote_master) {
332                                 local string dovote, dovote_display;
333                                 dovote = VoteParse();
334                                 if(dovote == "") {
335                                         sprint(self, "^1Your command was empty. See help for more info.\n");
336                                 } else if(VoteCheckNasty(dovote)) {
337                                         sprint(self, "Syntax error in command.\n");
338                                 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
339                                         if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
340                                                 return;
341                                         if(substring(dovote, 0, 6) == "chmap ")
342                                                 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
343                                         if(substring(dovote, 0, 8) == "gotomap ")
344                                         {
345                                                 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8))))
346                                                         return;
347                                                 dovote = strcat("gotomap ", dovote);
348                                         }
349
350                                         dovote_display = dovote;
351                                         if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
352                                         {
353                                                 if(!(e = GetKickVoteVictim(dovote, "vdo")))
354                                                         return;
355                                                 dovote = GetKickVoteVictim_newcommand;
356                                                 dovote_display = strcat("^1", dovote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason);
357                                         }
358                                         bprint("\{1}^2* ^3", self.netname, "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
359                                         localcmd(strcat(dovote, "\n"));
360                                 } else {
361                                         sprint(self, "^1This command is not ok. See help for more info.\n");
362                                 }
363                         } else {
364                                 sprint(self, "^1You are NOT a master.\n");
365                         }
366                 } else if(argv(1) == "yes") {
367                         if(!votecalled) {
368                                 sprint(self, "^1No vote called.\n");
369                         } else if(self.vote_vote == 0
370                                   || cvar("sv_vote_change")) {
371                                 sprint(self, "^1You accepted the vote.\n");
372                                 self.vote_vote = 1;
373                                 centerprint_expire(self, CENTERPRIO_VOTE);
374                                 if(!cvar("sv_vote_singlecount")) {
375                                         VoteCount();
376                                 }
377                         } else {
378                                 sprint(self, "^1You have already voted.\n");
379                         }
380                 } else if(argv(1) == "no") {
381                         if(!votecalled) {
382                                 sprint(self, "^1No vote called.\n");
383                         } else if(self.vote_vote == 0
384                                   || cvar("sv_vote_change")) {
385                                 sprint(self, "^1You rejected the vote.\n");
386                                 self.vote_vote = -1;
387                                 centerprint_expire(self, CENTERPRIO_VOTE);
388                                 if(!cvar("sv_vote_singlecount")) {
389                                         VoteCount();
390                                 }
391                         } else {
392                                 sprint(self, "^1You have already voted.\n");
393                         }
394                 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
395                         if(!votecalled) {
396                                 sprint(self, "^1No vote called.\n");
397                         } else if(self.vote_vote == 0
398                                   || cvar("sv_vote_change")) {
399                                 sprint(self, "^1You abstained from your vote.\n");
400                                 self.vote_vote = -2;
401                                 centerprint_expire(self, CENTERPRIO_VOTE);
402                                 if(!cvar("sv_vote_singlecount")) {
403                                         VoteCount();
404                                 }
405                         } else {
406                                 sprint(self, "^1You have already voted.\n");
407                         }
408                 } else {
409                         // ignore this?
410                         sprint(self, "^1Unknown vote command.\n");
411                 }
412         } else if(argv(0) == "autoswitch") {
413                 // be backwards compatible with older clients (enabled)
414                 self.autoswitch = ("0" != argv(1));
415                 local string autoswitchmsg;
416                 if (self.autoswitch) {
417                         autoswitchmsg = "on";
418                 } else {
419                         autoswitchmsg = "off";
420                 }
421                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
422         } else if(argv(0) == "clientversion") {
423                 if (argv(1) == "$gameversion") {
424                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
425                         // either that or someone wants to be funny
426                         self.version = 1;
427                 } else {
428                         self.version = stof(argv(1));
429                 }
430                 if(self.version != cvar("gameversion")) 
431                 {
432                         self.classname = "observer";
433                         self.frags = -2;
434                         PutClientInServer();
435                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
436                         //JoinBestTeam(self, 0);
437                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
438                         self.classname = "observer";
439                         stuffcmd(self,"menu_showteamselect\n");
440                 }
441         } else if(argv(0) == "reportcvar") { // old system
442                 GetCvars(1);
443         } else if(argv(0) == "sentcvar") { // new system
444                 GetCvars(1);
445         } else if(argv(0) == "spectate") {
446                 if(g_lms || g_arena)
447                         return; // don't allow spectating in lms, unless player runs out of lives
448                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
449                         if(self.flagcarried)
450                                 DropFlag(self.flagcarried);
451                         kh_Key_DropAll(self, TRUE);
452                         WaypointSprite_PlayerDead();
453                         DistributeFragsAmongTeam(self, self.team, 1.0);
454                         self.classname = "observer";
455                         PutClientInServer();
456                 }
457         } else if(argv(0) == "join") {
458                 if(!g_arena)
459                 if (self.classname != "player")
460                 {
461                         self.classname = "player";
462                         self.frags = 0;
463                         bprint ("^4", self.netname, "^4 is playing now\n");
464                         PutClientInServer();
465                 }
466         } else if( argv(0) == "selectteam" ) {
467                 if( !cvar("teamplay") ) {
468                         sprint( self, "selecteam can only be used in teamgames\n");
469                 } else if(cvar("g_campaign")) {
470                         //JoinBestTeam(self, 0);
471                 } else if( argv(1) == "red" ) {
472                         DoTeamChange(COLOR_TEAM1);
473                 } else if( argv(1) == "blue" ) {
474                         DoTeamChange(COLOR_TEAM2);
475                 } else if( argv(1) == "yellow" ) {
476                         DoTeamChange(COLOR_TEAM3);
477                 } else if( argv(1) == "pink" ) {
478                         DoTeamChange(COLOR_TEAM4);
479                 } else if( argv(1) == "auto" ) {
480                         DoTeamChange(-1);
481                 } else {
482                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
483                 }
484         } else if(argv(0) == "ready") {
485                 if(cvar("sv_ready_restart"))
486                 {
487                         if(!restart_countdown)
488                         {
489                                 self.ready = TRUE;
490                                 bprint(self.netname, "^2 is ready\n");
491                                 ReadyCount();
492                         } else {
493                                 sprint(self, "^1game has already been restarted\n");
494                         }
495                 }
496         } else if(argv(0) == "maplist") {
497                 local float n;
498                 local string col;
499                 n = tokenize(cvar_string("g_maplist"));
500                 sprint(self, "^7Maps in list: ");
501                 for(i = 0; i < n; ++i)
502                 {
503                         if(mod(i, 2))
504                                 col = "^2";
505                         else
506                                 col = "^3";
507                         sprint(self, strcat(col, argv(i), " "));
508                 }
509                 sprint(self, "\n");
510 #ifdef MAPINFO
511         } else if(argv(0) == "lsmaps") {
512                 sprint(self, "^7Maps available: ");
513                 for(i = 0; i < MapInfo_count; ++i)
514                 {
515                         if(mod(i, 2))
516                                 col = "^2";
517                         else
518                                 col = "^3";
519                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
520                 }
521                 sprint(self, "\n");
522 #endif
523         } else if(argv(0) == "teamstatus") {
524                 PrintScoreboard(self);
525         } else if(argv(0) == "voice") {
526                 VoiceMessage(argv(1));
527         } else if(argv(0) == "say") {
528                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
529                 //clientcommand(self, formatmessage(s));
530         } else if(argv(0) == "say_team") {
531                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
532                 //clientcommand(self, formatmessage(s));
533         } else if(argv(0) == "info") {
534                 cmd = cvar_string(strcat("sv_info_", argv(1)));
535                 if(cmd == "")
536                         sprint(self, "ERROR: unsupported info command\n");
537                 else
538                         wordwrap_sprint(cmd, 1111);
539         } else if(argv(0) == "suggestmap") {
540                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
541         } else {
542                 cmd = argv(0);
543                 /* checks not needed any more since DP has separated clientcommands and regular commands
544                 if(cmd != "status")
545                 if(cmd != "name")
546                 //if(cmd != "say") // handled above
547                 //if(cmd != "say_team") // handled above
548                 if(cmd != "tell")
549                 if(cmd != "color")
550                 if(cmd != "kill")
551                 if(cmd != "pause")
552                 if(cmd != "kick")
553                 if(cmd != "ping")
554                 if(cmd != "pings")
555                 if(cmd != "ban")
556                 if(cmd != "pmodel")
557                 if(cmd != "rate")
558                 if(cmd != "playermodel")
559                 if(cmd != "playerskin")
560                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
561                 {
562                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
563                         return;
564                 }
565                 */
566                 clientcommand(self,s);
567         }
568 }
569
570 string ValidateMap(string m)
571 {
572 #ifdef MAPINFO
573         m = MapInfo_FixName(m);
574         if(!m)
575         {
576                 sprint(self, "This map is not available on this server.\n");
577                 return string_null;
578         }
579 #endif
580         if(!cvar("sv_vote_change_gametype"))
581                 if(!IsSameGametype(m))
582                 {
583                         sprint(self, "This server does not allow changing the game type by map votes.\n");
584                         return string_null;
585                 }
586         if(!cvar("sv_vote_override_mostrecent"))
587                 if(Map_IsRecent(m))
588                 {
589                         sprint(self, "This server does not allow for recent maps to be played again. Please be patient for some rounds.\n");
590                         return string_null;
591                 }
592 #ifdef MAPINFO
593         if(!MapInfo_CheckMap(m))
594         {
595                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode.\n"));
596                 return string_null;
597         }
598 #else
599         if(!TryFile(strcat("maps/", m, ".mapcfg")))
600         {
601                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server.\n"));
602                 return string_null;
603         }
604 #endif
605
606         return m;
607 }
608
609
610 void VoteThink() {
611         if(votefinished > 0 // a vote was called
612             && time > votefinished) // time is up
613         {
614                 VoteCount();
615         }
616 }
617
618 string VoteParse() {
619         local float index;
620         index = 3;
621         local string vote;
622         vote = argv(2);
623         while(argv(index) != "") {
624                 vote = strcat(vote, " ", argv(index));
625                 index++;
626         }
627
628         // necessary for some of the string operations
629         vote = strzone(vote);
630
631         // now we remove some things that could be misused
632         index = 0;
633         local float found;
634         found = FALSE;
635         local float votelength;
636         votelength = strlen(vote);
637         while(!found && index < votelength)
638         {
639                 local string badchar;
640                 badchar = substring(vote, index, 1);
641                 if(badchar == ";"
642                    || badchar == "\r"
643                    || badchar == "\n")
644                 {
645                         found = TRUE;
646                 } else {
647                         index++;
648                 }
649         }
650         return substring(vote, 0, index);
651 }
652
653 float VoteAllowed(string votecommand) {
654         tokenize(cvar_string("sv_vote_commands"));
655         local float index;
656         index = 0;
657         while(argv(index) != "") {
658                 local string allowed;
659                 allowed = argv(index);
660                 if(votecommand == allowed) {
661                         return TRUE;
662                 }
663                 index++;
664         }
665         return FALSE;
666 }
667
668 void VoteReset() {
669         local entity player;
670
671         FOR_EACH_CLIENT(player)
672         {
673                 player.vote_vote = 0;
674                 centerprint_expire(player, CENTERPRIO_VOTE);
675         }
676
677         if(votecalled)
678         {
679                 strunzone(votecalledvote);
680                 strunzone(votecalledvote_display);
681         }
682
683         votecalled = FALSE;
684         votecalledmaster = FALSE;
685         votefinished = 0;
686 }
687
688 void VoteAccept() {
689         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
690         if(votecalledmaster)
691         {
692                 votecaller.vote_master = 1;
693         } else {
694                 localcmd(strcat(votecalledvote, "\n"));
695         }
696         votecaller.vote_next = 0; // people like your votes, no wait for next vote
697         VoteReset();
698 }
699
700 void VoteReject() {
701         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
702         VoteReset();
703 }
704
705 void VoteTimeout() {
706         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 timed out\n");
707         VoteReset();
708 }
709
710 void VoteStop(entity stopper) {
711         bprint("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n");
712         if(stopper == votecaller) {
713                 // no wait for next vote so you can correct your vote
714                 votecaller.vote_next = 0;
715         }
716         VoteReset();
717 }
718
719 void VoteNag() {
720         if(votecalled)
721                 if(self.vote_vote == 0)
722                         centerprint_atprio(self, CENTERPRIO_VOTE, strcat("^7^3", votecaller.netname, "^2 called a vote for ", votecalledvote_display, "\n\n^2You have not voted yet!\n^2HINT: By default, F1 is yes and F2 is no."));
723 }
724
725 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount)
726 {
727         string s;
728         if(mincount >= 0)
729         {
730                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
731                 s = strcat(s, ftos(nocount), "^2 (^1");
732                 s = strcat(s, ftos(mincount), "^2 needed), ^1");
733                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
734                 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
735         }
736         else
737         {
738                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
739                 s = strcat(s, ftos(nocount), "^2 (^1");
740                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
741                 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
742         }
743         bprint(s);
744 }
745
746 void VoteCount() {
747         local float playercount;
748         playercount = 0;
749         local float yescount;
750         yescount = 0;
751         local float nocount;
752         nocount = 0;
753         local float abstaincount;
754         abstaincount = 0;
755         local entity player;
756
757         FOR_EACH_REALCLIENT(player)
758         {
759                 if(player.vote_vote == -1) {
760                         nocount++;
761                 } else if(player.vote_vote == 1) {
762                         yescount++;
763                 } else if(player.vote_vote == -2) {
764                         abstaincount++;
765                 }
766                 playercount++;
767         }
768
769         if((playercount == 1) && votecalledmaster) {
770                 // if only one player is on the server becoming vote
771                 // master is not allowed.  This could be used for
772                 // trolling or worse. 'self' is the user who has
773                 // called the vote because this function is called
774                 // by SV_ParseClientCommand. Maybe all voting should
775                 // be disabled for a single player?
776                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
777                 votecaller.vote_next = 0;
778                 VoteReset();
779         } else {
780                 float votefactor;
781                 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
782                 if(yescount > (playercount - abstaincount) * votefactor)
783                 {
784                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
785                         VoteAccept();
786                 }
787                 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
788                 {
789                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
790                         VoteReject();
791                 }
792                 else if(time > votefinished)
793                 {
794                         if(cvar("sv_vote_simple_majority"))
795                         {
796                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1);
797                                 if(yescount > (yescount + nocount) * votefactor)
798                                         VoteAccept();
799                                 else if(yescount + nocount > 0)
800                                         VoteReject();
801                                 else
802                                         VoteTimeout();
803                         }
804                         else
805                         {
806                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1);
807                                 VoteTimeout();
808                         }
809                 }
810         }
811 }
812
813 float timelimit_orig;
814
815 void ReadyCount()
816 {
817         local entity e;
818         local float r, p;
819
820         FOR_EACH_REALPLAYER(e)
821         {
822                 p += 1;
823                 if(e.ready)
824                         r += 1;
825         }
826
827         if(!p || r < p)
828                 return;
829
830         bprint("^1Server is restarting...\n");
831         
832         // no arena, assault & onslaught support yet...
833         if(g_arena | g_assault | g_onslaught | gameover | intermission_running)
834                 localcmd("restart\n");
835
836         restart_countdown = time + RESTART_COUNTDOWN;
837         if(0<cvar("timelimit"))
838         {
839                 // remember original timelimit on first restart
840                 if(!timelimit_orig)
841                         timelimit_orig = cvar("timelimit");
842                 cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
843         }
844
845         reset_map();
846         
847         if(cvar("sv_eventlog"))
848                 GameLogEcho(":restart", FALSE);
849 }