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