]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
0cf5702e3ec56068fb5cec3d8d3d473109ce3b49
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 .string clipboard_model;
2 .float clipboard_movetype;
3
4 entity sandbox_EditObject()
5 {
6         // returns the traced entity if the player can edit it, and world if not
7
8         makevectors(self.v_angle);
9         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
10         if(trace_ent.classname == "object" && trace_ent.realowner == self)
11                 return trace_ent;
12         else
13                 return world;
14 }
15
16 entity sandbox_SpawnObject()
17 {
18         // spawn a new object with default properties
19
20         entity e;
21         e = spawn();
22         e.realowner = self;
23         e.classname = "object";
24         e.takedamage = DAMAGE_NO;
25         e.movetype = MOVETYPE_TOSS;
26         e.solid = SOLID_BSP;
27
28         // set origin and direction based on player position and view angle
29         makevectors(self.v_angle);
30         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
31         setorigin(e, trace_endpos);
32         e.angles_y = self.v_angle_y;
33
34         return e;
35 }
36
37 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
38 {
39         if(MUTATOR_RETURNVALUE) // command was already handled?
40                 return FALSE;
41         if(cmd_name == "g_sandbox")
42         {
43                 if(cmd_argc < 2)
44                 {
45                         print_to(self, "Sandbox mode is active. For more information, use 'sandbox help'");
46                         return TRUE;
47                 }
48
49                 entity e;
50                 if(argv(1) == "help")
51                 {
52                         print_to(self, "You can use the following sandbox commands:");
53                         print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
54                         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");
55                         print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
56                         print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
57                         return TRUE;
58                 }
59                 else if(argv(1) == "spawn_item")
60                 {
61                         // only weapons are currently supported
62
63                         if(cmd_argc < 3)
64                         {
65                                 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");
66                                 return TRUE;
67                         }
68
69                         // spawn a new item
70                         float i;
71                         makevectors(self.v_angle);
72                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
73
74                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
75                         {
76                                 e = get_weaponinfo(i);
77                                 if(e.netname == argv(2))
78                                 {
79                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
80                                         if(autocvar_g_sandbox_info)
81                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
82                                         return TRUE;
83                                 }
84                         }
85
86                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
87                         return TRUE;
88                 }
89                 else if(argv(1) == "spawn_object")
90                 {
91                         // don't allow spawning objects without a model
92                         if(cmd_argc < 3)
93                         {
94                                 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");
95                                 return TRUE;
96                         }
97                         else if not(fexists(argv(2)))
98                         {
99                                 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
100                                 return TRUE;
101                         }
102
103                         e = sandbox_SpawnObject();
104                         setmodel(e, argv(2));
105
106                         if(autocvar_g_sandbox_info)
107                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
108
109                         return TRUE;
110                 }
111                 else if(argv(1) == "remove_object")
112                 {
113                         e = sandbox_EditObject();
114                         if(e != world)
115                         {
116                                 if(autocvar_g_sandbox_info)
117                                         print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
118                                 remove(e);
119                                 e = world;
120                                 return TRUE;
121                         }
122
123                         print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
124                         return TRUE;
125                 }
126                 else if(argv(1) == "duplicate_object_copy")
127                 {
128                         // copies customizable properties of the selected object to the clipboard
129
130                         e = sandbox_EditObject(); // you can only copy objects you can edit, so this works
131                         if(e != world)
132                         {
133                                 // -------- COPY PROPERTIES --------
134                                 self.clipboard_model = e.model;
135                                 self.clipboard_movetype = e.movetype;
136                                 // -------- COPY PROPERTIES --------
137
138                                 print_to(self, "Object copied to clipboard");
139                                 return TRUE;
140                         }
141
142                         print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
143                         return TRUE;
144                 }
145                 else if(argv(1) == "duplicate_object_paste")
146                 {
147                         // spawns a new object using the properties in the player's clipboard
148
149                         if(self.clipboard_model == "") // no object in clipboard
150                         {
151                                 print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
152                                 return TRUE;
153                         }
154
155                         e = sandbox_SpawnObject();
156
157                         // -------- PASTE PROPERTIES --------
158                         setmodel(e, self.clipboard_model);
159                         e.movetype = self.clipboard_movetype;
160                         // -------- PASTE PROPERTIES --------
161
162                         print_to(self, "Object pasted");
163                         if(autocvar_g_sandbox_info)
164                                 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
165
166                         return TRUE;
167                 }
168         }
169         return FALSE;
170 }
171
172 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
173 {
174         // if the player is close enough to their object, they can drag it
175
176         if(autocvar_sv_cheats)
177                 return FALSE; // cheat dragging is used instead
178
179         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
180         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
181         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
182         // it goes out of range while slinging it around.
183
184         entity e;
185         float grab;
186
187         e = sandbox_EditObject();
188         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
189                 grab = TRUE;
190
191         if(Drag(e, grab)) // execute dragging
192         {
193                 if(autocvar_g_sandbox_info)
194                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
195                 return TRUE;
196         }
197
198         return FALSE;
199 }
200
201 MUTATOR_DEFINITION(sandbox)
202 {
203         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
204         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
205
206         return 0;
207 }