]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
6aa97b6960f9aedb87e63114ec216348f1e0ce8a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 .string object_clipboard;
2
3 entity sandbox_EditObject_Get()
4 {
5         // returns the traced entity if the player can edit it, and world if not
6
7         makevectors(self.v_angle);
8         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
9         if(trace_ent.classname == "object" && trace_ent.realowner == self)
10                 return trace_ent;
11         else
12                 return world;
13 }
14
15 void sandbox_EditObject_Scale(entity e, float f)
16 {
17         e.scale = f;
18         if(e.scale)
19         {
20                 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
21                 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
22         }
23 }
24
25 entity sandbox_SpawnObject()
26 {
27         // spawn a new object with default properties
28
29         entity e;
30         e = spawn();
31         e.realowner = self;
32         e.classname = "object";
33         e.takedamage = DAMAGE_AIM;
34         e.damageforcescale = 0;
35         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
36         e.movetype = MOVETYPE_TOSS;
37         e.frame = 0;
38         e.skin = 0;
39
40         // set origin and direction based on player position and view angle
41         makevectors(self.v_angle);
42         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
43         setorigin(e, trace_endpos);
44         e.angles_y = self.v_angle_y;
45
46         return e;
47 }
48
49 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
50 {
51         if(MUTATOR_RETURNVALUE) // command was already handled?
52                 return FALSE;
53         if(cmd_name == "g_sandbox")
54         {
55                 if(cmd_argc < 2)
56                 {
57                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
58                         return TRUE;
59                 }
60
61                 switch(argv(1))
62                 {
63                         entity e;
64
65                         // ---------------- COMMAND: HELP ----------------
66                         case "help":
67                                 print_to(self, "You can use the following sandbox commands:");
68                                 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
69                                 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");
70                                 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
71                                 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
72                                 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
73                                 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
74                                 print_to(self, "^7Object properties for ^2edit_object^7:");
75                                 print_to(self, "^3skin value ^7- changes the skin of the object");
76                                 print_to(self, "^3alpha value ^7- sets object transparency");
77                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
78                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
79                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
80                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
81                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
82                                 print_to(self, "^force value ^7- amount of force applied to objects that are shot");
83                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
84                                 return TRUE;
85
86                         // ---------------- COMMAND: SPAWN ITEM ----------------
87                         case "spawn_item":
88                                 // only weapons are currently supported
89
90                                 if(cmd_argc < 3)
91                                 {
92                                         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");
93                                         return TRUE;
94                                 }
95
96                                 // spawn a new item
97                                 float i;
98                                 makevectors(self.v_angle);
99                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
100
101                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
102                                 {
103                                         e = get_weaponinfo(i);
104                                         if(e.netname == argv(2))
105                                         {
106                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
107                                                 if(autocvar_g_sandbox_info)
108                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
109                                                 return TRUE;
110                                         }
111                                 }
112
113                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
114                                 return TRUE;
115
116                         // ---------------- COMMAND: SPAWN OBJECT ----------------
117                         case "spawn_object":
118                                 // don't allow spawning objects without a model
119                                 if(cmd_argc < 3)
120                                 {
121                                         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");
122                                         return TRUE;
123                                 }
124                                 else if not(fexists(argv(2)))
125                                 {
126                                         print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
127                                         return TRUE;
128                                 }
129
130                                 e = sandbox_SpawnObject();
131                                 setmodel(e, argv(2));
132
133                                 if(autocvar_g_sandbox_info)
134                                         print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
135
136                                 return TRUE;
137
138                         // ---------------- COMMAND: REMOVE OBJECT ----------------
139                         case "remove_object":
140                                 e = sandbox_EditObject_Get();
141                                 if(e != world)
142                                 {
143                                         if(autocvar_g_sandbox_info)
144                                                 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
145                                         remove(e);
146                                         e = world;
147                                         return TRUE;
148                                 }
149
150                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
151                                 return TRUE;
152
153                         // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
154                         case "duplicate_object_copy":
155                                 // copies customizable properties of the selected object to the clipboard
156
157                                 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
158                                 if(e != world)
159                                 {
160                                         if(self.object_clipboard)
161                                                 strunzone(self.object_clipboard);
162
163                                         // set clipboard properties
164                                         self.object_clipboard = strcat(e.model, " ");
165                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.skin), " ");
166                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.alpha), " ");
167                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", e.colormod), " ");
168                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", e.glowmod), " ");
169                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.frame), " ");
170                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.scale), " ");
171                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.movetype), " ");
172                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.damageforcescale), " ");
173
174                                         self.object_clipboard = strzone(self.object_clipboard);
175                                         print_to(self, "Object copied to clipboard");
176                                         return TRUE;
177                                 }
178
179                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
180                                 return TRUE;
181
182                         // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
183                         case "duplicate_object_paste":
184                                 // spawns a new object using the properties in the player's clipboard
185
186                                 if(!self.object_clipboard) // no object in clipboard
187                                 {
188                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
189                                         return TRUE;
190                                 }
191
192                                 e = sandbox_SpawnObject();
193                                 tokenize_console(self.object_clipboard);
194
195                                 // apply clipboard properties
196                                 setmodel(e, argv(0));
197                                 e.skin = stof(argv(1));
198                                 e.alpha = stof(argv(2));
199                                 e.colormod = stov(argv(3));
200                                 e.glowmod = stov(argv(4));
201                                 e.frame = stof(argv(5));
202                                 sandbox_EditObject_Scale(e, stof(argv(6)));
203                                 e.movetype = stof(argv(7));
204                                 e.damageforcescale = stof(argv(8));
205
206                                 print_to(self, "Object pasted");
207                                 if(autocvar_g_sandbox_info)
208                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
209
210                                 return TRUE;
211
212                         // ---------------- COMMAND: EDIT OBJECT ----------------
213                         case "edit_object":
214                                 if(!argv(2) || !argv(3))
215                                 {
216                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
217                                         return TRUE;
218                                 }
219
220                                 e = sandbox_EditObject_Get();
221                                 if(e != world)
222                                 {
223                                         switch(argv(2))
224                                         {
225                                                 case "skin":
226                                                         e.skin = stof(argv(3));
227                                                         break;
228                                                 case "alpha":
229                                                         e.alpha = stof(argv(3));
230                                                         break;
231                                                 case "color_main":
232                                                         e.colormod = stov(argv(3));
233                                                         break;
234                                                 case "color_glow":
235                                                         e.glowmod = stov(argv(3));
236                                                         break;
237                                                 case "frame":
238                                                         e.frame = stof(argv(3));
239                                                         break;
240                                                 case "scale":
241                                                         sandbox_EditObject_Scale(e, stof(argv(3)));
242                                                         break;
243                                                 case "physics":
244                                                         switch(argv(3))
245                                                         {
246                                                                 case "0": // static
247                                                                         e.movetype = MOVETYPE_NONE;
248                                                                         break;
249                                                                 case "1": // movable
250                                                                         e.movetype = MOVETYPE_TOSS;
251                                                                         break;
252                                                                 case "2": // physical
253                                                                         e.movetype = MOVETYPE_PHYSICS;
254                                                                         break;
255                                                                 default:
256                                                                         break;
257                                                         }
258                                                         break;
259                                                 case "force":
260                                                         e.damageforcescale = stof(argv(3));
261                                                         break;
262                                                 default:
263                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
264                                                         break;
265                                         }
266
267                                         return TRUE;
268                                 }
269
270                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
271                                 return TRUE;
272
273                         // ---------------- COMMAND: DEFAULT ----------------
274                         default:
275                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
276                                 return TRUE;
277                 }
278         }
279         return FALSE;
280 }
281
282 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
283 {
284         // if the player is close enough to their object, they can drag it
285
286         if(autocvar_sv_cheats)
287                 return FALSE; // cheat dragging is used instead
288
289         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
290         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
291         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
292         // it goes out of range while slinging it around.
293
294         entity e;
295         float grab;
296
297         e = sandbox_EditObject_Get();
298         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
299                 grab = TRUE;
300
301         if(Drag(e, grab)) // execute dragging
302         {
303                 if(autocvar_g_sandbox_info)
304                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
305                 return TRUE;
306         }
307
308         return FALSE;
309 }
310
311 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
312 {
313         // unzone the player's clipboard if it's not empty
314         if(self.object_clipboard)
315         {
316                 strunzone(self.object_clipboard);
317                 self.object_clipboard = string_null;
318         }
319
320         return FALSE;
321 }
322
323 MUTATOR_DEFINITION(sandbox)
324 {
325         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
326         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
327         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
328
329         return FALSE;
330 }