]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/clientcommands.qc
start of a generic mutator 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 != cvar("gameversion"))
168                 {
169                         self.classname = "observer";
170                         self.version_mismatch = 1;
171                         PutClientInServer();
172                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
173                         //JoinBestTeam(self, FALSE, TRUE);
174                 } else if(teams_matter && !cvar("sv_spectate")) {
175                         self.classname = "observer";
176                         stuffcmd(self,"menu_showteamselect\n");
177                 }
178         } else if(cmd == "reportcvar") { // old system
179                 if(substring(argv(2), 0, 1) == "$") // undefined cvar: use the default value on the server then
180                 {
181                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
182                         tokens = tokenize_console(s);
183                 }
184                 GetCvars(1);
185 #ifdef UID
186         } else if(cmd == "uid") {
187                 if not(self.uid)
188                 {
189                         self.uid = strzone(argv(1));
190                         self.uid_kicktime = 0;
191                         print("Client ", etos(self), " has UID ", self.uid, "\n");
192                         Ban_MaybeEnforceBan(self);
193                 }
194 #endif
195         } else if(cmd == "sentcvar") { // new system
196                 if(tokens == 2) // undefined cvar: use the default value on the server then
197                 {
198                         s = strcat(substring(s, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
199                         tokens = tokenize_console(s);
200                 }
201                 GetCvars(1);
202         } else if(cmd == "spectate") {
203                 if(cmd_floodcheck())
204                         return;
205                 if not(self.flags & FL_CLIENT)
206                         return;
207                 if(g_arena)
208                         return;
209                 if(g_lms)
210                 {
211                         if(self.lms_spectate_warning)
212                         {
213                                 // mark player as spectator
214                                 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
215                         }
216                         else
217                         {
218                                 self.lms_spectate_warning = 1;
219                                 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");
220                                 return;
221                         }
222                 }
223                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
224                         if(self.flagcarried)
225                                 DropFlag(self.flagcarried, world, world);
226                         if(self.ballcarried)
227                                 DropBall(self.ballcarried, self.origin, self.velocity);
228                         MUTATOR_CALLHOOK(MakePlayerSpectator);
229                         WaypointSprite_PlayerDead();
230                         self.classname = "observer";
231                         if(g_ca)
232                                 self.caplayer = 0;
233                         if(blockSpectators)
234                                 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"));
235                         PutClientInServer();
236                 }
237         } else if(cmd == "join") {
238                 if not(self.flags & FL_CLIENT)
239                         return;
240                 if(!g_arena)
241                 if (self.classname != "player" && !lockteams)
242                 {
243                         if(isJoinAllowed()) {
244                                 self.classname = "player";
245                                 if(g_ca)
246                                         self.caplayer = 1;
247                                 PlayerScore_Clear(self);
248                                 bprint ("^4", self.netname, "^4 is playing now\n");
249                                 self.stat_count = WEP_LAST;
250                                 PutClientInServer();
251                                 if(cvar("g_campaign"))
252                                         campaign_bots_may_start = 1;
253                         }
254                         else {
255                                 //player may not join because of g_maxplayers is set
256                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
257                         }
258                 }
259         } else if( cmd == "selectteam" ) {
260                 if not(self.flags & FL_CLIENT)
261                         return;
262                 if( !teams_matter ) {
263                         sprint( self, "selecteam can only be used in teamgames\n");
264                 } else if(cvar("g_campaign")) {
265                         //JoinBestTeam(self, 0);
266                 } else if(lockteams) {
267                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
268                 } else if( argv(1) == "red" ) {
269                         DoTeamChange(COLOR_TEAM1);
270                 } else if( argv(1) == "blue" ) {
271                         DoTeamChange(COLOR_TEAM2);
272                 } else if( argv(1) == "yellow" ) {
273                         DoTeamChange(COLOR_TEAM3);
274                 } else if( argv(1) == "pink" ) {
275                         DoTeamChange(COLOR_TEAM4);
276                 } else if( argv(1) == "auto" ) {
277                         DoTeamChange(-1);
278                 } else {
279                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
280                 }
281         } else if(cmd == "ready") {
282                 if not(self.flags & FL_CLIENT)
283                         return;
284
285                 if((inWarmupStage && 0 >= g_warmup_limit) // with unlimited warmup players have to be able to restart
286                    || cvar("sv_ready_restart") || g_race_qualifying == 2)
287                 {
288                         if(!readyrestart_happened || cvar("sv_ready_restart_repeatable"))
289                         {
290                                 if (self.ready) // toggle
291                                 {
292                                         self.ready = FALSE;
293                                         bprint(self.netname, "^2 is ^1NOT^2 ready\n");
294                                 }
295                                 else
296                                 {
297                                         self.ready = TRUE;
298                                         bprint(self.netname, "^2 is ready\n");
299                                 }
300
301                                 // cannot reset the game while a timeout is active!
302                                 if(!timeoutStatus)
303                                         ReadyCount();
304                         } else {
305                                 sprint(self, "^1Game has already been restarted\n");
306                         }
307                 }
308         } else if(cmd == "maplist") {
309                 sprint(self, maplist_reply);
310         } else if(cmd == "lsmaps") {
311                 sprint(self, lsmaps_reply);
312         } else if(cmd == "lsnewmaps") {
313                 sprint(self, lsnewmaps_reply);
314         } else if(cmd == "records") {
315                 for(i = 0; i < 10; ++i)
316                         sprint(self, records_reply[i]);
317         } else if(cmd == "rankings") {
318                 sprint(self, rankings_reply);
319         } else if(cmd == "voice") {
320                 if(tokens >= 3)
321                         VoiceMessage(argv(1), substring(s, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
322                 else
323                         VoiceMessage(argv(1), "");
324         } else if(cmd == "say") {
325                 if(tokens >= 2)
326                         Say(self, FALSE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
327                 //clientcommand(self, formatmessage(s));
328         } else if(cmd == "say_team") {
329                 if(tokens >= 2)
330                         Say(self, TRUE, world, substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
331                 //clientcommand(self, formatmessage(s));
332         } else if(cmd == "tell") {
333                 e = GetCommandPlayerSlotTargetFromTokenizedCommand(tokens, 1);
334                 if(e && tokens > ParseCommandPlayerSlotTarget_firsttoken)
335                 {
336                         Say(self, FALSE, e, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)), TRUE);
337                 }
338                 else
339                 {
340                         if(tokens > ParseCommandPlayerSlotTarget_firsttoken)
341                                 trigger_magicear_processmessage_forallears(self, -1, world, substring(s, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken)));
342                         sprint(self, "ERROR: usage: tell # playerid text...\n");
343                 }
344                 //clientcommand(self, formatmessage(s));
345         } else if(cmd == "info") {
346                 cmd = cvar_string_builtin(strcat("sv_info_", argv(1))); // This needed fixed for the cvar check
347                 if(cmd == "")
348                         sprint(self, "ERROR: unsupported info command\n");
349                 else
350                         wordwrap_sprint(cmd, 1111);
351         } else if(cmd == "suggestmap") {
352                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
353         } else if(cmd == "timeout") {
354                 if not(self.flags & FL_CLIENT)
355                         return;
356                 if(cvar("sv_timeout")) {
357                         if(self.classname == "player") {
358                                 if(votecalled)
359                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
360                                 else
361                                         evaluateTimeout();
362                         }
363                         else
364                                 sprint(self, "^7Error: only players can call a timeout!\n");
365                 }
366         } else if(cmd == "timein") {
367                 if not(self.flags & FL_CLIENT)
368                         return;
369                 if(cvar("sv_timeout")) {
370                         evaluateTimein();
371                 }
372         } else if(cmd == "teamstatus") {
373                 Score_NicePrint(self);
374         } else if(cmd == "cvar_changes") {
375                 sprint(self, cvar_changes);
376         } else if(CheatCommand(tokens)) {
377         } else {
378                 //if(ctf_clientcommand())
379                 //      return;
380                 // grep for Cmd_AddCommand_WithClientCommand to find them all
381                 if(cmd != "status")
382                 //if(cmd != "say") // handled above
383                 //if(cmd != "say_team") // handled above
384                 if(cmd != "kill")
385                 if(cmd != "pause")
386                 if(cmd != "ping")
387                 if(cmd != "name")
388                 if(cmd != "color")
389                 if(cmd != "rate")
390                 if(cmd != "pmodel")
391                 if(cmd != "playermodel")
392                 if(cmd != "playerskin")
393                 if(cmd != "prespawn")
394                 if(cmd != "spawn")
395                 if(cmd != "begin")
396                 if(cmd != "pings")
397                 if(cmd != "sv_startdownload")
398                 if(cmd != "download")
399                 {
400                         print("WARNING: Invalid clientcommand by ", self.netname, ": ", s, "\n");
401                         return;
402                 }
403
404                 if(self.jointime > 0 && time > self.jointime + 10 && time > self.nickspamtime) // allow any changes in the first 10 seconds since joining
405                 if(cmd == "name" || cmd == "playermodel") // TODO also playerskin and color?
406                 {
407                         if(self.nickspamtime == 0 || time > self.nickspamtime + cvar("g_nick_flood_timeout"))
408                                 // good, no serious flood
409                                 self.nickspamcount = 1;
410                         else
411                                 self.nickspamcount += 1;
412                         self.nickspamtime = time + cvar("g_nick_flood_penalty");
413
414                         if (timeoutStatus == 2) //when game is paused, no flood protection
415                                 self.nickspamcount = self.nickspamtime = 0;
416                 }
417
418                 clientcommand(self,s);
419         }
420 }
421
422 void ReadyRestartForce()
423 {
424         local entity e;
425
426         bprint("^1Server is restarting...\n");
427
428         VoteReset();
429
430         // clear overtime
431         if (checkrules_overtimesadded > 0 && g_race_qualifying != 2) {
432                 //we have to decrease timelimit to its original value again!!
433                 float newTL;
434                 newTL = cvar("timelimit");
435                 newTL -= checkrules_overtimesadded * cvar("timelimit_overtime");
436                 cvar_set("timelimit", ftos(newTL));
437         }
438
439         checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;
440
441
442         readyrestart_happened = 1;
443         game_starttime = time;
444         if(!g_ca)
445                 game_starttime += RESTART_COUNTDOWN;
446         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
447
448         inWarmupStage = 0; //once the game is restarted the game is in match stage
449
450         //reset the .ready status of all players (also spectators)
451         FOR_EACH_CLIENTSLOT(e)
452                 e.ready = 0;
453         readycount = 0;
454         Nagger_ReadyCounted(); // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
455
456         if(cvar("teamplay_lockonrestart") && teams_matter) {
457                 lockteams = 1;
458                 bprint("^1The teams are now locked.\n");
459         }
460
461         //initiate the restart-countdown-announcer entity
462         if(cvar("sv_ready_restart_after_countdown"))
463         {
464                 restartTimer = spawn();
465                 restartTimer.think = restartTimer_Think;
466                 restartTimer.nextthink = game_starttime;
467         }
468
469         //after a restart every players number of allowed timeouts gets reset, too
470         if(cvar("sv_timeout"))
471         {
472                 FOR_EACH_REALPLAYER(e)
473                         e.allowedTimeouts = cvar("sv_timeout_number");
474         }
475
476         //reset map immediately if this cvar is not set
477         if (!cvar("sv_ready_restart_after_countdown"))
478                 reset_map(TRUE);
479
480         if(cvar("sv_eventlog"))
481                 GameLogEcho(":restart");
482 }
483
484 void ReadyRestart()
485 {
486         // no arena, assault support yet...
487         if(g_arena | g_assault | gameover | intermission_running | race_completing)
488                 localcmd("restart\n");
489         else
490                 localcmd("\nsv_hook_gamerestart;");
491
492         ReadyRestartForce();
493
494         // reset ALL scores, but only do that at the beginning
495         //of the countdown if sv_ready_restart_after_countdown is off!
496         //Otherwise scores could be manipulated during the countdown!
497         if (!cvar("sv_ready_restart_after_countdown"))
498                 Score_ClearAll();
499 }
500
501 /**
502  * Counts how many players are ready. If not enough players are ready, the function
503  * does nothing. If all players are ready, the timelimit will be extended and the
504  * restart_countdown variable is set to allow other functions like PlayerPostThink
505  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
506  * is not set the map will be resetted.
507  *
508  * Function is called after the server receives a 'ready' sign from a player.
509  */
510 void ReadyCount()
511 {
512         local entity e;
513         local float r, p;
514
515         r = p = 0;
516
517         FOR_EACH_REALPLAYER(e)
518         {
519                 p += 1;
520                 if(e.ready)
521                         r += 1;
522         }
523
524         readycount = r;
525
526         Nagger_ReadyCounted();
527
528         if(r) // at least one is ready
529         if(r == p) // and, everyone is ready
530                 ReadyRestart();
531 }
532
533 /**
534  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
535  * is set)
536  */
537 void restartTimer_Think() {
538         restart_mapalreadyrestarted = 1;
539         reset_map(TRUE);
540         Score_ClearAll();
541         remove(self);
542         return;
543 }
544
545 /**
546  * Checks whether the player who calls the timeout is allowed to do so.
547  * If so, it initializes the timeout countdown. It also checks whether another
548  * timeout was already running at this time and reacts correspondingly.
549  *
550  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
551  *                          timeoutInitiator, timeoutStatus, timeoutHandler
552  *
553  * This function is called when a player issues the calltimeout command.
554  */
555 void evaluateTimeout() {
556         if (inWarmupStage && !g_warmup_allow_timeout)
557                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
558         if (time < game_starttime )
559                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
560         if (timeoutStatus != 2) {
561                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
562                 if (cvar("timelimit")) {
563                         //a timelimit was used
564                         local float myTl;
565                         myTl = cvar("timelimit");
566
567                         local float lastPossibleTimeout;
568                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
569
570                         if (lastPossibleTimeout < time - game_starttime)
571                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
572                 }
573         }
574         //player may not call a timeout if he has no calls left
575         if (self.allowedTimeouts < 1)
576                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
577         //now all required checks are passed
578         self.allowedTimeouts -= 1;
579         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)
580         remainingTimeoutTime = cvar("sv_timeout_length");
581         remainingLeadTime = cvar("sv_timeout_leadtime");
582         timeoutInitiator = self;
583         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
584                 timeoutStatus = 1;
585                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
586                 timeoutHandler = spawn();
587                 timeoutHandler.think = timeoutHandler_Think;
588         }
589         timeoutHandler.nextthink = time; //always let the entity think asap
590
591         //inform all connected clients about the timeout call
592         Announce("timeoutcalled");
593 }
594
595 /**
596  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
597  * and the lead time for the timeout is still active, this countdown just will be aborted (the
598  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
599  * value of the cvar sv_timeout_resumetime.
600  *
601  * This function is called when a player issues the resumegame command.
602  */
603 void evaluateTimein() {
604         if (!timeoutStatus)
605                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
606         if (self != timeoutInitiator)
607                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
608         if (timeoutStatus == 1) {
609                 remainingTimeoutTime = timeoutStatus = 0;
610                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
611                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
612         }
613         else if (timeoutStatus == 2) {
614                 //only shorten the remainingTimeoutTime if it makes sense
615                 if( remainingTimeoutTime > (cvar("sv_timeout_resumetime") + 1) ) {
616                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
617                         remainingTimeoutTime = cvar("sv_timeout_resumetime");
618                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
619                 }
620                 else
621                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
622
623         }
624 }