]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
Object materials. They influence what sounds are generated when the object impacts...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 .string object_clipboard;
2 .float material;
3
4 const float MATERIAL_METAL = 0;
5 const float MATERIAL_STONE = 1;
6 const float MATERIAL_WOOD = 2;
7
8 entity sandbox_EditObject_Get()
9 {
10         // returns the traced entity if the player can edit it, and world if not
11
12         makevectors(self.v_angle);
13         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
14         if(trace_ent.classname == "object" && trace_ent.realowner == self)
15                 return trace_ent;
16         else
17                 return world;
18 }
19
20 void sandbox_EditObject_Scale(entity e, float f)
21 {
22         e.scale = f;
23         if(e.scale)
24         {
25                 e.scale = bound(autocvar_g_sandbox_object_scale_min, e.scale, autocvar_g_sandbox_object_scale_max);
26                 setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
27         }
28 }
29
30 void sandbox_Object_Touch()
31 {
32         switch(self.material)
33         {
34                 case MATERIAL_METAL:
35                         sound(self, CH_TRIGGER, strcat("object/impact_metal_", ftos(ceil(random() * 5)) , ".ogg"), VOL_BASE, ATTN_NORM);
36                         break;
37                 default:
38                         break;
39         }
40 }
41
42 entity sandbox_SpawnObject()
43 {
44         // spawn a new object with default properties
45
46         entity e;
47         e = spawn();
48         e.realowner = self;
49         e.classname = "object";
50         e.takedamage = DAMAGE_AIM;
51         e.damageforcescale = 1;
52         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
53         e.movetype = MOVETYPE_TOSS;
54         e.frame = 0;
55         e.skin = 0;
56         e.material = MATERIAL_METAL;
57
58         e.touch = sandbox_Object_Touch;
59
60         // set origin and direction based on player position and view angle
61         makevectors(self.v_angle);
62         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
63         setorigin(e, trace_endpos);
64         e.angles_y = self.v_angle_y;
65
66         return e;
67 }
68
69 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
70 {
71         if(MUTATOR_RETURNVALUE) // command was already handled?
72                 return FALSE;
73         if(cmd_name == "g_sandbox")
74         {
75                 if(cmd_argc < 2)
76                 {
77                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
78                         return TRUE;
79                 }
80
81                 switch(argv(1))
82                 {
83                         entity e;
84
85                         // ---------------- COMMAND: HELP ----------------
86                         case "help":
87                                 print_to(self, "You can use the following sandbox commands:");
88                                 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
89                                 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");
90                                 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
91                                 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
92                                 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
93                                 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
94                                 print_to(self, "^7Object properties for ^2edit_object^7:");
95                                 print_to(self, "^3skin value ^7- changes the skin of the object");
96                                 print_to(self, "^3alpha value ^7- sets object transparency");
97                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
98                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
99                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
100                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
101                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
102                                 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
103                                 print_to(self, "^3material value ^7- sets the material of the object. Valid materials are: 0 (metal), 1 (stone), 2 (wood)");
104                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
105                                 return TRUE;
106
107                         // ---------------- COMMAND: SPAWN ITEM ----------------
108                         case "spawn_item":
109                                 // only weapons are currently supported
110
111                                 if(cmd_argc < 3)
112                                 {
113                                         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");
114                                         return TRUE;
115                                 }
116
117                                 // spawn a new item
118                                 float i;
119                                 makevectors(self.v_angle);
120                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
121
122                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
123                                 {
124                                         e = get_weaponinfo(i);
125                                         if(e.netname == argv(2))
126                                         {
127                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
128                                                 if(autocvar_g_sandbox_info)
129                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
130                                                 return TRUE;
131                                         }
132                                 }
133
134                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
135                                 return TRUE;
136
137                         // ---------------- COMMAND: SPAWN OBJECT ----------------
138                         case "spawn_object":
139                                 // don't allow spawning objects without a model
140                                 if(cmd_argc < 3)
141                                 {
142                                         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");
143                                         return TRUE;
144                                 }
145                                 else if not(fexists(argv(2)))
146                                 {
147                                         print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
148                                         return TRUE;
149                                 }
150
151                                 e = sandbox_SpawnObject();
152                                 setmodel(e, argv(2));
153
154                                 if(autocvar_g_sandbox_info)
155                                         print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
156
157                                 return TRUE;
158
159                         // ---------------- COMMAND: REMOVE OBJECT ----------------
160                         case "remove_object":
161                                 e = sandbox_EditObject_Get();
162                                 if(e != world)
163                                 {
164                                         if(autocvar_g_sandbox_info)
165                                                 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
166                                         remove(e);
167                                         e = world;
168                                         return TRUE;
169                                 }
170
171                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
172                                 return TRUE;
173
174                         // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
175                         case "duplicate_object_copy":
176                                 // copies customizable properties of the selected object to the clipboard
177
178                                 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
179                                 if(e != world)
180                                 {
181                                         if(self.object_clipboard)
182                                                 strunzone(self.object_clipboard);
183
184                                         // set clipboard properties
185                                         self.object_clipboard = strcat(e.model, " ");
186                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.skin), " ");
187                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.alpha), " ");
188                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", e.colormod), " ");
189                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", e.glowmod), " ");
190                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.frame), " ");
191                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.scale), " ");
192                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.movetype), " ");
193                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.damageforcescale), " ");
194                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.material), " ");
195
196                                         self.object_clipboard = strzone(self.object_clipboard);
197                                         print_to(self, "Object copied to clipboard");
198                                         return TRUE;
199                                 }
200
201                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
202                                 return TRUE;
203
204                         // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
205                         case "duplicate_object_paste":
206                                 // spawns a new object using the properties in the player's clipboard
207
208                                 if(!self.object_clipboard) // no object in clipboard
209                                 {
210                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
211                                         return TRUE;
212                                 }
213
214                                 e = sandbox_SpawnObject();
215                                 tokenize_console(self.object_clipboard);
216
217                                 // apply clipboard properties
218                                 setmodel(e, argv(0));
219                                 e.skin = stof(argv(1));
220                                 e.alpha = stof(argv(2));
221                                 e.colormod = stov(argv(3));
222                                 e.glowmod = stov(argv(4));
223                                 e.frame = stof(argv(5));
224                                 sandbox_EditObject_Scale(e, stof(argv(6)));
225                                 e.movetype = stof(argv(7));
226                                 e.damageforcescale = stof(argv(8));
227                                 e.material = stof(argv(9));
228
229                                 print_to(self, "Object pasted");
230                                 if(autocvar_g_sandbox_info)
231                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
232
233                                 return TRUE;
234
235                         // ---------------- COMMAND: EDIT OBJECT ----------------
236                         case "edit_object":
237                                 if(!argv(2) || !argv(3))
238                                 {
239                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
240                                         return TRUE;
241                                 }
242
243                                 e = sandbox_EditObject_Get();
244                                 if(e != world)
245                                 {
246                                         switch(argv(2))
247                                         {
248                                                 case "skin":
249                                                         e.skin = stof(argv(3));
250                                                         break;
251                                                 case "alpha":
252                                                         e.alpha = stof(argv(3));
253                                                         break;
254                                                 case "color_main":
255                                                         e.colormod = stov(argv(3));
256                                                         break;
257                                                 case "color_glow":
258                                                         e.glowmod = stov(argv(3));
259                                                         break;
260                                                 case "frame":
261                                                         e.frame = stof(argv(3));
262                                                         break;
263                                                 case "scale":
264                                                         sandbox_EditObject_Scale(e, stof(argv(3)));
265                                                         break;
266                                                 case "physics":
267                                                         switch(argv(3))
268                                                         {
269                                                                 case "0": // static
270                                                                         e.movetype = MOVETYPE_NONE;
271                                                                         break;
272                                                                 case "1": // movable
273                                                                         e.movetype = MOVETYPE_TOSS;
274                                                                         break;
275                                                                 case "2": // physical
276                                                                         e.movetype = MOVETYPE_PHYSICS;
277                                                                         break;
278                                                                 default:
279                                                                         break;
280                                                         }
281                                                         break;
282                                                 case "force":
283                                                         e.damageforcescale = stof(argv(3));
284                                                         break;
285                                                 case "material":
286                                                         e.material = stof(argv(3));
287                                                         break;
288                                                 default:
289                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
290                                                         break;
291                                         }
292
293                                         return TRUE;
294                                 }
295
296                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
297                                 return TRUE;
298
299                         // ---------------- COMMAND: DEFAULT ----------------
300                         default:
301                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
302                                 return TRUE;
303                 }
304         }
305         return FALSE;
306 }
307
308 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
309 {
310         // if the player is close enough to their object, they can drag it
311
312         if(autocvar_sv_cheats)
313                 return FALSE; // cheat dragging is used instead
314
315         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
316         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
317         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
318         // it goes out of range while slinging it around.
319
320         entity e;
321         float grab;
322
323         e = sandbox_EditObject_Get();
324         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
325                 grab = TRUE;
326
327         if(Drag(e, grab)) // execute dragging
328         {
329                 if(autocvar_g_sandbox_info)
330                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
331                 return TRUE;
332         }
333
334         return FALSE;
335 }
336
337 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
338 {
339         // unzone the player's clipboard if it's not empty
340         if(self.object_clipboard)
341         {
342                 strunzone(self.object_clipboard);
343                 self.object_clipboard = string_null;
344         }
345
346         return FALSE;
347 }
348
349 MUTATOR_DEFINITION(sandbox)
350 {
351         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
352         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
353         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
354
355         MUTATOR_ONADD
356         {
357                 float i;
358                 for (i = 1; i <= 5; i++)
359                 {
360                         precache_sound(strcat("objects/impact_metal_", ftos(i), ".ogg"));
361                 }
362         }
363
364         return FALSE;
365 }
366