]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/clientcommands.qc
same for stopsound()
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / clientcommands.qc
1 entity nagger;
2 float readycount;
3 float Nagger_SendEntity(entity to, float sendflags)
4 {
5         float nags, i, f, b;
6         entity e;
7         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
8
9         nags = 0;
10         if(readycount)
11         {
12                 nags |= 1;
13                 if(to.ready == 0)
14                         nags |= 2;
15         }
16         if(votecalled)
17         {
18                 nags |= 4;
19                 if(to.vote_vote == 0)
20                         nags |= 8;
21         }
22         if(inWarmupStage)
23                 nags |= 16;
24
25         if(sendflags & 128)
26                 nags |= 128;
27
28         if(!(nags & 4)) // no vote called? send no string
29                 nags &~= 128;
30
31         WriteByte(MSG_ENTITY, nags);
32
33         if(nags & 128)
34         {
35                 WriteString(MSG_ENTITY, votecalledvote_display);
36         }
37
38         if(nags & 1)
39         {
40                 for(i = 1; i <= maxclients; i += 8)
41                 {
42                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
43                                 if(clienttype(e) != CLIENTTYPE_REAL || e.ready)
44                                         f |= b;
45                         WriteByte(MSG_ENTITY, f);
46                 }
47         }
48
49         return TRUE;
50 }
51 void Nagger_Init()
52 {
53         Net_LinkEntity(nagger = spawn(), FALSE, 0, Nagger_SendEntity);
54 }
55 void Nagger_VoteChanged()
56 {
57         if(nagger)
58                 nagger.SendFlags |= 128;
59 }
60 void Nagger_VoteCountChanged()
61 {
62         if(nagger)
63                 nagger.SendFlags |= 1;
64 }
65 void Nagger_ReadyCounted()
66 {
67         if(nagger)
68                 nagger.SendFlags |= 1;
69 }
70
71 void ReadyCount();
72 string MapVote_Suggest(string m);
73
74 entity GetPlayer(string name)
75 {
76         float num;
77         entity e;
78         string ns;
79
80         if(substring(name, 0, 1) == "#") {
81                 num = stof(substring(name, 1, 999));
82                 if(num >= 1 && num <= maxclients) {
83                         for((e = world); num > 0; --num, (e = nextent(e)))
84                                 ;
85                         //if(clienttype(e) == CLIENTTYPE_REAL)
86                         if(e.classname == "player")
87                                 return e;
88                 }
89         } else {
90                 ns = strdecolorize(name);
91                 FOR_EACH_REALPLAYER(e) {
92                         if(!strcasecmp(strdecolorize(e.netname), ns)) {
93                                 return e;
94                         }
95                 }
96         }
97         return world;
98 }
99
100 //float ctf_clientcommand();
101 float readyrestart_happened;
102 .float lms_spectate_warning;
103 void spawnfunc_func_breakable();
104
105 .float cmd_floodtime;
106 .float cmd_floodcount;
107 float cmd_floodcheck()
108 {
109         if (timeoutStatus != 2)
110         {
111                 if(time == self.cmd_floodtime)
112                 {
113                         self.cmd_floodcount += 1;
114                         if(self.cmd_floodcount > 8)
115                                 return TRUE;
116                 }
117                 else
118                 {
119                         self.cmd_floodtime = time;
120                         self.cmd_floodcount = 1;
121                 }
122         }
123         return FALSE;
124 }
125
126 .float checkfail;
127 void SV_ParseClientCommand(string s) {
128         string cmd;
129         float tokens;
130         float i;
131         entity e;
132
133         tokens = tokenize_console(s);
134
135         cmd = strtolower(argv(0));
136         if(cmd != "reportcvar")
137         if(cmd != "sentcvar")
138         if(cmd != "pause")
139         if(cmd != "prespawn")
140         if(cmd != "spawn")
141         if(cmd != "begin")
142         {
143                 if(cmd_floodcheck())
144                         return;
145         }
146
147         if(GameCommand_Vote(s, self)) {
148                 return;
149         } else if(GameCommand_MapVote(argv(0))) {
150                 return;
151         } else if(cmd == "checkfail") {
152                 print(sprintf("CHECKFAIL: %s (%s) epically failed check %s\n", self.netname, self.netaddress, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1))));
153                 self.checkfail = 1;
154         } else if(cmd == "autoswitch") {
155                 // be backwards compatible with older clients (enabled)
156                 self.autoswitch = ("0" != argv(1));
157                 local string autoswitchmsg;
158                 if (self.autoswitch) {
159                         autoswitchmsg = "on";
160                 } else {
161                         autoswitchmsg = "off";
162                 }
163                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
164         } else if(cmd == "clientversion") {
165                 if not(self.flags & FL_CLIENT)
166                         return;
167                 if (argv(1) == "$gameversion") {
168                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.xonotic.com)^8";
169                         // either that or someone wants to be funny
170                         self.version = 1;
171                 } else {
172                         self.version = stof(argv(1));
173                 }
174                 if(self.version < autocvar_gameversion_min || self.version > autocvar_gameversion_max)
175                 {
176                         self.version_mismatch = 1;
177                         ClientKill_TeamChange(-2); // observe
178                 } else if(autocvar_g_campaign || autocvar_g_balance_teams || autocvar_g_balance_teams_force) {
179                         //JoinBestTeam(self, FALSE, TRUE);
180                 } else if(teamplay && !autocvar_sv_spectate && !(self.team_forced > 0)) {
181                         self.classname = "observer";
182                         stuffcmd(self,"menu_showteamselect\n");
183                 }
184         } else if(cmd == "reportcvar") { // old system
185                 if(substring(argv(2), 0, 1) == "$") // undefined cvar: use the default value on the server then
186                 {
187                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
188                         tokens = tokenize_console(s);
189                 }
190                 GetCvars(1);
191         } else if(cmd == "sentcvar") { // new system
192                 if(tokens == 2) // undefined cvar: use the default value on the server then
193                 {
194                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
195                         tokens = tokenize_console(s);
196                 }
197                 GetCvars(1);
198         } else if(cmd == "spectate") {
199                 if(cmd_floodcheck())
200                         return;
201                 if not(self.flags & FL_CLIENT)
202                         return;
203                 if(g_arena)
204                         return;
205                 if(g_lms)
206                 {
207                         if(self.lms_spectate_warning)
208                         {
209                                 // mark player as spectator
210                                 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
211                         }
212                         else
213                         {
214                                 self.lms_spectate_warning = 1;
215                                 sprint(self, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
216                                 return;
217                         }
218                 }
219                 if(self.classname == "player" && autocvar_sv_spectate == 1) {
220                         ClientKill_TeamChange(-2); // observe
221                 }
222         } else if(cmd == "join") {
223                 if not(self.flags & FL_CLIENT)
224                         return;
225                 if(!g_arena)
226                 if (self.classname != "player" && !lockteams)
227                 {
228                         if(nJoinAllowed(1)) {
229                                 self.classname = "player";
230                                 if(g_ca)
231                                         self.caplayer = 1;
232                                 PlayerScore_Clear(self);
233                                 bprint ("^4", self.netname, "^4 is playing now\n");
234                                 PutClientInServer();
235                                 if(autocvar_g_campaign)
236                                         campaign_bots_may_start = 1;
237                         }
238                         else {
239                                 //player may not join because of g_maxplayers is set
240                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
241                         }
242                 }
243         } else if( cmd == "selectteam" ) {
244                 if not(self.flags & FL_CLIENT)
245                         return;
246                 if( !teamplay ) {
247                         sprint( self, "selectteam can only be used in teamgames\n");
248                 } else if(autocvar_g_campaign) {
249                         //JoinBestTeam(self, 0);
250                 } else if(self.team_forced > 0) {
251                         sprint( self, "selectteam can not be used as your team is forced\n");
252                 } else if(lockteams) {
253                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
254                 } else if( argv(1) == "red" ) {
255                         ClientKill_TeamChange(COLOR_TEAM1);
256                 } else if( argv(1) == "blue" ) {
257                         ClientKill_TeamChange(COLOR_TEAM2);
258                 } else if( argv(1) == "yellow" ) {
259                         ClientKill_TeamChange(COLOR_TEAM3);
260                 } else if( argv(1) == "pink" ) {
261                         ClientKill_TeamChange(COLOR_TEAM4);
262                 } else if( argv(1) == "auto" ) {
263                         ClientKill_TeamChange(-1);
264                 } else {
265                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
266                 }
267         } else if(cmd == "ready") {
268                 if not(self.flags & FL_CLIENT)
269                         return;
270
271                 if((inWarmupStage)
272                    || autocvar_sv_ready_restart || g_race_qualifying == 2)
273                 {
274                         if(!readyrestart_happened || autocvar_sv_ready_restart_repeatable)
275                         {
276                                 if (self.ready) // toggle
277                                 {
278                                         self.ready = FALSE;
279                                         bprint(self.netname, "^2 is ^1NOT^2 ready\n");
280                                 }
281                                 else
282                                 {
283                                         self.ready = TRUE;
284                                         bprint(self.netname, "^2 is ready\n");
285                                 }
286
287                                 // cannot reset the game while a timeout is active!
288                                 if(!timeoutStatus)
289                                         ReadyCount();
290                         } else {
291                                 sprint(self, "^1Game has already been restarted\n");
292                         }
293                 }
294         } else if(cmd == "maplist") {
295                 sprint(self, maplist_reply);
296         } else if(cmd == "lsmaps") {
297                 sprint(self, lsmaps_reply);
298         } else if(cmd == "lsnewmaps") {
299                 sprint(self, lsnewmaps_reply);
300         } else if(cmd == "records") {
301                 for(i = 0; i < 10; ++i)
302                         sprint(self, records_reply[i]);
303         } else if(cmd == "ladder") {
304                 sprint(self, ladder_reply);
305         } else if(cmd == "rankings") {
306                 sprint(self, rankings_reply);
307         } else if(cmd == "voice") {
308                 if(tokens >= 3)
309                         VoiceMessage(argv(1), substring(s, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
310                 else
311                         VoiceMessage(argv(1), "");
312         } else if(cmd == "say") {
313                 if(tokens >= 2)
314                         Say(self, FALSE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
315                 //clientcommand(self, formatmessage(s));
316         } else if(cmd == "say_team") {
317                 if(tokens >= 2)
318                         Say(self, TRUE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
319                 //clientcommand(self, formatmessage(s));
320         } else if(cmd == "tell") {
321                 e = GetCommandPlayerSlotTargetFromTokenizedCommand(tokens, 1);
322                 if(e && tokens > ParseCommandPlayerSlotTarget_firsttoken)
323                 {
324                         Say(self, FALSE, e, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)), TRUE);
325                 }
326                 else
327                 {
328                         if(tokens > ParseCommandPlayerSlotTarget_firsttoken)
329                                 trigger_magicear_processmessage_forallears(self, -1, world, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)));
330                         sprint(self, "ERROR: usage: tell # playerid text...\n");
331                 }
332                 //clientcommand(self, formatmessage(s));
333         } else if(cmd == "info") {
334                 cmd = cvar_string_builtin(strcat("sv_info_", argv(1))); // This needed fixed for the cvar check
335                 if(cmd == "")
336                         sprint(self, "ERROR: unsupported info command\n");
337                 else
338                         wordwrap_sprint(cmd, 1111);
339         } else if(cmd == "suggestmap") {
340                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
341         } else if(cmd == "timeout") {
342                 if not(self.flags & FL_CLIENT)
343                         return;
344                 if(autocvar_sv_timeout) {
345                         if(self.classname == "player") {
346                                 if(votecalled)
347                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
348                                 else
349                                         evaluateTimeout();
350                         }
351                         else
352                                 sprint(self, "^7Error: only players can call a timeout!\n");
353                 }
354         } else if(cmd == "timein") {
355                 if not(self.flags & FL_CLIENT)
356                         return;
357                 if(autocvar_sv_timeout) {
358                         evaluateTimein();
359                 }
360         } else if(cmd == "teamstatus") {
361                 Score_NicePrint(self);
362         } else if(cmd == "cvar_changes") {
363                 sprint(self, cvar_changes);
364         } else if(cmd == "cvar_purechanges") {
365                 sprint(self, cvar_purechanges);
366         } else if(CheatCommand(tokens)) {
367         } else {
368 #if 0
369                 //if(ctf_clientcommand())
370                 //      return;
371                 // grep for Cmd_AddCommand_WithClientCommand to find them all
372                 if(cmd != "status")
373                 //if(cmd != "say") // handled above
374                 //if(cmd != "say_team") // handled above
375                 if(cmd != "kill")
376                 if(cmd != "pause")
377                 if(cmd != "ping")
378                 if(cmd != "name")
379                 if(cmd != "color")
380                 if(cmd != "rate")
381                 if(cmd != "pmodel")
382                 if(cmd != "playermodel")
383                 if(cmd != "playerskin")
384                 if(cmd != "prespawn")
385                 if(cmd != "spawn")
386                 if(cmd != "begin")
387                 if(cmd != "pings")
388                 if(cmd != "sv_startdownload")
389                 if(cmd != "download")
390                 {
391                         print("WARNING: Invalid clientcommand by ", self.netname, ": ", s, "\n");
392                         return;
393                 }
394 #endif
395
396                 if(self.jointime > 0 && time > self.jointime + 10 && time > self.nickspamtime) // allow any changes in the first 10 seconds since joining
397                 if(cmd == "name" || cmd == "playermodel") // TODO also playerskin and color?
398                 {
399                         if(self.nickspamtime == 0 || time > self.nickspamtime + autocvar_g_nick_flood_timeout)
400                                 // good, no serious flood
401                                 self.nickspamcount = 1;
402                         else
403                                 self.nickspamcount += 1;
404                         self.nickspamtime = time + autocvar_g_nick_flood_penalty;
405
406                         if (timeoutStatus == 2) //when game is paused, no flood protection
407                                 self.nickspamcount = self.nickspamtime = 0;
408                 }
409
410                 clientcommand(self,s);
411         }
412 }
413
414 void ReadyRestartForce()
415 {
416         local entity e;
417
418         bprint("^1Server is restarting...\n");
419
420         VoteReset();
421
422         // clear overtime
423         if (checkrules_overtimesadded > 0 && g_race_qualifying != 2) {
424                 //we have to decrease timelimit to its original value again!!
425                 float newTL;
426                 newTL = autocvar_timelimit;
427                 newTL -= checkrules_overtimesadded * autocvar_timelimit_overtime;
428                 cvar_set("timelimit", ftos(newTL));
429         }
430
431         checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;
432
433
434         readyrestart_happened = 1;
435         game_starttime = time;
436         if(!g_ca && !g_arena)
437                 game_starttime += RESTART_COUNTDOWN;
438         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
439
440         inWarmupStage = 0; //once the game is restarted the game is in match stage
441
442         //reset the .ready status of all players (also spectators)
443         FOR_EACH_CLIENTSLOT(e)
444                 e.ready = 0;
445         readycount = 0;
446         Nagger_ReadyCounted(); // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
447
448         if(autocvar_teamplay_lockonrestart && teamplay) {
449                 lockteams = 1;
450                 bprint("^1The teams are now locked.\n");
451         }
452
453         //initiate the restart-countdown-announcer entity
454         if(autocvar_sv_ready_restart_after_countdown && !g_ca && !g_arena)
455         {
456                 restartTimer = spawn();
457                 restartTimer.think = restartTimer_Think;
458                 restartTimer.nextthink = game_starttime;
459         }
460
461         //after a restart every players number of allowed timeouts gets reset, too
462         if(autocvar_sv_timeout)
463         {
464                 FOR_EACH_REALPLAYER(e)
465                         e.allowedTimeouts = autocvar_sv_timeout_number;
466         }
467
468         //reset map immediately if this cvar is not set
469         if (!autocvar_sv_ready_restart_after_countdown)
470                 reset_map(TRUE);
471
472         if(autocvar_sv_eventlog)
473                 GameLogEcho(":restart");
474 }
475
476 void ReadyRestart()
477 {
478         // no arena, assault support yet...
479         if(g_arena | g_assault | gameover | intermission_running | race_completing)
480                 localcmd("restart\n");
481         else
482                 localcmd("\nsv_hook_gamerestart\n");
483
484         ReadyRestartForce();
485
486         // reset ALL scores, but only do that at the beginning
487         //of the countdown if sv_ready_restart_after_countdown is off!
488         //Otherwise scores could be manipulated during the countdown!
489         if (!autocvar_sv_ready_restart_after_countdown)
490                 Score_ClearAll();
491 }
492
493 /**
494  * Counts how many players are ready. If not enough players are ready, the function
495  * does nothing. If all players are ready, the timelimit will be extended and the
496  * restart_countdown variable is set to allow other functions like PlayerPostThink
497  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
498  * is not set the map will be resetted.
499  *
500  * Function is called after the server receives a 'ready' sign from a player.
501  */
502 void ReadyCount()
503 {
504         local entity e;
505         local float r, p;
506
507         r = p = 0;
508
509         FOR_EACH_REALPLAYER(e)
510         {
511                 p += 1;
512                 if(e.ready)
513                         r += 1;
514         }
515
516         readycount = r;
517
518         Nagger_ReadyCounted();
519
520         if(r) // at least one is ready
521         if(r == p) // and, everyone is ready
522                 ReadyRestart();
523 }
524
525 /**
526  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
527  * is set)
528  */
529 void restartTimer_Think() {
530         restart_mapalreadyrestarted = 1;
531         reset_map(TRUE);
532         Score_ClearAll();
533         remove(self);
534         return;
535 }
536
537 /**
538  * Checks whether the player who calls the timeout is allowed to do so.
539  * If so, it initializes the timeout countdown. It also checks whether another
540  * timeout was already running at this time and reacts correspondingly.
541  *
542  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
543  *                          timeoutInitiator, timeoutStatus, timeoutHandler
544  *
545  * This function is called when a player issues the calltimeout command.
546  */
547 void evaluateTimeout() {
548         if (inWarmupStage && !g_warmup_allow_timeout)
549                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
550         if (time < game_starttime )
551                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
552         if (timeoutStatus != 2) {
553                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
554                 if (autocvar_timelimit) {
555                         //a timelimit was used
556                         local float myTl;
557                         myTl = autocvar_timelimit;
558
559                         local float lastPossibleTimeout;
560                         lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
561
562                         if (lastPossibleTimeout < time - game_starttime)
563                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
564                 }
565         }
566         //player may not call a timeout if he has no calls left
567         if (self.allowedTimeouts < 1)
568                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
569         //now all required checks are passed
570         self.allowedTimeouts -= 1;
571         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)
572         remainingTimeoutTime = autocvar_sv_timeout_length;
573         remainingLeadTime = autocvar_sv_timeout_leadtime;
574         timeoutInitiator = self;
575         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
576                 timeoutStatus = 1;
577                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
578                 timeoutHandler = spawn();
579                 timeoutHandler.think = timeoutHandler_Think;
580         }
581         timeoutHandler.nextthink = time; //always let the entity think asap
582
583         //inform all connected clients about the timeout call
584         Announce("timeoutcalled");
585 }
586
587 /**
588  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
589  * and the lead time for the timeout is still active, this countdown just will be aborted (the
590  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
591  * value of the cvar sv_timeout_resumetime.
592  *
593  * This function is called when a player issues the resumegame command.
594  */
595 void evaluateTimein() {
596         if (!timeoutStatus)
597                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
598         if (self != timeoutInitiator)
599                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
600         if (timeoutStatus == 1) {
601                 remainingTimeoutTime = timeoutStatus = 0;
602                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
603                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
604         }
605         else if (timeoutStatus == 2) {
606                 //only shorten the remainingTimeoutTime if it makes sense
607                 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) ) {
608                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
609                         remainingTimeoutTime = autocvar_sv_timeout_resumetime;
610                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
611                 }
612                 else
613                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
614
615         }
616 }