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