]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
0b472ad6ac689ccfb5b7b0f2bc983c76d831382d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 .string object_clipboard;
2
3 entity sandbox_EditObject()
4 {
5         // returns the traced entity if the player can edit it, and world if not
6
7         makevectors(self.v_angle);
8         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_edit, MOVE_NORMAL, self);
9         if(trace_ent.classname == "object" && trace_ent.realowner == self)
10                 return trace_ent;
11         else
12                 return world;
13 }
14
15 entity sandbox_SpawnObject()
16 {
17         // spawn a new object with default properties
18
19         entity e;
20         e = spawn();
21         e.realowner = self;
22         e.classname = "object";
23         e.takedamage = DAMAGE_AIM;
24         e.solid = SOLID_BBOX; // SOLID_BSP would be best, but can lag the server badly
25         e.movetype = MOVETYPE_TOSS;
26         e.frame = 0;
27         e.skin = 0;
28
29         // set origin and direction based on player position and view angle
30         makevectors(self.v_angle);
31         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NORMAL, self);
32         setorigin(e, trace_endpos);
33         e.angles_y = self.v_angle_y;
34
35         return e;
36 }
37
38 MUTATOR_HOOKFUNCTION(sandbox_PlayerCommand)
39 {
40         if(MUTATOR_RETURNVALUE) // command was already handled?
41                 return FALSE;
42         if(cmd_name == "g_sandbox")
43         {
44                 if(cmd_argc < 2)
45                 {
46                         print_to(self, "Sandbox mode is active. For usage information, type 'sandbox help'");
47                         return TRUE;
48                 }
49
50                 switch(argv(1))
51                 {
52                         entity e;
53
54                         // ---------------- COMMAND: HELP ----------------
55                         case "help":
56                                 print_to(self, "You can use the following sandbox commands:");
57                                 print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
58                                 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");
59                                 print_to(self, "^7\"^2remove_object^7\" removes the object the player is looking at. Players can only remove their own objects");
60                                 print_to(self, "^7\"^2duplicate_object_copy^7\" copies the object the player is looking at. Players can only copy their own objects");
61                                 print_to(self, "^7\"^2duplicate_object_paste^7\" pastes the copied object in front of the player");
62                                 print_to(self, "^7\"^2edit_object ^3property value^7\" edits the given property of the object. Players can only edit their own objects");
63                                 print_to(self, "^7Object properties for ^2edit_object^7:");
64                                 print_to(self, "^3skin ^7- changes the skin of the object");
65                                 print_to(self, "^3frame ^7- object animation frame, for self-animated models");
66                                 print_to(self, "^3physics ^7- object physics, 0 = static, 1 = movable, 2 = physical");
67                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
68                                 return TRUE;
69
70                         // ---------------- COMMAND: SPAWN ITEM ----------------
71                         case "spawn_item":
72                                 // only weapons are currently supported
73
74                                 if(cmd_argc < 3)
75                                 {
76                                         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");
77                                         return TRUE;
78                                 }
79
80                                 // spawn a new item
81                                 float i;
82                                 makevectors(self.v_angle);
83                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
84
85                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
86                                 {
87                                         e = get_weaponinfo(i);
88                                         if(e.netname == argv(2))
89                                         {
90                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
91                                                 if(autocvar_g_sandbox_info)
92                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
93                                                 return TRUE;
94                                         }
95                                 }
96
97                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
98                                 return TRUE;
99
100                         // ---------------- COMMAND: SPAWN OBJECT ----------------
101                         case "spawn_object":
102                                 // don't allow spawning objects without a model
103                                 if(cmd_argc < 3)
104                                 {
105                                         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");
106                                         return TRUE;
107                                 }
108                                 else if not(fexists(argv(2)))
109                                 {
110                                         print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
111                                         return TRUE;
112                                 }
113
114                                 e = sandbox_SpawnObject();
115                                 setmodel(e, argv(2));
116
117                                 if(autocvar_g_sandbox_info)
118                                         print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
119
120                                 return TRUE;
121
122                         // ---------------- COMMAND: REMOVE OBJECT ----------------
123                         case "remove_object":
124                                 e = sandbox_EditObject();
125                                 if(e != world)
126                                 {
127                                         if(autocvar_g_sandbox_info)
128                                                 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
129                                         remove(e);
130                                         e = world;
131                                         return TRUE;
132                                 }
133
134                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
135                                 return TRUE;
136
137                         // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
138                         case "duplicate_object_copy":
139                                 // copies customizable properties of the selected object to the clipboard
140
141                                 e = sandbox_EditObject(); // you can only copy objects you can edit, so this works
142                                 if(e != world)
143                                 {
144                                         // set clipboard properties
145                                         if(self.object_clipboard)
146                                                 strunzone(self.object_clipboard);
147                                         self.object_clipboard = strzone(strcat(e.model, " ", ftos(e.skin), " ", ftos(e.frame), " ", ftos(e.movetype)));
148
149                                         print_to(self, "Object copied to clipboard");
150                                         return TRUE;
151                                 }
152
153                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
154                                 return TRUE;
155
156                         // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
157                         case "duplicate_object_paste":
158                                 // spawns a new object using the properties in the player's clipboard
159
160                                 if(!self.object_clipboard) // no object in clipboard
161                                 {
162                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
163                                         return TRUE;
164                                 }
165
166                                 e = sandbox_SpawnObject();
167                                 tokenize_console(self.object_clipboard);
168
169                                 // apply clipboard properties
170                                 setmodel(e, argv(0));
171                                 e.skin = stof(argv(1));
172                                 e.frame = stof(argv(2));
173                                 e.movetype = stof(argv(3));
174
175                                 print_to(self, "Object pasted");
176                                 if(autocvar_g_sandbox_info)
177                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
178
179                                 return TRUE;
180
181                         // ---------------- COMMAND: EDIT OBJECT ----------------
182                         case "edit_object":
183                                 if(!argv(2) || !argv(3))
184                                 {
185                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
186                                         return TRUE;
187                                 }
188
189                                 e = sandbox_EditObject();
190                                 if(e != world)
191                                 {
192                                         switch(argv(2))
193                                         {
194                                                 case "skin":
195                                                         e.skin = stof(argv(3));
196                                                         break;
197                                                 case "frame":
198                                                         e.frame = stof(argv(3));
199                                                         break;
200                                                 case "physics":
201                                                         if(argv(3) == "0") // static
202                                                                 e.movetype = MOVETYPE_NONE;
203                                                         else if(argv(3) == "1") // movable
204                                                                 e.movetype = MOVETYPE_TOSS;
205                                                         else if(argv(3) == "2") // physical
206                                                                 e.movetype = MOVETYPE_PHYSICS;
207                                                         break;
208                                                 default:
209                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
210                                                         break;
211                                         }
212
213                                         return TRUE;
214                                 }
215
216                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
217                                 return TRUE;
218
219                         // ---------------- COMMAND: DEFAULT ----------------
220                         default:
221                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
222                                 return TRUE;
223                 }
224         }
225         return FALSE;
226 }
227
228 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
229 {
230         // if the player is close enough to their object, they can drag it
231
232         if(autocvar_sv_cheats)
233                 return FALSE; // cheat dragging is used instead
234
235         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
236         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
237         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
238         // it goes out of range while slinging it around.
239
240         entity e;
241         float grab;
242
243         e = sandbox_EditObject();
244         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
245                 grab = TRUE;
246
247         if(Drag(e, grab)) // execute dragging
248         {
249                 if(autocvar_g_sandbox_info)
250                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
251                 return TRUE;
252         }
253
254         return FALSE;
255 }
256
257 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
258 {
259         // unzone the player's clipboard if it's not empty
260         if(self.object_clipboard)
261         {
262                 strunzone(self.object_clipboard);
263                 self.object_clipboard = string_null;
264         }
265
266         return FALSE;
267 }
268
269 MUTATOR_DEFINITION(sandbox)
270 {
271         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
272         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
273         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
274
275         return FALSE;
276 }