Add default properties to newly spawned objects
[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_object ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
17                         print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
18                         return TRUE;
19                 }
20                 else if(argv(1) == "spawn_object")
21                 {
22                         // don't allow spawning objects without a model
23                         if(cmd_argc < 3)
24                         {
25                                 print_to(self, "WARNING: Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'spawn_object' command");
26                                 return TRUE;
27                         }
28                         else if not(fexists(argv(2)))
29                         {
30                                 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
31                                 return TRUE;
32                         }
33
34                         // spawn a new object with the default settings
35                         entity e;
36                         e = spawn();
37                         e.classname = "object";
38                         e.takedamage = DAMAGE_NO;
39
40                         // those properties are defaults that can be edited later
41                         e.movetype = MOVETYPE_TOSS;
42                         e.solid = SOLID_TRIGGER;
43
44                         makevectors(self.v_angle);
45                         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance, MOVE_NOMONSTERS, self);
46                         setorigin(e, trace_endpos);
47                         setmodel(e, argv(2));
48                         e.angles = self.v_angle; // give the player's angles to the object, as he spawns it from behind
49
50                         if(autocvar_g_sandbox_info)
51                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
52
53                         return TRUE;
54                 }
55                 else if(argv(1) == "spawn_item")
56                 {
57                         // spawn a new item
58                         // weapons are the only items currently supported
59
60                         if(cmd_argc < 3)
61                         {
62                                 print_to(self, "WARNING: Attempted to spawn an item without specifying its type. Please specify the name of your item after the 'spawn_item' command");
63                                 return TRUE;
64                         }
65
66                         entity e;
67                         float i;
68                         makevectors(self.v_angle);
69                         traceline(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance, MOVE_NOMONSTERS, self);
70
71                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
72                         {
73                                 e = get_weaponinfo(i);
74                                 if(e.netname == argv(2))
75                                 {
76                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
77                                         if(autocvar_g_sandbox_info)
78                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
79                                         return TRUE;
80                                 }
81                         }
82
83                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'g_sandbox help' for supported items");
84                         return TRUE;
85                 }
86         }
87         return FALSE;
88 }
89
90 MUTATOR_DEFINITION(sandbox)
91 {
92         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
93
94         return 0;
95 }