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