]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
No need to use == "" or != "" checks for strings. Dunno why I remember those were...
[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_BSP;
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                         return TRUE;
59                 }
60                 else if(argv(1) == "spawn_item")
61                 {
62                         // only weapons are currently supported
63
64                         if(cmd_argc < 3)
65                         {
66                                 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");
67                                 return TRUE;
68                         }
69
70                         // spawn a new item
71                         float i;
72                         makevectors(self.v_angle);
73                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
74
75                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
76                         {
77                                 e = get_weaponinfo(i);
78                                 if(e.netname == argv(2))
79                                 {
80                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
81                                         if(autocvar_g_sandbox_info)
82                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
83                                         return TRUE;
84                                 }
85                         }
86
87                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
88                         return TRUE;
89                 }
90                 else if(argv(1) == "spawn_object")
91                 {
92                         // don't allow spawning objects without a model
93                         if(cmd_argc < 3)
94                         {
95                                 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");
96                                 return TRUE;
97                         }
98                         else if not(fexists(argv(2)))
99                         {
100                                 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
101                                 return TRUE;
102                         }
103
104                         e = sandbox_SpawnObject();
105                         setmodel(e, argv(2));
106
107                         if(autocvar_g_sandbox_info)
108                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
109
110                         return TRUE;
111                 }
112                 else if(argv(1) == "remove_object")
113                 {
114                         e = sandbox_EditObject();
115                         if(e != world)
116                         {
117                                 if(autocvar_g_sandbox_info)
118                                         print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
119                                 remove(e);
120                                 e = world;
121                                 return TRUE;
122                         }
123
124                         print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
125                         return TRUE;
126                 }
127                 else if(argv(1) == "duplicate_object_copy")
128                 {
129                         // copies customizable properties of the selected object to the clipboard
130
131                         e = sandbox_EditObject(); // you can only copy objects you can edit, so this works
132                         if(e != world)
133                         {
134                                 if(self.object_clipboard) // unzone the player's clipboard if it's not empty
135                                         strunzone(self.object_clipboard);
136                                 self.object_clipboard = strzone(strcat(e.model, " ", ftos(e.movetype)));
137
138                                 print_to(self, "Object copied to clipboard");
139                                 return TRUE;
140                         }
141
142                         print_to(self, "WARNING: Object could not be copied. Make sure you are facing an object that belongs to you");
143                         return TRUE;
144                 }
145                 else if(argv(1) == "duplicate_object_paste")
146                 {
147                         // spawns a new object using the properties in the player's clipboard
148
149                         if(!self.object_clipboard) // no object in clipboard
150                         {
151                                 print_to(self, "WARNING: No object in clipboard. You must copy an object before you can paste it");
152                                 return TRUE;
153                         }
154
155                         e = sandbox_SpawnObject();
156                         tokenize_console(self.object_clipboard);
157
158                         setmodel(e, argv(0));
159                         e.movetype = stof(argv(1));
160
161                         print_to(self, "Object pasted");
162                         if(autocvar_g_sandbox_info)
163                                 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
164
165                         return TRUE;
166                 }
167                 else
168                 {
169                         print_to(self, "Invalid command. For usage information, type 'sandbox help'");
170                         return TRUE;
171                 }
172         }
173         return FALSE;
174 }
175
176 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
177 {
178         // if the player is close enough to their object, they can drag it
179
180         if(autocvar_sv_cheats)
181                 return FALSE; // cheat dragging is used instead
182
183         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
184         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
185         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
186         // it goes out of range while slinging it around.
187
188         entity e;
189         float grab;
190
191         e = sandbox_EditObject();
192         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
193                 grab = TRUE;
194
195         if(Drag(e, grab)) // execute dragging
196         {
197                 if(autocvar_g_sandbox_info)
198                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
199                 return TRUE;
200         }
201
202         return FALSE;
203 }
204
205 MUTATOR_HOOKFUNCTION(sandbox_ClientDisconnect)
206 {
207         // unzone the player's clipboard if it's not empty
208         if(self.object_clipboard)
209         {
210                 strunzone(self.object_clipboard);
211                 self.object_clipboard = string_null;
212         }
213 }
214
215 MUTATOR_DEFINITION(sandbox)
216 {
217         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
218         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
219         MUTATOR_HOOK(ClientDisconnect, sandbox_ClientDisconnect, CBC_ORDER_ANY);
220
221         return 0;
222 }