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