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