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