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