]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
Don't allow attaching to attachments, for obvious reasons. Also don't persist the...
[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_ObjectFunction_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_ObjectEdit_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_ObjectEdit_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 void sandbox_ObjectAttach_Remove(entity e);
60 void sandbox_ObjectAttach_Set(entity e, entity parent, string s)
61 {
62         // attaches e to parent on string s
63
64         // we can't attach to an attachment, for obvious reasons
65         sandbox_ObjectAttach_Remove(e);
66
67         e.movetype = MOVETYPE_FOLLOW;
68         e.solid = SOLID_NOT;
69         e.takedamage = DAMAGE_NO;
70
71         setattachment(e, parent, s);
72         e.owner = parent;
73 }
74
75 void sandbox_ObjectAttach_Remove(entity e)
76 {
77         // detaches any object attached to e
78
79         entity head;
80         for(head = world; (head = find(head, classname, "object")); )
81         {
82                 if(head.owner == e)
83                 {
84                         head.movetype = MOVETYPE_TOSS; // default
85                         head.solid = SOLID_BBOX;
86                         head.takedamage = DAMAGE_AIM;
87
88                         setattachment(head, world, "");
89                         head.owner = world;
90
91                         // objects reset origin and angles when detached, so apply the parent's to prevent teleporting
92                         setorigin(head, e.origin);
93                         head.angles = e.angles;
94                 }
95         }
96 }
97
98 entity sandbox_ObjectSpawn(float database)
99 {
100         // spawn a new object with default properties
101
102         entity e;
103         e = spawn();
104         e.classname = "object";
105         e.takedamage = DAMAGE_AIM;
106         e.damageforcescale = 1;
107         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
108         e.movetype = MOVETYPE_TOSS;
109         e.frame = 0;
110         e.skin = 0;
111         e.material = string_null;
112         e.touch = sandbox_ObjectFunction_Touch;
113
114         if(!database)
115         {
116                 // set the object's owner via player UID
117                 // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
118                 if(self.crypto_idfp != "")
119                         e.crypto_idfp = strzone(self.crypto_idfp);
120                 else
121                         print_to(self, "^1SANDBOX - WARNING: ^7You spawned an object, but lack a player UID. ^1Your objects are not secured and can be edited by any player!");
122
123                 // set origin and direction based on player position and view angle
124                 makevectors(self.v_angle);
125                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
126                 setorigin(e, trace_endpos);
127                 e.angles_y = self.v_angle_y;
128         }
129
130         object_count += 1;
131         return e;
132 }
133
134 void sandbox_ObjectRemove(entity e)
135 {
136         sandbox_ObjectAttach_Remove(e); // detach child objects
137         if(e.material)
138         {
139                 strunzone(e.material);
140                 e.material = string_null;
141         }
142         if(e.crypto_idfp)
143         {
144                 strunzone(e.crypto_idfp);
145                 e.crypto_idfp = string_null;
146         }
147         remove(e);
148         e = world;
149
150         object_count -= 1;
151 }
152
153 string sandbox_ObjectPort_Save(entity e, float database)
154 {
155         // save object properties
156         string s;
157
158         s = strcat(e.model, " ");
159         s = strcat(s, ftos(e.skin), " ");
160         s = strcat(s, ftos(e.alpha), " ");
161         s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
162         s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
163         s = strcat(s, ftos(e.frame), " ");
164         s = strcat(s, ftos(e.scale), " ");
165         s = strcat(s, ftos(e.movetype), " ");
166         s = strcat(s, ftos(e.damageforcescale), " ");
167         if(e.material)  s = strcat(s, e.material, " "); else    s = strcat(s, "- "); // none
168         if(database)
169         {
170                 if(e.crypto_idfp)       s = strcat(s, e.crypto_idfp, " ");      else    s = strcat(s, "- "); // none
171                 s = strcat(s, sprintf("\"%.9v\"", e.origin), " ");
172                 s = strcat(s, sprintf("\"%.9v\"", e.angles), " ");
173         }
174
175         return s;
176 }
177
178 void sandbox_ObjectPort_Load(entity e, string s, float database)
179 {
180         // load object properties
181         tokenize_console(s);
182
183         setmodel(e, argv(0));
184         e.skin = stof(argv(1));
185         e.alpha = stof(argv(2));
186         e.colormod = stov(argv(3));
187         e.glowmod = stov(argv(4));
188         e.frame = stof(argv(5));
189         sandbox_ObjectEdit_Scale(e, stof(argv(6)));
190         e.movetype = stof(argv(7));
191         e.damageforcescale = stof(argv(8));
192         if(e.material)  strunzone(e.material);  if(argv(9) != "-")      e.material = strzone(argv(9));  else    e.material = string_null;
193         if(database)
194         {
195                 if(e.crypto_idfp)       strunzone(e.crypto_idfp);       if(argv(10) != "-")     e.crypto_idfp = strzone(argv(10));      else    e.crypto_idfp = string_null;
196                 setorigin(e, stov(argv(11)));
197                 e.angles = stov(argv(12));
198         }
199 }
200
201 void sandbox_Database_Save()
202 {
203         // saves all objects to the database file
204         entity head;
205         string file_name;
206         float file_get;
207
208         file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
209         file_get = fopen(file_name, FILE_WRITE);
210         fputs(file_get, strcat("// sandbox storage \"", autocvar_g_sandbox_storage_name, "\" for map \"", GetMapname(), "\" last updated ", strftime(TRUE, "%d-%m-%Y %H:%M:%S")));
211         fputs(file_get, strcat(" containing ", ftos(object_count), " objects\n"));
212
213         for(head = world; (head = find(head, classname, "object")); )
214         {
215                 // Unfortunately, attached objects cannot be persisted yet. That's because the parent is specified as an entity,
216                 // but only properties can be saved to this file, leaving no way to identify the owner once all objects are spawned
217                 // TODO: Find some way to fix this!
218                 if(head.owner != world)
219                         continue;
220
221                 // use a line for each object, listing all properties
222                 fputs(file_get, strcat(sandbox_ObjectPort_Save(head, TRUE), "\n"));
223         }
224         fclose(file_get);
225 }
226
227 void sandbox_Database_Load()
228 {
229         // loads all objects from the database file
230         string file_read, file_name;
231         float file_get, i;
232
233         file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
234         file_get = fopen(file_name, FILE_READ);
235         if(file_get < 0)
236         {
237                 if(autocvar_g_sandbox_info > 0)
238                         print(strcat("^3SANDBOX - SERVER: ^7could not find storage file ^3", file_name, "^7, no objects were loaded\n"));
239         }
240         else
241         {
242                 for(;;)
243                 {
244                         file_read = fgets(file_get);
245                         if(!file_read)
246                                 break;
247                         if(substring(file_read, 0, 2) == "//")
248                                 continue;
249                         if(substring(file_read, 0, 1) == "#")
250                                 continue;
251
252                         entity e;
253                         e = sandbox_ObjectSpawn(TRUE);
254                         sandbox_ObjectPort_Load(e, file_read, TRUE);
255
256                         if(e.material)
257                         {
258                                 // since objects are being loaded for the first time, precache material sounds for each
259                                 for (i = 1; i <= 5; i++) // 5 sounds in total
260                                         precache_sound(strcat("object/impact_", e.material, "_", ftos(i), ".ogg"));
261                         }
262                 }
263                 if(autocvar_g_sandbox_info > 0)
264                         print(strcat("^3SANDBOX - SERVER: ^7successfully loaded storage file ^3", file_name, "\n"));
265         }
266 }
267
268 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
269 {
270         if(MUTATOR_RETURNVALUE) // command was already handled?
271                 return FALSE;
272         if(cmd_name == "g_sandbox")
273         {
274                 if(cmd_argc < 2)
275                 {
276                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
277                         return TRUE;
278                 }
279
280                 switch(argv(1))
281                 {
282                         entity e;
283                         float i;
284
285                         // ---------------- COMMAND: HELP ----------------
286                         case "help":
287                                 print_to(self, "You can use the following sandbox commands:");
288                                 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");
289                                 print_to(self, "^7\"^2object_remove^7\" removes the object the player is looking at. Players can only remove their own objects");
290                                 print_to(self, "^7\"^2object_duplicate ^3value^7\" duplicates the object. 'copy' copies the object, 'paste' puts it in front of the player");
291                                 print_to(self, "^7\"^2object_claim^7\" sets the player as the owner of the object, if he has the right to edit it");
292                                 print_to(self, "^7\"^2object_attach ^3property value^7\" attaches one object to another. Players can only attach their own objects");
293                                 print_to(self, "^7Attachment properties for ^2object_attach^7:");
294                                 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
295                                 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
296                                 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
297                                 print_to(self, "^7\"^2object_edit ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
298                                 print_to(self, "^7Object properties for ^2object_edit^7:");
299                                 print_to(self, "^3skin value ^7- changes the skin of the object");
300                                 print_to(self, "^3alpha value ^7- sets object transparency");
301                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
302                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
303                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
304                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
305                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
306                                 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
307                                 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
308                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
309                                 return TRUE;
310
311                         // ---------------- COMMAND: SPAWN OBJECT ----------------
312                         case "object_spawn":
313                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
314                                 {
315                                         print_to(self, strcat("^1SANDBOX - WARNING: ^7Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
316                                         return TRUE;
317                                 }
318                                 if(cmd_argc < 3)
319                                 {
320                                         print_to(self, "^1SANDBOX - WARNING: ^7Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'object_spawn' command");
321                                         return TRUE;
322                                 }
323                                 else if not(fexists(argv(2)))
324                                 {
325                                         print_to(self, "^1SANDBOX - WARNING: ^7Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
326                                         return TRUE;
327                                 }
328
329                                 e = sandbox_ObjectSpawn(FALSE);
330                                 setmodel(e, argv(2));
331
332                                 if(autocvar_g_sandbox_info > 0)
333                                         print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " spawned an object at origin ^3", vtos(e.origin), "\n"));
334                                 return TRUE;
335
336                         // ---------------- COMMAND: REMOVE OBJECT ----------------
337                         case "object_remove":
338                                 e = sandbox_ObjectEdit_Get();
339                                 if(e != world)
340                                 {
341                                         if(autocvar_g_sandbox_info > 0)
342                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " removed an object at origin ^3", vtos(e.origin), "\n"));
343                                         sandbox_ObjectRemove(e);
344                                         return TRUE;
345                                 }
346
347                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be removed. Make sure you are facing an object that you have edit rights over");
348                                 return TRUE;
349
350                         // ---------------- COMMAND: DUPLICATE OBJECT ----------------
351                         case "object_duplicate":
352                                 switch(argv(2))
353                                 {
354                                         case "copy":
355                                                 // copies customizable properties of the selected object to the clipboard
356                                                 e = sandbox_ObjectEdit_Get(); // you can only copy objects you can edit, so this works
357                                                 if(e != world)
358                                                 {
359                                                         if(self.object_clipboard)
360                                                                 strunzone(self.object_clipboard);
361                                                         self.object_clipboard = strzone(sandbox_ObjectPort_Save(e, FALSE));
362
363                                                         print_to(self, "^2SANDBOX - INFO: ^7Object copied to clipboard");
364                                                         return TRUE;
365                                                 }
366                                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be copied. Make sure you are facing an object that you have edit rights over");
367                                                 return TRUE;
368
369                                         case "paste":
370                                                 // spawns a new object using the properties in the player's clipboard
371                                                 if(!self.object_clipboard) // no object in clipboard
372                                                 {
373                                                         print_to(self, "^1SANDBOX - WARNING: ^7No object in clipboard. You must copy an object before you can paste it");
374                                                         return TRUE;
375                                                 }
376                                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
377                                                 {
378                                                         print_to(self, strcat("^1SANDBOX - WARNING: ^7Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
379                                                         return TRUE;
380                                                 }
381
382                                                 e = sandbox_ObjectSpawn(FALSE);
383                                                 sandbox_ObjectPort_Load(e, self.object_clipboard, FALSE);
384
385                                                 print_to(self, "^2SANDBOX - INFO: ^7Object pasted successfully");
386                                                 if(autocvar_g_sandbox_info > 0)
387                                                         print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " pasted an object at origin ^3", vtos(e.origin), "\n"));
388                                                 return TRUE;
389                                 }
390                                 return TRUE;
391
392                         // ---------------- COMMAND: CLAIM OBJECT ----------------
393                         case "object_claim":
394                                 // if the player can edit an object but is not its owner, this can be used to claim that object
395                                 if(self.crypto_idfp == "")
396                                 {
397                                         print_to(self, "^1SANDBOX - WARNING: ^7You do not have a player UID, and cannot claim objects");
398                                         return TRUE;
399                                 }
400                                 e = sandbox_ObjectEdit_Get();
401                                 if(e != world)
402                                 {
403                                         if(e.crypto_idfp == self.crypto_idfp)
404                                         {
405                                                 print_to(self, "^2SANDBOX - INFO: ^7Object is already yours, nothing to claim");
406                                                 return TRUE;
407                                         }
408
409                                         if(e.crypto_idfp)
410                                                 strunzone(e.crypto_idfp);
411                                         e.crypto_idfp = self.crypto_idfp;
412                                         print_to(self, "^2SANDBOX - INFO: ^7Object claimed successfully");
413                                 }
414                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be claimed. Make sure you are facing an object that you have edit rights over");
415                                 return TRUE;
416
417                         // ---------------- COMMAND: ATTACH OBJECT ----------------
418                         case "object_attach":
419                                 switch(argv(2))
420                                 {
421                                         case "get":
422                                                 // select e as the object as meant to be attached
423                                                 e = sandbox_ObjectEdit_Get();
424                                                 if(e != world)
425                                                 {
426                                                         self.object_attach = e;
427                                                         print_to(self, "^2SANDBOX - INFO: ^7Object selected for attachment");
428                                                         return TRUE;
429                                                 }
430                                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be selected for attachment. Make sure you are facing an object that you have edit rights over");
431                                                 return TRUE;
432                                         case "set":
433                                                 if(self.object_attach == world)
434                                                 {
435                                                         print_to(self, "^1SANDBOX - WARNING: ^7No object selected for attachment. Please select an object to be attached first.");
436                                                         return TRUE;
437                                                 }
438
439                                                 // attaches the previously selected object to e
440                                                 e = sandbox_ObjectEdit_Get();
441                                                 if(e != world)
442                                                 {
443                                                         sandbox_ObjectAttach_Set(self.object_attach, e, argv(3));
444                                                         self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
445                                                         print_to(self, "^2SANDBOX - INFO: ^7Object attached successfully");
446                                                         if(autocvar_g_sandbox_info > 1)
447                                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " attached objects at origin ^3", vtos(e.origin), "\n"));
448                                                         return TRUE;
449                                                 }
450                                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be attached to the parent. Make sure you are facing an object that you have edit rights over");
451                                                 return TRUE;
452                                         case "remove":
453                                                 // removes e if it was attached
454                                                 e = sandbox_ObjectEdit_Get();
455                                                 if(e != world)
456                                                 {
457                                                         sandbox_ObjectAttach_Remove(e);
458                                                         print_to(self, "^2SANDBOX - INFO: ^7Child objects detached successfully");
459                                                         if(autocvar_g_sandbox_info > 1)
460                                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " detached objects at origin ^3", vtos(e.origin), "\n"));
461                                                         return TRUE;
462                                                 }
463                                                 print_to(self, "^1SANDBOX - WARNING: ^7Child objects could not be detached. Make sure you are facing an object that you have edit rights over");
464                                                 return TRUE;
465                                 }
466                                 return TRUE;
467
468                         // ---------------- COMMAND: EDIT OBJECT ----------------
469                         case "object_edit":
470                                 if(!argv(2))
471                                 {
472                                         print_to(self, "^1SANDBOX - WARNING: ^7Too few parameters. You must specify a property to edit");
473                                         return TRUE;
474                                 }
475
476                                 e = sandbox_ObjectEdit_Get();
477                                 if(e != world)
478                                 {
479                                         switch(argv(2))
480                                         {
481                                                 case "skin":
482                                                         e.skin = stof(argv(3));
483                                                         break;
484                                                 case "alpha":
485                                                         e.alpha = stof(argv(3));
486                                                         break;
487                                                 case "color_main":
488                                                         e.colormod = stov(argv(3));
489                                                         break;
490                                                 case "color_glow":
491                                                         e.glowmod = stov(argv(3));
492                                                         break;
493                                                 case "frame":
494                                                         e.frame = stof(argv(3));
495                                                         break;
496                                                 case "scale":
497                                                         sandbox_ObjectEdit_Scale(e, stof(argv(3)));
498                                                         break;
499                                                 case "physics":
500                                                         switch(argv(3))
501                                                         {
502                                                                 case "0": // static
503                                                                         e.movetype = MOVETYPE_NONE;
504                                                                         break;
505                                                                 case "1": // movable
506                                                                         e.movetype = MOVETYPE_TOSS;
507                                                                         break;
508                                                                 case "2": // physical
509                                                                         e.movetype = MOVETYPE_PHYSICS;
510                                                                         break;
511                                                                 default:
512                                                                         break;
513                                                         }
514                                                         break;
515                                                 case "force":
516                                                         e.damageforcescale = stof(argv(3));
517                                                         break;
518                                                 case "material":
519                                                         if(e.material)
520                                                                 strunzone(e.material);
521                                                         if(argv(3))
522                                                         {
523                                                                 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
524                                                                         precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
525                                                                 e.material = strzone(argv(3));
526                                                         }
527                                                         else
528                                                                 e.material = string_null; // no material
529                                                         break;
530                                                 default:
531                                                         print_to(self, "^1SANDBOX - WARNING: ^7Invalid object property. For usage information, type 'sandbox help'");
532                                                         break;
533                                         }
534                                         if(autocvar_g_sandbox_info > 1)
535                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " edited property ^3", argv(2), " ^7of an object at origin ^3", vtos(e.origin), "\n"));
536                                         return TRUE;
537                                 }
538
539                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be edited. Make sure you are facing an object that you have edit rights over");
540                                 return TRUE;
541
542                         // ---------------- COMMAND: DEFAULT ----------------
543                         default:
544                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
545                                 return TRUE;
546                 }
547         }
548         return FALSE;
549 }
550
551 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
552 {
553         // if the player is close enough to their object, they can drag it
554
555         if(autocvar_sv_cheats)
556                 return FALSE; // cheat dragging is used instead
557
558         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
559         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
560         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
561         // it goes out of range while slinging it around.
562
563         entity e;
564         float grab;
565
566         e = sandbox_ObjectEdit_Get();
567         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
568                 grab = TRUE;
569
570         if(Drag(e, grab)) // execute dragging
571                 return TRUE;
572
573         return FALSE;
574 }
575
576 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
577 {
578         // unzone the player's clipboard if it's not empty
579         if(self.object_clipboard)
580         {
581                 strunzone(self.object_clipboard);
582                 self.object_clipboard = string_null;
583         }
584
585         return FALSE;
586 }
587
588 float autosave_time;
589 MUTATOR_HOOKFUNCTION(sandbox_StartFrame)
590 {
591         if(!autocvar_g_sandbox_storage_autosave)
592                 return FALSE;
593         if(time < autocvar_g_sandbox_storage_autosave)
594                 return FALSE;
595         autosave_time = time + autocvar_g_sandbox_storage_autosave;
596
597         sandbox_Database_Save();
598
599         return TRUE;
600 }
601
602 MUTATOR_DEFINITION(sandbox)
603 {
604         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
605         MUTATOR_HOOK(SV_StartFrame, sandbox_StartFrame, CBC_ORDER_ANY);
606         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
607         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
608
609         MUTATOR_ONADD
610         {
611                 autosave_time = time + autocvar_g_sandbox_storage_autosave; // don't save the first server frame
612                 if(autocvar_g_sandbox_storage_autoload)
613                         sandbox_Database_Load();
614         }
615
616         return FALSE;
617 }
618