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