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