]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
git-svn-id: svn://svn.icculus.org/nexuiz/trunk@3661 f962a42d-fe04-0410-a3ab-8c8b0445ebaa
[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                 //when g_tourney is active and g_tourney_disable_spec_chat is set and game is in matchstage
127                 //don't print the text from specs/observers to the players but only the other spectators
128                 if(g_tourney && cvar("g_tourney_disable_spec_chat") && tourneyInMatchStage && source.classname != "player") {
129                         FOR_EACH_REALCLIENT(head) if(head.classname != "player") {
130                                 sprint(head, msgstr);
131                         }
132                 }
133                 else
134                         bprint(msgstr);
135                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
136         }
137
138         strunzone(msgstr);
139 }
140
141 float VoteCheckNasty(string cmd)
142 {
143         if(strstrofs(cmd, ";", 0) >= 0)
144                 return TRUE;
145         if(strstrofs(cmd, "\n", 0) >= 0)
146                 return TRUE;
147         if(strstrofs(cmd, "\r", 0) >= 0)
148                 return TRUE;
149         if(strstrofs(cmd, "$", 0) >= 0)
150                 return TRUE;
151         return FALSE;
152 }
153
154 string GetKickVoteVictim_newcommand;
155 string GetKickVoteVictim_reason;
156 entity GetKickVoteVictim(string vote, string cmd)
157 {
158         float tokens;
159         float i, n, t;
160         string ns;
161         entity e;
162
163         tokens = tokenize(vote);
164         ns = "";
165
166         if(tokens >= 2)
167                 if(substring(argv(1), 0, 1) == "#")
168                 {
169                         ns = substring(argv(1), 1, 999);
170                         t = 2;
171                 }
172
173         if(tokens >= 3)
174                 if(argv(1) == "#")
175                 {
176                         ns = argv(2);
177                         t = 3;
178                 }
179
180         if(ns != "")
181         {
182                 GetKickVoteVictim_reason = "";
183                 for(i = t; i < tokens; ++i)
184                         GetKickVoteVictim_reason = strcat(GetKickVoteVictim_reason, argv(i), " ");
185                 GetKickVoteVictim_reason = substring(GetKickVoteVictim_reason, 0, strlen(GetKickVoteVictim_reason) - 1);
186
187                 n = stof(ns);
188                 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
189                 {
190                         e = edict_num(n);
191                         if(clienttype(e) == CLIENTTYPE_REAL)
192                         {
193                                 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
194                                 return e;
195                         }
196                 }
197         }
198
199         sprint(self, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
200         return world;
201 }
202
203 void SV_ParseClientCommand(string s) {
204         local string cmd;
205         local entity e;
206         local float i;
207
208         tokenize(s);
209
210         if(argv(0) == "vote") {
211                 if(argv(1) == "help") {
212                         local string vmasterdis;
213                         if(!cvar("sv_vote_master")) {
214                                 vmasterdis = " ^1(disabled)";
215                         }
216                         local string vcalldis;
217                         if(!cvar("sv_vote_call")) {
218                                 vcalldis = " ^1(disabled)";
219                         }
220                         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");
221                         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");
222                         sprint(self, "^7\"^2help^7\" shows this info.\n");
223                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
224                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
225                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
226                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
227                         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");
228                         sprint(self, "^7\"^2yes^7\", \"^2no^7\" and \"^2dontcare^7\" to make your vote.\n");
229                         sprint(self, "^7If enough of the players vote yes the vote is accepted.\n");
230                         sprint(self, "^7If enough of the players vote no the vote is rejected.\n");
231                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
232                         sprint(self, "^7You can call a vote for or execute these commands:\n");
233                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
234                 } else if(argv(1) == "status") {
235                         if(votecalled) {
236                                 sprint(self, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", votecaller.netname, "^7.\n"));
237                         } else {
238                                 sprint(self, "^1No vote called.\n");
239                         }
240                 } else if(argv(1) == "call") {
241                         if(cvar("sv_vote_call")) {
242                                 if(tourneyInMatchStage && cvar("g_tourney_disable_spec_vote") && self.classname != "player") {
243                                         sprint(self, "^1Error: Only players can call a vote during the match-stage.\n");
244                                 }
245                                 else if(timeoutStatus) { //don't allow a vote call during a timeout
246                                         sprint(self, "^1Error: You can not call a vote while a timeout is active.\n");
247                                 }
248                                 else if(votecalled) {
249                                         sprint(self, "^1There is already a vote called.\n");
250                                 } else {
251                                         local string vote;
252                                         vote = VoteParse();
253                                         if(vote == "") {
254                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
255                                         } else if(time < self.vote_next) {
256                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
257                                         } else if(VoteCheckNasty(vote)) {
258                                                 sprint(self, "Syntax error in command.\n");
259                                         } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
260                                                 // remap chmap to gotomap (forces intermission)
261                                                 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
262                                                         return;
263                                                 if(substring(vote, 0, 6) == "chmap ")
264                                                         vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
265                                                 if(substring(vote, 0, 8) == "gotomap ")
266                                                 {
267                                                         if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8))))
268                                                                 return;
269                                                         vote = strcat("gotomap ", vote);
270                                                 }
271
272                                                 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
273                                                 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
274                                                 {
275                                                         if(!(e = GetKickVoteVictim(vote, "vcall")))
276                                                                 return;
277                                                         vote = GetKickVoteVictim_newcommand;
278                                                         votecalledvote_display = strzone(strcat("^1", vote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason));
279                                                 }
280                                                 else
281                                                 {
282                                                         votecalledvote_display = strzone(strcat("^1", vote));
283                                                 }
284                                                 votecalledvote = strzone(vote);
285                                                 votecalled = TRUE;
286                                                 votecalledmaster = FALSE;
287                                                 votecaller = self; // remember who called the vote
288                                                 votefinished = time + cvar("sv_vote_timeout");
289                                                 votecaller.vote_vote = 1; // of course you vote yes
290                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
291                                                 bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ", votecalledvote_display, "\n");
292                                                 VoteCount(); // needed if you are the only one
293                                         } else {
294                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
295                                         }
296                                 }
297                         } else {
298                                 sprint(self, "^1Vote calling is NOT allowed.\n");
299                         }
300                 } else if(argv(1) == "stop") {
301                         if(!votecalled) {
302                                 sprint(self, "^1No vote called.\n");
303                         } else if(self == votecaller) { // the votecaller can stop a vote
304                                 VoteStop(self);
305                         } else if(self.vote_master) { // masters can, too
306                                 VoteStop(self);
307                         } else {
308                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
309                         }
310                 } else if(argv(1) == "master") {
311                         if(cvar("sv_vote_master")) {
312                                 if(votecalled) {
313                                         sprint(self, "^1There is already a vote called.\n");
314                                 } else {
315                                         votecalled = TRUE;
316                                         votecalledmaster = TRUE;
317                                         votecalledvote = strzone("XXX");
318                                         votecalledvote_display = strzone("^3master");
319                                         votecaller = self; // remember who called the vote
320                                         votefinished = time + cvar("sv_vote_timeout");
321                                         votecaller.vote_vote = 1; // of course you vote yes
322                                         votecaller.vote_next = time + cvar("sv_vote_wait");
323                                         bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n");
324                                         VoteCount(); // needed if you are the only one
325                                 }
326                         } else {
327                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
328                         }
329                 } else if(argv(1) == "do") {
330                         if(argv(2) == "login") {
331                                 local string masterpwd;
332                                 masterpwd = cvar_string("sv_vote_master_password");
333                                 if(masterpwd != "") {
334                                         self.vote_master = (masterpwd == argv(3));
335                                         if(self.vote_master) {
336                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
337                                                 bprint("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n");
338                                         }
339                                         else
340                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
341                                 }
342                                 else
343                                         sprint(self, "^1You are NOT a master.\n");
344                         } else if(self.vote_master) {
345                                 local string dovote, dovote_display;
346                                 dovote = VoteParse();
347                                 if(dovote == "") {
348                                         sprint(self, "^1Your command was empty. See help for more info.\n");
349                                 } else if(VoteCheckNasty(dovote)) {
350                                         sprint(self, "Syntax error in command.\n");
351                                 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
352                                         if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
353                                                 return;
354                                         if(substring(dovote, 0, 6) == "chmap ")
355                                                 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
356                                         if(substring(dovote, 0, 8) == "gotomap ")
357                                         {
358                                                 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8))))
359                                                         return;
360                                                 dovote = strcat("gotomap ", dovote);
361                                         }
362
363                                         dovote_display = dovote;
364                                         if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
365                                         {
366                                                 if(!(e = GetKickVoteVictim(dovote, "vdo")))
367                                                         return;
368                                                 dovote = GetKickVoteVictim_newcommand;
369                                                 dovote_display = strcat("^1", dovote, " (^7", e.netname, "^1): ", GetKickVoteVictim_reason);
370                                         }
371                                         bprint("\{1}^2* ^3", self.netname, "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
372                                         localcmd(strcat(dovote, "\n"));
373                                 } else {
374                                         sprint(self, "^1This command is not ok. See help for more info.\n");
375                                 }
376                         } else {
377                                 sprint(self, "^1You are NOT a master.\n");
378                         }
379                 } else if(argv(1) == "yes") {
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 accepted 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) == "no") {
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 rejected the vote.\n");
399                                 self.vote_vote = -1;
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 if(argv(1) == "abstain" || argv(1) == "dontcare") {
408                         if(!votecalled) {
409                                 sprint(self, "^1No vote called.\n");
410                         } else if(self.vote_vote == 0
411                                   || cvar("sv_vote_change")) {
412                                 sprint(self, "^1You abstained from your vote.\n");
413                                 self.vote_vote = -2;
414                                 centerprint_expire(self, CENTERPRIO_VOTE);
415                                 if(!cvar("sv_vote_singlecount")) {
416                                         VoteCount();
417                                 }
418                         } else {
419                                 sprint(self, "^1You have already voted.\n");
420                         }
421                 } else {
422                         // ignore this?
423                         sprint(self, "^1Unknown vote command.\n");
424                 }
425         } else if(argv(0) == "autoswitch") {
426                 // be backwards compatible with older clients (enabled)
427                 self.autoswitch = ("0" != argv(1));
428                 local string autoswitchmsg;
429                 if (self.autoswitch) {
430                         autoswitchmsg = "on";
431                 } else {
432                         autoswitchmsg = "off";
433                 }
434                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
435         } else if(argv(0) == "clientversion") {
436                 if (argv(1) == "$gameversion") {
437                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
438                         // either that or someone wants to be funny
439                         self.version = 1;
440                 } else {
441                         self.version = stof(argv(1));
442                 }
443                 if(self.version != cvar("gameversion"))
444                 {
445                         self.classname = "observer";
446                         self.frags = -2;
447                         PutClientInServer();
448                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
449                         //JoinBestTeam(self, FALSE, TRUE);
450                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
451                         self.classname = "observer";
452                         stuffcmd(self,"menu_showteamselect\n");
453                 }
454         } else if(argv(0) == "reportcvar") { // old system
455                 GetCvars(1);
456         } else if(argv(0) == "sentcvar") { // new system
457                 GetCvars(1);
458         } else if(argv(0) == "spectate") {
459                 if(g_lms || g_arena)
460                         return; // don't allow spectating in lms, unless player runs out of lives
461                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
462                         if(self.flagcarried)
463                                 DropFlag(self.flagcarried);
464                         kh_Key_DropAll(self, TRUE);
465                         WaypointSprite_PlayerDead();
466                         DistributeFragsAmongTeam(self, self.team, 1.0);
467                         self.classname = "observer";
468                         if(blockSpectators)
469                                 sprint(self, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));
470                         PutClientInServer();
471                 }
472         } else if(argv(0) == "join") {
473                 if(!g_arena)
474                 if (self.classname != "player" && !lockteams)
475                 {
476                         if(isJoinAllowed()) {
477                                 self.classname = "player";
478                                 self.frags = 0;
479                                 bprint ("^4", self.netname, "^4 is playing now\n");
480                                 PutClientInServer();
481                         }
482                         else {
483                                 //player may not join because of g_maxplayers is set
484                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
485                         }
486                 }
487         } else if( argv(0) == "selectteam" ) {
488                 if( !cvar("teamplay") ) {
489                         sprint( self, "selecteam can only be used in teamgames\n");
490                 } else if(cvar("g_campaign")) {
491                         //JoinBestTeam(self, 0);
492                 } else if(lockteams) {
493                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
494                 } else if( argv(1) == "red" ) {
495                         DoTeamChange(COLOR_TEAM1);
496                 } else if( argv(1) == "blue" ) {
497                         DoTeamChange(COLOR_TEAM2);
498                 } else if( argv(1) == "yellow" ) {
499                         DoTeamChange(COLOR_TEAM3);
500                 } else if( argv(1) == "pink" ) {
501                         DoTeamChange(COLOR_TEAM4);
502                 } else if( argv(1) == "auto" ) {
503                         DoTeamChange(-1);
504                 } else {
505                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
506                 }
507         } else if(argv(0) == "ready") {
508                 if(cvar("sv_ready_restart"))
509                 {
510                         if(timeoutStatus) {
511                                 return sprint(self, "^1You cannot reset the game while a timeout is active!\n");
512                         }
513                         
514                         if(!restart_countdown || cvar("sv_ready_restart_repeatable"))
515                         {
516                                 self.ready = TRUE;
517                                 bprint(self.netname, "^2 is ready\n");
518                                 ReadyCount();
519                         } else {
520                                 sprint(self, "^1game has already been restarted\n");
521                         }
522                 }
523         } else if(argv(0) == "maplist") {
524                 local float n;
525                 local string col;
526                 n = tokenize(cvar_string("g_maplist"));
527                 sprint(self, "^7Maps in list: ");
528                 for(i = 0; i < n; ++i)
529                 {
530                         if(mod(i, 2))
531                                 col = "^2";
532                         else
533                                 col = "^3";
534                         sprint(self, strcat(col, argv(i), " "));
535                 }
536                 sprint(self, "\n");
537 #ifdef MAPINFO
538         } else if(argv(0) == "lsmaps") {
539                 sprint(self, "^7Maps available: ");
540                 for(i = 0; i < MapInfo_count; ++i)
541                 {
542                         if(mod(i, 2))
543                                 col = "^2";
544                         else
545                                 col = "^3";
546                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
547                 }
548                 sprint(self, "\n");
549 #endif
550         } else if(argv(0) == "teamstatus") {
551                 PrintScoreboard(self);
552         } else if(argv(0) == "voice") {
553                 VoiceMessage(argv(1));
554         } else if(argv(0) == "say") {
555                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
556                 //clientcommand(self, formatmessage(s));
557         } else if(argv(0) == "say_team") {
558                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
559                 //clientcommand(self, formatmessage(s));
560         } else if(argv(0) == "info") {
561                 cmd = cvar_string(strcat("sv_info_", argv(1)));
562                 if(cmd == "")
563                         sprint(self, "ERROR: unsupported info command\n");
564                 else
565                         wordwrap_sprint(cmd, 1111);
566         } else if(argv(0) == "suggestmap") {
567                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
568         } else if(argv(0) == "calltimeout") {
569                 if(cvar("sv_timeout")) {
570                         if(self.classname == "player") {
571                                 if(votecalled)
572                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
573                                 else
574                                         evaluateTimeoutCall();
575                         }
576                         else
577                                 sprint(self, "^7Error: only players can call a timeout!\n");
578                 }
579         } else if(argv(0) == "resumegame") {
580                 if(cvar("sv_timeout")) {
581                         evaluateResumeGame();
582                 }
583         } else {
584                 cmd = argv(0);
585                 /* checks not needed any more since DP has separated clientcommands and regular commands
586                 if(cmd != "status")
587                 if(cmd != "name")
588                 //if(cmd != "say") // handled above
589                 //if(cmd != "say_team") // handled above
590                 if(cmd != "tell")
591                 if(cmd != "color")
592                 if(cmd != "kill")
593                 if(cmd != "pause")
594                 if(cmd != "kick")
595                 if(cmd != "ping")
596                 if(cmd != "pings")
597                 if(cmd != "ban")
598                 if(cmd != "pmodel")
599                 if(cmd != "rate")
600                 if(cmd != "playermodel")
601                 if(cmd != "playerskin")
602                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
603                 {
604                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
605                         return;
606                 }
607                 */
608                 clientcommand(self,s);
609         }
610 }
611
612 string ValidateMap(string m)
613 {
614 #ifdef MAPINFO
615         m = MapInfo_FixName(m);
616         if(!m)
617         {
618                 sprint(self, "This map is not available on this server.\n");
619                 return string_null;
620         }
621 #else
622         if(!cvar("sv_vote_change_gametype"))
623                 if(!IsSameGametype(m))
624                 {
625                         sprint(self, "This server does not allow changing the game type by map votes.\n");
626                         return string_null;
627                 }
628 #endif
629         if(!cvar("sv_vote_override_mostrecent"))
630                 if(Map_IsRecent(m))
631                 {
632                         sprint(self, "This server does not allow for recent maps to be played again. Please be patient for some rounds.\n");
633                         return string_null;
634                 }
635 #ifdef MAPINFO
636         if(!MapInfo_CheckMap(m))
637         {
638                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode.\n"));
639                 return string_null;
640         }
641 #else
642         if(!TryFile(strcat("maps/", m, ".mapcfg")))
643         {
644                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server.\n"));
645                 return string_null;
646         }
647 #endif
648
649         return m;
650 }
651
652
653 void VoteThink() {
654         if(votefinished > 0 // a vote was called
655             && time > votefinished) // time is up
656         {
657                 VoteCount();
658         }
659 }
660
661 string VoteParse() {
662         local float index;
663         index = 3;
664         local string vote;
665         vote = argv(2);
666         while(argv(index) != "") {
667                 vote = strcat(vote, " ", argv(index));
668                 index++;
669         }
670
671         // necessary for some of the string operations
672         vote = strzone(vote);
673
674         // now we remove some things that could be misused
675         index = 0;
676         local float found;
677         found = FALSE;
678         local float votelength;
679         votelength = strlen(vote);
680         while(!found && index < votelength)
681         {
682                 local string badchar;
683                 badchar = substring(vote, index, 1);
684                 if(badchar == ";"
685                    || badchar == "\r"
686                    || badchar == "\n")
687                 {
688                         found = TRUE;
689                 } else {
690                         index++;
691                 }
692         }
693         return substring(vote, 0, index);
694 }
695
696 float VoteAllowed(string votecommand) {
697         tokenize(cvar_string("sv_vote_commands"));
698         local float index;
699         index = 0;
700         while(argv(index) != "") {
701                 local string allowed;
702                 allowed = argv(index);
703                 if(votecommand == allowed) {
704                         return TRUE;
705                 }
706                 index++;
707         }
708         return FALSE;
709 }
710
711 void VoteReset() {
712         local entity player;
713
714         FOR_EACH_CLIENT(player)
715         {
716                 player.vote_vote = 0;
717                 centerprint_expire(player, CENTERPRIO_VOTE);
718         }
719
720         if(votecalled)
721         {
722                 strunzone(votecalledvote);
723                 strunzone(votecalledvote_display);
724         }
725
726         votecalled = FALSE;
727         votecalledmaster = FALSE;
728         votefinished = 0;
729 }
730
731 void VoteAccept() {
732         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
733         if(votecalledmaster)
734         {
735                 votecaller.vote_master = 1;
736         } else {
737                 //in g_tourney mode and if the vote is a timelimit-change, don't change it immediately but after restart
738                 if(cvar("g_tourney") && substring(votecalledvote, 0, 10) == "timelimit ") {
739                         if( stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10)) > 0 ) {
740                                 timelimit_orig = stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10));
741                                 bprint(strcat("The timelimit will be set to ", ftos(timelimit_orig), " minutes after the next restart!\n"));
742                         }
743                         else //calls like "timelimit -1" can pass immediately
744                                 localcmd(strcat(votecalledvote, "\n"));
745                 }
746                 else
747                         localcmd(strcat(votecalledvote, "\n"));
748         }
749         votecaller.vote_next = 0; // people like your votes, no wait for next vote
750         VoteReset();
751 }
752
753 void VoteReject() {
754         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
755         VoteReset();
756 }
757
758 void VoteTimeout() {
759         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 timed out\n");
760         VoteReset();
761 }
762
763 void VoteStop(entity stopper) {
764         bprint("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n");
765         if(stopper == votecaller) {
766                 // no wait for next vote so you can correct your vote
767                 votecaller.vote_next = 0;
768         }
769         VoteReset();
770 }
771
772 void VoteNag() {
773         if(votecalled)
774                 if(self.vote_vote == 0)
775                         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."));
776 }
777
778 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount)
779 {
780         string s;
781         if(mincount >= 0)
782         {
783                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
784                 s = strcat(s, ftos(nocount), "^2 (^1");
785                 s = strcat(s, ftos(mincount), "^2 needed), ^1");
786                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
787                 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
788         }
789         else
790         {
791                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
792                 s = strcat(s, ftos(nocount), "^2 (^1");
793                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
794                 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
795         }
796         bprint(s);
797 }
798
799 void VoteCount() {
800         local float playercount;
801         playercount = 0;
802         local float yescount;
803         yescount = 0;
804         local float nocount;
805         nocount = 0;
806         local float abstaincount;
807         abstaincount = 0;
808         local entity player;
809         //same for real players
810         local float realplayercount;
811         local float realplayeryescount;
812         local float realplayernocount;
813         local float realplayerabstaincount;
814         realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
815
816         FOR_EACH_REALCLIENT(player)
817         {
818                 if(player.vote_vote == -1) {
819                         nocount++;
820                 } else if(player.vote_vote == 1) {
821                         yescount++;
822                 } else if(player.vote_vote == -2) {
823                         abstaincount++;
824                 }
825                 playercount++;
826                 //do the same for real players
827                 if(player.classname == "player") {
828                         if(player.vote_vote == -1) {
829                                 realplayernocount++;
830                         } else if(player.vote_vote == 1) {
831                                 realplayeryescount++;
832                         } else if(player.vote_vote == -2) {
833                                 realplayerabstaincount++;
834                         }
835                         realplayercount++;
836                 }
837         }
838
839         //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)
840         if( cvar("g_tourney") && (realplayercount > 0) ) {
841                 yescount = realplayeryescount;
842                 nocount = realplayernocount;
843                 abstaincount = realplayerabstaincount;
844                 playercount = realplayercount;
845         }
846
847
848         if((playercount == 1) && votecalledmaster) {
849                 // if only one player is on the server becoming vote
850                 // master is not allowed.  This could be used for
851                 // trolling or worse. 'self' is the user who has
852                 // called the vote because this function is called
853                 // by SV_ParseClientCommand. Maybe all voting should
854                 // be disabled for a single player?
855                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
856                 votecaller.vote_next = 0;
857                 VoteReset();
858         } else {
859                 float votefactor;
860                 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
861                 if(yescount > (playercount - abstaincount) * votefactor)
862                 {
863                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
864                         VoteAccept();
865                 }
866                 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
867                 {
868                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
869                         VoteReject();
870                 }
871                 else if(time > votefinished)
872                 {
873                         if(cvar("sv_vote_simple_majority"))
874                         {
875                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1);
876                                 if(yescount > (yescount + nocount) * votefactor)
877                                         VoteAccept();
878                                 else if(yescount + nocount > 0)
879                                         VoteReject();
880                                 else
881                                         VoteTimeout();
882                         }
883                         else
884                         {
885                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1);
886                                 VoteTimeout();
887                         }
888                 }
889         }
890 }
891
892 /**
893  * Counts how many players are ready. If not enough players are ready, the function
894  * does nothing. If all players are ready, the timelimit will be extended and the
895  * restart_countdown variable is set to allow other functions like PlayerPostThink
896  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
897  * is not set the map will be resetted.
898  * 
899  * Function is called after the server receives a 'ready' sign from a player.
900  */
901 void ReadyCount()
902 {
903         local entity e;
904         local float r, p;
905
906         FOR_EACH_REALPLAYER(e)
907         {
908                 p += 1;
909                 if(e.ready)
910                         r += 1;
911         }
912
913         if(cvar("sv_ready_restart_nag")) {
914                 if(!readyNagActive) {
915                         readyNagger = spawn();
916                         readyNagger.think = readyNagger_Think;
917                         readyNagger.cnt = cvar("sv_ready_restart_nag_duration");
918                         readyNagger.nextthink = time;
919                         readyNagActive = 1;
920                 }
921         }
922
923         if(!p || r < p)
924                 return;
925
926         bprint("^1Server is restarting...\n");
927
928         // no arena, assault support yet...
929         if(g_arena | g_assault | gameover | intermission_running)
930                 localcmd("restart\n");
931
932         if(readyNagActive) { //if every player is ready, remove the ready-nagger again
933                 readyNagActive = 0;
934                 remove(readyNagger);
935         }
936
937         if(g_tourney) {
938                 tourneyInMatchStage = 1; //once the game is restarted the game is in match stage
939                 //reset weapons and ammo, health and armor to default:
940                 start_items = IT_LASER | IT_SHOTGUN;
941                 start_switchweapon = WEP_SHOTGUN;
942                 start_ammo_shells = cvar("g_start_ammo_shells");
943                 start_ammo_nails = cvar("g_start_ammo_nails");
944                 start_ammo_rockets = cvar("g_start_ammo_rockets");
945                 start_ammo_cells = cvar("g_start_ammo_cells");
946                 start_health = cvar("g_balance_health_start");
947                 start_armorvalue = cvar("g_balance_armor_start");
948         }
949
950         restart_countdown = time + RESTART_COUNTDOWN;
951         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
952         //reset the .ready status of all players (also spectators)
953         FOR_EACH_CLIENT(e)
954         {
955                 e.ready = 0;
956         }
957         if(0<cvar("timelimit") || (g_tourney && cvar("g_tourney_warmup_unlimited_time")) )
958         {
959                 // remember original timelimit on first restart
960                 if(!timelimit_orig)
961                         timelimit_orig = cvar("timelimit");
962                 //only set the new timelimit if, when loading the map, a timelimit was really set
963                 if(timelimit_orig)
964                         cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
965         }
966         if(cvar("teamplay_lockonrestart") && teams_matter) {
967                 lockteams = 1;
968                 bprint("^1The teams are now locked.\n");
969         }
970         
971         //initiate the restart-countdown-announcer entity
972         restartAnnouncer = spawn();
973         restartAnnouncer.think = restartAnnouncer_Think;
974         restartAnnouncer.nextthink = time;
975         restartAnnouncer.cnt = RESTART_COUNTDOWN;
976         
977         //after a restart every players number of allowed timeouts gets reset, too
978         if(cvar("sv_timeout"))
979         {
980                 FOR_EACH_REALPLAYER(e)
981                 {
982                         e.allowedTimeouts = cvar("sv_timeout_number");
983                 }
984         }
985
986         //play the prepareforbattle sound to everyone
987         sound(world, CHAN_AUTO, "announcer/robotic/prepareforbattle.wav", 1, ATTN_NONE);
988
989         //reset map immediately if this cvar is not set
990         if (!cvar("sv_ready_restart_after_countdown"))
991                 reset_map();
992         
993         if(cvar("sv_eventlog"))
994                 GameLogEcho(":restart", FALSE);
995 }
996
997 /**
998  * Centerprints the information to all players who didn't ready up yet to do so.
999  */
1000 void readyNagger_Think() {
1001         local entity plr;
1002         if(self.cnt <= 0) { //have a break showing the ready nag
1003                 //make sure that the old ready-nag-centerprint isn't shown too long:
1004                 FOR_EACH_REALCLIENT(plr) {
1005                         if(plr.classname == "player") {
1006                                 if (!plr.ready)
1007                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "");
1008                         }
1009                 }
1010                 self.cnt = cvar("sv_ready_restart_nag_duration");
1011                 self.nextthink = time + cvar("sv_ready_restart_nag_interval");
1012         }
1013         else {
1014                 //show the ready nagging to all players who aren't ready yet
1015                 FOR_EACH_REALCLIENT(plr) {
1016                         if(plr.classname == "player") {
1017                                 if (!plr.ready) {
1018                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "^2Please ready up (F4 by default)!");
1019                                         //play reminder sound once the centerprint appears for the first time after the pause:
1020                                         if (self.cnt == cvar("sv_ready_restart_nag_duration"))
1021                                                 play2(plr, "misc/talk2.wav");
1022                                 }
1023                         }
1024                 }
1025
1026                 self.nextthink = time + 1;
1027                 self.cnt -= 1;
1028         }
1029 }
1030
1031 /**
1032  * Shows the restart countdown for all players.
1033  * Plays the countdown sounds for the seconds 3, 2 1, begin for everyone.
1034  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
1035  * is set to 1).
1036  */
1037 void restartAnnouncer_Think() {
1038         local entity plr;
1039         local string s;
1040         if(self.cnt <= 0) { //show the "Begin" message and
1041                 if (cvar("sv_ready_restart_after_countdown")) {
1042                         restart_mapalreadyrestarted = 1;
1043                         reset_map();
1044                 }
1045
1046                 FOR_EACH_REALCLIENT(plr) {
1047                         if(plr.classname == "player") {
1048                                 s = strcat(NEWLINES, "^1Begin!");
1049                                 centerprint(plr, s);
1050                         }
1051                 }
1052                 sound(world, CHAN_AUTO, "announcer/robotic/begin.wav", 1, ATTN_NONE);
1053
1054                 remove(self);
1055                 return;
1056         }
1057         else {
1058                 FOR_EACH_REALCLIENT(plr) {
1059                         if(plr.classname == "player") {
1060                                 s = strcat(NEWLINES, "^1Game starts in ", ftos(self.cnt), " seconds");
1061                                 centerprint(plr, s);
1062                         }
1063                 }
1064
1065                 if(self.cnt <= 3) {
1066                         sound(world, CHAN_AUTO, strcat("announcer/robotic/", ftos(self.cnt), ".ogg"), 1, ATTN_NONE);
1067                 }
1068                 self.nextthink = time + 1;
1069                 self.cnt -= 1;
1070         }
1071 }
1072
1073 /**
1074  * Checks whether the player who calls the timeout is allowed to do so.
1075  * If so, it initializes the timeout countdown. It also checks whether another
1076  * timeout was already running at this time and reacts correspondingly.
1077  *
1078  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
1079  *                          timeoutInitiator, timeoutStatus, timeoutHandler
1080  *
1081  * This function is called when a player issues the calltimeout command.
1082  */
1083 void evaluateTimeoutCall() {
1084         if (g_tourney && !tourneyInMatchStage && !cvar("g_tourney_warmup_allow_timeout"))
1085                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
1086         if (time < restart_countdown )
1087                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
1088         if (timeoutStatus != 2) {
1089                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
1090                 if (cvar("timelimit")) {
1091                         //a timelimit was used
1092                         local float myTl;
1093                         if (cvar("timelimit"))
1094                                 myTl = cvar("timelimit");
1095                         else
1096                                 myTl = timelimit_orig;
1097
1098                         local float lastPossibleTimeout;
1099                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
1100
1101                         if (lastPossibleTimeout < time)
1102                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
1103                 }
1104         }
1105         //player may not call a timeout if he has no calls left
1106         if (self.allowedTimeouts < 1)
1107                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
1108         //now all required checks are passed
1109         self.allowedTimeouts -= 1;
1110         bprint(self.netname, " ^7called a timeout (", ftos(self.allowedTimeouts), " timeouts left)!\n"); //write a bprint who started the timeout (and how many he has left)
1111         remainingTimeoutTime = cvar("sv_timeout_length");
1112         remainingLeadTime = cvar("sv_timeout_leadtime");
1113         timeoutInitiator = self;
1114         if (timeoutStatus == 0) { //if another timeout was already active, don't change its status (which was 1 or 2) to 1, only change it to 1 if no timeout was active yet
1115                 timeoutStatus = 1;
1116                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
1117                 timeoutHandler = spawn();
1118                 timeoutHandler.think = timeoutHandler_Think;
1119         }
1120         timeoutHandler.nextthink = time; //always let the entity think asap
1121
1122         //inform all connected clients about the timeout call
1123         sound(world, CHAN_AUTO, "announcer/robotic/timeoutcalled.wav", 1, ATTN_NONE);
1124 }
1125
1126 /**
1127  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
1128  * and the lead time for the timeout is still active, this countdown just will be aborted (the
1129  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
1130  * value of the cvar sv_timeout_resumetime.
1131  *
1132  * This function is called when a player issues the resumegame command.
1133  */
1134 void evaluateResumeGame() {
1135         if (!timeoutStatus)
1136                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
1137         if (self != timeoutInitiator)
1138                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
1139         if (timeoutStatus == 1) {
1140                 remainingTimeoutTime = timeoutStatus = 0;
1141                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
1142                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
1143         }
1144         else if (timeoutStatus == 2) {
1145                 //only shorten the remainingTimeoutTime if it makes sense
1146                 if( remainingTimeoutTime > (cvar("sv_timeout_resumetime") + 1) ) {
1147                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
1148                         remainingTimeoutTime = cvar("sv_timeout_resumetime");
1149                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
1150                 }
1151                 else
1152                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
1153
1154         }
1155 }