]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
Move non-cheat grabbing back into cheatfrae, so all code is in one place. Also remove...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 const float MAX_STORAGE_ATTACHMENTS = 16;
2 float object_count;
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 void sandbox_ObjectFunction_Think()
33 {
34         if(autocvar_g_sandbox_editor_free < 2 && self.crypto_idfp)
35                 self.grab = 1;
36         else
37                 self.grab = 3;
38
39         self.nextthink = time;
40 }
41
42 entity sandbox_ObjectEdit_Get(float permissions)
43 {
44         // returns the traced entity if the player can edit it, and world if not
45         // if permissions if FALSE, the object is returned regardless of editing rights
46         // attached objects are SOLID_NOT and don't risk getting traced
47
48         makevectors(self.v_angle);
49         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
50
51         if(trace_ent.classname != "object")
52                 return world; // entity is not an object
53         if(!permissions)
54                 return trace_ent; // don't check permissions, anyone can edit this object
55         if(!trace_ent.crypto_idfp)
56                 return trace_ent; // the player who spawned this object did not have an UID, so anyone can edit it
57         if not(trace_ent.crypto_idfp != self.crypto_idfp && autocvar_g_sandbox_editor_free < 2)
58                 return trace_ent; // object does not belong to the player, and players can only edit their own objects on this server
59         return world;
60 }
61
62 void sandbox_ObjectEdit_Scale(entity e, float f)
63 {
64         e.scale = f;
65         if(e.scale)
66         {
67                 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
68                 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
69         }
70 }
71
72 .float old_movetype;
73 void sandbox_ObjectAttach_Remove(entity e);
74 void sandbox_ObjectAttach_Set(entity e, entity parent, string s)
75 {
76         // attaches e to parent on string s
77
78         // we can't attach to an attachment, for obvious reasons
79         sandbox_ObjectAttach_Remove(e);
80
81         e.old_movetype = e.movetype; // persist physics
82         e.movetype = MOVETYPE_FOLLOW;
83         e.solid = SOLID_NOT;
84         e.takedamage = DAMAGE_NO;
85
86         setattachment(e, parent, s);
87         e.owner = parent;
88 }
89
90 void sandbox_ObjectAttach_Remove(entity e)
91 {
92         // detaches any object attached to e
93
94         entity head;
95         for(head = world; (head = find(head, classname, "object")); )
96         {
97                 if(head.owner == e)
98                 {
99                         vector org;
100                         head.movetype = head.old_movetype; // restore persisted physics
101                         head.solid = SOLID_BBOX;
102                         head.takedamage = DAMAGE_AIM;
103
104                         org = gettaginfo(head, 0);
105                         setattachment(head, world, "");
106                         head.owner = world;
107
108                         // objects change origin and angles when detached, so apply previous position
109                         setorigin(head, org);
110                         head.angles = e.angles; // don't allow detached objects to spin or roll
111                 }
112         }
113 }
114
115 entity sandbox_ObjectSpawn(float database)
116 {
117         // spawn a new object with default properties
118
119         entity e;
120         e = spawn();
121         e.classname = "object";
122         e.takedamage = DAMAGE_AIM;
123         e.damageforcescale = 1;
124         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
125         e.movetype = MOVETYPE_TOSS;
126         e.frame = 0;
127         e.skin = 0;
128         e.material = string_null;
129         e.touch = sandbox_ObjectFunction_Touch;
130         e.think = sandbox_ObjectFunction_Think;
131         e.nextthink = time;
132         //e.effects |= EF_SELECTABLE; // don't do this all the time, maybe just when editing objects?
133
134         if(!database)
135         {
136                 // set the object's owner via player UID
137                 // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
138                 if(self.crypto_idfp != "")
139                         e.crypto_idfp = strzone(self.crypto_idfp);
140                 else
141                         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!");
142
143                 // set public object information
144                 e.netname = strzone(self.netname); // name of the owner
145                 e.message = strzone(strftime(TRUE, "%d-%m-%Y %H:%M:%S")); // creation time
146                 e.message2 = strzone(strftime(TRUE, "%d-%m-%Y %H:%M:%S")); // last editing time
147
148                 // set origin and direction based on player position and view angle
149                 makevectors(self.v_angle);
150                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
151                 setorigin(e, trace_endpos);
152                 e.angles_y = self.v_angle_y;
153         }
154
155         object_count += 1;
156         return e;
157 }
158
159 void sandbox_ObjectRemove(entity e)
160 {
161         sandbox_ObjectAttach_Remove(e); // detach child objects
162
163         if(e.material)  {       strunzone(e.material);  e.material = string_null;       }
164         if(e.crypto_idfp)       {       strunzone(e.crypto_idfp);       e.crypto_idfp = string_null;    }
165         if(e.netname)   {       strunzone(e.netname);   e.netname = string_null;        }
166         if(e.message)   {       strunzone(e.message);   e.message = string_null;        }
167         if(e.message2)  {       strunzone(e.message2);  e.message2 = string_null;       }
168         remove(e);
169         e = world;
170
171         object_count -= 1;
172 }
173
174 string port_string[MAX_STORAGE_ATTACHMENTS]; // fteqcc crashes if this isn't defined as a global
175
176 string sandbox_ObjectPort_Save(entity e, float database)
177 {
178         // save object properties, and return them as a string
179         float i;
180         string s;
181         entity head;
182
183         for(head = world; (head = find(head, classname, "object")); )
184         {
185                 // the main object needs to be first in the array [0] with attached objects following
186                 float slot, physics;
187                 if(head == e) // this is the main object, place it first
188                 {
189                         slot = 0;
190                         physics = head.movetype; // applied physics are normal physics for parents
191                 }
192                 else if(head.owner == e) // child object, list them in order
193                 {
194                         i += 1; // children start from 1
195                         slot = i;
196                         physics = head.old_movetype; // persisted physics are normal physics for children
197                         gettaginfo(head.owner, head.tag_index); // get the name of the tag our object is attached to, used further below
198                 }
199                 else
200                         continue;
201
202                 // ---------------- OBJECT PROPERTY STORAGE: SAVE ----------------
203                 if(slot)
204                 {
205                         // properties stored only for child objects
206                         if(gettaginfo_name)     port_string[slot] = strcat(port_string[slot], "\"", gettaginfo_name, "\" ");    else    port_string[slot] = strcat(port_string[slot], "- "); // none
207                 }
208                 else
209                 {
210                         // properties stored only for parent objects
211                         if(database)
212                         {
213                                 port_string[slot] = strcat(port_string[slot], sprintf("\"%.9v\"", head.origin), " ");
214                                 port_string[slot] = strcat(port_string[slot], sprintf("\"%.9v\"", head.angles), " ");
215                         }
216                 }
217                 // properties stored for all objects
218                 port_string[slot] = strcat(port_string[slot], "\"", head.model, "\" ");
219                 port_string[slot] = strcat(port_string[slot], ftos(head.skin), " ");
220                 port_string[slot] = strcat(port_string[slot], ftos(head.alpha), " ");
221                 port_string[slot] = strcat(port_string[slot], sprintf("\"%.9v\"", head.colormod), " ");
222                 port_string[slot] = strcat(port_string[slot], sprintf("\"%.9v\"", head.glowmod), " ");
223                 port_string[slot] = strcat(port_string[slot], ftos(head.frame), " ");
224                 port_string[slot] = strcat(port_string[slot], ftos(head.scale), " ");
225                 port_string[slot] = strcat(port_string[slot], ftos(physics), " ");
226                 port_string[slot] = strcat(port_string[slot], ftos(head.damageforcescale), " ");
227                 if(head.material)       port_string[slot] = strcat(port_string[slot], "\"", head.material, "\" ");      else    port_string[slot] = strcat(port_string[slot], "- "); // none
228                 if(database)
229                 {
230                         // properties stored only for the database
231                         if(head.crypto_idfp)    port_string[slot] = strcat(port_string[slot], "\"", head.crypto_idfp, "\" ");   else    port_string[slot] = strcat(port_string[slot], "- "); // none
232                         port_string[slot] = strcat(port_string[slot], "\"", e.netname, "\" ");
233                         port_string[slot] = strcat(port_string[slot], "\"", e.message, "\" ");
234                         port_string[slot] = strcat(port_string[slot], "\"", e.message2, "\" ");
235                 }
236         }
237
238         // now apply the array to a simple string, with the ; symbol separating objects
239         for(i = 0; i <= MAX_STORAGE_ATTACHMENTS; ++i)
240         {
241                 if(port_string[i])
242                         s = strcat(s, port_string[i], "; ");
243                 port_string[i] = string_null; // fully clear the string
244         }
245
246         return s;
247 }
248
249 entity sandbox_ObjectPort_Load(string s, float database)
250 {
251         // load object properties, and spawn a new object with them
252         float n, i;
253         entity e, parent;
254
255         // separate objects between the ; symbols
256         n = tokenizebyseparator(s, "; ");
257         for(i = 0; i < n; ++i)
258                 port_string[i] = argv(i);
259
260         // now separate and apply the properties of each object
261         for(i = 0; i < n; ++i)
262         {
263                 float argv_num;
264                 string tagname;
265                 argv_num = 0;
266                 tokenize_console(port_string[i]);
267                 e = sandbox_ObjectSpawn(database);
268
269                 // ---------------- OBJECT PROPERTY STORAGE: LOAD ----------------
270                 if(i)
271                 {
272                         // properties stored only for child objects
273                         if(argv(argv_num) != "-")       tagname = argv(argv_num);       else tagname = string_null;     ++argv_num;
274                 }
275                 else
276                 {
277                         // properties stored only for parent objects
278                         if(database)
279                         {
280                                 setorigin(e, stov(argv(argv_num)));     ++argv_num;
281                                 e.angles = stov(argv(argv_num));        ++argv_num;
282                         }
283                         parent = e; // mark parent objects as such
284                 }
285                 // properties stored for all objects
286                 setmodel(e, argv(argv_num));    ++argv_num;
287                 e.skin = stof(argv(argv_num));  ++argv_num;
288                 e.alpha = stof(argv(argv_num)); ++argv_num;
289                 e.colormod = stov(argv(argv_num));      ++argv_num;
290                 e.glowmod = stov(argv(argv_num));       ++argv_num;
291                 e.frame = stof(argv(argv_num)); ++argv_num;
292                 sandbox_ObjectEdit_Scale(e, stof(argv(argv_num)));      ++argv_num;
293                 e.movetype = e.old_movetype = stof(argv(argv_num));     ++argv_num;
294                 e.damageforcescale = stof(argv(argv_num));      ++argv_num;
295                 if(e.material)  strunzone(e.material);  if(argv(argv_num) != "-")       e.material = strzone(argv(argv_num));   else    e.material = string_null;       ++argv_num;
296                 if(database)
297                 {
298                         // properties stored only for the database
299                         if(e.crypto_idfp)       strunzone(e.crypto_idfp);       if(argv(argv_num) != "-")       e.crypto_idfp = strzone(argv(argv_num));        else    e.crypto_idfp = string_null;    ++argv_num;
300                         if(e.netname)   strunzone(e.netname);   e.netname = strzone(argv(argv_num));    ++argv_num;
301                         if(e.message)   strunzone(e.message);   e.message = strzone(argv(argv_num));    ++argv_num;
302                         if(e.message2)  strunzone(e.message2);  e.message2 = strzone(argv(argv_num));   ++argv_num;
303                 }
304
305                 // attach last
306                 if(i)
307                         sandbox_ObjectAttach_Set(e, parent, tagname);
308         }
309
310         for(i = 0; i <= MAX_STORAGE_ATTACHMENTS; ++i)
311                 port_string[i] = string_null; // fully clear the string
312
313         return e;
314 }
315
316 void sandbox_Database_Save()
317 {
318         // saves all objects to the database file
319         entity head;
320         string file_name;
321         float file_get;
322
323         file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
324         file_get = fopen(file_name, FILE_WRITE);
325         fputs(file_get, strcat("// sandbox storage \"", autocvar_g_sandbox_storage_name, "\" for map \"", GetMapname(), "\" last updated ", strftime(TRUE, "%d-%m-%Y %H:%M:%S")));
326         fputs(file_get, strcat(" containing ", ftos(object_count), " objects\n"));
327
328         for(head = world; (head = find(head, classname, "object")); )
329         {
330                 // attached objects are persisted separately, ignore them here
331                 if(head.owner != world)
332                         continue;
333
334                 // use a line of text for each object, listing all properties
335                 fputs(file_get, strcat(sandbox_ObjectPort_Save(head, TRUE), "\n"));
336         }
337         fclose(file_get);
338 }
339
340 void sandbox_Database_Load()
341 {
342         // loads all objects from the database file
343         string file_read, file_name;
344         float file_get, i;
345
346         file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
347         file_get = fopen(file_name, FILE_READ);
348         if(file_get < 0)
349         {
350                 if(autocvar_g_sandbox_info > 0)
351                         print(strcat("^3SANDBOX - SERVER: ^7could not find storage file ^3", file_name, "^7, no objects were loaded\n"));
352         }
353         else
354         {
355                 for(;;)
356                 {
357                         file_read = fgets(file_get);
358                         if(!file_read)
359                                 break;
360                         if(substring(file_read, 0, 2) == "//")
361                                 continue;
362                         if(substring(file_read, 0, 1) == "#")
363                                 continue;
364
365                         entity e;
366                         e = sandbox_ObjectPort_Load(file_read, TRUE);
367
368                         if(e.material)
369                         {
370                                 // since objects are being loaded for the first time, precache material sounds for each
371                                 for (i = 1; i <= 5; i++) // 5 sounds in total
372                                         precache_sound(strcat("object/impact_", e.material, "_", ftos(i), ".ogg"));
373                         }
374                 }
375                 if(autocvar_g_sandbox_info > 0)
376                         print(strcat("^3SANDBOX - SERVER: ^7successfully loaded storage file ^3", file_name, "\n"));
377         }
378 }
379
380 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
381 {
382         if(MUTATOR_RETURNVALUE) // command was already handled?
383                 return FALSE;
384         if(cmd_name == "g_sandbox")
385         {
386                 if(cmd_argc < 2)
387                 {
388                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
389                         return TRUE;
390                 }
391
392                 switch(argv(1))
393                 {
394                         entity e;
395                         float i;
396                         string s;
397
398                         // ---------------- COMMAND: HELP ----------------
399                         case "help":
400                                 print_to(self, "You can use the following sandbox commands:");
401                                 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");
402                                 print_to(self, "^7\"^2object_remove^7\" removes the object the player is looking at. Players can only remove their own objects");
403                                 print_to(self, "^7\"^2object_duplicate ^3value^7\" duplicates the object, if the player has copying rights over the original");
404                                 print_to(self, "^3copy value ^7- copies the properties of the object to the specified client cvar");
405                                 print_to(self, "^3paste value ^7- spawns an object with the given properties. Properties or cvars must be specified as follows; eg1: \"0 1 2 ...\", eg2: \"$cl_cvar\"");
406                                 print_to(self, "^7\"^2object_attach ^3property value^7\" attaches one object to another. Players can only attach their own objects");
407                                 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
408                                 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
409                                 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
410                                 print_to(self, "^7\"^2object_edit ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
411                                 print_to(self, "^3skin value ^7- changes the skin of the object");
412                                 print_to(self, "^3alpha value ^7- sets object transparency");
413                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
414                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
415                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
416                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
417                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
418                                 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
419                                 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
420                                 print_to(self, "^7\"^2object_claim^7\" sets the player as the owner of the object, if he has the right to edit it");
421                                 print_to(self, "^7\"^2object_info ^3value^7\" shows public information about the object");
422                                 print_to(self, "^3object ^7- prints general information about the object, such as owner and creation / editing date");
423                                 print_to(self, "^3mesh ^7- prints information about the object's mesh, including skeletal bones");
424                                 print_to(self, "^3attachments ^7- prints information about the object's attachments");
425                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
426                                 return TRUE;
427
428                         // ---------------- COMMAND: OBJECT, SPAWN ----------------
429                         case "object_spawn":
430                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
431                                 {
432                                         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"));
433                                         return TRUE;
434                                 }
435                                 if(cmd_argc < 3)
436                                 {
437                                         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");
438                                         return TRUE;
439                                 }
440                                 else if not(fexists(argv(2)))
441                                 {
442                                         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");
443                                         return TRUE;
444                                 }
445
446                                 e = sandbox_ObjectSpawn(FALSE);
447                                 setmodel(e, argv(2));
448
449                                 if(autocvar_g_sandbox_info > 0)
450                                         print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " spawned an object at origin ^3", vtos(e.origin), "\n"));
451                                 return TRUE;
452
453                         // ---------------- COMMAND: OBJECT, REMOVE ----------------
454                         case "object_remove":
455                                 e = sandbox_ObjectEdit_Get(TRUE);
456                                 if(e != world)
457                                 {
458                                         if(autocvar_g_sandbox_info > 0)
459                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " removed an object at origin ^3", vtos(e.origin), "\n"));
460                                         sandbox_ObjectRemove(e);
461                                         return TRUE;
462                                 }
463
464                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be removed. Make sure you are facing an object that you have edit rights over");
465                                 return TRUE;
466
467                         // ---------------- COMMAND: OBJECT, DUPLICATE ----------------
468                         case "object_duplicate":
469                                 switch(argv(2))
470                                 {
471                                         case "copy":
472                                                 // copies customizable properties of the selected object to the clipboard cvar
473                                                 e = sandbox_ObjectEdit_Get(autocvar_g_sandbox_editor_free); // can we copy objects we can't edit?
474                                                 if(e != world)
475                                                 {
476                                                         s = sandbox_ObjectPort_Save(e, FALSE);
477                                                         s = strreplace("\"", "\\\"", s);
478                                                         stuffcmd(self, strcat("set ", argv(3), " \"", s, "\""));
479
480                                                         print_to(self, "^2SANDBOX - INFO: ^7Object copied to clipboard");
481                                                         return TRUE;
482                                                 }
483                                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be copied. Make sure you are facing an object that you have copy rights over");
484                                                 return TRUE;
485
486                                         case "paste":
487                                                 // spawns a new object using the properties in the player's clipboard cvar
488                                                 if(!argv(3)) // no object in clipboard
489                                                 {
490                                                         print_to(self, "^1SANDBOX - WARNING: ^7No object in clipboard. You must copy an object before you can paste it");
491                                                         return TRUE;
492                                                 }
493                                                 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
494                                                 {
495                                                         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"));
496                                                         return TRUE;
497                                                 }
498                                                 e = sandbox_ObjectPort_Load(argv(3), FALSE);
499
500                                                 print_to(self, "^2SANDBOX - INFO: ^7Object pasted successfully");
501                                                 if(autocvar_g_sandbox_info > 0)
502                                                         print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " pasted an object at origin ^3", vtos(e.origin), "\n"));
503                                                 return TRUE;
504                                 }
505                                 return TRUE;
506
507                         // ---------------- COMMAND: OBJECT, ATTACH ----------------
508                         case "object_attach":
509                                 switch(argv(2))
510                                 {
511                                         case "get":
512                                                 // select e as the object as meant to be attached
513                                                 e = sandbox_ObjectEdit_Get(TRUE);
514                                                 if(e != world)
515                                                 {
516                                                         self.object_attach = e;
517                                                         print_to(self, "^2SANDBOX - INFO: ^7Object selected for attachment");
518                                                         return TRUE;
519                                                 }
520                                                 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");
521                                                 return TRUE;
522                                         case "set":
523                                                 if(self.object_attach == world)
524                                                 {
525                                                         print_to(self, "^1SANDBOX - WARNING: ^7No object selected for attachment. Please select an object to be attached first.");
526                                                         return TRUE;
527                                                 }
528
529                                                 // attaches the previously selected object to e
530                                                 e = sandbox_ObjectEdit_Get(TRUE);
531                                                 if(e != world)
532                                                 {
533                                                         sandbox_ObjectAttach_Set(self.object_attach, e, argv(3));
534                                                         self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
535                                                         print_to(self, "^2SANDBOX - INFO: ^7Object attached successfully");
536                                                         if(autocvar_g_sandbox_info > 1)
537                                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " attached objects at origin ^3", vtos(e.origin), "\n"));
538                                                         return TRUE;
539                                                 }
540                                                 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");
541                                                 return TRUE;
542                                         case "remove":
543                                                 // removes e if it was attached
544                                                 e = sandbox_ObjectEdit_Get(TRUE);
545                                                 if(e != world)
546                                                 {
547                                                         sandbox_ObjectAttach_Remove(e);
548                                                         print_to(self, "^2SANDBOX - INFO: ^7Child objects detached successfully");
549                                                         if(autocvar_g_sandbox_info > 1)
550                                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " detached objects at origin ^3", vtos(e.origin), "\n"));
551                                                         return TRUE;
552                                                 }
553                                                 print_to(self, "^1SANDBOX - WARNING: ^7Child objects could not be detached. Make sure you are facing an object that you have edit rights over");
554                                                 return TRUE;
555                                 }
556                                 return TRUE;
557
558                         // ---------------- COMMAND: OBJECT, EDIT ----------------
559                         case "object_edit":
560                                 if(!argv(2))
561                                 {
562                                         print_to(self, "^1SANDBOX - WARNING: ^7Too few parameters. You must specify a property to edit");
563                                         return TRUE;
564                                 }
565
566                                 e = sandbox_ObjectEdit_Get(TRUE);
567                                 if(e != world)
568                                 {
569                                         switch(argv(2))
570                                         {
571                                                 case "skin":
572                                                         e.skin = stof(argv(3));
573                                                         break;
574                                                 case "alpha":
575                                                         e.alpha = stof(argv(3));
576                                                         break;
577                                                 case "color_main":
578                                                         e.colormod = stov(argv(3));
579                                                         break;
580                                                 case "color_glow":
581                                                         e.glowmod = stov(argv(3));
582                                                         break;
583                                                 case "frame":
584                                                         e.frame = stof(argv(3));
585                                                         break;
586                                                 case "scale":
587                                                         sandbox_ObjectEdit_Scale(e, stof(argv(3)));
588                                                         break;
589                                                 case "physics":
590                                                         switch(argv(3))
591                                                         {
592                                                                 case "0": // static
593                                                                         e.movetype = MOVETYPE_NONE;
594                                                                         break;
595                                                                 case "1": // movable
596                                                                         e.movetype = MOVETYPE_TOSS;
597                                                                         break;
598                                                                 case "2": // physical
599                                                                         e.movetype = MOVETYPE_PHYSICS;
600                                                                         break;
601                                                                 default:
602                                                                         break;
603                                                         }
604                                                         break;
605                                                 case "force":
606                                                         e.damageforcescale = stof(argv(3));
607                                                         break;
608                                                 case "material":
609                                                         if(e.material)  strunzone(e.material);
610                                                         if(argv(3))
611                                                         {
612                                                                 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
613                                                                         precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
614                                                                 e.material = strzone(argv(3));
615                                                         }
616                                                         else
617                                                                 e.material = string_null; // no material
618                                                         break;
619                                                 default:
620                                                         print_to(self, "^1SANDBOX - WARNING: ^7Invalid object property. For usage information, type 'sandbox help'");
621                                                         return TRUE;
622                                         }
623
624                                         // update last editing time
625                                         if(e.message2)  strunzone(e.message2);
626                                         e.message2 = strzone(strftime(TRUE, "%d-%m-%Y %H:%M:%S"));
627
628                                         if(autocvar_g_sandbox_info > 1)
629                                                 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " edited property ^3", argv(2), " ^7of an object at origin ^3", vtos(e.origin), "\n"));
630                                         return TRUE;
631                                 }
632
633                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be edited. Make sure you are facing an object that you have edit rights over");
634                                 return TRUE;
635
636                         // ---------------- COMMAND: OBJECT, CLAIM ----------------
637                         case "object_claim":
638                                 // if the player can edit an object but is not its owner, this can be used to claim that object
639                                 if(self.crypto_idfp == "")
640                                 {
641                                         print_to(self, "^1SANDBOX - WARNING: ^7You do not have a player UID, and cannot claim objects");
642                                         return TRUE;
643                                 }
644                                 e = sandbox_ObjectEdit_Get(TRUE);
645                                 if(e != world)
646                                 {
647                                         // update the owner's name
648                                         // Do this before checking if you're already the owner and skipping if such, so we
649                                         // also update the player's nickname if he changed it (but has the same player UID)
650                                         if(e.netname != self.netname)
651                                         {
652                                                 if(e.netname)   strunzone(e.netname);
653                                                 e.netname = strzone(self.netname);
654                                                 print_to(self, "^2SANDBOX - INFO: ^7Object owner name updated");
655                                         }
656
657                                         if(e.crypto_idfp == self.crypto_idfp)
658                                         {
659                                                 print_to(self, "^2SANDBOX - INFO: ^7Object is already yours, nothing to claim");
660                                                 return TRUE;
661                                         }
662
663                                         if(e.crypto_idfp)       strunzone(e.crypto_idfp);
664                                         e.crypto_idfp = strzone(self.crypto_idfp);
665
666                                         print_to(self, "^2SANDBOX - INFO: ^7Object claimed successfully");
667                                 }
668                                 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be claimed. Make sure you are facing an object that you have edit rights over");
669                                 return TRUE;
670
671                         // ---------------- COMMAND: OBJECT, INFO ----------------
672                         case "object_info":
673                                 // prints public information about the object to the player
674                                 e = sandbox_ObjectEdit_Get(FALSE);
675                                 if(e != world)
676                                 {
677                                         switch(argv(2))
678                                         {
679                                                 case "object":
680                                                         print_to(self, strcat("^2SANDBOX - INFO: ^7Object is owned by \"^7", e.netname, "^7\", created \"^3", e.message, "^7\", last edited \"^3", e.message2, "^7\""));
681                                                         return TRUE;
682                                                 case "mesh":
683                                                         for(i = 1; gettaginfo(e, i); i++)
684                                                                 s = strcat(s, "^7\"^5", gettaginfo_name, "^7\", ");
685                                                         print_to(self, strcat("^2SANDBOX - INFO: ^7Object mesh is \"^3", e.model, "^7\" at animation frame ^3", ftos(e.frame), " ^7containing the following tags: ", s));
686                                                         return TRUE;
687                                                 case "attachments":
688                                                         // this should show the same info as 'mesh' but for attachments
689                                                         entity head;
690                                                         for(head = world; (head = find(head, classname, "object")); )
691                                                         {
692                                                                 if(head.owner == e)
693                                                                 {
694                                                                         ++i; // start from 1
695                                                                         gettaginfo(e, head.tag_index);
696                                                                         s = strcat(s, "^1attachment ", ftos(i), "^7 has mesh \"^3", head.model, "^7\" at animation frame ^3", ftos(head.frame));
697                                                                         s = strcat(s, "^7 and is attached to bone \"^5", gettaginfo_name, "^7\", ");
698                                                                 }
699                                                         }
700                                                         if(i) // object contains attachments
701                                                                 print_to(self, strcat("^2SANDBOX - INFO: ^7Object contains the following ^1", ftos(i), "^7 attachment(s): ", s));
702                                                         else
703                                                                 print_to(self, "^2SANDBOX - INFO: ^7Object contains no attachments");
704                                                         return TRUE;
705                                         }
706                                 }
707                                 print_to(self, "^1SANDBOX - WARNING: ^7No information could be found. Make sure you are facing an object");
708                                 return TRUE;
709
710                         // ---------------- COMMAND: DEFAULT ----------------
711                         default:
712                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
713                                 return TRUE;
714                 }
715         }
716         return FALSE;
717 }
718
719 float autosave_time;
720 MUTATOR_HOOKFUNCTION(sandbox_StartFrame)
721 {
722         if(!autocvar_g_sandbox_storage_autosave)
723                 return FALSE;
724         if(time < autosave_time)
725                 return FALSE;
726         autosave_time = time + autocvar_g_sandbox_storage_autosave;
727
728         sandbox_Database_Save();
729
730         return TRUE;
731 }
732
733 MUTATOR_DEFINITION(sandbox)
734 {
735         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
736         MUTATOR_HOOK(SV_StartFrame, sandbox_StartFrame, CBC_ORDER_ANY);
737
738         MUTATOR_ONADD
739         {
740                 autosave_time = time + autocvar_g_sandbox_storage_autosave; // don't save the first server frame
741                 if(autocvar_g_sandbox_storage_autoload)
742                         sandbox_Database_Load();
743         }
744
745         return FALSE;
746 }
747