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