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