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