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