game mode "keyhunt", still in testing
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 float 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         colorstr = Team_ColorCode(source.team);
24
25         if(!teams_matter)
26                 teamsay = FALSE;
27
28         if(intermission_running)
29                 teamsay = FALSE;
30
31         if(source.classname != "player") // observers can't
32                 teamsay = FALSE;
33
34         /*
35          * using bprint solves this... me stupid
36         // how can we prevent the message from appearing in a listen server?
37         // for now, just give "say" back and only handle say_team
38         if(!teamsay)
39         {
40                 clientcommand(self, strcat("say ", msgin));
41                 return;
42         }
43         */
44
45         if(cvar("g_chat_teamcolors"))
46                 namestr = playername(source);
47         else
48                 namestr = source.netname;
49         if(teamsay)
50         {
51                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
52                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
53         }
54         else
55                 msgstr = strzone(strcat("\{1}^3", namestr, "^7: ", msgin, "\n"));
56
57         // FLOOD CONTROL
58         flood = 0;
59         {
60                 float flood_spl;
61                 float flood_burst;
62                 float flood_lmax;
63                 var .float flood_field;
64                 float lines;
65                 if(teamsay)
66                 {
67                         flood_spl = cvar("g_chat_flood_spl_team");
68                         flood_burst = cvar("g_chat_flood_burst_team");
69                         flood_lmax = cvar("g_chat_flood_lmax_team");
70                         flood_field = floodcontrol_team;
71                 }
72                 else
73                 {
74                         flood_spl = cvar("g_chat_flood_spl");
75                         flood_burst = cvar("g_chat_flood_burst");
76                         flood_lmax = cvar("g_chat_flood_lmax");
77                         flood_field = floodcontrol_chat;
78                 }
79                 flood_burst = max(0, flood_burst - 1);
80                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
81                 lines = ceil(strlennocol(msgstr) / 75);
82                 if(flood_lmax && lines > flood_lmax)
83                         flood = 2;
84                 else if(time >= self.flood_field)
85                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
86                 else
87                         flood = 1;
88         }
89
90         if(flood)
91         {
92                 if(cvar("g_chat_flood_notify_flooder"))
93                 {
94                         if(flood == 1)
95                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
96                         else if(flood == 2)
97                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
98                 }
99                 else
100                         sprint(self, msgstr);
101                 ServerConsoleEcho(strcat("NOTE: ", playername(self), "^7 is flooding."), TRUE);
102         }
103         else if(teamsay)
104         {
105                 FOR_EACH_REALPLAYER(head)
106                 {
107                         if(head.team == source.team)
108                         {
109                                 sprint(head, msgstr);
110                                 centerprint(head, cmsgstr);
111                         }
112                 }
113         }
114         else
115         {
116                 bprint(msgstr);
117                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
118         }
119
120         strunzone(msgstr);
121 }
122
123 void SV_ParseClientCommand(string s) {
124         local string cmd;
125
126         tokenize(s);
127
128         if(argv(0) == "vote") {
129                 if(argv(1) == "help") {
130                         local string vmasterdis;
131                         if(!cvar("sv_vote_master")) {
132                                 vmasterdis = " ^1(disabled)";
133                         }
134                         local string vcalldis;
135                         if(!cvar("sv_vote_call")) {
136                                 vcalldis = " ^1(disabled)";
137                         }
138                         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\".\n");
139                         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\".\n");
140                         sprint(self, "^7\"^2help^7\" shows this info.\n");
141                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
142                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
143                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
144                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
145                         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");
146                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
147                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
148                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
149                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
150                         sprint(self, "^7You can call a vote for or execute these commands:\n");
151                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
152                 } else if(argv(1) == "status") {
153                         if(votecalled) {
154                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
155                         } else {
156                                 sprint(self, "^1No vote called.\n");
157                         }
158                 } else if(argv(1) == "call") {
159                         if(cvar("sv_vote_call")) {
160                                 if(votecalled) {
161                                         sprint(self, "^1There is already a vote called.\n");
162                                 } else {
163                                         local string vote;
164                                         vote = VoteParse();
165                                         if(vote == "") {
166                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
167                                         } else if(time < self.vote_next) {
168                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
169                                         } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
170                                                 if(!ValidateMap(vote))
171                                                         return;
172                                                 votecalled = TRUE;
173                                                 votecalledmaster = FALSE;
174                                                 // remap chmap to gotomap (forces intermission)
175                                                 if(strlen(vote) >= 6)
176                                                         if(substring(vote, 0, 6) == "chmap ")
177                                                                 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
178                                                 votecalledvote = strzone(vote);
179                                                 votecaller = self; // remember who called the vote
180                                                 votefinished = time + cvar("sv_vote_timeout");
181                                                 votecaller.vote_vote = 1; // of course you vote yes
182                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
183                                                 bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ^1", votecalledvote, "\n");
184                                                 VoteCount(); // needed if you are the only one
185                                         } else {
186                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
187                                         }
188                                 }
189                         } else {
190                                 sprint(self, "^1Vote calling is NOT allowed.\n");
191                         }
192                 } else if(argv(1) == "stop") {
193                         if(!votecalled) {
194                                 sprint(self, "^1No vote called.\n");
195                         } else if(self == votecaller) { // the votecaller can stop a vote
196                                 VoteStop(self);
197                         } else {
198                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
199                         }
200                 } else if(argv(1) == "master") {
201                         if(cvar("sv_vote_master")) {
202                                 if(votecalled) {
203                                         sprint(self, "^1There is already a vote called.\n");
204                                 } else {
205                                         votecalled = TRUE;
206                                         votecalledmaster = TRUE;
207                                         votecalledvote = strzone("^3master");
208                                         votecaller = self; // remember who called the vote
209                                         votefinished = time + cvar("sv_vote_timeout");
210                                         votecaller.vote_vote = 1; // of course you vote yes
211                                         votecaller.vote_next = time + cvar("sv_vote_wait");
212                                         bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n");
213                                         VoteCount(); // needed if you are the only one
214                                 }
215                         } else {
216                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
217                         }
218                 } else if(argv(1) == "do") {
219                         if(argv(2) == "login") {
220                                 local string masterpwd;
221                                 masterpwd = cvar_string("sv_vote_master_password");
222                                 if(masterpwd != "") {
223                                         self.vote_master = (masterpwd == argv(3));
224                                         if(self.vote_master) {
225                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
226                                                 bprint("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n");
227                                         }
228                                         else
229                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
230                                 }
231                                 else
232                                         sprint(self, "^1You are NOT a master.\n");
233                         } else if(self.vote_master) {
234                                 local string dovote;
235                                 dovote = VoteParse();
236                                 if(dovote == "") {
237                                         sprint(self, "^1Your command was empty. See help for more info.\n");
238                                 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
239                                         if(!ValidateMap(dovote))
240                                                 return;
241                                         // remap chmap to gotomap (forces intermission)
242                                         if(strlen(dovote) >= 6)
243                                                 if(substring(dovote, 0, 6) == "chmap ")
244                                                         vote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
245                                         bprint("\{1}^2* ^3", self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n");
246                                         localcmd(strcat(dovote, "\n"));
247                                 } else {
248                                         sprint(self, "^1This command is not ok. See help for more info.\n");
249                                 }
250                         } else {
251                                 sprint(self, "^1You are NOT a master.\n");
252                         }
253                 } else if(argv(1) == "yes") {
254                         if(!votecalled) {
255                                 sprint(self, "^1No vote called.\n");
256                         } else if(self.vote_vote == 0
257                                   || cvar("sv_vote_change")) {
258                                 sprint(self, "^1You accepted the vote.\n");
259                                 self.vote_vote = 1;
260                                 if(!cvar("sv_vote_singlecount")) {
261                                         VoteCount();
262                                 }
263                         } else {
264                                 sprint(self, "^1You have already voted.\n");
265                         }
266                 } else if(argv(1) == "no") {
267                         if(!votecalled) {
268                                 sprint(self, "^1No vote called.\n");
269                         } else if(self.vote_vote == 0
270                                   || cvar("sv_vote_change")) {
271                                 sprint(self, "^1You rejected the vote.\n");
272                                 self.vote_vote = -1;
273                                 if(!cvar("sv_vote_singlecount")) {
274                                         VoteCount();
275                                 }
276                         } else {
277                                 sprint(self, "^1You have already voted.\n");
278                         }
279                 } else {
280                         // ignore this?
281                         sprint(self, "^1Unknown vote command.\n");
282                 }
283         } else if(argv(0) == "autoswitch") {
284                 // be backwards compatible with older clients (enabled)
285                 self.autoswitch = ("0" != argv(1));
286                 local string autoswitchmsg;
287                 if (self.autoswitch) {
288                         autoswitchmsg = "on";
289                 } else {
290                         autoswitchmsg = "off";
291                 }
292                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
293         } else if(argv(0) == "clientversion") {
294                 if (argv(1) == "$gameversion") {
295                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
296                         // either that or someone wants to be funny
297                         self.version = 1;
298                 } else {
299                         self.version = stof(argv(1));
300                 }
301                 if(self.version != cvar("gameversion")) 
302                 {
303                         self.classname = "observer";
304                         self.frags = -2;
305                         PutClientInServer();
306                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
307                         //JoinBestTeam(self, 0);
308                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
309                         self.classname = "observer";
310                         stuffcmd(self,"menu_showteamselect\n");
311                 }
312         } else if(argv(0) == "reportcvar") { // old system
313                 GetCvars(1);
314         } else if(argv(0) == "sentcvar") { // new system
315                 GetCvars(1);
316         } else if(argv(0) == "spectate") {
317                 if(cvar("g_lms") || cvar("g_arena"))
318                         return; // don't allow spectating in lms, unless player runs out of lives
319                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
320                         if(self.flagcarried)
321                                 DropFlag(self.flagcarried);
322                         kh_Key_DropAll(self);
323                         WaypointSprite_PlayerDead();
324                         DistributeFragsAmongTeam(self, self.team, 1.0);
325                         self.classname = "observer";
326                         PutClientInServer();
327                 }
328         } else if(argv(0) == "join") {
329                 if(!cvar("g_arena"))
330                 if (self.classname != "player")
331                 {
332                         self.classname = "player";
333                         self.frags = 0;
334                         bprint ("^4", self.netname, "^4 is playing now\n");
335                         PutClientInServer();
336                 }
337         } else if( argv(0) == "selectteam" ) {
338                 if( !cvar("teamplay") ) {
339                         sprint( self, "selecteam can only be used in teamgames\n");
340                 } else if(cvar("g_campaign")) {
341                         //JoinBestTeam(self, 0);
342                 } else if( argv(1) == "none" ) {
343                         SV_ChangeTeam( 0 );
344                 } else if( argv(1) == "red" ) {
345                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
346                 } else if( argv(1) == "blue" ) {
347                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
348                 } else if( argv(1) == "pink" ) {
349                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
350                 } else if( argv(1) == "yellow" ) {
351                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
352                 } else if( argv(1) == "auto" ) {
353                         self.team = -1;
354                         JoinBestTeam( self, 0 );
355                 } else {
356                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
357                 }
358         } else if(argv(0) == "ready") {
359                 if(cvar("sv_ready_restart"))
360                 {
361                         self.ready = TRUE;
362                         bprint(self.netname, "^2 is ready\n");
363                         ReadyCount();
364                 }
365         } else if(argv(0) == "maplist") {
366                 local float i, n;
367                 local string col;
368                 n = tokenize(cvar_string("g_maplist"));
369                 sprint(self, "^7Maps in list: ");
370                 for(i = 0; i < n; ++i)
371                 {
372                         if(math_mod(i, 2))
373                                 col = "^2";
374                         else
375                                 col = "^3";
376                         sprint(self, strcat(col, argv(i), " "));
377                 }
378                 sprint(self, "\n");
379         } else if(argv(0) == "teamstatus") {
380                 PrintScoreboard(self);
381         } else if(argv(0) == "say") {
382                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
383                 //clientcommand(self, formatmessage(s));
384         } else if(argv(0) == "say_team") {
385                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
386                 //clientcommand(self, formatmessage(s));
387         } else if(argv(0) == "info") {
388                 cmd = cvar_string(strcat("sv_info_", argv(1)));
389                 if(cmd == "")
390                         sprint(self, "ERROR: unsupported info command\n");
391                 else
392                         wordwrap_sprint(cmd, 1111);
393         } else if(argv(0) == "suggestmap") {
394                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
395         } else {
396                 cmd = argv(0);
397                 /* checks not needed any more since DP has separated clientcommands and regular commands
398                 if(cmd != "status")
399                 if(cmd != "name")
400                 //if(cmd != "say") // handled above
401                 //if(cmd != "say_team") // handled above
402                 if(cmd != "tell")
403                 if(cmd != "color")
404                 if(cmd != "kill")
405                 if(cmd != "pause")
406                 if(cmd != "kick")
407                 if(cmd != "ping")
408                 if(cmd != "pings")
409                 if(cmd != "ban")
410                 if(cmd != "pmodel")
411                 if(cmd != "rate")
412                 if(cmd != "playermodel")
413                 if(cmd != "playerskin")
414                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
415                 {
416                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
417                         return;
418                 }
419                 */
420                 clientcommand(self,s);
421         }
422 }
423
424 float ValidateMap(string vote)
425 {
426         string ext;
427         
428         tokenize(vote);
429         if(argv(0) == "map" || argv(0) == "changelevel")
430                 ext = ".bsp";
431         else if(argv(0) == "chmap")
432                 ext = ".mapcfg";
433         else if(argv(0) == "gotomap")
434                 ext = ".mapcfg";
435         else
436                 return TRUE;
437
438         if(!TryFile(strcat("maps/", argv(1), ext)))
439         {
440                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
441                 return FALSE;
442         }
443         return TRUE;
444 }
445
446
447 void VoteThink() {
448         if(votefinished > 0 // a vote was called
449             && time > votefinished) // time is up
450         {
451                 VoteCount();
452         }
453 }
454
455 string VoteParse() {
456         local float index;
457         index = 3;
458         local string vote;
459         vote = argv(2);
460         while(argv(index) != "") {
461                 vote = strcat(vote, " ", argv(index));
462                 index++;
463         }
464
465         // necessary for some of the string operations
466         vote = strzone(vote);
467
468         // now we remove some things that could be misused
469         index = 0;
470         local float found;
471         found = FALSE;
472         local float votelength;
473         votelength = strlen(vote);
474         while(!found && index < votelength)
475         {
476                 local string badchar;
477                 badchar = substring(vote, index, 1);
478                 if(badchar == ";"
479                    || badchar == "\r"
480                    || badchar == "\n")
481                 {
482                         found = TRUE;
483                 } else {
484                         index++;
485                 }
486         }
487         return substring(vote, 0, index);
488 }
489
490 float VoteAllowed(string votecommand) {
491         tokenize(cvar_string("sv_vote_commands"));
492         local float index;
493         index = 0;
494         while(argv(index) != "") {
495                 local string allowed;
496                 allowed = argv(index);
497                 if(votecommand == allowed) {
498                         return TRUE;
499                 }
500                 index++;
501         }
502         return FALSE;
503 }
504
505 void VoteReset() {
506         local entity player;
507
508         FOR_EACH_CLIENT(player)
509         {
510                 player.vote_vote = 0;
511                 centerprint_expire(player, CENTERPRIO_VOTE);
512         }
513
514         votecalled = FALSE;
515         votecalledmaster = FALSE;
516         votefinished = 0;
517 }
518
519 void VoteAccept() {
520         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was accepted\n");
521         if(votecalledmaster)
522         {
523                 votecaller.vote_master = 1;
524         } else {
525                 localcmd(strcat(votecalledvote, "\n"));
526         }
527         votecaller.vote_next = 0; // people like your votes, no wait for next vote
528         VoteReset();
529 }
530
531 void VoteReject() {
532         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was rejected\n");
533         VoteReset();
534 }
535
536 void VoteTimeout() {
537         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 timed out\n");
538         VoteReset();
539 }
540
541 void VoteStop(entity stopper) {
542         bprint("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n");
543         if(stopper == votecaller) {
544                 // no wait for next vote so you can correct your vote
545                 votecaller.vote_next = 0;
546         }
547         VoteReset();
548 }
549
550 void VoteNag() {
551         if(votecalled)
552                 if(self.vote_vote == 0)
553                         centerprint_atprio(self, CENTERPRIO_VOTE, strcat("^7^3", votecaller.netname, "^2 called a vote for ^1", votecalledvote, "\n\n^2You have not voted yet!\n^2HINT: By default, F1 is yes and F2 is no."));
554 }
555
556 void VoteCount() {
557         local float playercount;
558         playercount = 0;
559         local float yescount;
560         yescount = 0;
561         local float nocount;
562         nocount = 0;
563         local entity player;
564
565         FOR_EACH_REALCLIENT(player)
566         {
567                 if(player.vote_vote < 0) {
568                         nocount++;
569                 } else if(player.vote_vote > 0) {
570                         yescount++;
571                 }
572                 playercount++;
573         }
574
575         if((playercount == 1) && votecalledmaster) {
576                 // if only one player is on the server becoming vote
577                 // master is not allowed.  This could be used for
578                 // trolling or worse. 'self' is the user who has
579                 // called the vote because this function is called
580                 // by SV_ParseClientCommand. Maybe all voting should
581                 // be disabled for a single player?
582                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
583                 votecaller.vote_next = 0;
584                 VoteReset();
585         } else if((playercount / 2) < yescount) { // vote accepted
586                 VoteAccept();
587         } else if((playercount / 2) < nocount) { // vote rejected
588                 VoteReject();
589         } else if(time > votefinished) { // vote timedout
590                 if(cvar("sv_vote_simple_majority"))
591                         if(yescount > nocount)
592                                 VoteAccept();
593                         else if(nocount > yescount)
594                                 VoteReject();
595                         else
596                                 VoteTimeout();
597                 else
598                         VoteTimeout();
599         } // else still running
600 }
601
602
603 void ReadyCount()
604 {
605         local entity e;
606         local float r, p;
607
608         FOR_EACH_REALPLAYER(e)
609         {
610                 p += 1;
611                 if(e.ready)
612                         r += 1;
613         }
614
615         if(p && r == p)
616         {
617                 bprint("^1Server is restarting...\n");
618                 localcmd("restart\n");
619         }
620 }