]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
new vote/ready nagger display (untested, can't test this alone)
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 entity nagger;
2 float readycount;
3 void Nagger_SendEntity(entity to, float sendflags)
4 {
5         float nags;
6         WriteByte(MSG_ENTITY, ENT_CLIENT_NAGGER);
7
8         nags = 0;
9         if(readycount)
10         {
11                 nags |= 1;
12                 if(to.ready == 0)
13                         nags |= 2;
14         }
15         if(votecalled)
16         {
17                 nags |= 4;
18                 if(to.vote_vote == 0)
19                         nags |= 8;
20         }
21
22         if(sendflags & 128)
23         {
24                 WriteByte(MSG_ENTITY, nags | 128);
25                 WriteString(MSG_ENTITY, votecalledvote);
26         }
27         else
28                 WriteByte(MSG_ENTITY, nags);
29 }
30 void Nagger_Init()
31 {
32         nagger = spawn();
33         Net_LinkEntity(nagger);
34         nagger.SendFlags = 128;
35         nagger.SendEntity = Nagger_SendEntity;
36 }
37 void Nagger_VoteChanged()
38 {
39         nagger.SendFlags = 128;
40 }
41 void Nagger_ReadyCounted()
42 {
43         nagger.SendFlags = 1;
44 }
45
46 void ReadyCount();
47 string MapVote_Suggest(string m);
48
49 .float floodcontrol_chat;
50 .float floodcontrol_team;
51 void Say(entity source, float teamsay, string msgin)
52 {
53         string msgstr, colorstr, cmsgstr, namestr;
54         float flood;
55         entity head;
56
57         if(Ban_MaybeEnforceBan(source))
58                 return;
59
60         if(!teamsay)
61                 if(substring(msgin, 0, 1) == " ")
62                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
63
64         msgin = formatmessage(msgin);
65
66         if(msgin == "")
67                 return;
68
69         if(source.classname != "player")
70                 colorstr = "^0"; // black for spectators
71         else if(teams_matter)
72                 colorstr = Team_ColorCode(source.team);
73         else
74                 teamsay = FALSE;
75
76         if(intermission_running)
77                 teamsay = FALSE;
78
79         /*
80          * using bprint solves this... me stupid
81         // how can we prevent the message from appearing in a listen server?
82         // for now, just give "say" back and only handle say_team
83         if(!teamsay)
84         {
85                 clientcommand(self, strcat("say ", msgin));
86                 return;
87         }
88         */
89
90         if(cvar("g_chat_teamcolors"))
91                 namestr = playername(source);
92         else
93                 namestr = source.netname;
94         if(teamsay)
95         {
96                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
97                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
98         }
99         else
100                 msgstr = strzone(strcat("\{1}", namestr, "^7: ", msgin, "\n"));
101
102         // FLOOD CONTROL
103         flood = 0;
104         {
105                 float flood_spl;
106                 float flood_burst;
107                 float flood_lmax;
108                 var .float flood_field;
109                 float lines;
110                 if(teamsay)
111                 {
112                         flood_spl = cvar("g_chat_flood_spl_team");
113                         flood_burst = cvar("g_chat_flood_burst_team");
114                         flood_lmax = cvar("g_chat_flood_lmax_team");
115                         flood_field = floodcontrol_team;
116                 }
117                 else
118                 {
119                         flood_spl = cvar("g_chat_flood_spl");
120                         flood_burst = cvar("g_chat_flood_burst");
121                         flood_lmax = cvar("g_chat_flood_lmax");
122                         flood_field = floodcontrol_chat;
123                 }
124                 flood_burst = max(0, flood_burst - 1);
125                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
126                 lines = ceil(strlennocol(msgstr) / 75);
127                 if(flood_lmax && lines > flood_lmax)
128                         flood = 2;
129                 else if(time >= self.flood_field)
130                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
131                 else
132                         flood = 1;
133         }
134
135         if(flood)
136         {
137                 if(cvar("g_chat_flood_notify_flooder"))
138                 {
139                         if(flood == 1)
140                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
141                         else if(flood == 2)
142                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
143                 }
144                 else
145                         sprint(self, msgstr);
146                 print("NOTE: ", playername(self), "^7 is flooding.\n");
147         }
148         else if(teamsay)
149         {
150                 if(source.classname == "player")
151                 {
152                         FOR_EACH_REALPLAYER(head)
153                         {
154                                 if(head.team == source.team)
155                                 {
156                                         sprint(head, msgstr);
157                                         centerprint(head, cmsgstr);
158                                 }
159                         }
160                 }
161                 else
162                 {
163                         FOR_EACH_REALCLIENT(head) if(head.classname != "player")
164                         {
165                                 sprint(head, msgstr);
166                                 centerprint(head, cmsgstr);
167                         }
168                 }
169         }
170         else
171         {
172                 // TODO invent a cvar name for allowing global chat by spectators during warmup anyway
173                 if(cvar("g_chat_nospectators") && source.classname != "player") {
174                         FOR_EACH_REALCLIENT(head) if(head.classname != "player") {
175                                 sprint(head, msgstr);
176                         }
177                 }
178                 else
179                         bprint(msgstr);
180         }
181
182         strunzone(msgstr);
183 }
184
185 entity GetPlayer(string name)
186 {
187         float num;
188         entity e;
189         string ns;
190
191         if(substring(name, 0, 1) == "#") {
192                 num = stof(substring(name, 1, 999));
193                 if(num >= 1 && num <= maxclients) {
194                         for((e = world); num > 0; --num, (e = nextent(e)))
195                                 ;
196                         //if(clienttype(e) == CLIENTTYPE_REAL)
197                         if(e.classname == "player")
198                                 return e;
199                 }
200         } else {
201                 ns = strdecolorize(name);
202                 FOR_EACH_REALPLAYER(e) {
203                         if(!strcasecmp(strdecolorize(e.netname), ns)) {
204                                 return e;
205                         }
206                 }
207         }
208         return world;
209 }
210
211 //float ctf_clientcommand();
212 void SV_ParseClientCommand(string s) {
213         local string cmd;
214         local float i;
215
216         tokenize(s);
217
218         if(GameCommand_Vote(s, self)) {
219                 return;
220         } else if(GameCommand_MapVote(argv(0))) {
221                 return;
222         } else if(argv(0) == "autoswitch") {
223                 // be backwards compatible with older clients (enabled)
224                 self.autoswitch = ("0" != argv(1));
225                 local string autoswitchmsg;
226                 if (self.autoswitch) {
227                         autoswitchmsg = "on";
228                 } else {
229                         autoswitchmsg = "off";
230                 }
231                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
232         } else if(argv(0) == "clientversion") {
233                 if not(self.flags & FL_CLIENT)
234                         return;
235                 if (argv(1) == "$gameversion") {
236                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
237                         // either that or someone wants to be funny
238                         self.version = 1;
239                 } else {
240                         self.version = stof(argv(1));
241                 }
242                 if(self.version != cvar("gameversion"))
243                 {
244                         self.classname = "observer";
245                         self.version_mismatch = 1;
246                         PutClientInServer();
247                 } else if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force")) {
248                         //JoinBestTeam(self, FALSE, TRUE);
249                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
250                         self.classname = "observer";
251                         stuffcmd(self,"menu_showteamselect\n");
252                 }
253         } else if(argv(0) == "reportcvar") { // old system
254                 GetCvars(1);
255         } else if(argv(0) == "sentcvar") { // new system
256                 GetCvars(1);
257         } else if(argv(0) == "spectate") {
258                 if not(self.flags & FL_CLIENT)
259                         return;
260                 if(g_lms || g_arena)
261                         return; // don't allow spectating in lms, unless player runs out of lives
262                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
263                         if(self.flagcarried)
264                                 DropFlag(self.flagcarried, 0);
265                         kh_Key_DropAll(self, TRUE);
266                         WaypointSprite_PlayerDead();
267                         self.classname = "observer";
268                         if(blockSpectators)
269                                 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"));
270                         PutClientInServer();
271                 }
272         } else if(argv(0) == "join") {
273                 if not(self.flags & FL_CLIENT)
274                         return;
275                 if(!g_arena)
276                 if (self.classname != "player" && !lockteams)
277                 {
278                         if(isJoinAllowed()) {
279                                 self.classname = "player";
280                                 PlayerScore_Clear(self);
281                                 bprint ("^4", self.netname, "^4 is playing now\n");
282                                 PutClientInServer();
283                         }
284                         else {
285                                 //player may not join because of g_maxplayers is set
286                                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
287                         }
288                 }
289         } else if( argv(0) == "selectteam" ) {
290                 if not(self.flags & FL_CLIENT)
291                         return;
292                 if( !cvar("teamplay") ) {
293                         sprint( self, "selecteam can only be used in teamgames\n");
294                 } else if(cvar("g_campaign")) {
295                         //JoinBestTeam(self, 0);
296                 } else if(lockteams) {
297                         sprint( self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
298                 } else if( argv(1) == "red" ) {
299                         DoTeamChange(COLOR_TEAM1);
300                 } else if( argv(1) == "blue" ) {
301                         DoTeamChange(COLOR_TEAM2);
302                 } else if( argv(1) == "yellow" ) {
303                         DoTeamChange(COLOR_TEAM3);
304                 } else if( argv(1) == "pink" ) {
305                         DoTeamChange(COLOR_TEAM4);
306                 } else if( argv(1) == "auto" ) {
307                         DoTeamChange(-1);
308                 } else {
309                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
310                 }
311         } else if(argv(0) == "ready") {
312                 if not(self.flags & FL_CLIENT)
313                         return;
314                 if((inWarmupStage && 0 < g_warmup_limit) // with unlimited warmup players have to be able to restart
315                    || cvar("sv_ready_restart"))
316                 {
317                         if(timeoutStatus) {
318                                 return sprint(self, "^1You cannot reset the game while a timeout is active!\n");
319                         }
320                         
321                         if(!restart_countdown || cvar("sv_ready_restart_repeatable"))
322                         {
323                                 self.ready = TRUE;
324                                 bprint(self.netname, "^2 is ready\n");
325                                 ReadyCount();
326                         } else {
327                                 sprint(self, "^1game has already been restarted\n");
328                         }
329                 }
330         } else if(argv(0) == "maplist") {
331                 local float n, j;
332                 local string col;
333                 n = tokenize(cvar_string("g_maplist"));
334                 sprint(self, "^7Maps in list: ");
335                 for(i = 0, j = 0; i < n; ++i)
336                 {
337                         if(MapInfo_CheckMap(argv(i)))
338                         {
339                                 if(mod(j, 2))
340                                         col = "^2";
341                                 else
342                                         col = "^3";
343                                 sprint(self, strcat(col, argv(i), " "));
344                                 ++j;
345                         }
346                 }
347                 sprint(self, "\n");
348         } else if(argv(0) == "lsmaps") {
349                 sprint(self, "^7Maps available: ");
350                 for(i = 0; i < MapInfo_count; ++i)
351                 {
352                         if(mod(i, 2))
353                                 col = "^2";
354                         else
355                                 col = "^3";
356                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
357                 }
358                 sprint(self, "\n");
359         } else if(argv(0) == "voice") {
360                 VoiceMessage(argv(1));
361         } else if(argv(0) == "say") {
362                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
363                 //clientcommand(self, formatmessage(s));
364         } else if(argv(0) == "say_team") {
365                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
366                 //clientcommand(self, formatmessage(s));
367         } else if(argv(0) == "info") {
368                 cmd = cvar_string(strcat("sv_info_", argv(1)));
369                 if(cmd == "")
370                         sprint(self, "ERROR: unsupported info command\n");
371                 else
372                         wordwrap_sprint(cmd, 1111);
373         } else if(argv(0) == "suggestmap") {
374                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
375         } else if(argv(0) == "calltimeout") {
376                 if not(self.flags & FL_CLIENT)
377                         return;
378                 if(cvar("sv_timeout")) {
379                         if(self.classname == "player") {
380                                 if(votecalled)
381                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
382                                 else
383                                         evaluateTimeoutCall();
384                         }
385                         else
386                                 sprint(self, "^7Error: only players can call a timeout!\n");
387                 }
388         } else if(argv(0) == "resumegame") {
389                 if not(self.flags & FL_CLIENT)
390                         return;
391                 if(cvar("sv_timeout")) {
392                         evaluateResumeGame();
393                 }
394         } else if(argv(0) == "teamstatus") {
395                 Score_NicePrint(self);
396         } else {
397                 //if(ctf_clientcommand())
398                 //      return;
399                 cmd = argv(0);
400                 /* checks not needed any more since DP has separated clientcommands and regular commands
401                 if(cmd != "status")
402                 if(cmd != "name")
403                 //if(cmd != "say") // handled above
404                 //if(cmd != "say_team") // handled above
405                 if(cmd != "tell")
406                 if(cmd != "color")
407                 if(cmd != "kill")
408                 if(cmd != "pause")
409                 if(cmd != "kick")
410                 if(cmd != "ping")
411                 if(cmd != "pings")
412                 if(cmd != "ban")
413                 if(cmd != "pmodel")
414                 if(cmd != "rate")
415                 if(cmd != "playermodel")
416                 if(cmd != "playerskin")
417                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
418                 {
419                         print("WARNING: Invalid clientcommand by ", self.netname, ": ", s, "\n");
420                         return;
421                 }
422                 */
423                 clientcommand(self,s);
424         }
425 }
426
427 void ReadyRestart()
428 {
429         local entity e;
430
431         bprint("^1Server is restarting...\n");
432
433         VoteReset();
434
435         // no arena, assault support yet...
436         if(g_arena | g_assault | gameover | intermission_running | race_completing)
437                 localcmd("restart\n");
438
439         // clear overtime
440         if(checkrules_overtimeend)
441                 checkrules_overtimeend = 0;
442
443         restart_countdown = time + RESTART_COUNTDOWN;
444         restart_mapalreadyrestarted = 0; //reset this var, needed when cvar sv_ready_restart_repeatable is in use
445         if(0 < cvar("timelimit") || inWarmupStage)
446         {
447                 // remember original timelimit on first restart
448                 if(!timelimit_orig)
449                         timelimit_orig = cvar("timelimit");
450                 //only set the new timelimit if, when loading the map, a timelimit was really set
451                 if(timelimit_orig)
452                         cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
453         }
454
455         inWarmupStage = 0; //once the game is restarted the game is in match stage
456
457         //reset the .ready status of all players (also spectators)
458         FOR_EACH_CLIENTSLOT(e)
459                 e.ready = 0;
460         FOR_EACH_REALCLIENT(e)
461         {
462                 msg_entity = e;
463                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
464                 WriteByte(MSG_ONE, TE_CSQC_WARMUP);
465                 WriteByte(MSG_ONE, 0);
466         }
467
468         if(cvar("teamplay_lockonrestart") && teams_matter) {
469                 lockteams = 1;
470                 bprint("^1The teams are now locked.\n");
471         }
472         
473         //initiate the restart-countdown-announcer entity
474         restartAnnouncer = spawn();
475         restartAnnouncer.think = restartAnnouncer_Think;
476         restartAnnouncer.nextthink = time;
477         restartAnnouncer.cnt = RESTART_COUNTDOWN;
478         
479         //after a restart every players number of allowed timeouts gets reset, too
480         if(cvar("sv_timeout"))
481         {
482                 FOR_EACH_REALPLAYER(e)
483                         e.allowedTimeouts = cvar("sv_timeout_number");
484         }
485
486         //play the prepareforbattle sound to everyone
487         play2all("announcer/robotic/prepareforbattle.wav");
488
489         //reset map immediately if this cvar is not set
490         if (!cvar("sv_ready_restart_after_countdown"))
491                 reset_map();
492
493         // reset ALL scores
494         Score_ClearAll();
495         
496         if(cvar("sv_eventlog"))
497                 GameLogEcho(":restart");
498 }
499
500 /**
501  * Counts how many players are ready. If not enough players are ready, the function
502  * does nothing. If all players are ready, the timelimit will be extended and the
503  * restart_countdown variable is set to allow other functions like PlayerPostThink
504  * to detect that the countdown is now active. If the cvar sv_ready_restart_after_countdown
505  * is not set the map will be resetted.
506  * 
507  * Function is called after the server receives a 'ready' sign from a player.
508  */
509 void ReadyCount()
510 {
511         local entity e;
512         local float r, p;
513
514         r = p = 0;
515
516         FOR_EACH_REALPLAYER(e)
517         {
518                 p += 1;
519                 if(e.ready)
520                         r += 1;
521         }
522
523         readycount = r;
524
525         if(r) // at least one is ready
526         if(r == p) // and, everyone is ready
527                 ReadyRestart();
528
529         Nagger_ReadyCounted();
530 }
531
532 /**
533  * Shows the restart countdown for all players.
534  * Plays the countdown sounds for the seconds 3, 2 1, begin for everyone.
535  * Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown
536  * is set to 1).
537  */
538 void restartAnnouncer_Think() {
539         local entity plr;
540         local string s;
541         if(self.cnt <= 0) { //show the "Begin" message and
542                 if (cvar("sv_ready_restart_after_countdown")) {
543                         restart_mapalreadyrestarted = 1;
544                         reset_map();
545                 }
546
547                 FOR_EACH_REALCLIENT(plr) {
548                         if(plr.classname == "player") {
549                                 s = strcat(NEWLINES, "^1Begin!");
550                                 centerprint(plr, s);
551                         }
552                 }
553                 play2all("announcer/robotic/begin.wav");
554
555                 remove(self);
556                 return;
557         }
558         else {
559                 FOR_EACH_REALCLIENT(plr) {
560                         if(plr.classname == "player") {
561                                 s = strcat(NEWLINES, "^1Game starts in ", ftos(self.cnt), " seconds");
562                                 centerprint(plr, s);
563                         }
564                 }
565
566                 if(self.cnt <= 3) {
567                         play2all(strcat("announcer/robotic/", ftos(self.cnt), ".ogg"));
568                 }
569                 self.nextthink = time + 1;
570                 self.cnt -= 1;
571         }
572 }
573
574 /**
575  * Checks whether the player who calls the timeout is allowed to do so.
576  * If so, it initializes the timeout countdown. It also checks whether another
577  * timeout was already running at this time and reacts correspondingly.
578  *
579  * affected globals/fields: .allowedTimeouts, remainingTimeoutTime, remainingLeadTime,
580  *                          timeoutInitiator, timeoutStatus, timeoutHandler
581  *
582  * This function is called when a player issues the calltimeout command.
583  */
584 void evaluateTimeoutCall() {
585         if (inWarmupStage && !g_warmup_allow_timeout)
586                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
587         if (time < restart_countdown )
588                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
589         if (timeoutStatus != 2) {
590                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
591                 if (cvar("timelimit")) {
592                         //a timelimit was used
593                         local float myTl;
594                         if (cvar("timelimit"))
595                                 myTl = cvar("timelimit");
596                         else
597                                 myTl = timelimit_orig;
598
599                         local float lastPossibleTimeout;
600                         lastPossibleTimeout = (myTl*60) - cvar("sv_timeout_leadtime") - 1;
601
602                         if (lastPossibleTimeout < time)
603                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
604                 }
605         }
606         //player may not call a timeout if he has no calls left
607         if (self.allowedTimeouts < 1)
608                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
609         //now all required checks are passed
610         self.allowedTimeouts -= 1;
611         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)
612         remainingTimeoutTime = cvar("sv_timeout_length");
613         remainingLeadTime = cvar("sv_timeout_leadtime");
614         timeoutInitiator = self;
615         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
616                 timeoutStatus = 1;
617                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
618                 timeoutHandler = spawn();
619                 timeoutHandler.think = timeoutHandler_Think;
620         }
621         timeoutHandler.nextthink = time; //always let the entity think asap
622
623         //inform all connected clients about the timeout call
624         play2all("announcer/robotic/timeoutcalled.wav");
625 }
626
627 /**
628  * Checks whether a player is allowed to resume the game. If he is allowed to do it,
629  * and the lead time for the timeout is still active, this countdown just will be aborted (the
630  * game will never be paused). Otherwise the remainingTimeoutTime will be set to the corresponding
631  * value of the cvar sv_timeout_resumetime.
632  *
633  * This function is called when a player issues the resumegame command.
634  */
635 void evaluateResumeGame() {
636         if (!timeoutStatus)
637                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
638         if (self != timeoutInitiator)
639                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
640         if (timeoutStatus == 1) {
641                 remainingTimeoutTime = timeoutStatus = 0;
642                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
643                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
644         }
645         else if (timeoutStatus == 2) {
646                 //only shorten the remainingTimeoutTime if it makes sense
647                 if( remainingTimeoutTime > (cvar("sv_timeout_resumetime") + 1) ) {
648                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
649                         remainingTimeoutTime = cvar("sv_timeout_resumetime");
650                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
651                 }
652                 else
653                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
654
655         }
656 }