]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
Use a different method of checking grab distance
[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 default properties
35                         entity e;
36                         e = spawn();
37                         e.realowner = self;
38                         e.classname = "object";
39                         e.takedamage = DAMAGE_NO;
40                         e.movetype = MOVETYPE_TOSS;
41                         e.solid = SOLID_BSP;
42
43                         makevectors(self.v_angle);
44                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
45                         setorigin(e, trace_endpos);
46                         setmodel(e, argv(2));
47                         e.angles_y = self.v_angle_y; // apply the player's direction to the object, as he spawns it from behind
48
49                         if(autocvar_g_sandbox_info)
50                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
51
52                         return TRUE;
53                 }
54                 else if(argv(1) == "spawn_item")
55                 {
56                         // weapons are the only items currently supported
57
58                         if(cmd_argc < 3)
59                         {
60                                 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");
61                                 return TRUE;
62                         }
63
64                         // spawn a new item
65                         entity e;
66                         float i;
67                         makevectors(self.v_angle);
68                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
69
70                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
71                         {
72                                 e = get_weaponinfo(i);
73                                 if(e.netname == argv(2))
74                                 {
75                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
76                                         if(autocvar_g_sandbox_info)
77                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
78                                         return TRUE;
79                                 }
80                         }
81
82                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'g_sandbox help' for supported items");
83                         return TRUE;
84                 }
85                 else if(argv(1) == "remove_object")
86                 {
87                         makevectors(self.v_angle);
88                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
89                         if(trace_ent.classname == "object")
90                         {
91                                 if(autocvar_g_sandbox_info)
92                                         print(strcat(self.netname, " removed an object at origin ", vtos(trace_ent.origin), "\n"));
93                                 remove(trace_ent);
94                                 trace_ent = world;
95                                 return TRUE;
96                         }
97
98                         print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that you have spawned");
99                         return TRUE;
100                 }
101         }
102         return FALSE;
103 }
104
105 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
106 {
107         // if the player is close enough to their own object and facing it, they can grab it
108
109         if(autocvar_sv_cheats)
110                 return FALSE; // cheats already allow dragging all objects
111
112         crosshair_trace_plusvisibletriggers(self);
113         if(trace_ent.classname == "object")
114                 if(Drag(trace_ent, autocvar_g_sandbox_editor_distance_edit)) // execute the dragging
115                         if(autocvar_g_sandbox_info)
116                                 print(strcat(self.netname, " grabbed an object at origin ", vtos(trace_ent.origin), "\n"));
117
118         return FALSE;
119 }
120
121 MUTATOR_DEFINITION(sandbox)
122 {
123         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
124         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
125
126         return 0;
127 }