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