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