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