]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
If removing an object that contains attachments, detach child objects first
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 float object_count;
2 .string object_clipboard;
3 .entity object_attach;
4 .string material;
5
6 .float touch_timer;
7 void sandbox_Object_Touch()
8 {
9         // apply material impact effects
10
11         if(!self.material)
12                 return;
13         if(self.touch_timer > time)
14                 return; // don't execute each frame
15         self.touch_timer = time + 0.1;
16
17         // make particle count and sound volume depend on impact speed
18         float intensity;
19         intensity = vlen(self.velocity) + vlen(other.velocity);
20         if(intensity) // avoid divisions by 0
21                 intensity /= 2; // average the two velocities
22         if not(intensity >= autocvar_g_sandbox_object_material_velocity_min)
23                 return; // impact not strong enough to do anything
24         // now offset intensity and apply it to the effects
25         intensity -= autocvar_g_sandbox_object_material_velocity_min; // start from minimum velocity, not actual velocity
26         intensity = bound(0, intensity * autocvar_g_sandbox_object_material_velocity_factor, 1);
27
28         sound(self, CH_TRIGGER, strcat("object/impact_", self.material, "_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE * intensity, ATTN_NORM);
29         pointparticles(particleeffectnum(strcat("impact_", self.material)), self.origin, '0 0 0', ceil(intensity * 10)); // allow a count from 1 to 10
30 }
31
32 entity sandbox_EditObject_Get()
33 {
34         // returns the traced entity if the player can edit it, and world if not
35         // attached objects are SOLID_NOT and don't risk getting traced
36
37         makevectors(self.v_angle);
38         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
39
40         if(trace_ent.classname != "object")
41                 return world; // entity is not an object
42         if(!trace_ent.crypto_idfp)
43                 return trace_ent; // the player who spawned this object did not have an UID, so anyone can edit it
44         else if not(trace_ent.crypto_idfp != self.crypto_idfp && !autocvar_g_sandbox_editor_free)
45                 return trace_ent; // object does not belong to the player, and players can only edit their own objects on this server
46         return world;
47 }
48
49 void sandbox_EditObject_Scale(entity e, float f)
50 {
51         e.scale = f;
52         if(e.scale)
53         {
54                 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
55                 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
56         }
57 }
58
59 .float old_movetype;
60 void sandbox_AttachObject_Set(entity e, entity parent, string s)
61 {
62         // attaches e to parent on string s
63
64         e.old_movetype = e.movetype; // persist this
65
66         e.movetype = MOVETYPE_FOLLOW;
67         e.solid = SOLID_NOT;
68         e.takedamage = DAMAGE_NO;
69
70         setattachment(e, parent, s);
71         e.owner = parent;
72 }
73
74 void sandbox_AttachObject_Remove(entity e)
75 {
76         // detaches any object attached to e
77
78         entity head;
79         for(head = world; (head = find(head, classname, "object")); )
80         {
81                 if(head.owner == e)
82                 {
83                         head.movetype = head.old_movetype; // revert to previous physics
84                         head.solid = SOLID_BBOX;
85                         head.takedamage = DAMAGE_AIM;
86
87                         setattachment(head, world, "");
88                         head.owner = world;
89
90                         // objects reset origin and angles when detached, so apply the parent's to prevent teleporting
91                         setorigin(head, e.origin);
92                         head.angles = e.angles;
93                 }
94         }
95 }
96
97 entity sandbox_SpawnObject()
98 {
99         // spawn a new object with default properties
100
101         entity e;
102         e = spawn();
103         e.classname = "object";
104         e.takedamage = DAMAGE_AIM;
105         e.damageforcescale = 1;
106         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
107         e.movetype = MOVETYPE_TOSS;
108         e.frame = 0;
109         e.skin = 0;
110         e.material = string_null;
111         e.touch = sandbox_Object_Touch;
112
113         // set the object's owner via player UID
114         // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
115         if(self.crypto_idfp != "")
116                 e.crypto_idfp = strzone(self.crypto_idfp);
117         else
118                 print_to(self, "WARNING: You spawned an object, but lack a player UID. ^1Your objects are not secured and can be edited by any player!");
119
120         // set origin and direction based on player position and view angle
121         makevectors(self.v_angle);
122         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
123         setorigin(e, trace_endpos);
124         e.angles_y = self.v_angle_y;
125
126         object_count += 1;
127         return e;
128 }
129
130 void sandbox_RemoveObject(entity e)
131 {
132         sandbox_AttachObject_Remove(e); // detach child objects
133         if(e.material)
134         {
135                 strunzone(e.material);
136                 e.material = string_null;
137         }
138         if(e.crypto_idfp)
139         {
140                 strunzone(e.crypto_idfp);
141                 e.crypto_idfp = string_null;
142         }
143         remove(e);
144         e = world;
145
146         object_count -= 1;
147 }
148
149 string sandbox_Storage_Save(entity e)
150 {
151         // save object properties
152         string s;
153
154         s = strcat(e.model, " ");
155         s = strcat(s, ftos(e.skin), " ");
156         s = strcat(s, ftos(e.alpha), " ");
157         s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
158         s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
159         s = strcat(s, ftos(e.frame), " ");
160         s = strcat(s, ftos(e.scale), " ");
161         s = strcat(s, ftos(e.movetype), " ");
162         s = strcat(s, ftos(e.damageforcescale), " ");
163         s = strcat(s, e.material, " ");
164
165         return s;
166 }
167
168 void sandbox_Storage_Load(entity e, string s)
169 {
170         // load object properties
171         tokenize_console(s);
172
173         setmodel(e, argv(0));
174         e.skin = stof(argv(1));
175         e.alpha = stof(argv(2));
176         e.colormod = stov(argv(3));
177         e.glowmod = stov(argv(4));
178         e.frame = stof(argv(5));
179         sandbox_EditObject_Scale(e, stof(argv(6)));
180         e.movetype = stof(argv(7));
181         e.damageforcescale = stof(argv(8));
182         if(e.material)  strunzone(e.material);  if(argv(9))     e.material = strzone(argv(9));  else    e.material = string_null;
183 }
184
185 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
186 {
187         if(MUTATOR_RETURNVALUE) // command was already handled?
188                 return FALSE;
189         if(cmd_name == "g_sandbox")
190         {
191                 if(cmd_argc < 2)
192                 {
193                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
194                         return TRUE;
195                 }
196
197                 switch(argv(1))
198                 {
199                         entity e;
200                         float i;
201
202                         // ---------------- COMMAND: HELP ----------------
203                         case "help":
204                                 print_to(self, "You can use the following sandbox commands:");
205                                 print_to(self, "^7\"^2item_spawn ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
206                                 print_to(self, "^7\"^2object_spawn ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
207                                 print_to(self, "^7\"^2object_remove^7\" removes the object the player is looking at. Players can only remove their own objects");
208                                 print_to(self, "^7\"^2object_duplicate ^3value^7\" duplicates the object. 'copy' copies the object, 'paste' puts it in front of the player");
209                                 print_to(self, "^7\"^2object_claim^7\" sets the player as the owner of the object, if he has the right to edit it");
210                                 print_to(self, "^7\"^2object_attach ^3property value^7\" attaches one object to another. Players can only attach their own objects");
211                                 print_to(self, "^7Attachment properties for ^2object_attach^7:");
212                                 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
213                                 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
214                                 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
215                                 print_to(self, "^7\"^2object_edit ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
216                                 print_to(self, "^7Object properties for ^2object_edit^7:");
217                                 print_to(self, "^3skin value ^7- changes the skin of the object");
218                                 print_to(self, "^3alpha value ^7- sets object transparency");
219                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
220                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
221                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
222                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
223                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
224                                 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
225                                 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
226                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
227                                 return TRUE;
228
229                         // ---------------- COMMAND: SPAWN ITEM ----------------
230                         case "item_spawn":
231                                 // only weapons are currently supported
232
233                                 if(cmd_argc < 3)
234                                 {
235                                         print_to(self, "WARNING: Attempted to spawn an item without specifying its type. Please specify the name of your item after the 'item_spawn' command");
236                                         return TRUE;
237                                 }
238
239                                 // spawn a new item
240                                 makevectors(self.v_angle);
241                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
242
243                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
244                                 {
245                                         e = get_weaponinfo(i);
246                                         if(e.netname == argv(2))
247                                         {
248                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
249                                                 if(autocvar_g_sandbox_info)
250                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
251                                                 return TRUE;
252                                         }
253                                 }
254
255                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
256                                 return TRUE;
257
258                         // ---------------- COMMAND: SPAWN OBJECT ----------------
259                         case "object_spawn":
260                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
261                                 {
262                                         print_to(self, strcat("WARNING: Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
263                                         return TRUE;
264                                 }
265                                 if(cmd_argc < 3)
266                                 {
267                                         print_to(self, "WARNING: Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'object_spawn' command");
268                                         return TRUE;
269                                 }
270                                 else if not(fexists(argv(2)))
271                                 {
272                                         print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
273                                         return TRUE;
274                                 }
275
276                                 e = sandbox_SpawnObject();
277                                 setmodel(e, argv(2));
278
279                                 if(autocvar_g_sandbox_info)
280                                         print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
281
282                                 return TRUE;
283
284                         // ---------------- COMMAND: REMOVE OBJECT ----------------
285                         case "object_remove":
286                                 e = sandbox_EditObject_Get();
287                                 if(e != world)
288                                 {
289                                         if(autocvar_g_sandbox_info)
290                                                 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
291                                         sandbox_RemoveObject(e);
292                                         return TRUE;
293                                 }
294
295                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you (default)");
296                                 return TRUE;
297
298                         // ---------------- COMMAND: DUPLICATE OBJECT ----------------
299                         case "object_duplicate":
300                                 switch(argv(2))
301                                 {
302                                         case "copy":
303                                                 // copies customizable properties of the selected object to the clipboard
304                                                 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
305                                                 if(e != world)
306                                                 {
307                                                         if(self.object_clipboard)
308                                                                 strunzone(self.object_clipboard);
309                                                         self.object_clipboard = strzone(sandbox_Storage_Save(e));
310
311                                                         print_to(self, "Object copied to clipboard");
312                                                         return TRUE;
313                                                 }
314                                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you (default)");
315                                                 return TRUE;
316
317                                         case "paste":
318                                                 // spawns a new object using the properties in the player's clipboard
319                                                 if(!self.object_clipboard) // no object in clipboard
320                                                 {
321                                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
322                                                         return TRUE;
323                                                 }
324                                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
325                                                 {
326                                                         print_to(self, strcat("WARNING: Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
327                                                         return TRUE;
328                                                 }
329
330                                                 e = sandbox_SpawnObject();
331                                                 sandbox_Storage_Load(e, self.object_clipboard);
332
333                                                 print_to(self, "Object pasted successfully");
334                                                 if(autocvar_g_sandbox_info)
335                                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
336                                                 return TRUE;
337                                 }
338                                 return TRUE;
339
340                         // ---------------- COMMAND: CLAIM OBJECT ----------------
341                         case "object_claim":
342                                 if(self.crypto_idfp == "")
343                                 {
344                                         print_to(self, "WARNING: You do not have a player UID, and cannot claim objects");
345                                         return TRUE;
346                                 }
347                                 e = sandbox_EditObject_Get();
348                                 if(e != world)
349                                 {
350                                         if(e.crypto_idfp == self.crypto_idfp)
351                                         {
352                                                 print_to(self, "Object is already yours, nothing to claim");
353                                                 return TRUE;
354                                         }
355
356                                         if(e.crypto_idfp)
357                                                 strunzone(e.crypto_idfp);
358                                         e.crypto_idfp = self.crypto_idfp;
359                                         print_to(self, "Object claimed successfully");
360                                 }
361                                 print_to(self, "WARNING: Object could not be claimed. Make sure you are facing an object that belongs to you (default)");
362                                 return TRUE;
363
364                         // ---------------- COMMAND: ATTACH OBJECT ----------------
365                         case "object_attach":
366                                 switch(argv(2))
367                                 {
368                                         case "get":
369                                                 // select e as the object as meant to be attached
370                                                 e = sandbox_EditObject_Get();
371                                                 if(e != world)
372                                                 {
373                                                         self.object_attach = e;
374                                                         print_to(self, "Object selected for attachment");
375                                                         return TRUE;
376                                                 }
377                                                 print_to(self, "WARNING: Object could not be selected for attachment. Make sure you are facing an object that belongs to you (default)");
378                                                 return TRUE;
379                                         case "set":
380                                                 if(self.object_attach == world)
381                                                 {
382                                                         print_to(self, "WARNING: No object selected for attachment. Please select an object to be attached first.");
383                                                         return TRUE;
384                                                 }
385
386                                                 // attaches the previously selected object to e
387                                                 e = sandbox_EditObject_Get();
388                                                 if(e != world)
389                                                 {
390                                                         sandbox_AttachObject_Set(self.object_attach, e, argv(3));
391                                                         print_to(self, "Object attached successfully");
392                                                         self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
393                                                         return TRUE;
394                                                 }
395                                                 print_to(self, "WARNING: Object could not be attached to the parent. Make sure you are facing an object that belongs to you (default)");
396                                                 return TRUE;
397                                         case "remove":
398                                                 // removes e if it was attached
399                                                 e = sandbox_EditObject_Get();
400                                                 if(e != world)
401                                                 {
402                                                         sandbox_AttachObject_Remove(e);
403                                                         print_to(self, "Child objects detached successfully");
404                                                         return TRUE;
405                                                 }
406                                                 print_to(self, "WARNING: Child objects could not be detached. Make sure you are facing an object that belongs to you (default)");
407                                                 return TRUE;
408                                 }
409                                 return TRUE;
410
411                         // ---------------- COMMAND: EDIT OBJECT ----------------
412                         case "object_edit":
413                                 if(!argv(2))
414                                 {
415                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit");
416                                         return TRUE;
417                                 }
418
419                                 e = sandbox_EditObject_Get();
420                                 if(e != world)
421                                 {
422                                         switch(argv(2))
423                                         {
424                                                 case "skin":
425                                                         e.skin = stof(argv(3));
426                                                         break;
427                                                 case "alpha":
428                                                         e.alpha = stof(argv(3));
429                                                         break;
430                                                 case "color_main":
431                                                         e.colormod = stov(argv(3));
432                                                         break;
433                                                 case "color_glow":
434                                                         e.glowmod = stov(argv(3));
435                                                         break;
436                                                 case "frame":
437                                                         e.frame = stof(argv(3));
438                                                         break;
439                                                 case "scale":
440                                                         sandbox_EditObject_Scale(e, stof(argv(3)));
441                                                         break;
442                                                 case "physics":
443                                                         switch(argv(3))
444                                                         {
445                                                                 case "0": // static
446                                                                         e.movetype = MOVETYPE_NONE;
447                                                                         break;
448                                                                 case "1": // movable
449                                                                         e.movetype = MOVETYPE_TOSS;
450                                                                         break;
451                                                                 case "2": // physical
452                                                                         e.movetype = MOVETYPE_PHYSICS;
453                                                                         break;
454                                                                 default:
455                                                                         break;
456                                                         }
457                                                         break;
458                                                 case "force":
459                                                         e.damageforcescale = stof(argv(3));
460                                                         break;
461                                                 case "material":
462                                                         if(e.material)
463                                                                 strunzone(e.material);
464                                                         if(argv(3))
465                                                         {
466                                                                 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
467                                                                         precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
468                                                                 e.material = strzone(argv(3));
469                                                         }
470                                                         else
471                                                                 e.material = string_null; // no material
472                                                         break;
473                                                 default:
474                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
475                                                         break;
476                                         }
477                                         return TRUE;
478                                 }
479
480                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you (default)");
481                                 return TRUE;
482
483                         // ---------------- COMMAND: DEFAULT ----------------
484                         default:
485                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
486                                 return TRUE;
487                 }
488         }
489         return FALSE;
490 }
491
492 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
493 {
494         // if the player is close enough to their object, they can drag it
495
496         if(autocvar_sv_cheats)
497                 return FALSE; // cheat dragging is used instead
498
499         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
500         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
501         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
502         // it goes out of range while slinging it around.
503
504         entity e;
505         float grab;
506
507         e = sandbox_EditObject_Get();
508         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
509                 grab = TRUE;
510
511         if(Drag(e, grab)) // execute dragging
512         {
513                 if(autocvar_g_sandbox_info)
514                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
515                 return TRUE;
516         }
517
518         return FALSE;
519 }
520
521 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
522 {
523         // unzone the player's clipboard if it's not empty
524         if(self.object_clipboard)
525         {
526                 strunzone(self.object_clipboard);
527                 self.object_clipboard = string_null;
528         }
529
530         return FALSE;
531 }
532
533 MUTATOR_DEFINITION(sandbox)
534 {
535         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
536         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
537         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
538
539         return FALSE;
540 }
541