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