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