3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/miscfunctions.qh>
6 #include <common/effects/all.qh>
7 #include <server/resources.qh>
8 #include <server/main.qh>
9 #include <server/world.qh>
12 #include "clientkill.qh"
15 #include "../common/mapobjects/teleporters.qh"
17 #include <server/mutators/_mod.qh>
19 #include "weapons/tracing.qh"
21 #include "../common/constants.qh"
22 #include "../common/deathtypes/all.qh"
23 #include "../common/util.qh"
25 #include <common/physics/player.qh>
27 #include "../common/monsters/_mod.qh"
29 #include <common/items/_mod.qh>
31 #include <common/weapons/_all.qh>
33 #include "../common/mapobjects/subs.qh"
34 #include <common/mapobjects/triggers.qh>
36 #include "../common/mapobjects/func/breakable.qh"
38 #include "../lib/csqcmodel/sv_model.qh"
40 #include "../lib/warpzone/anglestransform.qh"
41 #include "../lib/warpzone/util_server.qh"
45 float CheatImpulse(entity this, int imp) { return 0; }
46 float CheatCommand(entity this, int argc) { return 0; }
47 float CheatFrame(entity this) { return 0; }
48 void CheatInit() { cheatcount_total = world.cheatcount; }
49 void CheatShutdown() { }
50 void Drag_MoveDrag(entity from, entity to) { }
55 float gamestart_sv_cheats;
61 gamestart_sv_cheats = autocvar_sv_cheats;
68 float CheatsAllowed(entity this, float i, int argc, float fr) // the cheat gets passed as argument for possible future ACL checking
72 if(gamestart_sv_cheats < 2 && !IS_PLAYER(this))
75 if(i == CHIMPULSE_CLONE_MOVING.impulse || i == CHIMPULSE_CLONE_STANDING.impulse)
76 if(this.lip < autocvar_sv_clones)
83 if(gamestart_sv_cheats && autocvar_sv_cheats)
86 // if we get here, player is not allowed to cheat. Log it.
88 bprintf("Player %s^7 tried to use cheat 'impulse %d'\n", playername(this.netname, this.team, false), i);
90 bprintf("Player %s^7 tried to use cheat '%s'\n", playername(this.netname, this.team, false), argv(0));
92 bprintf("Player %s^7 tried to use cheat frame %d\n", playername(this.netname, this.team, false), fr);
94 bprintf("Player %s^7 tried to use an unknown cheat\n", playername(this.netname, this.team, false));
99 #define BEGIN_CHEAT_FUNCTION() \
100 float cheating = 0, attempting = 0
101 #define DID_CHEAT() \
103 #define ADD_CHEATS(e,n) \
104 cheatcount_total += n; \
106 #define END_CHEAT_FUNCTION() \
107 ADD_CHEATS(this, cheating); \
109 #define IS_CHEAT(ent,i,argc,fr) \
110 if((++attempting, !CheatsAllowed(ent,i,argc,fr))) \
113 float num_autoscreenshot;
114 void info_autoscreenshot_findtarget(entity this)
117 e = find(NULL, targetname, this.target);
120 objerror(this, "Missing target. FAIL!");
123 vector a = vectoangles(e.origin - this.origin);
124 a.x = -a.x; // don't ask
127 // we leave Rick Roll alone
129 spawnfunc(info_autoscreenshot)
131 // this one just has to exist
132 if(++num_autoscreenshot > autocvar_g_max_info_autoscreenshot)
134 objerror(this, "Too many info_autoscreenshot entitites. FAIL!");
137 if(this.target != "")
138 InitializeEntity(this, info_autoscreenshot_findtarget, INITPRIO_FINDTARGET);
141 float CheatImpulse(entity this, int imp)
143 BEGIN_CHEAT_FUNCTION();
148 case CHIMPULSE_SPEEDRUN_INIT.impulse: // deploy personal waypoint
149 // shared with regular waypoint init, so this is not a cheat by itself
152 this.personal = new(personal_wp);
154 this.personal.origin = this.origin;
155 this.personal.v_angle = this.v_angle;
156 this.personal.velocity = this.velocity;
157 SetResource(this.personal, RES_ROCKETS, GetResource(this, RES_ROCKETS));
158 SetResource(this.personal, RES_BULLETS, GetResource(this, RES_BULLETS));
159 SetResource(this.personal, RES_CELLS, GetResource(this, RES_CELLS));
160 SetResource(this.personal, RES_PLASMA, GetResource(this, RES_PLASMA));
161 SetResource(this.personal, RES_SHELLS, GetResource(this, RES_SHELLS));
162 SetResource(this.personal, RES_FUEL, GetResource(this, RES_FUEL));
163 SetResource(this.personal, RES_HEALTH, max(1, GetResource(this, RES_HEALTH)));
164 SetResource(this.personal, RES_ARMOR, GetResource(this, RES_ARMOR));
165 STAT(WEAPONS, this.personal) = STAT(WEAPONS, this);
166 STAT(BUFFS, this.personal) = STAT(BUFFS, this);
167 STAT(BUFF_TIME, this.personal) = STAT(BUFF_TIME, this);
168 this.personal.items = this.items;
169 this.personal.pauserotarmor_finished = this.pauserotarmor_finished;
170 this.personal.pauserothealth_finished = this.pauserothealth_finished;
171 this.personal.pauserotfuel_finished = this.pauserotfuel_finished;
172 this.personal.pauseregen_finished = this.pauseregen_finished;
173 STAT(STRENGTH_FINISHED, this.personal) = STAT(STRENGTH_FINISHED, this);
174 STAT(INVINCIBLE_FINISHED, this.personal) = STAT(INVINCIBLE_FINISHED, this);
175 this.personal.teleport_time = time;
176 break; // this part itself doesn't cheat, so let's not count this
177 case CHIMPULSE_CLONE_MOVING.impulse:
178 IS_CHEAT(this, imp, 0, 0);
179 makevectors (this.v_angle);
180 this.velocity = this.velocity + v_forward * 300;
183 this.velocity = this.velocity - v_forward * 300;
186 case CHIMPULSE_CLONE_STANDING.impulse:
187 IS_CHEAT(this, imp, 0, 0);
192 case CHIMPULSE_GIVE_ALL.impulse:
193 IS_CHEAT(this, imp, 0, 0);
194 CheatCommand(this, tokenize_console("give all"));
195 break; // already counted as cheat
196 case CHIMPULSE_SPEEDRUN.impulse:
197 if(!autocvar_g_allow_checkpoints)
198 IS_CHEAT(this, imp, 0, 0);
201 this.speedrunning = true;
202 tracebox(this.personal.origin, this.mins, this.maxs, this.personal.origin, MOVE_WORLDONLY, this);
205 sprint(this, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
209 // Abort speedrun, teleport back
210 setorigin(this, this.personal.origin);
211 this.oldvelocity = this.velocity = this.personal.velocity;
212 this.angles = this.personal.v_angle;
213 this.fixangle = true;
215 MUTATOR_CALLHOOK(AbortSpeedrun, this);
218 SetResource(this, RES_ROCKETS, GetResource(this.personal, RES_ROCKETS));
219 SetResource(this, RES_BULLETS, GetResource(this.personal, RES_BULLETS));
220 SetResource(this, RES_CELLS, GetResource(this.personal, RES_CELLS));
221 SetResource(this, RES_PLASMA, GetResource(this.personal, RES_PLASMA));
222 SetResource(this, RES_SHELLS, GetResource(this.personal, RES_SHELLS));
223 SetResource(this, RES_FUEL, GetResource(this.personal, RES_FUEL));
224 SetResource(this, RES_HEALTH, GetResource(this.personal, RES_HEALTH));
225 SetResource(this, RES_ARMOR, GetResource(this.personal, RES_ARMOR));
226 STAT(WEAPONS, this) = STAT(WEAPONS, this.personal);
227 STAT(BUFFS, this) = STAT(BUFFS, this.personal);
228 STAT(BUFF_TIME, this) = STAT(BUFF_TIME, this.personal);
229 this.items = this.personal.items;
230 this.pauserotarmor_finished = time + this.personal.pauserotarmor_finished - this.personal.teleport_time;
231 this.pauserothealth_finished = time + this.personal.pauserothealth_finished - this.personal.teleport_time;
232 this.pauserotfuel_finished = time + this.personal.pauserotfuel_finished - this.personal.teleport_time;
233 this.pauseregen_finished = time + this.personal.pauseregen_finished - this.personal.teleport_time;
234 STAT(STRENGTH_FINISHED, this) = time + STAT(STRENGTH_FINISHED, this.personal) - this.personal.teleport_time;
235 STAT(INVINCIBLE_FINISHED, this) = time + STAT(INVINCIBLE_FINISHED, this.personal) - this.personal.teleport_time;
237 if(!autocvar_g_allow_checkpoints)
242 sprint(this, "UR DEAD AHAHAH))\n");
244 sprint(this, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
246 case CHIMPULSE_TELEPORT.impulse:
247 IS_CHEAT(this, imp, 0, 0);
248 if(this.move_movetype == MOVETYPE_NOCLIP)
250 e = find(NULL, classname, "info_autoscreenshot");
253 sprint(this, "Emergency teleport used info_autoscreenshot location\n");
254 setorigin(this, e.origin);
255 this.angles = e.angles;
257 // should we? this.angles_x = -this.angles_x;
258 this.fixangle = true;
259 this.velocity = '0 0 0';
264 if(MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((gamestart_sv_cheats < 2) ? 100 : 100000), 384, 384))
266 sprint(this, "Emergency teleport used random location\n");
267 this.angles_x = -this.angles.x;
268 this.fixangle = true;
269 this.velocity = '0 0 0';
273 sprint(this, "Emergency teleport could not find a good location, forget it!\n");
275 case CHIMPULSE_R00T.impulse:
276 IS_CHEAT(this, imp, 0, 0);
277 RandomSelection_Init();
278 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && DIFF_TEAM(it, this), { RandomSelection_AddEnt(it, 1, 1); });
279 if(RandomSelection_chosen_ent)
280 e = RandomSelection_chosen_ent;
284 Send_Effect(EFFECT_ROCKET_EXPLODE, e.origin, '0 0 0', 1);
285 sound(e, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
288 setorigin(e2, e.origin);
289 RadiusDamage(e2, this, 1000, 0, 128, NULL, NULL, 500, DEATH_CHEAT.m_id, DMG_NOWEP, e);
292 LOG_INFO("404 Sportsmanship not found.");
297 END_CHEAT_FUNCTION();
301 float CheatCommand(entity this, int argc)
303 BEGIN_CHEAT_FUNCTION();
311 case "pointparticles":
312 IS_CHEAT(this, 0, argc, 0);
317 // origin (0..1, on crosshair line)
321 crosshair_trace(this);
322 start = (1-f) * this.origin + f * trace_endpos;
325 Send_Effect_(argv(1), start, end, f);
329 sprint(this, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
331 case "trailparticles":
332 IS_CHEAT(this, 0, argc, 0);
337 effectnum = _particleeffectnum(argv(1));
338 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
339 traceline(w_shotorg, w_shotorg + w_shotdir * max_shot_distance, MOVE_NORMAL, this);
340 __trailparticles(this, effectnum, w_shotorg, trace_endpos);
344 sprint(this, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
347 IS_CHEAT(this, 0, argc, 0);
353 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
354 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, this);
355 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
357 sprint(this, "cannot make stuff there (bad surface)\n");
362 e.model = strzone(argv(1));
363 e.mdl = "rocket_explode";
364 SetResourceExplicit(e, RES_HEALTH, 1000);
365 setorigin(e, trace_endpos);
366 e.effects = EF_NOMODELFLAGS;
369 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
370 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
372 spawnfunc_func_breakable(e);
376 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
380 sprint(this, "cannot make stuff there (no space)\n");
390 sprint(this, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
393 IS_CHEAT(this, 0, argc, 0);
396 race_ImposePenaltyTime(this, stof(argv(1)), argv(2));
400 sprint(this, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
402 case "dragbox_spawn": {
403 IS_CHEAT(this, 0, argc, 0);
404 entity e = new(dragbox_box);
405 setthink(e, DragBox_Think);
407 e.solid = -1; // black
408 setmodel(e, MDL_Null); // network it
410 e.cnt = stof(argv(1));
412 e.cnt = max(0, drag_lastcnt);
414 e.aiment = new(dragbox_corner_1);
416 setmodel(e.aiment, MDL_MARKER);
418 setsize(e.aiment, '0 0 0', '0 0 0');
420 setorigin(e.aiment, stov(argv(2)));
423 crosshair_trace(this);
424 setorigin(e.aiment, trace_endpos);
427 e.enemy = new(dragbox_corner_2);
429 setmodel(e.enemy, MDL_MARKER);
431 setsize(e.enemy, '0 0 0', '0 0 0');
432 end = normalize(this.origin + this.view_ofs - e.aiment.origin);
433 end.x = (end.x > 0) * 2 - 1;
434 end.y = (end.y > 0) * 2 - 1;
435 end.z = (end.z > 0) * 2 - 1;
437 setorigin(e.enemy, stov(argv(3)));
439 setorigin(e.enemy, e.aiment.origin + 32 * end);
441 e.killindicator = new(drag_digit);
442 e.killindicator.owner = e;
443 setattachment(e.killindicator, e, "");
444 setorigin(e.killindicator, '0 0 -8');
445 e.killindicator.killindicator = new(drag_digit);
446 e.killindicator.killindicator.owner = e;
447 setattachment(e.killindicator.killindicator, e, "");
448 setorigin(e.killindicator.killindicator, '0 0 8');
452 case "dragpoint_spawn": {
453 IS_CHEAT(this, 0, argc, 0);
454 entity e = new(dragpoint);
455 setthink(e, DragBox_Think);
457 e.solid = 0; // nothing special
458 setmodel(e, MDL_MARKER);
459 setsize(e, STAT(PL_MIN, this), STAT(PL_MAX, this));
462 e.cnt = stof(argv(1));
464 e.cnt = drag_lastcnt;
466 setorigin(e, stov(argv(2)));
469 crosshair_trace(this);
470 setorigin(e, trace_endpos + normalize(this.origin + this.view_ofs - trace_endpos));
471 move_out_of_solid(e);
474 e.killindicator = new(drag_digit);
475 e.killindicator.owner = e;
476 setattachment(e.killindicator, e, "");
477 setorigin(e.killindicator, '0 0 40');
478 e.killindicator.killindicator = new(drag_digit);
479 e.killindicator.killindicator.owner = e;
480 setattachment(e.killindicator.killindicator, e, "");
481 setorigin(e.killindicator.killindicator, '0 0 56');
486 IS_CHEAT(this, 0, argc, 0);
487 RandomSelection_Init();
488 crosshair_trace(this);
489 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
490 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
491 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
492 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
493 if(RandomSelection_chosen_ent)
495 delete(RandomSelection_chosen_ent.killindicator.killindicator);
496 delete(RandomSelection_chosen_ent.killindicator);
497 if(RandomSelection_chosen_ent.aiment)
498 delete(RandomSelection_chosen_ent.aiment);
499 if(RandomSelection_chosen_ent.enemy)
500 delete(RandomSelection_chosen_ent.enemy);
501 delete(RandomSelection_chosen_ent);
506 IS_CHEAT(this, 0, argc, 0);
509 RandomSelection_Init();
510 crosshair_trace(this);
511 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
512 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
513 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
514 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
515 if(RandomSelection_chosen_ent)
517 if(substring(argv(1), 0, 1) == "*")
518 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
520 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
525 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
528 IS_CHEAT(this, 0, argc, 0);
531 f = fopen(argv(1), FILE_WRITE);
532 fputs(f, "cmd drag_clear\n");
533 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
535 fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
537 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
539 fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
545 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
547 case "drag_saveraceent":
548 IS_CHEAT(this, 0, argc, 0);
551 f = fopen(argv(1), FILE_WRITE);
552 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
555 fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
556 fputs(f, strcat("\"origin\" \"", ftos(e.absmin.x), " ", ftos(e.absmin.y), " ", ftos(e.absmin.z), "\"\n"));
557 fputs(f, strcat("\"maxs\" \"", ftos(e.absmax.x - e.absmin.x), " ", ftos(e.absmax.y - e.absmin.y), " ", ftos(e.absmax.z - e.absmin.z), "\"\n"));
558 fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
559 fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
562 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
566 for(entity ent = NULL; (ent = find(ent, classname, "dragbox_box")); )
568 if((e.cnt <= 0 && ent.cnt == 0) || e.cnt == ent.cnt)
570 start = start + ent.origin;
574 start *= 1 / effectnum;
576 fputs(f, "\"classname\" \"info_player_race\"\n");
577 fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
578 fputs(f, strcat("\"origin\" \"", ftos(e.origin.x), " ", ftos(e.origin.y), " ", ftos(e.origin.z), "\"\n"));
581 fputs(f, "\"target\" \"checkpoint0\"\n");
582 fputs(f, "\"race_place\" \"0\"\n");
586 fputs(f, "\"target\" \"checkpoint0\"\n");
587 fputs(f, "\"race_place\" \"-1\"\n");
591 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
594 // these need race_place
597 for(entity ent = NULL; (ent = find(ent, classname, "dragpoint")); )
600 if(vlen2(ent.origin - start) < vlen2(e.origin - start))
602 else if(vlen2(ent.origin - start) == vlen2(e.origin - start) && etof(ent) < etof(e))
605 fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
614 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
617 IS_CHEAT(this, 0, argc, 0);
618 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
620 for(entity e = NULL; (e = find(e, classname, "dragbox_corner_1")); )
622 for(entity e = NULL; (e = find(e, classname, "dragbox_corner_2")); )
624 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
626 for(entity e = NULL; (e = find(e, classname, "drag_digit")); )
631 IS_CHEAT(this, 0, argc, 0);
632 BITXOR_ASSIGN(this.flags, FL_GODMODE);
633 if(this.flags & FL_GODMODE)
635 sprint(this, "godmode ON\n");
639 sprint(this, "godmode OFF\n");
642 IS_CHEAT(this, 0, argc, 0);
643 BITXOR_ASSIGN(this.flags, FL_NOTARGET);
644 if(this.flags & FL_NOTARGET)
646 sprint(this, "notarget ON\n");
650 sprint(this, "notarget OFF\n");
653 IS_CHEAT(this, 0, argc, 0);
654 if(this.move_movetype != MOVETYPE_NOCLIP)
656 set_movetype(this, MOVETYPE_NOCLIP);
657 sprint(this, "noclip ON\n");
662 set_movetype(this, MOVETYPE_WALK);
663 sprint(this, "noclip OFF\n");
667 IS_CHEAT(this, 0, argc, 0);
668 if(this.move_movetype != MOVETYPE_FLY)
670 set_movetype(this, MOVETYPE_FLY);
671 sprint(this, "flymode ON\n");
676 set_movetype(this, MOVETYPE_WALK);
677 sprint(this, "flymode OFF\n");
681 IS_CHEAT(this, 0, argc, 0);
682 if(GiveItems(this, 1, argc))
686 IS_CHEAT(this, 0, argc, 0);
689 SUB_UseTargets(e, this, NULL);
694 IS_CHEAT(this, 0, argc, 0);
696 e2.killtarget = argv(1);
697 SUB_UseTargets(e2, this, NULL);
701 case "teleporttotarget":
702 IS_CHEAT(this, 0, argc, 0);
703 entity ent = new(cheattriggerteleport);
704 setorigin(ent, ent.origin);
705 ent.target = argv(1);
706 teleport_findtarget(ent);
709 Simple_TeleportPlayer(ent, this);
716 END_CHEAT_FUNCTION();
721 float CheatFrame(entity this)
723 BEGIN_CHEAT_FUNCTION();
725 // Dragging can be used as either a cheat, or a function for some objects. If sv_cheats is active,
726 // the cheat dragging is used (unlimited pickup range and any entity can be carried). If sv_cheats
727 // is disabled, normal dragging is used (limited pickup range and only dragable objects can be carried),
728 // grabbing itself no longer being accounted as cheating.
733 if(this.maycheat || (gamestart_sv_cheats && autocvar_sv_cheats))
735 // use cheat dragging if cheats are enabled
736 //if(Drag_IsDragging(this))
737 //crosshair_trace_plusvisibletriggers(this);
738 Drag(this, true, true);
742 Drag(this, false, false); // execute dragging
747 END_CHEAT_FUNCTION();
758 .float dragspeed; // speed of mouse wheel action
759 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
760 .vector draglocalvector; // local attachment vector of the dragentity
761 .float draglocalangle;
766 float Drag(entity this, float force_allow_pick, float ischeat)
768 BEGIN_CHEAT_FUNCTION();
770 // returns true when an entity has been picked up
771 // If pick is true, the object can also be picked up if it's not being held already
772 // If pick is false, only keep dragging the object if it's already being held
777 if(Drag_IsDragging(this))
779 if(PHYS_INPUT_BUTTON_DRAG(this))
781 if(CS(this).impulse == 10 || CS(this).impulse == 15 || CS(this).impulse == 18)
783 Drag_MoveForward(this);
784 CS(this).impulse = 0;
786 else if(CS(this).impulse == 12 || CS(this).impulse == 16 || CS(this).impulse == 19)
788 Drag_MoveBackward(this);
789 CS(this).impulse = 0;
791 else if(CS(this).impulse >= 1 && CS(this).impulse <= 9)
793 Drag_SetSpeed(this, CS(this).impulse - 1);
795 else if(CS(this).impulse == 14)
797 Drag_SetSpeed(this, 9);
808 else if(Drag_CanDrag(this) && PHYS_INPUT_BUTTON_DRAG(this))
810 crosshair_trace_plusvisibletriggers(this);
811 entity e = trace_ent;
812 float pick = force_allow_pick;
813 if (e && !pick && vdist(this.origin - e.origin, <=, autocvar_g_grab_range))
815 // pick is true if the object can be picked up. While an object is being carried, the Drag() function
816 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
817 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
818 // it goes out of range while slinging it around.
822 case 0: // can't grab
824 case 1: // owner can grab
825 if(e.owner == this || e.realowner == this)
828 case 2: // owner and team mates can grab
829 if(SAME_TEAM(e.owner, this) || SAME_TEAM(e.realowner, this) || e.team == this.team)
832 case 3: // anyone can grab
840 if(e && pick && Drag_IsDraggable(e, this))
843 IS_CHEAT(this, 0, 0, CHRAME_DRAG);
845 Drag_Finish(e.draggedby);
847 detach_sameorigin(e);
848 Drag_Begin(this, e, trace_endpos);
859 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
863 draggee.dragmovetype = draggee.move_movetype;
864 draggee.draggravity = draggee.gravity;
865 set_movetype(draggee, MOVETYPE_WALK);
866 draggee.gravity = 0.00001;
867 UNSET_ONGROUND(draggee);
868 draggee.draggedby = dragger;
870 dragger.dragentity = draggee;
872 dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
873 dragger.draglocalangle = draggee.angles.y - dragger.v_angle.y;
874 touchpoint = touchpoint - gettaginfo(draggee, 0);
875 tagscale = (vlen(v_forward) ** -2);
876 dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
877 dragger.draglocalvector_y = touchpoint * v_right * tagscale;
878 dragger.draglocalvector_z = touchpoint * v_up * tagscale;
880 dragger.dragspeed = 64;
883 void Drag_Finish(entity dragger)
886 draggee = dragger.dragentity;
888 dragger.dragentity = NULL;
889 draggee.draggedby = NULL;
890 set_movetype(draggee, draggee.dragmovetype);
891 draggee.gravity = draggee.draggravity;
893 switch(draggee.move_movetype)
898 case MOVETYPE_FLYMISSILE:
899 case MOVETYPE_BOUNCE:
900 case MOVETYPE_BOUNCEMISSILE:
901 case MOVETYPE_PHYSICS:
904 draggee.velocity = '0 0 0';
908 if((draggee.flags & FL_ITEM) && (vdist(draggee.velocity, <, 32)))
910 draggee.velocity = '0 0 0';
911 SET_ONGROUND(draggee); // floating items are FUN
915 bool drag_undraggable(entity draggee, entity dragger)
917 // stuff probably shouldn't need this, we should figure out why they do!
918 // exceptions of course are observers and weapon entities, where things mess up
922 float Drag_IsDraggable(entity draggee, entity dragger)
924 // TODO add more checks for bad stuff here
927 if(draggee.classname == "door") // FIXME find out why these must be excluded, or work around the problem (trying to drag these causes like 4 fps)
928 return false; // probably due to BSP collision
929 //if(draggee.model == "")
932 return ((draggee.draggable) ? draggee.draggable(draggee, dragger) : true);
935 float Drag_MayChangeAngles(entity draggee)
937 // TODO add more checks for bad stuff here
938 if(substring(draggee.model, 0, 1) == "*")
943 void Drag_MoveForward(entity dragger)
945 dragger.dragdistance += dragger.dragspeed;
948 void Drag_SetSpeed(entity dragger, float s)
950 dragger.dragspeed = (2 ** s);
953 void Drag_MoveBackward(entity dragger)
955 dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
958 void Drag_Update(entity dragger)
960 vector curorigin, neworigin, goodvelocity;
964 draggee = dragger.dragentity;
965 UNSET_ONGROUND(draggee);
967 curorigin = gettaginfo(draggee, 0);
968 curorigin = curorigin + v_forward * dragger.draglocalvector.x + v_right * dragger.draglocalvector.y + v_up * dragger.draglocalvector.z;
969 makevectors(dragger.v_angle);
970 neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
971 goodvelocity = (neworigin - curorigin) * (1 / frametime);
973 while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle > 180)
974 dragger.draglocalangle += 360;
975 while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle <= -180)
976 dragger.draglocalangle -= 360;
978 f = min(frametime * 10, 1);
979 draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
981 if(Drag_MayChangeAngles(draggee))
982 draggee.angles_y = draggee.angles.y * (1 - f) + (dragger.v_angle.y + dragger.draglocalangle) * f;
984 draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
986 vector vecs = '0 0 0';
987 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
988 if(dragger.(weaponentity).movedir.x > 0)
989 vecs = dragger.(weaponentity).movedir;
991 vector dv = v_right * -vecs_y + v_up * vecs_z;
993 te_lightning1(draggee, dragger.origin + dragger.view_ofs + dv, curorigin);
996 float Drag_CanDrag(entity dragger)
998 return (!IS_DEAD(dragger)) || (IS_PLAYER(dragger));
1001 float Drag_IsDragging(entity dragger)
1003 if(!dragger.dragentity)
1005 if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
1007 dragger.dragentity = NULL;
1010 if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity, dragger))
1012 Drag_Finish(dragger);
1018 void Drag_MoveDrag(entity from, entity to)
1022 to.draggedby = from.draggedby;
1023 to.draggedby.dragentity = to;
1024 from.draggedby = NULL;
1028 void DragBox_Think(entity this)
1030 if(this.aiment && this.enemy)
1032 this.origin_x = (this.aiment.origin.x + this.enemy.origin.x) * 0.5;
1033 this.origin_y = (this.aiment.origin.y + this.enemy.origin.y) * 0.5;
1034 this.origin_z = (this.aiment.origin.z + this.enemy.origin.z) * 0.5;
1035 this.maxs_x = fabs(this.aiment.origin.x - this.enemy.origin.x) * 0.5;
1036 this.maxs_y = fabs(this.aiment.origin.y - this.enemy.origin.y) * 0.5;
1037 this.maxs_z = fabs(this.aiment.origin.z - this.enemy.origin.z) * 0.5;
1038 this.mins = -1 * this.maxs;
1039 setorigin(this, this.origin);
1040 setsize(this, this.mins, this.maxs); // link edict
1043 if(this.cnt == -1) // actually race_place -1
1045 // show "10 10" for qualifying spawns
1046 setmodel(this.killindicator, MDL_NUM(10));
1047 setmodel(this.killindicator.killindicator, MDL_NUM(10));
1049 else if(this.cnt == -2) // actually race_place 0
1051 // show "10 0" for loser spawns
1052 setmodel(this.killindicator, MDL_NUM(10));
1053 setmodel(this.killindicator.killindicator, MDL_NUM(0));
1057 setmodel(this.killindicator, MDL_NUM(this.cnt % 10));
1058 setmodel(this.killindicator.killindicator, MDL_NUM(floor(this.cnt / 10)));
1061 this.nextthink = time;