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