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