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