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()
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;
113 // set the object's owner via player UID
114 // if the player does not have an UID, the owner cannot be stored and his objects may be edited by anyone
115 if(self.crypto_idfp != "")
116 e.crypto_idfp = strzone(self.crypto_idfp);
118 print_to(self, "WARNING: You spawned an object, but lack a player UID. ^1Your objects are not secured and can be edited by any player!");
120 // set origin and direction based on player position and view angle
121 makevectors(self.v_angle);
122 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
123 setorigin(e, trace_endpos);
124 e.angles_y = self.v_angle_y;
130 void sandbox_RemoveObject(entity e)
134 strunzone(e.material);
135 e.material = string_null;
139 strunzone(e.crypto_idfp);
140 e.crypto_idfp = string_null;
148 string sandbox_Storage_Save(entity e)
150 // save object properties
153 s = strcat(e.model, " ");
154 s = strcat(s, ftos(e.skin), " ");
155 s = strcat(s, ftos(e.alpha), " ");
156 s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
157 s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
158 s = strcat(s, ftos(e.frame), " ");
159 s = strcat(s, ftos(e.scale), " ");
160 s = strcat(s, ftos(e.movetype), " ");
161 s = strcat(s, ftos(e.damageforcescale), " ");
162 s = strcat(s, e.material, " ");
167 void sandbox_Storage_Load(entity e, string s)
169 // load object properties
172 setmodel(e, argv(0));
173 e.skin = stof(argv(1));
174 e.alpha = stof(argv(2));
175 e.colormod = stov(argv(3));
176 e.glowmod = stov(argv(4));
177 e.frame = stof(argv(5));
178 sandbox_EditObject_Scale(e, stof(argv(6)));
179 e.movetype = stof(argv(7));
180 e.damageforcescale = stof(argv(8));
181 if(e.material) strunzone(e.material); if(argv(9)) e.material = strzone(argv(9)); else e.material = string_null;
184 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
186 if(MUTATOR_RETURNVALUE) // command was already handled?
188 if(cmd_name == "g_sandbox")
192 print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
201 // ---------------- COMMAND: HELP ----------------
203 print_to(self, "You can use the following sandbox commands:");
204 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
205 print_to(self, "^7\"^2spawn_object ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
206 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
207 print_to(self, "^7\"^2duplicate_object ^3value^7\" duplicates the object. 'copy' copies the object, 'paste' puts it in front of the player");
208 print_to(self, "^7\"^2claim_object^7\" sets the player as the owner of the object, if he has the right to edit it");
209 print_to(self, "^7\"^2attach_object ^3property value^7\" attaches one object to another. Players can only attach their own objects");
210 print_to(self, "^7Attachment properties for ^2attach_object^7:");
211 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
212 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
213 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
214 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
215 print_to(self, "^7Object properties for ^2edit_object^7:");
216 print_to(self, "^3skin value ^7- changes the skin of the object");
217 print_to(self, "^3alpha value ^7- sets object transparency");
218 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
219 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
220 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
221 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
222 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
223 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
224 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
225 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
228 // ---------------- COMMAND: SPAWN ITEM ----------------
230 // only weapons are currently supported
234 print_to(self, "WARNING: Attempted to spawn an item without specifying its type. Please specify the name of your item after the 'spawn_item' command");
239 makevectors(self.v_angle);
240 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
242 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
244 e = get_weaponinfo(i);
245 if(e.netname == argv(2))
247 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
248 if(autocvar_g_sandbox_info)
249 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
254 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
257 // ---------------- COMMAND: SPAWN OBJECT ----------------
259 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
261 print_to(self, strcat("WARNING: Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
266 print_to(self, "WARNING: Attempted to spawn an object without specifying a model. Please specify the path to your model file after the 'spawn_object' command");
269 else if not(fexists(argv(2)))
271 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
275 e = sandbox_SpawnObject();
276 setmodel(e, argv(2));
278 if(autocvar_g_sandbox_info)
279 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
283 // ---------------- COMMAND: REMOVE OBJECT ----------------
284 case "remove_object":
285 e = sandbox_EditObject_Get();
288 if(autocvar_g_sandbox_info)
289 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
290 sandbox_RemoveObject(e);
294 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you (default)");
297 // ---------------- COMMAND: DUPLICATE OBJECT ----------------
298 case "duplicate_object":
302 // copies customizable properties of the selected object to the clipboard
303 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
306 if(self.object_clipboard)
307 strunzone(self.object_clipboard);
308 self.object_clipboard = strzone(sandbox_Storage_Save(e));
310 print_to(self, "Object copied to clipboard");
313 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you (default)");
317 // spawns a new object using the properties in the player's clipboard
318 if(!self.object_clipboard) // no object in clipboard
320 print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
323 if(object_count >= autocvar_g_sandbox_editor_maxobjects)
325 print_to(self, strcat("WARNING: Cannot spawn any more objects. Up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects may exist at a time"));
329 e = sandbox_SpawnObject();
330 sandbox_Storage_Load(e, self.object_clipboard);
332 print_to(self, "Object pasted successfully");
333 if(autocvar_g_sandbox_info)
334 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
339 // ---------------- COMMAND: CLAIM OBJECT ----------------
341 if(self.crypto_idfp == "")
343 print_to(self, "WARNING: You do not have a player UID, and cannot claim objects");
346 e = sandbox_EditObject_Get();
349 if(e.crypto_idfp == self.crypto_idfp)
351 print_to(self, "Object is already yours, nothing to claim");
356 strunzone(e.crypto_idfp);
357 e.crypto_idfp = self.crypto_idfp;
358 print_to(self, "Object claimed successfully");
360 print_to(self, "WARNING: Object could not be claimed. Make sure you are facing an object that belongs to you (default)");
363 // ---------------- COMMAND: ATTACH OBJECT ----------------
364 case "attach_object":
368 // select e as the object as meant to be attached
369 e = sandbox_EditObject_Get();
372 self.object_attach = e;
373 print_to(self, "Object selected for attachment");
376 print_to(self, "WARNING: Object could not be selected for attachment. Make sure you are facing an object that belongs to you (default)");
379 if(self.object_attach == world)
381 print_to(self, "WARNING: No object selected for attachment. Please select an object to be attached first.");
385 // attaches the previously selected object to e
386 e = sandbox_EditObject_Get();
389 sandbox_AttachObject_Set(self.object_attach, e, argv(3));
390 print_to(self, "Object attached successfully");
391 self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
394 print_to(self, "WARNING: Object could not be attached to the parent. Make sure you are facing an object that belongs to you (default)");
397 // removes e if it was attached
398 e = sandbox_EditObject_Get();
401 sandbox_AttachObject_Remove(e);
402 print_to(self, "Child objects detached successfully");
405 print_to(self, "WARNING: Child objects could not be detached. Make sure you are facing an object that belongs to you (default)");
410 // ---------------- COMMAND: EDIT OBJECT ----------------
414 print_to(self, "WARNING: Too few parameters. You must specify a property to edit");
418 e = sandbox_EditObject_Get();
424 e.skin = stof(argv(3));
427 e.alpha = stof(argv(3));
430 e.colormod = stov(argv(3));
433 e.glowmod = stov(argv(3));
436 e.frame = stof(argv(3));
439 sandbox_EditObject_Scale(e, stof(argv(3)));
445 e.movetype = MOVETYPE_NONE;
448 e.movetype = MOVETYPE_TOSS;
450 case "2": // physical
451 e.movetype = MOVETYPE_PHYSICS;
458 e.damageforcescale = stof(argv(3));
462 strunzone(e.material);
465 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
466 precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
467 e.material = strzone(argv(3));
470 e.material = string_null; // no material
473 print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
479 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you (default)");
482 // ---------------- COMMAND: DEFAULT ----------------
484 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
491 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
493 // if the player is close enough to their object, they can drag it
495 if(autocvar_sv_cheats)
496 return FALSE; // cheat dragging is used instead
498 // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
499 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
500 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
501 // it goes out of range while slinging it around.
506 e = sandbox_EditObject_Get();
507 if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
510 if(Drag(e, grab)) // execute dragging
512 if(autocvar_g_sandbox_info)
513 print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
520 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
522 // unzone the player's clipboard if it's not empty
523 if(self.object_clipboard)
525 strunzone(self.object_clipboard);
526 self.object_clipboard = string_null;
532 MUTATOR_DEFINITION(sandbox)
534 MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
535 MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
536 MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);