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