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 && PHYS_ENTGRAVITY(pushed_entity))
40 grav *= PHYS_ENTGRAVITY(pushed_entity);
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))
138 targ.velocity = trigger_push_calculatevelocity(targ.origin, this.enemy, this.height, targ);
140 else if(this.target && this.target != "")
143 RandomSelection_Init();
144 for(e = NULL; (e = find(e, targetname, this.target)); )
147 RandomSelection_AddEnt(e, e.cnt, 1);
149 RandomSelection_AddEnt(e, 1, 1);
151 targ.velocity = trigger_push_calculatevelocity(targ.origin, RandomSelection_chosen_ent, this.height, targ);
155 targ.velocity = this.movedir;
158 UNSET_ONGROUND(targ);
161 if (targ.flags & FL_PROJECTILE)
163 targ.angles = vectoangles (targ.velocity);
164 switch(targ.move_movetype)
167 set_movetype(targ, MOVETYPE_TOSS);
170 case MOVETYPE_BOUNCEMISSILE:
171 set_movetype(targ, MOVETYPE_BOUNCE);
181 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
182 targ.oldvelocity = targ.velocity;
184 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
186 // flash when activated
187 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
188 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
189 this.pushltime = time + 0.2;
191 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
194 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
195 if(targ.(jumppadsused[i]) == this)
199 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
200 targ.jumppadcount = targ.jumppadcount + 1;
203 if(IS_REAL_CLIENT(targ))
206 centerprint(targ, this.message);
210 targ.lastteleporttime = time;
211 targ.lastteleport_origin = targ.origin;
215 animdecide_setaction(targ, ANIMACTION_JUMP, true);
218 targ.jumppadcount = 1;
220 // reset tracking of who pushed you into a hazard (for kill credit)
225 if(this.enemy.target)
226 SUB_UseTargets(this.enemy, targ, this);
228 if (targ.flags & FL_PROJECTILE)
230 targ.angles = vectoangles (targ.velocity);
231 targ.com_phys_gravity_factor = 1;
232 switch(targ.move_movetype)
235 set_movetype(targ, MOVETYPE_TOSS);
238 case MOVETYPE_BOUNCEMISSILE:
239 set_movetype(targ, MOVETYPE_BOUNCE);
243 UpdateCSQCProjectile(targ);
250 void trigger_push_touch(entity this, entity toucher)
252 if (this.active == ACTIVE_NOT)
256 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
259 EXACTTRIGGER_TOUCH(this, toucher);
261 noref bool success = jumppad_push(this, toucher);
264 if (success && (this.spawnflags & PUSH_ONCE))
266 settouch(this, func_null);
267 setthink(this, SUB_Remove);
268 this.nextthink = time;
274 void trigger_push_link(entity this);
275 void trigger_push_updatelink(entity this);
276 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
278 setorigin(tracetest_ent, org);
279 tracetoss(tracetest_ent, tracetest_ent);
285 // since tracetoss starting from jumppad's origin often fails when target
286 // is very close to real destination, start it directly from target's
288 vector ofs = '0 0 0';
289 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
292 tracetest_ent.velocity.z = 0;
293 setorigin(tracetest_ent, targ.origin + ofs);
294 tracetoss(tracetest_ent, tracetest_ent);
295 if (trace_startsolid && ofs.z)
297 setorigin(tracetest_ent, targ.origin + ofs / 2);
298 tracetoss(tracetest_ent, tracetest_ent);
299 if (trace_startsolid && ofs.z)
301 setorigin(tracetest_ent, targ.origin);
302 tracetoss(tracetest_ent, tracetest_ent);
303 if (trace_startsolid)
308 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
312 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
314 setorigin(tracetest_ent, org);
315 tracetoss(tracetest_ent, tracetest_ent);
319 if (trace_ent == item)
322 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
324 if (trace_ent == item)
331 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
332 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
333 bool trigger_push_test(entity this, entity item)
335 // first calculate a typical start point for the jump
336 vector org = (this.absmin + this.absmax) * 0.5;
337 org.z = this.absmax.z - PL_MIN_CONST.z - 7;
343 vector vel = '0 0 0';
345 for(entity t = NULL; (t = find(t, targetname, this.target)); )
349 if(t.move_movetype != MOVETYPE_NONE)
353 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
354 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
355 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
358 vector best_target = '0 0 0';
359 vector best_org = '0 0 0';
360 vector best_vel = '0 0 0';
361 bool valid_best_target = false;
364 if (!trigger_push_testorigin_for_item(e, item, org))
372 if (trigger_push_testorigin(e, t, this, org))
374 best_target = trace_endpos;
376 best_vel = e.velocity;
377 valid_best_target = true;
382 vector dist = t.origin - org;
383 if (dist.x || dist.y) // if not perfectly vertical
385 // test trajectory with different starting points, sometimes the trajectory
386 // starting from the jumppad origin can't reach the real destination
387 // and destination waypoint ends up near the jumppad itself
388 vector flatdir = normalize(dist - eZ * dist.z);
389 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
391 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
394 if (!trigger_push_testorigin_for_item(e, item, new_org))
403 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
404 e.velocity = autocvar_sv_maxspeed * flatdir;
405 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
407 best_target = trace_endpos;
410 valid_best_target = true;
414 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
417 if (!trigger_push_testorigin_for_item(e, item, new_org))
426 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
427 e.velocity = autocvar_sv_maxspeed * flatdir;
428 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
430 best_target = trace_endpos;
433 valid_best_target = true;
444 if (valid_best_target)
446 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
448 float velxy = vlen(vec2(best_vel));
449 float cost = vlen(vec2(t.origin - best_org)) / velxy;
450 if(velxy < autocvar_sv_maxspeed)
451 velxy = autocvar_sv_maxspeed;
452 cost += vlen(vec2(best_target - t.origin)) / velxy;
453 waypoint_spawnforteleporter(this, best_target, cost, e);
467 objerror (this, "Jumppad with nonexistant target");
473 // exactly one dest - bots love that
474 this.enemy = find(NULL, targetname, this.target);
478 // have to use random selection every single time
486 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
487 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
489 e.velocity = this.movedir;
493 bool r = (trace_ent == item);
497 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
498 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
502 defer(this, 0.1, trigger_push_updatelink);
507 void trigger_push_findtarget(entity this)
509 trigger_push_test(this, NULL);
513 float trigger_push_send(entity this, entity to, float sf)
515 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
517 WriteByte(MSG_ENTITY, this.team);
518 WriteInt24_t(MSG_ENTITY, this.spawnflags);
519 WriteByte(MSG_ENTITY, this.active);
520 WriteCoord(MSG_ENTITY, this.height);
522 WriteVector(MSG_ENTITY, this.movedir);
524 trigger_common_write(this, true);
529 void trigger_push_updatelink(entity this)
531 this.SendFlags |= SF_TRIGGER_INIT;
534 void trigger_push_link(entity this)
536 trigger_link(this, trigger_push_send);
542 * target: target of jump
543 * height: the absolute value is the height of the highest point of the jump
544 * trajectory above the higher one of the player and the target.
545 * the sign indicates whether the highest point is INSIDE (positive)
546 * or OUTSIDE (negative) of the jump trajectory. General rule: use
547 * positive values for targets mounted on the floor, and use negative
548 * values to target a point on the ceiling.
549 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
551 spawnfunc(trigger_push)
557 this.active = ACTIVE_ACTIVE;
558 this.use = trigger_push_use;
559 settouch(this, trigger_push_touch);
564 this.movedir = this.movedir * this.speed * 10;
567 this.noise = "misc/jumppad.wav";
568 precache_sound (this.noise);
570 trigger_push_link(this); // link it now
572 IL_PUSH(g_jumppads, this);
574 // this must be called to spawn the teleport waypoints for bots
575 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
579 bool target_push_send(entity this, entity to, float sf)
581 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
583 WriteByte(MSG_ENTITY, this.cnt);
584 WriteString(MSG_ENTITY, this.targetname);
585 WriteVector(MSG_ENTITY, this.origin);
587 WriteAngle(MSG_ENTITY, this.angles_x);
588 WriteAngle(MSG_ENTITY, this.angles_y);
589 WriteAngle(MSG_ENTITY, this.angles_z);
594 void target_push_use(entity this, entity actor, entity trigger)
596 if(trigger.classname == "trigger_push" || trigger == this)
597 return; // WTF, why is this a thing
599 jumppad_push(this, actor);
602 void target_push_link(entity this)
604 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
605 Net_LinkEntity(this, false, 0, target_push_send);
606 //this.SendFlags |= 1; // update
609 void target_push_init(entity this)
611 this.mangle = this.angles;
612 setorigin(this, this.origin);
613 target_push_link(this);
616 void target_push_init2(entity this)
618 if(this.target && this.target != "") // we have an old style pusher!
620 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
621 this.use = target_push_use;
624 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
627 spawnfunc(target_push)
629 target_push_init2(this);
632 spawnfunc(info_notnull)
634 target_push_init(this);
636 spawnfunc(target_position)
638 target_push_init(this);
643 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
645 this.classname = "jumppad";
646 int mytm = ReadByte();
649 this.team = mytm - 1;
651 this.spawnflags = ReadInt24_t();
652 this.active = ReadByte();
653 this.height = ReadCoord();
655 this.movedir = ReadVector();
657 trigger_common_read(this, true);
659 this.entremove = trigger_remove_generic;
660 this.solid = SOLID_TRIGGER;
661 settouch(this, trigger_push_touch);
662 this.move_time = time;
663 defer(this, 0.25, trigger_push_findtarget);
668 void target_push_remove(entity this)
670 // strfree(this.classname);
671 strfree(this.targetname);
674 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
676 this.classname = "push_target";
677 this.cnt = ReadByte();
678 this.targetname = strzone(ReadString());
679 this.origin = ReadVector();
681 this.angles_x = ReadAngle();
682 this.angles_y = ReadAngle();
683 this.angles_z = ReadAngle();
687 setorigin(this, this.origin);
689 this.drawmask = MASK_NORMAL;
690 this.entremove = target_push_remove;