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