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