2 // TODO: split target_push and put it in the target folder
4 #include <common/physics/movetypes/movetypes.qh>
6 void trigger_push_use(entity this, entity actor, entity trigger)
10 this.team = actor.team;
11 this.SendFlags |= SF_TRIGGER_UPDATE;
16 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
17 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH_VELOCITY)
18 REGISTER_NET_LINKED(ENT_CLIENT_TARGET_PUSH)
21 trigger_push_calculatevelocity
24 org - origin of the object which is to be pushed
25 tgt - target entity (can be either a point or a model entity; if it is
26 the latter, its midpoint is used)
27 ht - jump height, measured from the higher one of org and tgt's midpoint
28 pushed_entity - object that is to be pushed
30 Returns: velocity for the jump
32 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht, entity pushed_entity)
34 float grav, sdist, zdist, vs, vz, jumpheight;
37 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
39 grav = PHYS_GRAVITY(NULL);
40 if(pushed_entity && pushed_entity.gravity)
41 grav *= pushed_entity.gravity;
43 zdist = torg.z - org.z;
44 sdist = vlen(torg - org - zdist * '0 0 1');
45 sdir = normalize(torg - org - zdist * '0 0 1');
47 // how high do we need to push the player?
48 jumpheight = fabs(ht);
50 jumpheight = jumpheight + zdist;
55 You will not understand the following equations anyway...
56 But here is what I did to get them.
61 z(t) = t * vz - 1/2 grav t^2
67 max(z, ti) = jumpheight
69 From these three equations, you will find the three parameters vs, vz
73 // push them so high...
74 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
76 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
82 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
83 // ALWAYS solvable because jumpheight >= zdist
85 solution_y = solution.x; // just in case it is not solvable due to roundoff errors, assume two equal solutions at their center (this is mainly for the usual case with ht == 0)
87 solution_x = solution.y; // solution_x is 0 in this case, so don't use it, but rather use solution_y (which will be sqrt(0.5 * jumpheight / grav), actually)
95 // almost straight line type
96 // jump apex is before the jump
97 // we must take the larger one
98 flighttime = solution.y;
103 // jump apex is during the jump
104 // we must take the larger one too
105 flighttime = solution.y;
113 // almost straight line type
114 // jump apex is after the jump
115 // we must take the smaller one
116 flighttime = solution.x;
121 // jump apex is during the jump
122 // we must take the larger one
123 flighttime = solution.y;
126 vs = sdist / flighttime;
128 // finally calculate the velocity
129 return sdir * vs + '0 0 1' * vz;
132 vector trigger_push_velocity_calculatevelocity(entity this, vector org, entity tgt, float speed, float count, entity pushed_entity, bool is_pushed)
134 bool is_playerdir_xy = boolean(this.spawnflags & PUSH_VELOCITY_PLAYERDIR_XY);
135 bool is_add_xy = boolean(this.spawnflags & PUSH_VELOCITY_ADD_XY);
136 bool is_playerdir_z = boolean(this.spawnflags & PUSH_VELOCITY_PLAYERDIR_Z);
137 bool is_add_z = boolean(this.spawnflags & PUSH_VELOCITY_ADD_Z);
138 bool is_bidirectional_xy = boolean(this.spawnflags & PUSH_VELOCITY_BIDIRECTIONAL_XY);
139 bool is_bidirectional_z = boolean(this.spawnflags & PUSH_VELOCITY_BIDIRECTIONAL_Z);
140 bool is_clamp_negative_adds = boolean(this.spawnflags & PUSH_VELOCITY_CLAMP_NEGATIVE_ADDS);
142 vector sdir = normalize(vec2(pushed_entity.velocity));
143 float zdir = pushed_entity.velocity.z;
144 if(zdir != 0) zdir = copysign(1, zdir);
146 vector vs_tgt = '0 0 0';
148 if (!is_playerdir_xy || !is_playerdir_z)
150 vector vel_tgt = trigger_push_calculatevelocity(org, tgt, 0, pushed_entity);
151 vs_tgt = vec2(vel_tgt);
154 // bidirectional jump pads do not play nicely with xonotic's jump pad targets
155 if (is_bidirectional_xy)
157 if (normalize(vs_tgt) * sdir < 0)
163 if (is_bidirectional_z)
165 if (signbit(vz_tgt) != signbit(zdir))
194 vector vs_add = vec2(pushed_entity.velocity);
203 if (is_clamp_negative_adds)
205 if ((normalize(vs) * sdir) < 0)
215 float vz_add = pushed_entity.velocity.z;
224 if (is_clamp_negative_adds)
226 if (signbit(vz) != signbit(zdir))
234 return vs + '0 0 1' * vz;
237 // TODO: move this check to player/projectile physics?
238 void check_pushed(entity this) // first jump pad to think thinks for every jump pad
240 IL_EACH(are_pushed, true,
243 for(int i = 0; i < MAX_PUSHED; ++i)
245 if(!it.has_pushed[i]) continue;
246 if(WarpZoneLib_ExactTrigger_Touch(it.has_pushed[i], it))
248 it.has_pushed[i] = NULL;
255 IL_REMOVE(are_pushed, it);
259 if(!IL_EMPTY(are_pushed))
261 this.nextthink = time;
265 bool jumppad_push(entity this, entity targ, bool is_velocity_pad)
267 if (!isPushable(targ))
270 vector org = targ.origin;
272 if(STAT(Q3COMPAT) && (STAT(Q3COMPAT_JUMPPADS) >= 1) || (STAT(Q3COMPAT_JUMPPADS) >= 2))
274 org = (this.absmin + this.absmax) * 0.5;
277 bool is_pushed = false;
280 for(int i = 0; i < MAX_PUSHED; ++i)
282 if(this == targ.has_pushed[i])
289 if(!is_pushed) // remember velocity jump pads
291 bool limit_reached = true;
292 for(int i = 0; i < MAX_PUSHED; ++i)
294 if(targ.has_pushed[i]) continue;
295 limit_reached = false;
296 targ.has_pushed[i] = this; // may be briefly out of sync between client and server if client prediction is toggled
301 return false; // too many overlapping jump pads
303 IL_PUSH(are_pushed, targ);
304 this.nextthink = time;
312 targ.velocity = trigger_push_calculatevelocity(org, this.enemy, this.height, targ);
316 targ.velocity = trigger_push_velocity_calculatevelocity(this, org, this.enemy, this.speed, this.count, targ, is_pushed);
319 else if(this.target && this.target != "")
322 RandomSelection_Init();
323 for(e = NULL; (e = find(e, targetname, this.target)); )
326 RandomSelection_AddEnt(e, e.cnt, 1);
328 RandomSelection_AddEnt(e, 1, 1);
332 targ.velocity = trigger_push_calculatevelocity(org, RandomSelection_chosen_ent, this.height, targ);
336 targ.velocity = trigger_push_velocity_calculatevelocity(this, org, RandomSelection_chosen_ent, this.speed, this.count, targ, is_pushed);
343 targ.velocity = this.movedir;
348 objerror (this, "Jumppad with no target");
354 if(!is_velocity_pad) UNSET_ONGROUND(targ);
357 if (targ.flags & FL_PROJECTILE)
359 targ.angles = vectoangles (targ.velocity);
360 switch(targ.move_movetype)
363 set_movetype(targ, MOVETYPE_TOSS);
366 case MOVETYPE_BOUNCEMISSILE:
367 set_movetype(targ, MOVETYPE_BOUNCE);
377 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
378 targ.oldvelocity = targ.velocity;
380 // prevent sound spam when a player hits the jumppad more than once
381 // or when a dead player gets stuck in the jumppad for some reason
382 if(!is_pushed && this.pushltime < time && !(IS_DEAD(targ) && targ.velocity == '0 0 0'))
384 // flash when activated
385 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
386 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
387 this.pushltime = time + 0.2;
389 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
392 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
393 if(targ.(jumppadsused[i]) == this)
397 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
398 targ.jumppadcount = targ.jumppadcount + 1;
401 if(IS_REAL_CLIENT(targ))
404 centerprint(targ, this.message);
408 targ.lastteleporttime = time;
409 targ.lastteleport_origin = targ.origin;
413 animdecide_setaction(targ, ANIMACTION_JUMP, true);
416 targ.jumppadcount = 1;
418 // reset tracking of who pushed you into a hazard (for kill credit)
423 if(this.enemy.target)
424 SUB_UseTargets(this.enemy, targ, this);
426 if (targ.flags & FL_PROJECTILE)
428 targ.angles = vectoangles (targ.velocity);
429 targ.com_phys_gravity_factor = 1;
430 switch(targ.move_movetype)
433 set_movetype(targ, MOVETYPE_TOSS);
436 case MOVETYPE_BOUNCEMISSILE:
437 set_movetype(targ, MOVETYPE_BOUNCE);
441 UpdateCSQCProjectile(targ);
448 void trigger_push_touch(entity this, entity toucher)
450 if (this.active == ACTIVE_NOT)
454 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
457 EXACTTRIGGER_TOUCH(this, toucher);
459 noref bool success = jumppad_push(this, toucher, false);
462 if (success && (this.spawnflags & PUSH_ONCE))
464 settouch(this, func_null);
465 setthink(this, SUB_Remove);
466 this.nextthink = time;
471 void trigger_push_velocity_touch(entity this, entity toucher)
473 if (this.active == ACTIVE_NOT)
476 if(this.team && DIFF_TEAM(this, toucher))
479 EXACTTRIGGER_TOUCH(this, toucher);
481 noref bool success = jumppad_push(this, toucher, true);
485 void trigger_push_link(entity this);
486 void trigger_push_updatelink(entity this);
487 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
489 setorigin(tracetest_ent, org);
490 tracetoss(tracetest_ent, tracetest_ent);
496 // since tracetoss starting from jumppad's origin often fails when target
497 // is very close to real destination, start it directly from target's
499 vector ofs = '0 0 0';
500 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
503 tracetest_ent.velocity.z = 0;
504 setorigin(tracetest_ent, targ.origin + ofs);
505 tracetoss(tracetest_ent, tracetest_ent);
506 if (trace_startsolid && ofs.z)
508 setorigin(tracetest_ent, targ.origin + ofs / 2);
509 tracetoss(tracetest_ent, tracetest_ent);
510 if (trace_startsolid && ofs.z)
512 setorigin(tracetest_ent, targ.origin);
513 tracetoss(tracetest_ent, tracetest_ent);
514 if (trace_startsolid)
519 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
523 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
525 setorigin(tracetest_ent, org);
526 tracetoss(tracetest_ent, tracetest_ent);
530 if (trace_ent == item)
533 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
535 if (trace_ent == item)
543 vector trigger_push_get_start_point(entity this)
545 // calculate a typical start point for the jump
546 vector org = (this.absmin + this.absmax) * 0.5;
547 org.z = this.absmax.z - PL_MIN_CONST.z - 7;
551 float trigger_push_get_push_time(entity this, vector endpos)
553 vector org = trigger_push_get_start_point(this);
555 float grav = PHYS_GRAVITY(NULL);
557 entity t = this.enemy;
561 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
562 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
563 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
564 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
566 return (v.z + v2.z) / grav;
568 else if (!(this.target && this.target != ""))
572 vector v = this.movedir;
574 float t = v.z / grav;
575 float jump_height = 1/2 * grav * (t ** 2);
576 float remaining_height = org.z + jump_height - endpos.z;
577 float v2_z = sqrt(2 * grav * remaining_height);
579 return (v.z + v2_z) / grav;
586 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
587 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
588 bool trigger_push_test(entity this, entity item)
591 vector org = trigger_push_get_start_point(this);
598 vector vel = '0 0 0';
600 for(entity t = NULL; (t = find(t, targetname, this.target)); )
604 if(t.move_movetype != MOVETYPE_NONE)
607 // bots can't tell teamed jumppads from normal ones
612 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
613 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
614 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
617 vector best_target = '0 0 0';
618 vector best_org = '0 0 0';
619 vector best_vel = '0 0 0';
620 bool valid_best_target = false;
623 if (!trigger_push_testorigin_for_item(e, item, org))
631 if (trigger_push_testorigin(e, t, this, org))
633 best_target = trace_endpos;
635 best_vel = e.velocity;
636 valid_best_target = true;
641 vector dist = t.origin - org;
642 if (dist.x || dist.y) // if not perfectly vertical
644 // test trajectory with different starting points, sometimes the trajectory
645 // starting from the jumppad origin can't reach the real destination
646 // and destination waypoint ends up near the jumppad itself
647 vector flatdir = normalize(dist - eZ * dist.z);
648 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
652 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
655 if (!trigger_push_testorigin_for_item(e, item, new_org))
664 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
665 e.velocity = autocvar_sv_maxspeed * flatdir;
666 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
668 best_target = trace_endpos;
671 valid_best_target = true;
674 if (ofs && new_org != org - ofs)
687 if (valid_best_target)
689 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
691 float velxy = vlen(vec2(best_vel));
692 float cost = vlen(vec2(t.origin - best_org)) / velxy;
693 if(velxy < autocvar_sv_maxspeed)
694 velxy = autocvar_sv_maxspeed;
695 cost += vlen(vec2(best_target - t.origin)) / velxy;
696 waypoint_spawnforteleporter(this, best_target, cost, e);
710 objerror (this, "Jumppad with nonexistant target");
716 // exactly one dest - bots love that
718 this.enemy = find(NULL, targetname, this.target);
719 else // bots can't tell teamed jumppads from normal ones
724 // have to use random selection every single time
735 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
736 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
738 e.velocity = this.movedir;
742 bool r = (trace_ent == item);
746 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
747 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
754 defer(this, 0.1, trigger_push_updatelink);
759 void trigger_push_findtarget(entity this)
761 trigger_push_test(this, NULL);
765 float trigger_push_send(entity this, entity to, float sf)
767 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
769 WriteByte(MSG_ENTITY, this.team);
770 WriteInt24_t(MSG_ENTITY, this.spawnflags);
771 WriteByte(MSG_ENTITY, this.active);
772 WriteCoord(MSG_ENTITY, this.height);
774 trigger_common_write(this, true);
779 float trigger_push_velocity_send(entity this, entity to, float sf)
781 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH_VELOCITY);
783 WriteByte(MSG_ENTITY, this.team);
784 WriteInt24_t(MSG_ENTITY, this.spawnflags);
785 WriteByte(MSG_ENTITY, this.active);
786 WriteCoord(MSG_ENTITY, this.speed);
787 WriteCoord(MSG_ENTITY, this.count);
789 trigger_common_write(this, true);
794 void trigger_push_updatelink(entity this)
796 this.SendFlags |= SF_TRIGGER_INIT;
799 void trigger_push_link(entity this)
801 trigger_link(this, trigger_push_send);
804 void trigger_push_velocity_link(entity this)
806 trigger_link(this, trigger_push_velocity_send);
812 * target: target of jump
813 * height: the absolute value is the height of the highest point of the jump
814 * trajectory above the higher one of the player and the target.
815 * the sign indicates whether the highest point is INSIDE (positive)
816 * or OUTSIDE (negative) of the jump trajectory. General rule: use
817 * positive values for targets mounted on the floor, and use negative
818 * values to target a point on the ceiling.
819 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
821 spawnfunc(trigger_push)
827 this.active = ACTIVE_ACTIVE;
828 this.use = trigger_push_use;
829 settouch(this, trigger_push_touch);
834 this.movedir = this.movedir * this.speed * 10;
837 this.noise = "misc/jumppad.wav";
838 precache_sound (this.noise);
840 trigger_push_link(this); // link it now
842 IL_PUSH(g_jumppads, this);
844 // this must be called to spawn the teleport waypoints for bots
845 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
851 * target: this points to the target_position to which the player will jump.
852 * speed: XY speed for player-directional velocity pads - either sets or adds to the player's horizontal velocity.
853 * count: Z speed for player-directional velocity pads - either sets or adds to the player's vertical velocity.
855 spawnfunc(trigger_push_velocity)
859 this.active = ACTIVE_ACTIVE;
860 this.use = trigger_push_use;
861 settouch(this, trigger_push_velocity_touch);
865 this.noise = "misc/jumppad.wav";
866 precache_sound (this.noise);
868 trigger_push_velocity_link(this); // link it now
870 setthink(this, check_pushed);
874 bool target_push_send(entity this, entity to, float sf)
876 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
878 WriteByte(MSG_ENTITY, this.cnt);
879 WriteString(MSG_ENTITY, this.targetname);
880 WriteVector(MSG_ENTITY, this.origin);
882 WriteAngleVector(MSG_ENTITY, this.angles);
887 void target_push_use(entity this, entity actor, entity trigger)
889 if(trigger.classname == "trigger_push" || trigger == this)
890 return; // WTF, why is this a thing
892 jumppad_push(this, actor, false);
895 void target_push_link(entity this)
897 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
898 Net_LinkEntity(this, false, 0, target_push_send);
899 //this.SendFlags |= 1; // update
902 void target_push_init(entity this)
904 this.mangle = this.angles;
905 setorigin(this, this.origin);
906 target_push_link(this);
909 void target_push_init2(entity this)
911 if(this.target && this.target != "") // we have an old style pusher!
913 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
914 this.use = target_push_use;
917 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
920 spawnfunc(target_push)
922 target_push_init2(this);
925 spawnfunc(info_notnull)
927 target_push_init(this);
929 spawnfunc(target_position)
931 target_push_init(this);
936 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
938 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
939 this.spawnflags = ReadInt24_t();
940 this.active = ReadByte();
941 this.height = ReadCoord();
943 trigger_common_read(this, true);
945 this.entremove = trigger_remove_generic;
946 this.solid = SOLID_TRIGGER;
947 settouch(this, trigger_push_touch);
948 this.move_time = time;
949 defer(this, 0.25, trigger_push_findtarget);
954 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH_VELOCITY, bool isnew)
956 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
957 this.spawnflags = ReadInt24_t();
958 this.active = ReadByte();
959 this.speed = ReadCoord();
960 this.count = ReadCoord();
962 trigger_common_read(this, true);
964 this.entremove = trigger_remove_generic;
965 this.solid = SOLID_TRIGGER;
966 settouch(this, trigger_push_velocity_touch);
967 this.move_time = time;
968 setthink(this, check_pushed);
973 void target_push_remove(entity this)
975 // strfree(this.classname);
976 strfree(this.targetname);
979 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
981 this.cnt = ReadByte();
982 this.targetname = strzone(ReadString());
983 this.origin = ReadVector();
985 this.angles = ReadAngleVector();
989 setorigin(this, this.origin);
991 this.drawmask = MASK_NORMAL;
992 this.entremove = target_push_remove;