]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
use stov instead of stof's where possible. The clipboard is still broken however
[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 value ^7- changes the skin of the object");
65                                 print_to(self, "^3alpha value ^7- sets object transparency");
66                                 print_to(self, "^3colormod \"value_x value_y value_z\" ^7- main object color");
67                                 print_to(self, "^3glowmod \"value_x value_y value_z\" ^7- glow object color");
68                                 print_to(self, "^3frame value ^7- object animation frame, for self-animated models");
69                                 print_to(self, "^3scale value ^7- changes object scale. 0.5 is half size and 2 is double size");
70                                 print_to(self, "^3physics value ^7- object physics, 0 = static, 1 = movable, 2 = physical");
71                                 print_to(self, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
72                                 return TRUE;
73
74                         // ---------------- COMMAND: SPAWN ITEM ----------------
75                         case "spawn_item":
76                                 // only weapons are currently supported
77
78                                 if(cmd_argc < 3)
79                                 {
80                                         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");
81                                         return TRUE;
82                                 }
83
84                                 // spawn a new item
85                                 float i;
86                                 makevectors(self.v_angle);
87                                 WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
88
89                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
90                                 {
91                                         e = get_weaponinfo(i);
92                                         if(e.netname == argv(2))
93                                         {
94                                                 W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
95                                                 if(autocvar_g_sandbox_info)
96                                                         print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
97                                                 return TRUE;
98                                         }
99                                 }
100
101                                 print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
102                                 return TRUE;
103
104                         // ---------------- COMMAND: SPAWN OBJECT ----------------
105                         case "spawn_object":
106                                 // don't allow spawning objects without a model
107                                 if(cmd_argc < 3)
108                                 {
109                                         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");
110                                         return TRUE;
111                                 }
112                                 else if not(fexists(argv(2)))
113                                 {
114                                         print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
115                                         return TRUE;
116                                 }
117
118                                 e = sandbox_SpawnObject();
119                                 setmodel(e, argv(2));
120
121                                 if(autocvar_g_sandbox_info)
122                                         print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
123
124                                 return TRUE;
125
126                         // ---------------- COMMAND: REMOVE OBJECT ----------------
127                         case "remove_object":
128                                 e = sandbox_EditObject();
129                                 if(e != world)
130                                 {
131                                         if(autocvar_g_sandbox_info)
132                                                 print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
133                                         remove(e);
134                                         e = world;
135                                         return TRUE;
136                                 }
137
138                                 print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
139                                 return TRUE;
140
141                         // ---------------- COMMAND: DUPLICATE OBJECT COPY ----------------
142                         case "duplicate_object_copy":
143                                 // copies customizable properties of the selected object to the clipboard
144
145                                 e = sandbox_EditObject(); // you can only copy objects you can edit, so this works
146                                 if(e != world)
147                                 {
148                                         if(self.object_clipboard)
149                                                 strunzone(self.object_clipboard);
150
151                                         // set clipboard properties
152                                         self.object_clipboard = strcat(e.model, " ");
153                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.skin), " ");
154                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.alpha), " ");
155                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", self.colormod), " ");
156                                         self.object_clipboard = strcat(self.object_clipboard, sprintf("\"%.9v\"", self.glowmod), " ");
157                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.frame), " ");
158                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.scale), " ");
159                                         self.object_clipboard = strcat(self.object_clipboard, ftos(e.movetype), " ");
160
161                                         self.object_clipboard = strzone(self.object_clipboard);
162                                         print_to(self, "Object copied to clipboard");
163                                         return TRUE;
164                                 }
165
166                                 print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
167                                 return TRUE;
168
169                         // ---------------- COMMAND: DUPLICATE OBJECT PASTE ----------------
170                         case "duplicate_object_paste":
171                                 // spawns a new object using the properties in the player's clipboard
172
173                                 if(!self.object_clipboard) // no object in clipboard
174                                 {
175                                         print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
176                                         return TRUE;
177                                 }
178
179                                 e = sandbox_SpawnObject();
180                                 tokenize_console(self.object_clipboard);
181
182                                 // apply clipboard properties
183                                 setmodel(e, argv(0));
184                                 e.skin = stof(argv(1));
185                                 e.alpha = stof(argv(2));
186                                 e.colormod = stov(argv(3));
187                                 e.glowmod = stov(argv(4));
188                                 e.frame = stof(argv(5));
189                                 e.scale = stof(argv(6));        setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
190                                 e.movetype = stof(argv(7));
191 dprint(strcat(argv(3), " --------\n"));
192                                 print_to(self, "Object pasted");
193                                 if(autocvar_g_sandbox_info)
194                                         print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
195
196                                 return TRUE;
197
198                         // ---------------- COMMAND: EDIT OBJECT ----------------
199                         case "edit_object":
200                                 if(!argv(2) || !argv(3))
201                                 {
202                                         print_to(self, "WARNING: Too few parameters. You must specify a property to edit, followed by its value");
203                                         return TRUE;
204                                 }
205
206                                 e = sandbox_EditObject();
207                                 if(e != world)
208                                 {
209                                         switch(argv(2))
210                                         {
211                                                 case "skin":
212                                                         e.skin = stof(argv(3));
213                                                         break;
214                                                 case "alpha":
215                                                         e.alpha = stof(argv(3));
216                                                         break;
217                                                 case "color_main":
218                                                         e.colormod = stov(argv(3));
219                                                         break;
220                                                 case "color_glow":
221                                                         e.glowmod = stov(argv(3));
222                                                         break;
223                                                 case "frame":
224                                                         e.frame = stof(argv(3));
225                                                         break;
226                                                 case "scale":
227                                                         e.scale = stof(argv(3));
228                                                         setsize(e, e.mins * e.scale, e.maxs * e.scale); // adapt bounding box size to model size
229                                                         break;
230                                                 case "physics":
231                                                         switch(argv(3))
232                                                         {
233                                                                 case "0": // static
234                                                                         e.movetype = MOVETYPE_NONE;
235                                                                         break;
236                                                                 case "1": // movable
237                                                                         e.movetype = MOVETYPE_TOSS;
238                                                                         break;
239                                                                 case "2": // physical
240                                                                         e.movetype = MOVETYPE_PHYSICS;
241                                                                         break;
242                                                                 default:
243                                                                         break;
244                                                         }
245                                                         break;
246                                                 default:
247                                                         print_to(self, "WARNING: Invalid object property. For usage information, type 'sandbox help'");
248                                                         break;
249                                         }
250
251                                         return TRUE;
252                                 }
253
254                                 print_to(self, "WARNING: Object could not be edited. Make sure you are facing an object that belongs to you");
255                                 return TRUE;
256
257                         // ---------------- COMMAND: DEFAULT ----------------
258                         default:
259                                 print_to(self, "Invalid command. For usage information, type 'sandbox help'");
260                                 return TRUE;
261                 }
262         }
263         return FALSE;
264 }
265
266 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
267 {
268         // if the player is close enough to their object, they can drag it
269
270         if(autocvar_sv_cheats)
271                 return FALSE; // cheat dragging is used instead
272
273         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
274         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
275         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
276         // it goes out of range while slinging it around.
277
278         entity e;
279         float grab;
280
281         e = sandbox_EditObject();
282         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
283                 grab = TRUE;
284
285         if(Drag(e, grab)) // execute dragging
286         {
287                 if(autocvar_g_sandbox_info)
288                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
289                 return TRUE;
290         }
291
292         return FALSE;
293 }
294
295 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
296 {
297         // unzone the player's clipboard if it's not empty
298         if(self.object_clipboard)
299         {
300                 strunzone(self.object_clipboard);
301                 self.object_clipboard = string_null;
302         }
303
304         return FALSE;
305 }
306
307 MUTATOR_DEFINITION(sandbox)
308 {
309         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
310         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
311         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
312
313         return FALSE;
314 }