2 // TODO: split target_push and put it in the target folder
4 #include <common/physics/movetypes/movetypes.qh>
5 #include <server/main.qh>
7 void trigger_push_use(entity this, entity actor, entity trigger)
11 this.team = actor.team;
12 this.SendFlags |= SF_TRIGGER_UPDATE;
17 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
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 him 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 bool jumppad_push(entity this, entity targ)
134 if (!isPushable(targ))
137 vector org = targ.origin;
139 if(autocvar_sv_q3defragcompat)
141 if(STAT(Q3DEFRAGCOMPAT))
144 org.z += targ.mins_z;
145 org.z += 1; // off by 1!
150 targ.velocity = trigger_push_calculatevelocity(org, this.enemy, this.height, targ);
152 else if(this.target && this.target != "")
155 RandomSelection_Init();
156 for(e = NULL; (e = find(e, targetname, this.target)); )
159 RandomSelection_AddEnt(e, e.cnt, 1);
161 RandomSelection_AddEnt(e, 1, 1);
163 targ.velocity = trigger_push_calculatevelocity(org, RandomSelection_chosen_ent, this.height, targ);
167 targ.velocity = this.movedir;
170 UNSET_ONGROUND(targ);
173 if (targ.flags & FL_PROJECTILE)
175 targ.angles = vectoangles (targ.velocity);
176 switch(targ.move_movetype)
179 set_movetype(targ, MOVETYPE_TOSS);
182 case MOVETYPE_BOUNCEMISSILE:
183 set_movetype(targ, MOVETYPE_BOUNCE);
193 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
194 targ.oldvelocity = targ.velocity;
196 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
198 // flash when activated
199 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
200 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
201 this.pushltime = time + 0.2;
203 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
206 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
207 if(targ.(jumppadsused[i]) == this)
211 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
212 targ.jumppadcount = targ.jumppadcount + 1;
215 if(IS_REAL_CLIENT(targ))
218 centerprint(targ, this.message);
222 targ.lastteleporttime = time;
223 targ.lastteleport_origin = targ.origin;
227 animdecide_setaction(targ, ANIMACTION_JUMP, true);
230 targ.jumppadcount = 1;
232 // reset tracking of who pushed you into a hazard (for kill credit)
237 if(this.enemy.target)
238 SUB_UseTargets(this.enemy, targ, this);
240 if (targ.flags & FL_PROJECTILE)
242 targ.angles = vectoangles (targ.velocity);
243 targ.com_phys_gravity_factor = 1;
244 switch(targ.move_movetype)
247 set_movetype(targ, MOVETYPE_TOSS);
250 case MOVETYPE_BOUNCEMISSILE:
251 set_movetype(targ, MOVETYPE_BOUNCE);
255 UpdateCSQCProjectile(targ);
262 void trigger_push_touch(entity this, entity toucher)
264 if (this.active == ACTIVE_NOT)
268 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
271 EXACTTRIGGER_TOUCH(this, toucher);
273 noref bool success = jumppad_push(this, toucher);
276 if (success && (this.spawnflags & PUSH_ONCE))
278 settouch(this, func_null);
279 setthink(this, SUB_Remove);
280 this.nextthink = time;
286 void trigger_push_link(entity this);
287 void trigger_push_updatelink(entity this);
288 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
290 setorigin(tracetest_ent, org);
291 tracetoss(tracetest_ent, tracetest_ent);
297 // since tracetoss starting from jumppad's origin often fails when target
298 // is very close to real destination, start it directly from target's
300 vector ofs = '0 0 0';
301 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
304 tracetest_ent.velocity.z = 0;
305 setorigin(tracetest_ent, targ.origin + ofs);
306 tracetoss(tracetest_ent, tracetest_ent);
307 if (trace_startsolid && ofs.z)
309 setorigin(tracetest_ent, targ.origin + ofs / 2);
310 tracetoss(tracetest_ent, tracetest_ent);
311 if (trace_startsolid && ofs.z)
313 setorigin(tracetest_ent, targ.origin);
314 tracetoss(tracetest_ent, tracetest_ent);
315 if (trace_startsolid)
320 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
324 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
326 setorigin(tracetest_ent, org);
327 tracetoss(tracetest_ent, tracetest_ent);
331 if (trace_ent == item)
334 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
336 if (trace_ent == item)
344 vector trigger_push_get_start_point(entity this)
346 // 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;
352 float trigger_push_get_push_time(entity this, vector endpos)
354 vector org = trigger_push_get_start_point(this);
356 float grav = PHYS_GRAVITY(NULL);
358 entity t = this.enemy;
362 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
363 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
364 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
365 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
367 return (v.z + v2.z) / grav;
369 else if (!(this.target && this.target != ""))
373 vector v = this.movedir;
375 float t = v.z / grav;
376 float jump_height = 1/2 * grav * (t ** 2);
377 float remaining_height = org.z + jump_height - endpos.z;
378 float v2_z = sqrt(2 * grav * remaining_height);
380 return (v.z + v2_z) / grav;
387 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
388 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
389 bool trigger_push_test(entity this, entity item)
392 vector org = trigger_push_get_start_point(this);
399 vector vel = '0 0 0';
401 for(entity t = NULL; (t = find(t, targetname, this.target)); )
405 if(t.move_movetype != MOVETYPE_NONE)
408 // bots can't tell teamed jumppads from normal ones
413 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
414 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
415 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
418 vector best_target = '0 0 0';
419 vector best_org = '0 0 0';
420 vector best_vel = '0 0 0';
421 bool valid_best_target = false;
424 if (!trigger_push_testorigin_for_item(e, item, org))
432 if (trigger_push_testorigin(e, t, this, org))
434 best_target = trace_endpos;
436 best_vel = e.velocity;
437 valid_best_target = true;
442 vector dist = t.origin - org;
443 if (dist.x || dist.y) // if not perfectly vertical
445 // test trajectory with different starting points, sometimes the trajectory
446 // starting from the jumppad origin can't reach the real destination
447 // and destination waypoint ends up near the jumppad itself
448 vector flatdir = normalize(dist - eZ * dist.z);
449 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
453 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
456 if (!trigger_push_testorigin_for_item(e, item, new_org))
465 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
466 e.velocity = autocvar_sv_maxspeed * flatdir;
467 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
469 best_target = trace_endpos;
472 valid_best_target = true;
475 if (ofs && new_org != org - ofs)
488 if (valid_best_target)
490 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
492 float velxy = vlen(vec2(best_vel));
493 float cost = vlen(vec2(t.origin - best_org)) / velxy;
494 if(velxy < autocvar_sv_maxspeed)
495 velxy = autocvar_sv_maxspeed;
496 cost += vlen(vec2(best_target - t.origin)) / velxy;
497 waypoint_spawnforteleporter(this, best_target, cost, e);
511 objerror (this, "Jumppad with nonexistant target");
517 // exactly one dest - bots love that
519 this.enemy = find(NULL, targetname, this.target);
520 else // bots can't tell teamed jumppads from normal ones
525 // have to use random selection every single time
536 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
537 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
539 e.velocity = this.movedir;
543 bool r = (trace_ent == item);
547 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
548 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
555 defer(this, 0.1, trigger_push_updatelink);
560 void trigger_push_findtarget(entity this)
562 trigger_push_test(this, NULL);
566 float trigger_push_send(entity this, entity to, float sf)
568 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
570 WriteByte(MSG_ENTITY, this.team);
571 WriteInt24_t(MSG_ENTITY, this.spawnflags);
572 WriteByte(MSG_ENTITY, this.active);
573 WriteCoord(MSG_ENTITY, this.height);
575 trigger_common_write(this, true);
580 void trigger_push_updatelink(entity this)
582 this.SendFlags |= SF_TRIGGER_INIT;
585 void trigger_push_link(entity this)
587 trigger_link(this, trigger_push_send);
593 * target: target of jump
594 * height: the absolute value is the height of the highest point of the jump
595 * trajectory above the higher one of the player and the target.
596 * the sign indicates whether the highest point is INSIDE (positive)
597 * or OUTSIDE (negative) of the jump trajectory. General rule: use
598 * positive values for targets mounted on the floor, and use negative
599 * values to target a point on the ceiling.
600 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
602 spawnfunc(trigger_push)
608 this.active = ACTIVE_ACTIVE;
609 this.use = trigger_push_use;
610 settouch(this, trigger_push_touch);
615 this.movedir = this.movedir * this.speed * 10;
618 this.noise = "misc/jumppad.wav";
619 precache_sound (this.noise);
621 trigger_push_link(this); // link it now
623 IL_PUSH(g_jumppads, this);
625 // this must be called to spawn the teleport waypoints for bots
626 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
630 bool target_push_send(entity this, entity to, float sf)
632 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
634 WriteByte(MSG_ENTITY, this.cnt);
635 WriteString(MSG_ENTITY, this.targetname);
636 WriteVector(MSG_ENTITY, this.origin);
638 WriteAngleVector(MSG_ENTITY, this.angles);
643 void target_push_use(entity this, entity actor, entity trigger)
645 if(trigger.classname == "trigger_push" || trigger == this)
646 return; // WTF, why is this a thing
648 jumppad_push(this, actor);
651 void target_push_link(entity this)
653 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
654 Net_LinkEntity(this, false, 0, target_push_send);
655 //this.SendFlags |= 1; // update
658 void target_push_init(entity this)
660 this.mangle = this.angles;
661 setorigin(this, this.origin);
662 target_push_link(this);
665 void target_push_init2(entity this)
667 if(this.target && this.target != "") // we have an old style pusher!
669 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
670 this.use = target_push_use;
673 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
676 spawnfunc(target_push)
678 target_push_init2(this);
681 spawnfunc(info_notnull)
683 target_push_init(this);
685 spawnfunc(target_position)
687 target_push_init(this);
692 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
694 this.classname = "jumppad";
695 int mytm = ReadByte();
698 this.team = mytm - 1;
700 this.spawnflags = ReadInt24_t();
701 this.active = ReadByte();
702 this.height = ReadCoord();
704 trigger_common_read(this, true);
706 this.entremove = trigger_remove_generic;
707 this.solid = SOLID_TRIGGER;
708 settouch(this, trigger_push_touch);
709 this.move_time = time;
710 defer(this, 0.25, trigger_push_findtarget);
715 void target_push_remove(entity this)
717 // strfree(this.classname);
718 strfree(this.targetname);
721 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
723 this.classname = "push_target";
724 this.cnt = ReadByte();
725 this.targetname = strzone(ReadString());
726 this.origin = ReadVector();
728 this.angles = ReadAngleVector();
732 setorigin(this, this.origin);
734 this.drawmask = MASK_NORMAL;
735 this.entremove = target_push_remove;