]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
077952db57de6eaeb90af4034867ed33c645442a
[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 void sandbox_AttachObject_Set(entity e, entity parent, string s)
78 {
79         // attaches e to parent on string s
80
81         e.movetype = MOVETYPE_FOLLOW;
82         e.solid = SOLID_NOT;
83         e.takedamage = DAMAGE_NO;
84
85         setorigin(e, parent.origin);
86         setattachment(e, parent, s);
87         e.owner = parent;
88 }
89
90 void sandbox_AttachObject_Remove(entity e)
91 {
92         // detaches any object attached to e
93
94         entity head;
95         for(head = world; (head = find(head, classname, "object")); )
96         {
97                 if(head.owner == e)
98                 {
99                         head.movetype = MOVETYPE_TOSS;
100                         head.solid = SOLID_BBOX;
101                         head.takedamage = DAMAGE_AIM;
102
103                         setattachment(head, world, "");
104                         setorigin(head, e.origin); // prevents a bug
105                         head.owner = world;
106                 }
107         }
108 }
109
110 entity sandbox_SpawnObject()
111 {
112         // spawn a new object with default properties
113
114         entity e;
115         e = spawn();
116         e.realowner = self;
117         e.classname = "object";
118         e.takedamage = DAMAGE_AIM;
119         e.damageforcescale = 1;
120         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
121         e.movetype = MOVETYPE_TOSS;
122         e.frame = 0;
123         e.skin = 0;
124         e.material = MATERIAL_NONE;
125
126         e.touch = sandbox_Object_Touch;
127
128         // set origin and direction based on player position and view angle
129         makevectors(self.v_angle);
130         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
131         setorigin(e, trace_endpos);
132         e.angles_y = self.v_angle_y;
133
134         return e;
135 }
136
137 string sandbox_Storage_Save(entity e)
138 {
139         // save object properties
140         string s;
141
142         s = strcat(e.model, " ");
143         s = strcat(s, ftos(e.skin), " ");
144         s = strcat(s, ftos(e.alpha), " ");
145         s = strcat(s, sprintf("\"%.9v\"", e.colormod), " ");
146         s = strcat(s, sprintf("\"%.9v\"", e.glowmod), " ");
147         s = strcat(s, ftos(e.frame), " ");
148         s = strcat(s, ftos(e.scale), " ");
149         s = strcat(s, ftos(e.movetype), " ");
150         s = strcat(s, ftos(e.damageforcescale), " ");
151         s = strcat(s, ftos(e.material), " ");
152
153         return s;
154 }
155
156 void sandbox_Storage_Load(entity e, string s)
157 {
158         // load object properties
159         tokenize_console(s);
160
161         setmodel(e, argv(0));
162         e.skin = stof(argv(1));
163         e.alpha = stof(argv(2));
164         e.colormod = stov(argv(3));
165         e.glowmod = stov(argv(4));
166         e.frame = stof(argv(5));
167         sandbox_EditObject_Scale(e, stof(argv(6)));
168         e.movetype = stof(argv(7));
169         e.damageforcescale = stof(argv(8));
170         e.material = stof(argv(9));
171 }
172
173 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
174 {
175         if(MUTATOR_RETURNVALUE) // command was already handled?
176                 return FALSE;
177         if(cmd_name == "g_sandbox")
178         {
179                 if(cmd_argc < 2)
180                 {
181                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
182                         return TRUE;
183                 }
184
185                 switch(argv(1))
186                 {
187                         entity e;
188
189                         // ---------------- COMMAND: HELP ----------------
190                         case "help":
191                                 print_to(self, "You can use the following sandbox commands:");
192                                 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
193                                 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");
194                                 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
195                                 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
196                                 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
197                                 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
198                                 print_to(self, "^7Object properties for ^2edit_object^7:");
199                                 print_to(self, "^3skin value ^7- changes the skin of the object");
200                                 print_to(self, "^3alpha value ^7- sets object transparency");
201                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
202                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
203                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
204                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
205                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
206                                 print_to(self, "^3force value ^7- amount of force applied to objects that are shot");
207                                 print_to(self, "^3material value ^7- sets the material of the object. Valid materials are: 1 (metal), 2 (stone), 3 (wood), 4 (flesh)");
208                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
209                                 return TRUE;
210
211                         // ---------------- COMMAND: SPAWN ITEM ----------------
212                         case "spawn_item":
213                                 // only weapons are currently supported
214
215                                 if(cmd_argc < 3)
216                                 {
217                                         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");
218                                         return TRUE;
219                                 }
220
221                                 // spawn a new item
222                                 float i;
223                                 makevectors(self.v_angle);
224                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
225
226                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
227                                 {
228                                         e = get_weaponinfo(i);
229                                         if(e.netname == argv(2))
230                                         {
231                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
232                                                 if(autocvar_g_sandbox_info)
233                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
234                                                 return TRUE;
235                                         }
236                                 }
237
238                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
239                                 return TRUE;
240
241                         // ---------------- COMMAND: SPAWN OBJECT ----------------
242                         case "spawn_object":
243                                 // don't allow spawning objects without a model
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                                         remove(e);
271                                         e = world;
272                                         return TRUE;
273                                 }
274
275                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
276                                 return TRUE;
277
278                         // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
279                         case "duplicate_object_copy":
280                                 // copies customizable properties of the selected object to the clipboard
281
282                                 e = sandbox_EditObject_Get(); // you can only copy objects you can edit, so this works
283                                 if(e != world)
284                                 {
285                                         if(self.object_clipboard)
286                                                 strunzone(self.object_clipboard);
287                                         self.object_clipboard = strzone(sandbox_Storage_Save(e));
288
289                                         print_to(self, "Object copied to clipboard");
290                                         return TRUE;
291                                 }
292
293                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
294                                 return TRUE;
295
296                         // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
297                         case "duplicate_object_paste":
298                                 // spawns a new object using the properties in the player's clipboard
299
300                                 if(!self.object_clipboard) // no object in clipboard
301                                 {
302                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
303                                         return TRUE;
304                                 }
305
306                                 e = sandbox_SpawnObject();
307                                 sandbox_Storage_Load(e, self.object_clipboard);
308
309                                 print_to(self, "Object pasted successfully");
310                                 if(autocvar_g_sandbox_info)
311                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
312
313                                 return TRUE;
314
315                         // ---------------- COMMAND: ATTACH OBJECT ----------------
316                         case "attach_object":
317                                 switch(argv(2))
318                                 {
319                                         case "get":
320                                                 // select e as the object as meant to be attached
321                                                 e = sandbox_EditObject_Get();
322                                                 if(e != world)
323                                                 {
324                                                         self.object_attach = e;
325                                                         print_to(self, "Object selected for attachment");
326                                                         return TRUE;
327                                                 }
328                                                 print_to(self, "WARNING: Object could not be selected for attachment. Make sure you are facing an object that belongs to you");
329                                                 return TRUE;
330                                         case "set":
331                                                 if(self.object_attach == world)
332                                                 {
333                                                         print_to(self, "WARNING: No object selected for attachment. Please select an object to be attached first.");
334                                                         return TRUE;
335                                                 }
336
337                                                 // attaches the previously selected object to e
338                                                 e = sandbox_EditObject_Get();
339                                                 if(e != world)
340                                                 {
341                                                         sandbox_AttachObject_Set(self.object_attach, e, argv(3));
342                                                         print_to(self, "Object attached successfully");
343                                                         self.object_attach = world; // object was attached, no longer keep it scheduled for attachment
344                                                         return TRUE;
345                                                 }
346                                                 print_to(self, "WARNING: Object could not be attached to the parent. Make sure you are facing an object that belongs to you");
347                                                 return TRUE;
348                                         case "remove":
349                                                 // removes e if it was attached
350                                                 e = sandbox_EditObject_Get();
351                                                 if(e != world)
352                                                 {
353                                                         sandbox_AttachObject_Remove(e);
354                                                         print_to(self, "Child objects detached successfully");
355                                                         return TRUE;
356                                                 }
357                                                 print_to(self, "WARNING: Child objects could not be detached. Make sure you are facing an object that belongs to you");
358                                                 return TRUE;
359                                 }
360                                 return TRUE;
361
362                         // ---------------- COMMAND: EDIT OBJECT ----------------
363                         case "edit_object":
364                                 if(!argv(2) || !argv(3))
365                                 {
366                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
367                                         return TRUE;
368                                 }
369
370                                 e = sandbox_EditObject_Get();
371                                 if(e != world)
372                                 {
373                                         switch(argv(2))
374                                         {
375                                                 case "skin":
376                                                         e.skin = stof(argv(3));
377                                                         break;
378                                                 case "alpha":
379                                                         e.alpha = stof(argv(3));
380                                                         break;
381                                                 case "color_main":
382                                                         e.colormod = stov(argv(3));
383                                                         break;
384                                                 case "color_glow":
385                                                         e.glowmod = stov(argv(3));
386                                                         break;
387                                                 case "frame":
388                                                         e.frame = stof(argv(3));
389                                                         break;
390                                                 case "scale":
391                                                         sandbox_EditObject_Scale(e, stof(argv(3)));
392                                                         break;
393                                                 case "physics":
394                                                         switch(argv(3))
395                                                         {
396                                                                 case "0": // static
397                                                                         e.movetype = MOVETYPE_NONE;
398                                                                         break;
399                                                                 case "1": // movable
400                                                                         e.movetype = MOVETYPE_TOSS;
401                                                                         break;
402                                                                 case "2": // physical
403                                                                         e.movetype = MOVETYPE_PHYSICS;
404                                                                         break;
405                                                                 default:
406                                                                         break;
407                                                         }
408                                                         break;
409                                                 case "force":
410                                                         e.damageforcescale = stof(argv(3));
411                                                         break;
412                                                 case "material":
413                                                         e.material = stof(argv(3));
414                                                         break;
415                                                 default:
416                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
417                                                         break;
418                                         }
419                                         return TRUE;
420                                 }
421
422                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
423                                 return TRUE;
424
425                         // ---------------- COMMAND: DEFAULT ----------------
426                         default:
427                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
428                                 return TRUE;
429                 }
430         }
431         return FALSE;
432 }
433
434 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
435 {
436         // if the player is close enough to their object, they can drag it
437
438         if(autocvar_sv_cheats)
439                 return FALSE; // cheat dragging is used instead
440
441         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
442         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
443         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
444         // it goes out of range while slinging it around.
445
446         entity e;
447         float grab;
448
449         e = sandbox_EditObject_Get();
450         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
451                 grab = TRUE;
452
453         if(Drag(e, grab)) // execute dragging
454         {
455                 if(autocvar_g_sandbox_info)
456                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
457                 return TRUE;
458         }
459
460         return FALSE;
461 }
462
463 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
464 {
465         // unzone the player's clipboard if it's not empty
466         if(self.object_clipboard)
467         {
468                 strunzone(self.object_clipboard);
469                 self.object_clipboard = string_null;
470         }
471
472         return FALSE;
473 }
474
475 MUTATOR_DEFINITION(sandbox)
476 {
477         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
478         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
479         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
480
481         MUTATOR_ONADD
482         {
483                 float i;
484                 for (i = 1; i <= 5; i++)
485                 {
486                         // precache material sounds
487                         precache_sound(strcat("objects/impact_metal_", ftos(i), ".ogg"));
488                         precache_sound(strcat("objects/impact_stone_", ftos(i), ".ogg"));
489                         precache_sound(strcat("objects/impact_wood_", ftos(i), ".ogg"));
490                         precache_sound(strcat("objects/impact_flesh_", ftos(i), ".ogg"));
491                 }
492         }
493
494         return FALSE;
495 }
496