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