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