2 // TODO: split target_push and put it in the target folder
5 #include <common/physics/movetypes/movetypes.qh>
7 void trigger_push_use(entity this, entity actor, entity trigger)
11 this.team = actor.team;
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
29 Returns: velocity for the jump
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
33 float grav, sdist, zdist, vs, vz, jumpheight;
36 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
38 grav = PHYS_GRAVITY(other);
39 if(PHYS_ENTGRAVITY(other))
40 grav *= PHYS_ENTGRAVITY(other);
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);
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);
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);
209 targ.lastteleporttime = time;
212 animdecide_setaction(targ, ANIMACTION_JUMP, true);
215 targ.jumppadcount = 1;
217 // reset tracking of who pushed you into a hazard (for kill credit)
222 if(this.enemy.target)
223 SUB_UseTargets(this.enemy, targ, this);
225 if (targ.flags & FL_PROJECTILE)
227 targ.angles = vectoangles (targ.velocity);
228 targ.com_phys_gravity_factor = 1;
229 switch(targ.move_movetype)
232 set_movetype(targ, MOVETYPE_TOSS);
235 case MOVETYPE_BOUNCEMISSILE:
236 set_movetype(targ, MOVETYPE_BOUNCE);
240 UpdateCSQCProjectile(targ);
247 void trigger_push_touch(entity this, entity toucher)
249 if (this.active == ACTIVE_NOT)
253 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
256 EXACTTRIGGER_TOUCH(this, toucher);
258 noref bool success = jumppad_push(this, toucher);
261 if (success && (this.spawnflags & PUSH_ONCE))
263 settouch(this, func_null);
264 setthink(this, SUB_Remove);
265 this.nextthink = time;
271 void trigger_push_link(entity this);
272 void trigger_push_updatelink(entity this);
273 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
275 setorigin(tracetest_ent, org);
276 tracetoss(tracetest_ent, tracetest_ent);
282 // since tracetoss starting from jumppad's origin often fails when target
283 // is very close to real destination, start it directly from target's
285 tracetest_ent.velocity.z = 0;
286 setorigin(tracetest_ent, targ.origin + stepheightvec);
287 tracetoss(tracetest_ent, tracetest_ent);
290 setorigin(tracetest_ent, targ.origin + stepheightvec / 2);
291 tracetoss(tracetest_ent, tracetest_ent);
294 setorigin(tracetest_ent, targ.origin);
295 tracetoss(tracetest_ent, tracetest_ent);
301 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
306 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
307 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
308 bool trigger_push_test(entity this, entity item)
310 // first calculate a typical start point for the jump
311 vector org = (this.absmin + this.absmax) * 0.5;
312 org.z = this.absmax.z - PL_MIN_CONST.z;
318 vector vel = '0 0 0';
320 for(entity t = NULL; (t = find(t, targetname, this.target)); )
324 if(t.move_movetype != MOVETYPE_NONE)
328 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
329 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
330 e.velocity = trigger_push_calculatevelocity(org, t, this.height);
336 bool r = (trace_ent == item);
342 vector best_target = '0 0 0';
343 vector best_org = '0 0 0';
344 vector best_vel = '0 0 0';
345 bool valid_best_target = false;
346 if (trigger_push_testorigin(e, t, this, org))
348 best_target = trace_endpos;
350 best_vel = e.velocity;
351 valid_best_target = true;
355 vector dist = t.origin - org;
356 if (dist.x || dist.y) // if not perfectly vertical
358 // test trajectory with different starting points, sometimes the trajectory
359 // starting from the jumppad origin can't reach the real destination
360 // and destination waypoint ends up near the jumppad itself
361 vector flatdir = normalize(dist - eZ * dist.z);
362 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
364 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
366 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
367 e.velocity = autocvar_sv_maxspeed * flatdir;
368 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
370 best_target = trace_endpos;
373 valid_best_target = true;
376 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
378 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
379 e.velocity = autocvar_sv_maxspeed * flatdir;
380 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
382 best_target = trace_endpos;
385 valid_best_target = true;
389 if (valid_best_target)
391 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
393 float velxy = vlen(vec2(best_vel));
394 float cost = vlen(vec2(t.origin - best_org)) / velxy;
395 if(velxy < autocvar_sv_maxspeed)
396 velxy = autocvar_sv_maxspeed;
397 cost += vlen(vec2(best_target - t.origin)) / velxy;
398 waypoint_spawnforteleporter(this, best_target, cost, e);
412 objerror (this, "Jumppad with nonexistant target");
418 // exactly one dest - bots love that
419 this.enemy = find(NULL, targetname, this.target);
423 // have to use random selection every single time
431 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
432 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
434 e.velocity = this.movedir;
438 bool r = (trace_ent == item);
442 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
443 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
447 defer(this, 0.1, trigger_push_updatelink);
452 void trigger_push_findtarget(entity this)
454 trigger_push_test(this, NULL);
458 float trigger_push_send(entity this, entity to, float sf)
460 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
462 WriteByte(MSG_ENTITY, this.team);
463 WriteInt24_t(MSG_ENTITY, this.spawnflags);
464 WriteByte(MSG_ENTITY, this.active);
465 WriteCoord(MSG_ENTITY, this.height);
467 WriteCoord(MSG_ENTITY, this.movedir_x);
468 WriteCoord(MSG_ENTITY, this.movedir_y);
469 WriteCoord(MSG_ENTITY, this.movedir_z);
471 trigger_common_write(this, true);
476 void trigger_push_updatelink(entity this)
481 void trigger_push_link(entity this)
483 trigger_link(this, trigger_push_send);
489 * target: target of jump
490 * height: the absolute value is the height of the highest point of the jump
491 * trajectory above the higher one of the player and the target.
492 * the sign indicates whether the highest point is INSIDE (positive)
493 * or OUTSIDE (negative) of the jump trajectory. General rule: use
494 * positive values for targets mounted on the floor, and use negative
495 * values to target a point on the ceiling.
496 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
498 spawnfunc(trigger_push)
504 this.active = ACTIVE_ACTIVE;
505 this.use = trigger_push_use;
506 settouch(this, trigger_push_touch);
511 this.movedir = this.movedir * this.speed * 10;
514 this.noise = "misc/jumppad.wav";
515 precache_sound (this.noise);
517 trigger_push_link(this); // link it now
519 IL_PUSH(g_jumppads, this);
521 // this must be called to spawn the teleport waypoints for bots
522 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
526 bool target_push_send(entity this, entity to, float sf)
528 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
530 WriteByte(MSG_ENTITY, this.cnt);
531 WriteString(MSG_ENTITY, this.targetname);
532 WriteCoord(MSG_ENTITY, this.origin_x);
533 WriteCoord(MSG_ENTITY, this.origin_y);
534 WriteCoord(MSG_ENTITY, this.origin_z);
536 WriteAngle(MSG_ENTITY, this.angles_x);
537 WriteAngle(MSG_ENTITY, this.angles_y);
538 WriteAngle(MSG_ENTITY, this.angles_z);
543 void target_push_use(entity this, entity actor, entity trigger)
545 if(trigger.classname == "trigger_push" || trigger == this)
546 return; // WTF, why is this a thing
548 jumppad_push(this, actor);
551 void target_push_link(entity this)
553 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
554 Net_LinkEntity(this, false, 0, target_push_send);
555 //this.SendFlags |= 1; // update
558 void target_push_init(entity this)
560 this.mangle = this.angles;
561 setorigin(this, this.origin);
562 target_push_link(this);
565 void target_push_init2(entity this)
567 if(this.target && this.target != "") // we have an old style pusher!
569 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
570 this.use = target_push_use;
573 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
576 spawnfunc(target_push) { target_push_init2(this); }
577 spawnfunc(info_notnull) { target_push_init(this); }
578 spawnfunc(target_position) { target_push_init(this); }
582 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
584 this.classname = "jumppad";
585 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
586 this.spawnflags = ReadInt24_t();
587 this.active = ReadByte();
588 this.height = ReadCoord();
590 this.movedir_x = ReadCoord();
591 this.movedir_y = ReadCoord();
592 this.movedir_z = ReadCoord();
594 trigger_common_read(this, true);
596 this.entremove = trigger_remove_generic;
597 this.solid = SOLID_TRIGGER;
598 settouch(this, trigger_push_touch);
599 this.move_time = time;
600 defer(this, 0.25, trigger_push_findtarget);
605 void target_push_remove(entity this)
608 //strunzone(this.classname);
609 //this.classname = string_null;
612 strunzone(this.targetname);
613 this.targetname = string_null;
616 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
618 this.classname = "push_target";
619 this.cnt = ReadByte();
620 this.targetname = strzone(ReadString());
621 this.origin_x = ReadCoord();
622 this.origin_y = ReadCoord();
623 this.origin_z = ReadCoord();
625 this.angles_x = ReadAngle();
626 this.angles_y = ReadAngle();
627 this.angles_z = ReadAngle();
631 setorigin(this, this.origin);
633 this.drawmask = MASK_NORMAL;
634 this.entremove = target_push_remove;