1 .string object_clipboard;
4 const float MATERIAL_NONE = 0;
5 const float MATERIAL_METAL = 1;
6 const float MATERIAL_STONE = 2;
7 const float MATERIAL_WOOD = 3;
8 const float MATERIAL_FLESH = 4;
11 void sandbox_Object_Touch()
13 // apply material impact effects
15 if(self.touch_timer > time)
16 return; // don't execute each frame
17 self.touch_timer = time + 0.1;
18 if not(vlen(self.velocity) >= autocvar_g_sandbox_object_matvel || vlen(other.velocity) >= autocvar_g_sandbox_object_matvel)
19 return; // impact not strong enough
24 sound(self, CH_TRIGGER, strcat("object/impact_metal_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE, ATTN_NORM);
27 sound(self, CH_TRIGGER, strcat("object/impact_stone_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE, ATTN_NORM);
30 sound(self, CH_TRIGGER, strcat("object/impact_wood_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE, ATTN_NORM);
33 sound(self, CH_TRIGGER, strcat("object/impact_flesh_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE, ATTN_NORM);
40 entity sandbox_EditObject_Get()
42 // returns the traced entity if the player can edit it, and world if not
44 makevectors(self.v_angle);
45 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
46 if(trace_ent.classname == "object" && trace_ent.realowner == self)
52 void sandbox_EditObject_Scale(entity e, float f)
57 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
58 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
62 entity sandbox_SpawnObject()
64 // spawn a new object with default properties
69 e.classname = "object";
70 e.takedamage = DAMAGE_AIM;
71 e.damageforcescale = 1;
72 e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
73 e.movetype = MOVETYPE_TOSS;
76 e.material = MATERIAL_NONE;
78 e.touch = sandbox_Object_Touch;
80 // set origin and direction based on player position and view angle
81 makevectors(self.v_angle);
82 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
83 setorigin(e, trace_endpos);
84 e.angles_y = self.v_angle_y;
89 string sandbox_Storage_Save(entity e)
91 // save object properties
94 s = strcat(e.model, " ");
95 s = strcat(s, ftos(e.skin), " ");
96 s = strcat(s, ftos(e.alpha), " ");
97 s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
98 s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
99 s = strcat(s, ftos(e.frame), " ");
100 s = strcat(s, ftos(e.scale), " ");
101 s = strcat(s, ftos(e.movetype), " ");
102 s = strcat(s, ftos(e.damageforcescale), " ");
103 s = strcat(s, ftos(e.material), " ");
108 void sandbox_Storage_Load(entity e, string s)
110 // load object properties
113 setmodel(e, argv(0));
114 e.skin = stof(argv(1));
115 e.alpha = stof(argv(2));
116 e.colormod = stov(argv(3));
117 e.glowmod = stov(argv(4));
118 e.frame = stof(argv(5));
119 sandbox_EditObject_Scale(e, stof(argv(6)));
120 e.movetype = stof(argv(7));
121 e.damageforcescale = stof(argv(8));
122 e.material = stof(argv(9));
125 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
127 if(MUTATOR_RETURNVALUE) // command was already handled?
129 if(cmd_name == "g_sandbox")
133 print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
141 // ---------------- COMMAND: HELP ----------------
143 print_to(self, "You can use the following sandbox commands:");
144 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
145 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");
146 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
147 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
148 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
149 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
150 print_to(self, "^7Object properties for ^2edit_object^7:");
151 print_to(self, "^3skin value ^7- changes the skin of the object");
152 print_to(self, "^3alpha value ^7- sets object transparency");
153 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
154 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
155 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
156 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
157 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
158 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
159 print_to(self, "^3material value ^7- sets the material of the object. Valid materials are: 1 (metal), 2 (stone), 3 (wood), 4 (flesh)");
160 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
163 // ---------------- COMMAND: SPAWN ITEM ----------------
165 // only weapons are currently supported
169 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");
175 makevectors(self.v_angle);
176 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
178 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
180 e = get_weaponinfo(i);
181 if(e.netname == argv(2))
183 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
184 if(autocvar_g_sandbox_info)
185 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
190 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
193 // ---------------- COMMAND: SPAWN OBJECT ----------------
195 // don't allow spawning objects without a model
198 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");
201 else if not(fexists(argv(2)))
203 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
207 e = sandbox_SpawnObject();
208 setmodel(e, argv(2));
210 if(autocvar_g_sandbox_info)
211 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
215 // ---------------- COMMAND: REMOVE OBJECT ----------------
216 case "remove_object":
217 e = sandbox_EditObject_Get();
220 if(autocvar_g_sandbox_info)
221 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
227 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
230 // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
231 case "duplicate_object_copy":
232 // copies customizable properties of the selected object to the clipboard
234 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
237 if(self.object_clipboard)
238 strunzone(self.object_clipboard);
239 self.object_clipboard = strzone(sandbox_Storage_Save(e));
241 print_to(self, "Object copied to clipboard");
245 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
248 // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
249 case "duplicate_object_paste":
250 // spawns a new object using the properties in the player's clipboard
252 if(!self.object_clipboard) // no object in clipboard
254 print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
258 e = sandbox_SpawnObject();
259 sandbox_Storage_Load(e, self.object_clipboard);
261 print_to(self, "Object pasted");
262 if(autocvar_g_sandbox_info)
263 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
267 // ---------------- COMMAND: EDIT OBJECT ----------------
269 if(!argv(2) || !argv(3))
271 print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
275 e = sandbox_EditObject_Get();
281 e.skin = stof(argv(3));
284 e.alpha = stof(argv(3));
287 e.colormod = stov(argv(3));
290 e.glowmod = stov(argv(3));
293 e.frame = stof(argv(3));
296 sandbox_EditObject_Scale(e, stof(argv(3)));
302 e.movetype = MOVETYPE_NONE;
305 e.movetype = MOVETYPE_TOSS;
307 case "2": // physical
308 e.movetype = MOVETYPE_PHYSICS;
315 e.damageforcescale = stof(argv(3));
318 e.material = stof(argv(3));
321 print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
328 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
331 // ---------------- COMMAND: DEFAULT ----------------
333 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
340 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
342 // if the player is close enough to their object, they can drag it
344 if(autocvar_sv_cheats)
345 return FALSE; // cheat dragging is used instead
347 // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
348 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
349 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
350 // it goes out of range while slinging it around.
355 e = sandbox_EditObject_Get();
356 if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
359 if(Drag(e, grab)) // execute dragging
361 if(autocvar_g_sandbox_info)
362 print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
369 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
371 // unzone the player's clipboard if it's not empty
372 if(self.object_clipboard)
374 strunzone(self.object_clipboard);
375 self.object_clipboard = string_null;
381 MUTATOR_DEFINITION(sandbox)
383 MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
384 MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
385 MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
390 for (i = 1; i <= 5; i++)
392 // precache material sounds
393 precache_sound(strcat("objects/impact_metal_", ftos(i), ".ogg"));
394 precache_sound(strcat("objects/impact_stone_", ftos(i), ".ogg"));
395 precache_sound(strcat("objects/impact_wood_", ftos(i), ".ogg"));
396 precache_sound(strcat("objects/impact_flesh_", ftos(i), ".ogg"));