9620ce545e75bdd3dc5b49b8724746278fe95c7e
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cheats.qc
1 #include "cheats.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include <common/effects/all.qh>
6
7 #include "g_damage.qh"
8 #include "race.qh"
9 #include "../common/triggers/teleporters.qh"
10
11 #include "mutators/_mod.qh"
12
13 #include "weapons/tracing.qh"
14
15 #include "../common/constants.qh"
16 #include "../common/deathtypes/all.qh"
17 #include "../common/util.qh"
18
19 #include <common/physics/player.qh>
20
21 #include "../common/monsters/_mod.qh"
22
23 #include <common/weapons/_all.qh>
24
25 #include "../common/triggers/subs.qh"
26
27 #include "../common/triggers/func/breakable.qh"
28
29 #include "../lib/csqcmodel/sv_model.qh"
30
31 #include "../lib/warpzone/anglestransform.qh"
32 #include "../lib/warpzone/util_server.qh"
33
34 void CopyBody(entity this, float keepvelocity);
35
36 #ifdef NOCHEATS
37
38 float CheatImpulse(entity this, int imp) { return 0; }
39 float CheatCommand(entity this, int argc) { return 0; }
40 float CheatFrame(entity this) { return 0; }
41 void CheatInit() { cheatcount_total = world.cheatcount; }
42 void CheatShutdown() { }
43 void Drag_MoveDrag(entity from, entity to) { }
44
45 #else
46
47 .float maycheat;
48 float gamestart_sv_cheats;
49
50
51
52 void CheatInit()
53 {
54         gamestart_sv_cheats = autocvar_sv_cheats;
55 }
56
57 void CheatShutdown()
58 {
59 }
60
61 float CheatsAllowed(entity this, float i, float argc, float fr) // the cheat gets passed as argument for possible future ACL checking
62 {
63         // dead people cannot cheat
64         if(IS_DEAD(this))
65                 return 0;
66         if(gamestart_sv_cheats < 2 && !IS_PLAYER(this))
67                 return 0;
68
69         // sv_clones
70         if(i == CHIMPULSE_CLONE_MOVING.impulse || i == CHIMPULSE_CLONE_STANDING.impulse)
71                 if(this.lip < sv_clones)
72                         return 1;
73
74         // haha
75         if(this.maycheat)
76                 return 1;
77
78         // sv_cheats
79         if(gamestart_sv_cheats && autocvar_sv_cheats)
80                 return 1;
81
82         // if we get here, player is not allowed to cheat. Log it.
83         if(i)
84                 bprintf("Player %s^7 tried to use cheat 'impulse %d'\n", playername(this, false), i);
85         else if(argc)
86                 bprintf("Player %s^7 tried to use cheat '%s'\n", playername(this, false), argv(0));
87         else if(fr)
88                 bprintf("Player %s^7 tried to use cheat frame %d\n", playername(this, false), fr);
89         else
90                 bprintf("Player %s^7 tried to use an unknown cheat\n", playername(this, false));
91
92         return 0;
93 }
94
95 #define BEGIN_CHEAT_FUNCTION() \
96         float cheating, attempting; \
97         cheating = 0; attempting = 0
98 #define DID_CHEAT() \
99         ++cheating
100 #define ADD_CHEATS(e,n) \
101         cheatcount_total += n; \
102         e.cheatcount += n
103 #define END_CHEAT_FUNCTION() \
104         ADD_CHEATS(this, cheating); \
105         return attempting
106 #define IS_CHEAT(ent,i,argc,fr) \
107         if((++attempting, !CheatsAllowed(ent,i,argc,fr))) \
108                 break
109
110 float num_autoscreenshot;
111 void info_autoscreenshot_findtarget(entity this)
112 {
113         entity e;
114         e = find(NULL, targetname, this.target);
115         if(!e)
116         {
117                 objerror(this, "Missing target. FAIL!");
118                 return;
119         }
120         vector a = vectoangles(e.origin - this.origin);
121         a.x = -a.x; // don't ask
122         this.angles_x = a.x;
123         this.angles_y = a.y;
124         // we leave Rick Roll alone
125 }
126 spawnfunc(info_autoscreenshot)
127 {
128         if(++num_autoscreenshot > autocvar_g_max_info_autoscreenshot)
129         {
130                 objerror(this, "Too many info_autoscreenshot entitites. FAIL!");
131                 return;
132         }
133         if(this.target != "")
134                 InitializeEntity(this, info_autoscreenshot_findtarget, INITPRIO_FINDTARGET);
135         // this one just has to exist
136 }
137
138 float CheatImpulse(entity this, int imp)
139 {
140         BEGIN_CHEAT_FUNCTION();
141         switch(imp)
142         {
143                 entity e, e2;
144
145                 case CHIMPULSE_SPEEDRUN_INIT.impulse: // deploy personal waypoint
146                         // shared with regular waypoint init, so this is not a cheat by itself
147                         if(!this.personal)
148                         {
149                                 this.personal = new(personal_wp);
150                         }
151                         this.personal.origin = this.origin;
152                         this.personal.v_angle = this.v_angle;
153                         this.personal.velocity = this.velocity;
154                         this.personal.ammo_rockets = this.ammo_rockets;
155                         this.personal.ammo_nails = this.ammo_nails;
156                         this.personal.ammo_cells = this.ammo_cells;
157                         this.personal.ammo_plasma = this.ammo_plasma;
158                         this.personal.ammo_shells = this.ammo_shells;
159                         this.personal.ammo_fuel = this.ammo_fuel;
160                         this.personal.health = max(1, this.health);
161                         this.personal.armorvalue = this.armorvalue;
162                         this.personal.weapons = STAT(WEAPONS, this);
163                         this.personal.items = this.items;
164                         this.personal.pauserotarmor_finished = this.pauserotarmor_finished;
165                         this.personal.pauserothealth_finished = this.pauserothealth_finished;
166                         this.personal.pauserotfuel_finished = this.pauserotfuel_finished;
167                         this.personal.pauseregen_finished = this.pauseregen_finished;
168                         this.personal.strength_finished = this.strength_finished;
169                         this.personal.invincible_finished = this.invincible_finished;
170                         this.personal.teleport_time = time;
171                         break; // this part itself doesn't cheat, so let's not count this
172                 case CHIMPULSE_CLONE_MOVING.impulse:
173                         IS_CHEAT(this, imp, 0, 0);
174                         makevectors (this.v_angle);
175                         this.velocity = this.velocity + v_forward * 300;
176                         CopyBody(this, 1);
177                         this.lip += 1;
178                         this.velocity = this.velocity - v_forward * 300;
179                         DID_CHEAT();
180                         break;
181                 case CHIMPULSE_CLONE_STANDING.impulse:
182                         IS_CHEAT(this, imp, 0, 0);
183                         CopyBody(this, 0);
184                         this.lip += 1;
185                         DID_CHEAT();
186                         break;
187                 case CHIMPULSE_GIVE_ALL.impulse:
188                         IS_CHEAT(this, imp, 0, 0);
189                         CheatCommand(this, tokenize_console("give all"));
190                         break; // already counted as cheat
191                 case CHIMPULSE_SPEEDRUN.impulse:
192                         if(!autocvar_g_allow_checkpoints)
193                                 IS_CHEAT(this, imp, 0, 0);
194                         if(this.personal)
195                         {
196                                 this.speedrunning = true;
197                                 tracebox(this.personal.origin, this.mins, this.maxs, this.personal.origin, MOVE_WORLDONLY, this);
198                                 if(trace_startsolid)
199                                 {
200                                         sprint(this, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
201                                 }
202                                 else
203                                 {
204                                         // Abort speedrun, teleport back
205                                         setorigin(this, this.personal.origin);
206                                         this.oldvelocity = this.velocity = this.personal.velocity;
207                                         this.angles = this.personal.v_angle;
208                                         this.fixangle = true;
209
210                                         MUTATOR_CALLHOOK(AbortSpeedrun, this);
211                                 }
212
213                                 this.ammo_rockets = this.personal.ammo_rockets;
214                                 this.ammo_nails = this.personal.ammo_nails;
215                                 this.ammo_cells = this.personal.ammo_cells;
216                                 this.ammo_plasma = this.personal.ammo_plasma;
217                                 this.ammo_shells = this.personal.ammo_shells;
218                                 this.ammo_fuel = this.personal.ammo_fuel;
219                                 this.health = this.personal.health;
220                                 this.armorvalue = this.personal.armorvalue;
221                                 STAT(WEAPONS, this) = this.personal.weapons;
222                                 this.items = this.personal.items;
223                                 this.pauserotarmor_finished = time + this.personal.pauserotarmor_finished - this.personal.teleport_time;
224                                 this.pauserothealth_finished = time + this.personal.pauserothealth_finished - this.personal.teleport_time;
225                                 this.pauserotfuel_finished = time + this.personal.pauserotfuel_finished - this.personal.teleport_time;
226                                 this.pauseregen_finished = time + this.personal.pauseregen_finished - this.personal.teleport_time;
227                                 this.strength_finished = time + this.personal.strength_finished - this.personal.teleport_time;
228                                 this.invincible_finished = time + this.personal.invincible_finished - this.personal.teleport_time;
229
230                                 if(!autocvar_g_allow_checkpoints)
231                                         DID_CHEAT();
232                                 break;
233                         }
234                         if(IS_DEAD(this))
235                                 sprint(this, "UR DEAD AHAHAH))\n");
236                         else
237                                 sprint(this, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
238                         break;
239                 case CHIMPULSE_TELEPORT.impulse:
240                         IS_CHEAT(this, imp, 0, 0);
241                         if(this.move_movetype == MOVETYPE_NOCLIP)
242                         {
243                                 e = find(NULL, classname, "info_autoscreenshot");
244                                 if(e)
245                                 {
246                                         sprint(this, "Emergency teleport used info_autoscreenshot location\n");
247                                         setorigin(this, e.origin);
248                                         this.angles = e.angles;
249                                         delete(e);
250                                         // should we? this.angles_x = -this.angles_x;
251                                         this.fixangle = true;
252                                         this.velocity = '0 0 0';
253                                         DID_CHEAT();
254                                         break;
255                                 }
256                         }
257                         if(MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((gamestart_sv_cheats < 2) ? 100 : 100000), 384, 384))
258                         {
259                                 sprint(this, "Emergency teleport used random location\n");
260                                 this.angles_x = -this.angles.x;
261                                 this.fixangle = true;
262                                 this.velocity = '0 0 0';
263                                 DID_CHEAT();
264                                 break;
265                         }
266                         sprint(this, "Emergency teleport could not find a good location, forget it!\n");
267                         break;
268                 case CHIMPULSE_R00T.impulse:
269                         IS_CHEAT(this, imp, 0, 0);
270                         RandomSelection_Init();
271                         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && DIFF_TEAM(it, this), { RandomSelection_AddEnt(it, 1, 1); });
272                         if(RandomSelection_chosen_ent)
273                                 e = RandomSelection_chosen_ent;
274                         else
275                                 e = this;
276
277                         Send_Effect(EFFECT_ROCKET_EXPLODE, e.origin, '0 0 0', 1);
278                         sound(e, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
279
280                         e2 = spawn();
281                         setorigin(e2, e.origin);
282                         RadiusDamage(e2, this, 1000, 0, 128, NULL, NULL, 500, DEATH_CHEAT.m_id, DMG_NOWEP, e);
283                         delete(e2);
284
285                         LOG_INFO("404 Sportsmanship not found.");
286                         DID_CHEAT();
287                         break;
288         }
289
290         END_CHEAT_FUNCTION();
291 }
292
293 void DragBox_Think(entity this);
294 float drag_lastcnt;
295 float CheatCommand(entity this, int argc)
296 {
297         BEGIN_CHEAT_FUNCTION();
298         string cmd;
299         cmd = argv(0);
300         switch(cmd)
301         {
302                 float effectnum, f;
303                 vector start, end;
304
305                 case "pointparticles":
306                         IS_CHEAT(this, 0, argc, 0);
307                         if(argc == 5)
308                         {
309                                 // arguments:
310                                 //   effectname
311                                 //   origin (0..1, on crosshair line)
312                                 //   velocity
313                                 //   howmany
314                                 f = stof(argv(2));
315                                 crosshair_trace(this);
316                                 start = (1-f) * this.origin + f * trace_endpos;
317                                 end = stov(argv(3));
318                                 f = stof(argv(4));
319                                 Send_Effect_(argv(1), start, end, f);
320                                 DID_CHEAT();
321                                 break;
322                         }
323                         sprint(this, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
324                         break;
325                 case "trailparticles":
326                         IS_CHEAT(this, 0, argc, 0);
327                         if(argc == 2)
328                         {
329                                 // arguments:
330                                 //   effectname
331                                 effectnum = _particleeffectnum(argv(1));
332                                 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
333                                 traceline(w_shotorg, w_shotorg + w_shotdir * max_shot_distance, MOVE_NORMAL, this);
334                                 __trailparticles(this, effectnum, w_shotorg, trace_endpos);
335                                 DID_CHEAT();
336                                 break;
337                         }
338                         sprint(this, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
339                         break;
340                 case "make":
341                         IS_CHEAT(this, 0, argc, 0);
342                         if(argc == 3)
343                         {
344                                 // arguments:
345                                 //   modelname mode
346                                 f = stof(argv(2));
347                                 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
348                                 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, this);
349                                 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
350                                 {
351                                         sprint(this, "cannot make stuff there (bad surface)\n");
352                                 }
353                                 else
354                                 {
355                                         entity e = spawn();
356                                         e.model = strzone(argv(1));
357                                         e.mdl = "rocket_explode";
358                                         e.health = 1000;
359                                         setorigin(e, trace_endpos);
360                                         e.effects = EF_NOMODELFLAGS;
361                                         if(f == 1)
362                                         {
363                                                 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
364                                                 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
365                                         }
366                                         spawnfunc_func_breakable(e);
367                                         // now, is it valid?
368                                         if(f == 0)
369                                         {
370                                                 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
371                                                 if(trace_startsolid)
372                                                 {
373                                                         delete(e);
374                                                         sprint(this, "cannot make stuff there (no space)\n");
375                                                 }
376                                                 else
377                                                         DID_CHEAT();
378                                         }
379                                         else
380                                                 DID_CHEAT();
381                                 }
382                         }
383                         else
384                                 sprint(this, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
385                         break;
386                 case "penalty":
387                         IS_CHEAT(this, 0, argc, 0);
388                         if(argc == 3)
389                         {
390                                 race_ImposePenaltyTime(this, stof(argv(1)), argv(2));
391                                 DID_CHEAT();
392                                 break;
393                         }
394                         sprint(this, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
395                         break;
396                 case "dragbox_spawn": {
397                         IS_CHEAT(this, 0, argc, 0);
398                         entity e = new(dragbox_box);
399                         setthink(e, DragBox_Think);
400                         e.nextthink = time;
401                         e.solid = -1; // black
402                         setmodel(e, MDL_Null); // network it
403                         if(argc == 4)
404                                 e.cnt = stof(argv(1));
405                         else
406                                 e.cnt = max(0, drag_lastcnt);
407
408                         e.aiment = new(dragbox_corner_1);
409                         e.aiment.owner = e;
410                         setmodel(e.aiment, MDL_MARKER);
411                         e.aiment.skin = 0;
412                         setsize(e.aiment, '0 0 0', '0 0 0');
413                         if(argc == 4)
414                                 setorigin(e.aiment, stov(argv(2)));
415                         else
416                         {
417                                 crosshair_trace(this);
418                                 setorigin(e.aiment, trace_endpos);
419                         }
420
421                         e.enemy = new(dragbox_corner_2);
422                         e.enemy.owner = e;
423                         setmodel(e.enemy, MDL_MARKER);
424                         e.enemy.skin = 1;
425                         setsize(e.enemy, '0 0 0', '0 0 0');
426                         end = normalize(this.origin + this.view_ofs - e.aiment.origin);
427                         end.x = (end.x > 0) * 2 - 1;
428                         end.y = (end.y > 0) * 2 - 1;
429                         end.z = (end.z > 0) * 2 - 1;
430                         if(argc == 4)
431                                 setorigin(e.enemy, stov(argv(3)));
432                         else
433                                 setorigin(e.enemy, e.aiment.origin + 32 * end);
434
435                         e.killindicator = new(drag_digit);
436                         e.killindicator.owner = e;
437                         setattachment(e.killindicator, e, "");
438                         setorigin(e.killindicator, '0 0 -8');
439                         e.killindicator.killindicator = new(drag_digit);
440                         e.killindicator.killindicator.owner = e;
441                         setattachment(e.killindicator.killindicator, e, "");
442                         setorigin(e.killindicator.killindicator, '0 0 8');
443                         DID_CHEAT();
444                         break;
445                 }
446                 case "dragpoint_spawn": {
447                         IS_CHEAT(this, 0, argc, 0);
448                         entity e = new(dragpoint);
449                         setthink(e, DragBox_Think);
450                         e.nextthink = time;
451                         e.solid = 0; // nothing special
452                         setmodel(e, MDL_MARKER);
453                         setsize(e, STAT(PL_MIN, this), STAT(PL_MAX, this));
454                         e.skin = 2;
455                         if(argc == 3)
456                                 e.cnt = stof(argv(1));
457                         else
458                                 e.cnt = drag_lastcnt;
459                         if(argc == 3)
460                                 setorigin(e, stov(argv(2)));
461                         else
462                         {
463                                 crosshair_trace(this);
464                                 setorigin(e, trace_endpos + normalize(this.origin + this.view_ofs - trace_endpos));
465                                 move_out_of_solid(e);
466                         }
467
468                         e.killindicator = new(drag_digit);
469                         e.killindicator.owner = e;
470                         setattachment(e.killindicator, e, "");
471                         setorigin(e.killindicator, '0 0 40');
472                         e.killindicator.killindicator = new(drag_digit);
473                         e.killindicator.killindicator.owner = e;
474                         setattachment(e.killindicator.killindicator, e, "");
475                         setorigin(e.killindicator.killindicator, '0 0 56');
476                         DID_CHEAT();
477                         break;
478                 }
479                 case "drag_remove":
480                         IS_CHEAT(this, 0, argc, 0);
481                         RandomSelection_Init();
482                         crosshair_trace(this);
483                         for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
484                                 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
485                         for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
486                                 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
487                         if(RandomSelection_chosen_ent)
488                         {
489                                 delete(RandomSelection_chosen_ent.killindicator.killindicator);
490                                 delete(RandomSelection_chosen_ent.killindicator);
491                                 if(RandomSelection_chosen_ent.aiment)
492                                         delete(RandomSelection_chosen_ent.aiment);
493                                 if(RandomSelection_chosen_ent.enemy)
494                                         delete(RandomSelection_chosen_ent.enemy);
495                                 delete(RandomSelection_chosen_ent);
496                         }
497                         DID_CHEAT();
498                         break;
499                 case "drag_setcnt":
500                         IS_CHEAT(this, 0, argc, 0);
501                         if(argc == 2)
502                         {
503                                 RandomSelection_Init();
504                                 crosshair_trace(this);
505                                 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
506                                         RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
507                                 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
508                                         RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
509                                 if(RandomSelection_chosen_ent)
510                                 {
511                                         if(substring(argv(1), 0, 1) == "*")
512                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
513                                         else
514                                                 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
515                                 }
516                                 DID_CHEAT();
517                                 break;
518                         }
519                         sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
520                         break;
521                 case "drag_save":
522                         IS_CHEAT(this, 0, argc, 0);
523                         if(argc == 2)
524                         {
525                                 f = fopen(argv(1), FILE_WRITE);
526                                 fputs(f, "cmd drag_clear\n");
527                                 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
528                                 {
529                                         fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
530                                 }
531                                 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
532                                 {
533                                         fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
534                                 }
535                                 fclose(f);
536                                 DID_CHEAT();
537                                 break;
538                         }
539                         sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
540                         break;
541                 case "drag_saveraceent":
542                         IS_CHEAT(this, 0, argc, 0);
543                         if(argc == 2)
544                         {
545                                 f = fopen(argv(1), FILE_WRITE);
546                                 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
547                                 {
548                                         fputs(f, "{\n");
549                                         fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
550                                         fputs(f, strcat("\"origin\" \"", ftos(e.absmin.x), " ", ftos(e.absmin.y), " ", ftos(e.absmin.z), "\"\n"));
551                                         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"));
552                                         fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
553                                         fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
554                                         fputs(f, "}\n");
555                                 }
556                                 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
557                                 {
558                                         start = '0 0 0';
559                                         effectnum = 0;
560                                         for(entity ent = NULL; (ent = find(ent, classname, "dragbox_box")); )
561                                         {
562                                                 if(e.cnt <= 0 && ent.cnt == 0 || e.cnt == ent.cnt)
563                                                 {
564                                                         start = start + ent.origin;
565                                                         ++effectnum;
566                                                 }
567                                         }
568                                         start *= 1 / effectnum;
569                                         fputs(f, "{\n");
570                                         fputs(f, "\"classname\" \"info_player_race\"\n");
571                                         fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
572                                         fputs(f, strcat("\"origin\" \"", ftos(e.origin.x), " ", ftos(e.origin.y), " ", ftos(e.origin.z), "\"\n"));
573                                         if(e.cnt == -2)
574                                         {
575                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
576                                                 fputs(f, "\"race_place\" \"0\"\n");
577                                         }
578                                         else if(e.cnt == -1)
579                                         {
580                                                 fputs(f, "\"target\" \"checkpoint0\"\n");
581                                                 fputs(f, "\"race_place\" \"-1\"\n");
582                                         }
583                                         else
584                                         {
585                                                 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
586                                                 if(e.cnt == 0)
587                                                 {
588                                                         // these need race_place
589                                                         // counting...
590                                                         effectnum = 1;
591                                                         for(entity ent = NULL; (ent = find(ent, classname, "dragpoint")); )
592                                                         if(ent.cnt == 0)
593                                                         {
594                                                                 if(vlen2(ent.origin - start) < vlen2(e.origin - start))
595                                                                         ++effectnum;
596                                                                 else if(vlen2(ent.origin - start) == vlen2(e.origin - start) && etof(ent) < etof(e))
597                                                                         ++effectnum;
598                                                         }
599                                                         fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
600                                                 }
601                                         }
602                                         fputs(f, "}\n");
603                                 }
604                                 fclose(f);
605                                 DID_CHEAT();
606                                 break;
607                         }
608                         sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
609                         break;
610                 case "drag_clear":
611                         IS_CHEAT(this, 0, argc, 0);
612                         for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
613                                 delete(e);
614                         for(entity e = NULL; (e = find(e, classname, "dragbox_corner_1")); )
615                                 delete(e);
616                         for(entity e = NULL; (e = find(e, classname, "dragbox_corner_2")); )
617                                 delete(e);
618                         for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
619                                 delete(e);
620                         for(entity e = NULL; (e = find(e, classname, "drag_digit")); )
621                                 delete(e);
622                         DID_CHEAT();
623                         break;
624                 case "god":
625                         IS_CHEAT(this, 0, argc, 0);
626                         BITXOR_ASSIGN(this.flags, FL_GODMODE);
627                         if(this.flags & FL_GODMODE)
628                         {
629                                 sprint(this, "godmode ON\n");
630                                 DID_CHEAT();
631                         }
632                         else
633                                 sprint(this, "godmode OFF\n");
634                         break;
635                 case "notarget":
636                         IS_CHEAT(this, 0, argc, 0);
637                         BITXOR_ASSIGN(this.flags, FL_NOTARGET);
638                         if(this.flags & FL_NOTARGET)
639                         {
640                                 sprint(this, "notarget ON\n");
641                                 DID_CHEAT();
642                         }
643                         else
644                                 sprint(this, "notarget OFF\n");
645                         break;
646                 case "noclip":
647                         IS_CHEAT(this, 0, argc, 0);
648                         if(this.move_movetype != MOVETYPE_NOCLIP)
649                         {
650                                 set_movetype(this, MOVETYPE_NOCLIP);
651                                 sprint(this, "noclip ON\n");
652                                 DID_CHEAT();
653                         }
654                         else
655                         {
656                                 set_movetype(this, MOVETYPE_WALK);
657                                 sprint(this, "noclip OFF\n");
658                         }
659                         break;
660                 case "fly":
661                         IS_CHEAT(this, 0, argc, 0);
662                         if(this.move_movetype != MOVETYPE_FLY)
663                         {
664                                 set_movetype(this, MOVETYPE_FLY);
665                                 sprint(this, "flymode ON\n");
666                                 DID_CHEAT();
667                         }
668                         else
669                         {
670                                 set_movetype(this, MOVETYPE_WALK);
671                                 sprint(this, "flymode OFF\n");
672                         }
673                         break;
674                 case "give":
675                         IS_CHEAT(this, 0, argc, 0);
676                         if(GiveItems(this, 1, argc))
677                                 DID_CHEAT();
678                         break;
679                 case "usetarget":
680                         IS_CHEAT(this, 0, argc, 0);
681                         entity e = spawn();
682                         e.target = argv(1);
683                         SUB_UseTargets(e, this, NULL);
684                         delete(e);
685                         DID_CHEAT();
686                         break;
687                 case "killtarget":
688                         IS_CHEAT(this, 0, argc, 0);
689                         entity e2 = spawn();
690                         e2.killtarget = argv(1);
691                         SUB_UseTargets(e2, this, NULL);
692                         delete(e2);
693                         DID_CHEAT();
694                         break;
695                 case "teleporttotarget":
696                         IS_CHEAT(this, 0, argc, 0);
697                         entity ent = new(cheattriggerteleport);
698                         setorigin(ent, ent.origin);
699                         ent.target = argv(1);
700                         teleport_findtarget(ent);
701                         if(!wasfreed(ent))
702                         {
703                                 Simple_TeleportPlayer(ent, this);
704                                 delete(ent);
705                                 DID_CHEAT();
706                         }
707                         break;
708         }
709
710         END_CHEAT_FUNCTION();
711 }
712
713 float Drag(entity this, float force_allow_pick, float ischeat);
714 void Drag_Begin(entity dragger, entity draggee, vector touchpoint);
715 void Drag_Finish(entity dragger);
716 float Drag_IsDraggable(entity draggee);
717 float Drag_MayChangeAngles(entity draggee);
718 void Drag_MoveForward(entity dragger);
719 void Drag_SetSpeed(entity dragger, float s);
720 void Drag_MoveBackward(entity dragger);
721 void Drag_Update(entity dragger);
722 float Drag_CanDrag(entity dragger);
723 float Drag_IsDragging(entity dragger);
724 void Drag_MoveDrag(entity from, entity to);
725 .entity dragentity;
726
727 float CheatFrame(entity this)
728 {
729         BEGIN_CHEAT_FUNCTION();
730
731         // Dragging can be used as either a cheat, or a function for some objects. If sv_cheats is active,
732         // the cheat dragging is used (unlimited pickup range and any entity can be carried). If sv_cheats
733         // is disabled, normal dragging is used (limited pickup range and only dragable objects can be carried),
734         // grabbing itself no longer being accounted as cheating.
735
736         switch(0)
737         {
738                 default:
739                         if(this.maycheat || (gamestart_sv_cheats && autocvar_sv_cheats))
740                         {
741                                 // use cheat dragging if cheats are enabled
742                                 //if(Drag_IsDragging(this))
743                                         //crosshair_trace_plusvisibletriggers(this);
744                                 Drag(this, true, true);
745                         }
746                         else
747                         {
748                                 Drag(this, false, false); // execute dragging
749                         }
750                         break;
751         }
752
753         END_CHEAT_FUNCTION();
754 }
755
756
757
758
759
760 // ENTITY DRAGGING
761
762 // on dragger:
763 .float draggravity;
764 .float dragspeed; // speed of mouse wheel action
765 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
766 .vector draglocalvector; // local attachment vector of the dragentity
767 .float draglocalangle;
768 // on draggee:
769 .entity draggedby;
770 .float dragmovetype;
771
772 float Drag(entity this, float force_allow_pick, float ischeat)
773 {
774         BEGIN_CHEAT_FUNCTION();
775
776         // returns true when an entity has been picked up
777         // If pick is true, the object can also be picked up if it's not being held already
778         // If pick is false, only keep dragging the object if it's already being held
779
780         switch(0)
781         {
782                 default:
783                         if(Drag_IsDragging(this))
784                         {
785                                 if(PHYS_INPUT_BUTTON_DRAG(this))
786                                 {
787                                         if(CS(this).impulse == 10 || CS(this).impulse == 15 || CS(this).impulse == 18)
788                                         {
789                                                 Drag_MoveForward(this);
790                                                 CS(this).impulse = 0;
791                                         }
792                                         else if(CS(this).impulse == 12 || CS(this).impulse == 16 || CS(this).impulse == 19)
793                                         {
794                                                 Drag_MoveBackward(this);
795                                                 CS(this).impulse = 0;
796                                         }
797                                         else if(CS(this).impulse >= 1 && CS(this).impulse <= 9)
798                                         {
799                                                 Drag_SetSpeed(this, CS(this).impulse - 1);
800                                         }
801                                         else if(CS(this).impulse == 14)
802                                         {
803                                                 Drag_SetSpeed(this, 9);
804                                         }
805
806                                         if(frametime)
807                                                 Drag_Update(this);
808                                 }
809                                 else
810                                 {
811                                         Drag_Finish(this);
812                                 }
813                         }
814                         else
815                         {
816                                 if(Drag_CanDrag(this))
817                                         if(PHYS_INPUT_BUTTON_DRAG(this))
818                                         {
819                                                 crosshair_trace_plusvisibletriggers(this);
820                                                 entity e = trace_ent;
821                                                 float pick = force_allow_pick;
822                                                 if (e && !pick)
823                                                 {
824                                                         // pick is true if the object can be picked up. While an object is being carried, the Drag() function
825                                                         // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
826                                                         // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
827                                                         // it goes out of range while slinging it around.
828
829                                                         if(vdist(this.origin - e.origin, <=, autocvar_g_grab_range))
830                                                         {
831                                                                 switch(e.grab)
832                                                                 {
833                                                                         case 0: // can't grab
834                                                                                 break;
835                                                                         case 1: // owner can grab
836                                                                                 if(e.owner == this || e.realowner == this)
837                                                                                         pick = true;
838                                                                                 break;
839                                                                         case 2: // owner and team mates can grab
840                                                                                 if(SAME_TEAM(e.owner, this) || SAME_TEAM(e.realowner, this) || e.team == this.team)
841                                                                                         pick = true;
842                                                                                 break;
843                                                                         case 3: // anyone can grab
844                                                                                 pick = true;
845                                                                                 break;
846                                                                         default:
847                                                                                 break;
848                                                                 }
849                                                         }
850                                                 }
851                                                 // Find e and pick
852                                                 if(e && pick)
853                                                         if(Drag_IsDraggable(e))
854                                                         {
855                                                                 if(ischeat)
856                                                                         IS_CHEAT(this, 0, 0, CHRAME_DRAG);
857                                                                 if(e.draggedby)
858                                                                         Drag_Finish(e.draggedby);
859                                                                 if(e.tag_entity)
860                                                                         detach_sameorigin(e);
861                                                                 Drag_Begin(this, e, trace_endpos);
862                                                                 if(ischeat)
863                                                                         DID_CHEAT();
864                                                                 return true;
865                                                         }
866                                         }
867                         }
868                         break;
869         }
870         return false;
871 }
872
873 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
874 {
875         float tagscale;
876
877         draggee.dragmovetype = draggee.move_movetype;
878         draggee.draggravity = draggee.gravity;
879         set_movetype(draggee, MOVETYPE_WALK);
880         draggee.gravity = 0.00001;
881         UNSET_ONGROUND(draggee);
882         draggee.draggedby = dragger;
883
884         dragger.dragentity = draggee;
885
886         dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
887         dragger.draglocalangle = draggee.angles.y - dragger.v_angle.y;
888         touchpoint = touchpoint - gettaginfo(draggee, 0);
889         tagscale = (vlen(v_forward) ** -2);
890         dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
891         dragger.draglocalvector_y = touchpoint * v_right * tagscale;
892         dragger.draglocalvector_z = touchpoint * v_up * tagscale;
893
894         dragger.dragspeed = 64;
895 }
896
897 void Drag_Finish(entity dragger)
898 {
899         entity draggee;
900         draggee = dragger.dragentity;
901         if(dragger)
902                 dragger.dragentity = NULL;
903         draggee.draggedby = NULL;
904         set_movetype(draggee, draggee.dragmovetype);
905         draggee.gravity = draggee.draggravity;
906
907         switch(draggee.move_movetype)
908         {
909                 case MOVETYPE_TOSS:
910                 case MOVETYPE_WALK:
911                 case MOVETYPE_STEP:
912                 case MOVETYPE_FLYMISSILE:
913                 case MOVETYPE_BOUNCE:
914                 case MOVETYPE_BOUNCEMISSILE:
915                 case MOVETYPE_PHYSICS:
916                         break;
917                 default:
918                         draggee.velocity = '0 0 0';
919                         break;
920         }
921
922         if((draggee.flags & FL_ITEM) && (vdist(draggee.velocity, <, 32)))
923         {
924                 draggee.velocity = '0 0 0';
925                 SET_ONGROUND(draggee); // floating items are FUN
926         }
927 }
928
929 float Drag_IsDraggable(entity draggee)
930 {
931         // TODO add more checks for bad stuff here
932         if(draggee == NULL)
933                 return false;
934         if(draggee.classname == "func_bobbing")
935                 return false;
936         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)
937                 return false;
938         if(draggee.classname == "plat")
939                 return false;
940         if(draggee.classname == "func_button")
941                 return false;
942 //      if(draggee.model == "")
943 //              return false;
944         if(IS_SPEC(draggee))
945                 return false;
946         if(IS_OBSERVER(draggee))
947                 return false;
948         if(draggee.classname == "exteriorweaponentity")
949                 return false;
950         if(draggee.classname == "weaponentity")
951                 return false;
952
953         return true;
954 }
955
956 float Drag_MayChangeAngles(entity draggee)
957 {
958         // TODO add more checks for bad stuff here
959         if(substring(draggee.model, 0, 1) == "*")
960                 return false;
961         return true;
962 }
963
964 void Drag_MoveForward(entity dragger)
965 {
966         dragger.dragdistance += dragger.dragspeed;
967 }
968
969 void Drag_SetSpeed(entity dragger, float s)
970 {
971         dragger.dragspeed = (2 ** s);
972 }
973
974 void Drag_MoveBackward(entity dragger)
975 {
976         dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
977 }
978
979 void Drag_Update(entity dragger)
980 {
981         vector curorigin, neworigin, goodvelocity;
982         float f;
983         entity draggee;
984
985         draggee = dragger.dragentity;
986         UNSET_ONGROUND(draggee);
987
988         curorigin = gettaginfo(draggee, 0);
989         curorigin = curorigin + v_forward * dragger.draglocalvector.x + v_right * dragger.draglocalvector.y + v_up * dragger.draglocalvector.z;
990         makevectors(dragger.v_angle);
991         neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
992         goodvelocity = (neworigin - curorigin) * (1 / frametime);
993
994         while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle > 180)
995                 dragger.draglocalangle += 360;
996         while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle <= -180)
997                 dragger.draglocalangle -= 360;
998
999         f = min(frametime * 10, 1);
1000         draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
1001
1002         if(Drag_MayChangeAngles(draggee))
1003                 draggee.angles_y = draggee.angles.y * (1 - f) + (dragger.v_angle.y + dragger.draglocalangle) * f;
1004
1005         draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
1006
1007         vector vecs = '0 0 0';
1008         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1009         if(dragger.(weaponentity).movedir.x > 0)
1010                 vecs = dragger.(weaponentity).movedir;
1011
1012         vector dv = v_right * -vecs_y + v_up * vecs_z;
1013
1014         te_lightning1(draggee, dragger.origin + dragger.view_ofs + dv, curorigin);
1015 }
1016
1017 float Drag_CanDrag(entity dragger)
1018 {
1019         return (!IS_DEAD(dragger)) || (IS_PLAYER(dragger));
1020 }
1021
1022 float Drag_IsDragging(entity dragger)
1023 {
1024         if(!dragger.dragentity)
1025                 return false;
1026         if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
1027         {
1028                 dragger.dragentity = NULL;
1029                 return false;
1030         }
1031         if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity))
1032         {
1033                 Drag_Finish(dragger);
1034                 return false;
1035         }
1036         return true;
1037 }
1038
1039 void Drag_MoveDrag(entity from, entity to)
1040 {
1041         if(from.draggedby)
1042         {
1043                 to.draggedby = from.draggedby;
1044                 to.draggedby.dragentity = to;
1045                 from.draggedby = NULL;
1046         }
1047 }
1048
1049 void DragBox_Think(entity this)
1050 {
1051         if(this.aiment && this.enemy)
1052         {
1053                 this.origin_x = (this.aiment.origin.x + this.enemy.origin.x) * 0.5;
1054                 this.origin_y = (this.aiment.origin.y + this.enemy.origin.y) * 0.5;
1055                 this.origin_z = (this.aiment.origin.z + this.enemy.origin.z) * 0.5;
1056                 this.maxs_x = fabs(this.aiment.origin.x - this.enemy.origin.x) * 0.5;
1057                 this.maxs_y = fabs(this.aiment.origin.y - this.enemy.origin.y) * 0.5;
1058                 this.maxs_z = fabs(this.aiment.origin.z - this.enemy.origin.z) * 0.5;
1059                 this.mins = -1 * this.maxs;
1060                 setorigin(this, this.origin);
1061                 setsize(this, this.mins, this.maxs); // link edict
1062         }
1063
1064         if(this.cnt == -1) // actually race_place -1
1065         {
1066                 // show "10 10" for qualifying spawns
1067                 setmodel(this.killindicator, MDL_NUM(10));
1068                 setmodel(this.killindicator.killindicator, MDL_NUM(10));
1069         }
1070         else if(this.cnt == -2) // actually race_place 0
1071         {
1072                 // show "10 0" for loser spawns
1073                 setmodel(this.killindicator, MDL_NUM(10));
1074                 setmodel(this.killindicator.killindicator, MDL_NUM(0));
1075         }
1076         else
1077         {
1078                 setmodel(this.killindicator, MDL_NUM(this.cnt % 10));
1079                 setmodel(this.killindicator.killindicator, MDL_NUM(floor(this.cnt / 10)));
1080         }
1081
1082         this.nextthink = time;
1083 }
1084
1085 #endif