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>
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) { }
43 float gamestart_sv_cheats;
49 gamestart_sv_cheats = autocvar_sv_cheats;
56 float CheatsAllowed(entity this, float i, int argc, float fr) // the cheat gets passed as argument for possible future ACL checking
60 if(gamestart_sv_cheats < 2 && !IS_PLAYER(this))
63 if(i == CHIMPULSE_CLONE_MOVING.impulse || i == CHIMPULSE_CLONE_STANDING.impulse)
64 if(this.lip < autocvar_sv_clones)
71 if(gamestart_sv_cheats && autocvar_sv_cheats)
74 // if we get here, player is not allowed to cheat. Log it.
76 bprintf("Player %s^7 tried to use cheat 'impulse %d'\n", playername(this.netname, this.team, false), i);
78 bprintf("Player %s^7 tried to use cheat '%s'\n", playername(this.netname, this.team, false), argv(0));
80 bprintf("Player %s^7 tried to use cheat frame %d\n", playername(this.netname, this.team, false), fr);
82 bprintf("Player %s^7 tried to use an unknown cheat\n", playername(this.netname, this.team, false));
87 #define BEGIN_CHEAT_FUNCTION() \
88 float cheating = 0, attempting = 0
91 #define ADD_CHEATS(e,n) \
92 cheatcount_total += n; \
94 #define END_CHEAT_FUNCTION() \
95 ADD_CHEATS(this, cheating); \
97 #define IS_CHEAT(ent,i,argc,fr) \
98 if((++attempting, !CheatsAllowed(ent,i,argc,fr))) \
101 float num_autoscreenshot;
102 void info_autoscreenshot_findtarget(entity this)
105 e = find(NULL, targetname, this.target);
108 objerror(this, "Missing target. FAIL!");
111 vector a = vectoangles(e.origin - this.origin);
112 a.x = -a.x; // don't ask
115 // we leave Rick Roll alone
117 spawnfunc(info_autoscreenshot)
119 // this one just has to exist
120 if(++num_autoscreenshot > autocvar_g_max_info_autoscreenshot)
122 objerror(this, "Too many info_autoscreenshot entitites. FAIL!");
125 if(this.target != "")
126 InitializeEntity(this, info_autoscreenshot_findtarget, INITPRIO_FINDTARGET);
129 float CheatImpulse(entity this, int imp)
131 BEGIN_CHEAT_FUNCTION();
136 case CHIMPULSE_SPEEDRUN_INIT.impulse: // deploy personal waypoint
137 // shared with regular waypoint init, so this is not a cheat by itself
140 this.personal = new(personal_wp);
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;
171 this.velocity = this.velocity - v_forward * 300;
174 case CHIMPULSE_CLONE_STANDING.impulse:
175 IS_CHEAT(this, imp, 0, 0);
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);
189 this.speedrunning = true;
190 tracebox(this.personal.origin, this.mins, this.maxs, this.personal.origin, MOVE_WORLDONLY, this);
193 sprint(this, "Cannot move there, cheater - only waypoints set using g_waypointsprite_personal work\n");
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;
203 MUTATOR_CALLHOOK(AbortSpeedrun, this);
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;
225 if(!autocvar_g_allow_checkpoints)
230 sprint(this, "UR DEAD AHAHAH))\n");
232 sprint(this, "No waypoint set, cheater (use g_waypointsprite_personal to set one)\n");
234 case CHIMPULSE_TELEPORT.impulse:
235 IS_CHEAT(this, imp, 0, 0);
236 if(this.move_movetype == MOVETYPE_NOCLIP)
238 e = find(NULL, classname, "info_autoscreenshot");
241 sprint(this, "Emergency teleport used info_autoscreenshot location\n");
242 setorigin(this, e.origin);
243 this.angles = e.angles;
245 // should we? this.angles_x = -this.angles_x;
246 this.fixangle = true;
247 this.velocity = '0 0 0';
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))
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';
261 sprint(this, "Emergency teleport could not find a good location, forget it!\n");
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;
272 Send_Effect(EFFECT_ROCKET_EXPLODE, e.origin, '0 0 0', 1);
273 sound(e, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
276 setorigin(e2, e.origin);
277 RadiusDamage(e2, this, 1000, 0, 128, NULL, NULL, 500, DEATH_CHEAT.m_id, DMG_NOWEP, e);
280 LOG_INFO("404 Sportsmanship not found.");
285 END_CHEAT_FUNCTION();
289 float CheatCommand(entity this, int argc)
291 BEGIN_CHEAT_FUNCTION();
299 case "pointparticles":
300 IS_CHEAT(this, 0, argc, 0);
305 // origin (0..1, on crosshair line)
309 crosshair_trace(this);
310 start = (1-f) * this.origin + f * trace_endpos;
313 Send_Effect_(argv(1), start, end, f);
317 sprint(this, "Usage: sv_cheats 1; restart; cmd pointparticles effectname position(0..1) velocityvector multiplier\n");
319 case "trailparticles":
320 IS_CHEAT(this, 0, argc, 0);
325 effectnum = _particleeffectnum(argv(1));
326 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
327 traceline(w_shotorg, w_shotorg + w_shotdir * max_shot_distance, MOVE_NORMAL, this);
328 __trailparticles(this, effectnum, w_shotorg, trace_endpos);
332 sprint(this, "Usage: sv_cheats 1; restart; cmd trailparticles effectname\n");
335 IS_CHEAT(this, 0, argc, 0);
341 W_SetupShot(this, weaponentities[0], false, false, SND_Null, CH_WEAPON_A, 0, 0);
342 traceline(w_shotorg, w_shotorg + w_shotdir * 2048, MOVE_NORMAL, this);
343 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || trace_fraction == 1)
345 sprint(this, "cannot make stuff there (bad surface)\n");
350 e.model = strzone(argv(1));
351 e.mdl = "rocket_explode";
352 SetResourceExplicit(e, RES_HEALTH, 1000);
353 setorigin(e, trace_endpos);
354 e.effects = EF_NOMODELFLAGS;
357 e.angles = fixedvectoangles2(trace_plane_normal, v_forward);
358 e.angles = AnglesTransform_ApplyToAngles(e.angles, '-90 0 0'); // so unrotated models work
360 spawnfunc_func_breakable(e);
364 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_NORMAL, e);
368 sprint(this, "cannot make stuff there (no space)\n");
378 sprint(this, "Usage: sv_cheats 1; restart; cmd make models/... 0/1/2\n");
381 IS_CHEAT(this, 0, argc, 0);
384 race_ImposePenaltyTime(this, stof(argv(1)), argv(2));
388 sprint(this, "Usage: sv_cheats 1; restart; cmd penalty 5.0 AHAHAHAHAHAHAH))\n");
390 case "dragbox_spawn": {
391 IS_CHEAT(this, 0, argc, 0);
392 entity e = new(dragbox_box);
393 setthink(e, DragBox_Think);
395 e.solid = -1; // black
396 setmodel(e, MDL_Null); // network it
398 e.cnt = stof(argv(1));
400 e.cnt = max(0, drag_lastcnt);
402 e.aiment = new(dragbox_corner_1);
404 setmodel(e.aiment, MDL_MARKER);
406 setsize(e.aiment, '0 0 0', '0 0 0');
408 setorigin(e.aiment, stov(argv(2)));
411 crosshair_trace(this);
412 setorigin(e.aiment, trace_endpos);
415 e.enemy = new(dragbox_corner_2);
417 setmodel(e.enemy, MDL_MARKER);
419 setsize(e.enemy, '0 0 0', '0 0 0');
420 end = normalize(this.origin + this.view_ofs - e.aiment.origin);
421 end.x = (end.x > 0) * 2 - 1;
422 end.y = (end.y > 0) * 2 - 1;
423 end.z = (end.z > 0) * 2 - 1;
425 setorigin(e.enemy, stov(argv(3)));
427 setorigin(e.enemy, e.aiment.origin + 32 * end);
429 e.killindicator = new(drag_digit);
430 e.killindicator.owner = e;
431 setattachment(e.killindicator, e, "");
432 setorigin(e.killindicator, '0 0 -8');
433 e.killindicator.killindicator = new(drag_digit);
434 e.killindicator.killindicator.owner = e;
435 setattachment(e.killindicator.killindicator, e, "");
436 setorigin(e.killindicator.killindicator, '0 0 8');
440 case "dragpoint_spawn": {
441 IS_CHEAT(this, 0, argc, 0);
442 entity e = new(dragpoint);
443 setthink(e, DragBox_Think);
445 e.solid = 0; // nothing special
446 setmodel(e, MDL_MARKER);
447 setsize(e, STAT(PL_MIN, this), STAT(PL_MAX, this));
450 e.cnt = stof(argv(1));
452 e.cnt = drag_lastcnt;
454 setorigin(e, stov(argv(2)));
457 crosshair_trace(this);
458 setorigin(e, trace_endpos + normalize(this.origin + this.view_ofs - trace_endpos));
459 move_out_of_solid(e);
462 e.killindicator = new(drag_digit);
463 e.killindicator.owner = e;
464 setattachment(e.killindicator, e, "");
465 setorigin(e.killindicator, '0 0 40');
466 e.killindicator.killindicator = new(drag_digit);
467 e.killindicator.killindicator.owner = e;
468 setattachment(e.killindicator.killindicator, e, "");
469 setorigin(e.killindicator.killindicator, '0 0 56');
474 IS_CHEAT(this, 0, argc, 0);
475 RandomSelection_Init();
476 crosshair_trace(this);
477 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
478 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
479 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
480 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
481 if(RandomSelection_chosen_ent)
483 delete(RandomSelection_chosen_ent.killindicator.killindicator);
484 delete(RandomSelection_chosen_ent.killindicator);
485 if(RandomSelection_chosen_ent.aiment)
486 delete(RandomSelection_chosen_ent.aiment);
487 if(RandomSelection_chosen_ent.enemy)
488 delete(RandomSelection_chosen_ent.enemy);
489 delete(RandomSelection_chosen_ent);
494 IS_CHEAT(this, 0, argc, 0);
497 RandomSelection_Init();
498 crosshair_trace(this);
499 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
500 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
501 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
502 RandomSelection_AddEnt(e, 1, 1 / vlen(e.origin + (e.mins + e.maxs) * 0.5 - trace_endpos));
503 if(RandomSelection_chosen_ent)
505 if(substring(argv(1), 0, 1) == "*")
506 RandomSelection_chosen_ent.cnt = drag_lastcnt = RandomSelection_chosen_ent.cnt + stof(substring(argv(1), 1, -1));
508 RandomSelection_chosen_ent.cnt = drag_lastcnt = stof(argv(1));
513 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_setcnt cnt\n");
516 IS_CHEAT(this, 0, argc, 0);
519 f = fopen(argv(1), FILE_WRITE);
520 fputs(f, "cmd drag_clear\n");
521 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
523 fputs(f, strcat("cmd dragbox_spawn ", ftos(e.cnt), " \"", vtos(e.aiment.origin), "\" \"", vtos(e.enemy.origin), "\"\n"));
525 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
527 fputs(f, strcat("cmd dragpoint_spawn ", ftos(e.cnt), " \"", vtos(e.origin), "\"\n"));
533 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
535 case "drag_saveraceent":
536 IS_CHEAT(this, 0, argc, 0);
539 f = fopen(argv(1), FILE_WRITE);
540 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
543 fputs(f, "\"classname\" \"trigger_race_checkpoint\"\n");
544 fputs(f, strcat("\"origin\" \"", ftos(e.absmin.x), " ", ftos(e.absmin.y), " ", ftos(e.absmin.z), "\"\n"));
545 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"));
546 fputs(f, strcat("\"cnt\" \"", ftos(e.cnt), "\"\n"));
547 fputs(f, strcat("\"targetname\" \"checkpoint", ftos(e.cnt), "\"\n"));
550 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
554 for(entity ent = NULL; (ent = find(ent, classname, "dragbox_box")); )
556 if((e.cnt <= 0 && ent.cnt == 0) || e.cnt == ent.cnt)
558 start = start + ent.origin;
562 start *= 1 / effectnum;
564 fputs(f, "\"classname\" \"info_player_race\"\n");
565 fputs(f, strcat("\"angle\" \"", ftos(vectoyaw(start - e.origin)), "\"\n"));
566 fputs(f, strcat("\"origin\" \"", ftos(e.origin.x), " ", ftos(e.origin.y), " ", ftos(e.origin.z), "\"\n"));
569 fputs(f, "\"target\" \"checkpoint0\"\n");
570 fputs(f, "\"race_place\" \"0\"\n");
574 fputs(f, "\"target\" \"checkpoint0\"\n");
575 fputs(f, "\"race_place\" \"-1\"\n");
579 fputs(f, strcat("\"target\" \"checkpoint", ftos(e.cnt), "\"\n"));
582 // these need race_place
585 for(entity ent = NULL; (ent = find(ent, classname, "dragpoint")); )
588 if(vlen2(ent.origin - start) < vlen2(e.origin - start))
590 else if(vlen2(ent.origin - start) == vlen2(e.origin - start) && etof(ent) < etof(e))
593 fputs(f, strcat("\"race_place\" \"", ftos(effectnum), "\"\n"));
602 sprint(this, "Usage: sv_cheats 1; restart; cmd dragbox_save filename\n");
605 IS_CHEAT(this, 0, argc, 0);
606 for(entity e = NULL; (e = find(e, classname, "dragbox_box")); )
608 for(entity e = NULL; (e = find(e, classname, "dragbox_corner_1")); )
610 for(entity e = NULL; (e = find(e, classname, "dragbox_corner_2")); )
612 for(entity e = NULL; (e = find(e, classname, "dragpoint")); )
614 for(entity e = NULL; (e = find(e, classname, "drag_digit")); )
619 IS_CHEAT(this, 0, argc, 0);
620 BITXOR_ASSIGN(this.flags, FL_GODMODE);
621 if(this.flags & FL_GODMODE)
623 sprint(this, "godmode ON\n");
627 sprint(this, "godmode OFF\n");
630 IS_CHEAT(this, 0, argc, 0);
631 BITXOR_ASSIGN(this.flags, FL_NOTARGET);
632 if(this.flags & FL_NOTARGET)
634 sprint(this, "notarget ON\n");
638 sprint(this, "notarget OFF\n");
641 IS_CHEAT(this, 0, argc, 0);
642 if(this.move_movetype != MOVETYPE_NOCLIP)
644 set_movetype(this, MOVETYPE_NOCLIP);
645 sprint(this, "noclip ON\n");
650 set_movetype(this, MOVETYPE_WALK);
651 sprint(this, "noclip OFF\n");
655 IS_CHEAT(this, 0, argc, 0);
656 if(this.move_movetype != MOVETYPE_FLY)
658 set_movetype(this, MOVETYPE_FLY);
659 sprint(this, "flymode ON\n");
664 set_movetype(this, MOVETYPE_WALK);
665 sprint(this, "flymode OFF\n");
669 IS_CHEAT(this, 0, argc, 0);
670 if(GiveItems(this, 1, argc))
674 IS_CHEAT(this, 0, argc, 0);
677 SUB_UseTargets(e, this, NULL);
682 IS_CHEAT(this, 0, argc, 0);
684 e2.killtarget = argv(1);
685 SUB_UseTargets(e2, this, NULL);
689 case "teleporttotarget":
690 IS_CHEAT(this, 0, argc, 0);
691 entity ent = new(cheattriggerteleport);
692 setorigin(ent, ent.origin);
693 ent.target = argv(1);
694 teleport_findtarget(ent);
697 Simple_TeleportPlayer(ent, this);
704 END_CHEAT_FUNCTION();
709 float CheatFrame(entity this)
711 BEGIN_CHEAT_FUNCTION();
713 // Dragging can be used as either a cheat, or a function for some objects. If sv_cheats is active,
714 // the cheat dragging is used (unlimited pickup range and any entity can be carried). If sv_cheats
715 // is disabled, normal dragging is used (limited pickup range and only dragable objects can be carried),
716 // grabbing itself no longer being accounted as cheating.
721 if(this.maycheat || (gamestart_sv_cheats && autocvar_sv_cheats))
723 // use cheat dragging if cheats are enabled
724 //if(Drag_IsDragging(this))
725 //crosshair_trace_plusvisibletriggers(this);
726 Drag(this, true, true);
730 Drag(this, false, false); // execute dragging
735 END_CHEAT_FUNCTION();
746 .float dragspeed; // speed of mouse wheel action
747 .float dragdistance; // distance of dragentity's draglocalvector from view_ofs
748 .vector draglocalvector; // local attachment vector of the dragentity
749 .float draglocalangle;
754 float Drag(entity this, float force_allow_pick, float ischeat)
756 BEGIN_CHEAT_FUNCTION();
758 // returns true when an entity has been picked up
759 // If pick is true, the object can also be picked up if it's not being held already
760 // If pick is false, only keep dragging the object if it's already being held
765 if(Drag_IsDragging(this))
767 if(PHYS_INPUT_BUTTON_DRAG(this))
769 if(CS(this).impulse == 10 || CS(this).impulse == 15 || CS(this).impulse == 18)
771 Drag_MoveForward(this);
772 CS(this).impulse = 0;
774 else if(CS(this).impulse == 12 || CS(this).impulse == 16 || CS(this).impulse == 19)
776 Drag_MoveBackward(this);
777 CS(this).impulse = 0;
779 else if(CS(this).impulse >= 1 && CS(this).impulse <= 9)
781 Drag_SetSpeed(this, CS(this).impulse - 1);
783 else if(CS(this).impulse == 14)
785 Drag_SetSpeed(this, 9);
796 else if(Drag_CanDrag(this) && PHYS_INPUT_BUTTON_DRAG(this))
798 crosshair_trace_plusvisibletriggers(this);
799 entity e = trace_ent;
800 float pick = force_allow_pick;
801 if (e && !pick && vdist(this.origin - e.origin, <=, autocvar_g_grab_range))
803 // pick is true if the object can be picked up. While an object is being carried, the Drag() function
804 // must execute for it either way, otherwise it would cause bugs if it went out of the player's trace.
805 // This also makes sure that an object can only pe picked up if in range, but does not get dropped if
806 // it goes out of range while slinging it around.
810 case 0: // can't grab
812 case 1: // owner can grab
813 if(e.owner == this || e.realowner == this)
816 case 2: // owner and team mates can grab
817 if(SAME_TEAM(e.owner, this) || SAME_TEAM(e.realowner, this) || e.team == this.team)
820 case 3: // anyone can grab
828 if(e && pick && Drag_IsDraggable(e, this))
831 IS_CHEAT(this, 0, 0, CHRAME_DRAG);
833 Drag_Finish(e.draggedby);
835 detach_sameorigin(e);
836 Drag_Begin(this, e, trace_endpos);
847 void Drag_Begin(entity dragger, entity draggee, vector touchpoint)
851 draggee.dragmovetype = draggee.move_movetype;
852 draggee.draggravity = draggee.gravity;
853 set_movetype(draggee, MOVETYPE_WALK);
854 draggee.gravity = 0.00001;
855 UNSET_ONGROUND(draggee);
856 draggee.draggedby = dragger;
858 dragger.dragentity = draggee;
860 dragger.dragdistance = vlen(touchpoint - dragger.origin - dragger.view_ofs);
861 dragger.draglocalangle = draggee.angles.y - dragger.v_angle.y;
862 touchpoint = touchpoint - gettaginfo(draggee, 0);
863 tagscale = (vlen(v_forward) ** -2);
864 dragger.draglocalvector_x = touchpoint * v_forward * tagscale;
865 dragger.draglocalvector_y = touchpoint * v_right * tagscale;
866 dragger.draglocalvector_z = touchpoint * v_up * tagscale;
868 dragger.dragspeed = 64;
871 void Drag_Finish(entity dragger)
874 draggee = dragger.dragentity;
876 dragger.dragentity = NULL;
877 draggee.draggedby = NULL;
878 set_movetype(draggee, draggee.dragmovetype);
879 draggee.gravity = draggee.draggravity;
881 switch(draggee.move_movetype)
886 case MOVETYPE_FLYMISSILE:
887 case MOVETYPE_BOUNCE:
888 case MOVETYPE_BOUNCEMISSILE:
889 case MOVETYPE_PHYSICS:
892 draggee.velocity = '0 0 0';
896 if((draggee.flags & FL_ITEM) && (vdist(draggee.velocity, <, 32)))
898 draggee.velocity = '0 0 0';
899 SET_ONGROUND(draggee); // floating items are FUN
903 bool drag_undraggable(entity draggee, entity dragger)
905 // stuff probably shouldn't need this, we should figure out why they do!
906 // exceptions of course are observers and weapon entities, where things mess up
910 float Drag_IsDraggable(entity draggee, entity dragger)
912 // TODO add more checks for bad stuff here
915 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)
916 return false; // probably due to BSP collision
917 //if(draggee.model == "")
920 return ((draggee.draggable) ? draggee.draggable(draggee, dragger) : true);
923 float Drag_MayChangeAngles(entity draggee)
925 // TODO add more checks for bad stuff here
926 if(substring(draggee.model, 0, 1) == "*")
931 void Drag_MoveForward(entity dragger)
933 dragger.dragdistance += dragger.dragspeed;
936 void Drag_SetSpeed(entity dragger, float s)
938 dragger.dragspeed = (2 ** s);
941 void Drag_MoveBackward(entity dragger)
943 dragger.dragdistance = max(0, dragger.dragdistance - dragger.dragspeed);
946 void Drag_Update(entity dragger)
948 vector curorigin, neworigin, goodvelocity;
952 draggee = dragger.dragentity;
953 UNSET_ONGROUND(draggee);
955 curorigin = gettaginfo(draggee, 0);
956 curorigin = curorigin + v_forward * dragger.draglocalvector.x + v_right * dragger.draglocalvector.y + v_up * dragger.draglocalvector.z;
957 makevectors(dragger.v_angle);
958 neworigin = dragger.origin + dragger.view_ofs + v_forward * dragger.dragdistance;
959 goodvelocity = (neworigin - curorigin) * (1 / frametime);
961 while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle > 180)
962 dragger.draglocalangle += 360;
963 while(draggee.angles.y - dragger.v_angle.y - dragger.draglocalangle <= -180)
964 dragger.draglocalangle -= 360;
966 f = min(frametime * 10, 1);
967 draggee.velocity = draggee.velocity * (1 - f) + goodvelocity * f;
969 if(Drag_MayChangeAngles(draggee))
970 draggee.angles_y = draggee.angles.y * (1 - f) + (dragger.v_angle.y + dragger.draglocalangle) * f;
972 draggee.ltime = max(servertime + serverframetime, draggee.ltime); // fixes func_train breakage
974 vector vecs = '0 0 0';
975 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
976 if(dragger.(weaponentity).movedir.x > 0)
977 vecs = dragger.(weaponentity).movedir;
979 vector dv = v_right * -vecs_y + v_up * vecs_z;
981 te_lightning1(draggee, dragger.origin + dragger.view_ofs + dv, curorigin);
984 float Drag_CanDrag(entity dragger)
986 return (!IS_DEAD(dragger)) || (IS_PLAYER(dragger));
989 float Drag_IsDragging(entity dragger)
991 if(!dragger.dragentity)
993 if(wasfreed(dragger.dragentity) || dragger.dragentity.draggedby != dragger)
995 dragger.dragentity = NULL;
998 if(!Drag_CanDrag(dragger) || !Drag_IsDraggable(dragger.dragentity, dragger))
1000 Drag_Finish(dragger);
1006 void Drag_MoveDrag(entity from, entity to)
1010 to.draggedby = from.draggedby;
1011 to.draggedby.dragentity = to;
1012 from.draggedby = NULL;
1016 void DragBox_Think(entity this)
1018 if(this.aiment && this.enemy)
1020 this.origin_x = (this.aiment.origin.x + this.enemy.origin.x) * 0.5;
1021 this.origin_y = (this.aiment.origin.y + this.enemy.origin.y) * 0.5;
1022 this.origin_z = (this.aiment.origin.z + this.enemy.origin.z) * 0.5;
1023 this.maxs_x = fabs(this.aiment.origin.x - this.enemy.origin.x) * 0.5;
1024 this.maxs_y = fabs(this.aiment.origin.y - this.enemy.origin.y) * 0.5;
1025 this.maxs_z = fabs(this.aiment.origin.z - this.enemy.origin.z) * 0.5;
1026 this.mins = -1 * this.maxs;
1027 setorigin(this, this.origin);
1028 setsize(this, this.mins, this.maxs); // link edict
1031 if(this.cnt == -1) // actually race_place -1
1033 // show "10 10" for qualifying spawns
1034 setmodel(this.killindicator, MDL_NUM(10));
1035 setmodel(this.killindicator.killindicator, MDL_NUM(10));
1037 else if(this.cnt == -2) // actually race_place 0
1039 // show "10 0" for loser spawns
1040 setmodel(this.killindicator, MDL_NUM(10));
1041 setmodel(this.killindicator.killindicator, MDL_NUM(0));
1045 setmodel(this.killindicator, MDL_NUM(this.cnt % 10));
1046 setmodel(this.killindicator.killindicator, MDL_NUM(floor(this.cnt / 10)));
1049 this.nextthink = time;