1 .string object_clipboard;
7 void sandbox_Object_Touch()
9 // apply material impact effects
11 if(self.touch_timer > time)
12 return; // don't execute each frame
13 self.touch_timer = time + 0.1;
15 // make particle count and sound volume depend on impact speed
17 intensity = vlen(self.velocity) + vlen(other.velocity);
18 if(intensity) // avoid divisions by 0
19 intensity /= 2; // average the two velocities
20 if not(intensity >= autocvar_g_sandbox_object_material_velocity_min)
21 return; // impact not strong enough to do anything
22 // now offset intensity and apply it to the effects
23 intensity -= autocvar_g_sandbox_object_material_velocity_min; // start from minimum velocity, not actual velocity
24 intensity = bound(0, intensity * autocvar_g_sandbox_object_material_velocity_factor, 1);
26 sound(self, CH_TRIGGER, strcat("object/impact_", self.material, "_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE * intensity, ATTN_NORM);
27 pointparticles(particleeffectnum(strcat("impact_", self.material)), self.origin, '0 0 0', ceil(intensity * 10)); // allow a count from 1 to 10
30 entity sandbox_EditObject_Get()
32 // returns the traced entity if the player can edit it, and world if not
33 // attached objects are SOLID_NOT and don't risk getting traced
35 makevectors(self.v_angle);
36 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
37 if(trace_ent.classname == "object" && !(trace_ent.realowner != self && !autocvar_g_sandbox_editor_free))
43 void sandbox_EditObject_Scale(entity e, float f)
48 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
49 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
54 void sandbox_AttachObject_Set(entity e, entity parent, string s)
56 // attaches e to parent on string s
58 e.old_movetype = e.movetype; // persist this
60 e.movetype = MOVETYPE_FOLLOW;
62 e.takedamage = DAMAGE_NO;
64 setattachment(e, parent, s);
68 void sandbox_AttachObject_Remove(entity e)
70 // detaches any object attached to e
73 for(head = world; (head = find(head, classname, "object")); )
77 head.movetype = head.old_movetype; // revert to previous physics
78 head.solid = SOLID_BBOX;
79 head.takedamage = DAMAGE_AIM;
81 setattachment(head, world, "");
84 // objects reset origin and angles when detached, so apply the parent's to prevent teleporting
85 setorigin(head, e.origin);
86 head.angles = e.angles;
91 entity sandbox_SpawnObject()
93 // spawn a new object with default properties
98 e.classname = "object";
99 e.takedamage = DAMAGE_AIM;
100 e.damageforcescale = 1;
101 e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
102 e.movetype = MOVETYPE_TOSS;
107 e.touch = sandbox_Object_Touch;
109 // set origin and direction based on player position and view angle
110 makevectors(self.v_angle);
111 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
112 setorigin(e, trace_endpos);
113 e.angles_y = self.v_angle_y;
115 e.realowner.object_count += 1;
120 void sandbox_RemoveObject(entity e)
122 e.realowner.object_count -= 1;
128 string sandbox_Storage_Save(entity e)
130 // save object properties
133 s = strcat(e.model, " ");
134 s = strcat(s, ftos(e.skin), " ");
135 s = strcat(s, ftos(e.alpha), " ");
136 s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
137 s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
138 s = strcat(s, ftos(e.frame), " ");
139 s = strcat(s, ftos(e.scale), " ");
140 s = strcat(s, ftos(e.movetype), " ");
141 s = strcat(s, ftos(e.damageforcescale), " ");
142 s = strcat(s, e.material, " ");
147 void sandbox_Storage_Load(entity e, string s)
149 // load object properties
152 setmodel(e, argv(0));
153 e.skin = stof(argv(1));
154 e.alpha = stof(argv(2));
155 e.colormod = stov(argv(3));
156 e.glowmod = stov(argv(4));
157 e.frame = stof(argv(5));
158 sandbox_EditObject_Scale(e, stof(argv(6)));
159 e.movetype = stof(argv(7));
160 e.damageforcescale = stof(argv(8));
161 e.material = argv(9);
164 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
166 if(MUTATOR_RETURNVALUE) // command was already handled?
168 if(cmd_name == "g_sandbox")
172 print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
181 // ---------------- COMMAND: HELP ----------------
183 print_to(self, "You can use the following sandbox commands:");
184 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
185 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");
186 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
187 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
188 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
189 print_to(self, "^7\"^2attach_object ^3property value^7\" attaches one object to another. Players can only attach their own objects");
190 print_to(self, "^7Attachment properties for ^2attach_object^7:");
191 print_to(self, "^3get ^7- selects the object you are facing as the object to be attached");
192 print_to(self, "^3set value ^7- attaches the previously selected object to the object you are facing, on the specified bone");
193 print_to(self, "^3remove ^7- detaches all objects from the object you are facing");
194 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
195 print_to(self, "^7Object properties for ^2edit_object^7:");
196 print_to(self, "^3skin value ^7- changes the skin of the object");
197 print_to(self, "^3alpha value ^7- sets object transparency");
198 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
199 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
200 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
201 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
202 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
203 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
204 print_to(self, "^3material value ^7- sets the material of the object. Default materials are: metal, stone, wood, flesh");
205 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
208 // ---------------- COMMAND: SPAWN ITEM ----------------
210 // only weapons are currently supported
214 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");
219 makevectors(self.v_angle);
220 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
222 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
224 e = get_weaponinfo(i);
225 if(e.netname == argv(2))
227 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
228 if(autocvar_g_sandbox_info)
229 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
234 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
237 // ---------------- COMMAND: SPAWN OBJECT ----------------
239 if(self.object_count >= autocvar_g_sandbox_editor_maxobjects)
241 print_to(self, strcat("WARNING: Cannot spawn any more objects. Each player may have up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects at a time"));
246 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");
249 else if not(fexists(argv(2)))
251 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
255 e = sandbox_SpawnObject();
256 setmodel(e, argv(2));
258 if(autocvar_g_sandbox_info)
259 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
263 // ---------------- COMMAND: REMOVE OBJECT ----------------
264 case "remove_object":
265 e = sandbox_EditObject_Get();
268 if(autocvar_g_sandbox_info)
269 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
270 sandbox_RemoveObject(e);
274 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you (default)");
277 // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
278 case "duplicate_object_copy":
279 // copies customizable properties of the selected object to the clipboard
281 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
284 if(self.object_clipboard)
285 strunzone(self.object_clipboard);
286 self.object_clipboard = strzone(sandbox_Storage_Save(e));
288 print_to(self, "Object copied to clipboard");
292 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you (default)");
295 // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
296 case "duplicate_object_paste":
297 // spawns a new object using the properties in the player's clipboard
299 if(!self.object_clipboard) // no object in clipboard
301 print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
304 if(self.object_count >= autocvar_g_sandbox_editor_maxobjects)
306 print_to(self, strcat("WARNING: Cannot spawn any more objects. Each player may have up to ^3", ftos(autocvar_g_sandbox_editor_maxobjects), " ^7objects at a time"));
310 e = sandbox_SpawnObject();
311 sandbox_Storage_Load(e, self.object_clipboard);
313 print_to(self, "Object pasted successfully");
314 if(autocvar_g_sandbox_info)
315 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
319 // ---------------- COMMAND: ATTACH OBJECT ----------------
320 case "attach_object":
324 // select e as the object as meant to be attached
325 e = sandbox_EditObject_Get();
328 self.object_attach = e;
329 print_to(self, "Object selected for attachment");
332 print_to(self, "WARNING: Object could not be selected for attachment. Make sure you are facing an object that belongs to you (default)");
335 if(self.object_attach == world)
337 print_to(self, "WARNING: No object selected for attachment. Please select an object to be attached first.");
341 // attaches the previously selected object to e
342 e = sandbox_EditObject_Get();
345 sandbox_AttachObject_Set(self.object_attach, e, argv(3));
346 print_to(self, "Object attached successfully");
347 self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
350 print_to(self, "WARNING: Object could not be attached to the parent. Make sure you are facing an object that belongs to you (default)");
353 // removes e if it was attached
354 e = sandbox_EditObject_Get();
357 sandbox_AttachObject_Remove(e);
358 print_to(self, "Child objects detached successfully");
361 print_to(self, "WARNING: Child objects could not be detached. Make sure you are facing an object that belongs to you (default)");
366 // ---------------- COMMAND: EDIT OBJECT ----------------
368 if(!argv(2) || !argv(3))
370 print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
374 e = sandbox_EditObject_Get();
380 e.skin = stof(argv(3));
383 e.alpha = stof(argv(3));
386 e.colormod = stov(argv(3));
389 e.glowmod = stov(argv(3));
392 e.frame = stof(argv(3));
395 sandbox_EditObject_Scale(e, stof(argv(3)));
401 e.movetype = MOVETYPE_NONE;
404 e.movetype = MOVETYPE_TOSS;
406 case "2": // physical
407 e.movetype = MOVETYPE_PHYSICS;
414 e.damageforcescale = stof(argv(3));
417 for (i = 1; i <= 5; i++) // precache material sounds, 5 in total
418 precache_sound(strcat("object/impact_", argv(3), "_", ftos(i), ".ogg"));
419 e.material = argv(3);
422 print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
428 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you (default)");
431 // ---------------- COMMAND: DEFAULT ----------------
433 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
440 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
442 // if the player is close enough to their object, they can drag it
444 if(autocvar_sv_cheats)
445 return FALSE; // cheat dragging is used instead
447 // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
448 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
449 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
450 // it goes out of range while slinging it around.
455 e = sandbox_EditObject_Get();
456 if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
459 if(Drag(e, grab)) // execute dragging
461 if(autocvar_g_sandbox_info)
462 print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
469 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
471 // unzone the player's clipboard if it's not empty
472 if(self.object_clipboard)
474 strunzone(self.object_clipboard);
475 self.object_clipboard = string_null;
481 MUTATOR_DEFINITION(sandbox)
483 MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
484 MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
485 MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
490 for (i = 1; i <= 5; i++)
492 // precache material sounds
493 precache_sound(strcat("objects/impact_metal_", ftos(i), ".ogg"));
494 precache_sound(strcat("objects/impact_stone_", ftos(i), ".ogg"));
495 precache_sound(strcat("objects/impact_wood_", ftos(i), ".ogg"));
496 precache_sound(strcat("objects/impact_flesh_", ftos(i), ".ogg"));