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