2 .string object_clipboard;
7 void sandbox_ObjectFunction_Touch()
9 // apply material impact effects
13 if(self.touch_timer > time)
14 return; // don't execute each frame
15 self.touch_timer = time + 0.1;
17 // make particle count and sound volume depend on impact speed
19 intensity = vlen(self.velocity) + vlen(other.velocity);
20 if(intensity) // avoid divisions by 0
21 intensity /= 2; // average the two velocities
22 if not(intensity >= autocvar_g_sandbox_object_material_velocity_min)
23 return; // impact not strong enough to do anything
24 // now offset intensity and apply it to the effects
25 intensity -= autocvar_g_sandbox_object_material_velocity_min; // start from minimum velocity, not actual velocity
26 intensity = bound(0, intensity * autocvar_g_sandbox_object_material_velocity_factor, 1);
28 sound(self, CH_TRIGGER, strcat("object/impact_", self.material, "_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE * intensity, ATTN_NORM);
29 pointparticles(particleeffectnum(strcat("impact_", self.material)), self.origin, '0 0 0', ceil(intensity * 10)); // allow a count from 1 to 10
32 entity sandbox_ObjectEdit_Get()
34 // returns the traced entity if the player can edit it, and world if not
35 // attached objects are SOLID_NOT and don't risk getting traced
37 makevectors(self.v_angle);
38 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
40 if(trace_ent.classname != "object")
41 return world; // entity is not an object
42 if(!trace_ent.crypto_idfp)
43 return trace_ent; // the player who spawned this object did not have an UID, so anyone can edit it
44 else if not(trace_ent.crypto_idfp != self.crypto_idfp && !autocvar_g_sandbox_editor_free)
45 return trace_ent; // object does not belong to the player, and players can only edit their own objects on this server
49 void sandbox_ObjectEdit_Scale(entity e, float f)
54 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
55 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
59 void sandbox_ObjectAttach_Remove(entity e);
60 void sandbox_ObjectAttach_Set(entity e, entity parent, string s)
62 // attaches e to parent on string s
64 // we can't attach to an attachment, for obvious reasons
65 sandbox_ObjectAttach_Remove(e);
67 e.movetype = MOVETYPE_FOLLOW;
69 e.takedamage = DAMAGE_NO;
71 setattachment(e, parent, s);
75 void sandbox_ObjectAttach_Remove(entity e)
77 // detaches any object attached to e
80 for(head = world; (head = find(head, classname, "object")); )
84 head.movetype = MOVETYPE_TOSS; // default
85 head.solid = SOLID_BBOX;
86 head.takedamage = DAMAGE_AIM;
88 setattachment(head, world, "");
91 // objects reset origin and angles when detached, so apply the parent's to prevent teleporting
92 setorigin(head, e.origin);
93 head.angles = e.angles;
98 entity sandbox_ObjectSpawn(float database)
100 // spawn a new object with default properties
104 e.classname = "object";
105 e.takedamage = DAMAGE_AIM;
106 e.damageforcescale = 1;
107 e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
108 e.movetype = MOVETYPE_TOSS;
111 e.material = string_null;
112 e.touch = sandbox_ObjectFunction_Touch;
116 // set the object's owner via player UID
117 // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
118 if(self.crypto_idfp != "")
119 e.crypto_idfp = strzone(self.crypto_idfp);
121 print_to(self, "^1SANDBOX - WARNING: ^7You spawned an object, but lack a player UID. ^1Your objects are not secured and can be edited by any player!");
123 // set origin and direction based on player position and view angle
124 makevectors(self.v_angle);
125 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
126 setorigin(e, trace_endpos);
127 e.angles_y = self.v_angle_y;
134 void sandbox_ObjectRemove(entity e)
136 sandbox_ObjectAttach_Remove(e); // detach child objects
139 strunzone(e.material);
140 e.material = string_null;
144 strunzone(e.crypto_idfp);
145 e.crypto_idfp = string_null;
153 string sandbox_ObjectPort_Save_string(entity e, float database)
156 s = strcat(s, e.model, " ");
157 s = strcat(s, ftos(e.skin), " ");
158 s = strcat(s, ftos(e.alpha), " ");
159 s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
160 s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
161 s = strcat(s, ftos(e.frame), " ");
162 s = strcat(s, ftos(e.scale), " ");
163 s = strcat(s, ftos(e.movetype), " ");
164 s = strcat(s, ftos(e.damageforcescale), " ");
165 if(e.material) s = strcat(s, e.material, " "); else s = strcat(s, "- "); // none
168 if(e.crypto_idfp) s = strcat(s, e.crypto_idfp, " "); else s = strcat(s, "- "); // none
169 s = strcat(s, sprintf("\"%.9v\"", e.origin), " ");
170 s = strcat(s, sprintf("\"%.9v\"", e.angles), " ");
175 string sandbox_ObjectPort_Save(entity e, float database)
177 // save object properties
181 // first set the properties of the main object, then those of each child
182 s = sandbox_ObjectPort_Save_string(e, database);
183 for(head = world; (head = find(head, classname, "object")); )
186 s = strcat(s, sandbox_ObjectPort_Save_string(head, database));
192 void sandbox_ObjectPort_Load(entity e, string s, float database)
194 // load object properties
197 setmodel(e, argv(0));
198 e.skin = stof(argv(1));
199 e.alpha = stof(argv(2));
200 e.colormod = stov(argv(3));
201 e.glowmod = stov(argv(4));
202 e.frame = stof(argv(5));
203 sandbox_ObjectEdit_Scale(e, stof(argv(6)));
204 e.movetype = stof(argv(7));
205 e.damageforcescale = stof(argv(8));
206 if(e.material) strunzone(e.material); if(argv(9) != "-") e.material = strzone(argv(9)); else e.material = string_null;
209 if(e.crypto_idfp) strunzone(e.crypto_idfp); if(argv(10) != "-") e.crypto_idfp = strzone(argv(10)); else e.crypto_idfp = string_null;
210 setorigin(e, stov(argv(11)));
211 e.angles = stov(argv(12));
215 void sandbox_Database_Save()
217 // saves all objects to the database file
222 file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
223 file_get = fopen(file_name, FILE_WRITE);
224 fputs(file_get, strcat("// sandbox storage \"", autocvar_g_sandbox_storage_name, "\" for map \"", GetMapname(), "\" last updated ", strftime(TRUE, "%d-%m-%Y %H:%M:%S")));
225 fputs(file_get, strcat(" containing ", ftos(object_count), " objects\n"));
227 for(head = world; (head = find(head, classname, "object")); )
229 // Unfortunately, attached objects cannot be persisted yet. That's because the parent is specified as an entity,
230 // but only properties can be saved to this file, leaving no way to identify the owner once all objects are spawned
231 // TODO: Find some way to fix this!
232 if(head.owner != world)
235 // use a line for each object, listing all properties
236 fputs(file_get, strcat(sandbox_ObjectPort_Save(head, TRUE), "\n"));
241 void sandbox_Database_Load()
243 // loads all objects from the database file
244 string file_read, file_name;
247 file_name = strcat("sandbox/storage_", autocvar_g_sandbox_storage_name, "_", GetMapname(), ".txt");
248 file_get = fopen(file_name, FILE_READ);
251 if(autocvar_g_sandbox_info > 0)
252 print(strcat("^3SANDBOX - SERVER: ^7could not find storage file ^3", file_name, "^7, no objects were loaded\n"));
258 file_read = fgets(file_get);
261 if(substring(file_read, 0, 2) == "//")
263 if(substring(file_read, 0, 1) == "#")
267 e = sandbox_ObjectSpawn(TRUE);
268 sandbox_ObjectPort_Load(e, file_read, TRUE);
272 // since objects are being loaded for the first time, precache material sounds for each
273 for (i = 1; i <= 5; i++) // 5 sounds in total
274 precache_sound(strcat("object/impact_", e.material, "_", ftos(i), ".ogg"));
277 if(autocvar_g_sandbox_info > 0)
278 print(strcat("^3SANDBOX - SERVER: ^7successfully loaded storage file ^3", file_name, "\n"));
282 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
284 if(MUTATOR_RETURNVALUE) // command was already handled?
286 if(cmd_name == "g_sandbox")
290 print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
299 // ---------------- COMMAND: HELP ----------------
301 print_to(self, "You can use the following sandbox commands:");
302 print_to(self, "^7\"^2object_spawn ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
303 print_to(self, "^7\"^2object_remove^7\" removes the object the player is looking at. Players can only remove their own objects");
304 print_to(self, "^7\"^2object_duplicate ^3value^7\" duplicates the object. 'copy' copies the object, 'paste' puts it in front of the player");
305 print_to(self, "^7\"^2object_claim^7\" sets the player as the owner of the object, if he has the right to edit it");
306 print_to(self, "^7\"^2object_attach ^3property value^7\" attaches one object to another. Players can only attach their own objects");
307 print_to(self, "^7Attachment properties for ^2object_attach^7:");
308 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
309 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
310 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
311 print_to(self, "^7\"^2object_edit ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
312 print_to(self, "^7Object properties for ^2object_edit^7:");
313 print_to(self, "^3skin value ^7- changes the skin of the object");
314 print_to(self, "^3alpha value ^7- sets object transparency");
315 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
316 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
317 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
318 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
319 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
320 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
321 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
322 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
325 // ---------------- COMMAND: SPAWN OBJECT ----------------
327 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
329 print_to(self, strcat("^1SANDBOX - WARNING: ^7Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
334 print_to(self, "^1SANDBOX - WARNING: ^7Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'object_spawn' command");
337 else if not(fexists(argv(2)))
339 print_to(self, "^1SANDBOX - WARNING: ^7Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
343 e = sandbox_ObjectSpawn(FALSE);
344 setmodel(e, argv(2));
346 if(autocvar_g_sandbox_info > 0)
347 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " spawned an object at origin ^3", vtos(e.origin), "\n"));
350 // ---------------- COMMAND: REMOVE OBJECT ----------------
351 case "object_remove":
352 e = sandbox_ObjectEdit_Get();
355 if(autocvar_g_sandbox_info > 0)
356 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " removed an object at origin ^3", vtos(e.origin), "\n"));
357 sandbox_ObjectRemove(e);
361 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be removed. Make sure you are facing an object that you have edit rights over");
364 // ---------------- COMMAND: DUPLICATE OBJECT ----------------
365 case "object_duplicate":
369 // copies customizable properties of the selected object to the clipboard
370 e = sandbox_ObjectEdit_Get(); // you can only copy objects you can edit, so this works
373 if(self.object_clipboard)
374 strunzone(self.object_clipboard);
375 self.object_clipboard = strzone(sandbox_ObjectPort_Save(e, FALSE));
377 print_to(self, "^2SANDBOX - INFO: ^7Object copied to clipboard");
380 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be copied. Make sure you are facing an object that you have edit rights over");
384 // spawns a new object using the properties in the player's clipboard
385 if(!self.object_clipboard) // no object in clipboard
387 print_to(self, "^1SANDBOX - WARNING: ^7No object in clipboard. You must copy an object before you can paste it");
390 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
392 print_to(self, strcat("^1SANDBOX - WARNING: ^7Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
396 e = sandbox_ObjectSpawn(FALSE);
397 sandbox_ObjectPort_Load(e, self.object_clipboard, FALSE);
399 print_to(self, "^2SANDBOX - INFO: ^7Object pasted successfully");
400 if(autocvar_g_sandbox_info > 0)
401 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " pasted an object at origin ^3", vtos(e.origin), "\n"));
406 // ---------------- COMMAND: CLAIM OBJECT ----------------
408 // if the player can edit an object but is not its owner, this can be used to claim that object
409 if(self.crypto_idfp == "")
411 print_to(self, "^1SANDBOX - WARNING: ^7You do not have a player UID, and cannot claim objects");
414 e = sandbox_ObjectEdit_Get();
417 if(e.crypto_idfp == self.crypto_idfp)
419 print_to(self, "^2SANDBOX - INFO: ^7Object is already yours, nothing to claim");
424 strunzone(e.crypto_idfp);
425 e.crypto_idfp = self.crypto_idfp;
426 print_to(self, "^2SANDBOX - INFO: ^7Object claimed successfully");
428 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be claimed. Make sure you are facing an object that you have edit rights over");
431 // ---------------- COMMAND: ATTACH OBJECT ----------------
432 case "object_attach":
436 // select e as the object as meant to be attached
437 e = sandbox_ObjectEdit_Get();
440 self.object_attach = e;
441 print_to(self, "^2SANDBOX - INFO: ^7Object selected for attachment");
444 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be selected for attachment. Make sure you are facing an object that you have edit rights over");
447 if(self.object_attach == world)
449 print_to(self, "^1SANDBOX - WARNING: ^7No object selected for attachment. Please select an object to be attached first.");
453 // attaches the previously selected object to e
454 e = sandbox_ObjectEdit_Get();
457 sandbox_ObjectAttach_Set(self.object_attach, e, argv(3));
458 self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
459 print_to(self, "^2SANDBOX - INFO: ^7Object attached successfully");
460 if(autocvar_g_sandbox_info > 1)
461 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " attached objects at origin ^3", vtos(e.origin), "\n"));
464 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be attached to the parent. Make sure you are facing an object that you have edit rights over");
467 // removes e if it was attached
468 e = sandbox_ObjectEdit_Get();
471 sandbox_ObjectAttach_Remove(e);
472 print_to(self, "^2SANDBOX - INFO: ^7Child objects detached successfully");
473 if(autocvar_g_sandbox_info > 1)
474 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " detached objects at origin ^3", vtos(e.origin), "\n"));
477 print_to(self, "^1SANDBOX - WARNING: ^7Child objects could not be detached. Make sure you are facing an object that you have edit rights over");
482 // ---------------- COMMAND: EDIT OBJECT ----------------
486 print_to(self, "^1SANDBOX - WARNING: ^7Too few parameters. You must specify a property to edit");
490 e = sandbox_ObjectEdit_Get();
496 e.skin = stof(argv(3));
499 e.alpha = stof(argv(3));
502 e.colormod = stov(argv(3));
505 e.glowmod = stov(argv(3));
508 e.frame = stof(argv(3));
511 sandbox_ObjectEdit_Scale(e, stof(argv(3)));
517 e.movetype = MOVETYPE_NONE;
520 e.movetype = MOVETYPE_TOSS;
522 case "2": // physical
523 e.movetype = MOVETYPE_PHYSICS;
530 e.damageforcescale = stof(argv(3));
534 strunzone(e.material);
537 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
538 precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
539 e.material = strzone(argv(3));
542 e.material = string_null; // no material
545 print_to(self, "^1SANDBOX - WARNING: ^7Invalid object property. For usage information, type 'sandbox help'");
548 if(autocvar_g_sandbox_info > 1)
549 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " edited property ^3", argv(2), " ^7of an object at origin ^3", vtos(e.origin), "\n"));
553 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be edited. Make sure you are facing an object that you have edit rights over");
556 // ---------------- COMMAND: DEFAULT ----------------
558 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
565 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
567 // if the player is close enough to their object, they can drag it
569 if(autocvar_sv_cheats)
570 return FALSE; // cheat dragging is used instead
572 // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
573 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
574 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
575 // it goes out of range while slinging it around.
580 e = sandbox_ObjectEdit_Get();
581 if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
584 if(Drag(e, grab)) // execute dragging
590 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
592 // unzone the player's clipboard if it's not empty
593 if(self.object_clipboard)
595 strunzone(self.object_clipboard);
596 self.object_clipboard = string_null;
603 MUTATOR_HOOKFUNCTION(sandbox_StartFrame)
605 if(!autocvar_g_sandbox_storage_autosave)
607 if(time < autocvar_g_sandbox_storage_autosave)
609 autosave_time = time + autocvar_g_sandbox_storage_autosave;
611 sandbox_Database_Save();
616 MUTATOR_DEFINITION(sandbox)
618 MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
619 MUTATOR_HOOK(SV_StartFrame, sandbox_StartFrame, CBC_ORDER_ANY);
620 MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
621 MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
625 autosave_time = time + autocvar_g_sandbox_storage_autosave; // don't save the first server frame
626 if(autocvar_g_sandbox_storage_autoload)
627 sandbox_Database_Load();