Add a spawnitem command, which allows players to spawn items on the map. Currently...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
2 {
3         if(MUTATOR_RETURNVALUE) // command was already handled?
4                 return FALSE;
5         if(cmd_name == "g_sandbox")
6         {
7                 if(cmd_argc < 2)
8                 {
9                         print_to(self, "Sandbox mode is active. For more information, use 'g_sandbox help'");
10                         return TRUE;
11                 }
12
13                 if(argv(1) == "help")
14                 {
15                         print_to(self, "You can use the following sandbox commands:");
16                         print_to(self, "^7\"^2spawn ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
17                         return TRUE;
18                 }
19                 else if(argv(1) == "spawn")
20                 {
21                         // spawn a new object with the default settings
22
23                         // don't allow spawning objects without a model
24                         if(cmd_argc < 3)
25                         {
26                                 print_to(self, "WARNING: Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'spawn' command");
27                                 return TRUE;
28                         }
29                         else if not(fexists(argv(2)))
30                         {
31                                 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
32                                 return TRUE;
33                         }
34
35                         entity e;
36                         e = spawn();
37                         e.classname = "object";
38
39                         makevectors(self.v_angle);
40                         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance, MOVE_NOMONSTERS, self);
41                         setorigin(e, trace_endpos);
42                         setmodel(e, argv(2));
43                         e.angles = self.v_angle; // give the player's angles to the object, as he spawns it from behind
44
45                         if(autocvar_g_sandbox_info)
46                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
47
48                         return TRUE;
49                 }
50                 else if(argv(1) == "spawnitem")
51                 {
52                         // spawn a new item
53                         // weapons are the only items currently supported
54
55                         if(cmd_argc < 3)
56                         {
57                                 print_to(self, "WARNING: Attempted to spawn an item without specifying its type. Please specify the name of your item after the 'spawnitem' command");
58                                 return TRUE;
59                         }
60
61                         entity e;
62                         float i;
63                         makevectors(self.v_angle);
64                         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance, MOVE_NOMONSTERS, self);
65
66                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
67                         {
68                                 e = get_weaponinfo(i);
69                                 if(e.netname == argv(2))
70                                 {
71                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
72                                         if(autocvar_g_sandbox_info)
73                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
74                                         return TRUE;
75                                 }
76                         }
77
78                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'g_sandbox help' for supported items");
79                         return TRUE;
80                 }
81         }
82         return FALSE;
83 }
84
85 MUTATOR_DEFINITION(sandbox)
86 {
87         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
88
89         return 0;
90 }