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))
136 vector org = targ.origin;
138 if(autocvar_sv_vq3compat)
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 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
197 // flash when activated
198 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
199 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
200 this.pushltime = time + 0.2;
202 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
205 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
206 if(targ.(jumppadsused[i]) == this)
210 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
211 targ.jumppadcount = targ.jumppadcount + 1;
214 if(IS_REAL_CLIENT(targ))
217 centerprint(targ, this.message);
221 targ.lastteleporttime = time;
222 targ.lastteleport_origin = targ.origin;
226 animdecide_setaction(targ, ANIMACTION_JUMP, true);
229 targ.jumppadcount = 1;
231 // reset tracking of who pushed you into a hazard (for kill credit)
236 if(this.enemy.target)
237 SUB_UseTargets(this.enemy, targ, this);
239 if (targ.flags & FL_PROJECTILE)
241 targ.angles = vectoangles (targ.velocity);
242 targ.com_phys_gravity_factor = 1;
243 switch(targ.move_movetype)
246 set_movetype(targ, MOVETYPE_TOSS);
249 case MOVETYPE_BOUNCEMISSILE:
250 set_movetype(targ, MOVETYPE_BOUNCE);
254 UpdateCSQCProjectile(targ);
261 void trigger_push_touch(entity this, entity toucher)
263 if (this.active == ACTIVE_NOT)
267 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
270 EXACTTRIGGER_TOUCH(this, toucher);
272 noref bool success = jumppad_push(this, toucher);
275 if (success && (this.spawnflags & PUSH_ONCE))
277 settouch(this, func_null);
278 setthink(this, SUB_Remove);
279 this.nextthink = time;
285 void trigger_push_link(entity this);
286 void trigger_push_updatelink(entity this);
287 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
289 setorigin(tracetest_ent, org);
290 tracetoss(tracetest_ent, tracetest_ent);
296 // since tracetoss starting from jumppad's origin often fails when target
297 // is very close to real destination, start it directly from target's
299 vector ofs = '0 0 0';
300 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
303 tracetest_ent.velocity.z = 0;
304 setorigin(tracetest_ent, targ.origin + ofs);
305 tracetoss(tracetest_ent, tracetest_ent);
306 if (trace_startsolid && ofs.z)
308 setorigin(tracetest_ent, targ.origin + ofs / 2);
309 tracetoss(tracetest_ent, tracetest_ent);
310 if (trace_startsolid && ofs.z)
312 setorigin(tracetest_ent, targ.origin);
313 tracetoss(tracetest_ent, tracetest_ent);
314 if (trace_startsolid)
319 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
323 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
325 setorigin(tracetest_ent, org);
326 tracetoss(tracetest_ent, tracetest_ent);
330 if (trace_ent == item)
333 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
335 if (trace_ent == item)
342 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
343 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
344 bool trigger_push_test(entity this, entity item)
346 // first calculate a typical start point for the jump
347 vector org = (this.absmin + this.absmax) * 0.5;
348 org.z = this.absmax.z - PL_MIN_CONST.z - 7;
354 vector vel = '0 0 0';
356 for(entity t = NULL; (t = find(t, targetname, this.target)); )
360 if(t.move_movetype != MOVETYPE_NONE)
363 // bots can't tell teamed jumppads from normal ones
368 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
369 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
370 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
373 vector best_target = '0 0 0';
374 vector best_org = '0 0 0';
375 vector best_vel = '0 0 0';
376 bool valid_best_target = false;
379 if (!trigger_push_testorigin_for_item(e, item, org))
387 if (trigger_push_testorigin(e, t, this, org))
389 best_target = trace_endpos;
391 best_vel = e.velocity;
392 valid_best_target = true;
397 vector dist = t.origin - org;
398 if (dist.x || dist.y) // if not perfectly vertical
400 // test trajectory with different starting points, sometimes the trajectory
401 // starting from the jumppad origin can't reach the real destination
402 // and destination waypoint ends up near the jumppad itself
403 vector flatdir = normalize(dist - eZ * dist.z);
404 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
408 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
411 if (!trigger_push_testorigin_for_item(e, item, new_org))
420 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
421 e.velocity = autocvar_sv_maxspeed * flatdir;
422 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
424 best_target = trace_endpos;
427 valid_best_target = true;
430 if (ofs && new_org != org - ofs)
443 if (valid_best_target)
445 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
447 float velxy = vlen(vec2(best_vel));
448 float cost = vlen(vec2(t.origin - best_org)) / velxy;
449 if(velxy < autocvar_sv_maxspeed)
450 velxy = autocvar_sv_maxspeed;
451 cost += vlen(vec2(best_target - t.origin)) / velxy;
452 waypoint_spawnforteleporter(this, best_target, cost, e);
466 objerror (this, "Jumppad with nonexistant target");
472 // exactly one dest - bots love that
474 this.enemy = find(NULL, targetname, this.target);
475 else // bots can't tell teamed jumppads from normal ones
480 // have to use random selection every single time
491 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
492 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
494 e.velocity = this.movedir;
498 bool r = (trace_ent == item);
502 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
503 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
510 defer(this, 0.1, trigger_push_updatelink);
515 void trigger_push_findtarget(entity this)
517 trigger_push_test(this, NULL);
521 float trigger_push_send(entity this, entity to, float sf)
523 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
525 WriteByte(MSG_ENTITY, this.team);
526 WriteInt24_t(MSG_ENTITY, this.spawnflags);
527 WriteByte(MSG_ENTITY, this.active);
528 WriteCoord(MSG_ENTITY, this.height);
530 WriteVector(MSG_ENTITY, this.movedir);
532 trigger_common_write(this, true);
537 void trigger_push_updatelink(entity this)
539 this.SendFlags |= SF_TRIGGER_INIT;
542 void trigger_push_link(entity this)
544 trigger_link(this, trigger_push_send);
550 * target: target of jump
551 * height: the absolute value is the height of the highest point of the jump
552 * trajectory above the higher one of the player and the target.
553 * the sign indicates whether the highest point is INSIDE (positive)
554 * or OUTSIDE (negative) of the jump trajectory. General rule: use
555 * positive values for targets mounted on the floor, and use negative
556 * values to target a point on the ceiling.
557 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
559 spawnfunc(trigger_push)
565 this.active = ACTIVE_ACTIVE;
566 this.use = trigger_push_use;
567 settouch(this, trigger_push_touch);
572 this.movedir = this.movedir * this.speed * 10;
575 this.noise = "misc/jumppad.wav";
576 precache_sound (this.noise);
578 trigger_push_link(this); // link it now
580 IL_PUSH(g_jumppads, this);
582 // this must be called to spawn the teleport waypoints for bots
583 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
587 bool target_push_send(entity this, entity to, float sf)
589 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
591 WriteByte(MSG_ENTITY, this.cnt);
592 WriteString(MSG_ENTITY, this.targetname);
593 WriteVector(MSG_ENTITY, this.origin);
595 WriteAngle(MSG_ENTITY, this.angles_x);
596 WriteAngle(MSG_ENTITY, this.angles_y);
597 WriteAngle(MSG_ENTITY, this.angles_z);
602 void target_push_use(entity this, entity actor, entity trigger)
604 if(trigger.classname == "trigger_push" || trigger == this)
605 return; // WTF, why is this a thing
607 jumppad_push(this, actor);
610 void target_push_link(entity this)
612 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
613 Net_LinkEntity(this, false, 0, target_push_send);
614 //this.SendFlags |= 1; // update
617 void target_push_init(entity this)
619 this.mangle = this.angles;
620 setorigin(this, this.origin);
621 target_push_link(this);
624 void target_push_init2(entity this)
626 if(this.target && this.target != "") // we have an old style pusher!
628 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
629 this.use = target_push_use;
632 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
635 spawnfunc(target_push)
637 target_push_init2(this);
640 spawnfunc(info_notnull)
642 target_push_init(this);
644 spawnfunc(target_position)
646 target_push_init(this);
651 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
653 this.classname = "jumppad";
654 int mytm = ReadByte();
657 this.team = mytm - 1;
659 this.spawnflags = ReadInt24_t();
660 this.active = ReadByte();
661 this.height = ReadCoord();
663 this.movedir = ReadVector();
665 trigger_common_read(this, true);
667 this.entremove = trigger_remove_generic;
668 this.solid = SOLID_TRIGGER;
669 settouch(this, trigger_push_touch);
670 this.move_time = time;
671 defer(this, 0.25, trigger_push_findtarget);
676 void target_push_remove(entity this)
678 // strfree(this.classname);
679 strfree(this.targetname);
682 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
684 this.classname = "push_target";
685 this.cnt = ReadByte();
686 this.targetname = strzone(ReadString());
687 this.origin = ReadVector();
689 this.angles_x = ReadAngle();
690 this.angles_y = ReadAngle();
691 this.angles_z = ReadAngle();
695 setorigin(this, this.origin);
697 this.drawmask = MASK_NORMAL;
698 this.entremove = target_push_remove;