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