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