]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
add checks
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 entity nagger;
2 float readycount;
3 void Nagger_SendEntity(entity to, float sendflags)
4 {
5         float nags;
6         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
7
8         nags = 0;
9         if(readycount)
10         {
11                 nags |= 1;
12                 if(to.ready == 0)
13                         nags |= 2;
14         }
15         if(votecalled)
16         {
17                 nags |= 4;
18                 if(to.vote_vote == 0)
19                         nags |= 8;
20         }
21
22         if(sendflags & 128)
23         {
24                 WriteByte(MSG_ENTITY, nags | 128);
25                 WriteString(MSG_ENTITY, votecalledvote);
26         }
27         else
28                 WriteByte(MSG_ENTITY, nags);
29 }
30 void Nagger_Init()
31 {
32         nagger = spawn();
33         Net_LinkEntity(nagger);
34         nagger.SendFlags = 128;
35         nagger.SendEntity = Nagger_SendEntity;
36 }
37 void Nagger_VoteChanged()
38 {
39         if(nagger)
40                 nagger.SendFlags = 128;
41 }
42 void Nagger_ReadyCounted()
43 {
44         if(nagger)
45                 nagger.SendFlags = 1;
46 }
47
48 void ReadyCount();
49 string MapVote_Suggest(string m);
50
51 .float floodcontrol_chat;
52 .float floodcontrol_team;
53 void Say(entity source, float teamsay, string msgin)
54 {
55         string msgstr, colorstr, cmsgstr, namestr;
56         float flood;
57         entity head;
58
59         if(Ban_MaybeEnforceBan(source))
60                 return;
61
62         if(!teamsay)
63                 if(substring(msgin, 0, 1) == " ")
64                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
65
66         msgin = formatmessage(msgin);
67
68         if(msgin == "")
69                 return;
70
71         if(source.classname != "player")
72                 colorstr = "^0"; // black for spectators
73         else if(teams_matter)
74                 colorstr = Team_ColorCode(source.team);
75         else
76                 teamsay = FALSE;
77
78         if(intermission_running)
79                 teamsay = FALSE;
80
81         /*
82          * using bprint solves this... me stupid
83         // how can we prevent the message from appearing in a listen server?
84         // for now, just give "say" back and only handle say_team
85         if(!teamsay)
86         {
87                 clientcommand(self, strcat("say ", msgin));
88                 return;
89         }
90         */
91
92         if(cvar("g_chat_teamcolors"))
93                 namestr = playername(source);
94         else
95                 namestr = source.netname;
96         if(teamsay)
97         {
98                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
99                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
100         }
101         else
102                 msgstr = strzone(strcat("\{1}", namestr, "^7: ", msgin, "\n"));
103
104         // FLOOD CONTROL
105         flood = 0;
106         {
107                 float flood_spl;
108                 float flood_burst;
109                 float flood_lmax;
110                 var .float flood_field;
111                 float lines;
112                 if(teamsay)
113                 {
114                         flood_spl = cvar("g_chat_flood_spl_team");
115                         flood_burst = cvar("g_chat_flood_burst_team");
116                         flood_lmax = cvar("g_chat_flood_lmax_team");
117                         flood_field = floodcontrol_team;
118                 }
119                 else
120                 {
121                         flood_spl = cvar("g_chat_flood_spl");
122                         flood_burst = cvar("g_chat_flood_burst");
123                         flood_lmax = cvar("g_chat_flood_lmax");
124                         flood_field = floodcontrol_chat;
125                 }
126                 flood_burst = max(0, flood_burst - 1);
127                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
128                 lines = ceil(strlennocol(msgstr) / 75);
129                 if(flood_lmax && lines > flood_lmax)
130                         flood = 2;
131                 else if(time >= self.flood_field)
132                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
133                 else
134                         flood = 1;
135         }
136
137         if(flood)
138         {
139                 if(cvar("g_chat_flood_notify_flooder"))
140                 {
141                         if(flood == 1)
142                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
143                         else if(flood == 2)
144                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
145                 }
146                 else
147                         sprint(self, msgstr);
148                 print("NOTE: ", playername(self), "^7 is flooding.\n");
149         }
150         else if(teamsay)
151         {
152                 if(source.classname == "player")
153                 {
154                         FOR_EACH_REALPLAYER(head)
155                         {
156                                 if(head.team == source.team)
157                                 {
158                                         sprint(head, msgstr);
159                                         centerprint(head, cmsgstr);
160                                 }
161                         }
162                 }
163                 else
164                 {
165                         FOR_EACH_REALCLIENT(head) if(head.classname != "player")
166                         {
167                                 sprint(head, msgstr);
168                                 centerprint(head, cmsgstr);
169                         }
170                 }
171         }
172         else
173         {
174                 // TODO invent a cvar name for allowing global chat by spectators during warmup anyway
175                 if(cvar("g_chat_nospectators") && source.classname != "player") {
176                         FOR_EACH_REALCLIENT(head) if(head.classname != "player") {
177                                 sprint(head, msgstr);
178                         }
179                 }
180                 else
181                         bprint(msgstr);
182         }
183
184         strunzone(msgstr);
185 }
186
187 entity GetPlayer(string name)
188 {
189         float num;
190         entity e;
191         string ns;
192
193         if(substring(name, 0, 1) == "#") {
194                 num = stof(substring(name, 1, 999));
195                 if(num >= 1 && num <= maxclients) {
196                         for((e = world); num > 0; --num, (e = nextent(e)))
197                                 ;
198                         //if(clienttype(e) == CLIENTTYPE_REAL)
199                         if(e.classname == "player")
200                                 return e;
201                 }
202         } else {
203                 ns = strdecolorize(name);
204                 FOR_EACH_REALPLAYER(e) {
205                         if(!strcasecmp(strdecolorize(e.netname), ns)) {
206                                 return e;
207                         }
208                 }
209         }
210         return world;
211 }
212
213 //float ctf_clientcommand();
214 void SV_ParseClientCommand(string s) {
215         local string cmd;
216         local float i;
217
218         tokenize(s);
219
220         if(GameCommand_Vote(s, self)) {
221                 return;
222         } else if(GameCommand_MapVote(argv(0))) {
223                 return;
224         } else if(argv(0) == "autoswitch") {
225                 // be backwards compatible with older clients (enabled)
226                 self.autoswitch = ("0" != argv(1));
227                 local string autoswitchmsg;
228                 if (self.autoswitch) {
229                         autoswitchmsg = "on";
230                 } else {
231                         autoswitchmsg = "off";
232                 }
233                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
234         } else if(argv(0) == "clientversion") {
235                 if not(self.flags & FL_CLIENT)
236                         return;
237                 if (argv(1) == "$gameversion") {
238                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
239                         // either that or someone wants to be funny
240                         self.version = 1;
241                 } else {
242                         self.version = stof(argv(1));
243                 }
244                 if(self.version != cvar("gameversion"))
245                 {
246                         self.classname = "observer";
247                         self.version_mismatch = 1;
248                         PutClientInServer();
249                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
250                         //JoinBestTeam(self, FALSE, TRUE);
251                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
252                         self.classname = "observer";
253                         stuffcmd(self,"menu_showteamselect\n");
254                 }
255         } else if(argv(0) == "reportcvar") { // old system
256                 GetCvars(1);
257         } else if(argv(0) == "sentcvar") { // new system
258                 GetCvars(1);
259         } else if(argv(0) == "spectate") {
260                 if not(self.flags & FL_CLIENT)
261                         return;
262                 if(g_lms || g_arena)
263                         return; // don't allow spectating in lms, unless player runs out of lives
264                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
265                         if(self.flagcarried)
266                                 DropFlag(self.flagcarried, 0);
267                         kh_Key_DropAll(self, TRUE);
268                         WaypointSprite_PlayerDead();
269                         self.classname = "observer";
270                         if(blockSpectators)
271                                 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"));
272                         PutClientInServer();
273                 }
274         } else if(argv(0) == "join") {
275                 if not(self.flags & FL_CLIENT)
276                         return;
277                 if(!g_arena)
278                 if (self.classname != "player" && !lockteams)
279                 {
280                         if(isJoinAllowed()) {
281                                 self.classname = "player";
282                                 PlayerScore_Clear(self);
283                                 bprint ("^4", self.netname, "^4 is playing now\n");
284                                 PutClientInServer();
285                         }
286                         else {
287                                 //player may not join because of g_maxplayers is set
288                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
289                         }
290                 }
291         } else if( argv(0) == "selectteam" ) {
292                 if not(self.flags & FL_CLIENT)
293                         return;
294                 if( !cvar("teamplay") ) {
295                         sprint( self, "selecteam can only be used in teamgames\n");
296                 } else if(cvar("g_campaign")) {
297                         //JoinBestTeam(self, 0);
298                 } else if(lockteams) {
299                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
300                 } else if( argv(1) == "red" ) {
301                         DoTeamChange(COLOR_TEAM1);
302                 } else if( argv(1) == "blue" ) {
303                         DoTeamChange(COLOR_TEAM2);
304                 } else if( argv(1) == "yellow" ) {
305                         DoTeamChange(COLOR_TEAM3);
306                 } else if( argv(1) == "pink" ) {
307                         DoTeamChange(COLOR_TEAM4);
308                 } else if( argv(1) == "auto" ) {
309                         DoTeamChange(-1);
310                 } else {
311                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
312                 }
313         } else if(argv(0) == "ready") {
314                 if not(self.flags & FL_CLIENT)
315                         return;
316                 if((inWarmupStage && 0 < g_warmup_limit) // with unlimited warmup players have to be able to restart
317                    || cvar("sv_ready_restart"))
318                 {
319                         if(timeoutStatus) {
320                                 return sprint(self, "^1You cannot reset the game while a timeout is active!\n");
321                         }
322                         
323                         if(!restart_countdown || cvar("sv_ready_restart_repeatable"))
324                         {
325                                 self.ready = TRUE;
326                                 bprint(self.netname, "^2 is ready\n");
327                                 ReadyCount();
328                         } else {
329                                 sprint(self, "^1game has already been restarted\n");
330                         }
331                 }
332         } else if(argv(0) == "maplist") {
333                 local float n, j;
334                 local string col;
335                 n = tokenize(cvar_string("g_maplist"));
336                 sprint(self, "^7Maps in list: ");
337                 for(i = 0, j = 0; i < n; ++i)
338                 {
339                         if(MapInfo_CheckMap(argv(i)))
340                         {
341                                 if(mod(j, 2))
342                                         col = "^2";
343                                 else
344                                         col = "^3";
345                                 sprint(self, strcat(col, argv(i), " "));
346                                 ++j;
347                         }
348                 }
349                 sprint(self, "\n");
350         } else if(argv(0) == "lsmaps") {
351                 sprint(self, "^7Maps available: ");
352                 for(i = 0; i < MapInfo_count; ++i)
353                 {
354                         if(mod(i, 2))
355                                 col = "^2";
356                         else
357                                 col = "^3";
358                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
359                 }
360                 sprint(self, "\n");
361         } else if(argv(0) == "voice") {
362                 VoiceMessage(argv(1));
363         } else if(argv(0) == "say") {
364                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
365                 //clientcommand(self, formatmessage(s));
366         } else if(argv(0) == "say_team") {
367                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
368                 //clientcommand(self, formatmessage(s));
369         } else if(argv(0) == "info") {
370                 cmd = cvar_string(strcat("sv_info_", argv(1)));
371                 if(cmd == "")
372                         sprint(self, "ERROR: unsupported info command\n");
373                 else
374                         wordwrap_sprint(cmd, 1111);
375         } else if(argv(0) == "suggestmap") {
376                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
377         } else if(argv(0) == "calltimeout") {
378                 if not(self.flags & FL_CLIENT)
379                         return;
380                 if(cvar("sv_timeout")) {
381                         if(self.classname == "player") {
382                                 if(votecalled)
383                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
384                                 else
385                                         evaluateTimeoutCall();
386                         }
387                         else
388                                 sprint(self, "^7Error: only players can call a timeout!\n");
389                 }
390         } else if(argv(0) == "resumegame") {
391                 if not(self.flags & FL_CLIENT)
392                         return;
393                 if(cvar("sv_timeout")) {
394                         evaluateResumeGame();
395                 }
396         } else if(argv(0) == "teamstatus") {
397                 Score_NicePrint(self);
398         } else {
399                 //if(ctf_clientcommand())
400                 //      return;
401                 cmd = argv(0);
402                 /* checks not needed any more since DP has separated clientcommands and regular commands
403                 if(cmd != "status")
404                 if(cmd != "name")
405                 //if(cmd != "say") // handled above
406                 //if(cmd != "say_team") // handled above
407                 if(cmd != "tell")
408                 if(cmd != "color")
409                 if(cmd != "kill")
410                 if(cmd != "pause")
411                 if(cmd != "kick")
412                 if(cmd != "ping")
413                 if(cmd != "pings")
414                 if(cmd != "ban")
415                 if(cmd != "pmodel")
416                 if(cmd != "rate")
417                 if(cmd != "playermodel")
418                 if(cmd != "playerskin")
419                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
420                 {
421                         print("WARNING: Invalid clientcommand by ", self.netname, ": ", s, "\n");
422                         return;
423                 }
424                 */
425                 clientcommand(self,s);
426         }
427 }
428
429 void ReadyRestart()
430 {
431         local entity e;
432
433         bprint("^1Server is restarting...\n");
434
435         VoteReset();
436
437         // no arena, assault support yet...
438         if(g_arena | g_assault | gameover | intermission_running | race_completing)
439                 localcmd("restart\n");
440
441         // clear overtime
442         if(checkrules_overtimeend)
443                 checkrules_overtimeend = 0;
444
445         restart_countdown = time + RESTART_COUNTDOWN;
446         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
447         if(0 < cvar("timelimit") || inWarmupStage)
448         {
449                 // remember original timelimit on first restart
450                 if(!timelimit_orig)
451                         timelimit_orig = cvar("timelimit");
452                 //only set the new timelimit if, when loading the map, a timelimit was really set
453                 if(timelimit_orig)
454                         cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
455         }
456
457         inWarmupStage = 0; //once the game is restarted the game is in match stage
458
459         //reset the .ready status of all players (also spectators)
460         FOR_EACH_CLIENTSLOT(e)
461                 e.ready = 0;
462         FOR_EACH_REALCLIENT(e)
463         {
464                 msg_entity = e;
465                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
466                 WriteByte(MSG_ONE, TE_CSQC_WARMUP);
467                 WriteByte(MSG_ONE, 0);
468         }
469
470         if(cvar("teamplay_lockonrestart") && teams_matter) {
471                 lockteams = 1;
472                 bprint("^1The teams are now locked.\n");
473         }
474         
475         //initiate the restart-countdown-announcer entity
476         restartAnnouncer = spawn();
477         restartAnnouncer.think = restartAnnouncer_Think;
478         restartAnnouncer.nextthink = time;
479         restartAnnouncer.cnt = RESTART_COUNTDOWN;
480         
481         //after a restart every players number of allowed timeouts gets reset, too
482         if(cvar("sv_timeout"))
483         {
484                 FOR_EACH_REALPLAYER(e)
485                         e.allowedTimeouts = cvar("sv_timeout_number");
486         }
487
488         //play the prepareforbattle sound to everyone
489         play2all("announcer/robotic/prepareforbattle.wav");
490
491         //reset map immediately if this cvar is not set
492         if (!cvar("sv_ready_restart_after_countdown"))
493                 reset_map();
494
495         // reset ALL scores
496         Score_ClearAll();
497         
498         if(cvar("sv_eventlog"))
499                 GameLogEcho(":restart");
500 }
501
502 /**
503  * Counts how many players are ready. If not enough players are ready, the function
504  * does nothing. If all players are ready, the timelimit will be extended and the
505  * restart_countdown variable is set to allow other functions like PlayerPostThink
506  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
507  * is not set the map will be resetted.
508  * 
509  * Function is called after the server receives a 'ready' sign from a player.
510  */
511 void ReadyCount()
512 {
513         local entity e;
514         local float r, p;
515
516         r = p = 0;
517
518         FOR_EACH_REALPLAYER(e)
519         {
520                 p += 1;
521                 if(e.ready)
522                         r += 1;
523         }
524
525         readycount = r;
526
527         if(r) // at least one is ready
528         if(r == p) // and, everyone is ready
529                 ReadyRestart();
530
531         Nagger_ReadyCounted();
532 }
533
534 /**
535  * Shows the restart countdown for all players.
536  * Plays the countdown sounds for the seconds 3, 2 1, begin for everyone.
537  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
538  * is set to 1).
539  */
540 void restartAnnouncer_Think() {
541         local entity plr;
542         local string s;
543         if(self.cnt <= 0) { //show the "Begin" message and
544                 if (cvar("sv_ready_restart_after_countdown")) {
545                         restart_mapalreadyrestarted = 1;
546                         reset_map();
547                 }
548
549                 FOR_EACH_REALCLIENT(plr) {
550                         if(plr.classname == "player") {
551                                 s = strcat(NEWLINES, "^1Begin!");
552                                 centerprint(plr, s);
553                         }
554                 }
555                 play2all("announcer/robotic/begin.wav");
556
557                 remove(self);
558                 return;
559         }
560         else {
561                 FOR_EACH_REALCLIENT(plr) {
562                         if(plr.classname == "player") {
563                                 s = strcat(NEWLINES, "^1Game starts in ", ftos(self.cnt), " seconds");
564                                 centerprint(plr, s);
565                         }
566                 }
567
568                 if(self.cnt <= 3) {
569                         play2all(strcat("announcer/robotic/", ftos(self.cnt), ".ogg"));
570                 }
571                 self.nextthink = time + 1;
572                 self.cnt -= 1;
573         }
574 }
575
576 /**
577  * Checks whether the player who calls the timeout is allowed to do so.
578  * If so, it initializes the timeout countdown. It also checks whether another
579  * timeout was already running at this time and reacts correspondingly.
580  *
581  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
582  *                          timeoutInitiator, timeoutStatus, timeoutHandler
583  *
584  * This function is called when a player issues the calltimeout command.
585  */
586 void evaluateTimeoutCall() {
587         if (inWarmupStage && !g_warmup_allow_timeout)
588                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
589         if (time < restart_countdown )
590                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
591         if (timeoutStatus != 2) {
592                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
593                 if (cvar("timelimit")) {
594                         //a timelimit was used
595                         local float myTl;
596                         if (cvar("timelimit"))
597                                 myTl = cvar("timelimit");
598                         else
599                                 myTl = timelimit_orig;
600
601                         local float lastPossibleTimeout;
602                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
603
604                         if (lastPossibleTimeout < time)
605                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
606                 }
607         }
608         //player may not call a timeout if he has no calls left
609         if (self.allowedTimeouts < 1)
610                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
611         //now all required checks are passed
612         self.allowedTimeouts -= 1;
613         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)
614         remainingTimeoutTime = cvar("sv_timeout_length");
615         remainingLeadTime = cvar("sv_timeout_leadtime");
616         timeoutInitiator = self;
617         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
618                 timeoutStatus = 1;
619                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
620                 timeoutHandler = spawn();
621                 timeoutHandler.think = timeoutHandler_Think;
622         }
623         timeoutHandler.nextthink = time; //always let the entity think asap
624
625         //inform all connected clients about the timeout call
626         play2all("announcer/robotic/timeoutcalled.wav");
627 }
628
629 /**
630  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
631  * and the lead time for the timeout is still active, this countdown just will be aborted (the
632  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
633  * value of the cvar sv_timeout_resumetime.
634  *
635  * This function is called when a player issues the resumegame command.
636  */
637 void evaluateResumeGame() {
638         if (!timeoutStatus)
639                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
640         if (self != timeoutInitiator)
641                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
642         if (timeoutStatus == 1) {
643                 remainingTimeoutTime = timeoutStatus = 0;
644                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
645                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
646         }
647         else if (timeoutStatus == 2) {
648                 //only shorten the remainingTimeoutTime if it makes sense
649                 if( remainingTimeoutTime > (cvar("sv_timeout_resumetime") + 1) ) {
650                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
651                         remainingTimeoutTime = cvar("sv_timeout_resumetime");
652                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
653                 }
654                 else
655                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
656
657         }
658 }