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