]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
Nifreks lockonrestart feature. Used in team-based game modes. Once all players are...
[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                         e = edict_num(n);
183                         if(clienttype(e) == CLIENTTYPE_REAL)
184                         {
185                                 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
186                                 return e;
187                         }
188                 }
189         }
190
191         sprint(self, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
192         return world;
193 }
194
195 void SV_ParseClientCommand(string s) {
196         local string cmd;
197         local entity e;
198         local float i;
199
200         tokenize(s);
201
202         if(argv(0) == "vote") {
203                 if(argv(1) == "help") {
204                         local string vmasterdis;
205                         if(!cvar("sv_vote_master")) {
206                                 vmasterdis = " ^1(disabled)";
207                         }
208                         local string vcalldis;
209                         if(!cvar("sv_vote_call")) {
210                                 vcalldis = " ^1(disabled)";
211                         }
212                         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");
213                         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");
214                         sprint(self, "^7\"^2help^7\" shows this info.\n");
215                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
216                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
217                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
218                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
219                         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");
220                         sprint(self, "^7\"^2yes^7\", \"^2no^7\" and \"^2dontcare^7\" to make your vote.\n");
221                         sprint(self, "^7If enough of the players vote yes the vote is accepted.\n");
222                         sprint(self, "^7If enough of the players vote no the vote is rejected.\n");
223                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
224                         sprint(self, "^7You can call a vote for or execute these commands:\n");
225                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
226                 } else if(argv(1) == "status") {
227                         if(votecalled) {
228                                 sprint(self, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", votecaller.netname, "^7.\n"));
229                         } else {
230                                 sprint(self, "^1No vote called.\n");
231                         }
232                 } else if(argv(1) == "call") {
233                         if(cvar("sv_vote_call")) {
234                                 if(votecalled) {
235                                         sprint(self, "^1There is already a vote called.\n");
236                                 } else {
237                                         local string vote;
238                                         vote = VoteParse();
239                                         if(vote == "") {
240                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
241                                         } else if(time < self.vote_next) {
242                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
243                                         } else if(VoteCheckNasty(vote)) {
244                                                 sprint(self, "Syntax error in command.\n");
245                                         } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
246                                                 // remap chmap to gotomap (forces intermission)
247                                                 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
248                                                         return;
249                                                 if(substring(vote, 0, 6) == "chmap ")
250                                                         vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
251                                                 if(substring(vote, 0, 8) == "gotomap ")
252                                                 {
253                                                         if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8))))
254                                                                 return;
255                                                         vote = strcat("gotomap ", vote);
256                                                 }
257
258                                                 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
259                                                 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
260                                                 {
261                                                         if(!(e = GetKickVoteVictim(vote, "vcall")))
262                                                                 return;
263                                                         vote = GetKickVoteVictim_newcommand;
264                                                         votecalledvote_display = strzone(strcat("^1", vote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason));
265                                                 }
266                                                 else
267                                                 {
268                                                         votecalledvote_display = strzone(strcat("^1", vote));
269                                                 }
270                                                 votecalledvote = strzone(vote);
271                                                 votecalled = TRUE;
272                                                 votecalledmaster = FALSE;
273                                                 votecaller = self; // remember who called the vote
274                                                 votefinished = time + cvar("sv_vote_timeout");
275                                                 votecaller.vote_vote = 1; // of course you vote yes
276                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
277                                                 bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ", votecalledvote_display, "\n");
278                                                 VoteCount(); // needed if you are the only one
279                                         } else {
280                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
281                                         }
282                                 }
283                         } else {
284                                 sprint(self, "^1Vote calling is NOT allowed.\n");
285                         }
286                 } else if(argv(1) == "stop") {
287                         if(!votecalled) {
288                                 sprint(self, "^1No vote called.\n");
289                         } else if(self == votecaller) { // the votecaller can stop a vote
290                                 VoteStop(self);
291                         } else if(self.vote_master) { // masters can, too
292                                 VoteStop(self);
293                         } else {
294                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
295                         }
296                 } else if(argv(1) == "master") {
297                         if(cvar("sv_vote_master")) {
298                                 if(votecalled) {
299                                         sprint(self, "^1There is already a vote called.\n");
300                                 } else {
301                                         votecalled = TRUE;
302                                         votecalledmaster = TRUE;
303                                         votecalledvote = strzone("XXX");
304                                         votecalledvote_display = strzone("^3master");
305                                         votecaller = self; // remember who called the vote
306                                         votefinished = time + cvar("sv_vote_timeout");
307                                         votecaller.vote_vote = 1; // of course you vote yes
308                                         votecaller.vote_next = time + cvar("sv_vote_wait");
309                                         bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n");
310                                         VoteCount(); // needed if you are the only one
311                                 }
312                         } else {
313                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
314                         }
315                 } else if(argv(1) == "do") {
316                         if(argv(2) == "login") {
317                                 local string masterpwd;
318                                 masterpwd = cvar_string("sv_vote_master_password");
319                                 if(masterpwd != "") {
320                                         self.vote_master = (masterpwd == argv(3));
321                                         if(self.vote_master) {
322                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
323                                                 bprint("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n");
324                                         }
325                                         else
326                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
327                                 }
328                                 else
329                                         sprint(self, "^1You are NOT a master.\n");
330                         } else if(self.vote_master) {
331                                 local string dovote, dovote_display;
332                                 dovote = VoteParse();
333                                 if(dovote == "") {
334                                         sprint(self, "^1Your command was empty. See help for more info.\n");
335                                 } else if(VoteCheckNasty(dovote)) {
336                                         sprint(self, "Syntax error in command.\n");
337                                 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
338                                         if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
339                                                 return;
340                                         if(substring(dovote, 0, 6) == "chmap ")
341                                                 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
342                                         if(substring(dovote, 0, 8) == "gotomap ")
343                                         {
344                                                 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8))))
345                                                         return;
346                                                 dovote = strcat("gotomap ", dovote);
347                                         }
348
349                                         dovote_display = dovote;
350                                         if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
351                                         {
352                                                 if(!(e = GetKickVoteVictim(dovote, "vdo")))
353                                                         return;
354                                                 dovote = GetKickVoteVictim_newcommand;
355                                                 dovote_display = strcat("^1", dovote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason);
356                                         }
357                                         bprint("\{1}^2* ^3", self.netname, "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
358                                         localcmd(strcat(dovote, "\n"));
359                                 } else {
360                                         sprint(self, "^1This command is not ok. See help for more info.\n");
361                                 }
362                         } else {
363                                 sprint(self, "^1You are NOT a master.\n");
364                         }
365                 } else if(argv(1) == "yes") {
366                         if(!votecalled) {
367                                 sprint(self, "^1No vote called.\n");
368                         } else if(self.vote_vote == 0
369                                   || cvar("sv_vote_change")) {
370                                 sprint(self, "^1You accepted the vote.\n");
371                                 self.vote_vote = 1;
372                                 centerprint_expire(self, CENTERPRIO_VOTE);
373                                 if(!cvar("sv_vote_singlecount")) {
374                                         VoteCount();
375                                 }
376                         } else {
377                                 sprint(self, "^1You have already voted.\n");
378                         }
379                 } else if(argv(1) == "no") {
380                         if(!votecalled) {
381                                 sprint(self, "^1No vote called.\n");
382                         } else if(self.vote_vote == 0
383                                   || cvar("sv_vote_change")) {
384                                 sprint(self, "^1You rejected the vote.\n");
385                                 self.vote_vote = -1;
386                                 centerprint_expire(self, CENTERPRIO_VOTE);
387                                 if(!cvar("sv_vote_singlecount")) {
388                                         VoteCount();
389                                 }
390                         } else {
391                                 sprint(self, "^1You have already voted.\n");
392                         }
393                 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
394                         if(!votecalled) {
395                                 sprint(self, "^1No vote called.\n");
396                         } else if(self.vote_vote == 0
397                                   || cvar("sv_vote_change")) {
398                                 sprint(self, "^1You abstained from your vote.\n");
399                                 self.vote_vote = -2;
400                                 centerprint_expire(self, CENTERPRIO_VOTE);
401                                 if(!cvar("sv_vote_singlecount")) {
402                                         VoteCount();
403                                 }
404                         } else {
405                                 sprint(self, "^1You have already voted.\n");
406                         }
407                 } else {
408                         // ignore this?
409                         sprint(self, "^1Unknown vote command.\n");
410                 }
411         } else if(argv(0) == "autoswitch") {
412                 // be backwards compatible with older clients (enabled)
413                 self.autoswitch = ("0" != argv(1));
414                 local string autoswitchmsg;
415                 if (self.autoswitch) {
416                         autoswitchmsg = "on";
417                 } else {
418                         autoswitchmsg = "off";
419                 }
420                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
421         } else if(argv(0) == "clientversion") {
422                 if (argv(1) == "$gameversion") {
423                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
424                         // either that or someone wants to be funny
425                         self.version = 1;
426                 } else {
427                         self.version = stof(argv(1));
428                 }
429                 if(self.version != cvar("gameversion"))
430                 {
431                         self.classname = "observer";
432                         self.frags = -2;
433                         PutClientInServer();
434                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
435                         //JoinBestTeam(self, FALSE, TRUE);
436                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
437                         self.classname = "observer";
438                         stuffcmd(self,"menu_showteamselect\n");
439                 }
440         } else if(argv(0) == "reportcvar") { // old system
441                 GetCvars(1);
442         } else if(argv(0) == "sentcvar") { // new system
443                 GetCvars(1);
444         } else if(argv(0) == "spectate") {
445                 if(g_lms || g_arena)
446                         return; // don't allow spectating in lms, unless player runs out of lives
447                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
448                         if(self.flagcarried)
449                                 DropFlag(self.flagcarried);
450                         kh_Key_DropAll(self, TRUE);
451                         WaypointSprite_PlayerDead();
452                         DistributeFragsAmongTeam(self, self.team, 1.0);
453                         self.classname = "observer";
454                         PutClientInServer();
455                 }
456         } else if(argv(0) == "join") {
457                 if(!g_arena)
458                 if (self.classname != "player" && !lockteams)
459                 {
460                         self.classname = "player";
461                         self.frags = 0;
462                         bprint ("^4", self.netname, "^4 is playing now\n");
463                         PutClientInServer();
464                 }
465         } else if( argv(0) == "selectteam" ) {
466                 if( !cvar("teamplay") ) {
467                         sprint( self, "selecteam can only be used in teamgames\n");
468                 } else if(cvar("g_campaign")) {
469                         //JoinBestTeam(self, 0);
470                 } else if(lockteams) {
471                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
472                 } else if( argv(1) == "red" ) {
473                         DoTeamChange(COLOR_TEAM1);
474                 } else if( argv(1) == "blue" ) {
475                         DoTeamChange(COLOR_TEAM2);
476                 } else if( argv(1) == "yellow" ) {
477                         DoTeamChange(COLOR_TEAM3);
478                 } else if( argv(1) == "pink" ) {
479                         DoTeamChange(COLOR_TEAM4);
480                 } else if( argv(1) == "auto" ) {
481                         DoTeamChange(-1);
482                 } else {
483                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
484                 }
485         } else if(argv(0) == "ready") {
486                 if(cvar("sv_ready_restart"))
487                 {
488                         if(!restart_countdown || cvar("sv_ready_restart_repeatable"))
489                         {
490                                 self.ready = TRUE;
491                                 bprint(self.netname, "^2 is ready\n");
492                                 ReadyCount();
493                         } else {
494                                 sprint(self, "^1game has already been restarted\n");
495                         }
496                 }
497         } else if(argv(0) == "maplist") {
498                 local float n;
499                 local string col;
500                 n = tokenize(cvar_string("g_maplist"));
501                 sprint(self, "^7Maps in list: ");
502                 for(i = 0; i < n; ++i)
503                 {
504                         if(mod(i, 2))
505                                 col = "^2";
506                         else
507                                 col = "^3";
508                         sprint(self, strcat(col, argv(i), " "));
509                 }
510                 sprint(self, "\n");
511 #ifdef MAPINFO
512         } else if(argv(0) == "lsmaps") {
513                 sprint(self, "^7Maps available: ");
514                 for(i = 0; i < MapInfo_count; ++i)
515                 {
516                         if(mod(i, 2))
517                                 col = "^2";
518                         else
519                                 col = "^3";
520                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
521                 }
522                 sprint(self, "\n");
523 #endif
524         } else if(argv(0) == "teamstatus") {
525                 PrintScoreboard(self);
526         } else if(argv(0) == "voice") {
527                 VoiceMessage(argv(1));
528         } else if(argv(0) == "say") {
529                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
530                 //clientcommand(self, formatmessage(s));
531         } else if(argv(0) == "say_team") {
532                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
533                 //clientcommand(self, formatmessage(s));
534         } else if(argv(0) == "info") {
535                 cmd = cvar_string(strcat("sv_info_", argv(1)));
536                 if(cmd == "")
537                         sprint(self, "ERROR: unsupported info command\n");
538                 else
539                         wordwrap_sprint(cmd, 1111);
540         } else if(argv(0) == "suggestmap") {
541                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
542         } else {
543                 cmd = argv(0);
544                 /* checks not needed any more since DP has separated clientcommands and regular commands
545                 if(cmd != "status")
546                 if(cmd != "name")
547                 //if(cmd != "say") // handled above
548                 //if(cmd != "say_team") // handled above
549                 if(cmd != "tell")
550                 if(cmd != "color")
551                 if(cmd != "kill")
552                 if(cmd != "pause")
553                 if(cmd != "kick")
554                 if(cmd != "ping")
555                 if(cmd != "pings")
556                 if(cmd != "ban")
557                 if(cmd != "pmodel")
558                 if(cmd != "rate")
559                 if(cmd != "playermodel")
560                 if(cmd != "playerskin")
561                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
562                 {
563                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
564                         return;
565                 }
566                 */
567                 clientcommand(self,s);
568         }
569 }
570
571 string ValidateMap(string m)
572 {
573 #ifdef MAPINFO
574         m = MapInfo_FixName(m);
575         if(!m)
576         {
577                 sprint(self, "This map is not available on this server.\n");
578                 return string_null;
579         }
580 #else
581         if(!cvar("sv_vote_change_gametype"))
582                 if(!IsSameGametype(m))
583                 {
584                         sprint(self, "This server does not allow changing the game type by map votes.\n");
585                         return string_null;
586                 }
587 #endif
588         if(!cvar("sv_vote_override_mostrecent"))
589                 if(Map_IsRecent(m))
590                 {
591                         sprint(self, "This server does not allow for recent maps to be played again. Please be patient for some rounds.\n");
592                         return string_null;
593                 }
594 #ifdef MAPINFO
595         if(!MapInfo_CheckMap(m))
596         {
597                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode.\n"));
598                 return string_null;
599         }
600 #else
601         if(!TryFile(strcat("maps/", m, ".mapcfg")))
602         {
603                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server.\n"));
604                 return string_null;
605         }
606 #endif
607
608         return m;
609 }
610
611
612 void VoteThink() {
613         if(votefinished > 0 // a vote was called
614             && time > votefinished) // time is up
615         {
616                 VoteCount();
617         }
618 }
619
620 string VoteParse() {
621         local float index;
622         index = 3;
623         local string vote;
624         vote = argv(2);
625         while(argv(index) != "") {
626                 vote = strcat(vote, " ", argv(index));
627                 index++;
628         }
629
630         // necessary for some of the string operations
631         vote = strzone(vote);
632
633         // now we remove some things that could be misused
634         index = 0;
635         local float found;
636         found = FALSE;
637         local float votelength;
638         votelength = strlen(vote);
639         while(!found && index < votelength)
640         {
641                 local string badchar;
642                 badchar = substring(vote, index, 1);
643                 if(badchar == ";"
644                    || badchar == "\r"
645                    || badchar == "\n")
646                 {
647                         found = TRUE;
648                 } else {
649                         index++;
650                 }
651         }
652         return substring(vote, 0, index);
653 }
654
655 float VoteAllowed(string votecommand) {
656         tokenize(cvar_string("sv_vote_commands"));
657         local float index;
658         index = 0;
659         while(argv(index) != "") {
660                 local string allowed;
661                 allowed = argv(index);
662                 if(votecommand == allowed) {
663                         return TRUE;
664                 }
665                 index++;
666         }
667         return FALSE;
668 }
669
670 void VoteReset() {
671         local entity player;
672
673         FOR_EACH_CLIENT(player)
674         {
675                 player.vote_vote = 0;
676                 centerprint_expire(player, CENTERPRIO_VOTE);
677         }
678
679         if(votecalled)
680         {
681                 strunzone(votecalledvote);
682                 strunzone(votecalledvote_display);
683         }
684
685         votecalled = FALSE;
686         votecalledmaster = FALSE;
687         votefinished = 0;
688 }
689
690 void VoteAccept() {
691         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
692         if(votecalledmaster)
693         {
694                 votecaller.vote_master = 1;
695         } else {
696                 localcmd(strcat(votecalledvote, "\n"));
697         }
698         votecaller.vote_next = 0; // people like your votes, no wait for next vote
699         VoteReset();
700 }
701
702 void VoteReject() {
703         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
704         VoteReset();
705 }
706
707 void VoteTimeout() {
708         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 timed out\n");
709         VoteReset();
710 }
711
712 void VoteStop(entity stopper) {
713         bprint("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n");
714         if(stopper == votecaller) {
715                 // no wait for next vote so you can correct your vote
716                 votecaller.vote_next = 0;
717         }
718         VoteReset();
719 }
720
721 void VoteNag() {
722         if(votecalled)
723                 if(self.vote_vote == 0)
724                         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."));
725 }
726
727 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount)
728 {
729         string s;
730         if(mincount >= 0)
731         {
732                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
733                 s = strcat(s, ftos(nocount), "^2 (^1");
734                 s = strcat(s, ftos(mincount), "^2 needed), ^1");
735                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
736                 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
737         }
738         else
739         {
740                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
741                 s = strcat(s, ftos(nocount), "^2 (^1");
742                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
743                 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
744         }
745         bprint(s);
746 }
747
748 void VoteCount() {
749         local float playercount;
750         playercount = 0;
751         local float yescount;
752         yescount = 0;
753         local float nocount;
754         nocount = 0;
755         local float abstaincount;
756         abstaincount = 0;
757         local entity player;
758
759         FOR_EACH_REALCLIENT(player)
760         {
761                 if(player.vote_vote == -1) {
762                         nocount++;
763                 } else if(player.vote_vote == 1) {
764                         yescount++;
765                 } else if(player.vote_vote == -2) {
766                         abstaincount++;
767                 }
768                 playercount++;
769         }
770
771         if((playercount == 1) && votecalledmaster) {
772                 // if only one player is on the server becoming vote
773                 // master is not allowed.  This could be used for
774                 // trolling or worse. 'self' is the user who has
775                 // called the vote because this function is called
776                 // by SV_ParseClientCommand. Maybe all voting should
777                 // be disabled for a single player?
778                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
779                 votecaller.vote_next = 0;
780                 VoteReset();
781         } else {
782                 float votefactor;
783                 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
784                 if(yescount > (playercount - abstaincount) * votefactor)
785                 {
786                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
787                         VoteAccept();
788                 }
789                 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
790                 {
791                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
792                         VoteReject();
793                 }
794                 else if(time > votefinished)
795                 {
796                         if(cvar("sv_vote_simple_majority"))
797                         {
798                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1);
799                                 if(yescount > (yescount + nocount) * votefactor)
800                                         VoteAccept();
801                                 else if(yescount + nocount > 0)
802                                         VoteReject();
803                                 else
804                                         VoteTimeout();
805                         }
806                         else
807                         {
808                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1);
809                                 VoteTimeout();
810                         }
811                 }
812         }
813 }
814
815 float timelimit_orig;
816
817 /**
818  * Counts how many players are ready. If not enough players are ready, the function
819  * does nothing. If all players are ready, the timelimit will be extended and the
820  * restart_countdown variable is set to allow other functions like PlayerPostThink
821  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
822  * is not set the map will be resetted.
823  * 
824  * Function is called after the server receives a 'ready' sign from a player.
825  */
826 void ReadyCount()
827 {
828         local entity e;
829         local float r, p;
830
831         FOR_EACH_REALPLAYER(e)
832         {
833                 p += 1;
834                 if(e.ready)
835                         r += 1;
836         }
837
838         if(cvar("sv_ready_restart_nag")) {
839                 if(!readyNagActive) {
840                         readyNagger = spawn();
841                         readyNagger.think = readyNagger_Think;
842                         readyNagger.cnt = cvar("sv_ready_restart_nag_duration");
843                         readyNagger.nextthink = time;
844                         readyNagActive = 1;
845                 }
846         }
847
848         if(!p || r < p)
849                 return;
850
851         bprint("^1Server is restarting...\n");
852
853         // no arena, assault support yet...
854         if(g_arena | g_assault | gameover | intermission_running)
855                 localcmd("restart\n");
856
857         if(readyNagActive) { //if every player is ready, remove the ready-nagger again
858                 readyNagActive = 0;
859                 remove(readyNagger);
860         }
861
862         restart_countdown = time + RESTART_COUNTDOWN;
863         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
864         //reset the .ready status of all players (also spectators)
865         FOR_EACH_CLIENT(e)
866         {
867                 e.ready = 0;
868         }
869         if(0<cvar("timelimit"))
870         {
871                 // remember original timelimit on first restart
872                 if(!timelimit_orig)
873                         timelimit_orig = cvar("timelimit");
874                 cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
875         }
876         if(cvar("teamplay_lockonrestart") && teams_matter) {
877                 lockteams = 1;
878                 bprint("^1The teams are now locked.\n");
879         }
880         
881         //initiate the restart-countdown-announcer entity
882         restartAnnouncer = spawn();
883         restartAnnouncer.think = restartAnnouncer_Think;
884         restartAnnouncer.nextthink = time;
885         restartAnnouncer.cnt = RESTART_COUNTDOWN;
886
887         //play the prepareforbattle sound to everyone
888         sound(world, CHAN_AUTO, "announcer/robotic/prepareforbattle.wav", 1, ATTN_NONE);
889
890         //reset map immediately if this cvar is not set
891         if (!cvar("sv_ready_restart_after_countdown"))
892                 reset_map();
893         
894         if(cvar("sv_eventlog"))
895                 GameLogEcho(":restart", FALSE);
896 }
897
898 /**
899  * Centerprints the information to all players who didn't ready up yet to do so.
900  */
901 void readyNagger_Think() {
902         local entity plr;
903         if(self.cnt <= 0) { //have a break showing the ready nag
904                 //make sure that the old ready-nag-centerprint isn't shown too long:
905                 FOR_EACH_REALCLIENT(plr) {
906                         if(plr.classname == "player") {
907                                 if (!plr.ready)
908                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "");
909                         }
910                 }
911                 self.cnt = cvar("sv_ready_restart_nag_duration");
912                 self.nextthink = time + cvar("sv_ready_restart_nag_interval");
913         }
914         else {
915                 //show the ready nagging to all players who aren't ready yet
916                 FOR_EACH_REALCLIENT(plr) {
917                         if(plr.classname == "player") {
918                                 if (!plr.ready) {
919                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "^2Please ready up (F4 by default)!");
920                                         //play reminder sound once the centerprint appears for the first time after the pause:
921                                         if (self.cnt == cvar("sv_ready_restart_nag_duration"))
922                                                 play2(plr, "misc/talk2.wav");
923                                 }
924                         }
925                 }
926
927                 self.nextthink = time + 1;
928                 self.cnt -= 1;
929         }
930 }
931
932 /**
933  * Shows the restart countdown for all players.
934  * Plays the countdown sounds for the seconds 3, 2 1, begin for everyone.
935  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
936  * is set to 1).
937  */
938 void restartAnnouncer_Think() {
939         local entity plr;
940         local string s;
941         if(self.cnt <= 0) { //show the "Begin" message and
942                 if (cvar("sv_ready_restart_after_countdown")) {
943                         restart_mapalreadyrestarted = 1;
944                         reset_map();
945                 }
946
947                 FOR_EACH_REALCLIENT(plr) {
948                         if(plr.classname == "player") {
949                                 s = strcat(NEWLINES, "^1Begin!");
950                                 centerprint(plr, s);
951                         }
952                 }
953                 sound(world, CHAN_AUTO, "announcer/robotic/begin.wav", 1, ATTN_NONE);
954
955                 remove(self);
956                 return;
957         }
958         else {
959                 FOR_EACH_REALCLIENT(plr) {
960                         if(plr.classname == "player") {
961                                 s = strcat(NEWLINES, "^1Game starts in ", ftos(self.cnt), " seconds");
962                                 centerprint(plr, s);
963                         }
964                 }
965
966                 if(self.cnt <= 3) {
967                         sound(world, CHAN_AUTO, strcat("announcer/robotic/", ftos(self.cnt), ".ogg"), 1, ATTN_NONE);
968                 }
969                 self.nextthink = time + 1;
970                 self.cnt -= 1;
971         }
972 }