]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
- Moved all the voting stuff into a file of its own and made it accessible from the...
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 void(entity e) DropFlag;
3 string MapVote_Suggest(string m);
4
5 .float floodcontrol_chat;
6 .float floodcontrol_team;
7 void Say(entity source, float teamsay, string msgin)
8 {
9         string msgstr, colorstr, cmsgstr, namestr;
10         float flood;
11         entity head;
12
13         if(!teamsay)
14                 if(substring(msgin, 0, 1) == " ")
15                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
16
17         msgin = formatmessage(msgin);
18
19         if(msgin == "")
20                 return;
21
22         if(source.classname != "player")
23                 colorstr = "^0"; // black for spectators
24         else if(teams_matter)
25                 colorstr = Team_ColorCode(source.team);
26         else
27                 teamsay = FALSE;
28
29         if(intermission_running)
30                 teamsay = FALSE;
31
32         /*
33          * using bprint solves this... me stupid
34         // how can we prevent the message from appearing in a listen server?
35         // for now, just give "say" back and only handle say_team
36         if(!teamsay)
37         {
38                 clientcommand(self, strcat("say ", msgin));
39                 return;
40         }
41         */
42
43         if(cvar("g_chat_teamcolors"))
44                 namestr = playername(source);
45         else
46                 namestr = source.netname;
47         if(teamsay)
48         {
49                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
50                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
51         }
52         else
53                 msgstr = strzone(strcat("\{1}", namestr, "^7: ", msgin, "\n"));
54
55         // FLOOD CONTROL
56         flood = 0;
57         {
58                 float flood_spl;
59                 float flood_burst;
60                 float flood_lmax;
61                 var .float flood_field;
62                 float lines;
63                 if(teamsay)
64                 {
65                         flood_spl = cvar("g_chat_flood_spl_team");
66                         flood_burst = cvar("g_chat_flood_burst_team");
67                         flood_lmax = cvar("g_chat_flood_lmax_team");
68                         flood_field = floodcontrol_team;
69                 }
70                 else
71                 {
72                         flood_spl = cvar("g_chat_flood_spl");
73                         flood_burst = cvar("g_chat_flood_burst");
74                         flood_lmax = cvar("g_chat_flood_lmax");
75                         flood_field = floodcontrol_chat;
76                 }
77                 flood_burst = max(0, flood_burst - 1);
78                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
79                 lines = ceil(strlennocol(msgstr) / 75);
80                 if(flood_lmax && lines > flood_lmax)
81                         flood = 2;
82                 else if(time >= self.flood_field)
83                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
84                 else
85                         flood = 1;
86         }
87
88         if(flood)
89         {
90                 if(cvar("g_chat_flood_notify_flooder"))
91                 {
92                         if(flood == 1)
93                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
94                         else if(flood == 2)
95                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
96                 }
97                 else
98                         sprint(self, msgstr);
99                 ServerConsoleEcho(strcat("NOTE: ", playername(self), "^7 is flooding."), TRUE);
100         }
101         else if(teamsay)
102         {
103                 if(source.classname == "player")
104                 {
105                         FOR_EACH_REALPLAYER(head)
106                         {
107                                 if(head.team == source.team)
108                                 {
109                                         sprint(head, msgstr);
110                                         centerprint(head, cmsgstr);
111                                 }
112                         }
113                 }
114                 else
115                 {
116                         FOR_EACH_REALCLIENT(head) if(head.classname != "player")
117                         {
118                                 sprint(head, msgstr);
119                                 centerprint(head, cmsgstr);
120                         }
121                 }
122         }
123         else
124         {
125                 //when g_tourney is active and g_tourney_disable_spec_chat is set and game is in matchstage
126                 //don't print the text from specs/observers to the players but only the other spectators
127                 if(g_tourney && cvar("g_tourney_disable_spec_chat") && tourneyInMatchStage && source.classname != "player") {
128                         FOR_EACH_REALCLIENT(head) if(head.classname != "player") {
129                                 sprint(head, msgstr);
130                         }
131                 }
132                 else
133                         bprint(msgstr);
134                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
135         }
136
137         strunzone(msgstr);
138 }
139
140 void SV_ParseClientCommand(string s) {
141         local string cmd;
142         local float i;
143
144         tokenize(s);
145
146         if(GameCommand_Vote(s, self)) {
147                 return;
148         } else if(argv(0) == "autoswitch") {
149                 // be backwards compatible with older clients (enabled)
150                 self.autoswitch = ("0" != argv(1));
151                 local string autoswitchmsg;
152                 if (self.autoswitch) {
153                         autoswitchmsg = "on";
154                 } else {
155                         autoswitchmsg = "off";
156                 }
157                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
158         } else if(argv(0) == "clientversion") {
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.frags = -2;
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(cvar("teamplay") && !cvar("sv_spectate")) {
174                         self.classname = "observer";
175                         stuffcmd(self,"menu_showteamselect\n");
176                 }
177         } else if(argv(0) == "reportcvar") { // old system
178                 GetCvars(1);
179         } else if(argv(0) == "sentcvar") { // new system
180                 GetCvars(1);
181         } else if(argv(0) == "spectate") {
182                 if(g_lms || g_arena)
183                         return; // don't allow spectating in lms, unless player runs out of lives
184                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
185                         if(self.flagcarried)
186                                 DropFlag(self.flagcarried);
187                         kh_Key_DropAll(self, TRUE);
188                         WaypointSprite_PlayerDead();
189                         DistributeFragsAmongTeam(self, self.team, 1.0);
190                         self.classname = "observer";
191                         if(blockSpectators)
192                                 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"));
193                         PutClientInServer();
194                 }
195         } else if(argv(0) == "join") {
196                 if(!g_arena)
197                 if (self.classname != "player" && !lockteams)
198                 {
199                         if(isJoinAllowed()) {
200                                 self.classname = "player";
201                                 self.frags = 0;
202                                 bprint ("^4", self.netname, "^4 is playing now\n");
203                                 PutClientInServer();
204                         }
205                         else {
206                                 //player may not join because of g_maxplayers is set
207                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
208                         }
209                 }
210         } else if( argv(0) == "selectteam" ) {
211                 if( !cvar("teamplay") ) {
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(cvar("sv_ready_restart"))
232                 {
233                         if(timeoutStatus) {
234                                 return sprint(self, "^1You cannot reset the game while a timeout is active!\n");
235                         }
236                         
237                         if(!restart_countdown || cvar("sv_ready_restart_repeatable"))
238                         {
239                                 self.ready = TRUE;
240                                 bprint(self.netname, "^2 is ready\n");
241                                 ReadyCount();
242                         } else {
243                                 sprint(self, "^1game has already been restarted\n");
244                         }
245                 }
246         } else if(argv(0) == "maplist") {
247                 local float n;
248                 local string col;
249                 n = tokenize(cvar_string("g_maplist"));
250                 sprint(self, "^7Maps in list: ");
251                 for(i = 0; i < n; ++i)
252                 {
253                         if(mod(i, 2))
254                                 col = "^2";
255                         else
256                                 col = "^3";
257                         sprint(self, strcat(col, argv(i), " "));
258                 }
259                 sprint(self, "\n");
260 #ifdef MAPINFO
261         } else if(argv(0) == "lsmaps") {
262                 sprint(self, "^7Maps available: ");
263                 for(i = 0; i < MapInfo_count; ++i)
264                 {
265                         if(mod(i, 2))
266                                 col = "^2";
267                         else
268                                 col = "^3";
269                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
270                 }
271                 sprint(self, "\n");
272 #endif
273         } else if(argv(0) == "teamstatus") {
274                 PrintScoreboard(self);
275         } else if(argv(0) == "voice") {
276                 VoiceMessage(argv(1));
277         } else if(argv(0) == "say") {
278                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
279                 //clientcommand(self, formatmessage(s));
280         } else if(argv(0) == "say_team") {
281                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
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) == "calltimeout") {
292                 if(cvar("sv_timeout")) {
293                         if(self.classname == "player") {
294                                 if(votecalled)
295                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
296                                 else
297                                         evaluateTimeoutCall();
298                         }
299                         else
300                                 sprint(self, "^7Error: only players can call a timeout!\n");
301                 }
302         } else if(argv(0) == "resumegame") {
303                 if(cvar("sv_timeout")) {
304                         evaluateResumeGame();
305                 }
306         } else {
307                 cmd = argv(0);
308                 /* checks not needed any more since DP has separated clientcommands and regular commands
309                 if(cmd != "status")
310                 if(cmd != "name")
311                 //if(cmd != "say") // handled above
312                 //if(cmd != "say_team") // handled above
313                 if(cmd != "tell")
314                 if(cmd != "color")
315                 if(cmd != "kill")
316                 if(cmd != "pause")
317                 if(cmd != "kick")
318                 if(cmd != "ping")
319                 if(cmd != "pings")
320                 if(cmd != "ban")
321                 if(cmd != "pmodel")
322                 if(cmd != "rate")
323                 if(cmd != "playermodel")
324                 if(cmd != "playerskin")
325                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
326                 {
327                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
328                         return;
329                 }
330                 */
331                 clientcommand(self,s);
332         }
333 }
334
335 /**
336  * Counts how many players are ready. If not enough players are ready, the function
337  * does nothing. If all players are ready, the timelimit will be extended and the
338  * restart_countdown variable is set to allow other functions like PlayerPostThink
339  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
340  * is not set the map will be resetted.
341  * 
342  * Function is called after the server receives a 'ready' sign from a player.
343  */
344 void ReadyCount()
345 {
346         local entity e;
347         local float r, p;
348
349         FOR_EACH_REALPLAYER(e)
350         {
351                 p += 1;
352                 if(e.ready)
353                         r += 1;
354         }
355
356         if(cvar("sv_ready_restart_nag")) {
357                 if(!readyNagActive) {
358                         readyNagger = spawn();
359                         readyNagger.think = readyNagger_Think;
360                         readyNagger.cnt = cvar("sv_ready_restart_nag_duration");
361                         readyNagger.nextthink = time;
362                         readyNagActive = 1;
363                 }
364         }
365
366         if(!p || r < p)
367                 return;
368
369         bprint("^1Server is restarting...\n");
370
371         // no arena, assault support yet...
372         if(g_arena | g_assault | gameover | intermission_running)
373                 localcmd("restart\n");
374
375         if(readyNagActive) { //if every player is ready, remove the ready-nagger again
376                 readyNagActive = 0;
377                 remove(readyNagger);
378         }
379
380         if(g_tourney) {
381                 tourneyInMatchStage = 1; //once the game is restarted the game is in match stage
382                 //reset weapons and ammo, health and armor to default:
383                 start_items = IT_LASER | IT_SHOTGUN;
384                 start_switchweapon = WEP_SHOTGUN;
385                 start_ammo_shells = cvar("g_start_ammo_shells");
386                 start_ammo_nails = cvar("g_start_ammo_nails");
387                 start_ammo_rockets = cvar("g_start_ammo_rockets");
388                 start_ammo_cells = cvar("g_start_ammo_cells");
389                 start_health = cvar("g_balance_health_start");
390                 start_armorvalue = cvar("g_balance_armor_start");
391         }
392
393         restart_countdown = time + RESTART_COUNTDOWN;
394         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
395         //reset the .ready status of all players (also spectators)
396         FOR_EACH_CLIENT(e)
397         {
398                 e.ready = 0;
399         }
400         if(0<cvar("timelimit") || (g_tourney && cvar("g_tourney_warmup_unlimited_time")) )
401         {
402                 // remember original timelimit on first restart
403                 if(!timelimit_orig)
404                         timelimit_orig = cvar("timelimit");
405                 //only set the new timelimit if, when loading the map, a timelimit was really set
406                 if(timelimit_orig)
407                         cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
408         }
409         if(cvar("teamplay_lockonrestart") && teams_matter) {
410                 lockteams = 1;
411                 bprint("^1The teams are now locked.\n");
412         }
413         
414         //initiate the restart-countdown-announcer entity
415         restartAnnouncer = spawn();
416         restartAnnouncer.think = restartAnnouncer_Think;
417         restartAnnouncer.nextthink = time;
418         restartAnnouncer.cnt = RESTART_COUNTDOWN;
419         
420         //after a restart every players number of allowed timeouts gets reset, too
421         if(cvar("sv_timeout"))
422         {
423                 FOR_EACH_REALPLAYER(e)
424                 {
425                         e.allowedTimeouts = cvar("sv_timeout_number");
426                 }
427         }
428
429         //play the prepareforbattle sound to everyone
430         sound(world, CHAN_AUTO, "announcer/robotic/prepareforbattle.wav", 1, ATTN_NONE);
431
432         //reset map immediately if this cvar is not set
433         if (!cvar("sv_ready_restart_after_countdown"))
434                 reset_map();
435         
436         if(cvar("sv_eventlog"))
437                 GameLogEcho(":restart", FALSE);
438 }
439
440 /**
441  * Centerprints the information to all players who didn't ready up yet to do so.
442  */
443 void readyNagger_Think() {
444         local entity plr;
445         if(self.cnt <= 0) { //have a break showing the ready nag
446                 //make sure that the old ready-nag-centerprint isn't shown too long:
447                 FOR_EACH_REALCLIENT(plr) {
448                         if(plr.classname == "player") {
449                                 if (!plr.ready)
450                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "");
451                         }
452                 }
453                 self.cnt = cvar("sv_ready_restart_nag_duration");
454                 self.nextthink = time + cvar("sv_ready_restart_nag_interval");
455         }
456         else {
457                 //show the ready nagging to all players who aren't ready yet
458                 FOR_EACH_REALCLIENT(plr) {
459                         if(plr.classname == "player") {
460                                 if (!plr.ready) {
461                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "^2Please ready up (F4 by default)!");
462                                         //play reminder sound once the centerprint appears for the first time after the pause:
463                                         if (self.cnt == cvar("sv_ready_restart_nag_duration"))
464                                                 play2(plr, "misc/talk2.wav");
465                                 }
466                         }
467                 }
468
469                 self.nextthink = time + 1;
470                 self.cnt -= 1;
471         }
472 }
473
474 /**
475  * Shows the restart countdown for all players.
476  * Plays the countdown sounds for the seconds 3, 2 1, begin for everyone.
477  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
478  * is set to 1).
479  */
480 void restartAnnouncer_Think() {
481         local entity plr;
482         local string s;
483         if(self.cnt <= 0) { //show the "Begin" message and
484                 if (cvar("sv_ready_restart_after_countdown")) {
485                         restart_mapalreadyrestarted = 1;
486                         reset_map();
487                 }
488
489                 FOR_EACH_REALCLIENT(plr) {
490                         if(plr.classname == "player") {
491                                 s = strcat(NEWLINES, "^1Begin!");
492                                 centerprint(plr, s);
493                         }
494                 }
495                 sound(world, CHAN_AUTO, "announcer/robotic/begin.wav", 1, ATTN_NONE);
496
497                 remove(self);
498                 return;
499         }
500         else {
501                 FOR_EACH_REALCLIENT(plr) {
502                         if(plr.classname == "player") {
503                                 s = strcat(NEWLINES, "^1Game starts in ", ftos(self.cnt), " seconds");
504                                 centerprint(plr, s);
505                         }
506                 }
507
508                 if(self.cnt <= 3) {
509                         sound(world, CHAN_AUTO, strcat("announcer/robotic/", ftos(self.cnt), ".ogg"), 1, ATTN_NONE);
510                 }
511                 self.nextthink = time + 1;
512                 self.cnt -= 1;
513         }
514 }
515
516 /**
517  * Checks whether the player who calls the timeout is allowed to do so.
518  * If so, it initializes the timeout countdown. It also checks whether another
519  * timeout was already running at this time and reacts correspondingly.
520  *
521  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
522  *                          timeoutInitiator, timeoutStatus, timeoutHandler
523  *
524  * This function is called when a player issues the calltimeout command.
525  */
526 void evaluateTimeoutCall() {
527         if (g_tourney && !tourneyInMatchStage && !cvar("g_tourney_warmup_allow_timeout"))
528                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
529         if (time < restart_countdown )
530                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
531         if (timeoutStatus != 2) {
532                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
533                 if (cvar("timelimit")) {
534                         //a timelimit was used
535                         local float myTl;
536                         if (cvar("timelimit"))
537                                 myTl = cvar("timelimit");
538                         else
539                                 myTl = timelimit_orig;
540
541                         local float lastPossibleTimeout;
542                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
543
544                         if (lastPossibleTimeout < time)
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         sound(world, CHAN_AUTO, "announcer/robotic/timeoutcalled.wav", 1, ATTN_NONE);
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 evaluateResumeGame() {
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 }