move box dragging to cheats.qc too
[divverent/nexuiz.git] / data / qcsrc / server / cheats.qc
1 void CopyBody(float keepvelocity);
2
3 .float maycheat;
4 float sv_cheats;
5 float autocvar_sv_cheats;
6
7 #define CHIMPULSE_SPEEDRUN_INIT 30
8 #define CHIMPULSE_GIVE_ALL 99
9 #define CHIMPULSE_CLONE_MOVING 140
10 #define CHIMPULSE_SPEEDRUN 141
11 #define CHIMPULSE_CLONE_STANDING 142
12 #define CHIMPULSE_TELEPORT 143
13 #define CHIMPULSE_R00T 148
14
15 #define CHRAME_DRAG 1
16
17 float CheatsAllowed(float i, float argc, float fr) // the cheat gets passed as argument for possible future ACL checking
18 {
19         if(i == CHIMPULSE_CLONE_MOVING || i == CHIMPULSE_CLONE_STANDING)
20                 if(self.lip < sv_clones)
21                         return 1;
22         if(self.maycheat)
23                 return 1;
24         if(sv_cheats && autocvar_sv_cheats)
25                 return 1;
26         if(autocvar_sv_cheats)
27                 sprint(self, "Cheats are not yet allowed, and will be activated at next map change.\n");
28         return 0;
29 }
30
31 float CheatImpulse(float i)
32 {
33         if not(CheatsAllowed(i, 0, 0))
34                 return 0;
35         switch(i)
36         {
37                 float wep;
38                 entity e, e2;
39                 vector org;
40
41                 case CHIMPULSE_SPEEDRUN_INIT: // deploy personal waypoint
42                         if(!self.personal)
43                         {
44                                 self.personal = spawn();
45                                 self.personal.classname = "personal_wp";
46                         }
47                         self.personal.origin = self.origin;
48                         self.personal.v_angle = self.v_angle;
49                         self.personal.velocity = self.velocity;
50                         self.personal.ammo_rockets = self.ammo_rockets;
51                         self.personal.ammo_nails = self.ammo_nails;
52                         self.personal.ammo_cells = self.ammo_cells;
53                         self.personal.ammo_shells = self.ammo_shells;
54                         self.personal.ammo_fuel = self.ammo_fuel;
55                         self.personal.health = self.health;
56                         self.personal.armorvalue = self.armorvalue;
57                         self.personal.weapons = self.weapons;
58                         self.personal.items = self.items;
59                         self.personal.pauserotarmor_finished = self.pauserotarmor_finished;
60                         self.personal.pauserothealth_finished = self.pauserothealth_finished;
61                         self.personal.pauserotfuel_finished = self.pauserotfuel_finished;
62                         self.personal.pauseregen_finished = self.pauseregen_finished;
63                         self.personal.strength_finished = self.strength_finished;
64                         self.personal.invincible_finished = self.invincible_finished;
65                         self.personal.teleport_time = time;
66                         return 1;
67                 case CHIMPULSE_CLONE_MOVING:
68                         makevectors (self.v_angle);
69                         self.velocity = self.velocity + v_forward * 300;
70                         CopyBody(1);
71                         self.lip += 1;
72                         self.velocity = self.velocity - v_forward * 300;
73                         return 1;
74                 case CHIMPULSE_CLONE_STANDING:
75                         CopyBody(0);
76                         self.lip += 1;
77                         return 1;
78                 case CHIMPULSE_GIVE_ALL:
79                         self.weapons |= WEPBIT_ALL;
80                         self.items |= IT_UNLIMITED_AMMO;
81                         self.ammo_shells = g_pickup_shells_max;
82                         self.ammo_nails = g_pickup_nails_max;
83                         self.ammo_rockets = g_pickup_rockets_max;
84                         self.ammo_cells = g_pickup_cells_max;
85                         self.ammo_fuel = g_pickup_fuel_max;
86                         self.health = g_pickup_healthsmall_max;
87                         self.armorvalue = g_pickup_armorsmall_max;
88                         self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
89                         self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
90                         self.pauserotfuel_finished = time + cvar("g_balance_pause_fuel_rot_spawn");
91                         self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
92                         // precache weapon models/sounds
93                         wep = WEP_FIRST;
94                         while (wep <= WEP_LAST)
95                         {
96                                 weapon_action(wep, WR_PRECACHE);
97                                 wep = wep + 1;
98                         }
99                         return 1;
100                 case CHIMPULSE_SPEEDRUN:
101                         if(self.personal)
102                         {
103                                 self.speedrunning = TRUE;
104                                 tracebox(self.personal.origin, self.mins, self.maxs, self.personal.origin, MOVE_WORLDONLY, self);
105                                 if(trace_startsolid)
106                                 {
107                                         sprint(self, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
108                                 }
109                                 else
110                                 {
111                                         // Abort speedrun, teleport back
112                                         setorigin(self, self.personal.origin);
113                                         self.oldvelocity = self.velocity = self.personal.velocity;
114                                         self.angles = self.personal.v_angle;
115                                         self.fixangle = TRUE;
116                                         if(self.flagcarried)
117                                         {
118                                                 bprint("The ", self.flagcarried.netname, " was returned to base by its carrier\n");
119                                                 ReturnFlag(self.flagcarried);
120                                         }
121                                 }
122                                 if(g_ctf)
123                                 {
124                                         self.ammo_rockets = 999;
125                                         self.ammo_nails = 999;
126                                         self.ammo_cells = 999;
127                                         self.ammo_shells = 999;
128                                         self.ammo_fuel = 999;
129                                         self.health = start_health;
130                                         self.armorvalue = start_armorvalue;
131                                         self.weapons |= weaponsInMap;
132                                         self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
133                                         self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
134                                         self.pauserotfuel_finished = time + cvar("g_balance_pause_fuel_rot_spawn");
135                                         self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
136                                         self.strength_finished = 0;
137                                         self.invincible_finished = 0;
138                                 }
139                                 else
140                                 {
141                                         self.ammo_rockets = self.personal.ammo_rockets;
142                                         self.ammo_nails = self.personal.ammo_nails;
143                                         self.ammo_cells = self.personal.ammo_cells;
144                                         self.ammo_shells = self.personal.ammo_shells;
145                                         self.ammo_fuel = self.personal.ammo_fuel;
146                                         self.health = self.personal.health;
147                                         self.armorvalue = self.personal.armorvalue;
148                                         self.weapons = self.personal.weapons;
149                                         self.items = self.personal.items;
150                                         self.pauserotarmor_finished = time + self.personal.pauserotarmor_finished - self.personal.teleport_time;
151                                         self.pauserothealth_finished = time + self.personal.pauserothealth_finished - self.personal.teleport_time;
152                                         self.pauserotfuel_finished = time + self.personal.pauserotfuel_finished - self.personal.teleport_time;
153                                         self.pauseregen_finished = time + self.personal.pauseregen_finished - self.personal.teleport_time;
154                                         self.strength_finished = time + self.personal.strength_finished - self.personal.teleport_time;
155                                         self.invincible_finished = time + self.personal.invincible_finished - self.personal.teleport_time;
156                                 }
157                                 return 1;
158                         }
159                         else if(self.deadflag != DEAD_NO)
160                                 sprint(self, "UR DEAD AHAHAH))\n");
161                         else
162                                 sprint(self, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
163                         break;
164                 case CHIMPULSE_TELEPORT:
165                         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((sv_cheats >= 2) ? 100000 : 100), 1024, 256))
166                         {
167                                 self.angles_x = -self.angles_x;
168                                 self.fixangle = TRUE;
169                                 self.velocity = '0 0 0';
170                                 return 1;
171                         }
172                         else
173                                 sprint(self, "Emergency teleport could not find a good location, forget it!\n");
174                         break;
175                 case CHIMPULSE_R00T:
176                         FOR_EACH_PLAYER(e)
177                         {
178                                 if( e.playermodel == "models/player/jeandarc.zym"
179                                                 ||  e.playermodel == "models/player/pyria.zym"
180                                                 ||  e.playermodel == "models/player/skadi.zym"
181                                                 || (e.playermodel == "models/player/specop.zym" && random() < 0.5) // you never know
182                                                 ||  e.playermodel == "models/player/visitant.zym")
183                                 {
184                                         makevectors(e.angles);
185                                         traceline(e.origin, e.origin + v_right * 256, MOVE_NORMAL, e);
186                                 }
187                                 else
188                                 {
189                                         org_x = random();
190                                         org_y = random();
191                                         org_z = 0;
192                                         org = normalize(org);
193                                         traceline(e.origin, e.origin + org * 256, MOVE_NORMAL, e); // random direction
194                                 }
195
196                                 org = findbetterlocation(trace_endpos, 12);
197
198                                 e2 = spawn();
199                                 setorigin(e2, org);
200                                 pointparticles(particleeffectnum("rocket_explode"), org, '0 0 0', 1);
201                                 sound(e2, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
202                                 RadiusDamage(e2, e, 1000, 0, 128, e, 500, DEATH_CHEAT, world);
203                                 remove(e2);
204                         }
205                         print("404 Sportsmanship not found.\n");
206                         return 1;
207         }
208         return 0;
209 }
210
211 float CheatCommand(float argc)
212 {
213         if not(CheatsAllowed(0, argc, 0))
214                 return 0;
215         string cmd;
216         cmd = argv(0);
217         switch(cmd)
218         {
219                 entity e;
220                 float effectnum, f;
221                 vector start, end;
222                 entity oldself;
223
224                 case "pointparticles":
225                         if(argc == 5)
226                         {
227                                 // arguments:
228                                 //   effectname
229                                 //   origin (0..1, on crosshair line)
230                                 //   velocity
231                                 //   howmany
232                                 effectnum = particleeffectnum(argv(1));
233                                 f = stof(argv(2));
234                                 start = (1-f) * self.origin + f * self.cursor_trace_endpos;
235                                 end = stov(argv(3));
236                                 f = stof(argv(4));
237                                 pointparticles(effectnum, start, end, f);
238                                 return 1;
239                         }
240                         sprint(self, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
241                         break;
242                 case "trailparticles":
243                         if(argc == 2)
244                         {
245                                 // arguments:
246                                 //   effectname
247                                 effectnum = particleeffectnum(argv(1));
248                                 W_SetupShot(self, FALSE, FALSE, "",0);
249                                 traceline(w_shotorg, w_shotorg + w_shotdir * MAX_SHOT_DISTANCE, MOVE_NORMAL, self);
250                                 trailparticles(self, effectnum, w_shotorg, trace_endpos);
251                                 return 1;
252                         }
253                         sprint(self, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
254                         break;
255                 case "make":
256                         if(argc == 3)
257                         {
258                                 // arguments:
259                                 //   modelname mode
260                                 f = stof(argv(2));
261                                 W_SetupShot(self, FALSE, FALSE, "", 0);
262                                 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, self);
263                                 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
264                                 {
265                                         sprint(self, "cannot make stuff there (bad surface)\n");
266                                 }
267                                 else
268                                 {
269                                         e = spawn();
270                                         e.model = strzone(argv(1));
271                                         e.mdl = "rocket_explode";
272                                         e.health = 1000;
273                                         setorigin(e, trace_endpos);
274                                         e.effects = EF_NOMODELFLAGS;
275                                         if(f == 1)
276                                         {
277                                                 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
278                                                 e.angles = AnglesTransform_Multiply(e.angles, '-90 0 0'); // so unrotated models work
279                                         }
280                                         oldself = self;
281                                         self = e;
282                                         spawnfunc_func_breakable();
283                                         self = oldself;
284                                         // now, is it valid?
285                                         if(f == 0)
286                                         {
287                                                 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
288                                                 if(trace_startsolid)
289                                                 {
290                                                         remove(e);
291                                                         sprint(self, "cannot make stuff there (no space)\n");
292                                                 }
293                                                 else
294                                                         return 1;
295                                         }
296                                         else
297                                                 return 1;
298                                 }
299                         }
300                         else
301                                 sprint(self, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
302                         break;
303                 case "penalty":
304                         if(argc == 3)
305                         {
306                                 race_ImposePenaltyTime(self, stof(argv(1)), argv(2));
307                                 return 1;
308                         }
309                         sprint(self, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
310                         break;
311                 case "dragbox_spawn":
312                         e = spawn();
313                         e.classname = "dragbox_box";
314                         e.think = DragBox_Think;
315                         e.nextthink = time;
316                         e.solid = -1; // black
317                         setmodel(e, "null"); // network it
318                         if(argc == 4)
319                                 e.cnt = stof(argv(1));
320                         else
321                                 e.cnt = max(0, drag_lastcnt);
322
323                         e.aiment = spawn();
324                         e.aiment.classname = "dragbox_corner_1";
325                         e.aiment.owner = e;
326                         setmodel(e.aiment, "models/marker.md3");
327                         e.aiment.skin = 0;
328                         setsize(e.aiment, '0 0 0', '0 0 0');
329                         if(argc == 4)
330                                 setorigin(e.aiment, stov(argv(2)));
331                         else
332                                 setorigin(e.aiment, self.cursor_trace_endpos);
333
334                         e.enemy = spawn();
335                         e.enemy.classname = "dragbox_corner_2";
336                         e.enemy.owner = e;
337                         setmodel(e.enemy, "models/marker.md3");
338                         e.enemy.skin = 1;
339                         setsize(e.enemy, '0 0 0', '0 0 0');
340                         end = normalize(self.cursor_trace_start - e.aiment.origin);
341                         end_x = (end_x > 0) * 2 - 1;
342                         end_y = (end_y > 0) * 2 - 1;
343                         end_z = (end_z > 0) * 2 - 1;
344                         if(argc == 4)
345                                 setorigin(e.enemy, stov(argv(3)));
346                         else
347                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
348
349                         e.killindicator = spawn();
350                         e.killindicator.classname = "drag_digit";
351                         e.killindicator.owner = e;
352                         setattachment(e.killindicator, e, "");
353                         setorigin(e.killindicator, '0 0 -8');
354                         e.killindicator.killindicator = spawn();
355                         e.killindicator.killindicator.classname = "drag_digit";
356                         e.killindicator.killindicator.owner = e;
357                         setattachment(e.killindicator.killindicator, e, "");
358                         setorigin(e.killindicator.killindicator, '0 0 8');
359                         return 1;
360                 case "dragpoint_spawn":
361                         e = spawn();
362                         e.classname = "dragpoint";
363                         e.think = DragBox_Think;
364                         e.nextthink = time;
365                         e.solid = 0; // nothing special
366                         setmodel(e, "models/marker.md3");
367                         setsize(e, PL_MIN, PL_MAX);
368                         e.skin = 2;
369                         if(argc == 3)
370                                 e.cnt = stof(argv(1));
371                         else
372                                 e.cnt = drag_lastcnt;
373                         if(argc == 3)
374                                 setorigin(e, stov(argv(2)));
375                         else
376                         {
377                                 setorigin(e, self.cursor_trace_endpos + normalize(self.cursor_trace_start - self.cursor_trace_endpos));
378                                 move_out_of_solid(e);
379                         }
380
381                         e.killindicator = spawn();
382                         e.killindicator.classname = "drag_digit";
383                         e.killindicator.owner = e;
384                         setattachment(e.killindicator, e, "");
385                         setorigin(e.killindicator, '0 0 40');
386                         e.killindicator.killindicator = spawn();
387                         e.killindicator.killindicator.classname = "drag_digit";
388                         e.killindicator.killindicator.owner = e;
389                         setattachment(e.killindicator.killindicator, e, "");
390                         setorigin(e.killindicator.killindicator, '0 0 56');
391                         return 1;
392                 case "drag_remove":
393                         RandomSelection_Init();
394                         for(e = world; (e = find(e, classname, "dragbox_box")); )
395                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
396                         for(e = world; (e = find(e, classname, "dragpoint")); )
397                                 RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
398                         if(RandomSelection_chosen_ent)
399                         {
400                                 remove(RandomSelection_chosen_ent.killindicator.killindicator);
401                                 remove(RandomSelection_chosen_ent.killindicator);
402                                 if(RandomSelection_chosen_ent.aiment)
403                                         remove(RandomSelection_chosen_ent.aiment);
404                                 if(RandomSelection_chosen_ent.enemy)
405                                         remove(RandomSelection_chosen_ent.enemy);
406                                 remove(RandomSelection_chosen_ent);
407                         }
408                         return 1;
409                 case "drag_setcnt":
410                         if(argc == 2)
411                         {
412                                 RandomSelection_Init();
413                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
414                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
415                                 for(e = world; (e = find(e, classname, "dragpoint")); )
416                                         RandomSelection_Add(e, 0, string_null, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - self.cursor_trace_endpos));
417                                 if(RandomSelection_chosen_ent)
418                                 {
419                                         if(substring(argv(1), 0, 1) == "*")
420                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
421                                         else
422                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
423                                 }
424                                 return 1;
425                         }
426                         else
427                                 sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
428                         break;
429                 case "drag_save":
430                         if(argc == 2)
431                         {
432                                 f = fopen(argv(1), FILE_WRITE);
433                                 fputs(f, "cmd drag_clear\n");
434                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
435                                 {
436                                         fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
437                                 }
438                                 for(e = world; (e = find(e, classname, "dragpoint")); )
439                                 {
440                                         fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
441                                 }
442                                 fclose(f);
443                                 return 1;
444                         }
445                         else
446                                 sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
447                         break;
448                 case "drag_saveraceent":
449                         if(argc == 2)
450                         {
451                                 f = fopen(argv(1), FILE_WRITE);
452                                 for(e = world; (e = find(e, classname, "dragbox_box")); )
453                                 {
454                                         fputs(f, "{\n");
455                                         fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
456                                         fputs(f, strcat("\"origin\" \"", ftos(e.absmin_x), " ", ftos(e.absmin_y), " ", ftos(e.absmin_z), "\"\n"));
457                                         fputs(f, strcat("\"maxs\" \"", ftos(e.absmax_x - e.absmin_x), " ", ftos(e.absmax_y - e.absmin_y), " ", ftos(e.absmax_z - e.absmin_z), "\"\n"));
458                                         fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
459                                         fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
460                                         fputs(f, "}\n");
461                                 }
462                                 for(e = world; (e = find(e, classname, "dragpoint")); )
463                                 {
464                                         start = '0 0 0';
465                                         effectnum = 0;
466                                         for(oldself = world; (oldself = find(oldself, classname, "dragbox_box")); )
467                                         {
468                                                 if(e.cnt <= 0 && oldself.cnt == 0 || e.cnt == oldself.cnt)
469                                                 {
470                                                         start = start + oldself.origin;
471                                                         ++effectnum;
472                                                 }
473                                         }
474                                         start *= 1 / effectnum;
475                                         fputs(f, "{\n");
476                                         fputs(f, "\"classname\" \"info_player_race\"\n");
477                                         fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
478                                         fputs(f, strcat("\"origin\" \"", ftos(e.origin_x), " ", ftos(e.origin_y), " ", ftos(e.origin_z), "\"\n"));
479                                         if(e.cnt == -2)
480                                         {
481                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
482                                                 fputs(f, "\"race_place\" \"0\"\n");
483                                         }
484                                         else if(e.cnt == -1)
485                                         {
486                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
487                                                 fputs(f, "\"race_place\" \"-1\"\n");
488                                         }
489                                         else
490                                         {
491                                                 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
492                                                 if(e.cnt == 0)
493                                                 {
494                                                         // these need race_place
495                                                         // counting...
496                                                         effectnum = 1;
497                                                         for(oldself = world; (oldself = find(oldself, classname, "dragpoint")); )
498                                                         if(oldself.cnt == 0)
499                                                         {
500                                                                 if(vlen(oldself.origin - start) < vlen(e.origin - start))
501                                                                         ++effectnum;
502                                                                 else if(vlen(oldself.origin - start) == vlen(e.origin - start) && num_for_edict(oldself) < num_for_edict(e))
503                                                                         ++effectnum;
504                                                         }
505                                                         fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
506                                                 }
507                                         }
508                                         fputs(f, "}\n");
509                                 }
510                                 fclose(f);
511                                 return 1;
512                         }
513                         else
514                                 sprint(self, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
515                         break;
516                 case "drag_clear":
517                         for(e = world; (e = find(e, classname, "dragbox_box")); )
518                                 remove(e);
519                         for(e = world; (e = find(e, classname, "dragbox_corner_1")); )
520                                 remove(e);
521                         for(e = world; (e = find(e, classname, "dragbox_corner_2")); )
522                                 remove(e);
523                         for(e = world; (e = find(e, classname, "dragpoint")); )
524                                 remove(e);
525                         for(e = world; (e = find(e, classname, "drag_digit")); )
526                                 remove(e);
527                         return 1;
528         }
529         return 0;
530 }
531
532 void Drag_Begin(entity dragger, entity draggee, vector touchpoint);
533 void Drag_Finish(entity dragger);
534 float Drag_IsDraggable(entity draggee);
535 float Drag_MayChangeAngles(entity draggee);
536 void Drag_MoveForward(entity dragger);
537 void Drag_SetSpeed(entity dragger, float s);
538 void Drag_MoveBackward(entity dragger);
539 void Drag_Update(entity dragger);
540 float Drag_CanDrag(entity dragger);
541 float Drag_IsDragging(entity dragger);
542 void Drag_MoveDrag(entity from, entity to);
543 .entity dragentity;
544
545 float CheatFrame()
546 {
547         float cheating;
548         cheating = 0;
549
550         if(CheatsAllowed(0, 0, CHRAME_DRAG))
551                 if(Drag_CanDrag(self))
552                         if(self.BUTTON_DRAG)
553                                 if(!self.dragentity)
554                                         if(self.cursor_trace_ent)
555                                                 if(Drag_IsDraggable(self.cursor_trace_ent))
556                                                 {
557                                                         if(self.cursor_trace_ent.draggedby)
558                                                                 Drag_Finish(self.cursor_trace_ent.draggedby);
559                                                         if(self.cursor_trace_ent.tag_entity)
560                                                                 detach_sameorigin(self.cursor_trace_ent);
561                                                         Drag_Begin(self, self.cursor_trace_ent, self.cursor_trace_endpos);
562                                                         ++cheating;
563                                                 }
564         if(Drag_IsDragging(self))
565         {
566                 if(self.BUTTON_DRAG)
567                 {
568                         if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
569                         {
570                                 Drag_MoveForward(self);
571                                 self.impulse = 0;
572                         }
573                         else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19)
574                         {
575                                 Drag_MoveBackward(self);
576                                 self.impulse = 0;
577                         }
578                         else if(self.impulse >= 1 && self.impulse <= 9)
579                         {
580                                 Drag_SetSpeed(self, self.impulse - 1);
581                         }
582                         else if(self.impulse == 14)
583                         {
584                                 Drag_SetSpeed(self, 9);
585                         }
586
587                         if(frametime)
588                                 Drag_Update(self);
589                 }
590                 else
591                 {
592                         Drag_Finish(self);
593                 }
594         }
595
596         return cheating;
597 }
598
599
600
601
602
603 // ENTITY DRAGGING
604
605 // on dragger:
606 .float draggravity;
607 .float dragspeed; // speed of mouse wheel action
608 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
609 .vector draglocalvector; // local attachment vector of the dragentity
610 .float draglocalangle;
611 // on draggee:
612 .entity draggedby;
613 .float dragmovetype;
614 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
615 {
616         float tagscale;
617
618         draggee.dragmovetype = draggee.movetype;
619         draggee.draggravity = draggee.gravity;
620         draggee.movetype = MOVETYPE_WALK;
621         draggee.gravity = 0.00001;
622         draggee.flags &~= FL_ONGROUND;
623         draggee.draggedby = dragger;
624
625         dragger.dragentity = draggee;
626
627         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
628         dragger.draglocalangle = draggee.angles_y - dragger.v_angle_y;
629         touchpoint = touchpoint - gettaginfo(draggee, 0);
630         tagscale = pow(vlen(v_forward), -2);
631         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
632         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
633         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
634
635         dragger.dragspeed = 64;
636 }
637
638 void Drag_Finish(entity dragger)
639 {
640         entity draggee;
641         draggee = dragger.dragentity;
642         if(dragger)
643                 dragger.dragentity = world;
644         draggee.draggedby = world;
645         draggee.movetype = draggee.dragmovetype;
646         draggee.gravity = draggee.draggravity;
647
648         switch(draggee.movetype)
649         {
650                 case MOVETYPE_TOSS:
651                 case MOVETYPE_WALK:
652                 case MOVETYPE_STEP:
653                 case MOVETYPE_FLYMISSILE:
654                 case MOVETYPE_BOUNCE:
655                 case MOVETYPE_BOUNCEMISSILE:
656                 case MOVETYPE_PHYSICS:
657                         break;
658                 default:
659                         draggee.velocity = '0 0 0';
660                         break;
661         }
662
663         if((draggee.flags & FL_ITEM) && (vlen(draggee.velocity) < 32))
664         {
665                 draggee.velocity = '0 0 0';
666                 draggee.flags |= FL_ONGROUND; // floating items are FUN
667         }
668 }
669
670 float Drag_IsDraggable(entity draggee)
671 {
672         // TODO add more checks for bad stuff here
673         if(draggee.classname == "func_bobbing")
674                 return FALSE;
675         if(draggee.classname == "door") // FIXME find out why these must be excluded, or work around the problem (trying to drag these causes like 4 fps)
676                 return FALSE;
677         if(draggee.classname == "plat")
678                 return FALSE;
679         if(draggee.classname == "func_button")
680                 return FALSE;
681         if(draggee.model == "")
682                 return FALSE;
683         if(draggee.classname == "spectator")
684                 return FALSE;
685         if(draggee.classname == "observer")
686                 return FALSE;
687         if(draggee.classname == "exteriorweaponentity")
688                 return FALSE;
689
690         return TRUE;
691 }
692
693 float Drag_MayChangeAngles(entity draggee)
694 {
695         // TODO add more checks for bad stuff here
696         if(substring(draggee.model, 0, 1) == "*")
697                 return FALSE;
698         return TRUE;
699 }
700
701 void Drag_MoveForward(entity dragger)
702 {
703         dragger.dragdistance += dragger.dragspeed;
704 }
705
706 void Drag_SetSpeed(entity dragger, float s)
707 {
708         dragger.dragspeed = pow(2, s);
709 }
710
711 void Drag_MoveBackward(entity dragger)
712 {
713         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
714 }
715
716 void Drag_Update(entity dragger)
717 {
718         vector curorigin, neworigin, goodvelocity;
719         float f;
720         entity draggee;
721
722         draggee = dragger.dragentity;
723         draggee.flags &~= FL_ONGROUND;
724
725         curorigin = gettaginfo(draggee, 0);
726         curorigin = curorigin + v_forward * dragger.draglocalvector_x + v_right * dragger.draglocalvector_y + v_up * dragger.draglocalvector_z;
727         makevectors(dragger.v_angle);
728         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
729         goodvelocity = (neworigin - curorigin) * (1 / frametime);
730
731         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle > 180)
732                 dragger.draglocalangle += 360;
733         while(draggee.angles_y - dragger.v_angle_y - dragger.draglocalangle <= -180)
734                 dragger.draglocalangle -= 360;
735
736         f = min(frametime * 10, 1);
737         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
738
739         if(Drag_MayChangeAngles(draggee))
740                 draggee.angles_y = draggee.angles_y * (1 - f) + (dragger.v_angle_y + dragger.draglocalangle) * f;
741
742         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
743
744         te_lightning1(dragger, dragger.origin + dragger.view_ofs, curorigin);
745 }
746
747 float Drag_CanDrag(entity dragger)
748 {
749         return (dragger.deadflag == DEAD_NO) || (dragger.classname == "player");
750 }
751
752 float Drag_IsDragging(entity dragger)
753 {
754         if(!dragger.dragentity)
755                 return FALSE;
756         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
757         {
758                 dragger.dragentity = world;
759                 return FALSE;
760         }
761         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
762         {
763                 Drag_Finish(dragger);
764                 return FALSE;
765         }
766         return TRUE;
767 }
768
769 void Drag_MoveDrag(entity from, entity to)
770 {
771         if(from.draggedby)
772         {
773                 to.draggedby = from.draggedby;
774                 to.draggedby.dragentity = to;
775                 from.draggedby = world;
776         }
777 }
778