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