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