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