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