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_TARGET_PUSH)
20 trigger_push_calculatevelocity
23 org - origin of the object which is to be pushed
24 tgt - target entity (can be either a point or a model entity; if it is
25 the latter, its midpoint is used)
26 ht - jump height, measured from the higher one of org and tgt's midpoint
27 pushed_entity - object that is to be pushed
29 Returns: velocity for the jump
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht, entity pushed_entity)
33 float grav, sdist, zdist, vs, vz, jumpheight;
36 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
38 grav = PHYS_GRAVITY(NULL);
39 if(pushed_entity && pushed_entity.gravity)
40 grav *= pushed_entity.gravity;
42 zdist = torg.z - org.z;
43 sdist = vlen(torg - org - zdist * '0 0 1');
44 sdir = normalize(torg - org - zdist * '0 0 1');
46 // how high do we need to push the player?
47 jumpheight = fabs(ht);
49 jumpheight = jumpheight + zdist;
54 You will not understand the following equations anyway...
55 But here is what I did to get them.
60 z(t) = t * vz - 1/2 grav t^2
66 max(z, ti) = jumpheight
68 From these three equations, you will find the three parameters vs, vz
72 // push him so high...
73 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
75 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
81 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
82 // ALWAYS solvable because jumpheight >= zdist
84 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)
86 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)
94 // almost straight line type
95 // jump apex is before the jump
96 // we must take the larger one
97 flighttime = solution.y;
102 // jump apex is during the jump
103 // we must take the larger one too
104 flighttime = solution.y;
112 // almost straight line type
113 // jump apex is after the jump
114 // we must take the smaller one
115 flighttime = solution.x;
120 // jump apex is during the jump
121 // we must take the larger one
122 flighttime = solution.y;
125 vs = sdist / flighttime;
127 // finally calculate the velocity
128 return sdir * vs + '0 0 1' * vz;
131 bool jumppad_push(entity this, entity targ)
133 if (!isPushable(targ))
136 vector org = targ.origin;
138 if(autocvar_sv_q3defragcompat)
140 if(STAT(Q3DEFRAGCOMPAT))
143 org.z += targ.mins_z;
144 org.z += 1; // off by 1!
149 targ.velocity = trigger_push_calculatevelocity(org, this.enemy, this.height, targ);
151 else if(this.target && this.target != "")
154 RandomSelection_Init();
155 for(e = NULL; (e = find(e, targetname, this.target)); )
158 RandomSelection_AddEnt(e, e.cnt, 1);
160 RandomSelection_AddEnt(e, 1, 1);
162 targ.velocity = trigger_push_calculatevelocity(org, RandomSelection_chosen_ent, this.height, targ);
166 targ.velocity = this.movedir;
169 UNSET_ONGROUND(targ);
172 if (targ.flags & FL_PROJECTILE)
174 targ.angles = vectoangles (targ.velocity);
175 switch(targ.move_movetype)
178 set_movetype(targ, MOVETYPE_TOSS);
181 case MOVETYPE_BOUNCEMISSILE:
182 set_movetype(targ, MOVETYPE_BOUNCE);
192 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
193 targ.oldvelocity = targ.velocity;
195 // prevent sound spam when a player hits the jumppad more than once
196 // or when a dead player gets stuck in the jumppad for some reason
197 if(this.pushltime < time && !(IS_DEAD(targ) && targ.velocity == '0 0 0'))
199 // flash when activated
200 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
201 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
202 this.pushltime = time + 0.2;
204 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
207 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
208 if(targ.(jumppadsused[i]) == this)
212 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
213 targ.jumppadcount = targ.jumppadcount + 1;
216 if(IS_REAL_CLIENT(targ))
219 centerprint(targ, this.message);
223 targ.lastteleporttime = time;
224 targ.lastteleport_origin = targ.origin;
228 animdecide_setaction(targ, ANIMACTION_JUMP, true);
231 targ.jumppadcount = 1;
233 // reset tracking of who pushed you into a hazard (for kill credit)
238 if(this.enemy.target)
239 SUB_UseTargets(this.enemy, targ, this);
241 if (targ.flags & FL_PROJECTILE)
243 targ.angles = vectoangles (targ.velocity);
244 targ.com_phys_gravity_factor = 1;
245 switch(targ.move_movetype)
248 set_movetype(targ, MOVETYPE_TOSS);
251 case MOVETYPE_BOUNCEMISSILE:
252 set_movetype(targ, MOVETYPE_BOUNCE);
256 UpdateCSQCProjectile(targ);
263 void trigger_push_touch(entity this, entity toucher)
265 if (this.active == ACTIVE_NOT)
269 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
272 EXACTTRIGGER_TOUCH(this, toucher);
274 noref bool success = jumppad_push(this, toucher);
277 if (success && (this.spawnflags & PUSH_ONCE))
279 settouch(this, func_null);
280 setthink(this, SUB_Remove);
281 this.nextthink = time;
287 void trigger_push_link(entity this);
288 void trigger_push_updatelink(entity this);
289 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
291 setorigin(tracetest_ent, org);
292 tracetoss(tracetest_ent, tracetest_ent);
298 // since tracetoss starting from jumppad's origin often fails when target
299 // is very close to real destination, start it directly from target's
301 vector ofs = '0 0 0';
302 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
305 tracetest_ent.velocity.z = 0;
306 setorigin(tracetest_ent, targ.origin + ofs);
307 tracetoss(tracetest_ent, tracetest_ent);
308 if (trace_startsolid && ofs.z)
310 setorigin(tracetest_ent, targ.origin + ofs / 2);
311 tracetoss(tracetest_ent, tracetest_ent);
312 if (trace_startsolid && ofs.z)
314 setorigin(tracetest_ent, targ.origin);
315 tracetoss(tracetest_ent, tracetest_ent);
316 if (trace_startsolid)
321 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
325 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
327 setorigin(tracetest_ent, org);
328 tracetoss(tracetest_ent, tracetest_ent);
332 if (trace_ent == item)
335 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
337 if (trace_ent == item)
345 vector trigger_push_get_start_point(entity this)
347 // calculate a typical start point for the jump
348 vector org = (this.absmin + this.absmax) * 0.5;
349 org.z = this.absmax.z - PL_MIN_CONST.z - 7;
353 float trigger_push_get_push_time(entity this, vector endpos)
355 vector org = trigger_push_get_start_point(this);
357 float grav = PHYS_GRAVITY(NULL);
359 entity t = this.enemy;
363 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
364 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
365 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
366 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
368 return (v.z + v2.z) / grav;
370 else if (!(this.target && this.target != ""))
374 vector v = this.movedir;
376 float t = v.z / grav;
377 float jump_height = 1/2 * grav * (t ** 2);
378 float remaining_height = org.z + jump_height - endpos.z;
379 float v2_z = sqrt(2 * grav * remaining_height);
381 return (v.z + v2_z) / grav;
388 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
389 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
390 bool trigger_push_test(entity this, entity item)
393 vector org = trigger_push_get_start_point(this);
400 vector vel = '0 0 0';
402 for(entity t = NULL; (t = find(t, targetname, this.target)); )
406 if(t.move_movetype != MOVETYPE_NONE)
409 // bots can't tell teamed jumppads from normal ones
414 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
415 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
416 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
419 vector best_target = '0 0 0';
420 vector best_org = '0 0 0';
421 vector best_vel = '0 0 0';
422 bool valid_best_target = false;
425 if (!trigger_push_testorigin_for_item(e, item, org))
433 if (trigger_push_testorigin(e, t, this, org))
435 best_target = trace_endpos;
437 best_vel = e.velocity;
438 valid_best_target = true;
443 vector dist = t.origin - org;
444 if (dist.x || dist.y) // if not perfectly vertical
446 // test trajectory with different starting points, sometimes the trajectory
447 // starting from the jumppad origin can't reach the real destination
448 // and destination waypoint ends up near the jumppad itself
449 vector flatdir = normalize(dist - eZ * dist.z);
450 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
454 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
457 if (!trigger_push_testorigin_for_item(e, item, new_org))
466 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
467 e.velocity = autocvar_sv_maxspeed * flatdir;
468 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
470 best_target = trace_endpos;
473 valid_best_target = true;
476 if (ofs && new_org != org - ofs)
489 if (valid_best_target)
491 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
493 float velxy = vlen(vec2(best_vel));
494 float cost = vlen(vec2(t.origin - best_org)) / velxy;
495 if(velxy < autocvar_sv_maxspeed)
496 velxy = autocvar_sv_maxspeed;
497 cost += vlen(vec2(best_target - t.origin)) / velxy;
498 waypoint_spawnforteleporter(this, best_target, cost, e);
512 objerror (this, "Jumppad with nonexistant target");
518 // exactly one dest - bots love that
520 this.enemy = find(NULL, targetname, this.target);
521 else // bots can't tell teamed jumppads from normal ones
526 // have to use random selection every single time
537 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
538 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
540 e.velocity = this.movedir;
544 bool r = (trace_ent == item);
548 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
549 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
556 defer(this, 0.1, trigger_push_updatelink);
561 void trigger_push_findtarget(entity this)
563 trigger_push_test(this, NULL);
567 float trigger_push_send(entity this, entity to, float sf)
569 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
571 WriteByte(MSG_ENTITY, this.team);
572 WriteInt24_t(MSG_ENTITY, this.spawnflags);
573 WriteByte(MSG_ENTITY, this.active);
574 WriteCoord(MSG_ENTITY, this.height);
576 trigger_common_write(this, true);
581 void trigger_push_updatelink(entity this)
583 this.SendFlags |= SF_TRIGGER_INIT;
586 void trigger_push_link(entity this)
588 trigger_link(this, trigger_push_send);
594 * target: target of jump
595 * height: the absolute value is the height of the highest point of the jump
596 * trajectory above the higher one of the player and the target.
597 * the sign indicates whether the highest point is INSIDE (positive)
598 * or OUTSIDE (negative) of the jump trajectory. General rule: use
599 * positive values for targets mounted on the floor, and use negative
600 * values to target a point on the ceiling.
601 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
603 spawnfunc(trigger_push)
609 this.active = ACTIVE_ACTIVE;
610 this.use = trigger_push_use;
611 settouch(this, trigger_push_touch);
616 this.movedir = this.movedir * this.speed * 10;
619 this.noise = "misc/jumppad.wav";
620 precache_sound (this.noise);
622 trigger_push_link(this); // link it now
624 IL_PUSH(g_jumppads, this);
626 // this must be called to spawn the teleport waypoints for bots
627 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
631 bool target_push_send(entity this, entity to, float sf)
633 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
635 WriteByte(MSG_ENTITY, this.cnt);
636 WriteString(MSG_ENTITY, this.targetname);
637 WriteVector(MSG_ENTITY, this.origin);
639 WriteAngleVector(MSG_ENTITY, this.angles);
644 void target_push_use(entity this, entity actor, entity trigger)
646 if(trigger.classname == "trigger_push" || trigger == this)
647 return; // WTF, why is this a thing
649 jumppad_push(this, actor);
652 void target_push_link(entity this)
654 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
655 Net_LinkEntity(this, false, 0, target_push_send);
656 //this.SendFlags |= 1; // update
659 void target_push_init(entity this)
661 this.mangle = this.angles;
662 setorigin(this, this.origin);
663 target_push_link(this);
666 void target_push_init2(entity this)
668 if(this.target && this.target != "") // we have an old style pusher!
670 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
671 this.use = target_push_use;
674 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
677 spawnfunc(target_push)
679 target_push_init2(this);
682 spawnfunc(info_notnull)
684 target_push_init(this);
686 spawnfunc(target_position)
688 target_push_init(this);
693 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
695 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
696 this.spawnflags = ReadInt24_t();
697 this.active = ReadByte();
698 this.height = ReadCoord();
700 trigger_common_read(this, true);
702 this.entremove = trigger_remove_generic;
703 this.solid = SOLID_TRIGGER;
704 settouch(this, trigger_push_touch);
705 this.move_time = time;
706 defer(this, 0.25, trigger_push_findtarget);
711 void target_push_remove(entity this)
713 // strfree(this.classname);
714 strfree(this.targetname);
717 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
719 this.cnt = ReadByte();
720 this.targetname = strzone(ReadString());
721 this.origin = ReadVector();
723 this.angles = ReadAngleVector();
727 setorigin(this, this.origin);
729 this.drawmask = MASK_NORMAL;
730 this.entremove = target_push_remove;