When possible use simpler LOG_* macros instead of LOG_*F
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / sv_cmd.qc
1 #include "sv_cmd.qh"
2 #include "_mod.qh"
3
4 #include <common/effects/all.qh>
5
6 #include "banning.qh"
7 #include "cmd.qh"
8 #include "common.qh"
9 #include "getreplies.qh"
10 #include "radarmap.qh"
11
12 #include "../anticheat.qh"
13 #include "../campaign.qh"
14 #include "../client.qh"
15 #include "../player.qh"
16 #include "../g_world.qh"
17 #include "../ipban.qh"
18 #include "../teamplay.qh"
19
20 #include "../bot/api.qh"
21
22 #include <server/mutators/_mod.qh>
23 #include <common/gamemodes/_mod.qh>
24
25 #include <common/constants.qh>
26 #include <common/net_linked.qh>
27 #include <common/mapinfo.qh>
28 #include <common/notifications/all.qh>
29 #include <common/teams.qh>
30 #include <common/util.qh>
31
32 #include <common/monsters/sv_monsters.qh>
33
34 //  used by GameCommand_make_mapinfo()
35 void make_mapinfo_Think(entity this)
36 {
37         if (_MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, 0, 1))
38         {
39                 LOG_INFO("Done rebuiling mapinfos.");
40                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
41                 delete(this);
42         }
43         else
44         {
45                 setthink(this, make_mapinfo_Think);
46                 this.nextthink = time;
47         }
48 }
49
50 //  used by GameCommand_extendmatchtime() and GameCommand_reducematchtime()
51 void changematchtime(float delta, float mi, float ma)
52 {
53         float cur;
54         float update;
55         float lim;
56
57         if (delta == 0) return;
58         if (autocvar_timelimit < 0) return;
59
60         if (mi <= 10) mi = 10;  // at least ten sec in the future
61         cur = time - game_starttime;
62         if (cur > 0) mi += cur; // from current time!
63
64         lim = autocvar_timelimit * 60;
65
66         if (delta > 0)
67         {
68                 if (lim == 0) return; // cannot increase any further
69                 else if (lim < ma) update = min(ma, lim + delta);
70                 else                  // already above maximum: FAIL
71                         return;
72         }
73         else
74         {
75                 if (lim == 0)      // infinite: try reducing to max, if we are allowed to
76                         update = max(mi, ma);
77                 else if (lim > mi) // above minimum: decrease
78                         update = max(mi, lim + delta);
79                 else               // already below minimum: FAIL
80                         return;
81         }
82
83         cvar_set("timelimit", ftos(update / 60));
84 }
85
86
87 // =======================
88 //  Command Sub-Functions
89 // =======================
90
91 void GameCommand_adminmsg(int request, int argc)
92 {
93         switch (request)
94         {
95                 case CMD_REQUEST_COMMAND:
96                 {
97                         entity client;
98                         float accepted;
99
100                         string targets = strreplace(",", " ", argv(1));
101                         string original_targets = strreplace(" ", ", ", targets);
102                         string admin_message = argv(2);
103                         float infobartime = stof(argv(3));
104
105                         string successful, t;
106                         successful = string_null;
107
108                         if ((targets) && (admin_message))
109                         {
110                                 for ( ; targets; )
111                                 {
112                                         t = car(targets);
113                                         targets = cdr(targets);
114
115                                         // Check to see if the player is a valid target
116                                         client = GetFilteredEntity(t);
117                                         accepted = VerifyClientEntity(client, true, false);
118
119                                         if (accepted <= 0)
120                                         {
121                                                 LOG_INFO("adminmsg: ", GetClientErrorString(accepted, t), (targets ? ", skipping to next player.\n" : "."));
122                                                 continue;
123                                         }
124
125                                         // send the centerprint/console print or infomessage
126                                         if (infobartime)
127                                         {
128                                                 stuffcmd(client, sprintf("\ninfobar %f \"%s\"\n", infobartime, MakeConsoleSafe(admin_message)));
129                                         }
130                                         else
131                                         {
132                                                 centerprint(client, strcat("^3", GetCallerName(NULL), ":\n^7", admin_message));
133                                                 sprint(client, strcat("\{1}\{13}^3", GetCallerName(NULL), "^7: ", admin_message, "\n"));
134                                         }
135
136                                         successful = strcat(successful, (successful ? ", " : ""), playername(client, false));
137                                         LOG_TRACE("Message sent to ", playername(client, false));
138                                         continue;
139                                 }
140
141                                 if (successful) bprint("Successfully sent message '", admin_message, "' to ", successful, ".\n");
142                                 else LOG_INFO("No players given (", original_targets, ") could receive the message.");
143
144                                 return;
145                         }
146                 }
147
148                 default:
149                         LOG_INFO("Incorrect parameters for ^2adminmsg^7");
150                 case CMD_REQUEST_USAGE:
151                 {
152                         LOG_INFO("Usage:^3 sv_cmd adminmsg clients \"message\" [infobartime]");
153                         LOG_INFO("  'clients' is a list (separated by commas) of player entity ID's or nicknames");
154                         LOG_INFO("  If infobartime is provided, the message will be sent to infobar.");
155                         LOG_INFO("  Otherwise, it will just be sent as a centerprint message.");
156                         LOG_INFO("Examples: adminmsg 2,4 \"this infomessage will last for ten seconds\" 10");
157                         LOG_INFO("          adminmsg 2,5 \"this message will be a centerprint\"");
158                         return;
159                 }
160         }
161 }
162
163 void GameCommand_allready(int request)
164 {
165         switch (request)
166         {
167                 case CMD_REQUEST_COMMAND:
168                 {
169                         ReadyRestart();
170                         return;
171                 }
172
173                 default:
174                 case CMD_REQUEST_USAGE:
175                 {
176                         LOG_INFO("Usage:^3 sv_cmd allready");
177                         LOG_INFO("  No arguments required.");
178                         return;
179                 }
180         }
181 }
182
183 void GameCommand_allspec(int request, int argc)
184 {
185         switch (request)
186         {
187                 case CMD_REQUEST_COMMAND:
188                 {
189                         string reason = argv(1);
190                         int n = 0;
191                         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
192                                 if (it.caplayer) it.caplayer = 0;
193                                 PutObserverInServer(it);
194                                 ++n;
195                         });
196                         if (n)   bprint(strcat("Successfully forced all (", ftos(n), ") players to spectate", (reason ? strcat(" for reason: '", reason, "'") : ""), ".\n"));
197                         else   LOG_INFO("No players found to spectate.");
198                         return;
199                 }
200
201                 default:
202                 case CMD_REQUEST_USAGE:
203                 {
204                         LOG_INFO("Usage:^3 sv_cmd allspec [reason]");
205                         LOG_INFO("  Where 'reason' is an optional argument for explanation of allspec command.");
206                         LOG_INFO("See also: ^2moveplayer, shuffleteams^7");
207                         return;
208                 }
209         }
210 }
211
212 void GameCommand_anticheat(int request, int argc)
213 {
214         switch (request)
215         {
216                 case CMD_REQUEST_COMMAND:
217                 {
218                         entity client = GetIndexedEntity(argc, 1);
219                         float accepted = VerifyClientEntity(client, false, false);
220
221                         if (accepted > 0)
222                         {
223                                 anticheat_report_to_eventlog(client);
224                                 return;
225                         }
226                         else
227                         {
228                                 LOG_INFO("anticheat: ", GetClientErrorString(accepted, argv(1)), ".");
229                         }
230                 }
231
232                 default:
233                         LOG_INFO("Incorrect parameters for ^2anticheat^7");
234                 case CMD_REQUEST_USAGE:
235                 {
236                         LOG_INFO("Usage:^3 sv_cmd anticheat client");
237                         LOG_INFO("  'client' is the entity number or name of the player.");
238                         return;
239                 }
240         }
241 }
242
243 void GameCommand_bbox(int request)
244 {
245         switch (request)
246         {
247                 case CMD_REQUEST_COMMAND:
248                 {
249                         vector size_min = '0 0 0';
250                         vector size_max = '0 0 0';
251                         tracebox('1 0 0' * world.absmin.x,
252                                 '0 1 0' * world.absmin.y + '0 0 1' * world.absmin.z,
253                                 '0 1 0' * world.absmax.y + '0 0 1' * world.absmax.z,
254                                 '1 0 0' * world.absmax.x,
255                                 MOVE_WORLDONLY,
256                                 NULL);
257                         size_min.x = (trace_startsolid) ? world.absmin.x : trace_endpos.x;
258
259                         tracebox('0 1 0' * world.absmin.y,
260                                 '1 0 0' * world.absmin.x + '0 0 1' * world.absmin.z,
261                                 '1 0 0' * world.absmax.x + '0 0 1' * world.absmax.z,
262                                 '0 1 0' * world.absmax.y,
263                                 MOVE_WORLDONLY,
264                                 NULL);
265                         size_min.y = (trace_startsolid) ? world.absmin.y : trace_endpos.y;
266
267                         tracebox('0 0 1' * world.absmin.z,
268                                 '1 0 0' * world.absmin.x + '0 1 0' * world.absmin.y,
269                                 '1 0 0' * world.absmax.x + '0 1 0' * world.absmax.y,
270                                 '0 0 1' * world.absmax.z,
271                                 MOVE_WORLDONLY,
272                                 NULL);
273                         size_min.z = (trace_startsolid) ? world.absmin.z : trace_endpos.z;
274
275                         tracebox('1 0 0' * world.absmax.x,
276                                 '0 1 0' * world.absmin.y + '0 0 1' * world.absmin.z,
277                                 '0 1 0' * world.absmax.y + '0 0 1' * world.absmax.z,
278                                 '1 0 0' * world.absmin.x,
279                                 MOVE_WORLDONLY,
280                                 NULL);
281                         size_max.x = (trace_startsolid) ? world.absmax.x : trace_endpos.x;
282
283                         tracebox('0 1 0' * world.absmax.y,
284                                 '1 0 0' * world.absmin.x + '0 0 1' * world.absmin.z,
285                                 '1 0 0' * world.absmax.x + '0 0 1' * world.absmax.z,
286                                 '0 1 0' * world.absmin.y,
287                                 MOVE_WORLDONLY,
288                                 NULL);
289                         size_max.y = (trace_startsolid) ? world.absmax.y : trace_endpos.y;
290
291                         tracebox('0 0 1' * world.absmax.z,
292                                 '1 0 0' * world.absmin.x + '0 1 0' * world.absmin.y,
293                                 '1 0 0' * world.absmax.x + '0 1 0' * world.absmax.y,
294                                 '0 0 1' * world.absmin.z,
295                                 MOVE_WORLDONLY,
296                                 NULL);
297                         size_max.z = (trace_startsolid) ? world.absmax.z : trace_endpos.z;
298
299                         LOG_INFOF("Original size: %v %v", world.absmin, world.absmax);
300                         LOG_INFOF("Currently set size: %v %v", world.mins, world.maxs);
301                         LOG_INFOF("Solid bounding box size: %v %v", size_min, size_max);
302                         return;
303                 }
304
305                 default:
306                 case CMD_REQUEST_USAGE:
307                 {
308                         LOG_INFO("Usage:^3 sv_cmd bbox");
309                         LOG_INFO("  No arguments required.");
310                         LOG_INFO("See also: ^2gettaginfo, trace^7");
311                         return;
312                 }
313         }
314 }
315
316 void GameCommand_bot_cmd(int request, int argc, string command)
317 {
318         switch (request)
319         {
320                 case CMD_REQUEST_COMMAND:
321                 {
322                         entity bot;
323
324                         if (argv(1) == "reset")
325                         {
326                                 bot_resetqueues();
327                                 return;
328                         }
329                         else if (argv(1) == "setbots")
330                         {
331                                 cvar_settemp("bot_vs_human", "0");
332                                 cvar_settemp("minplayers", "0");
333                                 cvar_settemp("minplayers_per_team", "0");
334                                 cvar_settemp("bot_number", "0");
335                                 bot_fixcount();
336                                 cvar_settemp("bot_number", argv(2));
337                                 if (!bot_fixcount()) LOG_INFO("Sorry, could not set requested bot count");
338                                 return;
339                         }
340                         else if (argv(1) == "load" && argc == 3)
341                         {
342                                 float fh, i;
343                                 string s;
344                                 fh = fopen(argv(2), FILE_READ);
345                                 if (fh < 0)
346                                 {
347                                         LOG_INFO("cannot open the file");
348                                         return;
349                                 }
350
351                                 i = 0;
352                                 while ((s = fgets(fh)))
353                                 {
354                                         argc = tokenize_console(s);
355
356                                         if (argc >= 3 && argv(0) == "sv_cmd" && argv(1) == "bot_cmd")
357                                         {
358                                                 if (argv(2) == "reset")
359                                                 {
360                                                         bot_resetqueues();
361                                                 }
362                                                 else if (argv(2) == "setbots")
363                                                 {
364                                                         cvar_settemp("bot_vs_human", "0");
365                                                         cvar_settemp("minplayers", "0");
366                                                         cvar_settemp("minplayers_per_team", "0");
367                                                         cvar_settemp("bot_number", "0");
368                                                         bot_fixcount();
369                                                         cvar_settemp("bot_number", argv(3));
370                                                         if (!bot_fixcount()) LOG_INFO("Sorry, could not set requested bot count");
371                                                 }
372                                                 else
373                                                 {
374                                                         if(argv(2) == "*" || argv(2) == "all")
375                                                                 FOREACH_CLIENT(IS_BOT_CLIENT(it), {
376                                                                         bot_queuecommand(it, substring(s, argv_start_index(3), -1));
377                                                                 });
378                                                         else
379                                                         {
380                                                                 bot = find_bot_by_number(stof(argv(2)));
381                                                                 if (bot == NULL) bot = find_bot_by_name(argv(2));
382                                                                 if (bot) bot_queuecommand(bot, substring(s, argv_start_index(3), -1));
383                                                         }
384                                                 }
385                                         }
386                                         else
387                                         {
388                                                 localcmd(strcat(s, "\n"));
389                                         }
390
391                                         ++i;
392                                 }
393                                 LOG_INFO(ftos(i), " commands read");
394                                 fclose(fh);
395                                 return;
396                         }
397                         else if (argv(1) == "help")
398                         {
399                                 if (argv(2)) bot_cmdhelp(argv(2));
400                                 else bot_list_commands();
401                                 return;
402                         }
403                         else if (argc >= 3)  // this comes last
404                         {
405                                 if(argv(1) == "*" || argv(1) == "all")
406                                 {
407                                         int bot_num = 0;
408                                         FOREACH_CLIENT(IS_BOT_CLIENT(it), {
409                                                 bot_queuecommand(it, substring(command, argv_start_index(2), -1));
410                                                 bot_num++;
411                                         });
412                                         if(bot_num)
413                                                 LOG_INFO("Command '", substring(command, argv_start_index(2), -1), "' sent to all bots (", ftos(bot_num), ")");
414                                         return;
415                                 }
416                                 else
417                                 {
418                                         bot = find_bot_by_number(stof(argv(1)));
419                                         if (bot == NULL) bot = find_bot_by_name(argv(1));
420                                         if (bot)
421                                         {
422                                                 LOG_INFO("Command '", substring(command, argv_start_index(2), -1), "' sent to bot ", bot.netname);
423                                                 bot_queuecommand(bot, substring(command, argv_start_index(2), -1));
424                                                 return;
425                                         }
426                                         else
427                                         {
428                                                 LOG_INFO("Error: Can't find bot with the name or id '", argv(1), "' - Did you mistype the command?");  // don't return so that usage is shown
429                                         }
430                                 }
431                         }
432                 }
433
434                 default:
435                         LOG_INFO("Incorrect parameters for ^2bot_cmd^7");
436                 case CMD_REQUEST_USAGE:
437                 {
438                         LOG_INFO("Usage:^3 sv_cmd bot_cmd client command [argument]");
439                         LOG_INFO("  'client' can be either the name of the bot or a progressive number (not the entity number!)");
440                         LOG_INFO("           can also be '*' or 'all' to allow sending the command to all the bots");
441                         LOG_INFO("  For full list of commands, see bot_cmd help [command].");
442                         LOG_INFO("Examples: sv_cmd bot_cmd 1 cc \"say something\"");
443                         LOG_INFO("          sv_cmd bot_cmd 1 presskey jump");
444                         LOG_INFO("          sv_cmd bot_cmd * pause");
445                         return;
446                 }
447         }
448 }
449
450 void GameCommand_cointoss(int request, int argc)
451 {
452         switch (request)
453         {
454                 case CMD_REQUEST_COMMAND:
455                 {
456                         string result1 = (argv(2) ? strcat("^7", argv(1)) : "^1HEADS");
457                         string result2 = (argv(2) ? strcat("^7", argv(2)) : "^4TAILS");
458                         string choice = ((random() > 0.5) ? result1 : result2);
459
460                         Send_Notification(NOTIF_ALL, NULL, MSG_MULTI, MULTI_COINTOSS, choice);
461                         return;
462                 }
463
464                 default:
465                 case CMD_REQUEST_USAGE:
466                 {
467                         LOG_INFO("Usage:^3 sv_cmd cointoss [result1 result2]");
468                         LOG_INFO("  Where 'result1' and 'result2' are user created options.");
469                         return;
470                 }
471         }
472 }
473
474 void GameCommand_database(int request, int argc)
475 {
476         switch (request)
477         {
478                 case CMD_REQUEST_COMMAND:
479                 {
480                         if (argc == 3)
481                         {
482                                 if (argv(1) == "save")
483                                 {
484                                         db_save(ServerProgsDB, argv(2));
485                                         LOG_INFO("Copied serverprogs database to '", argv(2), "' in the data directory.");
486                                         return;
487                                 }
488                                 else if (argv(1) == "dump")
489                                 {
490                                         db_dump(ServerProgsDB, argv(2));
491                                         LOG_INFO("DB dumped.");  // wtf does this do?
492                                         return;
493                                 }
494                                 else if (argv(1) == "load")
495                                 {
496                                         db_close(ServerProgsDB);
497                                         ServerProgsDB = db_load(argv(2));
498                                         LOG_INFO("Loaded '", argv(2), "' as new serverprogs database.");
499                                         return;
500                                 }
501                         }
502                 }
503
504                 default:
505                         LOG_INFO("Incorrect parameters for ^2database^7");
506                 case CMD_REQUEST_USAGE:
507                 {
508                         LOG_INFO("Usage:^3 sv_cmd database action filename");
509                         LOG_INFO("  Where 'action' is the command to complete,");
510                         LOG_INFO("  and 'filename' is what it acts upon.");
511                         LOG_INFO("  Full list of commands here: \"save, dump, load.\"");
512                         return;
513                 }
514         }
515 }
516
517 void GameCommand_defer_clear(int request, int argc)
518 {
519         switch (request)
520         {
521                 case CMD_REQUEST_COMMAND:
522                 {
523                         entity client;
524                         float accepted;
525
526                         if (argc >= 2)
527                         {
528                                 client = GetIndexedEntity(argc, 1);
529                                 accepted = VerifyClientEntity(client, true, false);
530
531                                 if (accepted > 0)
532                                 {
533                                         stuffcmd(client, "defer clear\n");
534                                         LOG_INFO("defer clear stuffed to ", playername(client, false));
535                                 }
536                                 else { LOG_INFO("defer_clear: ", GetClientErrorString(accepted, argv(1)), "."); }
537
538                                 return;
539                         }
540                 }
541
542                 default:
543                         LOG_INFO("Incorrect parameters for ^2defer_clear^7");
544                 case CMD_REQUEST_USAGE:
545                 {
546                         LOG_INFO("Usage:^3 sv_cmd defer_clear client");
547                         LOG_INFO("  'client' is the entity number or name of the player.");
548                         LOG_INFO("See also: ^2defer_clear_all^7");
549                         return;
550                 }
551         }
552 }
553
554 void GameCommand_defer_clear_all(int request)
555 {
556         switch (request)
557         {
558                 case CMD_REQUEST_COMMAND:
559                 {
560                         int n = 0;
561                         int argc;
562
563                         FOREACH_CLIENT(true, {
564                                 argc = tokenize_console(strcat("defer_clear ", ftos(etof(it))));
565                                 GameCommand_defer_clear(CMD_REQUEST_COMMAND, argc);
566                                 ++n;
567                         });
568                         if (n)   LOG_INFO("Successfully stuffed defer clear to all clients (", ftos(n), ")");  // should a message be added if no players were found?
569                         return;
570                 }
571
572                 default:
573                 case CMD_REQUEST_USAGE:
574                 {
575                         LOG_INFO("Usage:^3 sv_cmd defer_clear_all");
576                         LOG_INFO("  No arguments required.");
577                         LOG_INFO("See also: ^2defer_clear^7");
578                         return;
579                 }
580         }
581 }
582
583 void GameCommand_delrec(int request, int argc)  // perhaps merge later with records and printstats and such?
584 {
585         switch (request)
586         {
587                 case CMD_REQUEST_COMMAND:
588                 {
589                         if (argv(1))
590                         {
591                                 if (argv(2)) race_deleteTime(argv(2), stof(argv(1)));
592                                 else race_deleteTime(GetMapname(), stof(argv(1)));
593                                 return;
594                         }
595                 }
596
597                 default:
598                         LOG_INFO("Incorrect parameters for ^2delrec^7");
599                 case CMD_REQUEST_USAGE:
600                 {
601                         LOG_INFO("Usage:^3 sv_cmd delrec ranking [map]");
602                         LOG_INFO("  'ranking' is which ranking level to clear up to, ");
603                         LOG_INFO("  it will clear all records up to nth place.");
604                         LOG_INFO("  if 'map' is not provided it will use current map.");
605                         return;
606                 }
607         }
608 }
609
610 void print_Effect_Index(int d, string effect_name)
611
612         // this is inside a function to avoid expanding it on compilation everytime
613         LOG_INFO("effect ", effect_name, " is ", ftos(_particleeffectnum(effect_name)), "\n");
614         db_put(d, effect_name, "1");
615 }
616
617 void GameCommand_effectindexdump(int request)
618 {
619         switch (request)
620         {
621                 case CMD_REQUEST_COMMAND:
622                 {
623                         float fh, d;
624                         string s;
625
626                         d = db_create();
627                         LOG_INFO("begin of effects list");
628
629                         print_Effect_Index(d, "TE_GUNSHOT");
630                         print_Effect_Index(d, "TE_GUNSHOTQUAD");
631                         print_Effect_Index(d, "TE_SPIKE");
632                         print_Effect_Index(d, "TE_SPIKEQUAD");
633                         print_Effect_Index(d, "TE_SUPERSPIKE");
634                         print_Effect_Index(d, "TE_SUPERSPIKEQUAD");
635                         print_Effect_Index(d, "TE_WIZSPIKE");
636                         print_Effect_Index(d, "TE_KNIGHTSPIKE");
637                         print_Effect_Index(d, "TE_EXPLOSION");
638                         print_Effect_Index(d, "TE_EXPLOSIONQUAD");
639                         print_Effect_Index(d, "TE_TAREXPLOSION");
640                         print_Effect_Index(d, "TE_TELEPORT");
641                         print_Effect_Index(d, "TE_LAVASPLASH");
642                         print_Effect_Index(d, "TE_SMALLFLASH");
643                         print_Effect_Index(d, "TE_FLAMEJET");
644                         print_Effect_Index(d, "EF_FLAME");
645                         print_Effect_Index(d, "TE_BLOOD");
646                         print_Effect_Index(d, "TE_SPARK");
647                         print_Effect_Index(d, "TE_PLASMABURN");
648                         print_Effect_Index(d, "TE_TEI_G3");
649                         print_Effect_Index(d, "TE_TEI_SMOKE");
650                         print_Effect_Index(d, "TE_TEI_BIGEXPLOSION");
651                         print_Effect_Index(d, "TE_TEI_PLASMAHIT");
652                         print_Effect_Index(d, "EF_STARDUST");
653                         print_Effect_Index(d, "TR_ROCKET");
654                         print_Effect_Index(d, "TR_GRENADE");
655                         print_Effect_Index(d, "TR_BLOOD");
656                         print_Effect_Index(d, "TR_WIZSPIKE");
657                         print_Effect_Index(d, "TR_SLIGHTBLOOD");
658                         print_Effect_Index(d, "TR_KNIGHTSPIKE");
659                         print_Effect_Index(d, "TR_VORESPIKE");
660                         print_Effect_Index(d, "TR_NEHAHRASMOKE");
661                         print_Effect_Index(d, "TR_NEXUIZPLASMA");
662                         print_Effect_Index(d, "TR_GLOWTRAIL");
663                         print_Effect_Index(d, "TR_SEEKER");
664                         print_Effect_Index(d, "SVC_PARTICLE");
665
666                         fh = fopen("effectinfo.txt", FILE_READ);
667                         while ((s = fgets(fh)))
668                         {
669                                 tokenize_console(s);
670                                 if (argv(0) == "effect")
671                                 {
672                                         if (db_get(d, argv(1)) != "1")
673                                         {
674                                                 int i = _particleeffectnum(argv(1));
675                                                 if (i >= 0) LOG_INFO("effect ", argv(1), " is ", ftos(i));
676                                                 db_put(d, argv(1), "1");
677                                         }
678                                 }
679                         }
680                         LOG_INFO("end of effects list");
681
682                         db_close(d);
683                         return;
684                 }
685
686                 default:
687                 case CMD_REQUEST_USAGE:
688                 {
689                         LOG_INFO("Usage:^3 sv_cmd effectindexdump");
690                         LOG_INFO("  No arguments required.");
691                         return;
692                 }
693         }
694 }
695
696 void GameCommand_extendmatchtime(int request)
697 {
698         switch (request)
699         {
700                 case CMD_REQUEST_COMMAND:
701                 {
702                         changematchtime(autocvar_timelimit_increment * 60, autocvar_timelimit_min * 60, autocvar_timelimit_max * 60);
703                         return;
704                 }
705
706                 default:
707                 case CMD_REQUEST_USAGE:
708                 {
709                         LOG_INFO("Usage:^3 sv_cmd extendmatchtime");
710                         LOG_INFO("  No arguments required.");
711                         LOG_INFO("See also: ^2reducematchtime^7");
712                         return;
713                 }
714         }
715 }
716
717 void GameCommand_gametype(int request, int argc)
718 {
719         switch (request)
720         {
721                 case CMD_REQUEST_COMMAND:
722                 {
723                         if (argv(1) != "")
724                         {
725                                 string s = argv(1);
726                                 Gametype t = MapInfo_Type_FromString(s), tsave = MapInfo_CurrentGametype();
727
728                                 if (t)
729                                 {
730                                         MapInfo_SwitchGameType(t);
731                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
732                                         if (MapInfo_count > 0)
733                                         {
734                                                 // update lsmaps in case the gametype changed, this way people can easily list maps for it
735                                                 if (lsmaps_reply != "")   strunzone(lsmaps_reply);
736                                                 lsmaps_reply = strzone(getlsmaps());
737                                                 bprint("Game type successfully switched to ", s, "\n");
738                                         }
739                                         else
740                                         {
741                                                 bprint("Cannot use this game type: no map for it found\n");
742                                                 MapInfo_SwitchGameType(tsave);
743                                                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
744                                         }
745                                 }
746                                 else
747                                 {
748                                         bprint("Game type switch to ", s, " failed: this type does not exist!\n");
749                                 }
750
751                                 return;
752                         }
753                 }
754
755                 default:
756                         LOG_INFO("Incorrect parameters for ^2gametype^7");
757                 case CMD_REQUEST_USAGE:
758                 {
759                         LOG_INFO("Usage:^3 sv_cmd gametype mode");
760                         LOG_INFO("  Where 'mode' is the gametype mode to switch to.");
761                         LOG_INFO("See also: ^2gotomap^7");
762                         return;
763                 }
764         }
765 }
766
767 void GameCommand_gettaginfo(int request, int argc)
768 {
769         switch (request)
770         {
771                 case CMD_REQUEST_COMMAND:
772                 {
773                         entity tmp_entity;
774                         float i;
775                         vector v;
776
777                         if (argc >= 4)
778                         {
779                                 tmp_entity = spawn();
780                                 if (argv(1) == "w")
781                                 {
782                                         .entity weaponentity = weaponentities[0];
783                                         _setmodel(tmp_entity, (nextent(NULL)).(weaponentity).model);
784                                 }
785                                 else
786                                 {
787                                         precache_model(argv(1));
788                                         _setmodel(tmp_entity, argv(1));
789                                 }
790                                 tmp_entity.frame = stof(argv(2));
791                                 if (substring(argv(3), 0, 1) == "#") i = stof(substring(argv(3), 1, -1));
792                                 else i = gettagindex(tmp_entity, argv(3));
793                                 if (i)
794                                 {
795                                         v = gettaginfo(tmp_entity, i);
796                                         LOG_INFOF(
797                                                 "model %s frame %s tag %s index %s parent %s",
798                                                 tmp_entity.model, ftos(tmp_entity.frame), gettaginfo_name, ftos(i), ftos(gettaginfo_parent)
799                                         );
800                                         LOG_INFOF(" vector = %s %s %s", ftos(v.x), ftos(v.y), ftos(v.z));
801                                         LOG_INFOF(" offset = %s %s %s", ftos(gettaginfo_offset.x), ftos(gettaginfo_offset.y), ftos(gettaginfo_offset.z));
802                                         LOG_INFOF(" forward = %s %s %s", ftos(gettaginfo_forward.x), ftos(gettaginfo_forward.y), ftos(gettaginfo_forward.z));
803                                         LOG_INFOF(" right = %s %s %s", ftos(gettaginfo_right.x), ftos(gettaginfo_right.y), ftos(gettaginfo_right.z));
804                                         LOG_INFOF(" up = %s %s %s", ftos(gettaginfo_up.x), ftos(gettaginfo_up.y), ftos(gettaginfo_up.z));
805                                         if (argc >= 6)
806                                         {
807                                                 v.y = -v.y;
808                                                 localcmd(strcat(argv(4), vtos(v), argv(5), "\n"));
809                                         }
810                                 }
811                                 else
812                                 {
813                                         LOG_INFO("bone not found");
814                                 }
815
816                                 delete(tmp_entity);
817                                 return;
818                         }
819                 }
820
821                 default:
822                         LOG_INFO("Incorrect parameters for ^2gettaginfo^7");
823                 case CMD_REQUEST_USAGE:
824                 {
825                         LOG_INFO("Usage:^3 sv_cmd gettaginfo model frame index [command one] [command two]");
826                         LOG_INFO("See also: ^2bbox, trace^7");
827                         return;
828                 }
829         }
830 }
831
832 void GameCommand_animbench(int request, int argc)
833 {
834         switch (request)
835         {
836                 case CMD_REQUEST_COMMAND:
837                 {
838                         entity tmp_entity;
839
840                         if (argc >= 4)
841                         {
842                                 tmp_entity = spawn();
843                                 if (argv(1) == "w")
844                                 {
845                                         .entity weaponentity = weaponentities[0];
846                                         _setmodel(tmp_entity, (nextent(NULL)).(weaponentity).model);
847                                 }
848                                 else
849                                 {
850                                         precache_model(argv(1));
851                                         _setmodel(tmp_entity, argv(1));
852                                 }
853                                 float f1 = stof(argv(2));
854                                 float f2 = stof(argv(3));
855                                 float t0;
856                                 float t1 = 0;
857                                 float t2 = 0;
858                                 float n = 0;
859
860                                 while (t1 + t2 < 1)
861                                 {
862                                         tmp_entity.frame = f1;
863                                         t0 = gettime(GETTIME_HIRES);
864                                         getsurfacepoint(tmp_entity, 0, 0);
865                                         t1 += gettime(GETTIME_HIRES) - t0;
866                                         tmp_entity.frame = f2;
867                                         t0 = gettime(GETTIME_HIRES);
868                                         getsurfacepoint(tmp_entity, 0, 0);
869                                         t2 += gettime(GETTIME_HIRES) - t0;
870                                         n += 1;
871                                 }
872                                 LOG_INFO("model ", tmp_entity.model, " frame ", ftos(f1), " animtime ", ftos(n / t1), "/s");
873                                 LOG_INFO("model ", tmp_entity.model, " frame ", ftos(f2), " animtime ", ftos(n / t2), "/s");
874
875                                 delete(tmp_entity);
876                                 return;
877                         }
878                 }
879
880                 default:
881                         LOG_INFO("Incorrect parameters for ^2gettaginfo^7");
882                 case CMD_REQUEST_USAGE:
883                 {
884                         LOG_INFO("Usage:^3 sv_cmd gettaginfo model frame index [command one] [command two]");
885                         LOG_INFO("See also: ^2bbox, trace^7");
886                         return;
887                 }
888         }
889 }
890
891 void GameCommand_gotomap(int request, int argc)
892 {
893         switch (request)
894         {
895                 case CMD_REQUEST_COMMAND:
896                 {
897                         if (argv(1))
898                         {
899                                 LOG_INFO(GotoMap(argv(1)));
900                                 return;
901                         }
902                 }
903
904                 default:
905                         LOG_INFO("Incorrect parameters for ^2gotomap^7");
906                 case CMD_REQUEST_USAGE:
907                 {
908                         LOG_INFO("Usage:^3 sv_cmd gotomap map");
909                         LOG_INFO("  Where 'map' is the *.bsp file to change to.");
910                         LOG_INFO("See also: ^2gametype^7");
911                         return;
912                 }
913         }
914 }
915
916 void GameCommand_lockteams(int request)
917 {
918         switch (request)
919         {
920                 case CMD_REQUEST_COMMAND:
921                 {
922                         if (teamplay)
923                         {
924                                 lockteams = 1;
925                                 bprint("^1The teams are now locked.\n");
926                         }
927                         else
928                         {
929                                 bprint("lockteams command can only be used in a team-based gamemode.\n");
930                         }
931                         return;
932                 }
933
934                 default:
935                 case CMD_REQUEST_USAGE:
936                 {
937                         LOG_INFO("Usage:^3 sv_cmd lockteams");
938                         LOG_INFO("  No arguments required.");
939                         LOG_INFO("See also: ^2unlockteams^7");
940                         return;
941                 }
942         }
943 }
944
945 void GameCommand_make_mapinfo(int request)
946 {
947         switch (request)
948         {
949                 case CMD_REQUEST_COMMAND:
950                 {
951                         entity tmp_entity;
952
953                         tmp_entity = new(make_mapinfo);
954                         setthink(tmp_entity, make_mapinfo_Think);
955                         tmp_entity.nextthink = time;
956                         MapInfo_Enumerate();
957                         return;
958                 }
959
960                 default:
961                 case CMD_REQUEST_USAGE:
962                 {
963                         LOG_INFO("Usage:^3 sv_cmd make_mapinfo");
964                         LOG_INFO("  No arguments required.");
965                         LOG_INFO("See also: ^2radarmap^7");
966                         return;
967                 }
968         }
969 }
970
971 void GameCommand_moveplayer(int request, int argc)
972 {
973         switch (request)
974         {
975                 case CMD_REQUEST_COMMAND:
976                 {
977                         float accepted;
978                         entity client;
979
980                         string targets = strreplace(",", " ", argv(1));
981                         string original_targets = strreplace(" ", ", ", targets);
982                         string destination = argv(2);
983
984                         string successful, t;
985                         successful = string_null;
986
987                         // lets see if the target(s) even actually exist.
988                         if ((targets) && (destination))
989                         {
990                                 for ( ; targets; )
991                                 {
992                                         t = car(targets);
993                                         targets = cdr(targets);
994
995                                         // Check to see if the player is a valid target
996                                         client = GetFilteredEntity(t);
997                                         accepted = VerifyClientEntity(client, false, false);
998
999                                         if (accepted <= 0)
1000                                         {
1001                                                 LOG_INFO("moveplayer: ", GetClientErrorString(accepted, t), (targets ? ", skipping to next player.\n" : "."));
1002                                                 continue;
1003                                         }
1004
1005                                         // Where are we putting this player?
1006                                         if (destination == "spec" || destination == "spectator")
1007                                         {
1008                                                 if (!IS_SPEC(client) && !IS_OBSERVER(client))
1009                                                 {
1010                                                         if (client.caplayer) client.caplayer = 0;
1011                                                         PutObserverInServer(client);
1012
1013                                                         successful = strcat(successful, (successful ? ", " : ""), playername(client, false));
1014                                                 }
1015                                                 else
1016                                                 {
1017                                                         LOG_INFO("Player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ") is already spectating.");
1018                                                 }
1019                                                 continue;
1020                                         }
1021                                         else
1022                                         {
1023                                                 if (!IS_SPEC(client) && !IS_OBSERVER(client))
1024                                                 {
1025                                                         if (teamplay)
1026                                                         {
1027                                                                 // set up
1028                                                                 float team_id;
1029                                                                 int save = Player_GetForcedTeamIndex(client);
1030                                                                 Player_SetForcedTeamIndex(client, TEAM_FORCE_DEFAULT);
1031
1032                                                                 // find the team to move the player to
1033                                                                 team_id = Team_ColorToTeam(destination);
1034                                                                 entity balance;
1035                                                                 if (team_id == client.team)  // already on the destination team
1036                                                                 {
1037                                                                         // keep the forcing undone
1038                                                                         LOG_INFO("Player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ") is already on the ", Team_ColoredFullName(client.team), (targets ? "^7, skipping to next player.\n" : "^7."));
1039                                                                         continue;
1040                                                                 }
1041                                                                 else if (team_id == 0)  // auto team
1042                                                                 {
1043                                                                         balance = TeamBalance_CheckAllowedTeams(client);
1044                                                                         team_id = Team_IndexToTeam(TeamBalance_FindBestTeam(balance, client, false));
1045                                                                 }
1046                                                                 else
1047                                                                 {
1048                                                                         balance = TeamBalance_CheckAllowedTeams(client);
1049                                                                 }
1050                                                                 Player_SetForcedTeamIndex(client, save);
1051
1052                                                                 // Check to see if the destination team is even available
1053                                                                 switch (team_id)
1054                                                                 {
1055                                                                         case NUM_TEAM_1:
1056                                                                         {
1057                                                                                 if (!TeamBalance_IsTeamAllowed(balance, 1))
1058                                                                                 {
1059                                                                                         LOG_INFO("Sorry, can't move player to red team if it doesn't exist.");
1060                                                                                         TeamBalance_Destroy(balance);
1061                                                                                         return;
1062                                                                                 }
1063                                                                                 TeamBalance_Destroy(balance);
1064                                                                                 break;
1065                                                                         }
1066                                                                         case NUM_TEAM_2:
1067                                                                         {
1068                                                                                 if (!TeamBalance_IsTeamAllowed(balance, 2))
1069                                                                                 {
1070                                                                                         LOG_INFO("Sorry, can't move player to blue team if it doesn't exist.");
1071                                                                                         TeamBalance_Destroy(balance);
1072                                                                                         return;
1073                                                                                 }
1074                                                                                 TeamBalance_Destroy(balance);
1075                                                                                 break;
1076                                                                         }
1077                                                                         case NUM_TEAM_3:
1078                                                                         {
1079                                                                                 if (!TeamBalance_IsTeamAllowed(balance, 3))
1080                                                                                 {
1081                                                                                         LOG_INFO("Sorry, can't move player to yellow team if it doesn't exist.");
1082                                                                                         TeamBalance_Destroy(balance);
1083                                                                                         return;
1084                                                                                 }
1085                                                                                 TeamBalance_Destroy(balance);
1086                                                                                 break;
1087                                                                         }
1088                                                                         case NUM_TEAM_4:
1089                                                                         {
1090                                                                                 if (!TeamBalance_IsTeamAllowed(balance, 4))
1091                                                                                 {
1092                                                                                         LOG_INFO("Sorry, can't move player to pink team if it doesn't exist.");
1093                                                                                         TeamBalance_Destroy(balance);
1094                                                                                         return;
1095                                                                                 }
1096                                                                                 TeamBalance_Destroy(balance);
1097                                                                                 break;
1098                                                                         }
1099                                                                         default:
1100                                                                         {
1101                                                                                 LOG_INFO("Sorry, can't move player here if team ", destination, " doesn't exist.");
1102                                                                                 return;
1103                                                                         }
1104                                                                 }
1105
1106                                                                 // If so, lets continue and finally move the player
1107                                                                 Player_SetForcedTeamIndex(client, TEAM_FORCE_DEFAULT);
1108                                                                 if (MoveToTeam(client, Team_TeamToIndex(team_id), 6))
1109                                                                 {
1110                                                                         successful = strcat(successful, (successful ? ", " : ""), playername(client, false));
1111                                                                         LOG_INFO("Player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ") has been moved to the ", Team_ColoredFullName(team_id), "^7.");
1112                                                                 }
1113                                                                 else
1114                                                                 {
1115                                                                         LOG_INFO("Unable to move player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ")");
1116                                                                 }
1117                                                                 continue;
1118                                                         }
1119                                                         else
1120                                                         {
1121                                                                 LOG_INFO("Can't change teams when currently not playing a team game.");
1122                                                                 return;
1123                                                         }
1124                                                 }
1125                                                 else
1126                                                 {
1127                                                         LOG_INFO("Can't change teams if the player isn't in the game.");  // well technically we could, but should we allow that? :P
1128                                                         return;
1129                                                 }
1130                                         }
1131                                 }
1132
1133                                 if (successful) bprint("Successfully moved players ", successful, " to destination ", destination, ".\n");
1134                                 else LOG_INFO("No players given (", original_targets, ") are able to move.");
1135
1136                                 return;  // still correct parameters so return to avoid usage print
1137                         }
1138                 }
1139
1140                 default:
1141                         LOG_INFO("Incorrect parameters for ^2moveplayer^7");
1142                 case CMD_REQUEST_USAGE:
1143                 {
1144                         LOG_INFO("Usage:^3 sv_cmd moveplayer clients destination");
1145                         LOG_INFO("  'clients' is a list (separated by commas) of player entity ID's or nicknames");
1146                         LOG_INFO("  'destination' is what to send the player to, be it team or spectating");
1147                         LOG_INFO("  Full list of destinations here: \"spec, spectator, red, blue, yellow, pink, auto.\"");
1148                         LOG_INFO("Examples: sv_cmd moveplayer 1,3,5 red 3");
1149                         LOG_INFO("          sv_cmd moveplayer 2 spec ");
1150                         LOG_INFO("See also: ^2allspec, shuffleteams^7");
1151                         return;
1152                 }
1153         }
1154 }
1155
1156 void GameCommand_nospectators(int request)
1157 {
1158         switch (request)
1159         {
1160                 case CMD_REQUEST_COMMAND:
1161                 {
1162                         blockSpectators = 1;
1163                         // give every spectator <g_maxplayers_spectator_blocktime> seconds time to become a player
1164                         FOREACH_CLIENT(IS_REAL_CLIENT(it) && (IS_SPEC(it) || IS_OBSERVER(it)) && !it.caplayer, {
1165                                 if(!it.caplayer)
1166                                 {
1167                                         CS(it).spectatortime = time;
1168                                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_INFO, INFO_SPECTATE_WARNING, autocvar_g_maxplayers_spectator_blocktime);
1169                                 }
1170                         });
1171                         bprint(strcat("^7All spectators will be automatically kicked when not joining the game after ", ftos(autocvar_g_maxplayers_spectator_blocktime), " seconds!\n"));
1172                         return;
1173                 }
1174
1175                 default:
1176                 case CMD_REQUEST_USAGE:
1177                 {
1178                         LOG_INFO("Usage:^3 sv_cmd nospectators");
1179                         LOG_INFO("  No arguments required.");
1180                         return;
1181                 }
1182         }
1183 }
1184
1185 void GameCommand_printstats(int request)
1186 {
1187         switch (request)
1188         {
1189                 case CMD_REQUEST_COMMAND:
1190                 {
1191                         DumpStats(false);
1192                         LOG_INFO("stats dumped.");
1193                         return;
1194                 }
1195
1196                 default:
1197                 case CMD_REQUEST_USAGE:
1198                 {
1199                         LOG_INFO("Usage:^3 sv_cmd printstats");
1200                         LOG_INFO("  No arguments required.");
1201                         return;
1202                 }
1203         }
1204 }
1205
1206 void GameCommand_radarmap(int request, int argc)
1207 {
1208         switch (request)
1209         {
1210                 case CMD_REQUEST_COMMAND:
1211                 {
1212                         if (RadarMap_Make(argc)) return;
1213                 }
1214
1215                 default:
1216                         LOG_INFO("Incorrect parameters for ^2radarmap^7");
1217                 case CMD_REQUEST_USAGE:
1218                 {
1219                         LOG_INFO("Usage:^3 sv_cmd radarmap [--force] [--loop] [--quit] [--block | --trace | --sample | --lineblock] [--sharpen N] [--res W H] [--qual Q]");
1220                         LOG_INFO("  The quality factor Q is roughly proportional to the time taken.");
1221                         LOG_INFO("  trace supports no quality factor; its result should look like --block with infinite quality factor.");
1222                         LOG_INFO("See also: ^2make_mapinfo^7");
1223                         return;
1224                 }
1225         }
1226 }
1227
1228 void GameCommand_reducematchtime(int request)
1229 {
1230         switch (request)
1231         {
1232                 case CMD_REQUEST_COMMAND:
1233                 {
1234                         changematchtime(autocvar_timelimit_decrement * -60, autocvar_timelimit_min * 60, autocvar_timelimit_max * 60);
1235                         return;
1236                 }
1237
1238                 default:
1239                 case CMD_REQUEST_USAGE:
1240                 {
1241                         LOG_INFO("Usage:^3 sv_cmd reducematchtime");
1242                         LOG_INFO("  No arguments required.");
1243                         LOG_INFO("See also: ^2extendmatchtime^7");
1244                         return;
1245                 }
1246         }
1247 }
1248
1249 void GameCommand_setbots(int request, int argc)
1250 {
1251         switch (request)
1252         {
1253                 case CMD_REQUEST_COMMAND:
1254                 {
1255                         if (argc >= 2)
1256                         {
1257                                 cvar_settemp("minplayers", "0");
1258                                 cvar_settemp("minplayers_per_team", "0");
1259                                 cvar_settemp("bot_number", argv(1));
1260                                 bot_fixcount();
1261                                 return;
1262                         }
1263                 }
1264
1265                 default:
1266                         LOG_INFO("Incorrect parameters for ^2setbots^7");
1267                 case CMD_REQUEST_USAGE:
1268                 {
1269                         LOG_INFO("Usage:^3 sv_cmd setbots botnumber");
1270                         LOG_INFO("  Where 'botnumber' is the amount of bots to set bot_number cvar to.");
1271                         LOG_INFO("See also: ^2bot_cmd^7");
1272                         return;
1273                 }
1274         }
1275 }
1276
1277 void GameCommand_shuffleteams(int request)
1278 {
1279         switch (request)
1280         {
1281                 case CMD_REQUEST_COMMAND:
1282                 {
1283                         if (!teamplay)
1284                         {
1285                                 LOG_INFO("Can't shuffle teams when currently not playing a team game.");
1286                                 return;
1287                         }
1288
1289                         FOREACH_CLIENT(IS_PLAYER(it) || it.caplayer, {
1290                                 if (Player_HasRealForcedTeam(it)) {
1291                                         // we could theoretically assign forced players to their teams
1292                                         // and shuffle the rest to fill the empty spots but in practise
1293                                         // either all players or none are gonna have forced teams
1294                                         LOG_INFO("Can't shuffle teams because at least one player has a forced team.");
1295                                         return;
1296                                 }
1297                         });
1298
1299                         int number_of_teams = 0;
1300                         entity balance = TeamBalance_CheckAllowedTeams(NULL);
1301                         for (int i = 1; i <= NUM_TEAMS; ++i)
1302                         {
1303                                 if (TeamBalance_IsTeamAllowed(balance, i))
1304                                 {
1305                                         number_of_teams = max(i, number_of_teams);
1306                                 }
1307                         }
1308                         TeamBalance_Destroy(balance);
1309
1310                         int team_index = 0;
1311                         FOREACH_CLIENT_RANDOM(IS_PLAYER(it) || it.caplayer, {
1312                                 int target_team_index = team_index + 1;
1313                                 if (Entity_GetTeamIndex(it) != target_team_index)
1314                                 {
1315                                         MoveToTeam(it, target_team_index, 6);
1316                                 }
1317                                 team_index = (team_index + 1) % number_of_teams;
1318                         });
1319
1320                         bprint("Successfully shuffled the players around randomly.\n");
1321                         return;
1322                 }
1323
1324                 default:
1325                 case CMD_REQUEST_USAGE:
1326                 {
1327                         LOG_INFO("Usage:^3 sv_cmd shuffleteams");
1328                         LOG_INFO("  No arguments required.");
1329                         LOG_INFO("See also: ^2moveplayer, allspec^7");
1330                         return;
1331                 }
1332         }
1333 }
1334
1335 void GameCommand_stuffto(int request, int argc)
1336 {
1337         // This... is a fairly dangerous and powerful command... - It allows any arguments to be sent to a client via rcon.
1338         // Because of this, it is disabled by default and must be enabled by the server owner when doing compilation. That way,
1339         // we can be certain they understand the risks of it... So to enable, compile server with -DSTUFFTO_ENABLED argument.
1340
1341 #ifdef STUFFTO_ENABLED
1342                 switch (request)
1343                 {
1344                         case CMD_REQUEST_COMMAND:
1345                         {
1346                                 if (argv(2))
1347                                 {
1348                                         entity client = GetIndexedEntity(argc, 1);
1349                                         float accepted = VerifyClientEntity(client, true, false);
1350
1351                                         if (accepted > 0)
1352                                         {
1353                                                 stuffcmd(client, strcat("\n", argv(next_token), "\n"));
1354                                                 LOG_INFO("Command: \"", argv(next_token), "\" sent to ", GetCallerName(client), " (", argv(1), ").");
1355                                         }
1356                                         else
1357                                         {
1358                                                 LOG_INFO("stuffto: ", GetClientErrorString(accepted, argv(1)), ".");
1359                                         }
1360
1361                                         return;
1362                                 }
1363                         }
1364
1365                         default:
1366                                 LOG_INFO("Incorrect parameters for ^2stuffto^7");
1367                         case CMD_REQUEST_USAGE:
1368                         {
1369                                 LOG_INFO("Usage:^3 sv_cmd stuffto client \"command\"");
1370                                 LOG_INFO("  'client' is the entity number or name of the player,");
1371                                 LOG_INFO("  and 'command' is the command to be sent to that player.");
1372                                 return;
1373                         }
1374                 }
1375 #else
1376                 if (request)
1377                 {
1378                         LOG_INFO("stuffto command is not enabled on this server.");
1379                         return;
1380                 }
1381 #endif
1382 }
1383
1384 void GameCommand_trace(int request, int argc)
1385 {
1386         switch (request)
1387         {
1388                 case CMD_REQUEST_COMMAND:
1389                 {
1390                         entity e;
1391                         vector org, delta, start, end, p, q, q0, pos, vv, dv;
1392                         float i, f, safe, unsafe, dq, dqf;
1393
1394                         switch (argv(1))
1395                         {
1396                                 case "debug":
1397                                 {
1398                                         float hitcount = 0;
1399                                         LOG_INFO("TEST CASE. If this returns the runaway loop counter error, possibly everything is oaky.");
1400                                         float worst_endpos_bug = 0;
1401                                         for ( ; ; )
1402                                         {
1403                                                 org = world.mins;
1404                                                 delta = world.maxs - world.mins;
1405
1406                                                 start.x = org.x + random() * delta.x;
1407                                                 start.y = org.y + random() * delta.y;
1408                                                 start.z = org.z + random() * delta.z;
1409
1410                                                 end.x = org.x + random() * delta.x;
1411                                                 end.y = org.y + random() * delta.y;
1412                                                 end.z = org.z + random() * delta.z;
1413
1414                                                 start = stov(vtos(start));
1415                                                 end = stov(vtos(end));
1416
1417                                                 tracebox(start, PL_MIN_CONST, PL_MAX_CONST, end, MOVE_NOMONSTERS, NULL);
1418                                                 if (!trace_startsolid && trace_fraction < 1)
1419                                                 {
1420                                                         p = trace_endpos;
1421                                                         tracebox(p, PL_MIN_CONST, PL_MAX_CONST, p, MOVE_NOMONSTERS, NULL);
1422                                                         if (trace_startsolid)
1423                                                         {
1424                                                                 rint(42);  // do an engine breakpoint on VM_rint so you can get the trace that errnoeously returns startsolid
1425                                                                 tracebox(start, PL_MIN_CONST, PL_MAX_CONST, end, MOVE_NOMONSTERS, NULL);
1426
1427                                                                 // how much do we need to back off?
1428                                                                 safe = 1;
1429                                                                 unsafe = 0;
1430                                                                 for ( ; ; )
1431                                                                 {
1432                                                                         pos = p * (1 - (safe + unsafe) * 0.5) + start * ((safe + unsafe) * 0.5);
1433                                                                         tracebox(pos, PL_MIN_CONST, PL_MAX_CONST, pos, MOVE_NOMONSTERS, NULL);
1434                                                                         if (trace_startsolid)
1435                                                                         {
1436                                                                                 if ((safe + unsafe) * 0.5 == unsafe) break;
1437                                                                                 unsafe = (safe + unsafe) * 0.5;
1438                                                                         }
1439                                                                         else
1440                                                                         {
1441                                                                                 if ((safe + unsafe) * 0.5 == safe) break;
1442                                                                                 safe = (safe + unsafe) * 0.5;
1443                                                                         }
1444                                                                 }
1445
1446                                                                 LOG_INFO("safe distance to back off: ", ftos(safe * vlen(p - start)), "qu");
1447                                                                 LOG_INFO("unsafe distance to back off: ", ftos(unsafe * vlen(p - start)), "qu");
1448
1449                                                                 tracebox(p, PL_MIN_CONST + '0.1 0.1 0.1', PL_MAX_CONST - '0.1 0.1 0.1', p, MOVE_NOMONSTERS, NULL);
1450                                                                 if (trace_startsolid) LOG_INFO("trace_endpos much in solid when tracing from ", vtos(start), " to ", vtos(end), " endpos ", vtos(p));
1451                                                                 else LOG_INFO("trace_endpos just in solid when tracing from ", vtos(start), " to ", vtos(end), " endpos ", vtos(p));
1452                                                                 if (++hitcount >= 10) break;
1453                                                         }
1454                                                         else
1455                                                         {
1456                                                                 q0 = p;
1457                                                                 dq = 0;
1458                                                                 dqf = 1;
1459                                                                 for ( ; ; )
1460                                                                 {
1461                                                                         q = p + normalize(end - p) * (dq + dqf);
1462                                                                         if (q == q0) break;
1463                                                                         tracebox(p, PL_MIN_CONST, PL_MAX_CONST, q, MOVE_NOMONSTERS, NULL);
1464                                                                         if (trace_startsolid) error("THIS ONE cannot happen");
1465                                                                         if (trace_fraction > 0) dq += dqf * trace_fraction;
1466                                                                         dqf *= 0.5;
1467                                                                         q0 = q;
1468                                                                 }
1469                                                                 if (dq > worst_endpos_bug)
1470                                                                 {
1471                                                                         worst_endpos_bug = dq;
1472                                                                         LOG_INFO("trace_endpos still before solid when tracing from ", vtos(start), " to ", vtos(end), " endpos ", vtos(p));
1473                                                                         LOG_INFO("could go ", ftos(dq), " units further to ", vtos(q));
1474                                                                         if (++hitcount >= 10) break;
1475                                                                 }
1476                                                         }
1477                                                 }
1478                                         }
1479                                         return;
1480                                 }
1481
1482                                 case "debug2":
1483                                 {
1484                                         e = nextent(NULL);
1485                                         tracebox(e.origin + '0 0 32', e.mins, e.maxs, e.origin + '0 0 -1024', MOVE_NORMAL, e);
1486                                         vv = trace_endpos;
1487                                         if (trace_fraction == 1)
1488                                         {
1489                                                 LOG_INFO("not above ground, aborting");
1490                                                 return;
1491                                         }
1492                                         f = 0;
1493                                         for (i = 0; i < 100000; ++i)
1494                                         {
1495                                                 dv = randomvec();
1496                                                 if (dv.z > 0) dv = -1 * dv;
1497                                                 tracebox(vv, e.mins, e.maxs, vv + dv, MOVE_NORMAL, e);
1498                                                 if (trace_startsolid) LOG_INFO("bug 1");
1499                                                 if (trace_fraction == 1)
1500                                                 {
1501                                                         if (dv.z < f)
1502                                                         {
1503                                                                 LOG_INFO("bug 2: ", ftos(dv.x), " ", ftos(dv.y), " ", ftos(dv.z));
1504                                                                 LOG_INFO(" (", ftos(asin(dv.z / vlen(dv)) * 180 / M_PI), " degrees)");
1505                                                                 f = dv.z;
1506                                                         }
1507                                                 }
1508                                         }
1509                                         LOG_INFO("highest possible dist: ", ftos(f));
1510                                         return;
1511                                 }
1512
1513                                 case "walk":
1514                                 {
1515                                         if (argc == 4 || argc == 5)
1516                                         {
1517                                                 e = nextent(NULL);
1518                                                 int dphitcontentsmask_save = e.dphitcontentsmask;
1519                                                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
1520                                                 if (tracewalk(e, stov(argv(2)), e.mins, e.maxs, stov(argv(3)), stof(argv(4)), MOVE_NORMAL))
1521                                                         LOG_INFO("can walk");
1522                                                 else
1523                                                         LOG_INFO("cannot walk");
1524                                                 e.dphitcontentsmask = dphitcontentsmask_save;
1525                                                 return;
1526                                         }
1527                                 }
1528
1529                                 case "showline":
1530                                 {
1531                                         if (argc == 4)
1532                                         {
1533                                                 vv = stov(argv(2));
1534                                                 dv = stov(argv(3));
1535                                                 traceline(vv, dv, MOVE_NORMAL, NULL);
1536                                                 __trailparticles(NULL, particleeffectnum(EFFECT_TR_NEXUIZPLASMA), vv, trace_endpos);
1537                                                 __trailparticles(NULL, particleeffectnum(EFFECT_TR_CRYLINKPLASMA), trace_endpos, dv);
1538                                                 return;
1539                                         }
1540                                 }
1541
1542                                         // no default case, just go straight to invalid
1543                         }
1544                 }
1545
1546                 default:
1547                         LOG_INFO("Incorrect parameters for ^2trace^7");
1548                 case CMD_REQUEST_USAGE:
1549                 {
1550                         LOG_INFO("Usage:^3 sv_cmd trace command [startpos endpos] [endpos_height]");
1551                         LOG_INFO("  Where startpos and endpos are parameters for 'walk' and 'showline' commands,");
1552                         LOG_INFO("  'endpos_height' is an optional parameter for 'walk' command,");
1553                         LOG_INFO("  Full list of commands here: \"debug, debug2, walk, showline.\"");
1554                         LOG_INFO("See also: ^2bbox, gettaginfo^7");
1555                         return;
1556                 }
1557         }
1558 }
1559
1560 void GameCommand_unlockteams(int request)
1561 {
1562         switch (request)
1563         {
1564                 case CMD_REQUEST_COMMAND:
1565                 {
1566                         if (teamplay)
1567                         {
1568                                 lockteams = 0;
1569                                 bprint("^1The teams are now unlocked.\n");
1570                         }
1571                         else
1572                         {
1573                                 bprint("unlockteams command can only be used in a team-based gamemode.\n");
1574                         }
1575                         return;
1576                 }
1577
1578                 default:
1579                 case CMD_REQUEST_USAGE:
1580                 {
1581                         LOG_INFO("Usage:^3 sv_cmd unlockteams");
1582                         LOG_INFO("  No arguments required.");
1583                         LOG_INFO("See also: ^2lockteams^7");
1584                         return;
1585                 }
1586         }
1587 }
1588
1589 void GameCommand_warp(int request, int argc)
1590 {
1591         switch (request)
1592         {
1593                 case CMD_REQUEST_COMMAND:
1594                 {
1595                         if (autocvar_g_campaign)
1596                         {
1597                                 if (argc >= 2)
1598                                 {
1599                                         CampaignLevelWarp(stof(argv(1)));
1600                                         LOG_INFO("Successfully warped to campaign level ", argv(1), ".");
1601                                 }
1602                                 else
1603                                 {
1604                                         CampaignLevelWarp(-1);
1605                                         LOG_INFO("Successfully warped to next campaign level.");
1606                                 }
1607                         }
1608                         else
1609                         {
1610                                 LOG_INFO("Not in campaign, can't level warp");
1611                         }
1612                         return;
1613                 }
1614
1615                 default:
1616                 case CMD_REQUEST_USAGE:
1617                 {
1618                         LOG_INFO("Usage:^3 sv_cmd warp [level]");
1619                         LOG_INFO("  'level' is the level to change campaign mode to.");
1620                         LOG_INFO("  if 'level' is not provided it will change to the next level.");
1621                         return;
1622                 }
1623         }
1624 }
1625
1626 /* use this when creating a new command, making sure to place it in alphabetical order... also,
1627 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
1628 void GameCommand_(int request)
1629 {
1630     switch(request)
1631     {
1632         case CMD_REQUEST_COMMAND:
1633         {
1634
1635             return;
1636         }
1637
1638         default:
1639         case CMD_REQUEST_USAGE:
1640         {
1641             print("\nUsage:^3 sv_cmd \n");
1642             print("  No arguments required.\n");
1643             return;
1644         }
1645     }
1646 }
1647 */
1648
1649
1650 // ==================================
1651 //  Macro system for server commands
1652 // ==================================
1653
1654 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
1655 SERVER_COMMAND(adminmsg, "Send an admin message to a client directly") { GameCommand_adminmsg(request, arguments); }
1656 SERVER_COMMAND(allready, "Restart the server and reset the players") { GameCommand_allready(request); }
1657 SERVER_COMMAND(allspec, "Force all players to spectate") { GameCommand_allspec(request, arguments); }
1658 SERVER_COMMAND(anticheat, "Create an anticheat report for a client") { GameCommand_anticheat(request, arguments); }
1659 SERVER_COMMAND(animbench, "Benchmark model animation (LAGS)") { GameCommand_animbench(request, arguments); }
1660 SERVER_COMMAND(bbox, "Print detailed information about world size") { GameCommand_bbox(request); }
1661 SERVER_COMMAND(bot_cmd, "Control and send commands to bots") { GameCommand_bot_cmd(request, arguments, command); }
1662 SERVER_COMMAND(cointoss, "Flip a virtual coin and give random result") { GameCommand_cointoss(request, arguments); }
1663 SERVER_COMMAND(database, "Extra controls of the serverprogs database") { GameCommand_database(request, arguments); }
1664 SERVER_COMMAND(defer_clear, "Clear all queued defer commands for a specific client") { GameCommand_defer_clear(request, arguments); }
1665 SERVER_COMMAND(defer_clear_all, "Clear all queued defer commands for all clients") { GameCommand_defer_clear_all(request); }
1666 SERVER_COMMAND(delrec, "Delete race time record for a map") { GameCommand_delrec(request, arguments); }
1667 SERVER_COMMAND(effectindexdump, "Dump list of effects from code and effectinfo.txt") { GameCommand_effectindexdump(request); }
1668 SERVER_COMMAND(extendmatchtime, "Increase the timelimit value incrementally") { GameCommand_extendmatchtime(request); }
1669 SERVER_COMMAND(gametype, "Simple command to change the active gametype") { GameCommand_gametype(request, arguments); }
1670 SERVER_COMMAND(gettaginfo, "Get specific information about a weapon model") { GameCommand_gettaginfo(request, arguments); }
1671 SERVER_COMMAND(gotomap, "Simple command to switch to another map") { GameCommand_gotomap(request, arguments); }
1672 SERVER_COMMAND(lockteams, "Disable the ability for players to switch or enter teams") { GameCommand_lockteams(request); }
1673 SERVER_COMMAND(make_mapinfo, "Automatically rebuild mapinfo files") { GameCommand_make_mapinfo(request); }
1674 SERVER_COMMAND(moveplayer, "Change the team/status of a player") { GameCommand_moveplayer(request, arguments); }
1675 SERVER_COMMAND(nospectators, "Automatically remove spectators from a match") { GameCommand_nospectators(request); }
1676 SERVER_COMMAND(printstats, "Dump eventlog player stats and other score information") { GameCommand_printstats(request); }
1677 SERVER_COMMAND(radarmap, "Generate a radar image of the map") { GameCommand_radarmap(request, arguments); }
1678 SERVER_COMMAND(reducematchtime, "Decrease the timelimit value incrementally") { GameCommand_reducematchtime(request); }
1679 SERVER_COMMAND(setbots, "Adjust how many bots are in the match") { GameCommand_setbots(request, arguments); }
1680 SERVER_COMMAND(shuffleteams, "Randomly move players to different teams") { GameCommand_shuffleteams(request); }
1681 SERVER_COMMAND(stuffto, "Send a command to be executed on a client") { GameCommand_stuffto(request, arguments); }
1682 SERVER_COMMAND(trace, "Various debugging tools with tracing") { GameCommand_trace(request, arguments); }
1683 SERVER_COMMAND(unlockteams, "Enable the ability for players to switch or enter teams") { GameCommand_unlockteams(request); }
1684 SERVER_COMMAND(warp, "Choose different level in campaign") { GameCommand_warp(request, arguments); }
1685
1686 void GameCommand_macro_help()
1687 {
1688         FOREACH(SERVER_COMMANDS, true, { LOG_INFOF("  ^2%s^7: %s", it.m_name, it.m_description); });
1689 }
1690
1691 float GameCommand_macro_command(int argc, string command)
1692 {
1693         string c = strtolower(argv(0));
1694         FOREACH(SERVER_COMMANDS, it.m_name == c, {
1695                 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
1696                 return true;
1697         });
1698         return false;
1699 }
1700
1701 float GameCommand_macro_usage(int argc)
1702 {
1703         string c = strtolower(argv(1));
1704         FOREACH(SERVER_COMMANDS, it.m_name == c, {
1705                 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
1706                 return true;
1707         });
1708         return false;
1709 }
1710
1711 void GameCommand_macro_write_aliases(float fh)
1712 {
1713         FOREACH(SERVER_COMMANDS, true, { CMD_Write_Alias("qc_cmd_sv", it.m_name, it.m_description); });
1714 }
1715
1716
1717 // =========================================
1718 //  Main Function Called By Engine (sv_cmd)
1719 // =========================================
1720 // If this function exists, game code handles gamecommand instead of the engine code.
1721
1722 void GameCommand(string command)
1723 {
1724         int argc = tokenize_console(command);
1725
1726         // Guide for working with argc arguments by example:
1727         // argc:   1    - 2      - 3     - 4
1728         // argv:   0    - 1      - 2     - 3
1729         // cmd     vote - master - login - password
1730
1731         if (strtolower(argv(0)) == "help")
1732         {
1733                 if (argc == 1)
1734                 {
1735                         LOG_INFO("Server console commands:");
1736                         GameCommand_macro_help();
1737
1738                         LOG_INFO("\nBanning commands:");
1739                         BanCommand_macro_help();
1740
1741                         LOG_INFO("\nCommon networked commands:");
1742                         CommonCommand_macro_help(NULL);
1743
1744                         LOG_INFO("\nGeneric commands shared by all programs:");
1745                         GenericCommand_macro_help();
1746
1747                         LOG_INFO(
1748                             "\nUsage:^3 sv_cmd COMMAND...^7, where possible commands are listed above.\n"
1749                 "For help about a specific command, type sv_cmd help COMMAND"
1750             );
1751
1752                         return;
1753                 }
1754                 else if (BanCommand_macro_usage(argc))  // Instead of trying to call a command, we're going to see detailed information about it
1755                 {
1756                         return;
1757                 }
1758                 else if (CommonCommand_macro_usage(argc, NULL))  // same here, but for common commands instead
1759                 {
1760                         return;
1761                 }
1762                 else if (GenericCommand_macro_usage(argc))  // same here, but for generic commands instead
1763                 {
1764                         return;
1765                 }
1766                 else if (GameCommand_macro_usage(argc))  // finally try for normal commands too
1767                 {
1768                         return;
1769                 }
1770         }
1771         else if (MUTATOR_CALLHOOK(SV_ParseServerCommand, strtolower(argv(0)), argc, command))
1772         {
1773                 return;  // handled by a mutator
1774         }
1775         else if (BanCommand(command))
1776         {
1777                 return;  // handled by server/command/ipban.qc
1778         }
1779         else if (CommonCommand_macro_command(argc, NULL, command))
1780         {
1781                 return;  // handled by server/command/common.qc
1782         }
1783         else if (GenericCommand(command))
1784         {
1785                 return;                                        // handled by common/command/generic.qc
1786         }
1787         else if (GameCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
1788         {
1789                 return;                                        // handled by one of the above GameCommand_* functions
1790         }
1791
1792         // nothing above caught the command, must be invalid
1793         LOG_INFO(((command != "") ? strcat("Unknown server command \"", command, "\"") : "No command provided"), ". For a list of supported commands, try sv_cmd help.");
1794 }