]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/gamec/clientcommands.c
add g_navnodeedit
[divverent/nexuiz.git] / data / qcsrc / gamec / clientcommands.c
1 void ReadyCount();
2
3 void SV_ParseClientCommand(string s) {
4         local float index;
5
6         tokenize(s);
7
8         if(argv(0) == "clogin") {
9                 if(cvar("sv_clientcommands")) {
10                         if(self.adminstatus < -5) {
11                                 sprint(self, "Too many unsuccessful tries.\n");
12                         } else if(argv(1) == cvar_string("sv_clientcommands_password")) {
13                                 self.adminstatus = 1;
14                                 sprint(self, "You now have remote admin status.\n");
15                         } else {
16                                 sprint(self, "Wrong password.\n");
17                                 // use of -- produces compiler warning in the if() line???
18                                 self.adminstatus = self.adminstatus - 1;
19                                 if(self.adminstatus == 0)
20                                         sprint(self, "You lost remote admin status.\n");
21                         }
22                 } else {
23                         sprint(self, "Clientside commands NOT allowed.\n");
24                 }
25         } else if(argv(0) == "ccmd") {
26                 if(cvar("sv_clientcommands")) {
27                         if(self.adminstatus > 0) {
28                                 local string command;
29                                 command = argv(1);
30                                 index = 2;
31                                 while(argv(index) != "") {
32                                         command = strcat(command, " ", argv(index));
33                                         index++;
34                                 }
35                                 localcmd(command);
36                         } else
37                                 sprint(self, "You don't have remote admin status.\n");
38                 } else {
39                         sprint(self, "Clientside commands NOT allowed.\n");
40                 }
41         } else if(argv(0) == "vote") {
42                 if(argv(1) == "help") {
43                         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");
44                         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");
45                         sprint(self, "^7\"^2help^7\" shows this info.\n");
46                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
47                         sprint(self, "^7\"^2call^7\" is used to call a vote. See the list of allowed commands.\n");
48                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
49                         sprint(self, "^7\"^2master^7\" is used to call a vote to become a master.\n");
50                         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");
51                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
52                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
53                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
54                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
55                         sprint(self, "^7You can call a vote for or execute these commands:\n");
56                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
57                 } else if(argv(1) == "status") {
58                         if(votecalled) {
59                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
60                         } else {
61                                 sprint(self, "^1No vote called.\n");
62                         }
63                 } else if(argv(1) == "call") {
64                         if(cvar("sv_vote_call")) {
65                                 if(votecalled) {
66                                         sprint(self, "^1There is already a vote called.\n");
67                                 } else {
68                                         local string vote;
69                                         vote = VoteParse();
70                                         if(vote == "") {
71                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
72                                         } else if(time < self.vote_next) {
73                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
74                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
75                                                 votecalled = TRUE;
76                                                 votecalledmaster = FALSE;
77                                                 votecalledvote = strzone(vote);
78                                                 votecaller = self; // remember who called the vote
79                                                 votecaller.vote_vote = 1; // of course you vote yes
80                                                 votecaller.vote_finished = time + cvar("sv_vote_timeout");
81                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
82                                                 bprint(strcat("^3Vote for \"^1", votecalledvote, "^3\" called by \"^7", votecaller.netname, "^3\".\n"));
83                                                 VoteCount(); // needed if you are the only one
84                                         } else {
85                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
86                                         }
87                                 }
88                         } else {
89                                 sprint(self, "^1Vote calling is NOT allowed.\n");
90                         }
91                 } else if(argv(1) == "stop") {
92                         if(!votecalled) {
93                                 sprint(self, "^1No vote called.\n");
94                         } else if(self == votecaller
95                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
96                                 VoteStop(self);
97                         } else {
98                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
99                         }
100                 } else if(argv(1) == "master") {
101                         if(cvar("sv_vote_master")) {
102                                 if(votecalled) {
103                                         sprint(self, "^1There is already a vote called.\n");
104                                 } else {
105                                         votecalled = TRUE;
106                                         votecalledmaster = TRUE;
107                                         votecalledvote = strzone("^3master");
108                                         votecaller = self; // remember who called the vote
109                                         votecaller.vote_vote = 1; // of course you vote yes
110                                         votecaller.vote_finished = time + cvar("sv_vote_timeout");
111                                         votecaller.vote_next = time + cvar("sv_vote_wait");
112                                         bprint(strcat("\"^7", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
113                                         VoteCount(); // needed if you are the only one
114                                 }
115                         } else {
116                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
117                         }
118                 } else if(argv(1) == "do") {
119                         if(self.vote_master) {
120                                 local string dovote;
121                                 dovote = VoteParse();
122                                 if(dovote == "") {
123                                         sprint(self, "^1Your command was empty. See help for more info.\n");
124                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
125                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
126                                         localcmd(dovote);
127                                 } else {
128                                         sprint(self, "^1This command is not ok. See help for more info.\n");
129                                 }
130                         } else {
131                                 sprint(self, "^1You are NOT a master.\n");
132                         }
133                 } else if(argv(1) == "yes") {
134                         if(!votecalled) {
135                                 sprint(self, "^1No vote called.\n");
136                         } else if(self.vote_vote == 0
137                                   || cvar("sv_vote_change")) {
138                                 sprint(self, "^1You accepted the vote.\n");
139                                 self.vote_vote = 1;
140                                 if(!cvar("sv_vote_singlecount")) {
141                                         VoteCount();
142                                 }
143                         } else {
144                                 sprint(self, "^1You have already voted.\n");
145                         }
146                 } else if(argv(1) == "no") {
147                         if(!votecalled) {
148                                 sprint(self, "^1No vote called.\n");
149                         } else if(self.vote_vote == 0
150                                   || cvar("sv_vote_change")) {
151                                 sprint(self, "^1You rejected the vote.\n");
152                                 self.vote_vote = -1;
153                                 if(!cvar("sv_vote_singlecount")) {
154                                         VoteCount();
155                                 }
156                         } else {
157                                 sprint(self, "^1You have already voted.\n");
158                         }
159                 } else {
160                         // ignore this?
161                         sprint(self, "^1Unknown vote command.\n");
162                 }
163         } else if(argv(0) == "autoswitch") {
164                 // be backwards compatible with older clients (enabled)
165                 self.autoswitch = ("0" != argv(1));
166                 local string autoswitchmsg;
167                 if (self.autoswitch) {
168                         autoswitchmsg = "on";
169                 } else {
170                         autoswitchmsg = "off";
171                 }
172                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
173         } else if(argv(0) == "clientversion") {
174                 if (argv(1) == "$g_nexuizversion_major") {
175                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
176                         // either that or someone wants to be funny
177                         self.version = 1;
178                 } else {
179                         self.version = stof(argv(1));
180                 }
181         } else if(argv(0) == "spectate") {
182                 if(cvar("g_lms"))
183                         return; // don't allow spectating in lms, unless player runs out of lives
184                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
185                         self.classname = "observer";
186                         PutClientInServer();
187                 }
188         } else if(argv(0) == "join") {
189                 self.classname = "player";
190                 self.frags = 0;
191                 // TODO: I have no idea whether this is needed or not
192                 if(!cvar("g_lms")) {
193                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
194                 }
195                 PutClientInServer();
196         } else if( argv(0) == "selectteam" ) {
197                 if( argv(1) == "none" ) {
198                         SV_ChangeTeam( 0 );
199                 } else if( argv(1) == "red" ) {
200                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
201                 } else if( argv(1) == "blue" ) {
202                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
203                 } else if( argv(1) == "pink" ) {
204                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
205                 } else if( argv(1) == "yellow" ) {
206                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
207                 } else if( argv(1) == "auto" ) {
208                         JoinBestTeam( self, 0 );
209                 } else {
210                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
211                 }
212         } else if(argv(0) == "ready") {
213                 if(cvar("sv_ready_restart"))
214                 {
215                         self.ready = TRUE;
216                         ReadyCount();
217                 }
218         } else {
219                 clientcommand(self,s);
220         }
221 }
222
223 void VoteThink() {
224         if(self.vote_finished > 0 // this player has called a vote
225             && time > self.vote_finished) // time is up
226         {
227                 VoteCount();
228         }
229 }
230
231 string VoteParse() {
232         local float index;
233         index = 3;
234         local string vote;
235         vote = argv(2);
236         while(argv(index) != "") {
237                 vote = strcat(vote, " ", argv(index));
238                 index++;
239         }
240
241         // necessary for some of the string operations
242         vote = strzone(vote);
243
244         // now we remove some things that could be misused
245         index = 0;
246         local float found;
247         found = FALSE;
248         local float votelength;
249         votelength = strlen(vote);
250         while(!found && index < votelength)
251         {
252                 local string badchar;
253                 badchar = substring(vote, index, 1);
254                 if(badchar == ";"
255                    || badchar == "\n")
256                 {
257                         found = TRUE;
258                 } else {
259                         index++;
260                 }
261         }
262         return substring(vote, 0, index);
263 }
264
265 float VoteAllowed(string votecommand) {
266         tokenize(cvar_string("sv_vote_commands"));
267         local float index;
268         index = 0;
269         while(argv(index) != "") {
270                 local string allowed;
271                 allowed = argv(index);
272                 if(votecommand == allowed) {
273                         return TRUE;
274                 }
275                 index++;
276         }
277         return FALSE;
278 }
279
280 void VoteReset() {
281         local string searchclass;
282         searchclass = "player";
283
284         while (TRUE)
285         {
286                 local entity player;
287                 player = find(player, classname, searchclass);
288                 while(player)
289                 {
290                         player.vote_vote = 0;
291                         player.vote_finished = 0;
292                         player = find(player, classname, searchclass);
293                 }
294
295                 if("player" == searchclass) {
296                         searchclass = "observer";
297                 } else if("observer" == searchclass) {
298                         searchclass = "spectator";
299                 } else {
300                         break;
301                 }
302         }
303
304         votecalled = FALSE;
305         votecalledmaster = FALSE;
306 }
307
308 void VoteAccept() {
309         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
310         if(votecalledmaster)
311         {
312                 votecaller.vote_master = 1;
313         } else {
314                 localcmd(votecalledvote);
315         }
316         votecaller.vote_next = 0; // people like your votes, no wait for next vote
317         VoteReset();
318 }
319
320 void VoteReject() {
321         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
322         VoteReset();
323 }
324
325 void VoteTimeout() {
326         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
327         VoteReset();
328 }
329
330 void VoteStop(entity stopper) {
331         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
332         if(stopper == votecaller) {
333                 // no wait for next vote so you can correct your vote
334                 votecaller.vote_next = 0;
335         }
336         VoteReset();
337 }
338
339 void VoteCount() {
340         local float playercount;
341         playercount = 0;
342         local float yescount;
343         yescount = 0;
344         local float nocount;
345         nocount = 0;
346         local string searchclass;
347         searchclass = "player";
348
349         while (TRUE)
350         {
351                 local entity player;
352                 player = find(player, classname, searchclass);
353
354                 while(player)
355                 {
356                         if(clienttype(player) != CLIENTTYPE_BOT) {
357                                 if(player.vote_vote < 0) {
358                                         nocount++;
359                                 } else if(player.vote_vote > 0) {
360                                         yescount++;
361                                 }
362                                 playercount++;
363                         }
364                         player = find(player, classname, searchclass);
365                 }
366
367                 if("player" == searchclass) {
368                         searchclass = "observer";
369                 } else if("observer" == searchclass) {
370                         searchclass = "specator";
371                 } else {
372                         break;
373                 }
374         }
375
376         if((playercount / 2) < yescount) { // vote accepted
377                 VoteAccept();
378         } else if((playercount / 2) < nocount) { // vote rejected
379                 VoteReject();
380         } else if(time > votecaller.vote_finished) { // vote timedout
381                 VoteTimeout();
382         } // else still running
383 }
384
385
386 void ReadyCount()
387 {
388         local entity e;
389         local float r, p;
390
391         e = find(world, classname, "player");
392
393         while(e)
394         {
395                 if(clienttype(e) == CLIENTTYPE_REAL)
396                 {
397                         p += 1;
398                         if(e.ready) r += 1;
399                 }
400                 e = find(e, classname, "player");
401         }
402
403         if(p && r == p)
404         {
405                 bprint("^1Server is restarting...\n");
406                 localcmd("restart\n");
407         }
408 }