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