]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/common.qc
Now make everything else compile too
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / common.qc
1 // ====================================================
2 //  Shared code for server commands, written by Samual
3 //  Last updated: December 13th, 2011
4 // ====================================================
5
6 // find a player which matches the input string, and return their entity number
7 float GetFilteredNumber(string input)
8 {
9         entity tmp_player, selection;
10         float output, matches;
11         
12         // check and see if we can get a number from input like "#3" or "3" 
13         if(substring(input, 0, 1) == "#")
14                 output = stof(substring(input, 1, -1));
15         else
16                 output = stof(input);
17                 
18         // if we can't, check and see if we can match the input to the netname of any player in the game
19         if not(output) 
20         {
21                 FOR_EACH_CLIENT(tmp_player)
22                         if (strdecolorize(tmp_player.netname) == strdecolorize(input))
23                                 selection = tmp_player;
24
25                 if (selection) { output = num_for_edict(selection); }
26         }
27                 
28         print(strcat("input: ", input, ", output: ", ftos(output), ",\n"));
29         return output;
30 }
31
32 // switch between sprint and print depending on whether the reciever is the server or a player
33 void print_to(entity to, string input)
34 {
35     if(to)
36         sprint(to, strcat(input, "\n"));
37     else
38         print(input, "\n");
39 }
40
41
42 // ===================================================
43 //  Common commands used in both sv_cmd.qc and cmd.qc
44 // ===================================================
45
46 void CommonCommand_cvar_changes(float request)
47 {
48         switch(request)
49         {
50                 case CMD_REQUEST_COMMAND:
51                 {
52                         sprint(self, cvar_changes);
53                         return; // never fall through to usage
54                 }
55                         
56                 default:
57                 case CMD_REQUEST_USAGE:
58                 {
59                         sprint(self, "\nUsage:^3 sv_cmd cvar_changes\n");
60                         sprint(self, "  No arguments required.\n");
61                         //sprint(self, "See also: ^2cvar_purechanges^7\n");
62                         return;
63                 }
64         }
65 }
66
67 void CommonCommand_cvar_purechanges(float request)
68 {
69         switch(request)
70         {
71                 case CMD_REQUEST_COMMAND:
72                 {
73                         sprint(self, cvar_purechanges);
74                         return; // never fall through to usage
75                 }
76                         
77                 default:
78                 case CMD_REQUEST_USAGE:
79                 {
80                         sprint(self, "\nUsage:^3 sv_cmd cvar_purechanges\n");
81                         sprint(self, "  No arguments required.\n");
82                         //sprint(self, "See also: ^2cvar_changes^7\n");
83                         return;
84                 }
85         }
86 }
87
88 void CommonCommand_info(float request, float argc)
89 {       
90         switch(request)
91         {
92                 case CMD_REQUEST_COMMAND:
93                 {
94                         string command;
95                         
96                         command = builtin_cvar_string(strcat("sv_info_", argv(1))); 
97                         if(command)
98                                 wordwrap_sprint(command, 1111); // why 1111?
99                         else
100                                 sprint(self, "ERROR: unsupported info command\n");
101                                 
102                         return; // never fall through to usage
103                 }
104                         
105                 default:
106                 case CMD_REQUEST_USAGE:
107                 {
108                         sprint(self, "\nUsage:^3 cmd info request\n");
109                         sprint(self, "  Where 'request' is the suffixed string appended onto the request for cvar.\n");
110                         return;
111                 }
112         }
113 }
114
115 void CommonCommand_ladder(float request)
116 {
117         switch(request)
118         {
119                 case CMD_REQUEST_COMMAND:
120                 {
121                         sprint(self, ladder_reply);
122                         return; // never fall through to usage
123                 }
124                         
125                 default:
126                 case CMD_REQUEST_USAGE:
127                 {
128                         sprint(self, "\nUsage:^3 cmd ladder\n");
129                         sprint(self, "  No arguments required.\n");
130                         return;
131                 }
132         }
133 }
134
135 void CommonCommand_lsmaps(float request)
136 {
137         switch(request)
138         {
139                 case CMD_REQUEST_COMMAND:
140                 {
141                         sprint(self, lsmaps_reply);
142                         return; // never fall through to usage
143                 }
144                         
145                 default:
146                 case CMD_REQUEST_USAGE:
147                 {
148                         sprint(self, "\nUsage:^3 cmd lsmaps\n");
149                         sprint(self, "  No arguments required.\n");
150                         return;
151                 }
152         }
153 }
154
155 void CommonCommand_lsnewmaps(float request)
156 {
157         switch(request)
158         {
159                 case CMD_REQUEST_COMMAND:
160                 {
161                         sprint(self, lsnewmaps_reply);
162                         return; // never fall through to usage
163                 }
164                         
165                 default:
166                 case CMD_REQUEST_USAGE:
167                 {
168                         sprint(self, "\nUsage:^3 cmd lsnewmaps\n");
169                         sprint(self, "  No arguments required.\n");
170                         return;
171                 }
172         }
173 }
174
175 void CommonCommand_maplist(float request)
176 {
177         switch(request)
178         {
179                 case CMD_REQUEST_COMMAND:
180                 {
181                         sprint(self, maplist_reply);
182                         return; // never fall through to usage
183                 }
184                         
185                 default:
186                 case CMD_REQUEST_USAGE:
187                 {
188                         sprint(self, "\nUsage:^3 cmd maplist\n");
189                         sprint(self, "  No arguments required.\n");
190                         return;
191                 }
192         }
193 }
194
195 void CommonCommand_rankings(float request)
196 {
197         switch(request)
198         {
199                 case CMD_REQUEST_COMMAND:
200                 {
201                         sprint(self, rankings_reply);
202                         return; // never fall through to usage
203                 }
204                         
205                 default:
206                 case CMD_REQUEST_USAGE:
207                 {
208                         sprint(self, "\nUsage:^3 cmd rankings\n");
209                         sprint(self, "  No arguments required.\n");
210                         return;
211                 }
212         }
213 }
214
215 void CommonCommand_records(float request) // TODO: Isn't this flooding with the sprint messages? Old code, but perhaps bad?
216 {       
217         switch(request)
218         {
219                 case CMD_REQUEST_COMMAND:
220                 {
221                         float i;
222                         
223                         for(i = 0; i < 10; ++i)
224                                 sprint(self, records_reply[i]);
225                                 
226                         return; // never fall through to usage
227                 }
228                         
229                 default:
230                 case CMD_REQUEST_USAGE:
231                 {
232                         sprint(self, "\nUsage:^3 cmd records\n");
233                         sprint(self, "  No arguments required.\n");
234                         return;
235                 }
236         }
237 }
238
239 void CommonCommand_teamstatus(float request)
240 {
241         switch(request)
242         {
243                 case CMD_REQUEST_COMMAND:
244                 {
245                         Score_NicePrint(self);
246                         return; // never fall through to usage
247                 }
248                         
249                 default:
250                 case CMD_REQUEST_USAGE:
251                 {
252                         sprint(self, "\nUsage:^3 cmd teamstatus\n");
253                         sprint(self, "  No arguments required.\n");
254                         return;
255                 }
256         }
257 }
258
259
260 void CommonCommand_timein(float request)
261 {
262         switch(request)
263         {
264                 case CMD_REQUEST_COMMAND:
265                 {
266                         if(self.flags & FL_CLIENT)
267                         {
268                                 if(autocvar_sv_timeout)
269                                 {
270                                         if (!timeoutStatus)
271                                                 return sprint(self, "^7Error: There is no active timeout which could be aborted!\n");
272                                         if (self != timeoutInitiator)
273                                                 return sprint(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
274                                                 
275                                         if (timeoutStatus == 1) 
276                                         {
277                                                 remainingTimeoutTime = timeoutStatus = 0;
278                                                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
279                                                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
280                                         }
281                                         else if (timeoutStatus == 2) 
282                                         {
283                                                 //only shorten the remainingTimeoutTime if it makes sense
284                                                 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) ) 
285                                                 {
286                                                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
287                                                         remainingTimeoutTime = autocvar_sv_timeout_resumetime;
288                                                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
289                                                 }
290                                                 else
291                                                         sprint(self, "^7Error: Your resumegame call was discarded!\n");
292                                         }
293                                 }
294                         }
295                         return; // never fall through to usage
296                 }
297                         
298                 default:
299                 case CMD_REQUEST_USAGE:
300                 {
301                         sprint(self, "\nUsage:^3 cmd timein\n");
302                         sprint(self, "  No arguments required.\n");
303                         return;
304                 }
305         }
306 }
307
308 void CommonCommand_timeout(float request) // DEAR GOD THIS COMMAND IS TERRIBLE.
309 {
310         switch(request)
311         {
312                 case CMD_REQUEST_COMMAND:
313                 {
314                         if(self.flags & FL_CLIENT)
315                         {
316                                 if(autocvar_sv_timeout) 
317                                 {
318                                         if(self.classname == "player") 
319                                         {
320                                                 if(votecalled)
321                                                         sprint(self, "^7Error: you can not call a timeout while a vote is active!\n");
322                                                 else
323                                                 {
324                                                         if (inWarmupStage && !g_warmup_allow_timeout)
325                                                                 return sprint(self, "^7Error: You can not call a timeout in warmup-stage!\n");
326                                                         if (time < game_starttime )
327                                                                 return sprint(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
328                                                                 
329                                                         if (timeoutStatus != 2) {
330                                                                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
331                                                                 if (autocvar_timelimit) {
332                                                                         //a timelimit was used
333                                                                         float myTl;
334                                                                         myTl = autocvar_timelimit;
335
336                                                                         float lastPossibleTimeout;
337                                                                         lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
338
339                                                                         if (lastPossibleTimeout < time - game_starttime)
340                                                                                 return sprint(self, "^7Error: It is too late to call a timeout now!\n");
341                                                                 }
342                                                         }
343                                                         
344                                                         //player may not call a timeout if he has no calls left
345                                                         if (self.allowedTimeouts < 1)
346                                                                 return sprint(self, "^7Error: You already used all your timeout calls for this map!\n");
347                                                                 
348                                                                 
349                                                         //now all required checks are passed
350                                                         self.allowedTimeouts -= 1;
351                                                         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)
352                                                         remainingTimeoutTime = autocvar_sv_timeout_length;
353                                                         remainingLeadTime = autocvar_sv_timeout_leadtime;
354                                                         timeoutInitiator = self;
355                                                         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
356                                                                 timeoutStatus = 1;
357                                                                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
358                                                                 timeoutHandler = spawn();
359                                                                 timeoutHandler.think = timeoutHandler_Think;
360                                                         }
361                                                         timeoutHandler.nextthink = time; //always let the entity think asap
362
363                                                         //inform all connected clients about the timeout call
364                                                         Announce("timeoutcalled");
365                                                 }
366                                         }
367                                         else
368                                                 sprint(self, "^7Error: only players can call a timeout!\n");
369                                 }
370                         }
371                         return; // never fall through to usage
372                 }
373                         
374                 default:
375                 case CMD_REQUEST_USAGE:
376                 {
377                         sprint(self, "\nUsage:^3 cmd timeout\n");
378                         sprint(self, "  No arguments required.\n");
379                         return;
380                 }
381         }
382 }
383
384 void CommonCommand_who(float request)
385 {
386         switch(request)
387         {
388                 case CMD_REQUEST_COMMAND:
389                 {
390                         float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds;
391                         entity tmp_player;
392                         //string tmp_player_name;
393                         
394                         sprint(self, strcat("List of client information", (autocvar_sv_status_privacy ? " (some data is hidden for privacy)" : string_null), ":\n"));
395                         sprint(self, sprintf(" %-4s %-20s %-5s %-3s %-9s %-16s %s\n", "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
396                         
397                         FOR_EACH_CLIENT(tmp_player)
398                         {
399                                 //tmp_player_name = strlimitedlen(tmp_player.netname, "...", TRUE, 20);
400                                 
401                                 tmp_hours = tmp_minutes = tmp_seconds = 0;
402                                 
403                                 tmp_seconds = (time - tmp_player.jointime);
404                                 tmp_minutes = (tmp_seconds / 60);
405                                 
406                                 if(tmp_minutes)
407                                 {
408                                         tmp_seconds -= (tmp_minutes * 60);
409                                         tmp_hours = (tmp_minutes / 60);
410                                         
411                                         if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
412                                 }
413                                 
414                                 sprint(self, sprintf(" %-4s %-20s %-5d %-3d %-9s %-16s %s\n", 
415                                         strcat("#", ftos(num_for_edict(tmp_player))), 
416                                         tmp_player.netname, //strcat(tmp_player_name, sprintf("%*s", (20 - strlen(strdecolorize(tmp_player_name))), "")),
417                                         tmp_player.ping, tmp_player.ping_packetloss, 
418                                         sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
419                                         (autocvar_sv_status_privacy ? "hidden" : tmp_player.netaddress),
420                                         (autocvar_sv_status_privacy ? "hidden" : tmp_player.crypto_idfp)));
421                                         
422                                 ++total_listed_players;
423                         }
424                         
425                         sprint(self, strcat("Finished listing ", ftos(total_listed_players), " client(s). \n"));
426                         
427                         return; // never fall through to usage
428                 }
429                         
430                 default:
431                 case CMD_REQUEST_USAGE:
432                 {
433                         sprint(self, "\nUsage:^3 cmd who\n");
434                         sprint(self, "  No arguments required.\n");
435                         return;
436                 }
437         }
438 }
439
440 /* use this when creating a new command, making sure to place it in alphabetical order.
441 void CommonCommand_(float request)
442 {
443         switch(request)
444         {
445                 case CMD_REQUEST_COMMAND:
446                 {
447                         
448                         return; // never fall through to usage
449                 }
450                         
451                 default:
452                 case CMD_REQUEST_USAGE:
453                 {
454                         sprint(self, "\nUsage:^3 cmd \n");
455                         sprint(self, "  No arguments required.\n");
456                         return;
457                 }
458         }
459 }
460 */