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