]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
so this is how you do local sounds...
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 float ValidateMap(string vote);
3
4 void Say(entity source, float teamsay, string msgin)
5 {
6         string msgstr, colorstr;
7         entity head;
8
9         msgin = formatmessage(msgin);
10
11         if(source.team == COLOR_TEAM1)
12                 colorstr = "^1";
13         else if(source.team == COLOR_TEAM2)
14                 colorstr = "^4";
15         else if(source.team == COLOR_TEAM3)
16                 colorstr = "^6";
17         else if(source.team == COLOR_TEAM4)
18                 colorstr = "^3";
19         else
20                 colorstr = "^7";
21
22         if(!teams_matter)
23                 teamsay = FALSE;
24
25         if(source.classname != "player") // observers can't
26                 teamsay = FALSE;
27
28         // how can we prevent the message from appearing in a listen server?
29         // for now, just give "say" back and only handle say_team
30         if(!teamsay)
31         {
32                 clientcommand(self, strcat("say ", msgin));
33                 return;
34         }
35
36         if(teamsay)
37                 msgstr = strcat(colorstr, "(^3", source.netname, colorstr, ") ^7", msgin, "\n");
38         else
39                 msgstr = strcat("^3", source.netname, "^7: ", msgin, "\n");
40
41         head = find(world, classname, "player");
42         while(head)
43         {
44                 if(clienttype(head) == CLIENTTYPE_REAL)
45                         if(!teamsay || (head.team == source.team))
46                         {
47                                 sprint(head, msgstr);
48                                 stuffcmd(head, "play2 misc/talk.wav\n");
49                         }
50                 head = find(head, classname, "player");
51         }
52
53         if(!teamsay)
54         {
55                 head = find(world, classname, "observer");
56                 while(head)
57                 {
58                         if(clienttype(head) == CLIENTTYPE_REAL)
59                         {
60                                 sprint(head, msgstr);
61                                 stuffcmd(head, "play2 misc/talk.wav\n");
62                         }
63                         head = find(head, classname, "observer");
64                 }
65                 head = find(world, classname, "spectator");
66                 while(head)
67                 {
68                         if(clienttype(head) == CLIENTTYPE_REAL)
69                         {
70                                 sprint(head, msgstr);
71                                 stuffcmd(head, "play2 misc/talk.wav\n");
72                         }
73                         head = find(head, classname, "spectator");
74                 }
75                 ServerConsoleEcho(substring(msgstr, 0, strlen(msgstr) - 1), TRUE);
76         }
77 }
78
79 void SV_ParseClientCommand(string s) {
80         local float index;
81
82         tokenize(s);
83
84         if(argv(0) == "clogin") {
85                 if(cvar("sv_clientcommands")) {
86                         if(self.adminstatus < -5) {
87                                 sprint(self, "Too many unsuccessful tries.\n");
88                         } else if(argv(1) == cvar_string("sv_clientcommands_password")) {
89                                 self.adminstatus = 1;
90                                 sprint(self, "You now have remote admin status.\n");
91                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " received admin status"), TRUE);
92                         } else {
93                                 sprint(self, "Wrong password.\n");
94                                 // use of -- produces compiler warning in the if() line???
95                                 self.adminstatus = self.adminstatus - 1;
96                                 if(self.adminstatus == 0)
97                                 {
98                                         sprint(self, "You lost remote admin status.\n");
99                                         ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " lost admin status"), TRUE);
100                                 }
101                         }
102                 } else {
103                         sprint(self, "Clientside commands NOT allowed.\n");
104                 }
105         } else if(argv(0) == "ccmd") {
106                 if(cvar("sv_clientcommands")) {
107                         if(self.adminstatus > 0) {
108                                 local string command;
109                                 command = argv(1);
110                                 index = 2;
111                                 while(argv(index) != "") {
112                                         command = strcat(command, " ", argv(index));
113                                         index++;
114                                 }
115                                 command = strzone(command);
116                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " issued command '", command, "'"), TRUE);
117                                 localcmd(strcat(command, "\n"));
118                                 strunzone(command);
119                         } else
120                                 sprint(self, "You don't have remote admin status.\n");
121                 } else {
122                         sprint(self, "Clientside commands NOT allowed.\n");
123                 }
124         } else if(argv(0) == "vote") {
125                 if(argv(1) == "help") {
126                         local string vmasterdis;
127                         if(!cvar("sv_vote_master")) {
128                                 vmasterdis = " ^1(disabled)";
129                         }
130                         local string vcalldis;
131                         if(!cvar("sv_vote_call")) {
132                                 vcalldis = " ^1(disabled)";
133                         }
134                         sprint(self, "^7You can use voting with \"^2cmd vote help^7\" \"^2cmd vote status^7\" \"^2cmd vote call ^3COMMAND ARGUMENTS^7\" \"^2cmd vote stop^7\" \"^2cmd vote master^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\".\n");
135                         sprint(self, "^7Or if your version is up to date you can use these aliases \"^2vhelp^7\" \"^2vstatus^7\" \"^2vcall ^3COMMAND ARGUMENTS^7\" \"^2vstop^7\" \"^2vmaster^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\".\n");
136                         sprint(self, "^7\"^2help^7\" shows this info.\n");
137                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
138                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
139                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
140                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
141                         sprint(self, "^7\"^2do^7\" If you are a master you can execute a command without a vote. See the list of allowed commands.\n");
142                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
143                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
144                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
145                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
146                         sprint(self, "^7You can call a vote for or execute these commands:\n");
147                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
148                 } else if(argv(1) == "status") {
149                         if(votecalled) {
150                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
151                         } else {
152                                 sprint(self, "^1No vote called.\n");
153                         }
154                 } else if(argv(1) == "call") {
155                         if(cvar("sv_vote_call")) {
156                                 if(votecalled) {
157                                         sprint(self, "^1There is already a vote called.\n");
158                                 } else {
159                                         local string vote;
160                                         vote = VoteParse();
161                                         if(vote == "") {
162                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
163                                         } else if(time < self.vote_next) {
164                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
165                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
166                                                 if(!ValidateMap(vote))
167                                                         return;
168                                                 votecalled = TRUE;
169                                                 votecalledmaster = FALSE;
170                                                 // remap chmap to gomap (forces intermission)
171                                                 if(strlen(vote) >= 6)
172                                                         if(substring(vote, 0, 6) == "chmap ")
173                                                                 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
174                                                 votecalledvote = strzone(vote);
175                                                 votecaller = self; // remember who called the vote
176                                                 votefinished = time + cvar("sv_vote_timeout");
177                                                 votecaller.vote_vote = 1; // of course you vote yes
178                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
179                                                 bprint(strcat("^3Vote for \"^1", votecalledvote, "^3\" called by \"^7", votecaller.netname, "^3\".\n"));
180                                                 VoteCount(); // needed if you are the only one
181                                         } else {
182                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
183                                         }
184                                 }
185                         } else {
186                                 sprint(self, "^1Vote calling is NOT allowed.\n");
187                         }
188                 } else if(argv(1) == "stop") {
189                         if(!votecalled) {
190                                 sprint(self, "^1No vote called.\n");
191                         } else if(self == votecaller
192                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
193                                 VoteStop(self);
194                         } else {
195                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
196                         }
197                 } else if(argv(1) == "master") {
198                         if(cvar("sv_vote_master")) {
199                                 if(votecalled) {
200                                         sprint(self, "^1There is already a vote called.\n");
201                                 } else {
202                                         votecalled = TRUE;
203                                         votecalledmaster = TRUE;
204                                         votecalledvote = strzone("^3master");
205                                         votecaller = self; // remember who called the vote
206                                         votefinished = time + cvar("sv_vote_timeout");
207                                         votecaller.vote_vote = 1; // of course you vote yes
208                                         votecaller.vote_next = time + cvar("sv_vote_wait");
209                                         bprint(strcat("\"^3", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
210                                         VoteCount(); // needed if you are the only one
211                                 }
212                         } else {
213                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
214                         }
215                 } else if(argv(1) == "do") {
216                         if(self.vote_master) {
217                                 local string dovote;
218                                 dovote = VoteParse();
219                                 if(dovote == "") {
220                                         sprint(self, "^1Your command was empty. See help for more info.\n");
221                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
222                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
223                                         localcmd(strcat(dovote, "\n"));
224                                 } else {
225                                         sprint(self, "^1This command is not ok. See help for more info.\n");
226                                 }
227                         } else {
228                                 sprint(self, "^1You are NOT a master.\n");
229                         }
230                 } else if(argv(1) == "yes") {
231                         if(!votecalled) {
232                                 sprint(self, "^1No vote called.\n");
233                         } else if(self.vote_vote == 0
234                                   || cvar("sv_vote_change")) {
235                                 sprint(self, "^1You accepted the vote.\n");
236                                 self.vote_vote = 1;
237                                 if(!cvar("sv_vote_singlecount")) {
238                                         VoteCount();
239                                 }
240                         } else {
241                                 sprint(self, "^1You have already voted.\n");
242                         }
243                 } else if(argv(1) == "no") {
244                         if(!votecalled) {
245                                 sprint(self, "^1No vote called.\n");
246                         } else if(self.vote_vote == 0
247                                   || cvar("sv_vote_change")) {
248                                 sprint(self, "^1You rejected the vote.\n");
249                                 self.vote_vote = -1;
250                                 if(!cvar("sv_vote_singlecount")) {
251                                         VoteCount();
252                                 }
253                         } else {
254                                 sprint(self, "^1You have already voted.\n");
255                         }
256                 } else {
257                         // ignore this?
258                         sprint(self, "^1Unknown vote command.\n");
259                 }
260         } else if(argv(0) == "autoswitch") {
261                 // be backwards compatible with older clients (enabled)
262                 self.autoswitch = ("0" != argv(1));
263                 local string autoswitchmsg;
264                 if (self.autoswitch) {
265                         autoswitchmsg = "on";
266                 } else {
267                         autoswitchmsg = "off";
268                 }
269                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
270         } else if(argv(0) == "clientversion") {
271                 if (argv(1) == "$gameversion") {
272                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
273                         // either that or someone wants to be funny
274                         self.version = 1;
275                 } else {
276                         self.version = stof(argv(1));
277                 }
278                 if(self.version != cvar("gameversion")) 
279                 {
280                         self.classname = "observer";
281                         self.frags = -2;
282                         PutClientInServer();
283                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
284                         //JoinBestTeam(self, 0);
285                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
286                         self.classname = "observer";
287                         stuffcmd(self,"menu_showteamselect\n");
288                 }
289         } else if(argv(0) == "reportcvar") {
290                 GetCvars(1);
291         } else if(argv(0) == "spectate") {
292                 if(cvar("g_lms") || cvar("g_arena"))
293                         return; // don't allow spectating in lms, unless player runs out of lives
294                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
295                         self.classname = "observer";
296                         PutClientInServer();
297                 }
298         } else if(argv(0) == "join") {
299                 if(!cvar("g_arena"))
300                 if (self.classname != "player")
301                 {
302                         self.classname = "player";
303                         self.frags = 0;
304                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
305                         PutClientInServer();
306                 }
307         } else if( argv(0) == "selectteam" ) {
308                 if( !cvar("teamplay") ) {
309                         sprint( self, "selecteam can only be used in teamgames\n");
310                 } else if(cvar("g_campaign")) {
311                         //JoinBestTeam(self, 0);
312                 } else if( argv(1) == "none" ) {
313                         SV_ChangeTeam( 0 );
314                 } else if( argv(1) == "red" ) {
315                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
316                 } else if( argv(1) == "blue" ) {
317                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
318                 } else if( argv(1) == "pink" ) {
319                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
320                 } else if( argv(1) == "yellow" ) {
321                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
322                 } else if( argv(1) == "auto" ) {
323                         self.team = -1;
324                         JoinBestTeam( self, 0 );
325                 } else {
326                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
327                 }
328         } else if(argv(0) == "ready") {
329                 if(cvar("sv_ready_restart"))
330                 {
331                         self.ready = TRUE;
332                         bprint(self.netname, "^2 is ready\n");
333                         ReadyCount();
334                 }
335         } else if(argv(0) == "say") {
336                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
337                 //clientcommand(self, formatmessage(s));
338         } else if(argv(0) == "say_team") {
339                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
340                 //clientcommand(self, formatmessage(s));
341         } else {
342                 clientcommand(self,s);
343         }
344 }
345
346 float ValidateMap(string vote)
347 {
348         string ext;
349         
350         tokenize(vote);
351         if(argv(0) == "map" || argv(0) == "changelevel")
352                 ext = ".bsp";
353         else if(argv(0) == "chmap")
354                 ext = ".mapcfg";
355         else if(argv(0) == "gotomap")
356                 ext = ".mapcfg";
357         else
358                 return TRUE;
359
360         if(!TryFile(strcat("maps/", argv(1), ext)))
361         {
362                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
363                 return FALSE;
364         }
365         return TRUE;
366 }
367
368
369 void VoteThink() {
370         if(votefinished > 0 // a vote was called
371             && time > votefinished) // time is up
372         {
373                 VoteCount();
374         }
375 }
376
377 string VoteParse() {
378         local float index;
379         index = 3;
380         local string vote;
381         vote = argv(2);
382         while(argv(index) != "") {
383                 vote = strcat(vote, " ", argv(index));
384                 index++;
385         }
386
387         // necessary for some of the string operations
388         vote = strzone(vote);
389
390         // now we remove some things that could be misused
391         index = 0;
392         local float found;
393         found = FALSE;
394         local float votelength;
395         votelength = strlen(vote);
396         while(!found && index < votelength)
397         {
398                 local string badchar;
399                 badchar = substring(vote, index, 1);
400                 if(badchar == ";"
401                    || badchar == "\r"
402                    || badchar == "\n")
403                 {
404                         found = TRUE;
405                 } else {
406                         index++;
407                 }
408         }
409         return substring(vote, 0, index);
410 }
411
412 float VoteAllowed(string votecommand) {
413         tokenize(cvar_string("sv_vote_commands"));
414         local float index;
415         index = 0;
416         while(argv(index) != "") {
417                 local string allowed;
418                 allowed = argv(index);
419                 if(votecommand == allowed) {
420                         return TRUE;
421                 }
422                 index++;
423         }
424         return FALSE;
425 }
426
427 void VoteReset() {
428         local string searchclass;
429         searchclass = "player";
430
431         while (TRUE)
432         {
433                 local entity player;
434                 player = find(player, classname, searchclass);
435                 while(player)
436                 {
437                         player.vote_vote = 0;
438                         player = find(player, classname, searchclass);
439                 }
440
441                 if("player" == searchclass) {
442                         searchclass = "observer";
443                 } else if("observer" == searchclass) {
444                         searchclass = "spectator";
445                 } else {
446                         break;
447                 }
448         }
449
450         votecalled = FALSE;
451         votecalledmaster = FALSE;
452         votefinished = 0;
453 }
454
455 void VoteAccept() {
456         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
457         if(votecalledmaster)
458         {
459                 votecaller.vote_master = 1;
460         } else {
461                 localcmd(strcat(votecalledvote, "\n"));
462         }
463         votecaller.vote_next = 0; // people like your votes, no wait for next vote
464         VoteReset();
465 }
466
467 void VoteReject() {
468         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
469         VoteReset();
470 }
471
472 void VoteTimeout() {
473         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
474         VoteReset();
475 }
476
477 void VoteStop(entity stopper) {
478         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
479         if(stopper == votecaller) {
480                 // no wait for next vote so you can correct your vote
481                 votecaller.vote_next = 0;
482         }
483         VoteReset();
484 }
485
486 void VoteCount() {
487         local float playercount;
488         playercount = 0;
489         local float yescount;
490         yescount = 0;
491         local float nocount;
492         nocount = 0;
493         local string searchclass;
494         searchclass = "player";
495
496         while (TRUE)
497         {
498                 local entity player;
499                 player = find(player, classname, searchclass);
500
501                 while(player)
502                 {
503                         if(clienttype(player) != CLIENTTYPE_BOT) {
504                                 if(player.vote_vote < 0) {
505                                         nocount++;
506                                 } else if(player.vote_vote > 0) {
507                                         yescount++;
508                                 }
509                                 playercount++;
510                         }
511                         player = find(player, classname, searchclass);
512                 }
513
514                 if("player" == searchclass) {
515                         searchclass = "observer";
516                 } else if("observer" == searchclass) {
517                         searchclass = "specator";
518                 } else {
519                         break;
520                 }
521         }
522
523         if((playercount == 1) && votecalledmaster) {
524                 // if only one player is on the server becoming vote
525                 // master is not allowed.  This could be used for
526                 // trolling or worse. 'self' is the user who has
527                 // called the vote because this function is called
528                 // by SV_ParseClientCommand. Maybe all voting should
529                 // be disabled for a single player?
530                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
531                 votecaller.vote_next = 0;
532                 VoteReset();
533         } else if((playercount / 2) < yescount) { // vote accepted
534                 VoteAccept();
535         } else if((playercount / 2) < nocount) { // vote rejected
536                 VoteReject();
537         } else if(time > votefinished) { // vote timedout
538                 VoteTimeout();
539         } // else still running
540 }
541
542
543 void ReadyCount()
544 {
545         local entity e;
546         local float r, p;
547
548         e = find(world, classname, "player");
549
550         while(e)
551         {
552                 if(clienttype(e) == CLIENTTYPE_REAL)
553                 {
554                         p += 1;
555                         if(e.ready) r += 1;
556                 }
557                 e = find(e, classname, "player");
558         }
559
560         if(p && r == p)
561         {
562                 bprint("^1Server is restarting...\n");
563                 localcmd("restart\n");
564         }
565 }