]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/sandbox.qc
Allow copying and pasting of objects. This is done by saving each object's property...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / sandbox.qc
1 .string clipboard_model;
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 more information, use '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_object ^3models/foo/bar.md3^7\" spawns a new object in front of the player, and gives it the specified model");
53                         print_to(self, "^7\"^2spawn_item ^3item^7\" spawns the specified item in front of the player. Only weapons are currently supported");
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, "^7The ^1drag object ^7key can be used to grab and carry objects. Players can only grab their own objects");
56                         return TRUE;
57                 }
58                 else if(argv(1) == "spawn_object")
59                 {
60                         // don't allow spawning objects without a model
61                         if(cmd_argc < 3)
62                         {
63                                 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");
64                                 return TRUE;
65                         }
66                         else if not(fexists(argv(2)))
67                         {
68                                 print_to(self, "WARNING: Attempted to spawn an object with a non-existent model. Make sure the path to your model file is correct");
69                                 return TRUE;
70                         }
71
72                         e = sandbox_SpawnObject();
73                         setmodel(e, argv(2));
74
75                         if(autocvar_g_sandbox_info)
76                                 print(strcat(self.netname, " spawned an object at origin ", vtos(e.origin), "\n"));
77
78                         return TRUE;
79                 }
80                 else if(argv(1) == "spawn_item")
81                 {
82                         // only weapons are currently supported
83
84                         if(cmd_argc < 3)
85                         {
86                                 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");
87                                 return TRUE;
88                         }
89
90                         // spawn a new item
91                         float i;
92                         makevectors(self.v_angle);
93                         WarpZone_TraceLine(self.origin + self.view_ofs, self.origin + self.view_ofs + v_forward * autocvar_g_sandbox_editor_distance_spawn, MOVE_NOMONSTERS, self);
94
95                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
96                         {
97                                 e = get_weaponinfo(i);
98                                 if(e.netname == argv(2))
99                                 {
100                                         W_ThrowNewWeapon(self, i, FALSE, trace_endpos, '0 0 0');
101                                         if(autocvar_g_sandbox_info)
102                                                 print(strcat(self.netname, " spawned a ^2", e.netname, "^7 at origin ", vtos(e.origin), "\n"));
103                                         return TRUE;
104                                 }
105                         }
106
107                         print_to(self, "WARNING: Attempted to spawn an invalid or unsupported item. See 'sandbox help' for allowed items");
108                         return TRUE;
109                 }
110                 else if(argv(1) == "remove_object")
111                 {
112                         e = sandbox_EditObject();
113                         if(e != world)
114                         {
115                                 if(autocvar_g_sandbox_info)
116                                         print(strcat(self.netname, " removed an object at origin ", vtos(e.origin), "\n"));
117                                 remove(e);
118                                 e = world;
119                                 return TRUE;
120                         }
121
122                         print_to(self, "WARNING: Object could not be removed. Make sure you are facing an object that belongs to you");
123                         return TRUE;
124                 }
125                 else if(argv(1) == "duplicate_copy")
126                 {
127                         // copies the properties of the selected object to the clipboard
128
129                         e = sandbox_EditObject(); // you can only copy objects you can edit, so this works
130                         if(e != world)
131                         {
132                                 // -------- COPY PROPERTIES --------
133                                 self.clipboard_model = e.model;
134                                 // -------- COPY PROPERTIES --------
135
136                                 print_to(self, "Object copied to clipboard");
137                         }
138                         return TRUE;
139                 }
140                 else if(argv(1) == "duplicate_paste")
141                 {
142                         // spawns an object with the properties in the player's clipboard
143
144                         e = sandbox_SpawnObject();
145
146                         // -------- PASTE PROPERTIES --------
147                         setmodel(e, self.clipboard_model);
148                         // -------- PASTE PROPERTIES --------
149
150                         print_to(self, "Object pasted");
151                         if(autocvar_g_sandbox_info)
152                                 print(strcat(self.netname, " pasted an object at origin ", vtos(e.origin), "\n"));
153
154                         return TRUE;
155                 }
156         }
157         return FALSE;
158 }
159
160 MUTATOR_HOOKFUNCTION(sandbox_PlayerPreThink)
161 {
162         // if the player is close enough to their object, they can drag it
163
164         if(autocvar_sv_cheats)
165                 return FALSE; // cheat dragging is used instead
166
167         // grab is TRUE if the object can be picked up. While an object is being carried, the Drag() function
168         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
169         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
170         // it goes out of range while slinging it around.
171
172         entity e;
173         float grab;
174
175         e = sandbox_EditObject();
176         if(e != world && vlen(e.origin - self.origin) <= autocvar_g_sandbox_editor_distance_edit)
177                 grab = TRUE;
178
179         if(Drag(e, grab)) // execute dragging
180         {
181                 if(autocvar_g_sandbox_info)
182                         print(strcat(self.netname, " grabbed an object at origin ", vtos(e.origin), "\n"));
183                 return TRUE;
184         }
185
186         return FALSE;
187 }
188
189 MUTATOR_DEFINITION(sandbox)
190 {
191         MUTATOR_HOOK(SV_ParseClientCommand, sandbox_PlayerCommand, CBC_ORDER_ANY);
192         MUTATOR_HOOK(PlayerPreThink, sandbox_PlayerPreThink, CBC_ORDER_ANY);
193
194         return 0;
195 }