2 .string object_clipboard;
7 void sandbox_Object_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_EditObject_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_EditObject_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
60 void sandbox_AttachObject_Set(entity e, entity parent, string s)
62 // attaches e to parent on string s
64 e.old_movetype = e.movetype; // persist this
66 e.movetype = MOVETYPE_FOLLOW;
68 e.takedamage = DAMAGE_NO;
70 setattachment(e, parent, s);
74 void sandbox_AttachObject_Remove(entity e)
76 // detaches any object attached to e
79 for(head = world; (head = find(head, classname, "object")); )
83 head.movetype = head.old_movetype; // revert to previous physics
84 head.solid = SOLID_BBOX;
85 head.takedamage = DAMAGE_AIM;
87 setattachment(head, world, "");
90 // objects reset origin and angles when detached, so apply the parent's to prevent teleporting
91 setorigin(head, e.origin);
92 head.angles = e.angles;
97 entity sandbox_SpawnObject(float database)
99 // spawn a new object with default properties
103 e.classname = "object";
104 e.takedamage = DAMAGE_AIM;
105 e.damageforcescale = 1;
106 e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
107 e.movetype = MOVETYPE_TOSS;
110 e.material = string_null;
111 e.touch = sandbox_Object_Touch;
115 // set the object's owner via player UID
116 // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
117 if(self.crypto_idfp != "")
118 e.crypto_idfp = strzone(self.crypto_idfp);
120 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!");
122 // set origin and direction based on player position and view angle
123 makevectors(self.v_angle);
124 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
125 setorigin(e, trace_endpos);
126 e.angles_y = self.v_angle_y;
133 void sandbox_RemoveObject(entity e)
135 sandbox_AttachObject_Remove(e); // detach child objects
138 strunzone(e.material);
139 e.material = string_null;
143 strunzone(e.crypto_idfp);
144 e.crypto_idfp = string_null;
152 string sandbox_Storage_Save(entity e, float database)
154 // save object properties
157 s = strcat(e.model, " ");
158 s = strcat(s, ftos(e.skin), " ");
159 s = strcat(s, ftos(e.alpha), " ");
160 s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
161 s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
162 s = strcat(s, ftos(e.frame), " ");
163 s = strcat(s, ftos(e.scale), " ");
164 s = strcat(s, ftos(e.movetype), " ");
165 s = strcat(s, ftos(e.damageforcescale), " ");
166 s = strcat(s, e.material, " ");
169 s = strcat(s, e.crypto_idfp, " ");
170 s = strcat(s, sprintf("\"%.9v\"", e.origin), " ");
171 s = strcat(s, sprintf("\"%.9v\"", e.angles), " ");
177 void sandbox_Storage_Load(entity e, string s, float database)
179 // load object properties
182 setmodel(e, argv(0));
183 e.skin = stof(argv(1));
184 e.alpha = stof(argv(2));
185 e.colormod = stov(argv(3));
186 e.glowmod = stov(argv(4));
187 e.frame = stof(argv(5));
188 sandbox_EditObject_Scale(e, stof(argv(6)));
189 e.movetype = stof(argv(7));
190 e.damageforcescale = stof(argv(8));
191 if(e.material) strunzone(e.material); if(argv(9)) e.material = strzone(argv(9)); else e.material = string_null;
194 if(e.crypto_idfp) strunzone(e.crypto_idfp); if(argv(10)) e.crypto_idfp = strzone(argv(10)); else e.crypto_idfp = string_null;
195 setorigin(e, stov(argv(11)));
196 e.angles = stov(argv(12));
200 void sandbox_Storage_DatabaseSave()
202 // saves all objects to the database file
204 return; // nothing to store
210 fn = strcat("sandbox/storage_", GetMapname(), ".txt");
211 fh = fopen(fn, FILE_WRITE);
212 fputs(fh, strcat("// sandbox storage for map ", GetMapname(), ", containing a total of ", ftos(object_count), " objects\n"));
214 for(head = world; (head = find(head, classname, "object")); )
216 // Unfortunately, attached objects cannot be persisted yet. That's because the parent is specified as an entity,
217 // but only properties can be saved to this file, leaving no way to identify the owner once all objects are spawned
218 // TODO: Find some way to fix this!
219 if(head.owner != world)
222 // use a line for each object, listing all properties
223 fputs(fh, strcat("object", ftos(i), " ", sandbox_Storage_Save(head, TRUE), "\n"));
229 void sandbox_Storage_DatabaseLoad()
231 // loads all objects from the database file
235 fn = strcat("sandbox/storage_", GetMapname(), ".txt");
236 fh = fopen(fn, FILE_READ);
238 { } // message comes here
246 if(substring(rf, 0, 2) == "//")
248 if(substring(rf, 0, 1) == "#")
250 dprint(strcat(rf, " --------\n"));
255 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
257 if(MUTATOR_RETURNVALUE) // command was already handled?
259 if(cmd_name == "g_sandbox")
263 print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
272 // ---------------- COMMAND: HELP ----------------
274 print_to(self, "You can use the following sandbox commands:");
275 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");
276 print_to(self, "^7\"^2object_remove^7\" removes the object the player is looking at. Players can only remove their own objects");
277 print_to(self, "^7\"^2object_duplicate ^3value^7\" duplicates the object. 'copy' copies the object, 'paste' puts it in front of the player");
278 print_to(self, "^7\"^2object_claim^7\" sets the player as the owner of the object, if he has the right to edit it");
279 print_to(self, "^7\"^2object_attach ^3property value^7\" attaches one object to another. Players can only attach their own objects");
280 print_to(self, "^7Attachment properties for ^2object_attach^7:");
281 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
282 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
283 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
284 print_to(self, "^7\"^2object_edit ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
285 print_to(self, "^7Object properties for ^2object_edit^7:");
286 print_to(self, "^3skin value ^7- changes the skin of the object");
287 print_to(self, "^3alpha value ^7- sets object transparency");
288 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
289 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
290 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
291 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
292 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
293 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
294 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
295 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
298 // ---------------- COMMAND: SPAWN OBJECT ----------------
300 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
302 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"));
307 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");
310 else if not(fexists(argv(2)))
312 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");
316 e = sandbox_SpawnObject(FALSE);
317 setmodel(e, argv(2));
319 if(autocvar_g_sandbox_info > 0)
320 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " spawned an object at origin ^3", vtos(e.origin), "\n"));
323 // ---------------- COMMAND: REMOVE OBJECT ----------------
324 case "object_remove":
325 e = sandbox_EditObject_Get();
328 if(autocvar_g_sandbox_info > 0)
329 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " removed an object at origin ^3", vtos(e.origin), "\n"));
330 sandbox_RemoveObject(e);
334 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be removed. Make sure you are facing an object that you have edit rights over");
337 // ---------------- COMMAND: DUPLICATE OBJECT ----------------
338 case "object_duplicate":
342 // copies customizable properties of the selected object to the clipboard
343 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
346 if(self.object_clipboard)
347 strunzone(self.object_clipboard);
348 self.object_clipboard = strzone(sandbox_Storage_Save(e, FALSE));
350 print_to(self, "^2SANDBOX - INFO: ^7Object copied to clipboard");
353 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be copied. Make sure you are facing an object that you have edit rights over");
357 // spawns a new object using the properties in the player's clipboard
358 if(!self.object_clipboard) // no object in clipboard
360 print_to(self, "^1SANDBOX - WARNING: ^7No object in clipboard. You must copy an object before you can paste it");
363 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
365 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"));
369 e = sandbox_SpawnObject(FALSE);
370 sandbox_Storage_Load(e, self.object_clipboard, FALSE);
372 print_to(self, "^2SANDBOX - INFO: ^7Object pasted successfully");
373 if(autocvar_g_sandbox_info > 0)
374 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " pasted an object at origin ^3", vtos(e.origin), "\n"));
379 // ---------------- COMMAND: CLAIM OBJECT ----------------
381 // if the player can edit an object but is not its owner, this can be used to claim that object
382 if(self.crypto_idfp == "")
384 print_to(self, "^1SANDBOX - WARNING: ^7You do not have a player UID, and cannot claim objects");
387 e = sandbox_EditObject_Get();
390 if(e.crypto_idfp == self.crypto_idfp)
392 print_to(self, "^2SANDBOX - INFO: ^7Object is already yours, nothing to claim");
397 strunzone(e.crypto_idfp);
398 e.crypto_idfp = self.crypto_idfp;
399 print_to(self, "^2SANDBOX - INFO: ^7Object claimed successfully");
401 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be claimed. Make sure you are facing an object that you have edit rights over");
404 // ---------------- COMMAND: ATTACH OBJECT ----------------
405 case "object_attach":
409 // select e as the object as meant to be attached
410 e = sandbox_EditObject_Get();
413 self.object_attach = e;
414 print_to(self, "^2SANDBOX - INFO: ^7Object selected for attachment");
417 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");
420 if(self.object_attach == world)
422 print_to(self, "^1SANDBOX - WARNING: ^7No object selected for attachment. Please select an object to be attached first.");
426 // attaches the previously selected object to e
427 e = sandbox_EditObject_Get();
430 sandbox_AttachObject_Set(self.object_attach, e, argv(3));
431 self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
432 print_to(self, "^2SANDBOX - INFO: ^7Object attached successfully");
433 if(autocvar_g_sandbox_info > 1)
434 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " attached objects at origin ^3", vtos(e.origin), "\n"));
437 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");
440 // removes e if it was attached
441 e = sandbox_EditObject_Get();
444 sandbox_AttachObject_Remove(e);
445 print_to(self, "^2SANDBOX - INFO: ^7Child objects detached successfully");
446 if(autocvar_g_sandbox_info > 1)
447 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " detached objects at origin ^3", vtos(e.origin), "\n"));
450 print_to(self, "^1SANDBOX - WARNING: ^7Child objects could not be detached. Make sure you are facing an object that you have edit rights over");
455 // ---------------- COMMAND: EDIT OBJECT ----------------
459 print_to(self, "^1SANDBOX - WARNING: ^7Too few parameters. You must specify a property to edit");
463 e = sandbox_EditObject_Get();
469 e.skin = stof(argv(3));
472 e.alpha = stof(argv(3));
475 e.colormod = stov(argv(3));
478 e.glowmod = stov(argv(3));
481 e.frame = stof(argv(3));
484 sandbox_EditObject_Scale(e, stof(argv(3)));
490 e.movetype = MOVETYPE_NONE;
493 e.movetype = MOVETYPE_TOSS;
495 case "2": // physical
496 e.movetype = MOVETYPE_PHYSICS;
503 e.damageforcescale = stof(argv(3));
507 strunzone(e.material);
510 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
511 precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
512 e.material = strzone(argv(3));
515 e.material = string_null; // no material
518 print_to(self, "^1SANDBOX - WARNING: ^7Invalid object property. For usage information, type 'sandbox help'");
521 if(autocvar_g_sandbox_info > 1)
522 print(strcat("^3SANDBOX - SERVER: ^7", self.netname, " edited property ^3", argv(2), " ^7of an object at origin ^3", vtos(e.origin), "\n"));
526 print_to(self, "^1SANDBOX - WARNING: ^7Object could not be edited. Make sure you are facing an object that you have edit rights over");
529 // ---------------- COMMAND: DEFAULT ----------------
531 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
538 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
540 // if the player is close enough to their object, they can drag it
542 if(autocvar_sv_cheats)
543 return FALSE; // cheat dragging is used instead
545 // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
546 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
547 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
548 // it goes out of range while slinging it around.
553 e = sandbox_EditObject_Get();
554 if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
557 if(Drag(e, grab)) // execute dragging
563 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
565 // unzone the player's clipboard if it's not empty
566 if(self.object_clipboard)
568 strunzone(self.object_clipboard);
569 self.object_clipboard = string_null;
576 MUTATOR_HOOKFUNCTION(sandbox_StartFrame)
578 if(!autocvar_g_sandbox_storage_autosave)
580 if(time < autocvar_g_sandbox_storage_autosave)
582 autosave_time = time + autocvar_g_sandbox_storage_autosave;
584 sandbox_Storage_DatabaseSave();
589 MUTATOR_DEFINITION(sandbox)
591 MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
592 MUTATOR_HOOK(SV_StartFrame, sandbox_StartFrame, CBC_ORDER_ANY);
593 MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
594 MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
598 if(autocvar_g_sandbox_storage_autoload)
599 sandbox_Storage_DatabaseLoad();